Skip to content

Commit 7d2c110

Browse files
fix: use LocalDefId instead of HirId in trait res
use LocalDefId instead of HirId in trait resolution to simplify the obligation clause resolution Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
1 parent 6b3cd03 commit 7d2c110

File tree

48 files changed

+265
-280
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+265
-280
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
766766
let copy_did = infcx.tcx.require_lang_item(LangItem::Copy, Some(span));
767767
let cause = ObligationCause::new(
768768
span,
769-
self.mir_hir_id(),
769+
self.mir_def_id(),
770770
rustc_infer::traits::ObligationCauseCode::MiscObligation,
771771
);
772772
let errors = rustc_trait_selection::traits::fully_solve_bound(

Diff for: compiler/rustc_borrowck/src/region_infer/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
use rustc_data_structures::graph::scc::Sccs;
88
use rustc_errors::Diagnostic;
99
use rustc_hir::def_id::CRATE_DEF_ID;
10-
use rustc_hir::CRATE_HIR_ID;
1110
use rustc_index::vec::IndexVec;
1211
use rustc_infer::infer::outlives::test_type_match;
1312
use rustc_infer::infer::region_constraints::{GenericKind, VarInfos, VerifyBound, VerifyIfEq};
@@ -2022,7 +2021,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20222021
.map(|constraint| BlameConstraint {
20232022
category: constraint.category,
20242023
from_closure: constraint.from_closure,
2025-
cause: ObligationCause::new(constraint.span, CRATE_HIR_ID, cause_code.clone()),
2024+
cause: ObligationCause::new(constraint.span, CRATE_DEF_ID, cause_code.clone()),
20262025
variance_info: constraint.variance_info,
20272026
outlives_constraint: *constraint,
20282027
})

Diff for: compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
273273
// This logic duplicates most of `check_opaque_meets_bounds`.
274274
// FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely.
275275
let param_env = self.tcx.param_env(def_id);
276-
let body_id = self.tcx.local_def_id_to_hir_id(def_id);
277276
// HACK This bubble is required for this tests to pass:
278277
// type-alias-impl-trait/issue-67844-nested-opaque.rs
279278
let infcx =
@@ -290,15 +289,15 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
290289
// the bounds that the function supplies.
291290
let opaque_ty = self.tcx.mk_opaque(def_id.to_def_id(), id_substs);
292291
if let Err(err) = ocx.eq(
293-
&ObligationCause::misc(instantiated_ty.span, body_id),
292+
&ObligationCause::misc(instantiated_ty.span, def_id),
294293
param_env,
295294
opaque_ty,
296295
definition_ty,
297296
) {
298297
infcx
299298
.err_ctxt()
300299
.report_mismatched_types(
301-
&ObligationCause::misc(instantiated_ty.span, body_id),
300+
&ObligationCause::misc(instantiated_ty.span, def_id),
302301
opaque_ty,
303302
definition_ty,
304303
err,
@@ -309,7 +308,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
309308
ocx.register_obligation(Obligation::misc(
310309
infcx.tcx,
311310
instantiated_ty.span,
312-
body_id,
311+
def_id,
313312
param_env,
314313
predicate,
315314
));

Diff for: compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -754,12 +754,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
754754
let ocx = ObligationCtxt::new(&infcx);
755755

756756
let predicates = tcx.predicates_of(callee).instantiate(tcx, substs);
757-
let hir_id = tcx
758-
.hir()
759-
.local_def_id_to_hir_id(self.body.source.def_id().expect_local());
760757
let cause = ObligationCause::new(
761758
terminator.source_info.span,
762-
hir_id,
759+
self.body.source.def_id().expect_local(),
763760
ObligationCauseCode::ItemObligation(callee),
764761
);
765762
let normalized_predicates = ocx.normalize(&cause, param_env, predicates);

Diff for: compiler/rustc_hir_analysis/src/autoderef.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use crate::errors::AutoDerefReachedRecursionLimit;
22
use crate::traits::query::evaluate_obligation::InferCtxtExt;
33
use crate::traits::NormalizeExt;
44
use crate::traits::{self, TraitEngine, TraitEngineExt};
5-
use rustc_hir as hir;
65
use rustc_infer::infer::InferCtxt;
76
use rustc_middle::ty::TypeVisitable;
87
use rustc_middle::ty::{self, Ty, TyCtxt};
98
use rustc_session::Limit;
9+
use rustc_span::def_id::LocalDefId;
1010
use rustc_span::def_id::LOCAL_CRATE;
1111
use rustc_span::Span;
1212

@@ -28,7 +28,7 @@ pub struct Autoderef<'a, 'tcx> {
2828
// Meta infos:
2929
infcx: &'a InferCtxt<'tcx>,
3030
span: Span,
31-
body_id: hir::HirId,
31+
body_id: LocalDefId,
3232
param_env: ty::ParamEnv<'tcx>,
3333

3434
// Current state:
@@ -96,14 +96,14 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
9696
pub fn new(
9797
infcx: &'a InferCtxt<'tcx>,
9898
param_env: ty::ParamEnv<'tcx>,
99-
body_id: hir::HirId,
99+
body_def_id: LocalDefId,
100100
span: Span,
101101
base_ty: Ty<'tcx>,
102102
) -> Autoderef<'a, 'tcx> {
103103
Autoderef {
104104
infcx,
105105
span,
106-
body_id,
106+
body_id: body_def_id,
107107
param_env,
108108
state: AutoderefSnapshot {
109109
steps: vec![],

Diff for: compiler/rustc_hir_analysis/src/check/check.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,6 @@ fn check_opaque_meets_bounds<'tcx>(
412412
span: Span,
413413
origin: &hir::OpaqueTyOrigin,
414414
) {
415-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
416415
let defining_use_anchor = match *origin {
417416
hir::OpaqueTyOrigin::FnReturn(did) | hir::OpaqueTyOrigin::AsyncFn(did) => did,
418417
hir::OpaqueTyOrigin::TyAlias => def_id,
@@ -438,7 +437,7 @@ fn check_opaque_meets_bounds<'tcx>(
438437
_ => re,
439438
});
440439

441-
let misc_cause = traits::ObligationCause::misc(span, hir_id);
440+
let misc_cause = traits::ObligationCause::misc(span, def_id);
442441

443442
match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
444443
Ok(()) => {}

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ fn compare_method_predicate_entailment<'tcx>(
147147
//
148148
// FIXME(@lcnr): remove that after removing `cause.body_id` from
149149
// obligations.
150-
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
150+
let impl_m_def_id = impl_m.def_id.expect_local();
151151
let cause = ObligationCause::new(
152152
impl_m_span,
153-
impl_m_hir_id,
153+
impl_m_def_id,
154154
ObligationCauseCode::CompareImplItemObligation {
155-
impl_item_def_id: impl_m.def_id.expect_local(),
155+
impl_item_def_id: impl_m_def_id,
156156
trait_item_def_id: trait_m.def_id,
157157
kind: impl_m.kind,
158158
},
@@ -198,7 +198,7 @@ fn compare_method_predicate_entailment<'tcx>(
198198
// Construct trait parameter environment and then shift it into the placeholder viewpoint.
199199
// The key step here is to update the caller_bounds's predicates to be
200200
// the new hybrid bounds we computed.
201-
let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_hir_id);
201+
let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_def_id);
202202
let param_env = ty::ParamEnv::new(
203203
tcx.intern_predicates(&hybrid_preds.predicates),
204204
Reveal::UserFacing,
@@ -213,14 +213,14 @@ fn compare_method_predicate_entailment<'tcx>(
213213

214214
let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_placeholder_substs);
215215
for (predicate, span) in impl_m_own_bounds {
216-
let normalize_cause = traits::ObligationCause::misc(span, impl_m_hir_id);
216+
let normalize_cause = traits::ObligationCause::misc(span, impl_m_def_id);
217217
let predicate = ocx.normalize(&normalize_cause, param_env, predicate);
218218

219219
let cause = ObligationCause::new(
220220
span,
221-
impl_m_hir_id,
221+
impl_m_def_id,
222222
ObligationCauseCode::CompareImplItemObligation {
223-
impl_item_def_id: impl_m.def_id.expect_local(),
223+
impl_item_def_id: impl_m_def_id,
224224
trait_item_def_id: trait_m.def_id,
225225
kind: impl_m.kind,
226226
},
@@ -253,7 +253,7 @@ fn compare_method_predicate_entailment<'tcx>(
253253
);
254254
let unnormalized_impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(unnormalized_impl_sig));
255255

256-
let norm_cause = ObligationCause::misc(impl_m_span, impl_m_hir_id);
256+
let norm_cause = ObligationCause::misc(impl_m_span, impl_m_def_id);
257257
let impl_sig = ocx.normalize(&norm_cause, param_env, unnormalized_impl_sig);
258258
debug!("compare_impl_method: impl_fty={:?}", impl_sig);
259259

@@ -311,6 +311,7 @@ fn compare_method_predicate_entailment<'tcx>(
311311
if !errors.is_empty() {
312312
match check_implied_wf {
313313
CheckImpliedWfMode::Check => {
314+
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m_def_id);
314315
return compare_method_predicate_entailment(
315316
tcx,
316317
impl_m,
@@ -336,7 +337,7 @@ fn compare_method_predicate_entailment<'tcx>(
336337
let outlives_env = OutlivesEnvironment::with_bounds(
337338
param_env,
338339
Some(infcx),
339-
infcx.implied_bounds_tys(param_env, impl_m_hir_id, wf_tys.clone()),
340+
infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys.clone()),
340341
);
341342
infcx.process_registered_region_obligations(
342343
outlives_env.region_bound_pairs(),
@@ -346,6 +347,7 @@ fn compare_method_predicate_entailment<'tcx>(
346347
if !errors.is_empty() {
347348
// FIXME(compiler-errors): This can be simplified when IMPLIED_BOUNDS_ENTAILMENT
348349
// becomes a hard error (i.e. ideally we'd just call `resolve_regions_and_report_errors`
350+
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m_def_id);
349351
match check_implied_wf {
350352
CheckImpliedWfMode::Check => {
351353
return compare_method_predicate_entailment(
@@ -371,7 +373,7 @@ fn compare_method_predicate_entailment<'tcx>(
371373
}
372374
CheckImpliedWfMode::Skip => {
373375
if infcx.tainted_by_errors().is_none() {
374-
infcx.err_ctxt().report_region_errors(impl_m.def_id.expect_local(), &errors);
376+
infcx.err_ctxt().report_region_errors(impl_m_def_id, &errors);
375377
}
376378
return Err(tcx
377379
.sess
@@ -610,13 +612,14 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
610612

611613
let trait_to_impl_substs = impl_trait_ref.substs;
612614

613-
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
615+
let impl_m_def_id = impl_m.def_id.expect_local();
616+
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m_def_id);
614617
let return_span = tcx.hir().fn_decl_by_hir_id(impl_m_hir_id).unwrap().output.span();
615618
let cause = ObligationCause::new(
616619
return_span,
617-
impl_m_hir_id,
620+
impl_m_def_id,
618621
ObligationCauseCode::CompareImplItemObligation {
619-
impl_item_def_id: impl_m.def_id.expect_local(),
622+
impl_item_def_id: impl_m_def_id,
620623
trait_item_def_id: trait_m.def_id,
621624
kind: impl_m.kind,
622625
},
@@ -633,7 +636,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
633636
let ocx = ObligationCtxt::new(infcx);
634637

635638
// Normalize the impl signature with fresh variables for lifetime inference.
636-
let norm_cause = ObligationCause::misc(return_span, impl_m_hir_id);
639+
let norm_cause = ObligationCause::misc(return_span, impl_m_def_id);
637640
let impl_sig = ocx.normalize(
638641
&norm_cause,
639642
param_env,
@@ -650,7 +653,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
650653
// the ImplTraitInTraitCollector, which gathers all of the RPITITs and replaces
651654
// them with inference variables.
652655
// We will use these inference variables to collect the hidden types of RPITITs.
653-
let mut collector = ImplTraitInTraitCollector::new(&ocx, return_span, param_env, impl_m_hir_id);
656+
let mut collector = ImplTraitInTraitCollector::new(&ocx, return_span, param_env, impl_m_def_id);
654657
let unnormalized_trait_sig = tcx
655658
.liberate_late_bound_regions(
656659
impl_m.def_id,
@@ -732,12 +735,11 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
732735
let outlives_environment = OutlivesEnvironment::with_bounds(
733736
param_env,
734737
Some(infcx),
735-
infcx.implied_bounds_tys(param_env, impl_m_hir_id, wf_tys),
738+
infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys),
736739
);
737-
infcx.err_ctxt().check_region_obligations_and_report_errors(
738-
impl_m.def_id.expect_local(),
739-
&outlives_environment,
740-
)?;
740+
infcx
741+
.err_ctxt()
742+
.check_region_obligations_and_report_errors(impl_m_def_id, &outlives_environment)?;
741743

742744
let mut collected_tys = FxHashMap::default();
743745
for (def_id, (ty, substs)) in collector.types {
@@ -819,15 +821,15 @@ struct ImplTraitInTraitCollector<'a, 'tcx> {
819821
types: FxHashMap<DefId, (Ty<'tcx>, ty::SubstsRef<'tcx>)>,
820822
span: Span,
821823
param_env: ty::ParamEnv<'tcx>,
822-
body_id: hir::HirId,
824+
body_id: LocalDefId,
823825
}
824826

825827
impl<'a, 'tcx> ImplTraitInTraitCollector<'a, 'tcx> {
826828
fn new(
827829
ocx: &'a ObligationCtxt<'a, 'tcx>,
828830
span: Span,
829831
param_env: ty::ParamEnv<'tcx>,
830-
body_id: hir::HirId,
832+
body_id: LocalDefId,
831833
) -> Self {
832834
ImplTraitInTraitCollector { ocx, types: FxHashMap::default(), span, param_env, body_id }
833835
}
@@ -1671,14 +1673,12 @@ pub(super) fn compare_impl_const_raw(
16711673

16721674
// Create a parameter environment that represents the implementation's
16731675
// method.
1674-
let impl_c_hir_id = tcx.hir().local_def_id_to_hir_id(impl_const_item_def);
1675-
16761676
// Compute placeholder form of impl and trait const tys.
16771677
let impl_ty = tcx.type_of(impl_const_item_def.to_def_id());
16781678
let trait_ty = tcx.bound_type_of(trait_const_item_def).subst(tcx, trait_to_impl_substs);
16791679
let mut cause = ObligationCause::new(
16801680
impl_c_span,
1681-
impl_c_hir_id,
1681+
impl_const_item_def,
16821682
ObligationCauseCode::CompareImplItemObligation {
16831683
impl_item_def_id: impl_const_item_def,
16841684
trait_item_def_id: trait_const_item_def,
@@ -1799,7 +1799,7 @@ fn compare_type_predicate_entailment<'tcx>(
17991799
// This `HirId` should be used for the `body_id` field on each
18001800
// `ObligationCause` (and the `FnCtxt`). This is what
18011801
// `regionck_item` expects.
1802-
let impl_ty_hir_id = tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local());
1802+
let impl_ty_def_id = impl_ty.def_id.expect_local();
18031803
debug!("compare_type_predicate_entailment: trait_to_impl_substs={:?}", trait_to_impl_substs);
18041804

18051805
// The predicates declared by the impl definition, the trait and the
@@ -1814,7 +1814,7 @@ fn compare_type_predicate_entailment<'tcx>(
18141814

18151815
debug!("compare_type_predicate_entailment: bounds={:?}", hybrid_preds);
18161816

1817-
let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_hir_id);
1817+
let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_def_id);
18181818
let param_env = ty::ParamEnv::new(
18191819
tcx.intern_predicates(&hybrid_preds.predicates),
18201820
Reveal::UserFacing,
@@ -1827,12 +1827,12 @@ fn compare_type_predicate_entailment<'tcx>(
18271827
debug!("compare_type_predicate_entailment: caller_bounds={:?}", param_env.caller_bounds());
18281828

18291829
for (predicate, span) in impl_ty_own_bounds {
1830-
let cause = ObligationCause::misc(span, impl_ty_hir_id);
1830+
let cause = ObligationCause::misc(span, impl_ty_def_id);
18311831
let predicate = ocx.normalize(&cause, param_env, predicate);
18321832

18331833
let cause = ObligationCause::new(
18341834
span,
1835-
impl_ty_hir_id,
1835+
impl_ty_def_id,
18361836
ObligationCauseCode::CompareImplItemObligation {
18371837
impl_item_def_id: impl_ty.def_id.expect_local(),
18381838
trait_item_def_id: trait_ty.def_id,
@@ -2008,7 +2008,7 @@ pub(super) fn check_type_bounds<'tcx>(
20082008
};
20092009
debug!(?normalize_param_env);
20102010

2011-
let impl_ty_hir_id = tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local());
2011+
let impl_ty_def_id = impl_ty.def_id.expect_local();
20122012
let impl_ty_substs = InternalSubsts::identity_for_item(tcx, impl_ty.def_id);
20132013
let rebased_substs = impl_ty_substs.rebase_onto(tcx, container_id, impl_trait_ref.substs);
20142014

@@ -2020,7 +2020,7 @@ pub(super) fn check_type_bounds<'tcx>(
20202020

20212021
let normalize_cause = ObligationCause::new(
20222022
impl_ty_span,
2023-
impl_ty_hir_id,
2023+
impl_ty_def_id,
20242024
ObligationCauseCode::CheckAssociatedTypeBounds {
20252025
impl_item_def_id: impl_ty.def_id.expect_local(),
20262026
trait_item_def_id: trait_ty.def_id,
@@ -2032,7 +2032,7 @@ pub(super) fn check_type_bounds<'tcx>(
20322032
} else {
20332033
traits::BindingObligation(trait_ty.def_id, span)
20342034
};
2035-
ObligationCause::new(impl_ty_span, impl_ty_hir_id, code)
2035+
ObligationCause::new(impl_ty_span, impl_ty_def_id, code)
20362036
};
20372037

20382038
let obligations = tcx
@@ -2063,7 +2063,7 @@ pub(super) fn check_type_bounds<'tcx>(
20632063

20642064
// Finally, resolve all regions. This catches wily misuses of
20652065
// lifetime parameters.
2066-
let implied_bounds = infcx.implied_bounds_tys(param_env, impl_ty_hir_id, assumed_wf_types);
2066+
let implied_bounds = infcx.implied_bounds_tys(param_env, impl_ty_def_id, assumed_wf_types);
20672067
let outlives_environment =
20682068
OutlivesEnvironment::with_bounds(param_env, Some(&infcx), implied_bounds);
20692069

Diff for: compiler/rustc_hir_analysis/src/check/intrinsic.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ fn equate_intrinsic_type<'tcx>(
5656
&& gen_count_ok(own_counts.consts, 0, "const")
5757
{
5858
let fty = tcx.mk_fn_ptr(sig);
59-
let cause = ObligationCause::new(it.span, it.hir_id(), ObligationCauseCode::IntrinsicType);
59+
let it_def_id = it.owner_id.def_id;
60+
let cause = ObligationCause::new(it.span, it_def_id, ObligationCauseCode::IntrinsicType);
6061
require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(it.owner_id)), fty);
6162
}
6263
}

0 commit comments

Comments
 (0)