Rollup merge of #95560 - lcnr:obligation-cause, r=oli-obk

convert more `DefId`s to `LocalDefId`
This commit is contained in:
Dylan DPC 2022-04-02 03:34:27 +02:00 committed by GitHub
commit 1c82fac3f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 51 additions and 45 deletions

View File

@ -237,7 +237,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
ObligationCauseCode::MatchImpl(parent, ..) => parent.code(), ObligationCauseCode::MatchImpl(parent, ..) => parent.code(),
_ => cause.code(), _ => cause.code(),
} }
&& let (ObligationCauseCode::ItemObligation(item_def_id), None) = (code, override_error_code) && let (&ObligationCauseCode::ItemObligation(item_def_id), None) = (code, override_error_code)
{ {
// Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static` // Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
// lifetime as above, but called using a fully-qualified path to the method: // lifetime as above, but called using a fully-qualified path to the method:
@ -245,7 +245,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
let mut v = TraitObjectVisitor(FxHashSet::default()); let mut v = TraitObjectVisitor(FxHashSet::default());
v.visit_ty(param.param_ty); v.visit_ty(param.param_ty);
if let Some((ident, self_ty)) = if let Some((ident, self_ty)) =
self.get_impl_ident_and_self_ty_from_trait(*item_def_id, &v.0) self.get_impl_ident_and_self_ty_from_trait(item_def_id, &v.0)
&& self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty) && self.suggest_constrain_dyn_trait_in_impl(&mut err, &v.0, ident, self_ty)
{ {
override_error_code = Some(ident.name); override_error_code = Some(ident.name);

View File

@ -7,7 +7,7 @@ use crate::traits::ObligationCauseCode::CompareImplMethodObligation;
use rustc_errors::ErrorGuaranteed; use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::Visitor; use rustc_hir::intravisit::Visitor;
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_middle::ty::print::RegionHighlightMode; use rustc_middle::ty::print::RegionHighlightMode;
@ -51,7 +51,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
{ {
let guar = self.emit_associated_type_err( let guar = self.emit_associated_type_err(
span, span,
self.infcx.tcx.item_name(impl_item_def_id), self.infcx.tcx.item_name(impl_item_def_id.to_def_id()),
impl_item_def_id, impl_item_def_id,
trait_item_def_id, trait_item_def_id,
); );
@ -155,7 +155,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
&self, &self,
span: Span, span: Span,
item_name: Symbol, item_name: Symbol,
impl_item_def_id: DefId, impl_item_def_id: LocalDefId,
trait_item_def_id: DefId, trait_item_def_id: DefId,
) -> ErrorGuaranteed { ) -> ErrorGuaranteed {
let impl_sp = self.tcx().def_span(impl_item_def_id); let impl_sp = self.tcx().def_span(impl_item_def_id);

View File

@ -348,7 +348,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let mut err = self.report_concrete_failure(*parent, sub, sup); let mut err = self.report_concrete_failure(*parent, sub, sup);
let trait_item_span = self.tcx.def_span(trait_item_def_id); let trait_item_span = self.tcx.def_span(trait_item_def_id);
let item_name = self.tcx.item_name(impl_item_def_id); let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
err.span_label( err.span_label(
trait_item_span, trait_item_span,
format!("definition of `{}` from trait", item_name), format!("definition of `{}` from trait", item_name),
@ -370,7 +370,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let where_clause_span = self let where_clause_span = self
.tcx .tcx
.hir() .hir()
.get_generics(impl_item_def_id.expect_local()) .get_generics(impl_item_def_id)
.unwrap() .unwrap()
.where_clause .where_clause
.tail_span_for_suggestion(); .tail_span_for_suggestion();

View File

@ -423,16 +423,20 @@ pub enum SubregionOrigin<'tcx> {
/// Comparing the signature and requirements of an impl method against /// Comparing the signature and requirements of an impl method against
/// the containing trait. /// the containing trait.
CompareImplMethodObligation { span: Span, impl_item_def_id: DefId, trait_item_def_id: DefId }, CompareImplMethodObligation {
span: Span,
impl_item_def_id: LocalDefId,
trait_item_def_id: DefId,
},
/// Comparing the signature and requirements of an impl associated type /// Comparing the signature and requirements of an impl associated type
/// against the containing trait /// against the containing trait
CompareImplTypeObligation { span: Span, impl_item_def_id: DefId, trait_item_def_id: DefId }, CompareImplTypeObligation { span: Span, impl_item_def_id: LocalDefId, trait_item_def_id: DefId },
/// Checking that the bounds of a trait's associated type hold for a given impl /// Checking that the bounds of a trait's associated type hold for a given impl
CheckAssociatedTypeBounds { CheckAssociatedTypeBounds {
parent: Box<SubregionOrigin<'tcx>>, parent: Box<SubregionOrigin<'tcx>>,
impl_item_def_id: DefId, impl_item_def_id: LocalDefId,
trait_item_def_id: DefId, trait_item_def_id: DefId,
}, },
} }

View File

@ -4,7 +4,7 @@ use crate::infer::InferCtxt;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{struct_span_err, DiagnosticBuilder, ErrorGuaranteed}; use rustc_errors::{struct_span_err, DiagnosticBuilder, ErrorGuaranteed};
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_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_span::{MultiSpan, Span}; use rustc_span::{MultiSpan, Span};
use std::fmt; use std::fmt;
@ -14,7 +14,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
pub fn report_extra_impl_obligation( pub fn report_extra_impl_obligation(
&self, &self,
error_span: Span, error_span: Span,
impl_item_def_id: DefId, impl_item_def_id: LocalDefId,
trait_item_def_id: DefId, trait_item_def_id: DefId,
requirement: &dyn fmt::Display, requirement: &dyn fmt::Display,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> { ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
@ -25,7 +25,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if let Some(trait_item_span) = self.tcx.hir().span_if_local(trait_item_def_id) { if let Some(trait_item_span) = self.tcx.hir().span_if_local(trait_item_def_id) {
let span = self.tcx.sess.source_map().guess_head_span(trait_item_span); let span = self.tcx.sess.source_map().guess_head_span(trait_item_span);
let item_name = self.tcx.item_name(impl_item_def_id); let item_name = self.tcx.item_name(impl_item_def_id.to_def_id());
err.span_label(span, format!("definition of `{}` from trait", item_name)); err.span_label(span, format!("definition of `{}` from trait", item_name));
} }

View File

@ -276,24 +276,23 @@ pub enum ObligationCauseCode<'tcx> {
/// Error derived when matching traits/impls; see ObligationCause for more details /// Error derived when matching traits/impls; see ObligationCause for more details
CompareImplMethodObligation { CompareImplMethodObligation {
impl_item_def_id: DefId, impl_item_def_id: LocalDefId,
trait_item_def_id: DefId, trait_item_def_id: DefId,
}, },
/// Error derived when matching traits/impls; see ObligationCause for more details /// Error derived when matching traits/impls; see ObligationCause for more details
CompareImplTypeObligation { CompareImplTypeObligation {
impl_item_def_id: DefId, impl_item_def_id: LocalDefId,
trait_item_def_id: DefId, trait_item_def_id: DefId,
}, },
/// Checking that the bounds of a trait's associated type hold for a given impl /// Checking that the bounds of a trait's associated type hold for a given impl
CheckAssociatedTypeBounds { CheckAssociatedTypeBounds {
impl_item_def_id: DefId, impl_item_def_id: LocalDefId,
trait_item_def_id: DefId, trait_item_def_id: DefId,
}, },
/// Checking that this expression can be assigned where it needs to be /// Checking that this expression can be assigned to its target.
// FIXME(eddyb) #11161 is the original Expr required?
ExprAssignable, ExprAssignable,
/// Computing common supertype in the arms of a match expression /// Computing common supertype in the arms of a match expression

View File

@ -1913,15 +1913,15 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id()); self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
} else if let ( } else if let (
Ok(ref snippet), Ok(ref snippet),
ObligationCauseCode::BindingObligation(ref def_id, _), &ObligationCauseCode::BindingObligation(def_id, _),
) = ) =
(self.tcx.sess.source_map().span_to_snippet(span), obligation.cause.code()) (self.tcx.sess.source_map().span_to_snippet(span), obligation.cause.code())
{ {
let generics = self.tcx.generics_of(*def_id); let generics = self.tcx.generics_of(def_id);
if generics.params.iter().any(|p| p.name != kw::SelfUpper) if generics.params.iter().any(|p| p.name != kw::SelfUpper)
&& !snippet.ends_with('>') && !snippet.ends_with('>')
&& !generics.has_impl_trait() && !generics.has_impl_trait()
&& !self.tcx.fn_trait_kind_from_lang_item(*def_id).is_some() && !self.tcx.fn_trait_kind_from_lang_item(def_id).is_some()
{ {
// FIXME: To avoid spurious suggestions in functions where type arguments // FIXME: To avoid spurious suggestions in functions where type arguments
// where already supplied, we check the snippet to make sure it doesn't // where already supplied, we check the snippet to make sure it doesn't
@ -2223,6 +2223,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
"suggest_unsized_bound_if_applicable: pred={:?} item_def_id={:?} span={:?}", "suggest_unsized_bound_if_applicable: pred={:?} item_def_id={:?} span={:?}",
pred, item_def_id, span pred, item_def_id, span
); );
let (Some(node), true) = ( let (Some(node), true) = (
self.tcx.hir().get_if_local(item_def_id), self.tcx.hir().get_if_local(item_def_id),
Some(pred.def_id()) == self.tcx.lang_items().sized_trait(), Some(pred.def_id()) == self.tcx.lang_items().sized_trait(),

View File

@ -128,7 +128,7 @@ pub trait InferCtxtExt<'tcx> {
fn suggest_fully_qualified_path( fn suggest_fully_qualified_path(
&self, &self,
err: &mut Diagnostic, err: &mut Diagnostic,
def_id: DefId, item_def_id: DefId,
span: Span, span: Span,
trait_ref: DefId, trait_ref: DefId,
); );
@ -1317,16 +1317,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
fn suggest_fully_qualified_path( fn suggest_fully_qualified_path(
&self, &self,
err: &mut Diagnostic, err: &mut Diagnostic,
def_id: DefId, item_def_id: DefId,
span: Span, span: Span,
trait_ref: DefId, trait_ref: DefId,
) { ) {
if let Some(assoc_item) = self.tcx.opt_associated_item(def_id) { if let Some(assoc_item) = self.tcx.opt_associated_item(item_def_id) {
if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind { if let ty::AssocKind::Const | ty::AssocKind::Type = assoc_item.kind {
err.note(&format!( err.note(&format!(
"{}s cannot be accessed directly on a `trait`, they can only be \ "{}s cannot be accessed directly on a `trait`, they can only be \
accessed through a specific `impl`", accessed through a specific `impl`",
assoc_item.kind.as_def_kind().descr(def_id) assoc_item.kind.as_def_kind().descr(item_def_id)
)); ));
err.span_suggestion( err.span_suggestion(
span, span,

View File

@ -28,7 +28,6 @@ use super::{potentially_plural_count, FnCtxt, Inherited};
/// - `impl_m_span`: span to use for reporting errors /// - `impl_m_span`: span to use for reporting errors
/// - `trait_m`: the method in the trait /// - `trait_m`: the method in the trait
/// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation /// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation
crate fn compare_impl_method<'tcx>( crate fn compare_impl_method<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
impl_m: &ty::AssocItem, impl_m: &ty::AssocItem,
@ -88,7 +87,7 @@ fn compare_predicate_entailment<'tcx>(
impl_m_span, impl_m_span,
impl_m_hir_id, impl_m_hir_id,
ObligationCauseCode::CompareImplMethodObligation { ObligationCauseCode::CompareImplMethodObligation {
impl_item_def_id: impl_m.def_id, impl_item_def_id: impl_m.def_id.expect_local(),
trait_item_def_id: trait_m.def_id, trait_item_def_id: trait_m.def_id,
}, },
); );
@ -231,7 +230,7 @@ fn compare_predicate_entailment<'tcx>(
span, span,
impl_m_hir_id, impl_m_hir_id,
ObligationCauseCode::CompareImplMethodObligation { ObligationCauseCode::CompareImplMethodObligation {
impl_item_def_id: impl_m.def_id, impl_item_def_id: impl_m.def_id.expect_local(),
trait_item_def_id: trait_m.def_id, trait_item_def_id: trait_m.def_id,
}, },
); );
@ -1154,7 +1153,7 @@ fn compare_type_predicate_entailment<'tcx>(
impl_ty_span, impl_ty_span,
impl_ty_hir_id, impl_ty_hir_id,
ObligationCauseCode::CompareImplTypeObligation { ObligationCauseCode::CompareImplTypeObligation {
impl_item_def_id: impl_ty.def_id, 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,
}, },
); );
@ -1383,7 +1382,7 @@ pub fn check_type_bounds<'tcx>(
impl_ty_span, impl_ty_span,
impl_ty_hir_id, impl_ty_hir_id,
ObligationCauseCode::CheckAssociatedTypeBounds { ObligationCauseCode::CheckAssociatedTypeBounds {
impl_item_def_id: impl_ty.def_id, 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,
}, },
); );

View File

@ -2,9 +2,9 @@ use super::{probe, MethodCallee};
use crate::astconv::{AstConv, CreateSubstsForGenericArgsCtxt, IsMethodCall}; use crate::astconv::{AstConv, CreateSubstsForGenericArgsCtxt, IsMethodCall};
use crate::check::{callee, FnCtxt}; use crate::check::{callee, FnCtxt};
use crate::hir::def_id::DefId;
use crate::hir::GenericArg;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::GenericArg;
use rustc_infer::infer::{self, InferOk}; use rustc_infer::infer::{self, InferOk};
use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext}; use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast}; use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast};

View File

@ -940,7 +940,7 @@ fn check_associated_item(
item.ident(fcx.tcx).span, item.ident(fcx.tcx).span,
sig, sig,
hir_sig.decl, hir_sig.decl,
item.def_id, item.def_id.expect_local(),
&mut implied_bounds, &mut implied_bounds,
); );
check_method_receiver(fcx, hir_sig, item, self_ty); check_method_receiver(fcx, hir_sig, item, self_ty);
@ -1068,7 +1068,7 @@ fn check_type_defn<'tcx, F>(
} }
} }
check_where_clauses(fcx, item.span, item.def_id.to_def_id(), None); check_where_clauses(fcx, item.span, item.def_id, None);
// No implied bounds in a struct definition. // No implied bounds in a struct definition.
FxHashSet::default() FxHashSet::default()
@ -1096,7 +1096,7 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
// FIXME: this shouldn't use an `FnCtxt` at all. // FIXME: this shouldn't use an `FnCtxt` at all.
for_item(tcx, item).with_fcx(|fcx| { for_item(tcx, item).with_fcx(|fcx| {
check_where_clauses(fcx, item.span, item.def_id.to_def_id(), None); check_where_clauses(fcx, item.span, item.def_id, None);
FxHashSet::default() FxHashSet::default()
}); });
@ -1144,7 +1144,7 @@ fn check_item_fn(
for_id(tcx, def_id, span).with_fcx(|fcx| { for_id(tcx, def_id, span).with_fcx(|fcx| {
let sig = tcx.fn_sig(def_id); let sig = tcx.fn_sig(def_id);
let mut implied_bounds = FxHashSet::default(); let mut implied_bounds = FxHashSet::default();
check_fn_or_method(fcx, ident.span, sig, decl, def_id.to_def_id(), &mut implied_bounds); check_fn_or_method(fcx, ident.span, sig, decl, def_id, &mut implied_bounds);
implied_bounds implied_bounds
}) })
} }
@ -1238,7 +1238,7 @@ fn check_impl<'tcx>(
} }
} }
check_where_clauses(fcx, item.span, item.def_id.to_def_id(), None); check_where_clauses(fcx, item.span, item.def_id, None);
fcx.impl_implied_bounds(item.def_id.to_def_id(), item.span) fcx.impl_implied_bounds(item.def_id.to_def_id(), item.span)
}); });
@ -1249,7 +1249,7 @@ fn check_impl<'tcx>(
fn check_where_clauses<'tcx, 'fcx>( fn check_where_clauses<'tcx, 'fcx>(
fcx: &FnCtxt<'fcx, 'tcx>, fcx: &FnCtxt<'fcx, 'tcx>,
span: Span, span: Span,
def_id: DefId, def_id: LocalDefId,
return_ty: Option<(Ty<'tcx>, Span)>, return_ty: Option<(Ty<'tcx>, Span)>,
) { ) {
let tcx = fcx.tcx; let tcx = fcx.tcx;
@ -1317,7 +1317,7 @@ fn check_where_clauses<'tcx, 'fcx>(
// For more examples see tests `defaults-well-formedness.rs` and `type-check-defaults.rs`. // For more examples see tests `defaults-well-formedness.rs` and `type-check-defaults.rs`.
// //
// First we build the defaulted substitution. // First we build the defaulted substitution.
let substs = InternalSubsts::for_item(tcx, def_id, |param, _| { let substs = InternalSubsts::for_item(tcx, def_id.to_def_id(), |param, _| {
match param.kind { match param.kind {
GenericParamDefKind::Lifetime => { GenericParamDefKind::Lifetime => {
// All regions are identity. // All regions are identity.
@ -1411,8 +1411,11 @@ fn check_where_clauses<'tcx, 'fcx>(
// below: there, we are not trying to prove those predicates // below: there, we are not trying to prove those predicates
// to be *true* but merely *well-formed*. // to be *true* but merely *well-formed*.
let pred = fcx.normalize_associated_types_in(sp, pred); let pred = fcx.normalize_associated_types_in(sp, pred);
let cause = let cause = traits::ObligationCause::new(
traits::ObligationCause::new(sp, fcx.body_id, traits::ItemObligation(def_id)); sp,
fcx.body_id,
traits::ItemObligation(def_id.to_def_id()),
);
traits::Obligation::new(cause, fcx.param_env, pred) traits::Obligation::new(cause, fcx.param_env, pred)
}); });
@ -1445,10 +1448,10 @@ fn check_fn_or_method<'fcx, 'tcx>(
span: Span, span: Span,
sig: ty::PolyFnSig<'tcx>, sig: ty::PolyFnSig<'tcx>,
hir_decl: &hir::FnDecl<'_>, hir_decl: &hir::FnDecl<'_>,
def_id: DefId, def_id: LocalDefId,
implied_bounds: &mut FxHashSet<Ty<'tcx>>, implied_bounds: &mut FxHashSet<Ty<'tcx>>,
) { ) {
let sig = fcx.tcx.liberate_late_bound_regions(def_id, sig); let sig = fcx.tcx.liberate_late_bound_regions(def_id.to_def_id(), sig);
// Normalize the input and output types one at a time, using a different // Normalize the input and output types one at a time, using a different
// `WellFormedLoc` for each. We cannot call `normalize_associated_types` // `WellFormedLoc` for each. We cannot call `normalize_associated_types`
@ -1462,7 +1465,7 @@ fn check_fn_or_method<'fcx, 'tcx>(
span, span,
ty, ty,
WellFormedLoc::Param { WellFormedLoc::Param {
function: def_id.expect_local(), function: def_id,
// Note that the `param_idx` of the output type is // Note that the `param_idx` of the output type is
// one greater than the index of the last input type. // one greater than the index of the last input type.
param_idx: i.try_into().unwrap(), param_idx: i.try_into().unwrap(),
@ -1485,7 +1488,7 @@ fn check_fn_or_method<'fcx, 'tcx>(
input_ty.into(), input_ty.into(),
ty.span, ty.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param { ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param {
function: def_id.expect_local(), function: def_id,
param_idx: i.try_into().unwrap(), param_idx: i.try_into().unwrap(),
})), })),
); );