Skip to content

Commit

Permalink
Rollup merge of #95560 - lcnr:obligation-cause, r=oli-obk
Browse files Browse the repository at this point in the history
convert more `DefId`s to `LocalDefId`
  • Loading branch information
Dylan-DPC committed Apr 2, 2022
2 parents 1e43cf4 + 8eacf60 commit 1c82fac
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,15 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
ObligationCauseCode::MatchImpl(parent, ..) => parent.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`
// lifetime as above, but called using a fully-qualified path to the method:
// `Foo::qux(bar)`.
let mut v = TraitObjectVisitor(FxHashSet::default());
v.visit_ty(param.param_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)
{
override_error_code = Some(ident.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::traits::ObligationCauseCode::CompareImplMethodObligation;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
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_middle::hir::nested_filter;
use rustc_middle::ty::print::RegionHighlightMode;
Expand Down Expand Up @@ -51,7 +51,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
{
let guar = self.emit_associated_type_err(
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,
trait_item_def_id,
);
Expand Down Expand Up @@ -155,7 +155,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
&self,
span: Span,
item_name: Symbol,
impl_item_def_id: DefId,
impl_item_def_id: LocalDefId,
trait_item_def_id: DefId,
) -> ErrorGuaranteed {
let impl_sp = self.tcx().def_span(impl_item_def_id);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/error_reporting/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let mut err = self.report_concrete_failure(*parent, sub, sup);

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(
trait_item_span,
format!("definition of `{}` from trait", item_name),
Expand All @@ -370,7 +370,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
let where_clause_span = self
.tcx
.hir()
.get_generics(impl_item_def_id.expect_local())
.get_generics(impl_item_def_id)
.unwrap()
.where_clause
.tail_span_for_suggestion();
Expand Down
10 changes: 7 additions & 3 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,20 @@ pub enum SubregionOrigin<'tcx> {

/// Comparing the signature and requirements of an impl method against
/// 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
/// 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
CheckAssociatedTypeBounds {
parent: Box<SubregionOrigin<'tcx>>,
impl_item_def_id: DefId,
impl_item_def_id: LocalDefId,
trait_item_def_id: DefId,
},
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_infer/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::infer::InferCtxt;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{struct_span_err, DiagnosticBuilder, ErrorGuaranteed};
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_span::{MultiSpan, Span};
use std::fmt;
Expand All @@ -14,7 +14,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
pub fn report_extra_impl_obligation(
&self,
error_span: Span,
impl_item_def_id: DefId,
impl_item_def_id: LocalDefId,
trait_item_def_id: DefId,
requirement: &dyn fmt::Display,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
Expand All @@ -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) {
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));
}

Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,24 +276,23 @@ pub enum ObligationCauseCode<'tcx> {

/// Error derived when matching traits/impls; see ObligationCause for more details
CompareImplMethodObligation {
impl_item_def_id: DefId,
impl_item_def_id: LocalDefId,
trait_item_def_id: DefId,
},

/// Error derived when matching traits/impls; see ObligationCause for more details
CompareImplTypeObligation {
impl_item_def_id: DefId,
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
CheckAssociatedTypeBounds {
impl_item_def_id: DefId,
impl_item_def_id: LocalDefId,
trait_item_def_id: DefId,
},

/// Checking that this expression can be assigned where it needs to be
// FIXME(eddyb) #11161 is the original Expr required?
/// Checking that this expression can be assigned to its target.
ExprAssignable,

/// Computing common supertype in the arms of a match expression
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
} else if let (
Ok(ref snippet),
ObligationCauseCode::BindingObligation(ref def_id, _),
&ObligationCauseCode::BindingObligation(def_id, _),
) =
(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)
&& !snippet.ends_with('>')
&& !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
// where already supplied, we check the snippet to make sure it doesn't
Expand Down Expand Up @@ -2223,6 +2223,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
"suggest_unsized_bound_if_applicable: pred={:?} item_def_id={:?} span={:?}",
pred, item_def_id, span
);

let (Some(node), true) = (
self.tcx.hir().get_if_local(item_def_id),
Some(pred.def_id()) == self.tcx.lang_items().sized_trait(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub trait InferCtxtExt<'tcx> {
fn suggest_fully_qualified_path(
&self,
err: &mut Diagnostic,
def_id: DefId,
item_def_id: DefId,
span: Span,
trait_ref: DefId,
);
Expand Down Expand Up @@ -1317,16 +1317,16 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
fn suggest_fully_qualified_path(
&self,
err: &mut Diagnostic,
def_id: DefId,
item_def_id: DefId,
span: Span,
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 {
err.note(&format!(
"{}s cannot be accessed directly on a `trait`, they can only be \
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(
span,
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_typeck/src/check/compare_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use super::{potentially_plural_count, FnCtxt, Inherited};
/// - `impl_m_span`: span to use for reporting errors
/// - `trait_m`: the method in the trait
/// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation

crate fn compare_impl_method<'tcx>(
tcx: TyCtxt<'tcx>,
impl_m: &ty::AssocItem,
Expand Down Expand Up @@ -88,7 +87,7 @@ fn compare_predicate_entailment<'tcx>(
impl_m_span,
impl_m_hir_id,
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,
},
);
Expand Down Expand Up @@ -231,7 +230,7 @@ fn compare_predicate_entailment<'tcx>(
span,
impl_m_hir_id,
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,
},
);
Expand Down Expand Up @@ -1154,7 +1153,7 @@ fn compare_type_predicate_entailment<'tcx>(
impl_ty_span,
impl_ty_hir_id,
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,
},
);
Expand Down Expand Up @@ -1383,7 +1382,7 @@ pub fn check_type_bounds<'tcx>(
impl_ty_span,
impl_ty_hir_id,
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,
},
);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/check/method/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::{probe, MethodCallee};

use crate::astconv::{AstConv, CreateSubstsForGenericArgsCtxt, IsMethodCall};
use crate::check::{callee, FnCtxt};
use crate::hir::def_id::DefId;
use crate::hir::GenericArg;
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use rustc_hir::GenericArg;
use rustc_infer::infer::{self, InferOk};
use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, PointerCast};
Expand Down
29 changes: 16 additions & 13 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ fn check_associated_item(
item.ident(fcx.tcx).span,
sig,
hir_sig.decl,
item.def_id,
item.def_id.expect_local(),
&mut implied_bounds,
);
check_method_receiver(fcx, hir_sig, item, self_ty);
Expand Down Expand Up @@ -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.
FxHashSet::default()
Expand Down Expand Up @@ -1096,7 +1096,7 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {

// FIXME: this shouldn't use an `FnCtxt` at all.
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()
});
Expand Down Expand Up @@ -1144,7 +1144,7 @@ fn check_item_fn(
for_id(tcx, def_id, span).with_fcx(|fcx| {
let sig = tcx.fn_sig(def_id);
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
})
}
Expand Down Expand Up @@ -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)
});
Expand All @@ -1249,7 +1249,7 @@ fn check_impl<'tcx>(
fn check_where_clauses<'tcx, 'fcx>(
fcx: &FnCtxt<'fcx, 'tcx>,
span: Span,
def_id: DefId,
def_id: LocalDefId,
return_ty: Option<(Ty<'tcx>, Span)>,
) {
let tcx = fcx.tcx;
Expand Down Expand Up @@ -1317,7 +1317,7 @@ fn check_where_clauses<'tcx, 'fcx>(
// For more examples see tests `defaults-well-formedness.rs` and `type-check-defaults.rs`.
//
// 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 {
GenericParamDefKind::Lifetime => {
// All regions are identity.
Expand Down Expand Up @@ -1411,8 +1411,11 @@ fn check_where_clauses<'tcx, 'fcx>(
// below: there, we are not trying to prove those predicates
// to be *true* but merely *well-formed*.
let pred = fcx.normalize_associated_types_in(sp, pred);
let cause =
traits::ObligationCause::new(sp, fcx.body_id, traits::ItemObligation(def_id));
let cause = traits::ObligationCause::new(
sp,
fcx.body_id,
traits::ItemObligation(def_id.to_def_id()),
);
traits::Obligation::new(cause, fcx.param_env, pred)
});

Expand Down Expand Up @@ -1445,10 +1448,10 @@ fn check_fn_or_method<'fcx, 'tcx>(
span: Span,
sig: ty::PolyFnSig<'tcx>,
hir_decl: &hir::FnDecl<'_>,
def_id: DefId,
def_id: LocalDefId,
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
// `WellFormedLoc` for each. We cannot call `normalize_associated_types`
Expand All @@ -1462,7 +1465,7 @@ fn check_fn_or_method<'fcx, 'tcx>(
span,
ty,
WellFormedLoc::Param {
function: def_id.expect_local(),
function: def_id,
// Note that the `param_idx` of the output type is
// one greater than the index of the last input type.
param_idx: i.try_into().unwrap(),
Expand All @@ -1485,7 +1488,7 @@ fn check_fn_or_method<'fcx, 'tcx>(
input_ty.into(),
ty.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param {
function: def_id.expect_local(),
function: def_id,
param_idx: i.try_into().unwrap(),
})),
);
Expand Down

0 comments on commit 1c82fac

Please sign in to comment.