From 3a9a0bc968d9bb97c80f18b4323b3ad75cc8bbad Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 14 Jul 2019 14:19:00 +0200 Subject: Add another test for assoc type resolution --- crates/ra_hir/src/ty/tests.rs | 52 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src/ty/tests.rs') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index d5f7a4d25..9d412ff61 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -2508,15 +2508,55 @@ struct S; impl Iterable for S { type Item = u32; } fn test() { let x: ::Item = 1; - let y: T::Item = no_matter; + let y: ::Item = no_matter; + let z: T::Item = no_matter; } "#), @r###" -[108; 181) '{ ...ter; }': () -[118; 119) 'x': i32 -[145; 146) '1': i32 -[156; 157) 'y': {unknown} -[169; 178) 'no_matter': {unknown}"### + ⋮ + ⋮[108; 227) '{ ...ter; }': () + ⋮[118; 119) 'x': i32 + ⋮[145; 146) '1': i32 + ⋮[156; 157) 'y': {unknown} + ⋮[183; 192) 'no_matter': {unknown} + ⋮[202; 203) 'z': {unknown} + ⋮[215; 224) 'no_matter': {unknown} + "### + ); +} + +#[test] +fn infer_return_associated_type() { + assert_snapshot_matches!( + infer(r#" +trait Iterable { + type Item; +} +struct S; +impl Iterable for S { type Item = u32; } +fn foo1(t: T) -> T::Item {} +fn foo2(t: T) -> ::Item {} +fn test() { + let x = foo1(S); + let y = foo2(S); +} +"#), + @r###" + ⋮ + ⋮[106; 107) 't': T + ⋮[123; 125) '{}': () + ⋮[147; 148) 't': T + ⋮[178; 180) '{}': () + ⋮[191; 236) '{ ...(S); }': () + ⋮[201; 202) 'x': {unknown} + ⋮[205; 209) 'foo1': fn foo1(T) -> {unknown} + ⋮[205; 212) 'foo1(S)': {unknown} + ⋮[210; 211) 'S': S + ⋮[222; 223) 'y': {unknown} + ⋮[226; 230) 'foo2': fn foo2(T) -> {unknown} + ⋮[226; 233) 'foo2(S)': {unknown} + ⋮[231; 232) 'S': S + "### ); } -- cgit v1.2.3 From 22724f37f3ae73983bf700d10d80a8dbd4fa4073 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 5 Aug 2019 22:42:38 +0200 Subject: Lower fully qualified associated type paths I.e. `::Foo`. --- crates/ra_hir/src/ty/tests.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_hir/src/ty/tests.rs') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 9d412ff61..01b358335 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -2515,10 +2515,10 @@ fn test() { @r###" ⋮ ⋮[108; 227) '{ ...ter; }': () - ⋮[118; 119) 'x': i32 - ⋮[145; 146) '1': i32 - ⋮[156; 157) 'y': {unknown} - ⋮[183; 192) 'no_matter': {unknown} + ⋮[118; 119) 'x': ::Item + ⋮[145; 146) '1': ::Item + ⋮[156; 157) 'y': ::Item + ⋮[183; 192) 'no_matter': ::Item ⋮[202; 203) 'z': {unknown} ⋮[215; 224) 'no_matter': {unknown} "### @@ -2552,9 +2552,9 @@ fn test() { ⋮[205; 209) 'foo1': fn foo1(T) -> {unknown} ⋮[205; 212) 'foo1(S)': {unknown} ⋮[210; 211) 'S': S - ⋮[222; 223) 'y': {unknown} - ⋮[226; 230) 'foo2': fn foo2(T) -> {unknown} - ⋮[226; 233) 'foo2(S)': {unknown} + ⋮[222; 223) 'y': ::Item + ⋮[226; 230) 'foo2': fn foo2(T) -> ::Item + ⋮[226; 233) 'foo2(S)': ::Item ⋮[231; 232) 'S': S "### ); -- cgit v1.2.3 From 9d72b14cfe3606e9c55e488d74568471f981bb3d Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 11 Aug 2019 13:52:34 +0200 Subject: Normalize assoc types in more places --- crates/ra_hir/src/ty/tests.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src/ty/tests.rs') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 01b358335..e6a09cc67 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -2515,10 +2515,10 @@ fn test() { @r###" ⋮ ⋮[108; 227) '{ ...ter; }': () - ⋮[118; 119) 'x': ::Item - ⋮[145; 146) '1': ::Item - ⋮[156; 157) 'y': ::Item - ⋮[183; 192) 'no_matter': ::Item + ⋮[118; 119) 'x': u32 + ⋮[145; 146) '1': u32 + ⋮[156; 157) 'y': {unknown} + ⋮[183; 192) 'no_matter': {unknown} ⋮[202; 203) 'z': {unknown} ⋮[215; 224) 'no_matter': {unknown} "### @@ -2552,9 +2552,9 @@ fn test() { ⋮[205; 209) 'foo1': fn foo1(T) -> {unknown} ⋮[205; 212) 'foo1(S)': {unknown} ⋮[210; 211) 'S': S - ⋮[222; 223) 'y': ::Item + ⋮[222; 223) 'y': u32 ⋮[226; 230) 'foo2': fn foo2(T) -> ::Item - ⋮[226; 233) 'foo2(S)': ::Item + ⋮[226; 233) 'foo2(S)': u32 ⋮[231; 232) 'S': S "### ); -- cgit v1.2.3 From 5af9691dc9132db61b50c4e90cdeda6fea0c5dd9 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 11 Aug 2019 15:54:31 +0200 Subject: Handle placeholder assoc types when Chalk produces them --- crates/ra_hir/src/ty/tests.rs | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'crates/ra_hir/src/ty/tests.rs') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index e6a09cc67..28727bb18 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -3181,6 +3181,55 @@ fn test(t: T) { (*t)<|>; } assert_eq!(t, "i128"); } +#[test] +fn associated_type_placeholder() { + let t = type_at( + r#" +//- /main.rs +pub trait ApplyL { + type Out; +} + +pub struct RefMutL; + +impl ApplyL for RefMutL { + type Out = ::Out; +} + +fn test() { + let y: as ApplyL>::Out = no_matter; + y<|>; +} +"#, + ); + // inside the generic function, the associated type gets normalized to a placeholder `ApplL::Out` [https://rust-lang.github.io/rustc-guide/traits/associated-types.html#placeholder-associated-types]. + // FIXME: fix type parameter names going missing when going through Chalk + assert_eq!(t, "ApplyL::Out<[missing name]>"); +} + +#[test] +fn associated_type_placeholder_2() { + let t = type_at( + r#" +//- /main.rs +pub trait ApplyL { + type Out; +} +fn foo(t: T) -> ::Out; + +fn test(t: T) { + let y = foo(t); + y<|>; +} +"#, + ); + // FIXME here Chalk doesn't normalize the type to a placeholder. I think we + // need to add a rule like Normalize(::Out -> ApplyL::Out) + // to the trait env ourselves here; probably Chalk can't do this by itself. + // assert_eq!(t, "ApplyL::Out<[missing name]>"); + assert_eq!(t, "{unknown}"); +} + fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { let file = db.parse(pos.file_id).ok().unwrap(); let expr = algo::find_node_at_offset::(file.syntax(), pos.offset).unwrap(); -- cgit v1.2.3