Skip to content

Commit 1fa596e

Browse files
BoxyUwUcamelid
authored andcommitted
use right span for inferrable ret ty
1 parent 3b4d221 commit 1fa596e

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

compiler/rustc_hir/src/hir.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,13 @@ impl<'hir> ConstItemRhs<'hir> {
415415
ConstItemRhs::TypeConst(ct_arg) => ct_arg.hir_id,
416416
}
417417
}
418+
419+
pub fn span<'tcx>(&self, tcx: impl crate::intravisit::HirTyCtxt<'tcx>) -> Span {
420+
match self {
421+
ConstItemRhs::Body(body_id) => tcx.hir_body(*body_id).value.span,
422+
ConstItemRhs::TypeConst(ct_arg) => ct_arg.span(),
423+
}
424+
}
418425
}
419426

420427
/// A constant that enters the type system, used for arguments to const generics (e.g. array lengths).

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
166166
def_id,
167167
ct_arg.hir_id(),
168168
ty.span,
169+
ct_arg.span(tcx),
169170
item.ident,
170171
"associated constant",
171172
)
@@ -190,6 +191,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
190191
def_id,
191192
ct_arg.hir_id(),
192193
ty.span,
194+
ct_arg.span(tcx),
193195
item.ident,
194196
"associated constant",
195197
)
@@ -214,6 +216,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
214216
def_id,
215217
body_id.hir_id,
216218
ty.span,
219+
tcx.hir_body(body_id).value.span,
217220
ident,
218221
"static variable",
219222
)
@@ -236,6 +239,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
236239
def_id,
237240
body.hir_id(),
238241
ty.span,
242+
body.span(tcx),
239243
ident,
240244
"constant",
241245
)
@@ -426,7 +430,8 @@ fn infer_placeholder_type<'tcx>(
426430
cx: &dyn HirTyLowerer<'tcx>,
427431
def_id: LocalDefId,
428432
hir_id: HirId,
429-
span: Span,
433+
ty_span: Span,
434+
body_span: Span,
430435
item_ident: Ident,
431436
kind: &'static str,
432437
) -> Ty<'tcx> {
@@ -439,10 +444,10 @@ fn infer_placeholder_type<'tcx>(
439444
// us to improve in typeck so we do that now.
440445
let guar = cx
441446
.dcx()
442-
.try_steal_modify_and_emit_err(span, StashKey::ItemNoType, |err| {
447+
.try_steal_modify_and_emit_err(ty_span, StashKey::ItemNoType, |err| {
443448
if !ty.references_error() {
444449
// Only suggest adding `:` if it was missing (and suggested by parsing diagnostic).
445-
let colon = if span == item_ident.span.shrink_to_hi() { ":" } else { "" };
450+
let colon = if ty_span == item_ident.span.shrink_to_hi() { ":" } else { "" };
446451

447452
// The parser provided a sub-optimal `HasPlaceholders` suggestion for the type.
448453
// We are typeck and have the real type, so remove that and suggest the actual type.
@@ -452,14 +457,14 @@ fn infer_placeholder_type<'tcx>(
452457

453458
if let Some(ty) = ty.make_suggestable(tcx, false, None) {
454459
err.span_suggestion(
455-
span,
460+
ty_span,
456461
format!("provide a type for the {kind}"),
457462
format!("{colon} {ty}"),
458463
Applicability::MachineApplicable,
459464
);
460465
} else {
461466
with_forced_trimmed_paths!(err.span_note(
462-
tcx.hir_span(hir_id),
467+
body_span,
463468
format!("however, the inferred type `{ty}` cannot be named"),
464469
));
465470
}
@@ -473,7 +478,7 @@ fn infer_placeholder_type<'tcx>(
473478
}
474479
// If we didn't find any infer tys, then just fallback to `span`.
475480
if visitor.spans.is_empty() {
476-
visitor.spans.push(span);
481+
visitor.spans.push(ty_span);
477482
}
478483
let mut diag = bad_placeholder(cx, visitor.spans, kind);
479484

@@ -482,20 +487,20 @@ fn infer_placeholder_type<'tcx>(
482487
// same span. If this happens, we will fall through to this arm, so
483488
// we need to suppress the suggestion since it's invalid. Ideally we
484489
// would suppress the duplicated error too, but that's really hard.
485-
if span.is_empty() && span.from_expansion() {
490+
if ty_span.is_empty() && ty_span.from_expansion() {
486491
// An approximately better primary message + no suggestion...
487492
diag.primary_message("missing type for item");
488493
} else if !ty.references_error() {
489494
if let Some(ty) = ty.make_suggestable(tcx, false, None) {
490495
diag.span_suggestion_verbose(
491-
span,
496+
ty_span,
492497
"replace this with a fully-specified type",
493498
ty,
494499
Applicability::MachineApplicable,
495500
);
496501
} else {
497502
with_forced_trimmed_paths!(diag.span_note(
498-
tcx.hir_span(hir_id),
503+
body_span,
499504
format!("however, the inferred type `{ty}` cannot be named"),
500505
));
501506
}

0 commit comments

Comments
 (0)