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>
This commit is contained in:
Vincenzo Palazzo 2023-01-15 12:58:46 +01:00
parent 6b3cd03fdb
commit 7d2c1103d7
48 changed files with 265 additions and 280 deletions

View file

@ -766,7 +766,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let copy_did = infcx.tcx.require_lang_item(LangItem::Copy, Some(span)); let copy_did = infcx.tcx.require_lang_item(LangItem::Copy, Some(span));
let cause = ObligationCause::new( let cause = ObligationCause::new(
span, span,
self.mir_hir_id(), self.mir_def_id(),
rustc_infer::traits::ObligationCauseCode::MiscObligation, rustc_infer::traits::ObligationCauseCode::MiscObligation,
); );
let errors = rustc_trait_selection::traits::fully_solve_bound( let errors = rustc_trait_selection::traits::fully_solve_bound(

View file

@ -7,7 +7,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::graph::scc::Sccs; use rustc_data_structures::graph::scc::Sccs;
use rustc_errors::Diagnostic; use rustc_errors::Diagnostic;
use rustc_hir::def_id::CRATE_DEF_ID; use rustc_hir::def_id::CRATE_DEF_ID;
use rustc_hir::CRATE_HIR_ID;
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_infer::infer::outlives::test_type_match; use rustc_infer::infer::outlives::test_type_match;
use rustc_infer::infer::region_constraints::{GenericKind, VarInfos, VerifyBound, VerifyIfEq}; use rustc_infer::infer::region_constraints::{GenericKind, VarInfos, VerifyBound, VerifyIfEq};
@ -2022,7 +2021,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
.map(|constraint| BlameConstraint { .map(|constraint| BlameConstraint {
category: constraint.category, category: constraint.category,
from_closure: constraint.from_closure, from_closure: constraint.from_closure,
cause: ObligationCause::new(constraint.span, CRATE_HIR_ID, cause_code.clone()), cause: ObligationCause::new(constraint.span, CRATE_DEF_ID, cause_code.clone()),
variance_info: constraint.variance_info, variance_info: constraint.variance_info,
outlives_constraint: *constraint, outlives_constraint: *constraint,
}) })

View file

@ -273,7 +273,6 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
// This logic duplicates most of `check_opaque_meets_bounds`. // This logic duplicates most of `check_opaque_meets_bounds`.
// FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely. // FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely.
let param_env = self.tcx.param_env(def_id); let param_env = self.tcx.param_env(def_id);
let body_id = self.tcx.local_def_id_to_hir_id(def_id);
// HACK This bubble is required for this tests to pass: // HACK This bubble is required for this tests to pass:
// type-alias-impl-trait/issue-67844-nested-opaque.rs // type-alias-impl-trait/issue-67844-nested-opaque.rs
let infcx = let infcx =
@ -290,7 +289,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
// the bounds that the function supplies. // the bounds that the function supplies.
let opaque_ty = self.tcx.mk_opaque(def_id.to_def_id(), id_substs); let opaque_ty = self.tcx.mk_opaque(def_id.to_def_id(), id_substs);
if let Err(err) = ocx.eq( if let Err(err) = ocx.eq(
&ObligationCause::misc(instantiated_ty.span, body_id), &ObligationCause::misc(instantiated_ty.span, def_id),
param_env, param_env,
opaque_ty, opaque_ty,
definition_ty, definition_ty,
@ -298,7 +297,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
infcx infcx
.err_ctxt() .err_ctxt()
.report_mismatched_types( .report_mismatched_types(
&ObligationCause::misc(instantiated_ty.span, body_id), &ObligationCause::misc(instantiated_ty.span, def_id),
opaque_ty, opaque_ty,
definition_ty, definition_ty,
err, err,
@ -309,7 +308,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
ocx.register_obligation(Obligation::misc( ocx.register_obligation(Obligation::misc(
infcx.tcx, infcx.tcx,
instantiated_ty.span, instantiated_ty.span,
body_id, def_id,
param_env, param_env,
predicate, predicate,
)); ));

View file

@ -754,12 +754,9 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
let ocx = ObligationCtxt::new(&infcx); let ocx = ObligationCtxt::new(&infcx);
let predicates = tcx.predicates_of(callee).instantiate(tcx, substs); let predicates = tcx.predicates_of(callee).instantiate(tcx, substs);
let hir_id = tcx
.hir()
.local_def_id_to_hir_id(self.body.source.def_id().expect_local());
let cause = ObligationCause::new( let cause = ObligationCause::new(
terminator.source_info.span, terminator.source_info.span,
hir_id, self.body.source.def_id().expect_local(),
ObligationCauseCode::ItemObligation(callee), ObligationCauseCode::ItemObligation(callee),
); );
let normalized_predicates = ocx.normalize(&cause, param_env, predicates); let normalized_predicates = ocx.normalize(&cause, param_env, predicates);

View file

@ -2,11 +2,11 @@ use crate::errors::AutoDerefReachedRecursionLimit;
use crate::traits::query::evaluate_obligation::InferCtxtExt; use crate::traits::query::evaluate_obligation::InferCtxtExt;
use crate::traits::NormalizeExt; use crate::traits::NormalizeExt;
use crate::traits::{self, TraitEngine, TraitEngineExt}; use crate::traits::{self, TraitEngine, TraitEngineExt};
use rustc_hir as hir;
use rustc_infer::infer::InferCtxt; use rustc_infer::infer::InferCtxt;
use rustc_middle::ty::TypeVisitable; use rustc_middle::ty::TypeVisitable;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::Limit; use rustc_session::Limit;
use rustc_span::def_id::LocalDefId;
use rustc_span::def_id::LOCAL_CRATE; use rustc_span::def_id::LOCAL_CRATE;
use rustc_span::Span; use rustc_span::Span;
@ -28,7 +28,7 @@ pub struct Autoderef<'a, 'tcx> {
// Meta infos: // Meta infos:
infcx: &'a InferCtxt<'tcx>, infcx: &'a InferCtxt<'tcx>,
span: Span, span: Span,
body_id: hir::HirId, body_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
// Current state: // Current state:
@ -96,14 +96,14 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
pub fn new( pub fn new(
infcx: &'a InferCtxt<'tcx>, infcx: &'a InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_def_id: LocalDefId,
span: Span, span: Span,
base_ty: Ty<'tcx>, base_ty: Ty<'tcx>,
) -> Autoderef<'a, 'tcx> { ) -> Autoderef<'a, 'tcx> {
Autoderef { Autoderef {
infcx, infcx,
span, span,
body_id, body_id: body_def_id,
param_env, param_env,
state: AutoderefSnapshot { state: AutoderefSnapshot {
steps: vec![], steps: vec![],

View file

@ -412,7 +412,6 @@ fn check_opaque_meets_bounds<'tcx>(
span: Span, span: Span,
origin: &hir::OpaqueTyOrigin, origin: &hir::OpaqueTyOrigin,
) { ) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let defining_use_anchor = match *origin { let defining_use_anchor = match *origin {
hir::OpaqueTyOrigin::FnReturn(did) | hir::OpaqueTyOrigin::AsyncFn(did) => did, hir::OpaqueTyOrigin::FnReturn(did) | hir::OpaqueTyOrigin::AsyncFn(did) => did,
hir::OpaqueTyOrigin::TyAlias => def_id, hir::OpaqueTyOrigin::TyAlias => def_id,
@ -438,7 +437,7 @@ fn check_opaque_meets_bounds<'tcx>(
_ => re, _ => re,
}); });
let misc_cause = traits::ObligationCause::misc(span, hir_id); let misc_cause = traits::ObligationCause::misc(span, def_id);
match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) { match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
Ok(()) => {} Ok(()) => {}

View file

@ -147,12 +147,12 @@ fn compare_method_predicate_entailment<'tcx>(
// //
// FIXME(@lcnr): remove that after removing `cause.body_id` from // FIXME(@lcnr): remove that after removing `cause.body_id` from
// obligations. // obligations.
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local()); let impl_m_def_id = impl_m.def_id.expect_local();
let cause = ObligationCause::new( let cause = ObligationCause::new(
impl_m_span, impl_m_span,
impl_m_hir_id, impl_m_def_id,
ObligationCauseCode::CompareImplItemObligation { ObligationCauseCode::CompareImplItemObligation {
impl_item_def_id: impl_m.def_id.expect_local(), impl_item_def_id: impl_m_def_id,
trait_item_def_id: trait_m.def_id, trait_item_def_id: trait_m.def_id,
kind: impl_m.kind, kind: impl_m.kind,
}, },
@ -198,7 +198,7 @@ fn compare_method_predicate_entailment<'tcx>(
// Construct trait parameter environment and then shift it into the placeholder viewpoint. // Construct trait parameter environment and then shift it into the placeholder viewpoint.
// The key step here is to update the caller_bounds's predicates to be // The key step here is to update the caller_bounds's predicates to be
// the new hybrid bounds we computed. // the new hybrid bounds we computed.
let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_hir_id); let normalize_cause = traits::ObligationCause::misc(impl_m_span, impl_m_def_id);
let param_env = ty::ParamEnv::new( let param_env = ty::ParamEnv::new(
tcx.intern_predicates(&hybrid_preds.predicates), tcx.intern_predicates(&hybrid_preds.predicates),
Reveal::UserFacing, Reveal::UserFacing,
@ -213,14 +213,14 @@ fn compare_method_predicate_entailment<'tcx>(
let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_placeholder_substs); let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_placeholder_substs);
for (predicate, span) in impl_m_own_bounds { for (predicate, span) in impl_m_own_bounds {
let normalize_cause = traits::ObligationCause::misc(span, impl_m_hir_id); let normalize_cause = traits::ObligationCause::misc(span, impl_m_def_id);
let predicate = ocx.normalize(&normalize_cause, param_env, predicate); let predicate = ocx.normalize(&normalize_cause, param_env, predicate);
let cause = ObligationCause::new( let cause = ObligationCause::new(
span, span,
impl_m_hir_id, impl_m_def_id,
ObligationCauseCode::CompareImplItemObligation { ObligationCauseCode::CompareImplItemObligation {
impl_item_def_id: impl_m.def_id.expect_local(), impl_item_def_id: impl_m_def_id,
trait_item_def_id: trait_m.def_id, trait_item_def_id: trait_m.def_id,
kind: impl_m.kind, kind: impl_m.kind,
}, },
@ -253,7 +253,7 @@ fn compare_method_predicate_entailment<'tcx>(
); );
let unnormalized_impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(unnormalized_impl_sig)); let unnormalized_impl_fty = tcx.mk_fn_ptr(ty::Binder::dummy(unnormalized_impl_sig));
let norm_cause = ObligationCause::misc(impl_m_span, impl_m_hir_id); let norm_cause = ObligationCause::misc(impl_m_span, impl_m_def_id);
let impl_sig = ocx.normalize(&norm_cause, param_env, unnormalized_impl_sig); let impl_sig = ocx.normalize(&norm_cause, param_env, unnormalized_impl_sig);
debug!("compare_impl_method: impl_fty={:?}", impl_sig); debug!("compare_impl_method: impl_fty={:?}", impl_sig);
@ -311,6 +311,7 @@ fn compare_method_predicate_entailment<'tcx>(
if !errors.is_empty() { if !errors.is_empty() {
match check_implied_wf { match check_implied_wf {
CheckImpliedWfMode::Check => { CheckImpliedWfMode::Check => {
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m_def_id);
return compare_method_predicate_entailment( return compare_method_predicate_entailment(
tcx, tcx,
impl_m, impl_m,
@ -336,7 +337,7 @@ fn compare_method_predicate_entailment<'tcx>(
let outlives_env = OutlivesEnvironment::with_bounds( let outlives_env = OutlivesEnvironment::with_bounds(
param_env, param_env,
Some(infcx), Some(infcx),
infcx.implied_bounds_tys(param_env, impl_m_hir_id, wf_tys.clone()), infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys.clone()),
); );
infcx.process_registered_region_obligations( infcx.process_registered_region_obligations(
outlives_env.region_bound_pairs(), outlives_env.region_bound_pairs(),
@ -346,6 +347,7 @@ fn compare_method_predicate_entailment<'tcx>(
if !errors.is_empty() { if !errors.is_empty() {
// FIXME(compiler-errors): This can be simplified when IMPLIED_BOUNDS_ENTAILMENT // FIXME(compiler-errors): This can be simplified when IMPLIED_BOUNDS_ENTAILMENT
// becomes a hard error (i.e. ideally we'd just call `resolve_regions_and_report_errors` // becomes a hard error (i.e. ideally we'd just call `resolve_regions_and_report_errors`
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m_def_id);
match check_implied_wf { match check_implied_wf {
CheckImpliedWfMode::Check => { CheckImpliedWfMode::Check => {
return compare_method_predicate_entailment( return compare_method_predicate_entailment(
@ -371,7 +373,7 @@ fn compare_method_predicate_entailment<'tcx>(
} }
CheckImpliedWfMode::Skip => { CheckImpliedWfMode::Skip => {
if infcx.tainted_by_errors().is_none() { if infcx.tainted_by_errors().is_none() {
infcx.err_ctxt().report_region_errors(impl_m.def_id.expect_local(), &errors); infcx.err_ctxt().report_region_errors(impl_m_def_id, &errors);
} }
return Err(tcx return Err(tcx
.sess .sess
@ -610,13 +612,14 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
let trait_to_impl_substs = impl_trait_ref.substs; let trait_to_impl_substs = impl_trait_ref.substs;
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local()); let impl_m_def_id = impl_m.def_id.expect_local();
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m_def_id);
let return_span = tcx.hir().fn_decl_by_hir_id(impl_m_hir_id).unwrap().output.span(); let return_span = tcx.hir().fn_decl_by_hir_id(impl_m_hir_id).unwrap().output.span();
let cause = ObligationCause::new( let cause = ObligationCause::new(
return_span, return_span,
impl_m_hir_id, impl_m_def_id,
ObligationCauseCode::CompareImplItemObligation { ObligationCauseCode::CompareImplItemObligation {
impl_item_def_id: impl_m.def_id.expect_local(), impl_item_def_id: impl_m_def_id,
trait_item_def_id: trait_m.def_id, trait_item_def_id: trait_m.def_id,
kind: impl_m.kind, kind: impl_m.kind,
}, },
@ -633,7 +636,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
let ocx = ObligationCtxt::new(infcx); let ocx = ObligationCtxt::new(infcx);
// Normalize the impl signature with fresh variables for lifetime inference. // Normalize the impl signature with fresh variables for lifetime inference.
let norm_cause = ObligationCause::misc(return_span, impl_m_hir_id); let norm_cause = ObligationCause::misc(return_span, impl_m_def_id);
let impl_sig = ocx.normalize( let impl_sig = ocx.normalize(
&norm_cause, &norm_cause,
param_env, param_env,
@ -650,7 +653,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
// the ImplTraitInTraitCollector, which gathers all of the RPITITs and replaces // the ImplTraitInTraitCollector, which gathers all of the RPITITs and replaces
// them with inference variables. // them with inference variables.
// We will use these inference variables to collect the hidden types of RPITITs. // We will use these inference variables to collect the hidden types of RPITITs.
let mut collector = ImplTraitInTraitCollector::new(&ocx, return_span, param_env, impl_m_hir_id); let mut collector = ImplTraitInTraitCollector::new(&ocx, return_span, param_env, impl_m_def_id);
let unnormalized_trait_sig = tcx let unnormalized_trait_sig = tcx
.liberate_late_bound_regions( .liberate_late_bound_regions(
impl_m.def_id, impl_m.def_id,
@ -732,12 +735,11 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
let outlives_environment = OutlivesEnvironment::with_bounds( let outlives_environment = OutlivesEnvironment::with_bounds(
param_env, param_env,
Some(infcx), Some(infcx),
infcx.implied_bounds_tys(param_env, impl_m_hir_id, wf_tys), infcx.implied_bounds_tys(param_env, impl_m_def_id, wf_tys),
); );
infcx.err_ctxt().check_region_obligations_and_report_errors( infcx
impl_m.def_id.expect_local(), .err_ctxt()
&outlives_environment, .check_region_obligations_and_report_errors(impl_m_def_id, &outlives_environment)?;
)?;
let mut collected_tys = FxHashMap::default(); let mut collected_tys = FxHashMap::default();
for (def_id, (ty, substs)) in collector.types { for (def_id, (ty, substs)) in collector.types {
@ -819,7 +821,7 @@ struct ImplTraitInTraitCollector<'a, 'tcx> {
types: FxHashMap<DefId, (Ty<'tcx>, ty::SubstsRef<'tcx>)>, types: FxHashMap<DefId, (Ty<'tcx>, ty::SubstsRef<'tcx>)>,
span: Span, span: Span,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
} }
impl<'a, 'tcx> ImplTraitInTraitCollector<'a, 'tcx> { impl<'a, 'tcx> ImplTraitInTraitCollector<'a, 'tcx> {
@ -827,7 +829,7 @@ impl<'a, 'tcx> ImplTraitInTraitCollector<'a, 'tcx> {
ocx: &'a ObligationCtxt<'a, 'tcx>, ocx: &'a ObligationCtxt<'a, 'tcx>,
span: Span, span: Span,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
) -> Self { ) -> Self {
ImplTraitInTraitCollector { ocx, types: FxHashMap::default(), span, param_env, body_id } ImplTraitInTraitCollector { ocx, types: FxHashMap::default(), span, param_env, body_id }
} }
@ -1671,14 +1673,12 @@ pub(super) fn compare_impl_const_raw(
// Create a parameter environment that represents the implementation's // Create a parameter environment that represents the implementation's
// method. // method.
let impl_c_hir_id = tcx.hir().local_def_id_to_hir_id(impl_const_item_def);
// Compute placeholder form of impl and trait const tys. // Compute placeholder form of impl and trait const tys.
let impl_ty = tcx.type_of(impl_const_item_def.to_def_id()); let impl_ty = tcx.type_of(impl_const_item_def.to_def_id());
let trait_ty = tcx.bound_type_of(trait_const_item_def).subst(tcx, trait_to_impl_substs); let trait_ty = tcx.bound_type_of(trait_const_item_def).subst(tcx, trait_to_impl_substs);
let mut cause = ObligationCause::new( let mut cause = ObligationCause::new(
impl_c_span, impl_c_span,
impl_c_hir_id, impl_const_item_def,
ObligationCauseCode::CompareImplItemObligation { ObligationCauseCode::CompareImplItemObligation {
impl_item_def_id: impl_const_item_def, impl_item_def_id: impl_const_item_def,
trait_item_def_id: trait_const_item_def, trait_item_def_id: trait_const_item_def,
@ -1799,7 +1799,7 @@ fn compare_type_predicate_entailment<'tcx>(
// This `HirId` should be used for the `body_id` field on each // This `HirId` should be used for the `body_id` field on each
// `ObligationCause` (and the `FnCtxt`). This is what // `ObligationCause` (and the `FnCtxt`). This is what
// `regionck_item` expects. // `regionck_item` expects.
let impl_ty_hir_id = tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local()); let impl_ty_def_id = impl_ty.def_id.expect_local();
debug!("compare_type_predicate_entailment: trait_to_impl_substs={:?}", trait_to_impl_substs); debug!("compare_type_predicate_entailment: trait_to_impl_substs={:?}", trait_to_impl_substs);
// The predicates declared by the impl definition, the trait and the // The predicates declared by the impl definition, the trait and the
@ -1814,7 +1814,7 @@ fn compare_type_predicate_entailment<'tcx>(
debug!("compare_type_predicate_entailment: bounds={:?}", hybrid_preds); debug!("compare_type_predicate_entailment: bounds={:?}", hybrid_preds);
let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_hir_id); let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_def_id);
let param_env = ty::ParamEnv::new( let param_env = ty::ParamEnv::new(
tcx.intern_predicates(&hybrid_preds.predicates), tcx.intern_predicates(&hybrid_preds.predicates),
Reveal::UserFacing, Reveal::UserFacing,
@ -1827,12 +1827,12 @@ fn compare_type_predicate_entailment<'tcx>(
debug!("compare_type_predicate_entailment: caller_bounds={:?}", param_env.caller_bounds()); debug!("compare_type_predicate_entailment: caller_bounds={:?}", param_env.caller_bounds());
for (predicate, span) in impl_ty_own_bounds { for (predicate, span) in impl_ty_own_bounds {
let cause = ObligationCause::misc(span, impl_ty_hir_id); let cause = ObligationCause::misc(span, impl_ty_def_id);
let predicate = ocx.normalize(&cause, param_env, predicate); let predicate = ocx.normalize(&cause, param_env, predicate);
let cause = ObligationCause::new( let cause = ObligationCause::new(
span, span,
impl_ty_hir_id, impl_ty_def_id,
ObligationCauseCode::CompareImplItemObligation { ObligationCauseCode::CompareImplItemObligation {
impl_item_def_id: impl_ty.def_id.expect_local(), impl_item_def_id: impl_ty.def_id.expect_local(),
trait_item_def_id: trait_ty.def_id, trait_item_def_id: trait_ty.def_id,
@ -2008,7 +2008,7 @@ pub(super) fn check_type_bounds<'tcx>(
}; };
debug!(?normalize_param_env); debug!(?normalize_param_env);
let impl_ty_hir_id = tcx.hir().local_def_id_to_hir_id(impl_ty.def_id.expect_local()); let impl_ty_def_id = impl_ty.def_id.expect_local();
let impl_ty_substs = InternalSubsts::identity_for_item(tcx, impl_ty.def_id); let impl_ty_substs = InternalSubsts::identity_for_item(tcx, impl_ty.def_id);
let rebased_substs = impl_ty_substs.rebase_onto(tcx, container_id, impl_trait_ref.substs); let rebased_substs = impl_ty_substs.rebase_onto(tcx, container_id, impl_trait_ref.substs);
@ -2020,7 +2020,7 @@ pub(super) fn check_type_bounds<'tcx>(
let normalize_cause = ObligationCause::new( let normalize_cause = ObligationCause::new(
impl_ty_span, impl_ty_span,
impl_ty_hir_id, impl_ty_def_id,
ObligationCauseCode::CheckAssociatedTypeBounds { ObligationCauseCode::CheckAssociatedTypeBounds {
impl_item_def_id: impl_ty.def_id.expect_local(), impl_item_def_id: impl_ty.def_id.expect_local(),
trait_item_def_id: trait_ty.def_id, trait_item_def_id: trait_ty.def_id,
@ -2032,7 +2032,7 @@ pub(super) fn check_type_bounds<'tcx>(
} else { } else {
traits::BindingObligation(trait_ty.def_id, span) traits::BindingObligation(trait_ty.def_id, span)
}; };
ObligationCause::new(impl_ty_span, impl_ty_hir_id, code) ObligationCause::new(impl_ty_span, impl_ty_def_id, code)
}; };
let obligations = tcx let obligations = tcx
@ -2063,7 +2063,7 @@ pub(super) fn check_type_bounds<'tcx>(
// Finally, resolve all regions. This catches wily misuses of // Finally, resolve all regions. This catches wily misuses of
// lifetime parameters. // lifetime parameters.
let implied_bounds = infcx.implied_bounds_tys(param_env, impl_ty_hir_id, assumed_wf_types); let implied_bounds = infcx.implied_bounds_tys(param_env, impl_ty_def_id, assumed_wf_types);
let outlives_environment = let outlives_environment =
OutlivesEnvironment::with_bounds(param_env, Some(&infcx), implied_bounds); OutlivesEnvironment::with_bounds(param_env, Some(&infcx), implied_bounds);

View file

@ -56,7 +56,8 @@ fn equate_intrinsic_type<'tcx>(
&& gen_count_ok(own_counts.consts, 0, "const") && gen_count_ok(own_counts.consts, 0, "const")
{ {
let fty = tcx.mk_fn_ptr(sig); let fty = tcx.mk_fn_ptr(sig);
let cause = ObligationCause::new(it.span, it.hir_id(), ObligationCauseCode::IntrinsicType); let it_def_id = it.owner_id.def_id;
let cause = ObligationCause::new(it.span, it_def_id, ObligationCauseCode::IntrinsicType);
require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(it.owner_id)), fty); require_same_types(tcx, &cause, tcx.mk_fn_ptr(tcx.fn_sig(it.owner_id)), fty);
} }
} }

View file

@ -37,7 +37,7 @@ use std::ops::{ControlFlow, Deref};
pub(super) struct WfCheckingCtxt<'a, 'tcx> { pub(super) struct WfCheckingCtxt<'a, 'tcx> {
pub(super) ocx: ObligationCtxt<'a, 'tcx>, pub(super) ocx: ObligationCtxt<'a, 'tcx>,
span: Span, span: Span,
body_id: hir::HirId, body_def_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
} }
impl<'a, 'tcx> Deref for WfCheckingCtxt<'a, 'tcx> { impl<'a, 'tcx> Deref for WfCheckingCtxt<'a, 'tcx> {
@ -59,7 +59,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
T: TypeFoldable<'tcx>, T: TypeFoldable<'tcx>,
{ {
self.ocx.normalize( self.ocx.normalize(
&ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)), &ObligationCause::new(span, self.body_def_id, ObligationCauseCode::WellFormed(loc)),
self.param_env, self.param_env,
value, value,
) )
@ -71,8 +71,11 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
loc: Option<WellFormedLoc>, loc: Option<WellFormedLoc>,
arg: ty::GenericArg<'tcx>, arg: ty::GenericArg<'tcx>,
) { ) {
let cause = let cause = traits::ObligationCause::new(
traits::ObligationCause::new(span, self.body_id, ObligationCauseCode::WellFormed(loc)); span,
self.body_def_id,
ObligationCauseCode::WellFormed(loc),
);
// for a type to be WF, we do not need to check if const trait predicates satisfy. // for a type to be WF, we do not need to check if const trait predicates satisfy.
let param_env = self.param_env.without_const(); let param_env = self.param_env.without_const();
self.ocx.register_obligation(traits::Obligation::new( self.ocx.register_obligation(traits::Obligation::new(
@ -93,11 +96,10 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
F: for<'a> FnOnce(&WfCheckingCtxt<'a, 'tcx>), F: for<'a> FnOnce(&WfCheckingCtxt<'a, 'tcx>),
{ {
let param_env = tcx.param_env(body_def_id); let param_env = tcx.param_env(body_def_id);
let body_id = tcx.hir().local_def_id_to_hir_id(body_def_id);
let infcx = &tcx.infer_ctxt().build(); let infcx = &tcx.infer_ctxt().build();
let ocx = ObligationCtxt::new(infcx); let ocx = ObligationCtxt::new(infcx);
let mut wfcx = WfCheckingCtxt { ocx, span, body_id, param_env }; let mut wfcx = WfCheckingCtxt { ocx, span, body_def_id, param_env };
if !tcx.features().trivial_bounds { if !tcx.features().trivial_bounds {
wfcx.check_false_global_bounds() wfcx.check_false_global_bounds()
@ -105,7 +107,7 @@ pub(super) fn enter_wf_checking_ctxt<'tcx, F>(
f(&mut wfcx); f(&mut wfcx);
let assumed_wf_types = wfcx.ocx.assumed_wf_types(param_env, span, body_def_id); let assumed_wf_types = wfcx.ocx.assumed_wf_types(param_env, span, body_def_id);
let implied_bounds = infcx.implied_bounds_tys(param_env, body_id, assumed_wf_types); let implied_bounds = infcx.implied_bounds_tys(param_env, body_def_id, assumed_wf_types);
let errors = wfcx.select_all_or_error(); let errors = wfcx.select_all_or_error();
if !errors.is_empty() { if !errors.is_empty() {
@ -374,7 +376,6 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
continue; continue;
} }
let item_hir_id = item.id.hir_id();
let param_env = tcx.param_env(item_def_id); let param_env = tcx.param_env(item_def_id);
let item_required_bounds = match item.kind { let item_required_bounds = match item.kind {
@ -390,7 +391,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
gather_gat_bounds( gather_gat_bounds(
tcx, tcx,
param_env, param_env,
item_hir_id, item_def_id.def_id,
sig.inputs_and_output, sig.inputs_and_output,
// We also assume that all of the function signature's parameter types // We also assume that all of the function signature's parameter types
// are well formed. // are well formed.
@ -412,7 +413,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
gather_gat_bounds( gather_gat_bounds(
tcx, tcx,
param_env, param_env,
item_hir_id, item_def_id.def_id,
tcx.explicit_item_bounds(item_def_id).to_vec(), tcx.explicit_item_bounds(item_def_id).to_vec(),
&FxIndexSet::default(), &FxIndexSet::default(),
gat_def_id.def_id, gat_def_id.def_id,
@ -458,7 +459,6 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
let gat_item_hir = tcx.hir().expect_trait_item(gat_def_id.def_id); let gat_item_hir = tcx.hir().expect_trait_item(gat_def_id.def_id);
debug!(?required_bounds); debug!(?required_bounds);
let param_env = tcx.param_env(gat_def_id); let param_env = tcx.param_env(gat_def_id);
let gat_hir = gat_item_hir.hir_id();
let mut unsatisfied_bounds: Vec<_> = required_bounds let mut unsatisfied_bounds: Vec<_> = required_bounds
.into_iter() .into_iter()
@ -466,13 +466,25 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
ty::PredicateKind::Clause(ty::Clause::RegionOutlives(ty::OutlivesPredicate( ty::PredicateKind::Clause(ty::Clause::RegionOutlives(ty::OutlivesPredicate(
a, a,
b, b,
))) => { ))) => !region_known_to_outlive(
!region_known_to_outlive(tcx, gat_hir, param_env, &FxIndexSet::default(), a, b) tcx,
} gat_def_id.def_id,
param_env,
&FxIndexSet::default(),
a,
b,
),
ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate( ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate(
a, a,
b, b,
))) => !ty_known_to_outlive(tcx, gat_hir, param_env, &FxIndexSet::default(), a, b), ))) => !ty_known_to_outlive(
tcx,
gat_def_id.def_id,
param_env,
&FxIndexSet::default(),
a,
b,
),
_ => bug!("Unexpected PredicateKind"), _ => bug!("Unexpected PredicateKind"),
}) })
.map(|clause| clause.to_string()) .map(|clause| clause.to_string())
@ -551,7 +563,7 @@ fn augment_param_env<'tcx>(
fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>( fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
item_hir: hir::HirId, item_def_id: LocalDefId,
to_check: T, to_check: T,
wf_tys: &FxIndexSet<Ty<'tcx>>, wf_tys: &FxIndexSet<Ty<'tcx>>,
gat_def_id: LocalDefId, gat_def_id: LocalDefId,
@ -584,7 +596,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
// reflected in a where clause on the GAT itself. // reflected in a where clause on the GAT itself.
for (ty, ty_idx) in &types { for (ty, ty_idx) in &types {
// In our example, requires that `Self: 'a` // In our example, requires that `Self: 'a`
if ty_known_to_outlive(tcx, item_hir, param_env, &wf_tys, *ty, *region_a) { if ty_known_to_outlive(tcx, item_def_id, param_env, &wf_tys, *ty, *region_a) {
debug!(?ty_idx, ?region_a_idx); debug!(?ty_idx, ?region_a_idx);
debug!("required clause: {ty} must outlive {region_a}"); debug!("required clause: {ty} must outlive {region_a}");
// Translate into the generic parameters of the GAT. In // Translate into the generic parameters of the GAT. In
@ -622,7 +634,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
if ty::ReStatic == **region_b || region_a == region_b { if ty::ReStatic == **region_b || region_a == region_b {
continue; continue;
} }
if region_known_to_outlive(tcx, item_hir, param_env, &wf_tys, *region_a, *region_b) { if region_known_to_outlive(tcx, item_def_id, param_env, &wf_tys, *region_a, *region_b) {
debug!(?region_a_idx, ?region_b_idx); debug!(?region_a_idx, ?region_b_idx);
debug!("required clause: {region_a} must outlive {region_b}"); debug!("required clause: {region_a} must outlive {region_b}");
// Translate into the generic parameters of the GAT. // Translate into the generic parameters of the GAT.
@ -658,7 +670,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
/// `ty` outlives `region`. /// `ty` outlives `region`.
fn ty_known_to_outlive<'tcx>( fn ty_known_to_outlive<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
id: hir::HirId, id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>, wf_tys: &FxIndexSet<Ty<'tcx>>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
@ -675,7 +687,7 @@ fn ty_known_to_outlive<'tcx>(
/// `region_a` outlives `region_b` /// `region_a` outlives `region_b`
fn region_known_to_outlive<'tcx>( fn region_known_to_outlive<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
id: hir::HirId, id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>, wf_tys: &FxIndexSet<Ty<'tcx>>,
region_a: ty::Region<'tcx>, region_a: ty::Region<'tcx>,
@ -699,7 +711,7 @@ fn region_known_to_outlive<'tcx>(
/// to be tested), then resolve region and return errors /// to be tested), then resolve region and return errors
fn resolve_regions_with_wf_tys<'tcx>( fn resolve_regions_with_wf_tys<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
id: hir::HirId, id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>, wf_tys: &FxIndexSet<Ty<'tcx>>,
add_constraints: impl for<'a> FnOnce(&'a InferCtxt<'tcx>, &'a RegionBoundPairs<'tcx>), add_constraints: impl for<'a> FnOnce(&'a InferCtxt<'tcx>, &'a RegionBoundPairs<'tcx>),
@ -1093,7 +1105,7 @@ fn check_type_defn<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'tcx>, all_sized: b
wfcx.register_bound( wfcx.register_bound(
traits::ObligationCause::new( traits::ObligationCause::new(
hir_ty.span, hir_ty.span,
wfcx.body_id, wfcx.body_def_id,
traits::FieldSized { traits::FieldSized {
adt_kind: match item_adt_kind(&item.kind) { adt_kind: match item_adt_kind(&item.kind) {
Some(i) => i, Some(i) => i,
@ -1113,7 +1125,7 @@ fn check_type_defn<'tcx>(tcx: TyCtxt<'tcx>, item: &hir::Item<'tcx>, all_sized: b
if let ty::VariantDiscr::Explicit(discr_def_id) = variant.discr { if let ty::VariantDiscr::Explicit(discr_def_id) = variant.discr {
let cause = traits::ObligationCause::new( let cause = traits::ObligationCause::new(
tcx.def_span(discr_def_id), tcx.def_span(discr_def_id),
wfcx.body_id, wfcx.body_def_id,
traits::MiscObligation, traits::MiscObligation,
); );
wfcx.register_obligation(traits::Obligation::new( wfcx.register_obligation(traits::Obligation::new(
@ -1174,7 +1186,7 @@ fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: &ty::AssocI
traits::wf::predicate_obligations( traits::wf::predicate_obligations(
wfcx.infcx, wfcx.infcx,
wfcx.param_env, wfcx.param_env,
wfcx.body_id, wfcx.body_def_id,
normalized_bound, normalized_bound,
bound_span, bound_span,
) )
@ -1214,7 +1226,7 @@ fn check_item_type(tcx: TyCtxt<'_>, item_id: LocalDefId, ty_span: Span, allow_fo
wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(item_id)), item_ty.into()); wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
if forbid_unsized { if forbid_unsized {
wfcx.register_bound( wfcx.register_bound(
traits::ObligationCause::new(ty_span, wfcx.body_id, traits::WellFormed(None)), traits::ObligationCause::new(ty_span, wfcx.body_def_id, traits::WellFormed(None)),
wfcx.param_env, wfcx.param_env,
item_ty, item_ty,
tcx.require_lang_item(LangItem::Sized, None), tcx.require_lang_item(LangItem::Sized, None),
@ -1229,7 +1241,7 @@ fn check_item_type(tcx: TyCtxt<'_>, item_id: LocalDefId, ty_span: Span, allow_fo
if should_check_for_sync { if should_check_for_sync {
wfcx.register_bound( wfcx.register_bound(
traits::ObligationCause::new(ty_span, wfcx.body_id, traits::SharedStatic), traits::ObligationCause::new(ty_span, wfcx.body_def_id, traits::SharedStatic),
wfcx.param_env, wfcx.param_env,
item_ty, item_ty,
tcx.require_lang_item(LangItem::Sync, Some(ty_span)), tcx.require_lang_item(LangItem::Sync, Some(ty_span)),
@ -1269,7 +1281,7 @@ fn check_impl<'tcx>(
let mut obligations = traits::wf::trait_obligations( let mut obligations = traits::wf::trait_obligations(
wfcx.infcx, wfcx.infcx,
wfcx.param_env, wfcx.param_env,
wfcx.body_id, wfcx.body_def_id,
&trait_pred, &trait_pred,
ast_trait_ref.path.span, ast_trait_ref.path.span,
item, item,
@ -1466,7 +1478,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
let pred = wfcx.normalize(sp, None, pred); let pred = wfcx.normalize(sp, None, pred);
let cause = traits::ObligationCause::new( let cause = traits::ObligationCause::new(
sp, sp,
wfcx.body_id, wfcx.body_def_id,
traits::ItemObligation(def_id.to_def_id()), traits::ItemObligation(def_id.to_def_id()),
); );
traits::Obligation::new(tcx, cause, wfcx.param_env, pred) traits::Obligation::new(tcx, cause, wfcx.param_env, pred)
@ -1482,12 +1494,11 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
traits::wf::predicate_obligations( traits::wf::predicate_obligations(
infcx, infcx,
wfcx.param_env.without_const(), wfcx.param_env.without_const(),
wfcx.body_id, wfcx.body_def_id,
p, p,
sp, sp,
) )
}); });
let obligations: Vec<_> = wf_obligations.chain(default_obligations).collect(); let obligations: Vec<_> = wf_obligations.chain(default_obligations).collect();
wfcx.register_obligations(obligations); wfcx.register_obligations(obligations);
} }
@ -1549,7 +1560,7 @@ fn check_fn_or_method<'tcx>(
// Check that the argument is a tuple // Check that the argument is a tuple
if let Some(ty) = inputs.next() { if let Some(ty) = inputs.next() {
wfcx.register_bound( wfcx.register_bound(
ObligationCause::new(span, wfcx.body_id, ObligationCauseCode::RustCall), ObligationCause::new(span, wfcx.body_def_id, ObligationCauseCode::RustCall),
wfcx.param_env, wfcx.param_env,
*ty, *ty,
tcx.require_lang_item(hir::LangItem::Tuple, Some(span)), tcx.require_lang_item(hir::LangItem::Tuple, Some(span)),
@ -1597,7 +1608,7 @@ fn check_return_position_impl_trait_in_trait_bounds<'tcx>(
traits::wf::predicate_obligations( traits::wf::predicate_obligations(
wfcx.infcx, wfcx.infcx,
wfcx.param_env, wfcx.param_env,
wfcx.body_id, wfcx.body_def_id,
normalized_bound, normalized_bound,
bound_span, bound_span,
) )
@ -1697,7 +1708,7 @@ fn receiver_is_valid<'tcx>(
let infcx = wfcx.infcx; let infcx = wfcx.infcx;
let tcx = wfcx.tcx(); let tcx = wfcx.tcx();
let cause = let cause =
ObligationCause::new(span, wfcx.body_id, traits::ObligationCauseCode::MethodReceiver); ObligationCause::new(span, wfcx.body_def_id, traits::ObligationCauseCode::MethodReceiver);
let can_eq_self = |ty| infcx.can_eq(wfcx.param_env, self_ty, ty).is_ok(); let can_eq_self = |ty| infcx.can_eq(wfcx.param_env, self_ty, ty).is_ok();
@ -1709,7 +1720,7 @@ fn receiver_is_valid<'tcx>(
return true; return true;
} }
let mut autoderef = Autoderef::new(infcx, wfcx.param_env, wfcx.body_id, span, receiver_ty); let mut autoderef = Autoderef::new(infcx, wfcx.param_env, wfcx.body_def_id, span, receiver_ty);
// The `arbitrary_self_types` feature allows raw pointer receivers like `self: *const Self`. // The `arbitrary_self_types` feature allows raw pointer receivers like `self: *const Self`.
if arbitrary_self_types_enabled { if arbitrary_self_types_enabled {
@ -1894,8 +1905,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
let mut span = self.span; let mut span = self.span;
let empty_env = ty::ParamEnv::empty(); let empty_env = ty::ParamEnv::empty();
let def_id = tcx.hir().local_def_id(self.body_id); let predicates_with_span = tcx.predicates_of(self.body_def_id).predicates.iter().copied();
let predicates_with_span = tcx.predicates_of(def_id).predicates.iter().copied();
// Check elaborated bounds. // Check elaborated bounds.
let implied_obligations = traits::elaborate_predicates_with_span(tcx, predicates_with_span); let implied_obligations = traits::elaborate_predicates_with_span(tcx, predicates_with_span);
@ -1910,7 +1920,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
// Match the existing behavior. // Match the existing behavior.
if pred.is_global() && !pred.has_late_bound_vars() { if pred.is_global() && !pred.has_late_bound_vars() {
let pred = self.normalize(span, None, pred); let pred = self.normalize(span, None, pred);
let hir_node = tcx.hir().find(self.body_id); let hir_node = tcx.hir().find_by_def_id(self.body_def_id);
// only use the span of the predicate clause (#90869) // only use the span of the predicate clause (#90869)
@ -1929,7 +1939,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
let obligation = traits::Obligation::new( let obligation = traits::Obligation::new(
tcx, tcx,
traits::ObligationCause::new(span, self.body_id, traits::TrivialBound), traits::ObligationCause::new(span, self.body_def_id, traits::TrivialBound),
empty_env, empty_env,
pred, pred,
); );

View file

@ -64,8 +64,6 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) { fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
debug!("visit_implementation_of_copy: impl_did={:?}", impl_did); debug!("visit_implementation_of_copy: impl_did={:?}", impl_did);
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did);
let self_type = tcx.type_of(impl_did); let self_type = tcx.type_of(impl_did);
debug!("visit_implementation_of_copy: self_type={:?} (bound)", self_type); debug!("visit_implementation_of_copy: self_type={:?} (bound)", self_type);
@ -80,7 +78,7 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
_ => bug!("expected Copy impl item"), _ => bug!("expected Copy impl item"),
}; };
let cause = traits::ObligationCause::misc(span, impl_hir_id); let cause = traits::ObligationCause::misc(span, impl_did);
match type_allowed_to_implement_copy(tcx, param_env, self_type, cause) { match type_allowed_to_implement_copy(tcx, param_env, self_type, cause) {
Ok(()) => {} Ok(()) => {}
Err(CopyImplementationError::InfrigingFields(fields)) => { Err(CopyImplementationError::InfrigingFields(fields)) => {
@ -224,7 +222,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
let create_err = |msg: &str| struct_span_err!(tcx.sess, span, E0378, "{}", msg); let create_err = |msg: &str| struct_span_err!(tcx.sess, span, E0378, "{}", msg);
let infcx = tcx.infer_ctxt().build(); let infcx = tcx.infer_ctxt().build();
let cause = ObligationCause::misc(span, impl_hir_id); let cause = ObligationCause::misc(span, impl_did);
use rustc_type_ir::sty::TyKind::*; use rustc_type_ir::sty::TyKind::*;
match (source.kind(), target.kind()) { match (source.kind(), target.kind()) {
@ -386,8 +384,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (free)", source, target); debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (free)", source, target);
let infcx = tcx.infer_ctxt().build(); let infcx = tcx.infer_ctxt().build();
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did); let cause = ObligationCause::misc(span, impl_did);
let cause = ObligationCause::misc(span, impl_hir_id);
let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>, let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>,
mt_b: ty::TypeAndMut<'tcx>, mt_b: ty::TypeAndMut<'tcx>,
mk_ptr: &dyn Fn(Ty<'tcx>) -> Ty<'tcx>| { mk_ptr: &dyn Fn(Ty<'tcx>) -> Ty<'tcx>| {
@ -575,7 +572,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
}; };
// Register an obligation for `A: Trait<B>`. // Register an obligation for `A: Trait<B>`.
let cause = traits::ObligationCause::misc(span, impl_hir_id); let cause = traits::ObligationCause::misc(span, impl_did);
let predicate = let predicate =
predicate_for_trait_def(tcx, param_env, cause, trait_def_id, 0, [source, target]); predicate_for_trait_def(tcx, param_env, cause, trait_def_id, 0, [source, target]);
let errors = traits::fully_solve_obligation(&infcx, predicate); let errors = traits::fully_solve_obligation(&infcx, predicate);

View file

@ -1248,11 +1248,12 @@ fn infer_return_ty_for_fn_sig<'tcx>(
} }
} }
// FIXME(vincenzopalazzo): remove the hir item when the refactoring is stable
fn suggest_impl_trait<'tcx>( fn suggest_impl_trait<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
ret_ty: Ty<'tcx>, ret_ty: Ty<'tcx>,
span: Span, span: Span,
hir_id: hir::HirId, _hir_id: hir::HirId,
def_id: LocalDefId, def_id: LocalDefId,
) -> Option<String> { ) -> Option<String> {
let format_as_assoc: fn(_, _, _, _, _) -> _ = let format_as_assoc: fn(_, _, _, _, _) -> _ =
@ -1324,7 +1325,7 @@ fn suggest_impl_trait<'tcx>(
} }
let ocx = ObligationCtxt::new_in_snapshot(&infcx); let ocx = ObligationCtxt::new_in_snapshot(&infcx);
let item_ty = ocx.normalize( let item_ty = ocx.normalize(
&ObligationCause::misc(span, hir_id), &ObligationCause::misc(span, def_id),
param_env, param_env,
tcx.mk_projection(assoc_item_def_id, substs), tcx.mk_projection(assoc_item_def_id, substs),
); );

View file

@ -1,11 +1,12 @@
use crate::collect::ItemCtxt; use crate::collect::ItemCtxt;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{ForeignItem, ForeignItemKind, HirId}; use rustc_hir::{ForeignItem, ForeignItemKind};
use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::{ObligationCause, WellFormedLoc}; use rustc_infer::traits::{ObligationCause, WellFormedLoc};
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, Region, TyCtxt, TypeFoldable, TypeFolder}; use rustc_middle::ty::{self, Region, TyCtxt, TypeFoldable, TypeFolder};
use rustc_span::def_id::LocalDefId;
use rustc_trait_selection::traits; use rustc_trait_selection::traits;
pub fn provide(providers: &mut Providers) { pub fn provide(providers: &mut Providers) {
@ -57,7 +58,7 @@ fn diagnostic_hir_wf_check<'tcx>(
cause: Option<ObligationCause<'tcx>>, cause: Option<ObligationCause<'tcx>>,
cause_depth: usize, cause_depth: usize,
icx: ItemCtxt<'tcx>, icx: ItemCtxt<'tcx>,
hir_id: HirId, def_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
depth: usize, depth: usize,
} }
@ -68,7 +69,7 @@ fn diagnostic_hir_wf_check<'tcx>(
let tcx_ty = self.icx.to_ty(ty).fold_with(&mut EraseAllBoundRegions { tcx: self.tcx }); let tcx_ty = self.icx.to_ty(ty).fold_with(&mut EraseAllBoundRegions { tcx: self.tcx });
let cause = traits::ObligationCause::new( let cause = traits::ObligationCause::new(
ty.span, ty.span,
self.hir_id, self.def_id,
traits::ObligationCauseCode::WellFormed(None), traits::ObligationCauseCode::WellFormed(None),
); );
let errors = traits::fully_solve_obligation( let errors = traits::fully_solve_obligation(
@ -106,7 +107,7 @@ fn diagnostic_hir_wf_check<'tcx>(
cause: None, cause: None,
cause_depth: 0, cause_depth: 0,
icx, icx,
hir_id, def_id,
param_env: tcx.param_env(def_id.to_def_id()), param_env: tcx.param_env(def_id.to_def_id()),
depth: 0, depth: 0,
}; };

View file

@ -164,7 +164,6 @@ fn get_impl_substs(
let infcx = &tcx.infer_ctxt().build(); let infcx = &tcx.infer_ctxt().build();
let ocx = ObligationCtxt::new(infcx); let ocx = ObligationCtxt::new(infcx);
let param_env = tcx.param_env(impl1_def_id); let param_env = tcx.param_env(impl1_def_id);
let impl1_hir_id = tcx.hir().local_def_id_to_hir_id(impl1_def_id);
let assumed_wf_types = let assumed_wf_types =
ocx.assumed_wf_types(param_env, tcx.def_span(impl1_def_id), impl1_def_id); ocx.assumed_wf_types(param_env, tcx.def_span(impl1_def_id), impl1_def_id);
@ -179,7 +178,7 @@ fn get_impl_substs(
return None; return None;
} }
let implied_bounds = infcx.implied_bounds_tys(param_env, impl1_hir_id, assumed_wf_types); let implied_bounds = infcx.implied_bounds_tys(param_env, impl1_def_id, assumed_wf_types);
let outlives_env = OutlivesEnvironment::with_bounds(param_env, Some(infcx), implied_bounds); let outlives_env = OutlivesEnvironment::with_bounds(param_env, Some(infcx), implied_bounds);
let _ = let _ =
infcx.err_ctxt().check_region_obligations_and_report_errors(impl1_def_id, &outlives_env); infcx.err_ctxt().check_region_obligations_and_report_errors(impl1_def_id, &outlives_env);
@ -372,15 +371,9 @@ fn check_predicates<'tcx>(
// Include the well-formed predicates of the type parameters of the impl. // Include the well-formed predicates of the type parameters of the impl.
for arg in tcx.impl_trait_ref(impl1_def_id).unwrap().subst_identity().substs { for arg in tcx.impl_trait_ref(impl1_def_id).unwrap().subst_identity().substs {
let infcx = &tcx.infer_ctxt().build(); let infcx = &tcx.infer_ctxt().build();
let obligations = wf::obligations( let obligations =
infcx, wf::obligations(infcx, tcx.param_env(impl1_def_id), impl1_def_id, 0, arg, span)
tcx.param_env(impl1_def_id), .unwrap();
tcx.hir().local_def_id_to_hir_id(impl1_def_id),
0,
arg,
span,
)
.unwrap();
assert!(!obligations.needs_infer()); assert!(!obligations.needs_infer());
impl2_predicates.extend( impl2_predicates.extend(

View file

@ -100,14 +100,14 @@ mod variance;
use rustc_errors::{struct_span_err, ErrorGuaranteed}; use rustc_errors::{struct_span_err, ErrorGuaranteed};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::Node;
use rustc_hir::{Node, CRATE_HIR_ID};
use rustc_infer::infer::{InferOk, TyCtxtInferExt}; use rustc_infer::infer::{InferOk, TyCtxtInferExt};
use rustc_middle::middle; use rustc_middle::middle;
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::util; use rustc_middle::util;
use rustc_session::{config::EntryFnType, parse::feature_err}; use rustc_session::{config::EntryFnType, parse::feature_err};
use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
use rustc_span::{symbol::sym, Span, DUMMY_SP}; use rustc_span::{symbol::sym, Span, DUMMY_SP};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _; use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
@ -185,16 +185,15 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
let main_fnsig = tcx.fn_sig(main_def_id); let main_fnsig = tcx.fn_sig(main_def_id);
let main_span = tcx.def_span(main_def_id); let main_span = tcx.def_span(main_def_id);
fn main_fn_diagnostics_hir_id(tcx: TyCtxt<'_>, def_id: DefId, sp: Span) -> hir::HirId { fn main_fn_diagnostics_def_id(tcx: TyCtxt<'_>, def_id: DefId, sp: Span) -> LocalDefId {
if let Some(local_def_id) = def_id.as_local() { if let Some(local_def_id) = def_id.as_local() {
let hir_id = tcx.hir().local_def_id_to_hir_id(local_def_id);
let hir_type = tcx.type_of(local_def_id); let hir_type = tcx.type_of(local_def_id);
if !matches!(hir_type.kind(), ty::FnDef(..)) { if !matches!(hir_type.kind(), ty::FnDef(..)) {
span_bug!(sp, "main has a non-function type: found `{}`", hir_type); span_bug!(sp, "main has a non-function type: found `{}`", hir_type);
} }
hir_id local_def_id
} else { } else {
CRATE_HIR_ID CRATE_DEF_ID
} }
} }
@ -251,7 +250,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
} }
let mut error = false; let mut error = false;
let main_diagnostics_hir_id = main_fn_diagnostics_hir_id(tcx, main_def_id, main_span); let main_diagnostics_def_id = main_fn_diagnostics_def_id(tcx, main_def_id, main_span);
let main_fn_generics = tcx.generics_of(main_def_id); let main_fn_generics = tcx.generics_of(main_def_id);
let main_fn_predicates = tcx.predicates_of(main_def_id); let main_fn_predicates = tcx.predicates_of(main_def_id);
if main_fn_generics.count() != 0 || !main_fnsig.bound_vars().is_empty() { if main_fn_generics.count() != 0 || !main_fnsig.bound_vars().is_empty() {
@ -326,7 +325,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
let param_env = ty::ParamEnv::empty(); let param_env = ty::ParamEnv::empty();
let cause = traits::ObligationCause::new( let cause = traits::ObligationCause::new(
return_ty_span, return_ty_span,
main_diagnostics_hir_id, main_diagnostics_def_id,
ObligationCauseCode::MainFunctionType, ObligationCauseCode::MainFunctionType,
); );
let ocx = traits::ObligationCtxt::new(&infcx); let ocx = traits::ObligationCtxt::new(&infcx);
@ -356,7 +355,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
tcx, tcx,
&ObligationCause::new( &ObligationCause::new(
main_span, main_span,
main_diagnostics_hir_id, main_diagnostics_def_id,
ObligationCauseCode::MainFunctionType, ObligationCauseCode::MainFunctionType,
), ),
se_ty, se_ty,
@ -444,7 +443,11 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
require_same_types( require_same_types(
tcx, tcx,
&ObligationCause::new(start_span, start_id, ObligationCauseCode::StartFunctionType), &ObligationCause::new(
start_span,
start_def_id,
ObligationCauseCode::StartFunctionType,
),
se_ty, se_ty,
tcx.mk_fn_ptr(tcx.fn_sig(start_def_id)), tcx.mk_fn_ptr(tcx.fn_sig(start_def_id)),
); );

View file

@ -186,10 +186,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>, prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
) { ) {
let hir = self.tcx.hir(); let hir = self.tcx.hir();
// First, check that we're actually in the tail of a function. // First, check that we're actually in the tail of a function.
let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Block(block, _), .. }) = let Some(body_id) = hir.maybe_body_owned_by(self.body_id) else { return; };
hir.get(self.body_id) else { return; }; let body = hir.body(body_id);
let hir::ExprKind::Block(block, _) = body.value.kind else { return; };
let Some(hir::Stmt { kind: hir::StmtKind::Semi(last_expr), .. }) let Some(hir::Stmt { kind: hir::StmtKind::Semi(last_expr), .. })
= block.innermost_block().stmts.last() else { return; }; = block.innermost_block().stmts.last() else { return; };
if last_expr.hir_id != expr.hir_id { if last_expr.hir_id != expr.hir_id {
@ -198,7 +198,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Next, make sure that we have no type expectation. // Next, make sure that we have no type expectation.
let Some(ret) = hir let Some(ret) = hir
.find_by_def_id(self.body_id.owner.def_id) .find_by_def_id(self.body_id)
.and_then(|owner| owner.fn_decl()) .and_then(|owner| owner.fn_decl())
.map(|decl| decl.output.span()) else { return; }; .map(|decl| decl.output.span()) else { return; };
let Expectation::IsLast(stmt) = expectation else { let Expectation::IsLast(stmt) = expectation else {

View file

@ -43,7 +43,7 @@ pub(super) fn check_fn<'a, 'tcx>(
let ret_ty = let ret_ty =
fcx.register_infer_ok_obligations(fcx.infcx.replace_opaque_types_with_inference_vars( fcx.register_infer_ok_obligations(fcx.infcx.replace_opaque_types_with_inference_vars(
declared_ret_ty, declared_ret_ty,
body.value.hir_id, fn_def_id,
decl.output.span(), decl.output.span(),
fcx.param_env, fcx.param_env,
)); ));

View file

@ -4,7 +4,6 @@ use super::{check_fn, Expectation, FnCtxt, GeneratorTypes};
use hir::def::DefKind; use hir::def::DefKind;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_hir_analysis::astconv::AstConv; use rustc_hir_analysis::astconv::AstConv;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
@ -14,6 +13,7 @@ use rustc_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::ty::subst::InternalSubsts; use rustc_middle::ty::subst::InternalSubsts;
use rustc_middle::ty::visit::TypeVisitable; use rustc_middle::ty::visit::TypeVisitable;
use rustc_middle::ty::{self, Ty, TypeSuperVisitable, TypeVisitor}; use rustc_middle::ty::{self, Ty, TypeSuperVisitable, TypeVisitor};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
use rustc_trait_selection::traits; use rustc_trait_selection::traits;
@ -80,7 +80,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!(?bound_sig, ?liberated_sig); debug!(?bound_sig, ?liberated_sig);
let mut fcx = FnCtxt::new(self, self.param_env.without_const(), body.value.hir_id); let mut fcx = FnCtxt::new(self, self.param_env.without_const(), closure.def_id);
let generator_types = check_fn( let generator_types = check_fn(
&mut fcx, &mut fcx,
liberated_sig, liberated_sig,
@ -620,8 +620,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// function. // function.
Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Fn)) => { Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Fn)) => {
debug!("closure is async fn body"); debug!("closure is async fn body");
self.deduce_future_output_from_obligations(expr_def_id, body.id().hir_id) let def_id = self.tcx.hir().body_owner_def_id(body.id());
.unwrap_or_else(|| { self.deduce_future_output_from_obligations(expr_def_id, def_id).unwrap_or_else(
|| {
// AFAIK, deducing the future output // AFAIK, deducing the future output
// always succeeds *except* in error cases // always succeeds *except* in error cases
// like #65159. I'd like to return Error // like #65159. I'd like to return Error
@ -630,7 +631,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// *have* reported an // *have* reported an
// error. --nikomatsakis // error. --nikomatsakis
astconv.ty_infer(None, decl.output.span()) astconv.ty_infer(None, decl.output.span())
}) },
)
} }
_ => astconv.ty_infer(None, decl.output.span()), _ => astconv.ty_infer(None, decl.output.span()),
@ -665,7 +667,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn deduce_future_output_from_obligations( fn deduce_future_output_from_obligations(
&self, &self,
expr_def_id: LocalDefId, expr_def_id: LocalDefId,
body_id: hir::HirId, body_def_id: LocalDefId,
) -> Option<Ty<'tcx>> { ) -> Option<Ty<'tcx>> {
let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| { let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
span_bug!(self.tcx.def_span(expr_def_id), "async fn generator outside of a fn") span_bug!(self.tcx.def_span(expr_def_id), "async fn generator outside of a fn")
@ -725,7 +727,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let InferOk { value: output_ty, obligations } = self let InferOk { value: output_ty, obligations } = self
.replace_opaque_types_with_inference_vars( .replace_opaque_types_with_inference_vars(
output_ty, output_ty,
body_id, body_def_id,
self.tcx.def_span(expr_def_id), self.tcx.def_span(expr_def_id),
self.param_env, self.param_env,
); );

View file

@ -852,7 +852,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Point any obligations that were registered due to opaque type // Point any obligations that were registered due to opaque type
// inference at the return expression. // inference at the return expression.
self.select_obligations_where_possible(|errors| { self.select_obligations_where_possible(|errors| {
self.point_at_return_for_opaque_ty_error(errors, span, return_expr_ty); self.point_at_return_for_opaque_ty_error(errors, span, return_expr_ty, return_expr.span);
}); });
} }
} }
@ -862,9 +862,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
errors: &mut Vec<traits::FulfillmentError<'tcx>>, errors: &mut Vec<traits::FulfillmentError<'tcx>>,
span: Span, span: Span,
return_expr_ty: Ty<'tcx>, return_expr_ty: Ty<'tcx>,
return_span: Span,
) { ) {
// Don't point at the whole block if it's empty // Don't point at the whole block if it's empty
if span == self.tcx.hir().span(self.body_id) { if span == return_span {
return; return;
} }
for err in errors { for err in errors {
@ -1374,7 +1375,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let body = self.tcx.hir().body(anon_const.body); let body = self.tcx.hir().body(anon_const.body);
// Create a new function context. // Create a new function context.
let fcx = FnCtxt::new(self, self.param_env.with_const(), body.value.hir_id); let def_id = anon_const.def_id;
let fcx = FnCtxt::new(self, self.param_env.with_const(), def_id);
crate::GatherLocalsVisitor::new(&fcx).visit_body(body); crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
let ty = fcx.check_expr_with_expectation(&body.value, expected); let ty = fcx.check_expr_with_expectation(&body.value, expected);
@ -2151,13 +2153,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
variant: &'tcx ty::VariantDef, variant: &'tcx ty::VariantDef,
access_span: Span, access_span: Span,
) -> Vec<Symbol> { ) -> Vec<Symbol> {
let body_owner_hir_id = self.tcx.hir().local_def_id_to_hir_id(self.body_id);
variant variant
.fields .fields
.iter() .iter()
.filter(|field| { .filter(|field| {
let def_scope = self let def_scope = self
.tcx .tcx
.adjust_ident_and_get_scope(field.ident(self.tcx), variant.def_id, self.body_id) .adjust_ident_and_get_scope(
field.ident(self.tcx),
variant.def_id,
body_owner_hir_id,
)
.1; .1;
field.vis.is_accessible_from(def_scope, self.tcx) field.vis.is_accessible_from(def_scope, self.tcx)
&& !matches!( && !matches!(
@ -2199,8 +2206,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match deref_base_ty.kind() { match deref_base_ty.kind() {
ty::Adt(base_def, substs) if !base_def.is_enum() => { ty::Adt(base_def, substs) if !base_def.is_enum() => {
debug!("struct named {:?}", deref_base_ty); debug!("struct named {:?}", deref_base_ty);
let body_hir_id = self.tcx.hir().local_def_id_to_hir_id(self.body_id);
let (ident, def_scope) = let (ident, def_scope) =
self.tcx.adjust_ident_and_get_scope(field, base_def.did(), self.body_id); self.tcx.adjust_ident_and_get_scope(field, base_def.did(), body_hir_id);
let fields = &base_def.non_enum_variant().fields; let fields = &base_def.non_enum_variant().fields;
if let Some(index) = fields if let Some(index) = fields
.iter() .iter()
@ -2538,7 +2546,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
fn point_at_param_definition(&self, err: &mut Diagnostic, param: ty::ParamTy) { fn point_at_param_definition(&self, err: &mut Diagnostic, param: ty::ParamTy) {
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id()); let generics = self.tcx.generics_of(self.body_id);
let generic_param = generics.type_param(&param, self.tcx); let generic_param = generics.type_param(&param, self.tcx);
if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param.kind { if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param.kind {
return; return;

View file

@ -2126,7 +2126,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match *callee_ty.kind() { match *callee_ty.kind() {
ty::Param(param) => { ty::Param(param) => {
let param = let param =
self.tcx.generics_of(self.body_id.owner).type_param(&param, self.tcx); self.tcx.generics_of(self.body_id).type_param(&param, self.tcx);
if param.kind.is_synthetic() { if param.kind.is_synthetic() {
// if it's `impl Fn() -> ..` then just fall down to the def-id based logic // if it's `impl Fn() -> ..` then just fall down to the def-id based logic
def_id = param.def_id; def_id = param.def_id;
@ -2135,7 +2135,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// and point at that. // and point at that.
let instantiated = self let instantiated = self
.tcx .tcx
.explicit_predicates_of(self.body_id.owner) .explicit_predicates_of(self.body_id)
.instantiate_identity(self.tcx); .instantiate_identity(self.tcx);
// FIXME(compiler-errors): This could be problematic if something has two // FIXME(compiler-errors): This could be problematic if something has two
// fn-like predicates with different args, but callable types really never // fn-like predicates with different args, but callable types really never

View file

@ -10,7 +10,7 @@ pub use suggestions::*;
use crate::coercion::DynamicCoerceMany; use crate::coercion::DynamicCoerceMany;
use crate::{Diverges, EnclosingBreakables, Inherited}; use crate::{Diverges, EnclosingBreakables, Inherited};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir_analysis::astconv::AstConv; use rustc_hir_analysis::astconv::AstConv;
use rustc_infer::infer; use rustc_infer::infer;
use rustc_infer::infer::error_reporting::TypeErrCtxt; use rustc_infer::infer::error_reporting::TypeErrCtxt;
@ -38,7 +38,7 @@ use std::ops::Deref;
/// [`ItemCtxt`]: rustc_hir_analysis::collect::ItemCtxt /// [`ItemCtxt`]: rustc_hir_analysis::collect::ItemCtxt
/// [`InferCtxt`]: infer::InferCtxt /// [`InferCtxt`]: infer::InferCtxt
pub struct FnCtxt<'a, 'tcx> { pub struct FnCtxt<'a, 'tcx> {
pub(super) body_id: hir::HirId, pub(super) body_id: LocalDefId,
/// The parameter environment used for proving trait obligations /// The parameter environment used for proving trait obligations
/// in this function. This can change when we descend into /// in this function. This can change when we descend into
@ -117,7 +117,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn new( pub fn new(
inh: &'a Inherited<'tcx>, inh: &'a Inherited<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
) -> FnCtxt<'a, 'tcx> { ) -> FnCtxt<'a, 'tcx> {
FnCtxt { FnCtxt {
body_id, body_id,
@ -204,7 +204,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
} }
fn item_def_id(&self) -> DefId { fn item_def_id(&self) -> DefId {
self.body_id.owner.to_def_id() self.body_id.to_def_id()
} }
fn get_type_parameter_bounds( fn get_type_parameter_bounds(

View file

@ -31,7 +31,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.typeck_results self.typeck_results
.borrow() .borrow()
.liberated_fn_sigs() .liberated_fn_sigs()
.get(self.tcx.hir().parent_id(self.body_id)) .get(self.tcx.hir().local_def_id_to_hir_id(self.body_id))
.copied() .copied()
} }
@ -164,7 +164,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
ty: Ty<'tcx>, ty: Ty<'tcx>,
) -> Option<(DefIdOrName, Ty<'tcx>, Vec<Ty<'tcx>>)> { ) -> Option<(DefIdOrName, Ty<'tcx>, Vec<Ty<'tcx>>)> {
self.err_ctxt().extract_callable_info(self.body_id, self.param_env, ty) let body_hir_id = self.tcx.hir().local_def_id_to_hir_id(self.body_id);
self.err_ctxt().extract_callable_info(body_hir_id, self.param_env, ty)
} }
pub fn suggest_two_fn_call( pub fn suggest_two_fn_call(

View file

@ -201,7 +201,7 @@ fn typeck_with_fallback<'tcx>(
let typeck_results = Inherited::build(tcx, def_id).enter(|inh| { let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
let param_env = tcx.param_env(def_id); let param_env = tcx.param_env(def_id);
let mut fcx = FnCtxt::new(&inh, param_env, body.value.hir_id); let mut fcx = FnCtxt::new(&inh, param_env, def_id);
if let Some(hir::FnSig { header, decl, .. }) = fn_sig { if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
let fn_sig = if rustc_hir_analysis::collect::get_infer_ret_ty(&decl.output).is_some() { let fn_sig = if rustc_hir_analysis::collect::get_infer_ret_ty(&decl.output).is_some() {

View file

@ -508,9 +508,10 @@ fn method_autoderef_steps<'tcx>(
let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal); let (ref infcx, goal, inference_vars) = tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &goal);
let ParamEnvAnd { param_env, value: self_ty } = goal; let ParamEnvAnd { param_env, value: self_ty } = goal;
let mut autoderef = Autoderef::new(infcx, param_env, hir::CRATE_HIR_ID, DUMMY_SP, self_ty) let mut autoderef =
.include_raw_pointers() Autoderef::new(infcx, param_env, hir::def_id::CRATE_DEF_ID, DUMMY_SP, self_ty)
.silence_errors(); .include_raw_pointers()
.silence_errors();
let mut reached_raw_pointer = false; let mut reached_raw_pointer = false;
let mut steps: Vec<_> = autoderef let mut steps: Vec<_> = autoderef
.by_ref() .by_ref()
@ -610,10 +611,9 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
fn push_candidate(&mut self, candidate: Candidate<'tcx>, is_inherent: bool) { fn push_candidate(&mut self, candidate: Candidate<'tcx>, is_inherent: bool) {
let is_accessible = if let Some(name) = self.method_name { let is_accessible = if let Some(name) = self.method_name {
let item = candidate.item; let item = candidate.item;
let def_scope = self let hir_id = self.tcx.hir().local_def_id_to_hir_id(self.body_id);
.tcx let def_scope =
.adjust_ident_and_get_scope(name, item.container_id(self.tcx), self.body_id) self.tcx.adjust_ident_and_get_scope(name, item.container_id(self.tcx), hir_id).1;
.1;
item.visibility(self.tcx).is_accessible_from(def_scope, self.tcx) item.visibility(self.tcx).is_accessible_from(def_scope, self.tcx)
} else { } else {
true true

View file

@ -352,7 +352,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let ty_span = match rcvr_ty.kind() { let ty_span = match rcvr_ty.kind() {
ty::Param(param_type) => { ty::Param(param_type) => {
Some(param_type.span_from_generics(self.tcx, self.body_id.owner.to_def_id())) Some(param_type.span_from_generics(self.tcx, self.body_id.to_def_id()))
} }
ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())), ty::Adt(def, _) if def.did().is_local() => Some(tcx.def_span(def.did())),
_ => None, _ => None,
@ -403,7 +403,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
args, args,
sugg_span, sugg_span,
); );
self.note_candidates_on_method_error( self.note_candidates_on_method_error(
rcvr_ty, rcvr_ty,
item_name, item_name,
@ -496,9 +495,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ty::Param(_) => { ty::Param(_) => {
// Account for `fn` items like in `issue-35677.rs` to // Account for `fn` items like in `issue-35677.rs` to
// suggest restricting its type params. // suggest restricting its type params.
let parent_body = Some(hir.get_by_def_id(self.body_id))
hir.body_owner(hir::BodyId { hir_id: self.body_id });
Some(hir.get(parent_body))
} }
ty::Adt(def, _) => { ty::Adt(def, _) => {
def.did().as_local().map(|def_id| hir.get_by_def_id(def_id)) def.did().as_local().map(|def_id| hir.get_by_def_id(def_id))
@ -1343,7 +1340,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
_ => None, _ => None,
}); });
if let Some((field, field_ty)) = field_receiver { if let Some((field, field_ty)) = field_receiver {
let scope = tcx.parent_module(self.body_id); let scope = tcx.parent_module_from_def_id(self.body_id);
let is_accessible = field.vis.is_accessible_from(scope, tcx); let is_accessible = field.vis.is_accessible_from(scope, tcx);
if is_accessible { if is_accessible {
@ -1593,7 +1590,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
else { return }; else { return };
let map = self.infcx.tcx.hir(); let map = self.infcx.tcx.hir();
let body = map.body(rustc_hir::BodyId { hir_id: self.body_id }); let body_id = self.tcx.hir().body_owned_by(self.body_id);
let body = map.body(body_id);
struct LetVisitor<'a> { struct LetVisitor<'a> {
result: Option<&'a hir::Expr<'a>>, result: Option<&'a hir::Expr<'a>>,
ident_name: Symbol, ident_name: Symbol,
@ -2195,7 +2193,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
true true
}); });
let module_did = self.tcx.parent_module(self.body_id); let module_did = self.tcx.parent_module_from_def_id(self.body_id);
let (module, _, _) = self.tcx.hir().get_module(module_did); let (module, _, _) = self.tcx.hir().get_module(module_did);
let span = module.spans.inject_use_span; let span = module.spans.inject_use_span;
@ -2517,7 +2515,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}; };
// Obtain the span for `param` and use it for a structured suggestion. // Obtain the span for `param` and use it for a structured suggestion.
if let Some(param) = param_type { if let Some(param) = param_type {
let generics = self.tcx.generics_of(self.body_id.owner.to_def_id()); let generics = self.tcx.generics_of(self.body_id.to_def_id());
let type_param = generics.type_param(param, self.tcx); let type_param = generics.type_param(param, self.tcx);
let hir = self.tcx.hir(); let hir = self.tcx.hir();
if let Some(def_id) = type_param.def_id.as_local() { if let Some(def_id) = type_param.def_id.as_local() {

View file

@ -1844,16 +1844,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
} }
} }
// In some (most?) cases cause.body_id points to actual body, but in some cases
// it's an actual definition. According to the comments (e.g. in
// rustc_hir_analysis/check/compare_impl_item.rs:compare_predicate_entailment) the latter
// is relied upon by some other code. This might (or might not) need cleanup.
let body_owner_def_id =
self.tcx.hir().opt_local_def_id(cause.body_id).unwrap_or_else(|| {
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
});
self.check_and_note_conflicting_crates(diag, terr); self.check_and_note_conflicting_crates(diag, terr);
self.tcx.note_and_explain_type_err(diag, terr, cause, span, body_owner_def_id.to_def_id()); self.tcx.note_and_explain_type_err(diag, terr, cause, span, cause.body_id.to_def_id());
if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values
&& let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind() && let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind()

View file

@ -411,8 +411,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
span: Span, span: Span,
) { ) {
let hir = self.tcx.hir(); let hir = self.tcx.hir();
let fn_hir_id = hir.parent_id(cause.body_id); if let Some(node) = self.tcx.hir().find_by_def_id(cause.body_id) &&
if let Some(node) = self.tcx.hir().find(fn_hir_id) &&
let hir::Node::Item(hir::Item { let hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn(_sig, _, body_id), .. kind: hir::ItemKind::Fn(_sig, _, body_id), ..
}) = node { }) = node {

View file

@ -3,7 +3,7 @@ use crate::infer::{DefiningAnchor, InferCtxt, InferOk};
use crate::traits; use crate::traits;
use hir::def::DefKind; use hir::def::DefKind;
use hir::def_id::{DefId, LocalDefId}; use hir::def_id::{DefId, LocalDefId};
use hir::{HirId, OpaqueTyOrigin}; use hir::OpaqueTyOrigin;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_data_structures::vec_map::VecMap; use rustc_data_structures::vec_map::VecMap;
use rustc_hir as hir; use rustc_hir as hir;
@ -48,7 +48,7 @@ impl<'tcx> InferCtxt<'tcx> {
pub fn replace_opaque_types_with_inference_vars<T: TypeFoldable<'tcx>>( pub fn replace_opaque_types_with_inference_vars<T: TypeFoldable<'tcx>>(
&self, &self,
value: T, value: T,
body_id: HirId, body_id: LocalDefId,
span: Span, span: Span,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
) -> InferOk<'tcx, T> { ) -> InferOk<'tcx, T> {

View file

@ -8,6 +8,7 @@ mod project;
mod structural_impls; mod structural_impls;
pub mod util; pub mod util;
use hir::def_id::LocalDefId;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::error::{ExpectedFound, TypeError};
use rustc_middle::ty::{self, Const, ToPredicate, Ty, TyCtxt}; use rustc_middle::ty::{self, Const, ToPredicate, Ty, TyCtxt};
@ -146,7 +147,7 @@ impl<'tcx, O> Obligation<'tcx, O> {
pub fn misc( pub fn misc(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
span: Span, span: Span,
body_id: hir::HirId, body_id: LocalDefId,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
trait_ref: impl ToPredicate<'tcx, O>, trait_ref: impl ToPredicate<'tcx, O>,
) -> Obligation<'tcx, O> { ) -> Obligation<'tcx, O> {

View file

@ -732,7 +732,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingCopyImplementations {
cx.tcx, cx.tcx,
param_env, param_env,
ty, ty,
traits::ObligationCause::misc(item.span, item.hir_id()), traits::ObligationCause::misc(item.span, item.owner_id.def_id),
) )
.is_ok() .is_ok()
{ {

View file

@ -139,9 +139,10 @@ fn suggest_question_mark<'tcx>(
let ty = substs.type_at(0); let ty = substs.type_at(0);
let infcx = cx.tcx.infer_ctxt().build(); let infcx = cx.tcx.infer_ctxt().build();
let body_def_id = cx.tcx.hir().body_owner_def_id(body_id);
let cause = ObligationCause::new( let cause = ObligationCause::new(
span, span,
body_id.hir_id, body_def_id,
rustc_infer::traits::ObligationCauseCode::MiscObligation, rustc_infer::traits::ObligationCauseCode::MiscObligation,
); );
let errors = rustc_trait_selection::traits::fully_solve_bound( let errors = rustc_trait_selection::traits::fully_solve_bound(

View file

@ -18,7 +18,8 @@ use crate::ty::{self, AdtKind, Ty, TyCtxt};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_errors::{Applicability, Diagnostic}; use rustc_errors::{Applicability, Diagnostic};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::def_id::DefId;
use rustc_span::def_id::{LocalDefId, CRATE_DEF_ID};
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
use smallvec::SmallVec; use smallvec::SmallVec;
@ -99,7 +100,7 @@ pub struct ObligationCause<'tcx> {
/// (in particular, closures can add new assumptions). See the /// (in particular, closures can add new assumptions). See the
/// field `region_obligations` of the `FulfillmentContext` for more /// field `region_obligations` of the `FulfillmentContext` for more
/// information. /// information.
pub body_id: hir::HirId, pub body_id: LocalDefId,
code: InternedObligationCauseCode<'tcx>, code: InternedObligationCauseCode<'tcx>,
} }
@ -120,13 +121,13 @@ impl<'tcx> ObligationCause<'tcx> {
#[inline] #[inline]
pub fn new( pub fn new(
span: Span, span: Span,
body_id: hir::HirId, body_id: LocalDefId,
code: ObligationCauseCode<'tcx>, code: ObligationCauseCode<'tcx>,
) -> ObligationCause<'tcx> { ) -> ObligationCause<'tcx> {
ObligationCause { span, body_id, code: code.into() } ObligationCause { span, body_id, code: code.into() }
} }
pub fn misc(span: Span, body_id: hir::HirId) -> ObligationCause<'tcx> { pub fn misc(span: Span, body_id: LocalDefId) -> ObligationCause<'tcx> {
ObligationCause::new(span, body_id, MiscObligation) ObligationCause::new(span, body_id, MiscObligation)
} }
@ -137,7 +138,7 @@ impl<'tcx> ObligationCause<'tcx> {
#[inline(always)] #[inline(always)]
pub fn dummy_with_span(span: Span) -> ObligationCause<'tcx> { pub fn dummy_with_span(span: Span) -> ObligationCause<'tcx> {
ObligationCause { span, body_id: hir::CRATE_HIR_ID, code: Default::default() } ObligationCause { span, body_id: CRATE_DEF_ID, code: Default::default() }
} }
pub fn span(&self) -> Span { pub fn span(&self) -> Span {

View file

@ -2437,6 +2437,7 @@ impl<'tcx> TyCtxt<'tcx> {
ident ident
} }
// FIXME(vincenzoapalzzo): move the HirId to a LocalDefId
pub fn adjust_ident_and_get_scope( pub fn adjust_ident_and_get_scope(
self, self,
mut ident: Ident, mut ident: Ident,

View file

@ -5,6 +5,7 @@ use rustc_middle::mir::{self, Field};
use rustc_middle::thir::{FieldPat, Pat, PatKind}; use rustc_middle::thir::{FieldPat, Pat, PatKind};
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::lint; use rustc_session::lint;
use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::Span; use rustc_span::Span;
use rustc_trait_selection::traits::predicate_for_trait_def; use rustc_trait_selection::traits::predicate_for_trait_def;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
@ -189,10 +190,11 @@ impl<'tcx> ConstToPat<'tcx> {
// using `PartialEq::eq` in this scenario in the past.) // using `PartialEq::eq` in this scenario in the past.)
let partial_eq_trait_id = let partial_eq_trait_id =
self.tcx().require_lang_item(hir::LangItem::PartialEq, Some(self.span)); self.tcx().require_lang_item(hir::LangItem::PartialEq, Some(self.span));
let def_id = self.tcx().hir().opt_local_def_id(self.id).unwrap_or(CRATE_DEF_ID);
let obligation: PredicateObligation<'_> = predicate_for_trait_def( let obligation: PredicateObligation<'_> = predicate_for_trait_def(
self.tcx(), self.tcx(),
self.param_env, self.param_env,
ObligationCause::misc(self.span, self.id), ObligationCause::misc(self.span, def_id),
partial_eq_trait_id, partial_eq_trait_id,
0, 0,
[ty, ty], [ty, ty],

View file

@ -17,7 +17,6 @@ use crate::traits::{
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_errors::Diagnostic; use rustc_errors::Diagnostic;
use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::CRATE_HIR_ID;
use rustc_infer::infer::{DefiningAnchor, InferCtxt, TyCtxtInferExt}; use rustc_infer::infer::{DefiningAnchor, InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::util; use rustc_infer::traits::util;
use rustc_middle::traits::specialization_graph::OverlapMode; use rustc_middle::traits::specialization_graph::OverlapMode;
@ -382,18 +381,14 @@ fn resolve_negative_obligation<'tcx>(
return false; return false;
} }
let (body_id, body_def_id) = if let Some(body_def_id) = body_def_id.as_local() { let body_def_id = body_def_id.as_local().unwrap_or(CRATE_DEF_ID);
(tcx.hir().local_def_id_to_hir_id(body_def_id), body_def_id)
} else {
(CRATE_HIR_ID, CRATE_DEF_ID)
};
let ocx = ObligationCtxt::new(&infcx); let ocx = ObligationCtxt::new(&infcx);
let wf_tys = ocx.assumed_wf_types(param_env, DUMMY_SP, body_def_id); let wf_tys = ocx.assumed_wf_types(param_env, DUMMY_SP, body_def_id);
let outlives_env = OutlivesEnvironment::with_bounds( let outlives_env = OutlivesEnvironment::with_bounds(
param_env, param_env,
Some(&infcx), Some(&infcx),
infcx.implied_bounds_tys(param_env, body_id, wf_tys), infcx.implied_bounds_tys(param_env, body_def_id, wf_tys),
); );
infcx.process_registered_region_obligations(outlives_env.region_bound_pairs(), param_env); infcx.process_registered_region_obligations(outlives_env.region_bound_pairs(), param_env);

View file

@ -190,8 +190,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
let tcx = self.infcx.tcx; let tcx = self.infcx.tcx;
let assumed_wf_types = tcx.assumed_wf_types(def_id); let assumed_wf_types = tcx.assumed_wf_types(def_id);
let mut implied_bounds = FxIndexSet::default(); let mut implied_bounds = FxIndexSet::default();
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let cause = ObligationCause::misc(span, def_id);
let cause = ObligationCause::misc(span, hir_id);
for ty in assumed_wf_types { for ty in assumed_wf_types {
// FIXME(@lcnr): rustc currently does not check wf for types // FIXME(@lcnr): rustc currently does not check wf for types
// pre-normalization, meaning that implied bounds are sometimes // pre-normalization, meaning that implied bounds are sometimes

View file

@ -81,7 +81,7 @@ pub fn recompute_applicable_impls<'tcx>(
); );
let predicates = let predicates =
tcx.predicates_of(obligation.cause.body_id.owner.to_def_id()).instantiate_identity(tcx); tcx.predicates_of(obligation.cause.body_id.to_def_id()).instantiate_identity(tcx);
for obligation in elaborate_predicates_with_span(tcx, predicates.into_iter()) { for obligation in elaborate_predicates_with_span(tcx, predicates.into_iter()) {
let kind = obligation.predicate.kind(); let kind = obligation.predicate.kind();
if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = kind.skip_binder() if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = kind.skip_binder()

View file

@ -839,14 +839,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
err.note(s.as_str()); err.note(s.as_str());
} }
if let Some(ref s) = parent_label { if let Some(ref s) = parent_label {
let body = tcx let body = obligation.cause.body_id;
.hir()
.opt_local_def_id(obligation.cause.body_id)
.unwrap_or_else(|| {
tcx.hir().body_owner_def_id(hir::BodyId {
hir_id: obligation.cause.body_id,
})
});
err.span_label(tcx.def_span(body), s); err.span_label(tcx.def_span(body), s);
} }
@ -934,6 +927,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
); );
} }
let body_hir_id =
self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id);
// Try to report a help message // Try to report a help message
if is_fn_trait if is_fn_trait
&& let Ok((implemented_kind, params)) = self.type_implements_fn_trait( && let Ok((implemented_kind, params)) = self.type_implements_fn_trait(
@ -1014,7 +1009,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
if !self.report_similar_impl_candidates( if !self.report_similar_impl_candidates(
impl_candidates, impl_candidates,
trait_ref, trait_ref,
obligation.cause.body_id, body_hir_id,
&mut err, &mut err,
true, true,
) { ) {
@ -1050,7 +1045,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
self.report_similar_impl_candidates( self.report_similar_impl_candidates(
impl_candidates, impl_candidates,
trait_ref, trait_ref,
obligation.cause.body_id, body_hir_id,
&mut err, &mut err,
true, true,
); );
@ -2305,10 +2300,12 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
predicate.to_opt_poly_trait_pred().unwrap(), predicate.to_opt_poly_trait_pred().unwrap(),
); );
if impl_candidates.len() < 10 { if impl_candidates.len() < 10 {
let hir =
self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id);
self.report_similar_impl_candidates( self.report_similar_impl_candidates(
impl_candidates, impl_candidates,
trait_ref, trait_ref,
body_id.map(|id| id.hir_id).unwrap_or(obligation.cause.body_id), body_id.map(|id| id.hir_id).unwrap_or(hir),
&mut err, &mut err,
false, false,
); );

View file

@ -149,10 +149,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
.unwrap_or_else(|| (trait_ref.def_id(), trait_ref.skip_binder().substs)); .unwrap_or_else(|| (trait_ref.def_id(), trait_ref.skip_binder().substs));
let trait_ref = trait_ref.skip_binder(); let trait_ref = trait_ref.skip_binder();
let mut flags = vec![( let body_hir = self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id);
sym::ItemContext, let mut flags =
self.describe_enclosure(obligation.cause.body_id).map(|s| s.to_owned()), vec![(sym::ItemContext, self.describe_enclosure(body_hir).map(|s| s.to_owned()))];
)];
match obligation.cause.code() { match obligation.cause.code() {
ObligationCauseCode::BuiltinDerivedObligation(..) ObligationCauseCode::BuiltinDerivedObligation(..)

View file

@ -9,7 +9,6 @@ use crate::infer::InferCtxt;
use crate::traits::{NormalizeExt, ObligationCtxt}; use crate::traits::{NormalizeExt, ObligationCtxt};
use hir::def::CtorOf; use hir::def::CtorOf;
use hir::{Expr, HirId};
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_errors::{ use rustc_errors::{
@ -22,6 +21,7 @@ use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::Visitor; use rustc_hir::intravisit::Visitor;
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node}; use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node};
use rustc_hir::{Expr, HirId};
use rustc_infer::infer::error_reporting::TypeErrCtxt; use rustc_infer::infer::error_reporting::TypeErrCtxt;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_infer::infer::{InferOk, LateBoundRegionConversionTime}; use rustc_infer::infer::{InferOk, LateBoundRegionConversionTime};
@ -34,6 +34,7 @@ use rustc_middle::ty::{
IsSuggestable, ToPredicate, Ty, TyCtxt, TypeAndMut, TypeFoldable, TypeFolder, IsSuggestable, ToPredicate, Ty, TyCtxt, TypeAndMut, TypeFoldable, TypeFolder,
TypeSuperFoldable, TypeVisitable, TypeckResults, TypeSuperFoldable, TypeVisitable, TypeckResults,
}; };
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP}; use rustc_span::{BytePos, DesugaringKind, ExpnKind, MacroKind, Span, DUMMY_SP};
use rustc_target::spec::abi; use rustc_target::spec::abi;
@ -179,7 +180,7 @@ pub trait TypeErrCtxtExt<'tcx> {
err: &mut Diagnostic, err: &mut Diagnostic,
trait_pred: ty::PolyTraitPredicate<'tcx>, trait_pred: ty::PolyTraitPredicate<'tcx>,
associated_item: Option<(&'static str, Ty<'tcx>)>, associated_item: Option<(&'static str, Ty<'tcx>)>,
body_id: hir::HirId, body_id: LocalDefId,
); );
fn suggest_dereferences( fn suggest_dereferences(
@ -522,7 +523,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
mut err: &mut Diagnostic, mut err: &mut Diagnostic,
trait_pred: ty::PolyTraitPredicate<'tcx>, trait_pred: ty::PolyTraitPredicate<'tcx>,
associated_ty: Option<(&'static str, Ty<'tcx>)>, associated_ty: Option<(&'static str, Ty<'tcx>)>,
body_id: hir::HirId, body_id: LocalDefId,
) { ) {
let trait_pred = self.resolve_numeric_literals_with_default(trait_pred); let trait_pred = self.resolve_numeric_literals_with_default(trait_pred);
@ -535,8 +536,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// FIXME: Add check for trait bound that is already present, particularly `?Sized` so we // FIXME: Add check for trait bound that is already present, particularly `?Sized` so we
// don't suggest `T: Sized + ?Sized`. // don't suggest `T: Sized + ?Sized`.
let mut hir_id = body_id; let mut body_id = body_id;
while let Some(node) = self.tcx.hir().find(hir_id) { while let Some(node) = self.tcx.hir().find_by_def_id(body_id) {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(body_id);
match node { match node {
hir::Node::Item(hir::Item { hir::Node::Item(hir::Item {
ident, ident,
@ -713,8 +715,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
_ => {} _ => {}
} }
body_id = self.tcx.local_parent(body_id);
hir_id = self.tcx.hir().get_parent_item(hir_id).into();
} }
} }
@ -905,8 +906,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
trait_pred.self_ty(), trait_pred.self_ty(),
); );
let body_hir_id = self.tcx.hir().local_def_id_to_hir_id(obligation.cause.body_id);
let Some((def_id_or_name, output, inputs)) = self.extract_callable_info( let Some((def_id_or_name, output, inputs)) = self.extract_callable_info(
obligation.cause.body_id, body_hir_id,
obligation.param_env, obligation.param_env,
self_ty, self_ty,
) else { return false; }; ) else { return false; };
@ -1004,8 +1006,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
span.remove_mark(); span.remove_mark();
} }
let mut expr_finder = FindExprBySpan::new(span); let mut expr_finder = FindExprBySpan::new(span);
let Some(hir::Node::Expr(body)) = self.tcx.hir().find(obligation.cause.body_id) else { return; }; let Some(body_id) = self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id) else { return; };
expr_finder.visit_expr(&body); let body = self.tcx.hir().body(body_id);
expr_finder.visit_expr(body.value);
let Some(expr) = expr_finder.result else { return; }; let Some(expr) = expr_finder.result else { return; };
let Some(typeck) = &self.typeck_results else { return; }; let Some(typeck) = &self.typeck_results else { return; };
let Some(ty) = typeck.expr_ty_adjusted_opt(expr) else { return; }; let Some(ty) = typeck.expr_ty_adjusted_opt(expr) else { return; };
@ -1060,8 +1063,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
) -> bool { ) -> bool {
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty()); let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
let ty = self.tcx.erase_late_bound_regions(self_ty); let ty = self.tcx.erase_late_bound_regions(self_ty);
let owner = self.tcx.hir().get_parent_item(obligation.cause.body_id); let Some(generics) = self.tcx.hir().get_generics(obligation.cause.body_id) else { return false };
let Some(generics) = self.tcx.hir().get_generics(owner.def_id) else { return false };
let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false }; let ty::Ref(_, inner_ty, hir::Mutability::Not) = ty.kind() else { return false };
let ty::Param(param) = inner_ty.kind() else { return false }; let ty::Param(param) = inner_ty.kind() else { return false };
let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else { return false }; let ObligationCauseCode::FunctionArgumentObligation { arg_hir_id, .. } = obligation.cause.code() else { return false };
@ -1104,6 +1106,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
/// Extracts information about a callable type for diagnostics. This is a /// Extracts information about a callable type for diagnostics. This is a
/// heuristic -- it doesn't necessarily mean that a type is always callable, /// heuristic -- it doesn't necessarily mean that a type is always callable,
/// because the callable type must also be well-formed to be called. /// because the callable type must also be well-formed to be called.
// FIXME(vincenzopalazzo): move the HirId to a LocalDefId
fn extract_callable_info( fn extract_callable_info(
&self, &self,
hir_id: HirId, hir_id: HirId,
@ -1429,10 +1432,11 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
span.remove_mark(); span.remove_mark();
} }
let mut expr_finder = super::FindExprBySpan::new(span); let mut expr_finder = super::FindExprBySpan::new(span);
let Some(hir::Node::Expr(body)) = self.tcx.hir().find(obligation.cause.body_id) else { let Some(body_id) = self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id) else {
return false; return false;
}; };
expr_finder.visit_expr(&body); let body = self.tcx.hir().body(body_id);
expr_finder.visit_expr(body.value);
let mut maybe_suggest = |suggested_ty, count, suggestions| { let mut maybe_suggest = |suggested_ty, count, suggestions| {
// Remapping bound vars here // Remapping bound vars here
let trait_pred_and_suggested_ty = let trait_pred_and_suggested_ty =
@ -1670,8 +1674,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
trait_pred: ty::PolyTraitPredicate<'tcx>, trait_pred: ty::PolyTraitPredicate<'tcx>,
) -> bool { ) -> bool {
let hir = self.tcx.hir(); let hir = self.tcx.hir();
let parent_node = hir.parent_id(obligation.cause.body_id); let node = hir.find_by_def_id(obligation.cause.body_id);
let node = hir.find(parent_node);
if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })) = node if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })) = node
&& let hir::ExprKind::Block(blk, _) = &hir.body(*body_id).value.kind && let hir::ExprKind::Block(blk, _) = &hir.body(*body_id).value.kind
&& sig.decl.output.span().overlaps(span) && sig.decl.output.span().overlaps(span)
@ -1707,8 +1710,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> { fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
let hir = self.tcx.hir(); let hir = self.tcx.hir();
let parent_node = hir.parent_id(obligation.cause.body_id); let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) = hir.find_by_def_id(obligation.cause.body_id) else {
let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) = hir.find(parent_node) else {
return None; return None;
}; };
@ -1732,8 +1734,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
} }
let hir = self.tcx.hir(); let hir = self.tcx.hir();
let fn_hir_id = hir.parent_id(obligation.cause.body_id); let fn_hir_id = hir.local_def_id_to_hir_id(obligation.cause.body_id);
let node = hir.find(fn_hir_id); let node = hir.find_by_def_id(obligation.cause.body_id);
let Some(hir::Node::Item(hir::Item { let Some(hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn(sig, _, body_id), kind: hir::ItemKind::Fn(sig, _, body_id),
.. ..
@ -1806,7 +1808,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
match liberated_sig.output().kind() { match liberated_sig.output().kind() {
ty::Dynamic(predicates, _, ty::Dyn) => { ty::Dynamic(predicates, _, ty::Dyn) => {
let cause = ObligationCause::misc(ret_ty.span, fn_hir_id); let cause = ObligationCause::misc(ret_ty.span, obligation.cause.body_id);
let param_env = ty::ParamEnv::empty(); let param_env = ty::ParamEnv::empty();
if !only_never_return { if !only_never_return {
@ -1944,8 +1946,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
} }
let hir = self.tcx.hir(); let hir = self.tcx.hir();
let parent_node = hir.parent_id(obligation.cause.body_id); let node = hir.find_by_def_id(obligation.cause.body_id);
let node = hir.find(parent_node);
if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) = if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) =
node node
{ {
@ -3283,12 +3284,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
trait_pred: ty::PolyTraitPredicate<'tcx>, trait_pred: ty::PolyTraitPredicate<'tcx>,
span: Span, span: Span,
) { ) {
let body_hir_id = obligation.cause.body_id; if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(obligation.cause.body_id) {
let item_id = self.tcx.hir().parent_id(body_hir_id);
if let Some(body_id) =
self.tcx.hir().maybe_body_owned_by(self.tcx.hir().local_def_id(item_id))
{
let body = self.tcx.hir().body(body_id); let body = self.tcx.hir().body(body_id);
if let Some(hir::GeneratorKind::Async(_)) = body.generator_kind { if let Some(hir::GeneratorKind::Async(_)) = body.generator_kind {
let future_trait = self.tcx.require_lang_item(LangItem::Future, None); let future_trait = self.tcx.require_lang_item(LangItem::Future, None);
@ -3727,9 +3723,14 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
term: ty_var.into(), term: ty_var.into(),
}, },
))); )));
let body_def_id = self.tcx.hir().enclosing_body_owner(body_id);
// Add `<ExprTy as Iterator>::Item = _` obligation. // Add `<ExprTy as Iterator>::Item = _` obligation.
ocx.register_obligation(Obligation::misc( ocx.register_obligation(Obligation::misc(
self.tcx, span, body_id, param_env, projection, self.tcx,
span,
body_def_id,
param_env,
projection,
)); ));
if ocx.select_where_possible().is_empty() { if ocx.select_where_possible().is_empty() {
// `ty_var` now holds the type that `Item` is for `ExprTy`. // `ty_var` now holds the type that `Item` is for `ExprTy`.

View file

@ -26,12 +26,11 @@ use crate::infer::{InferCtxt, TyCtxtInferExt};
use crate::traits::error_reporting::TypeErrCtxtExt as _; use crate::traits::error_reporting::TypeErrCtxtExt as _;
use crate::traits::query::evaluate_obligation::InferCtxtExt as _; use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use rustc_errors::ErrorGuaranteed; use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::fold::TypeFoldable;
use rustc_middle::ty::visit::TypeVisitable; use rustc_middle::ty::visit::TypeVisitable;
use rustc_middle::ty::{self, DefIdTree, ToPredicate, Ty, TyCtxt, TypeSuperVisitable}; use rustc_middle::ty::{self, DefIdTree, ToPredicate, Ty, TyCtxt, TypeSuperVisitable};
use rustc_middle::ty::{InternalSubsts, SubstsRef}; use rustc_middle::ty::{InternalSubsts, SubstsRef};
use rustc_span::def_id::{DefId, CRATE_DEF_ID};
use rustc_span::Span; use rustc_span::Span;
use std::fmt::Debug; use std::fmt::Debug;
@ -151,7 +150,7 @@ fn pred_known_to_hold_modulo_regions<'tcx>(
// We can use a dummy node-id here because we won't pay any mind // We can use a dummy node-id here because we won't pay any mind
// to region obligations that arise (there shouldn't really be any // to region obligations that arise (there shouldn't really be any
// anyhow). // anyhow).
cause: ObligationCause::misc(span, hir::CRATE_HIR_ID), cause: ObligationCause::misc(span, CRATE_DEF_ID),
recursion_depth: 0, recursion_depth: 0,
predicate: pred.to_predicate(infcx.tcx), predicate: pred.to_predicate(infcx.tcx),
}; };
@ -166,14 +165,12 @@ fn pred_known_to_hold_modulo_regions<'tcx>(
// that guess. While imperfect, I believe this is sound. // that guess. While imperfect, I believe this is sound.
// FIXME(@lcnr): this function doesn't seem right. // FIXME(@lcnr): this function doesn't seem right.
//
// The handling of regions in this area of the code is terrible, // The handling of regions in this area of the code is terrible,
// see issue #29149. We should be able to improve on this with // see issue #29149. We should be able to improve on this with
// NLL. // NLL.
let errors = fully_solve_obligation(infcx, obligation); let errors = fully_solve_obligation(infcx, obligation);
// Note: we only assume something is `Copy` if we can
// *definitively* show that it implements `Copy`. Otherwise,
// assume it is move; linear is always ok.
match &errors[..] { match &errors[..] {
[] => true, [] => true,
errors => { errors => {

View file

@ -3,9 +3,8 @@ use crate::traits::query::type_op::{self, TypeOp, TypeOpOutput};
use crate::traits::query::NoSolution; use crate::traits::query::NoSolution;
use crate::traits::ObligationCause; use crate::traits::ObligationCause;
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_hir as hir;
use rustc_hir::HirId;
use rustc_middle::ty::{self, ParamEnv, Ty}; use rustc_middle::ty::{self, ParamEnv, Ty};
use rustc_span::def_id::LocalDefId;
pub use rustc_middle::traits::query::OutlivesBound; pub use rustc_middle::traits::query::OutlivesBound;
@ -14,14 +13,14 @@ pub trait InferCtxtExt<'a, 'tcx> {
fn implied_outlives_bounds( fn implied_outlives_bounds(
&self, &self,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
ty: Ty<'tcx>, ty: Ty<'tcx>,
) -> Vec<OutlivesBound<'tcx>>; ) -> Vec<OutlivesBound<'tcx>>;
fn implied_bounds_tys( fn implied_bounds_tys(
&'a self, &'a self,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
tys: FxIndexSet<Ty<'tcx>>, tys: FxIndexSet<Ty<'tcx>>,
) -> Bounds<'a, 'tcx>; ) -> Bounds<'a, 'tcx>;
} }
@ -50,10 +49,10 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
fn implied_outlives_bounds( fn implied_outlives_bounds(
&self, &self,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
ty: Ty<'tcx>, ty: Ty<'tcx>,
) -> Vec<OutlivesBound<'tcx>> { ) -> Vec<OutlivesBound<'tcx>> {
let span = self.tcx.hir().span(body_id); let span = self.tcx.def_span(body_id);
let result = param_env let result = param_env
.and(type_op::implied_outlives_bounds::ImpliedOutlivesBounds { ty }) .and(type_op::implied_outlives_bounds::ImpliedOutlivesBounds { ty })
.fully_perform(self); .fully_perform(self);
@ -102,7 +101,7 @@ impl<'a, 'tcx: 'a> InferCtxtExt<'a, 'tcx> for InferCtxt<'tcx> {
fn implied_bounds_tys( fn implied_bounds_tys(
&'a self, &'a self,
param_env: ParamEnv<'tcx>, param_env: ParamEnv<'tcx>,
body_id: HirId, body_id: LocalDefId,
tys: FxIndexSet<Ty<'tcx>>, tys: FxIndexSet<Ty<'tcx>>,
) -> Bounds<'a, 'tcx> { ) -> Bounds<'a, 'tcx> {
tys.into_iter() tys.into_iter()

View file

@ -1,10 +1,10 @@
use crate::infer::InferCtxt; use crate::infer::InferCtxt;
use crate::traits; use crate::traits;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef}; use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable};
use rustc_span::def_id::{DefId, LocalDefId};
use rustc_span::Span; use rustc_span::Span;
use std::iter; use std::iter;
@ -17,7 +17,7 @@ use std::iter;
pub fn obligations<'tcx>( pub fn obligations<'tcx>(
infcx: &InferCtxt<'tcx>, infcx: &InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
recursion_depth: usize, recursion_depth: usize,
arg: GenericArg<'tcx>, arg: GenericArg<'tcx>,
span: Span, span: Span,
@ -82,7 +82,7 @@ pub fn obligations<'tcx>(
pub fn trait_obligations<'tcx>( pub fn trait_obligations<'tcx>(
infcx: &InferCtxt<'tcx>, infcx: &InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
trait_pred: &ty::TraitPredicate<'tcx>, trait_pred: &ty::TraitPredicate<'tcx>,
span: Span, span: Span,
item: &'tcx hir::Item<'tcx>, item: &'tcx hir::Item<'tcx>,
@ -105,7 +105,7 @@ pub fn trait_obligations<'tcx>(
pub fn predicate_obligations<'tcx>( pub fn predicate_obligations<'tcx>(
infcx: &InferCtxt<'tcx>, infcx: &InferCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
predicate: ty::Predicate<'tcx>, predicate: ty::Predicate<'tcx>,
span: Span, span: Span,
) -> Vec<traits::PredicateObligation<'tcx>> { ) -> Vec<traits::PredicateObligation<'tcx>> {
@ -167,7 +167,7 @@ pub fn predicate_obligations<'tcx>(
struct WfPredicates<'tcx> { struct WfPredicates<'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
body_id: hir::HirId, body_id: LocalDefId,
span: Span, span: Span,
out: Vec<traits::PredicateObligation<'tcx>>, out: Vec<traits::PredicateObligation<'tcx>>,
recursion_depth: usize, recursion_depth: usize,

View file

@ -2,13 +2,13 @@
//! Do not call this query directory. See //! Do not call this query directory. See
//! [`rustc_trait_selection::traits::query::type_op::implied_outlives_bounds`]. //! [`rustc_trait_selection::traits::query::type_op::implied_outlives_bounds`].
use rustc_hir as hir;
use rustc_infer::infer::canonical::{self, Canonical}; use rustc_infer::infer::canonical::{self, Canonical};
use rustc_infer::infer::outlives::components::{push_outlives_components, Component}; use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::traits::query::OutlivesBound; use rustc_infer::traits::query::OutlivesBound;
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable};
use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::source_map::DUMMY_SP; use rustc_span::source_map::DUMMY_SP;
use rustc_trait_selection::infer::InferCtxtBuilderExt; use rustc_trait_selection::infer::InferCtxtBuilderExt;
use rustc_trait_selection::traits::query::{CanonicalTyGoal, Fallible, NoSolution}; use rustc_trait_selection::traits::query::{CanonicalTyGoal, Fallible, NoSolution};
@ -67,9 +67,8 @@ fn compute_implied_outlives_bounds<'tcx>(
// FIXME(@lcnr): It's not really "always fine", having fewer implied // FIXME(@lcnr): It's not really "always fine", having fewer implied
// bounds can be backward incompatible, e.g. #101951 was caused by // bounds can be backward incompatible, e.g. #101951 was caused by
// us not dealing with inference vars in `TypeOutlives` predicates. // us not dealing with inference vars in `TypeOutlives` predicates.
let obligations = let obligations = wf::obligations(ocx.infcx, param_env, CRATE_DEF_ID, 0, arg, DUMMY_SP)
wf::obligations(ocx.infcx, param_env, hir::CRATE_HIR_ID, 0, arg, DUMMY_SP) .unwrap_or_default();
.unwrap_or_default();
// While these predicates should all be implied by other parts of // While these predicates should all be implied by other parts of
// the program, they are still relevant as they may constrain // the program, they are still relevant as they may constrain

View file

@ -6,6 +6,7 @@ use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, FnSig, Lift, PolyFnSig, Ty, TyCtxt, TypeFoldable}; use rustc_middle::ty::{self, FnSig, Lift, PolyFnSig, Ty, TyCtxt, TypeFoldable};
use rustc_middle::ty::{ParamEnvAnd, Predicate}; use rustc_middle::ty::{ParamEnvAnd, Predicate};
use rustc_middle::ty::{UserSelfTy, UserSubsts, UserType}; use rustc_middle::ty::{UserSelfTy, UserSubsts, UserType};
use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
use rustc_trait_selection::infer::InferCtxtBuilderExt; use rustc_trait_selection::infer::InferCtxtBuilderExt;
use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt; use rustc_trait_selection::traits::query::normalize::QueryNormalizeExt;
@ -76,7 +77,6 @@ fn relate_mir_and_user_ty<'tcx>(
// FIXME(#104764): We should check well-formedness before normalization. // FIXME(#104764): We should check well-formedness before normalization.
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(user_ty.into())); let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(user_ty.into()));
ocx.register_obligation(Obligation::new(ocx.infcx.tcx, cause, param_env, predicate)); ocx.register_obligation(Obligation::new(ocx.infcx.tcx, cause, param_env, predicate));
Ok(()) Ok(())
} }
@ -111,7 +111,7 @@ fn relate_mir_and_user_substs<'tcx>(
let span = if span == DUMMY_SP { predicate_span } else { span }; let span = if span == DUMMY_SP { predicate_span } else { span };
let cause = ObligationCause::new( let cause = ObligationCause::new(
span, span,
hir::CRATE_HIR_ID, CRATE_DEF_ID,
ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span), ObligationCauseCode::AscribeUserTypeProvePredicate(predicate_span),
); );
let instantiated_predicate = let instantiated_predicate =
@ -126,7 +126,6 @@ fn relate_mir_and_user_substs<'tcx>(
let impl_self_ty = ocx.normalize(&cause, param_env, impl_self_ty); let impl_self_ty = ocx.normalize(&cause, param_env, impl_self_ty);
ocx.eq(&cause, param_env, self_ty, impl_self_ty)?; ocx.eq(&cause, param_env, self_ty, impl_self_ty)?;
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into())); let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(impl_self_ty.into()));
ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate)); ocx.register_obligation(Obligation::new(tcx, cause.clone(), param_env, predicate));
} }

View file

@ -1,8 +1,8 @@
use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::fx::FxIndexSet;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::{self, Binder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt}; use rustc_middle::ty::{self, Binder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt};
use rustc_session::config::TraitSolver; use rustc_session::config::TraitSolver;
use rustc_span::def_id::{DefId, CRATE_DEF_ID};
use rustc_trait_selection::traits; use rustc_trait_selection::traits;
fn sized_constraint_for_ty<'tcx>( fn sized_constraint_for_ty<'tcx>(
@ -208,14 +208,7 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
constness, constness,
); );
let body_id = let body_id = local_did.unwrap_or(CRATE_DEF_ID);
local_did.and_then(|id| tcx.hir().maybe_body_owned_by(id).map(|body| body.hir_id));
let body_id = match body_id {
Some(id) => id,
None if hir_id.is_some() => hir_id.unwrap(),
_ => hir::CRATE_HIR_ID,
};
let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id); let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id);
traits::normalize_param_env_or_error(tcx, unnormalized_env, cause) traits::normalize_param_env_or_error(tcx, unnormalized_env, cause)
} }

View file

@ -78,7 +78,8 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
let send_trait = cx.tcx.get_diagnostic_item(sym::Send).unwrap(); let send_trait = cx.tcx.get_diagnostic_item(sym::Send).unwrap();
let span = decl.output.span(); let span = decl.output.span();
let infcx = cx.tcx.infer_ctxt().build(); let infcx = cx.tcx.infer_ctxt().build();
let cause = traits::ObligationCause::misc(span, hir_id); let def_id = cx.tcx.hir().local_def_id(hir_id);
let cause = traits::ObligationCause::misc(span, def_id);
let send_errors = traits::fully_solve_bound(&infcx, cause, cx.param_env, ret_ty, send_trait); let send_errors = traits::fully_solve_bound(&infcx, cause, cx.param_env, ret_ty, send_trait);
if !send_errors.is_empty() { if !send_errors.is_empty() {
span_lint_and_then( span_lint_and_then(

View file

@ -371,7 +371,7 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
&& let output_ty = return_ty(cx, item.hir_id()) && let output_ty = return_ty(cx, item.hir_id())
&& let local_def_id = cx.tcx.hir().local_def_id(item.hir_id()) && let local_def_id = cx.tcx.hir().local_def_id(item.hir_id())
&& Inherited::build(cx.tcx, local_def_id).enter(|inherited| { && Inherited::build(cx.tcx, local_def_id).enter(|inherited| {
let fn_ctxt = FnCtxt::new(inherited, cx.param_env, item.hir_id()); let fn_ctxt = FnCtxt::new(inherited, cx.param_env, local_def_id);
fn_ctxt.can_coerce(ty, output_ty) fn_ctxt.can_coerce(ty, output_ty)
}) { }) {
if has_lifetime(output_ty) && has_lifetime(ty) { if has_lifetime(output_ty) && has_lifetime(ty) {

View file

@ -46,7 +46,7 @@ fn check_cast<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>
let local_def_id = hir_id.owner.def_id; let local_def_id = hir_id.owner.def_id;
Inherited::build(cx.tcx, local_def_id).enter(|inherited| { Inherited::build(cx.tcx, local_def_id).enter(|inherited| {
let fn_ctxt = FnCtxt::new(inherited, cx.param_env, hir_id); let fn_ctxt = FnCtxt::new(inherited, cx.param_env, local_def_id);
// If we already have errors, we can't be sure we can pointer cast. // If we already have errors, we can't be sure we can pointer cast.
assert!( assert!(