Skip to content

Commit 07e6d0e

Browse files
committed
lower_field_path: don't query the type of the last field
1 parent 53fc6ad commit 07e6d0e

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
363363

364364
let mut field_indices = Vec::with_capacity(fields.len());
365365
let mut current_container = container;
366-
let mut fields = fields.into_iter();
366+
let mut fields = fields.into_iter().peekable();
367367
let mut infcx = None;
368368
let infcx = self.infcx().unwrap_or_else(|| {
369369
assert!(!container.has_infer());
@@ -375,6 +375,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
375375
let ty_ch_cx = RefCell::new(<dyn TraitEngine<'_, FulfillmentError<'tcx>>>::new(&infcx));
376376

377377
while let Some(&field) = fields.next() {
378+
let last = fields.peek().is_none();
378379
let result =
379380
at.structurally_normalize_ty(current_container, &mut **ty_ch_cx.borrow_mut());
380381
let container = match result {
@@ -392,8 +393,6 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
392393
.iter_enumerated()
393394
.find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == ident)
394395
{
395-
let field_ty = field.ty(self.tcx, args);
396-
397396
if field.vis.is_accessible_from(def_scope, self.tcx) {
398397
self.tcx.check_stability(field.did, Some(hir_id), span, None);
399398
} else {
@@ -411,7 +410,10 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
411410
}
412411

413412
field_indices.push((FIRST_VARIANT, index));
414-
current_container = field_ty;
413+
if !last {
414+
let field_ty = field.ty(self.tcx, args);
415+
current_container = field_ty;
416+
}
415417

416418
continue;
417419
}

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,10 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
282282

283283
let mut field_indices = Vec::with_capacity(fields.len());
284284
let mut current_container = container;
285-
let mut fields = fields.into_iter();
285+
let mut fields = fields.into_iter().peekable();
286286

287287
while let Some(&field) = fields.next() {
288+
let last = fields.peek().is_none();
288289
let container = self.structurally_resolve_type(span, current_container);
289290

290291
match container.kind() {
@@ -351,30 +352,33 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
351352
.emit_unless_delay(container.references_error()));
352353
};
353354

354-
let field_ty = self.field_ty(span, field, args);
355-
356-
// Enums are anyway always sized. But just to safeguard against future
357-
// language extensions, let's double-check.
358-
self.require_type_is_sized(
359-
field_ty,
360-
span,
361-
ObligationCauseCode::FieldSized {
362-
adt_kind: AdtKind::Enum,
363-
span: self.tcx.def_span(field.did),
364-
last: false,
365-
},
366-
);
367-
368-
if field.vis.is_accessible_from(sub_def_scope, self.tcx) {
369-
self.tcx.check_stability(field.did, Some(hir_id), span, None);
370-
} else {
371-
self.private_field_err(ident, container_def.did()).emit();
372-
}
373-
374355
// Save the index of all fields regardless of their visibility in case
375356
// of error recovery.
376357
field_indices.push((index, subindex));
377-
current_container = field_ty;
358+
359+
if !last {
360+
let field_ty = self.field_ty(span, field, args);
361+
362+
// Enums are anyway always sized. But just to safeguard against future
363+
// language extensions, let's double-check.
364+
self.require_type_is_sized(
365+
field_ty,
366+
span,
367+
ObligationCauseCode::FieldSized {
368+
adt_kind: AdtKind::Enum,
369+
span: self.tcx.def_span(field.did),
370+
last: false,
371+
},
372+
);
373+
374+
if field.vis.is_accessible_from(sub_def_scope, self.tcx) {
375+
self.tcx.check_stability(field.did, Some(hir_id), span, None);
376+
} else {
377+
self.private_field_err(ident, container_def.did()).emit();
378+
}
379+
380+
current_container = field_ty;
381+
}
378382

379383
continue;
380384
}
@@ -388,37 +392,40 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
388392
.iter_enumerated()
389393
.find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == ident)
390394
{
391-
let field_ty = self.field_ty(span, field, args);
395+
// Save the index of all fields regardless of their visibility in case
396+
// of error recovery.
397+
field_indices.push((FIRST_VARIANT, index));
398+
399+
if !last {
400+
let field_ty = self.field_ty(span, field, args);
392401

393-
match field_path_kind {
394-
FieldPathKind::OffsetOf => {
395-
if self.tcx.features().offset_of_slice() {
396-
self.require_type_has_static_alignment(field_ty, span);
397-
} else {
398-
self.require_type_is_sized(
399-
field_ty,
400-
span,
401-
ObligationCauseCode::Misc,
402-
);
402+
match field_path_kind {
403+
FieldPathKind::OffsetOf => {
404+
if self.tcx.features().offset_of_slice() {
405+
self.require_type_has_static_alignment(field_ty, span);
406+
} else {
407+
self.require_type_is_sized(
408+
field_ty,
409+
span,
410+
ObligationCauseCode::Misc,
411+
);
412+
}
413+
}
414+
FieldPathKind::FieldOf => {
415+
// A field type always exists regardless of weather it is aligned or
416+
// not.
403417
}
404418
}
405-
FieldPathKind::FieldOf => {
406-
// A field type always exists regardless of weather it is aligned or
407-
// not.
419+
420+
if field.vis.is_accessible_from(def_scope, self.tcx) {
421+
self.tcx.check_stability(field.did, Some(hir_id), span, None);
422+
} else {
423+
self.private_field_err(ident, container_def.did()).emit();
408424
}
409-
}
410425

411-
if field.vis.is_accessible_from(def_scope, self.tcx) {
412-
self.tcx.check_stability(field.did, Some(hir_id), span, None);
413-
} else {
414-
self.private_field_err(ident, container_def.did()).emit();
426+
current_container = field_ty;
415427
}
416428

417-
// Save the index of all fields regardless of their visibility in case
418-
// of error recovery.
419-
field_indices.push((FIRST_VARIANT, index));
420-
current_container = field_ty;
421-
422429
continue;
423430
}
424431
}

0 commit comments

Comments
 (0)