From 796b82837116462377167b16a6d7627250aaa1b0 Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 1 Apr 2022 13:38:43 +0200 Subject: [PATCH 1/2] convert more `DefId`s to `LocalDefId` --- .../nice_region_error/static_impl_trait.rs | 4 +-- .../trait_impl_difference.rs | 6 ++-- .../src/infer/error_reporting/note.rs | 4 +-- compiler/rustc_infer/src/infer/mod.rs | 10 +++++-- .../src/traits/error_reporting/mod.rs | 6 ++-- compiler/rustc_middle/src/traits/mod.rs | 6 ++-- .../src/traits/error_reporting/mod.rs | 7 +++-- .../src/traits/error_reporting/suggestions.rs | 8 ++--- .../rustc_typeck/src/check/compare_method.rs | 9 +++--- .../rustc_typeck/src/check/method/confirm.rs | 4 +-- compiler/rustc_typeck/src/check/wfcheck.rs | 29 ++++++++++--------- 11 files changed, 50 insertions(+), 43 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs index 4fcdcb6366683..c33c0c996bdee 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs @@ -237,7 +237,7 @@ 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: @@ -245,7 +245,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { 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); diff --git a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs index b1a42ee66c920..6d23dc4f471f7 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/nice_region_error/trait_impl_difference.rs @@ -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; @@ -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, ); @@ -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); diff --git a/compiler/rustc_infer/src/infer/error_reporting/note.rs b/compiler/rustc_infer/src/infer/error_reporting/note.rs index e6c8dc302b6ca..5dcac7f56cc41 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/note.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/note.rs @@ -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), @@ -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(); diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index bd59bf4dea883..0bb6605492281 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -425,16 +425,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>, - impl_item_def_id: DefId, + impl_item_def_id: LocalDefId, trait_item_def_id: DefId, }, } diff --git a/compiler/rustc_infer/src/traits/error_reporting/mod.rs b/compiler/rustc_infer/src/traits/error_reporting/mod.rs index c8ef08f48b609..25b11e31d57d4 100644 --- a/compiler/rustc_infer/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/traits/error_reporting/mod.rs @@ -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; @@ -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> { @@ -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)); } diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 1f18260d9154f..353b872a216da 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -276,19 +276,19 @@ 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, }, diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index d83781170e8c3..51b09906e396c 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1797,15 +1797,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 @@ -2107,6 +2107,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(), diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 58e002b336081..b49a5f6578f75 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -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, ); @@ -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, diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index a22c9210a9c22..0bd5e018f4a38 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -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, @@ -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, }, ); @@ -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, }, ); @@ -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, }, ); @@ -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, }, ); diff --git a/compiler/rustc_typeck/src/check/method/confirm.rs b/compiler/rustc_typeck/src/check/method/confirm.rs index 3fa873e46abbb..bc0fa9165561d 100644 --- a/compiler/rustc_typeck/src/check/method/confirm.rs +++ b/compiler/rustc_typeck/src/check/method/confirm.rs @@ -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}; diff --git a/compiler/rustc_typeck/src/check/wfcheck.rs b/compiler/rustc_typeck/src/check/wfcheck.rs index bd08da9529a5f..9b1767c783555 100644 --- a/compiler/rustc_typeck/src/check/wfcheck.rs +++ b/compiler/rustc_typeck/src/check/wfcheck.rs @@ -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); @@ -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() @@ -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() }); @@ -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 }) } @@ -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) }); @@ -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; @@ -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. @@ -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) }); @@ -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>, ) { - 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` @@ -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(), @@ -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(), })), ); From 8eacf6078fd923bddf54ae9227beeb41a7218e1d Mon Sep 17 00:00:00 2001 From: lcnr Date: Fri, 1 Apr 2022 13:47:01 +0200 Subject: [PATCH 2/2] update comment --- compiler/rustc_middle/src/traits/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index 353b872a216da..7c3d08b26bf54 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -292,8 +292,7 @@ pub enum ObligationCauseCode<'tcx> { 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