Skip to content

Commit ae395ef

Browse files
committed
WIP: funny hack that fixes up spans later
1 parent 584f2b4 commit ae395ef

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,14 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
12641264
self
12651265
} }
12661266

1267+
with_fn! { with_replace_span,
1268+
/// Replace all occurrences of the first span with the second one
1269+
#[rustc_lint_diagnostics]
1270+
pub fn replace_span(&mut self, before: Span, after: Span) -> &mut Self {
1271+
self.span.replace(before, after);
1272+
self
1273+
} }
1274+
12671275
#[rustc_lint_diagnostics]
12681276
pub fn is_lint(&mut self, name: String, has_future_breakage: bool) -> &mut Self {
12691277
self.is_lint = Some(IsLint { name, has_future_breakage });

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,27 +1051,21 @@ pub(crate) fn check_type_defn<'tcx>(
10511051
variant.fields.raw[..variant.fields.len() - unsized_len].iter().enumerate()
10521052
{
10531053
let last = idx == variant.fields.len() - 1;
1054-
let field_id = field.did.expect_local();
1055-
let hir::FieldDef { ty: hir_ty, .. } =
1056-
tcx.hir_node_by_def_id(field_id).expect_field();
1057-
let ty = wfcx.normalize(
1058-
hir_ty.span,
1059-
None,
1060-
tcx.type_of(field.did).instantiate_identity(),
1061-
);
1054+
let span = tcx.def_span(field.did);
1055+
let ty = wfcx.normalize(span, None, tcx.type_of(field.did).instantiate_identity());
10621056
wfcx.register_bound(
10631057
traits::ObligationCause::new(
1064-
hir_ty.span,
1058+
span,
10651059
wfcx.body_def_id,
10661060
ObligationCauseCode::FieldSized {
10671061
adt_kind: adt_def.adt_kind(),
1068-
span: hir_ty.span,
1062+
field: field.did,
10691063
last,
10701064
},
10711065
),
10721066
wfcx.param_env,
10731067
ty,
1074-
tcx.require_lang_item(LangItem::Sized, hir_ty.span),
1068+
tcx.require_lang_item(LangItem::Sized, span),
10751069
);
10761070
}
10771071

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3910,7 +3910,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
39103910
expr.span,
39113911
ObligationCauseCode::FieldSized {
39123912
adt_kind: AdtKind::Enum,
3913-
span: self.tcx.def_span(field.did),
3913+
field: field.did,
39143914
last: false,
39153915
},
39163916
);

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ pub enum ObligationCauseCode<'tcx> {
261261
/// Types of fields (other than the last, except for packed structs) in a struct must be sized.
262262
FieldSized {
263263
adt_kind: AdtKind,
264-
span: Span,
264+
field: DefId,
265265
last: bool,
266266
},
267267

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3121,7 +3121,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
31213121
ObligationCauseCode::StructInitializerSized => {
31223122
err.note("structs must have a statically known size to be initialized");
31233123
}
3124-
ObligationCauseCode::FieldSized { adt_kind: ref item, last, span } => {
3124+
ObligationCauseCode::FieldSized { adt_kind: ref item, last, field } => {
3125+
let def_span = tcx.def_span(field);
3126+
let span = field
3127+
.as_local()
3128+
.map(|field| tcx.hir_node_by_def_id(field).expect_field().ty.span)
3129+
.unwrap_or(def_span);
3130+
err.replace_span(def_span, span);
31253131
match *item {
31263132
AdtKind::Struct => {
31273133
if last {

0 commit comments

Comments
 (0)