Skip to content

Commit a4e6556

Browse files
committed
Back out "Remove has_default from FieldId"
This backs out commit 8aa6c09fcee6270c787a6f00615d76343fbe5c07.
1 parent cadc468 commit a4e6556

File tree

11 files changed

+64
-35
lines changed

11 files changed

+64
-35
lines changed

src/tools/rust-analyzer/crates/hir-def/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ pub type LocalModuleId = Idx<nameres::ModuleData>;
519519
pub struct FieldId {
520520
pub parent: VariantId,
521521
pub local_id: LocalFieldId,
522+
pub has_default: bool,
522523
}
523524

524525
impl FieldId {

src/tools/rust-analyzer/crates/hir-ty/src/infer/closure.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1167,9 +1167,11 @@ impl InferenceContext<'_> {
11671167
};
11681168
let mut p = place.clone();
11691169
self.current_capture_span_stack.push(MirSpan::PatId(arg));
1170+
let has_default = vd.fields()[local_id].has_default;
11701171
p.projections.push(ProjectionElem::Field(Either::Left(FieldId {
11711172
parent: variant,
11721173
local_id,
1174+
has_default,
11731175
})));
11741176
self.consume_with_pat(p, arg);
11751177
self.current_capture_span_stack.pop();
@@ -1219,12 +1221,13 @@ impl InferenceContext<'_> {
12191221
.iter()
12201222
.zip(fields.clone())
12211223
.chain(ar.iter().rev().zip(fields.rev()));
1222-
for (&arg, (i, _)) in it {
1224+
for (&arg, (i, d)) in it {
12231225
let mut p = place.clone();
12241226
self.current_capture_span_stack.push(MirSpan::PatId(arg));
12251227
p.projections.push(ProjectionElem::Field(Either::Left(FieldId {
12261228
parent: variant,
12271229
local_id: i,
1230+
has_default: d.has_default,
12281231
})));
12291232
self.consume_with_pat(p, arg);
12301233
self.current_capture_span_stack.pop();

src/tools/rust-analyzer/crates/hir-ty/src/infer/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,13 +1748,15 @@ impl InferenceContext<'_> {
17481748
TyKind::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => {
17491749
let vd = &self.db.struct_data(*s).variant_data;
17501750
let local_id = vd.field(name)?;
1751-
let field = FieldId { parent: (*s).into(), local_id };
1751+
let has_default = vd.fields()[local_id].has_default;
1752+
let field = FieldId { parent: (*s).into(), local_id, has_default };
17521753
(field, parameters.clone())
17531754
}
17541755
TyKind::Adt(AdtId(hir_def::AdtId::UnionId(u)), parameters) => {
17551756
let vd = &self.db.union_data(*u).variant_data;
17561757
let local_id = vd.field(name)?;
1757-
let field = FieldId { parent: (*u).into(), local_id };
1758+
let has_default = vd.fields()[local_id].has_default;
1759+
let field = FieldId { parent: (*u).into(), local_id, has_default };
17581760
(field, parameters.clone())
17591761
}
17601762
_ => return None,

src/tools/rust-analyzer/crates/hir-ty/src/mir/lower.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,13 @@ impl<'ctx> MirLowerCtx<'ctx> {
872872
None => {
873873
let local_id =
874874
LocalFieldId::from_raw(RawIdx::from(i as u32));
875+
let has_default =
876+
variant_data.fields()[local_id].has_default;
875877
let p = sp.project(
876878
ProjectionElem::Field(Either::Left(FieldId {
877879
parent: variant_id,
878880
local_id,
881+
has_default,
879882
})),
880883
&mut self.result.projection_store,
881884
);
@@ -897,10 +900,12 @@ impl<'ctx> MirLowerCtx<'ctx> {
897900
};
898901
let local_id =
899902
variant_data.field(name).ok_or(MirLowerError::UnresolvedField)?;
903+
let has_default = variant_data.fields()[local_id].has_default;
900904
let place = place.project(
901905
PlaceElem::Field(Either::Left(FieldId {
902906
parent: union_id.into(),
903907
local_id,
908+
has_default,
904909
})),
905910
&mut self.result.projection_store,
906911
);

src/tools/rust-analyzer/crates/hir-ty/src/mir/lower/pattern_matching.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,12 @@ impl MirLowerCtx<'_> {
632632
.map(|x| {
633633
let field_id =
634634
variant_data.field(&x.name).ok_or(MirLowerError::UnresolvedField)?;
635+
let has_default = variant_data.fields()[field_id].has_default;
635636
Ok((
636637
PlaceElem::Field(Either::Left(FieldId {
637638
parent: v,
638639
local_id: field_id,
640+
has_default,
639641
})),
640642
x.pat,
641643
))
@@ -644,8 +646,12 @@ impl MirLowerCtx<'_> {
644646
self.pattern_match_adt(current, current_else, it.into_iter(), cond_place, mode)?
645647
}
646648
AdtPatternShape::Tuple { args, ellipsis } => {
647-
let fields = variant_data.fields().iter().map(|(x, _)| {
648-
PlaceElem::Field(Either::Left(FieldId { parent: v, local_id: x }))
649+
let fields = variant_data.fields().iter().map(|(x, d)| {
650+
PlaceElem::Field(Either::Left(FieldId {
651+
parent: v,
652+
local_id: x,
653+
has_default: d.has_default,
654+
}))
649655
});
650656
self.pattern_match_tuple_like(
651657
current,

src/tools/rust-analyzer/crates/hir-ty/src/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn check_impl(
143143
let vd = db.variant_data(variant_id);
144144
defs.extend(vd.fields().iter().filter_map(|(local_id, fd)| {
145145
if fd.has_default {
146-
let field = FieldId { parent: variant_id, local_id };
146+
let field = FieldId { parent: variant_id, local_id, has_default: true };
147147
Some(DefWithBodyId::FieldId(field))
148148
} else {
149149
None
@@ -161,7 +161,7 @@ fn check_impl(
161161
let vd = db.variant_data(variant_id);
162162
defs.extend(vd.fields().iter().filter_map(|(local_id, fd)| {
163163
if fd.has_default {
164-
let field = FieldId { parent: variant_id, local_id };
164+
let field = FieldId { parent: variant_id, local_id, has_default: true };
165165
Some(DefWithBodyId::FieldId(field))
166166
} else {
167167
None
@@ -433,7 +433,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
433433
let vd = db.variant_data(variant_id);
434434
defs.extend(vd.fields().iter().filter_map(|(local_id, fd)| {
435435
if fd.has_default {
436-
let field = FieldId { parent: variant_id, local_id };
436+
let field = FieldId { parent: variant_id, local_id, has_default: true };
437437
Some(DefWithBodyId::FieldId(field))
438438
} else {
439439
None
@@ -451,7 +451,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
451451
let vd = db.variant_data(variant_id);
452452
defs.extend(vd.fields().iter().filter_map(|(local_id, fd)| {
453453
if fd.has_default {
454-
let field = FieldId { parent: variant_id, local_id };
454+
let field = FieldId { parent: variant_id, local_id, has_default: true };
455455
Some(DefWithBodyId::FieldId(field))
456456
} else {
457457
None

src/tools/rust-analyzer/crates/hir/src/display.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ impl HirDisplay for Struct {
252252
f.write_char('(')?;
253253
let mut it = variant_data.fields().iter().peekable();
254254

255-
while let Some((id, _)) = it.next() {
256-
let field = Field { parent: (*self).into(), id };
255+
while let Some((id, d)) = it.next() {
256+
let field = Field { parent: (*self).into(), id, has_default: d.has_default };
257257
write_visibility(module_id, field.visibility(f.db), f)?;
258258
field.ty(f.db).hir_fmt(f)?;
259259
if it.peek().is_some() {

src/tools/rust-analyzer/crates/hir/src/from_id.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,13 @@ impl From<VariantDef> for VariantId {
237237

238238
impl From<Field> for FieldId {
239239
fn from(def: Field) -> Self {
240-
FieldId { parent: def.parent.into(), local_id: def.id }
240+
FieldId { parent: def.parent.into(), local_id: def.id, has_default: def.has_default }
241241
}
242242
}
243243

244244
impl From<FieldId> for Field {
245245
fn from(def: FieldId) -> Self {
246-
Field { parent: def.parent.into(), id: def.local_id }
246+
Field { parent: def.parent.into(), id: def.local_id, has_default: def.has_default }
247247
}
248248
}
249249

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -415,24 +415,18 @@ impl ModuleDef {
415415
def.diagnostics(db, &mut acc);
416416
}
417417

418-
let vd: Option<(VariantDef, Arc<VariantData>)> = match self {
419-
ModuleDef::Adt(Adt::Struct(it)) => {
420-
Some((it.into(), db.struct_data(it.id).variant_data.clone()))
421-
}
422-
ModuleDef::Adt(Adt::Union(it)) => {
423-
Some((it.into(), db.union_data(it.id).variant_data.clone()))
424-
}
425-
ModuleDef::Variant(it) => {
426-
Some((it.into(), db.enum_variant_data(it.id).variant_data.clone()))
427-
}
418+
let fields = match self {
419+
ModuleDef::Adt(Adt::Struct(it)) => Some(it.fields(db)),
420+
ModuleDef::Adt(Adt::Union(it)) => Some(it.fields(db)),
421+
ModuleDef::Variant(it) => Some(it.fields(db)),
428422
_ => None,
429423
};
430-
if let Some((parent, vd)) = vd {
431-
for (id, fd) in vd.fields().iter() {
432-
if !fd.has_default {
424+
if let Some(fields) = fields {
425+
for field in fields {
426+
if !field.has_default {
433427
continue;
434428
}
435-
let def: DefWithBody = DefWithBody::Field(Field { parent, id });
429+
let def: DefWithBody = field.into();
436430
def.diagnostics(db, &mut acc, style_lints);
437431
}
438432
}
@@ -1258,6 +1252,7 @@ impl From<&Field> for DefWithBodyId {
12581252
pub struct Field {
12591253
pub(crate) parent: VariantDef,
12601254
pub(crate) id: LocalFieldId,
1255+
pub(crate) has_default: bool,
12611256
}
12621257

12631258
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
@@ -1423,7 +1418,7 @@ impl Struct {
14231418
.variant_data
14241419
.fields()
14251420
.iter()
1426-
.map(|(id, _)| Field { parent: self.into(), id })
1421+
.map(|(id, d)| Field { parent: self.into(), id, has_default: d.has_default })
14271422
.collect()
14281423
}
14291424

@@ -1485,7 +1480,7 @@ impl Union {
14851480
.variant_data
14861481
.fields()
14871482
.iter()
1488-
.map(|(id, _)| Field { parent: self.into(), id })
1483+
.map(|(id, d)| Field { parent: self.into(), id, has_default: d.has_default })
14891484
.collect()
14901485
}
14911486

@@ -1615,7 +1610,7 @@ impl Variant {
16151610
self.variant_data(db)
16161611
.fields()
16171612
.iter()
1618-
.map(|(id, _)| Field { parent: self.into(), id })
1613+
.map(|(id, d)| Field { parent: self.into(), id, has_default: d.has_default })
16191614
.collect()
16201615
}
16211616

@@ -5203,10 +5198,13 @@ impl Type {
52035198
_ => return Vec::new(),
52045199
};
52055200

5201+
let var_data = db.variant_data(variant_id);
5202+
let fields = var_data.fields();
52065203
db.field_types(variant_id)
52075204
.iter()
52085205
.map(|(local_id, ty)| {
5209-
let def = Field { parent: variant_id.into(), id: local_id };
5206+
let has_default = fields[local_id].has_default;
5207+
let def = Field { parent: variant_id.into(), id: local_id, has_default };
52105208
let ty = ty.clone().substitute(Interner, substs);
52115209
(def, self.derived(ty))
52125210
})

src/tools/rust-analyzer/crates/hir/src/semantics/child_by_source.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ impl ChildBySource for VariantId {
160160
let arena_map = arena_map.as_ref();
161161
let parent = *self;
162162
for (local_id, source) in arena_map.value.iter() {
163-
let id = FieldId { parent, local_id };
163+
let has_default = match source {
164+
Either::Left(_) => false,
165+
Either::Right(rec) => rec.expr().is_some(),
166+
};
167+
let id = FieldId { parent, local_id, has_default };
164168
match source.clone() {
165169
Either::Left(source) => res[keys::TUPLE_FIELD].insert(AstPtr::new(&source), id),
166170
Either::Right(source) => res[keys::RECORD_FIELD].insert(AstPtr::new(&source), id),

0 commit comments

Comments
 (0)