Skip to content

Commit afd8558

Browse files
authored
Rollup merge of #108945 - spastorino:pass-def-id-instead-of-using-hir-id, r=compiler-errors
Make some report and emit errors take DefIds instead of BodyIds Breaking off from #108915 r? `@compiler-errors`
2 parents 63c6a34 + 5b99723 commit afd8558

File tree

5 files changed

+50
-43
lines changed

5 files changed

+50
-43
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
581581

582582
if !errors.is_empty() {
583583
self.adjust_fulfillment_errors_for_expr_obligation(&mut errors);
584-
self.err_ctxt().report_fulfillment_errors(&errors, self.inh.body_id);
584+
self.err_ctxt().report_fulfillment_errors(&errors, Some(self.inh.body_def_id));
585585
}
586586
}
587587

@@ -594,7 +594,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
594594
if !result.is_empty() {
595595
mutate_fulfillment_errors(&mut result);
596596
self.adjust_fulfillment_errors_for_expr_obligation(&mut result);
597-
self.err_ctxt().report_fulfillment_errors(&result, self.inh.body_id);
597+
self.err_ctxt().report_fulfillment_errors(&result, Some(self.inh.body_def_id));
598598
}
599599
}
600600

@@ -1411,7 +1411,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14111411
} else {
14121412
let e = self.tainted_by_errors().unwrap_or_else(|| {
14131413
self.err_ctxt()
1414-
.emit_inference_failure_err((**self).body_id, sp, ty.into(), E0282, true)
1414+
.emit_inference_failure_err(
1415+
Some(self.inh.body_def_id),
1416+
sp,
1417+
ty.into(),
1418+
E0282,
1419+
true,
1420+
)
14151421
.emit()
14161422
});
14171423
let err = self.tcx.ty_error(e);

compiler/rustc_hir_typeck/src/inherited.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub struct Inherited<'tcx> {
5858
pub(super) deferred_generator_interiors:
5959
RefCell<Vec<(LocalDefId, hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
6060

61-
pub(super) body_id: Option<hir::BodyId>,
61+
pub(super) body_def_id: LocalDefId,
6262

6363
/// Whenever we introduce an adjustment from `!` into a type variable,
6464
/// we record that type variable here. This is later used to inform
@@ -116,7 +116,6 @@ impl<'tcx> Inherited<'tcx> {
116116
typeck_results: RefCell<ty::TypeckResults<'tcx>>,
117117
) -> Self {
118118
let tcx = infcx.tcx;
119-
let body_id = tcx.hir().maybe_body_owned_by(def_id);
120119

121120
Inherited {
122121
typeck_results,
@@ -130,7 +129,7 @@ impl<'tcx> Inherited<'tcx> {
130129
deferred_asm_checks: RefCell::new(Vec::new()),
131130
deferred_generator_interiors: RefCell::new(Vec::new()),
132131
diverging_type_vars: RefCell::new(Default::default()),
133-
body_id,
132+
body_def_id: def_id,
134133
infer_var_info: RefCell::new(Default::default()),
135134
}
136135
}

compiler/rustc_hir_typeck/src/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
748748
.infcx
749749
.err_ctxt()
750750
.emit_inference_failure_err(
751-
Some(self.body.id()),
751+
Some(self.tcx.hir().body_owner_def_id(self.body.id())),
752752
self.span.to_span(self.tcx),
753753
p.into(),
754754
E0282,

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, IntoDiagnosticArg};
1010
use rustc_hir as hir;
1111
use rustc_hir::def::Res;
1212
use rustc_hir::def::{CtorOf, DefKind, Namespace};
13-
use rustc_hir::def_id::DefId;
13+
use rustc_hir::def_id::{DefId, LocalDefId};
1414
use rustc_hir::intravisit::{self, Visitor};
1515
use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, Local, LocalSource};
1616
use rustc_middle::hir::nested_filter;
@@ -386,7 +386,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
386386
#[instrument(level = "debug", skip(self, error_code))]
387387
pub fn emit_inference_failure_err(
388388
&self,
389-
body_id: Option<hir::BodyId>,
389+
body_def_id: Option<LocalDefId>,
390390
failure_span: Span,
391391
arg: GenericArg<'tcx>,
392392
error_code: TypeAnnotationNeeded,
@@ -403,8 +403,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
403403
};
404404

405405
let mut local_visitor = FindInferSourceVisitor::new(&self, typeck_results, arg);
406-
if let Some(body_id) = body_id {
407-
let expr = self.tcx.hir().expect_expr(body_id.hir_id);
406+
if let Some(body_def_id) = body_def_id
407+
&& let Some(body_id) = self.tcx.hir().maybe_body_owned_by(body_def_id)
408+
{
409+
let expr = self.tcx.hir().body(body_id).value;
408410
local_visitor.visit_expr(expr);
409411
}
410412

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

+32-32
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ use rustc_errors::{
2424
};
2525
use rustc_hir as hir;
2626
use rustc_hir::def::Namespace;
27-
use rustc_hir::def_id::DefId;
27+
use rustc_hir::def_id::{DefId, LocalDefId};
2828
use rustc_hir::intravisit::Visitor;
29-
use rustc_hir::GenericParam;
30-
use rustc_hir::Item;
31-
use rustc_hir::Node;
29+
use rustc_hir::{GenericParam, Item, Node};
3230
use rustc_infer::infer::error_reporting::TypeErrCtxt;
3331
use rustc_infer::infer::{InferOk, TypeTrace};
3432
use rustc_middle::traits::select::OverflowError;
@@ -129,7 +127,7 @@ pub trait TypeErrCtxtExt<'tcx> {
129127
fn report_fulfillment_errors(
130128
&self,
131129
errors: &[FulfillmentError<'tcx>],
132-
body_id: Option<hir::BodyId>,
130+
body_def_id: Option<LocalDefId>,
133131
) -> ErrorGuaranteed;
134132

135133
fn report_overflow_obligation<T>(
@@ -391,7 +389,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
391389
fn report_fulfillment_errors(
392390
&self,
393391
errors: &[FulfillmentError<'tcx>],
394-
body_id: Option<hir::BodyId>,
392+
body_def_id: Option<LocalDefId>,
395393
) -> ErrorGuaranteed {
396394
#[derive(Debug)]
397395
struct ErrorDescriptor<'tcx> {
@@ -469,7 +467,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
469467
for from_expansion in [false, true] {
470468
for (error, suppressed) in iter::zip(errors, &is_suppressed) {
471469
if !suppressed && error.obligation.cause.span.from_expansion() == from_expansion {
472-
self.report_fulfillment_error(error, body_id);
470+
self.report_fulfillment_error(error, body_def_id);
473471
}
474472
}
475473
}
@@ -955,8 +953,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
955953
);
956954
}
957955

958-
let body_hir_id =
959-
self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id);
956+
let body_def_id = obligation.cause.body_id;
960957
// Try to report a help message
961958
if is_fn_trait
962959
&& let Ok((implemented_kind, params)) = self.type_implements_fn_trait(
@@ -1037,7 +1034,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10371034
if !self.report_similar_impl_candidates(
10381035
impl_candidates,
10391036
trait_ref,
1040-
body_hir_id,
1037+
body_def_id,
10411038
&mut err,
10421039
true,
10431040
) {
@@ -1073,7 +1070,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10731070
self.report_similar_impl_candidates(
10741071
impl_candidates,
10751072
trait_ref,
1076-
body_hir_id,
1073+
body_def_id,
10771074
&mut err,
10781075
true,
10791076
);
@@ -1497,7 +1494,7 @@ trait InferCtxtPrivExt<'tcx> {
14971494
fn report_fulfillment_error(
14981495
&self,
14991496
error: &FulfillmentError<'tcx>,
1500-
body_id: Option<hir::BodyId>,
1497+
body_def_id: Option<LocalDefId>,
15011498
);
15021499

15031500
fn report_projection_error(
@@ -1531,7 +1528,7 @@ trait InferCtxtPrivExt<'tcx> {
15311528
&self,
15321529
impl_candidates: Vec<ImplCandidate<'tcx>>,
15331530
trait_ref: ty::PolyTraitRef<'tcx>,
1534-
body_id: hir::HirId,
1531+
body_def_id: LocalDefId,
15351532
err: &mut Diagnostic,
15361533
other: bool,
15371534
) -> bool;
@@ -1564,7 +1561,7 @@ trait InferCtxtPrivExt<'tcx> {
15641561
fn maybe_report_ambiguity(
15651562
&self,
15661563
obligation: &PredicateObligation<'tcx>,
1567-
body_id: Option<hir::BodyId>,
1564+
body_def_id: Option<LocalDefId>,
15681565
);
15691566

15701567
fn predicate_can_apply(
@@ -1650,7 +1647,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16501647
fn report_fulfillment_error(
16511648
&self,
16521649
error: &FulfillmentError<'tcx>,
1653-
body_id: Option<hir::BodyId>,
1650+
body_def_id: Option<LocalDefId>,
16541651
) {
16551652
match error.code {
16561653
FulfillmentErrorCode::CodeSelectionError(ref selection_error) => {
@@ -1664,7 +1661,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
16641661
self.report_projection_error(&error.obligation, e);
16651662
}
16661663
FulfillmentErrorCode::CodeAmbiguity => {
1667-
self.maybe_report_ambiguity(&error.obligation, body_id);
1664+
self.maybe_report_ambiguity(&error.obligation, body_def_id);
16681665
}
16691666
FulfillmentErrorCode::CodeSubtypeError(ref expected_found, ref err) => {
16701667
self.report_mismatched_types(
@@ -2029,7 +2026,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20292026
&self,
20302027
impl_candidates: Vec<ImplCandidate<'tcx>>,
20312028
trait_ref: ty::PolyTraitRef<'tcx>,
2032-
body_id: hir::HirId,
2029+
body_def_id: LocalDefId,
20332030
err: &mut Diagnostic,
20342031
other: bool,
20352032
) -> bool {
@@ -2120,9 +2117,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21202117
// FIXME(compiler-errors): This could be generalized, both to
21212118
// be more granular, and probably look past other `#[fundamental]`
21222119
// types, too.
2123-
self.tcx
2124-
.visibility(def.did())
2125-
.is_accessible_from(body_id.owner.def_id, self.tcx)
2120+
self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx)
21262121
} else {
21272122
true
21282123
}
@@ -2234,7 +2229,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
22342229
fn maybe_report_ambiguity(
22352230
&self,
22362231
obligation: &PredicateObligation<'tcx>,
2237-
body_id: Option<hir::BodyId>,
2232+
body_def_id: Option<LocalDefId>,
22382233
) {
22392234
// Unable to successfully determine, probably means
22402235
// insufficient type information, but could mean
@@ -2277,7 +2272,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
22772272
if self.tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
22782273
if let None = self.tainted_by_errors() {
22792274
self.emit_inference_failure_err(
2280-
body_id,
2275+
body_def_id,
22812276
span,
22822277
trait_ref.self_ty().skip_binder().into(),
22832278
ErrorCode::E0282,
@@ -2304,7 +2299,13 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23042299
let subst = data.trait_ref.substs.iter().find(|s| s.has_non_region_infer());
23052300

23062301
let mut err = if let Some(subst) = subst {
2307-
self.emit_inference_failure_err(body_id, span, subst, ErrorCode::E0283, true)
2302+
self.emit_inference_failure_err(
2303+
body_def_id,
2304+
span,
2305+
subst,
2306+
ErrorCode::E0283,
2307+
true,
2308+
)
23082309
} else {
23092310
struct_span_err!(
23102311
self.tcx.sess,
@@ -2348,12 +2349,10 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23482349
predicate.to_opt_poly_trait_pred().unwrap(),
23492350
);
23502351
if impl_candidates.len() < 10 {
2351-
let hir =
2352-
self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id);
23532352
self.report_similar_impl_candidates(
23542353
impl_candidates,
23552354
trait_ref,
2356-
body_id.map(|id| id.hir_id).unwrap_or(hir),
2355+
body_def_id.unwrap_or(obligation.cause.body_id),
23572356
&mut err,
23582357
false,
23592358
);
@@ -2375,9 +2374,10 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23752374
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
23762375
}
23772376

2378-
if let (Some(body_id), Some(ty::subst::GenericArgKind::Type(_))) =
2379-
(body_id, subst.map(|subst| subst.unpack()))
2377+
if let (Some(body_def_id), Some(ty::subst::GenericArgKind::Type(_))) =
2378+
(body_def_id, subst.map(|subst| subst.unpack()))
23802379
{
2380+
let body_id = self.tcx.hir().body_owned_by(body_def_id);
23812381
let mut expr_finder = FindExprBySpan::new(span);
23822382
expr_finder.visit_expr(&self.tcx.hir().body(body_id).value);
23832383

@@ -2473,7 +2473,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
24732473
return;
24742474
}
24752475

2476-
self.emit_inference_failure_err(body_id, span, arg, ErrorCode::E0282, false)
2476+
self.emit_inference_failure_err(body_def_id, span, arg, ErrorCode::E0282, false)
24772477
}
24782478

24792479
ty::PredicateKind::Subtype(data) => {
@@ -2487,7 +2487,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
24872487
let SubtypePredicate { a_is_expected: _, a, b } = data;
24882488
// both must be type variables, or the other would've been instantiated
24892489
assert!(a.is_ty_var() && b.is_ty_var());
2490-
self.emit_inference_failure_err(body_id, span, a.into(), ErrorCode::E0282, true)
2490+
self.emit_inference_failure_err(body_def_id, span, a.into(), ErrorCode::E0282, true)
24912491
}
24922492
ty::PredicateKind::Clause(ty::Clause::Projection(data)) => {
24932493
if predicate.references_error() || self.tainted_by_errors().is_some() {
@@ -2501,7 +2501,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
25012501
.find(|g| g.has_non_region_infer());
25022502
if let Some(subst) = subst {
25032503
let mut err = self.emit_inference_failure_err(
2504-
body_id,
2504+
body_def_id,
25052505
span,
25062506
subst,
25072507
ErrorCode::E0284,
@@ -2530,7 +2530,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
25302530
let subst = data.walk().find(|g| g.is_non_region_infer());
25312531
if let Some(subst) = subst {
25322532
let err = self.emit_inference_failure_err(
2533-
body_id,
2533+
body_def_id,
25342534
span,
25352535
subst,
25362536
ErrorCode::E0284,

0 commit comments

Comments
 (0)