diff options
Diffstat (limited to 'crates/hir/src/source_analyzer.rs')
-rw-r--r-- | crates/hir/src/source_analyzer.rs | 63 |
1 files changed, 47 insertions, 16 deletions
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index 37d162b32..847d2537d 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs | |||
@@ -20,7 +20,7 @@ use hir_def::{ | |||
20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; | 20 | use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; |
21 | use hir_ty::{ | 21 | use hir_ty::{ |
22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, | 22 | diagnostics::{record_literal_missing_fields, record_pattern_missing_fields}, |
23 | InferenceResult, Substitution, | 23 | InferenceResult, Interner, Substitution, TyExt, TyLoweringContext, |
24 | }; | 24 | }; |
25 | use syntax::{ | 25 | use syntax::{ |
26 | ast::{self, AstNode}, | 26 | ast::{self, AstNode}, |
@@ -161,14 +161,15 @@ impl SourceAnalyzer { | |||
161 | db: &dyn HirDatabase, | 161 | db: &dyn HirDatabase, |
162 | field: &ast::RecordExprField, | 162 | field: &ast::RecordExprField, |
163 | ) -> Option<(Field, Option<Local>)> { | 163 | ) -> Option<(Field, Option<Local>)> { |
164 | let expr_id = | 164 | let record_expr = ast::RecordExpr::cast(field.syntax().parent().and_then(|p| p.parent())?)?; |
165 | self.body_source_map.as_ref()?.node_field(InFile::new(self.file_id, field))?; | 165 | let expr = ast::Expr::from(record_expr); |
166 | let expr_id = self.body_source_map.as_ref()?.node_expr(InFile::new(self.file_id, &expr))?; | ||
166 | 167 | ||
168 | let local_name = field.field_name()?.as_name(); | ||
167 | let local = if field.name_ref().is_some() { | 169 | let local = if field.name_ref().is_some() { |
168 | None | 170 | None |
169 | } else { | 171 | } else { |
170 | let local_name = field.field_name()?.as_name(); | 172 | let path = ModPath::from_segments(PathKind::Plain, once(local_name.clone())); |
171 | let path = ModPath::from_segments(PathKind::Plain, once(local_name)); | ||
172 | match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) { | 173 | match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) { |
173 | Some(ValueNs::LocalBinding(pat_id)) => { | 174 | Some(ValueNs::LocalBinding(pat_id)) => { |
174 | Some(Local { pat_id, parent: self.resolver.body_owner()? }) | 175 | Some(Local { pat_id, parent: self.resolver.body_owner()? }) |
@@ -176,18 +177,24 @@ impl SourceAnalyzer { | |||
176 | _ => None, | 177 | _ => None, |
177 | } | 178 | } |
178 | }; | 179 | }; |
179 | let struct_field = self.infer.as_ref()?.record_field_resolution(expr_id)?; | 180 | let variant = self.infer.as_ref()?.variant_resolution_for_expr(expr_id)?; |
180 | Some((struct_field.into(), local)) | 181 | let variant_data = variant.variant_data(db.upcast()); |
182 | let field = FieldId { parent: variant, local_id: variant_data.field(&local_name)? }; | ||
183 | Some((field.into(), local)) | ||
181 | } | 184 | } |
182 | 185 | ||
183 | pub(crate) fn resolve_record_pat_field( | 186 | pub(crate) fn resolve_record_pat_field( |
184 | &self, | 187 | &self, |
185 | _db: &dyn HirDatabase, | 188 | db: &dyn HirDatabase, |
186 | field: &ast::RecordPatField, | 189 | field: &ast::RecordPatField, |
187 | ) -> Option<Field> { | 190 | ) -> Option<Field> { |
188 | let pat_id = self.pat_id(&field.pat()?)?; | 191 | let field_name = field.field_name()?.as_name(); |
189 | let struct_field = self.infer.as_ref()?.record_pat_field_resolution(pat_id)?; | 192 | let record_pat = ast::RecordPat::cast(field.syntax().parent().and_then(|p| p.parent())?)?; |
190 | Some(struct_field.into()) | 193 | let pat_id = self.pat_id(&record_pat.into())?; |
194 | let variant = self.infer.as_ref()?.variant_resolution_for_pat(pat_id)?; | ||
195 | let variant_data = variant.variant_data(db.upcast()); | ||
196 | let field = FieldId { parent: variant, local_id: variant_data.field(&field_name)? }; | ||
197 | Some(field.into()) | ||
191 | } | 198 | } |
192 | 199 | ||
193 | pub(crate) fn resolve_macro_call( | 200 | pub(crate) fn resolve_macro_call( |
@@ -299,7 +306,7 @@ impl SourceAnalyzer { | |||
299 | let infer = self.infer.as_ref()?; | 306 | let infer = self.infer.as_ref()?; |
300 | 307 | ||
301 | let expr_id = self.expr_id(db, &literal.clone().into())?; | 308 | let expr_id = self.expr_id(db, &literal.clone().into())?; |
302 | let substs = infer.type_of_expr[expr_id].substs()?; | 309 | let substs = infer.type_of_expr[expr_id].as_adt()?.1; |
303 | 310 | ||
304 | let (variant, missing_fields, _exhaustive) = | 311 | let (variant, missing_fields, _exhaustive) = |
305 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; | 312 | record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?; |
@@ -317,7 +324,7 @@ impl SourceAnalyzer { | |||
317 | let infer = self.infer.as_ref()?; | 324 | let infer = self.infer.as_ref()?; |
318 | 325 | ||
319 | let pat_id = self.pat_id(&pattern.clone().into())?; | 326 | let pat_id = self.pat_id(&pattern.clone().into())?; |
320 | let substs = infer.type_of_pat[pat_id].substs()?; | 327 | let substs = infer.type_of_pat[pat_id].as_adt()?.1; |
321 | 328 | ||
322 | let (variant, missing_fields, _exhaustive) = | 329 | let (variant, missing_fields, _exhaustive) = |
323 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; | 330 | record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?; |
@@ -339,7 +346,7 @@ impl SourceAnalyzer { | |||
339 | .into_iter() | 346 | .into_iter() |
340 | .map(|local_id| { | 347 | .map(|local_id| { |
341 | let field = FieldId { parent: variant, local_id }; | 348 | let field = FieldId { parent: variant, local_id }; |
342 | let ty = field_types[local_id].clone().subst(substs); | 349 | let ty = field_types[local_id].clone().substitute(&Interner, substs); |
343 | (field.into(), Type::new_with_resolver_inner(db, krate, &self.resolver, ty)) | 350 | (field.into(), Type::new_with_resolver_inner(db, krate, &self.resolver, ty)) |
344 | }) | 351 | }) |
345 | .collect() | 352 | .collect() |
@@ -466,7 +473,21 @@ fn resolve_hir_path_( | |||
466 | prefer_value_ns: bool, | 473 | prefer_value_ns: bool, |
467 | ) -> Option<PathResolution> { | 474 | ) -> Option<PathResolution> { |
468 | let types = || { | 475 | let types = || { |
469 | resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty { | 476 | let (ty, unresolved) = match path.type_anchor() { |
477 | Some(type_ref) => { | ||
478 | let (_, res) = TyLoweringContext::new(db, resolver).lower_ty_ext(type_ref); | ||
479 | res.map(|ty_ns| (ty_ns, path.segments().first())) | ||
480 | } | ||
481 | None => { | ||
482 | let (ty, remaining) = | ||
483 | resolver.resolve_path_in_type_ns(db.upcast(), path.mod_path())?; | ||
484 | match remaining { | ||
485 | Some(remaining) if remaining > 1 => None, | ||
486 | _ => Some((ty, path.segments().get(1))), | ||
487 | } | ||
488 | } | ||
489 | }?; | ||
490 | let res = match ty { | ||
470 | TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), | 491 | TypeNs::SelfType(it) => PathResolution::SelfType(it.into()), |
471 | TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), | 492 | TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }), |
472 | TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { | 493 | TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => { |
@@ -476,7 +497,17 @@ fn resolve_hir_path_( | |||
476 | TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), | 497 | TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()), |
477 | TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), | 498 | TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()), |
478 | TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), | 499 | TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()), |
479 | }) | 500 | }; |
501 | match unresolved { | ||
502 | Some(unresolved) => res | ||
503 | .assoc_type_shorthand_candidates(db, |name, alias| { | ||
504 | (name == unresolved.name).then(|| alias) | ||
505 | }) | ||
506 | .map(TypeAlias::from) | ||
507 | .map(Into::into) | ||
508 | .map(PathResolution::Def), | ||
509 | None => Some(res), | ||
510 | } | ||
480 | }; | 511 | }; |
481 | 512 | ||
482 | let body_owner = resolver.body_owner(); | 513 | let body_owner = resolver.body_owner(); |