aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/hir/src/source_analyzer.rs12
-rw-r--r--crates/hir_def/src/attr.rs9
-rw-r--r--crates/hir_ty/src/display.rs49
-rw-r--r--crates/hir_ty/src/infer.rs6
-rw-r--r--crates/hir_ty/src/infer/pat.rs6
5 files changed, 30 insertions, 52 deletions
diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs
index c013e78d9..ce6f3c008 100644
--- a/crates/hir/src/source_analyzer.rs
+++ b/crates/hir/src/source_analyzer.rs
@@ -185,12 +185,16 @@ impl SourceAnalyzer {
185 185
186 pub(crate) fn resolve_record_pat_field( 186 pub(crate) fn resolve_record_pat_field(
187 &self, 187 &self,
188 _db: &dyn HirDatabase, 188 db: &dyn HirDatabase,
189 field: &ast::RecordPatField, 189 field: &ast::RecordPatField,
190 ) -> Option<Field> { 190 ) -> Option<Field> {
191 let pat_id = self.pat_id(&field.pat()?)?; 191 let field_name = field.field_name()?.as_name();
192 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())?)?;
193 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())
194 } 198 }
195 199
196 pub(crate) fn resolve_macro_call( 200 pub(crate) fn resolve_macro_call(
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index ab77d924a..8a25b8731 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -215,12 +215,11 @@ impl Attrs {
215 let mut res = ArenaMap::default(); 215 let mut res = ArenaMap::default();
216 216
217 for (id, fld) in src.value.iter() { 217 for (id, fld) in src.value.iter() {
218 let attrs = match fld { 218 let owner: &dyn AttrsOwner = match fld {
219 Either::Left(_tuple) => Attrs::default(), 219 Either::Left(tuple) => tuple,
220 Either::Right(record) => { 220 Either::Right(record) => record,
221 RawAttrs::from_attrs_owner(db, src.with_value(record)).filter(db, krate)
222 }
223 }; 221 };
222 let attrs = RawAttrs::from_attrs_owner(db, src.with_value(owner)).filter(db, krate);
224 223
225 res.insert(id, attrs); 224 res.insert(id, attrs);
226 } 225 }
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index 8fe4ed3fa..9e6bbcdf1 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -51,6 +51,10 @@ pub trait HirDisplay {
51 where 51 where
52 Self: Sized, 52 Self: Sized,
53 { 53 {
54 assert!(
55 !matches!(display_target, DisplayTarget::SourceCode { .. }),
56 "HirDisplayWrapper cannot fail with DisplaySourceCodeError, use HirDisplay::hir_fmt directly instead"
57 );
54 HirDisplayWrapper { db, t: self, max_size, omit_verbose_types, display_target } 58 HirDisplayWrapper { db, t: self, max_size, omit_verbose_types, display_target }
55 } 59 }
56 60
@@ -235,7 +239,7 @@ where
235 Err(HirDisplayError::FmtError) => Err(fmt::Error), 239 Err(HirDisplayError::FmtError) => Err(fmt::Error),
236 Err(HirDisplayError::DisplaySourceCodeError(_)) => { 240 Err(HirDisplayError::DisplaySourceCodeError(_)) => {
237 // This should never happen 241 // This should never happen
238 panic!("HirDisplay failed when calling Display::fmt!") 242 panic!("HirDisplay::hir_fmt failed with DisplaySourceCodeError when calling Display::fmt!")
239 } 243 }
240 } 244 }
241 } 245 }
@@ -256,13 +260,9 @@ impl HirDisplay for ProjectionTy {
256 } 260 }
257 261
258 let trait_ = f.db.trait_data(self.trait_(f.db)); 262 let trait_ = f.db.trait_data(self.trait_(f.db));
259 let first_parameter = self.self_type_parameter(&Interner).into_displayable( 263 write!(f, "<")?;
260 f.db, 264 self.self_type_parameter(&Interner).hir_fmt(f)?;
261 f.max_size, 265 write!(f, " as {}", trait_.name)?;
262 f.omit_verbose_types,
263 f.display_target,
264 );
265 write!(f, "<{} as {}", first_parameter, trait_.name)?;
266 if self.substitution.len(&Interner) > 1 { 266 if self.substitution.len(&Interner) > 1 {
267 write!(f, "<")?; 267 write!(f, "<")?;
268 f.write_joined(&self.substitution.interned()[1..], ", ")?; 268 f.write_joined(&self.substitution.interned()[1..], ", ")?;
@@ -341,9 +341,6 @@ impl HirDisplay for Ty {
341 write!(f, "]")?; 341 write!(f, "]")?;
342 } 342 }
343 TyKind::Raw(m, t) | TyKind::Ref(m, _, t) => { 343 TyKind::Raw(m, t) | TyKind::Ref(m, _, t) => {
344 let ty_display =
345 t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target);
346
347 if matches!(self.kind(&Interner), TyKind::Raw(..)) { 344 if matches!(self.kind(&Interner), TyKind::Raw(..)) {
348 write!( 345 write!(
349 f, 346 f,
@@ -398,16 +395,16 @@ impl HirDisplay for Ty {
398 if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_) 395 if fn_traits(f.db.upcast(), trait_).any(|it| it == trait_)
399 && predicates.len() <= 2 396 && predicates.len() <= 2
400 { 397 {
401 return write!(f, "{}", ty_display); 398 return t.hir_fmt(f);
402 } 399 }
403 } 400 }
404 401
405 if predicates.len() > 1 { 402 if predicates.len() > 1 {
406 write!(f, "(")?; 403 write!(f, "(")?;
407 write!(f, "{}", ty_display)?; 404 t.hir_fmt(f)?;
408 write!(f, ")")?; 405 write!(f, ")")?;
409 } else { 406 } else {
410 write!(f, "{}", ty_display)?; 407 t.hir_fmt(f)?;
411 } 408 }
412 } 409 }
413 TyKind::Tuple(_, substs) => { 410 TyKind::Tuple(_, substs) => {
@@ -454,14 +451,8 @@ impl HirDisplay for Ty {
454 write!(f, ")")?; 451 write!(f, ")")?;
455 let ret = sig.ret(); 452 let ret = sig.ret();
456 if !ret.is_unit() { 453 if !ret.is_unit() {
457 let ret_display = ret.into_displayable( 454 write!(f, " -> ")?;
458 f.db, 455 ret.hir_fmt(f)?;
459 f.max_size,
460 f.omit_verbose_types,
461 f.display_target,
462 );
463
464 write!(f, " -> {}", ret_display)?;
465 } 456 }
466 } 457 }
467 TyKind::Adt(AdtId(def_id), parameters) => { 458 TyKind::Adt(AdtId(def_id), parameters) => {
@@ -603,13 +594,8 @@ impl HirDisplay for Ty {
603 write!(f, "|")?; 594 write!(f, "|")?;
604 }; 595 };
605 596
606 let ret_display = sig.ret().into_displayable( 597 write!(f, " -> ")?;
607 f.db, 598 sig.ret().hir_fmt(f)?;
608 f.max_size,
609 f.omit_verbose_types,
610 f.display_target,
611 );
612 write!(f, " -> {}", ret_display)?;
613 } else { 599 } else {
614 write!(f, "{{closure}}")?; 600 write!(f, "{{closure}}")?;
615 } 601 }
@@ -697,9 +683,8 @@ impl HirDisplay for CallableSig {
697 write!(f, ")")?; 683 write!(f, ")")?;
698 let ret = self.ret(); 684 let ret = self.ret();
699 if !ret.is_unit() { 685 if !ret.is_unit() {
700 let ret_display = 686 write!(f, " -> ")?;
701 ret.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); 687 ret.hir_fmt(f)?;
702 write!(f, " -> {}", ret_display)?;
703 } 688 }
704 Ok(()) 689 Ok(())
705 } 690 }
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index c63878e7a..efe9198cc 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -131,8 +131,7 @@ pub struct InferenceResult {
131 method_resolutions: FxHashMap<ExprId, FunctionId>, 131 method_resolutions: FxHashMap<ExprId, FunctionId>,
132 /// For each field access expr, records the field it resolves to. 132 /// For each field access expr, records the field it resolves to.
133 field_resolutions: FxHashMap<ExprId, FieldId>, 133 field_resolutions: FxHashMap<ExprId, FieldId>,
134 record_pat_field_resolutions: FxHashMap<PatId, FieldId>, 134 /// For each struct literal or pattern, records the variant it resolves to.
135 /// For each struct literal, records the variant it resolves to.
136 variant_resolutions: FxHashMap<ExprOrPatId, VariantId>, 135 variant_resolutions: FxHashMap<ExprOrPatId, VariantId>,
137 /// For each associated item record what it resolves to 136 /// For each associated item record what it resolves to
138 assoc_resolutions: FxHashMap<ExprOrPatId, AssocItemId>, 137 assoc_resolutions: FxHashMap<ExprOrPatId, AssocItemId>,
@@ -151,9 +150,6 @@ impl InferenceResult {
151 pub fn field_resolution(&self, expr: ExprId) -> Option<FieldId> { 150 pub fn field_resolution(&self, expr: ExprId) -> Option<FieldId> {
152 self.field_resolutions.get(&expr).copied() 151 self.field_resolutions.get(&expr).copied()
153 } 152 }
154 pub fn record_pat_field_resolution(&self, pat: PatId) -> Option<FieldId> {
155 self.record_pat_field_resolutions.get(&pat).copied()
156 }
157 pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantId> { 153 pub fn variant_resolution_for_expr(&self, id: ExprId) -> Option<VariantId> {
158 self.variant_resolutions.get(&id.into()).copied() 154 self.variant_resolutions.get(&id.into()).copied()
159 } 155 }
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index 942f70edf..e4813c87c 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -7,7 +7,6 @@ use chalk_ir::Mutability;
7use hir_def::{ 7use hir_def::{
8 expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, 8 expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat},
9 path::Path, 9 path::Path,
10 FieldId,
11}; 10};
12use hir_expand::name::Name; 11use hir_expand::name::Name;
13 12
@@ -80,11 +79,6 @@ impl<'a> InferenceContext<'a> {
80 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default(); 79 let field_tys = def.map(|it| self.db.field_types(it)).unwrap_or_default();
81 for subpat in subpats { 80 for subpat in subpats {
82 let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name)); 81 let matching_field = var_data.as_ref().and_then(|it| it.field(&subpat.name));
83 if let Some(local_id) = matching_field {
84 let field_def = FieldId { parent: def.unwrap(), local_id };
85 self.result.record_pat_field_resolutions.insert(subpat.pat, field_def);
86 }
87
88 let expected_ty = matching_field.map_or(self.err_ty(), |field| { 82 let expected_ty = matching_field.map_or(self.err_ty(), |field| {
89 field_tys[field].clone().substitute(&Interner, &substs) 83 field_tys[field].clone().substitute(&Interner, &substs)
90 }); 84 });