diff options
Diffstat (limited to 'crates/hir_ty')
-rw-r--r-- | crates/hir_ty/src/display.rs | 56 | ||||
-rw-r--r-- | crates/hir_ty/src/tests.rs | 8 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/method_resolution.rs | 40 |
3 files changed, 44 insertions, 60 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index ddfd8c8af..0bf181a92 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -84,26 +84,17 @@ pub trait HirDisplay { | |||
84 | } | 84 | } |
85 | 85 | ||
86 | /// Returns a String representation of `self` for test purposes | 86 | /// Returns a String representation of `self` for test purposes |
87 | fn display_test<'a>( | 87 | fn display_test<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self> |
88 | &'a self, | 88 | where |
89 | db: &'a dyn HirDatabase, | 89 | Self: Sized, |
90 | module_id: ModuleId, | 90 | { |
91 | ) -> Result<String, DisplaySourceCodeError> { | 91 | HirDisplayWrapper { |
92 | let mut result = String::new(); | ||
93 | match self.hir_fmt(&mut HirFormatter { | ||
94 | db, | 92 | db, |
95 | fmt: &mut result, | 93 | t: self, |
96 | buf: String::with_capacity(20), | ||
97 | curr_size: 0, | ||
98 | max_size: None, | 94 | max_size: None, |
99 | omit_verbose_types: false, | 95 | omit_verbose_types: false, |
100 | display_target: DisplayTarget::Test { module_id }, | 96 | display_target: DisplayTarget::Test, |
101 | }) { | 97 | } |
102 | Ok(()) => {} | ||
103 | Err(HirDisplayError::FmtError) => panic!("Writing to String can't fail!"), | ||
104 | Err(HirDisplayError::DisplaySourceCodeError(e)) => return Err(e), | ||
105 | }; | ||
106 | Ok(result) | ||
107 | } | 98 | } |
108 | } | 99 | } |
109 | 100 | ||
@@ -158,7 +149,7 @@ enum DisplayTarget { | |||
158 | /// The generated code should compile, so paths need to be qualified. | 149 | /// The generated code should compile, so paths need to be qualified. |
159 | SourceCode { module_id: ModuleId }, | 150 | SourceCode { module_id: ModuleId }, |
160 | /// Only for test purpose to keep real types | 151 | /// Only for test purpose to keep real types |
161 | Test { module_id: ModuleId }, | 152 | Test, |
162 | } | 153 | } |
163 | 154 | ||
164 | impl DisplayTarget { | 155 | impl DisplayTarget { |
@@ -166,7 +157,7 @@ impl DisplayTarget { | |||
166 | matches!(self, Self::SourceCode {..}) | 157 | matches!(self, Self::SourceCode {..}) |
167 | } | 158 | } |
168 | fn is_test(&self) -> bool { | 159 | fn is_test(&self) -> bool { |
169 | matches!(self, Self::Test {..}) | 160 | matches!(self, Self::Test) |
170 | } | 161 | } |
171 | } | 162 | } |
172 | 163 | ||
@@ -348,7 +339,7 @@ impl HirDisplay for ApplicationTy { | |||
348 | } | 339 | } |
349 | TypeCtor::Adt(def_id) => { | 340 | TypeCtor::Adt(def_id) => { |
350 | match f.display_target { | 341 | match f.display_target { |
351 | DisplayTarget::Diagnostics => { | 342 | DisplayTarget::Diagnostics | DisplayTarget::Test => { |
352 | let name = match def_id { | 343 | let name = match def_id { |
353 | AdtId::StructId(it) => f.db.struct_data(it).name.clone(), | 344 | AdtId::StructId(it) => f.db.struct_data(it).name.clone(), |
354 | AdtId::UnionId(it) => f.db.union_data(it).name.clone(), | 345 | AdtId::UnionId(it) => f.db.union_data(it).name.clone(), |
@@ -356,7 +347,7 @@ impl HirDisplay for ApplicationTy { | |||
356 | }; | 347 | }; |
357 | write!(f, "{}", name)?; | 348 | write!(f, "{}", name)?; |
358 | } | 349 | } |
359 | DisplayTarget::SourceCode { module_id } | DisplayTarget::Test { module_id } => { | 350 | DisplayTarget::SourceCode { module_id } => { |
360 | if let Some(path) = find_path::find_path( | 351 | if let Some(path) = find_path::find_path( |
361 | f.db.upcast(), | 352 | f.db.upcast(), |
362 | ItemInNs::Types(def_id.into()), | 353 | ItemInNs::Types(def_id.into()), |
@@ -417,28 +408,23 @@ impl HirDisplay for ApplicationTy { | |||
417 | _ => panic!("not an associated type"), | 408 | _ => panic!("not an associated type"), |
418 | }; | 409 | }; |
419 | let trait_ = f.db.trait_data(trait_); | 410 | let trait_ = f.db.trait_data(trait_); |
420 | let type_alias = f.db.type_alias_data(type_alias); | 411 | let type_alias_data = f.db.type_alias_data(type_alias); |
421 | 412 | ||
422 | // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) | 413 | // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) |
423 | if f.display_target.is_test() || self.parameters.len() > 1 { | 414 | if f.display_target.is_test() { |
424 | write!(f, "{}::{}", trait_.name, type_alias.name)?; | 415 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; |
425 | if self.parameters.len() > 0 { | 416 | if self.parameters.len() > 0 { |
426 | write!(f, "<")?; | 417 | write!(f, "<")?; |
427 | f.write_joined(&*self.parameters.0, ", ")?; | 418 | f.write_joined(&*self.parameters.0, ", ")?; |
428 | write!(f, ">")?; | 419 | write!(f, ">")?; |
429 | } | 420 | } |
430 | } else { | 421 | } else { |
431 | if self.parameters.len() == 1 { | 422 | let projection_ty = ProjectionTy { |
432 | write!( | 423 | associated_ty: type_alias, |
433 | f, | 424 | parameters: self.parameters.clone(), |
434 | "<{} as {}>::{}", | 425 | }; |
435 | self.parameters.as_single().display(f.db), | 426 | |
436 | trait_.name, | 427 | projection_ty.hir_fmt(f)?; |
437 | type_alias.name | ||
438 | )?; | ||
439 | } else { | ||
440 | write!(f, "{}::{}", trait_.name, type_alias.name)?; | ||
441 | } | ||
442 | } | 428 | } |
443 | } | 429 | } |
444 | TypeCtor::ForeignType(type_alias) => { | 430 | TypeCtor::ForeignType(type_alias) => { |
diff --git a/crates/hir_ty/src/tests.rs b/crates/hir_ty/src/tests.rs index 510baadf2..29b178ec1 100644 --- a/crates/hir_ty/src/tests.rs +++ b/crates/hir_ty/src/tests.rs | |||
@@ -157,14 +157,13 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { | |||
157 | (node.value.text_range(), node.value.text().to_string().replace("\n", " ")) | 157 | (node.value.text_range(), node.value.text().to_string().replace("\n", " ")) |
158 | }; | 158 | }; |
159 | let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" }; | 159 | let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" }; |
160 | let module = db.module_for_file(node.file_id.original_file(&db)); | ||
161 | format_to!( | 160 | format_to!( |
162 | buf, | 161 | buf, |
163 | "{}{:?} '{}': {}\n", | 162 | "{}{:?} '{}': {}\n", |
164 | macro_prefix, | 163 | macro_prefix, |
165 | range, | 164 | range, |
166 | ellipsize(text, 15), | 165 | ellipsize(text, 15), |
167 | ty.display_test(&db, module).unwrap() | 166 | ty.display_test(&db) |
168 | ); | 167 | ); |
169 | } | 168 | } |
170 | if include_mismatches { | 169 | if include_mismatches { |
@@ -175,14 +174,13 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String { | |||
175 | for (src_ptr, mismatch) in &mismatches { | 174 | for (src_ptr, mismatch) in &mismatches { |
176 | let range = src_ptr.value.text_range(); | 175 | let range = src_ptr.value.text_range(); |
177 | let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" }; | 176 | let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" }; |
178 | let module = db.module_for_file(src_ptr.file_id.original_file(&db)); | ||
179 | format_to!( | 177 | format_to!( |
180 | buf, | 178 | buf, |
181 | "{}{:?}: expected {}, got {}\n", | 179 | "{}{:?}: expected {}, got {}\n", |
182 | macro_prefix, | 180 | macro_prefix, |
183 | range, | 181 | range, |
184 | mismatch.expected.display_test(&db, module).unwrap(), | 182 | mismatch.expected.display_test(&db), |
185 | mismatch.actual.display_test(&db, module).unwrap(), | 183 | mismatch.actual.display_test(&db), |
186 | ); | 184 | ); |
187 | } | 185 | } |
188 | } | 186 | } |
diff --git a/crates/hir_ty/src/tests/method_resolution.rs b/crates/hir_ty/src/tests/method_resolution.rs index 596d4f182..0f17ff151 100644 --- a/crates/hir_ty/src/tests/method_resolution.rs +++ b/crates/hir_ty/src/tests/method_resolution.rs | |||
@@ -108,16 +108,16 @@ fn infer_associated_method_with_modules() { | |||
108 | check_infer( | 108 | check_infer( |
109 | r#" | 109 | r#" |
110 | mod a { | 110 | mod a { |
111 | pub struct A; | 111 | struct A; |
112 | impl A { pub fn thing() -> A { A {} }} | 112 | impl A { pub fn thing() -> A { A {} }} |
113 | } | 113 | } |
114 | 114 | ||
115 | mod b { | 115 | mod b { |
116 | pub struct B; | 116 | struct B; |
117 | impl B { pub fn thing() -> u32 { 99 }} | 117 | impl B { pub fn thing() -> u32 { 99 }} |
118 | 118 | ||
119 | pub mod c { | 119 | mod c { |
120 | pub struct C; | 120 | struct C; |
121 | impl C { pub fn thing() -> C { C {} }} | 121 | impl C { pub fn thing() -> C { C {} }} |
122 | } | 122 | } |
123 | } | 123 | } |
@@ -130,22 +130,22 @@ fn infer_associated_method_with_modules() { | |||
130 | } | 130 | } |
131 | "#, | 131 | "#, |
132 | expect![[r#" | 132 | expect![[r#" |
133 | 59..67 '{ A {} }': a::A | 133 | 55..63 '{ A {} }': A |
134 | 61..65 'A {}': a::A | 134 | 57..61 'A {}': A |
135 | 133..139 '{ 99 }': u32 | 135 | 125..131 '{ 99 }': u32 |
136 | 135..137 '99': u32 | 136 | 127..129 '99': u32 |
137 | 217..225 '{ C {} }': c::C | 137 | 201..209 '{ C {} }': C |
138 | 219..223 'C {}': c::C | 138 | 203..207 'C {}': C |
139 | 256..340 '{ ...g(); }': () | 139 | 240..324 '{ ...g(); }': () |
140 | 266..267 'x': a::A | 140 | 250..251 'x': A |
141 | 270..281 'a::A::thing': fn thing() -> A | 141 | 254..265 'a::A::thing': fn thing() -> A |
142 | 270..283 'a::A::thing()': a::A | 142 | 254..267 'a::A::thing()': A |
143 | 293..294 'y': u32 | 143 | 277..278 'y': u32 |
144 | 297..308 'b::B::thing': fn thing() -> u32 | 144 | 281..292 'b::B::thing': fn thing() -> u32 |
145 | 297..310 'b::B::thing()': u32 | 145 | 281..294 'b::B::thing()': u32 |
146 | 320..321 'z': c::C | 146 | 304..305 'z': C |
147 | 324..335 'c::C::thing': fn thing() -> C | 147 | 308..319 'c::C::thing': fn thing() -> C |
148 | 324..337 'c::C::thing()': c::C | 148 | 308..321 'c::C::thing()': C |
149 | "#]], | 149 | "#]], |
150 | ); | 150 | ); |
151 | } | 151 | } |