From 5ca71a19903cea277ed8a347b36cffeca6b99922 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 13 Jun 2021 13:00:34 +0200 Subject: Make block-local trait impls work As long as either the trait or the implementing type are defined in the same block. --- crates/hir_ty/src/tests/traits.rs | 67 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'crates/hir_ty/src/tests') diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 588f0d1d4..6bcede4c4 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -3740,3 +3740,70 @@ mod future { "#, ); } + +#[test] +fn local_impl_1() { + check_types( + r#" +trait Trait { + fn foo(&self) -> T; +} + +fn test() { + struct S; + impl Trait for S { + fn foo(&self) { 0 } + } + + S.foo(); + // ^^^^^^^ u32 +} +"#, + ); +} + +#[test] +fn local_impl_2() { + check_types( + r#" +struct S; + +fn test() { + trait Trait { + fn foo(&self) -> T; + } + impl Trait for S { + fn foo(&self) { 0 } + } + + S.foo(); + // ^^^^^^^ u32 +} +"#, + ); +} + +#[test] +fn local_impl_3() { + check_types( + r#" +trait Trait { + fn foo(&self) -> T; +} + +fn test() { + struct S1; + { + struct S2; + + impl Trait for S2 { + fn foo(&self) { S1 } + } + + S2.foo(); + // ^^^^^^^^ S1 + } +} +"#, + ); +} -- cgit v1.2.3