diff options
Diffstat (limited to 'compiler/rustc_hir_analysis/src')
48 files changed, 492 insertions, 370 deletions
diff --git a/compiler/rustc_hir_analysis/src/autoderef.rs b/compiler/rustc_hir_analysis/src/autoderef.rs index 53c8586b52a..46a0d4f498a 100644 --- a/compiler/rustc_hir_analysis/src/autoderef.rs +++ b/compiler/rustc_hir_analysis/src/autoderef.rs @@ -4,6 +4,7 @@ use rustc_session::Limit; use rustc_span::def_id::{LocalDefId, LOCAL_CRATE}; use rustc_span::Span; use rustc_trait_selection::traits::ObligationCtxt; +use tracing::{debug, instrument}; use crate::errors::AutoDerefReachedRecursionLimit; use crate::traits; diff --git a/compiler/rustc_hir_analysis/src/bounds.rs b/compiler/rustc_hir_analysis/src/bounds.rs index c30a6f1eeb9..d0b0c08aa79 100644 --- a/compiler/rustc_hir_analysis/src/bounds.rs +++ b/compiler/rustc_hir_analysis/src/bounds.rs @@ -26,13 +26,13 @@ use rustc_span::Span; /// Our representation is a bit mixed here -- in some cases, we /// include the self type (e.g., `trait_bounds`) but in others we do not #[derive(Default, PartialEq, Eq, Clone, Debug)] -pub struct Bounds<'tcx> { +pub(crate) struct Bounds<'tcx> { clauses: Vec<(ty::Clause<'tcx>, Span)>, effects_min_tys: FxIndexMap<Ty<'tcx>, Span>, } impl<'tcx> Bounds<'tcx> { - pub fn push_region_bound( + pub(crate) fn push_region_bound( &mut self, tcx: TyCtxt<'tcx>, region: ty::PolyTypeOutlivesPredicate<'tcx>, @@ -42,7 +42,7 @@ impl<'tcx> Bounds<'tcx> { .push((region.map_bound(|p| ty::ClauseKind::TypeOutlives(p)).upcast(tcx), span)); } - pub fn push_trait_bound( + pub(crate) fn push_trait_bound( &mut self, tcx: TyCtxt<'tcx>, defining_def_id: DefId, @@ -154,7 +154,7 @@ impl<'tcx> Bounds<'tcx> { self.clauses.push((bound_trait_ref.rebind(new_trait_ref).upcast(tcx), span)); } - pub fn push_projection_bound( + pub(crate) fn push_projection_bound( &mut self, tcx: TyCtxt<'tcx>, projection: ty::PolyProjectionPredicate<'tcx>, @@ -166,14 +166,14 @@ impl<'tcx> Bounds<'tcx> { )); } - pub fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) { + pub(crate) fn push_sized(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, span: Span) { let sized_def_id = tcx.require_lang_item(LangItem::Sized, Some(span)); let trait_ref = ty::TraitRef::new(tcx, sized_def_id, [ty]); // Preferable to put this obligation first, since we report better errors for sized ambiguity. self.clauses.insert(0, (trait_ref.upcast(tcx), span)); } - pub fn clauses( + pub(crate) fn clauses( &self, // FIXME(effects): remove tcx _tcx: TyCtxt<'tcx>, @@ -181,7 +181,7 @@ impl<'tcx> Bounds<'tcx> { self.clauses.iter().cloned() } - pub fn effects_min_tys(&self) -> impl Iterator<Item = Ty<'tcx>> + '_ { + pub(crate) fn effects_min_tys(&self) -> impl Iterator<Item = Ty<'tcx>> + '_ { self.effects_min_tys.keys().copied() } } diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 0135cdf1e90..e47c707ee18 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -27,6 +27,7 @@ use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::traits; use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _; use rustc_type_ir::fold::TypeFoldable; +use tracing::{debug, instrument}; use {rustc_attr as attr, rustc_hir as hir}; use super::compare_impl_item::{check_type_bounds, compare_impl_method, compare_impl_ty}; @@ -529,7 +530,7 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe match tcx.named_bound_var(hir_id) { Some(ResolvedArg::EarlyBound(def_id)) => { - expected_captures.insert(def_id); + expected_captures.insert(def_id.to_def_id()); // Make sure we allow capturing these lifetimes through `Self` and // `T::Assoc` projection syntax, too. These will occur when we only @@ -538,7 +539,7 @@ fn check_opaque_precise_captures<'tcx>(tcx: TyCtxt<'tcx>, opaque_def_id: LocalDe // feature -- see <https://github.com/rust-lang/rust/pull/115659>. if let DefKind::LifetimeParam = tcx.def_kind(def_id) && let Some(def_id) = tcx - .map_opaque_lifetime_to_parent_lifetime(def_id.expect_local()) + .map_opaque_lifetime_to_parent_lifetime(def_id) .opt_param_def_id(tcx, tcx.parent(opaque_def_id.to_def_id())) { shadowed_captures.insert(def_id); @@ -1053,7 +1054,7 @@ fn check_impl_items_against_trait<'tcx>( } } -pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) { +fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) { let t = tcx.type_of(def_id).instantiate_identity(); if let ty::Adt(def, args) = t.kind() && def.is_struct() @@ -1259,7 +1260,8 @@ pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, adt: ty::AdtDef<'tcx>) ty::Tuple(list) => list.iter().try_for_each(|t| check_non_exhaustive(tcx, t)), ty::Array(ty, _) => check_non_exhaustive(tcx, *ty), ty::Adt(def, args) => { - if !def.did().is_local() { + if !def.did().is_local() && !tcx.has_attr(def.did(), sym::rustc_pub_transparent) + { let non_exhaustive = def.is_variant_list_non_exhaustive() || def .variants() @@ -1564,7 +1566,7 @@ fn check_type_alias_type_params_are_used<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalD // * compare the param span to the pred span to detect lone user-written `Sized` bounds let has_explicit_bounds = bounded_params.is_empty() || (*bounded_params).get(¶m.index).is_some_and(|&&pred_sp| pred_sp != span); - let const_param_help = (!has_explicit_bounds).then_some(()); + let const_param_help = !has_explicit_bounds; let mut diag = tcx.dcx().create_err(errors::UnusedGenericParameter { span, diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs index 35577613800..2afed04c5bc 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item.rs @@ -28,6 +28,7 @@ use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _; use rustc_trait_selection::traits::{ self, FulfillmentError, ObligationCause, ObligationCauseCode, ObligationCtxt, Reveal, }; +use tracing::{debug, instrument}; use super::potentially_plural_count; use crate::errors::{LifetimesOrBoundsMismatchOnTrait, MethodShouldReturnFuture}; diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs index 80daaa60324..d2b7ede6523 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs @@ -7,7 +7,8 @@ use rustc_lint_defs::builtin::{REFINING_IMPL_TRAIT_INTERNAL, REFINING_IMPL_TRAIT use rustc_middle::span_bug; use rustc_middle::traits::{ObligationCause, Reveal}; use rustc_middle::ty::{ - self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor, + self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, + TypeVisitableExt, TypeVisitor, }; use rustc_span::Span; use rustc_trait_selection::regions::InferCtxtRegionExt; @@ -177,6 +178,10 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( return; }; + if trait_bounds.references_error() || impl_bounds.references_error() { + return; + } + // For quicker lookup, use an `IndexSet` (we don't use one earlier because // it's not foldable..). // Also, We have to anonymize binders in these types because they may contain diff --git a/compiler/rustc_hir_analysis/src/check/entry.rs b/compiler/rustc_hir_analysis/src/check/entry.rs index 1f724580564..83d2c2c1e28 100644 --- a/compiler/rustc_hir_analysis/src/check/entry.rs +++ b/compiler/rustc_hir_analysis/src/check/entry.rs @@ -216,7 +216,7 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) { } if sig.header.asyncness.is_async() { let span = tcx.def_span(it.owner_id); - tcx.dcx().emit_err(errors::StartAsync { span: span }); + tcx.dcx().emit_err(errors::StartAsync { span }); error = true; } diff --git a/compiler/rustc_hir_analysis/src/check/errs.rs b/compiler/rustc_hir_analysis/src/check/errs.rs index 19a0476e630..22d7d1fea9c 100644 --- a/compiler/rustc_hir_analysis/src/check/errs.rs +++ b/compiler/rustc_hir_analysis/src/check/errs.rs @@ -6,7 +6,7 @@ use rustc_span::Span; use crate::errors; /// Check for shared or mutable references of `static mut` inside expression -pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) { +pub(crate) fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) { let span = expr.span; let hir_id = expr.hir_id; if let hir::ExprKind::AddrOf(borrow_kind, m, expr) = expr.kind @@ -26,7 +26,7 @@ pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) { } /// Check for shared or mutable references of `static mut` inside statement -pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) { +pub(crate) fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) { if let hir::StmtKind::Let(loc) = stmt.kind && let hir::PatKind::Binding(ba, _, _, _) = loc.pat.kind && let hir::ByRef::Yes(rmutbl) = ba.0 diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 678b8c89a50..6820a44f141 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -97,6 +97,7 @@ use rustc_trait_selection::error_reporting::infer::ObligationCauseExt as _; use rustc_trait_selection::error_reporting::traits::suggestions::ReturnsVisitor; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::traits::ObligationCtxt; +use tracing::debug; use self::compare_impl_item::collect_return_position_impl_trait_in_trait_tys; use self::region::region_scope_tree; diff --git a/compiler/rustc_hir_analysis/src/check/region.rs b/compiler/rustc_hir_analysis/src/check/region.rs index bc6641c688c..2d6147cff2a 100644 --- a/compiler/rustc_hir_analysis/src/check/region.rs +++ b/compiler/rustc_hir_analysis/src/check/region.rs @@ -18,11 +18,12 @@ use rustc_middle::bug; use rustc_middle::middle::region::*; use rustc_middle::ty::TyCtxt; use rustc_span::source_map; +use tracing::debug; use super::errs::{maybe_expr_static_mut, maybe_stmt_static_mut}; #[derive(Debug, Copy, Clone)] -pub struct Context { +struct Context { /// The scope that contains any new variables declared, plus its depth in /// the scope tree. var_parent: Option<(Scope, ScopeDepth)>, @@ -893,7 +894,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> { /// re-use in incremental scenarios. We may sometimes need to rerun the /// type checker even when the HIR hasn't changed, and in those cases /// we can avoid reconstructing the region scope tree. -pub fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree { +pub(crate) fn region_scope_tree(tcx: TyCtxt<'_>, def_id: DefId) -> &ScopeTree { let typeck_root_def_id = tcx.typeck_root_def_id(def_id); if typeck_root_def_id != def_id { return tcx.region_scope_tree(typeck_root_def_id); diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index bdf2914fc50..3627faf8dfc 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -36,6 +36,7 @@ use rustc_trait_selection::traits::{ }; use rustc_type_ir::solve::NoSolution; use rustc_type_ir::TypeFlags; +use tracing::{debug, instrument}; use {rustc_ast as ast, rustc_hir as hir}; use crate::autoderef::Autoderef; @@ -747,7 +748,7 @@ fn region_known_to_outlive<'tcx>( region_b: ty::Region<'tcx>, ) -> bool { test_region_obligations(tcx, id, param_env, wf_tys, |infcx| { - infcx.sub_regions(infer::RelateRegionParamBound(DUMMY_SP), region_b, region_a); + infcx.sub_regions(infer::RelateRegionParamBound(DUMMY_SP, None), region_b, region_a); }) } @@ -1015,7 +1016,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) -> Result<(), sym::adt_const_params, )]) } - // Implments `ConstParamTy`, suggest adding the feature to enable. + // Implements `ConstParamTy`, suggest adding the feature to enable. Ok(..) => Some(vec![(adt_const_params_feature_string, sym::adt_const_params)]), }; if let Some(features) = may_suggest_feature { @@ -1651,6 +1652,13 @@ fn check_fn_or_method<'tcx>( } } +/// The `arbitrary_self_types_pointers` feature implies `arbitrary_self_types`. +#[derive(Clone, Copy, PartialEq)] +enum ArbitrarySelfTypesLevel { + Basic, // just arbitrary_self_types + WithPointers, // both arbitrary_self_types and arbitrary_self_types_pointers +} + #[instrument(level = "debug", skip(wfcx))] fn check_method_receiver<'tcx>( wfcx: &WfCheckingCtxt<'_, 'tcx>, @@ -1683,14 +1691,27 @@ fn check_method_receiver<'tcx>( return Ok(()); } - if tcx.features().arbitrary_self_types { - if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) { - // Report error; `arbitrary_self_types` was enabled. - return Err(tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty })); - } + let arbitrary_self_types_level = if tcx.features().arbitrary_self_types_pointers { + Some(ArbitrarySelfTypesLevel::WithPointers) + } else if tcx.features().arbitrary_self_types { + Some(ArbitrarySelfTypesLevel::Basic) } else { - if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, false) { - return Err(if receiver_is_valid(wfcx, span, receiver_ty, self_ty, true) { + None + }; + + if !receiver_is_valid(wfcx, span, receiver_ty, self_ty, arbitrary_self_types_level) { + return Err(match arbitrary_self_types_level { + // Wherever possible, emit a message advising folks that the features + // `arbitrary_self_types` or `arbitrary_self_types_pointers` might + // have helped. + None if receiver_is_valid( + wfcx, + span, + receiver_ty, + self_ty, + Some(ArbitrarySelfTypesLevel::Basic), + ) => + { // Report error; would have worked with `arbitrary_self_types`. feature_err( &tcx.sess, @@ -1698,25 +1719,49 @@ fn check_method_receiver<'tcx>( span, format!( "`{receiver_ty}` cannot be used as the type of `self` without \ - the `arbitrary_self_types` feature", + the `arbitrary_self_types` feature", ), ) .with_help(fluent::hir_analysis_invalid_receiver_ty_help) .emit() - } else { - // Report error; would not have worked with `arbitrary_self_types`. + } + None | Some(ArbitrarySelfTypesLevel::Basic) + if receiver_is_valid( + wfcx, + span, + receiver_ty, + self_ty, + Some(ArbitrarySelfTypesLevel::WithPointers), + ) => + { + // Report error; would have worked with `arbitrary_self_types_pointers`. + feature_err( + &tcx.sess, + sym::arbitrary_self_types_pointers, + span, + format!( + "`{receiver_ty}` cannot be used as the type of `self` without \ + the `arbitrary_self_types_pointers` feature", + ), + ) + .with_help(fluent::hir_analysis_invalid_receiver_ty_help) + .emit() + } + _ => + // Report error; would not have worked with `arbitrary_self_types[_pointers]`. + { tcx.dcx().emit_err(errors::InvalidReceiverTy { span, receiver_ty }) - }); - } + } + }); } Ok(()) } /// Returns whether `receiver_ty` would be considered a valid receiver type for `self_ty`. If /// `arbitrary_self_types` is enabled, `receiver_ty` must transitively deref to `self_ty`, possibly -/// through a `*const/mut T` raw pointer. If the feature is not enabled, the requirements are more -/// strict: `receiver_ty` must implement `Receiver` and directly implement -/// `Deref<Target = self_ty>`. +/// through a `*const/mut T` raw pointer if `arbitrary_self_types_pointers` is also enabled. +/// If neither feature is enabled, the requirements are more strict: `receiver_ty` must implement +/// `Receiver` and directly implement `Deref<Target = self_ty>`. /// /// N.B., there are cases this function returns `true` but causes an error to be emitted, /// particularly when `receiver_ty` derefs to a type that is the same as `self_ty` but has the @@ -1726,7 +1771,7 @@ fn receiver_is_valid<'tcx>( span: Span, receiver_ty: Ty<'tcx>, self_ty: Ty<'tcx>, - arbitrary_self_types_enabled: bool, + arbitrary_self_types_enabled: Option<ArbitrarySelfTypesLevel>, ) -> bool { let infcx = wfcx.infcx; let tcx = wfcx.tcx(); @@ -1744,8 +1789,8 @@ fn receiver_is_valid<'tcx>( 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`. - if arbitrary_self_types_enabled { + // The `arbitrary_self_types_pointers` feature allows raw pointer receivers like `self: *const Self`. + if arbitrary_self_types_enabled == Some(ArbitrarySelfTypesLevel::WithPointers) { autoderef = autoderef.include_raw_pointers(); } @@ -1771,7 +1816,7 @@ fn receiver_is_valid<'tcx>( // Without `feature(arbitrary_self_types)`, we require that each step in the // deref chain implement `receiver`. - if !arbitrary_self_types_enabled { + if arbitrary_self_types_enabled.is_none() { if !receiver_is_implemented( wfcx, receiver_trait_def_id, @@ -1972,8 +2017,7 @@ fn report_bivariance<'tcx>( } let const_param_help = - matches!(param.kind, hir::GenericParamKind::Type { .. } if !has_explicit_bounds) - .then_some(()); + matches!(param.kind, hir::GenericParamKind::Type { .. } if !has_explicit_bounds); let mut diag = tcx.dcx().create_err(errors::UnusedGenericParameter { span: param.span, @@ -2175,7 +2219,8 @@ fn lint_redundant_lifetimes<'tcx>( | DefKind::Field | DefKind::LifetimeParam | DefKind::GlobalAsm - | DefKind::Closure => return, + | DefKind::Closure + | DefKind::SyntheticCoroutineBody => return, } // The ordering of this lifetime map is a bit subtle. diff --git a/compiler/rustc_hir_analysis/src/check_unused.rs b/compiler/rustc_hir_analysis/src/check_unused.rs index ed23dc2a827..68b7b44c36d 100644 --- a/compiler/rustc_hir_analysis/src/check_unused.rs +++ b/compiler/rustc_hir_analysis/src/check_unused.rs @@ -4,8 +4,9 @@ use rustc_hir::def_id::LocalDefId; use rustc_middle::query::Providers; use rustc_middle::ty::TyCtxt; use rustc_session::lint; +use tracing::debug; -pub fn provide(providers: &mut Providers) { +pub(crate) fn provide(providers: &mut Providers) { *providers = Providers { check_unused_traits, ..*providers }; } diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs index fecd78bc38f..30fc06829ed 100644 --- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs +++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs @@ -23,6 +23,7 @@ use rustc_trait_selection::traits::misc::{ ConstParamTyImplementationError, CopyImplementationError, InfringingFieldsReason, }; use rustc_trait_selection::traits::{self, ObligationCause, ObligationCtxt}; +use tracing::debug; use crate::errors; @@ -218,7 +219,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<() // Later parts of the compiler rely on all DispatchFromDyn types to be ABI-compatible with raw // pointers. This is enforced here: we only allow impls for references, raw pointers, and things // that are effectively repr(transparent) newtypes around types that already hav a - // DispatchedFromDyn impl. We cannot literally use repr(transparent) on those tpyes since some + // DispatchedFromDyn impl. We cannot literally use repr(transparent) on those types since some // of them support an allocator, but we ensure that for the cases where the type implements this // trait, they *do* satisfy the repr(transparent) rules, and then we assume that everything else // in the compiler (in particular, all the call ABI logic) will treat them as repr(transparent) @@ -332,7 +333,7 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<() } } -pub fn coerce_unsized_info<'tcx>( +pub(crate) fn coerce_unsized_info<'tcx>( tcx: TyCtxt<'tcx>, impl_did: LocalDefId, ) -> Result<CoerceUnsizedInfo, ErrorGuaranteed> { diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs index 89acb778d6c..9fce927e2c7 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs @@ -19,7 +19,7 @@ use rustc_span::ErrorGuaranteed; use crate::errors; /// On-demand query: yields a map containing all types mapped to their inherent impls. -pub fn crate_inherent_impls( +pub(crate) fn crate_inherent_impls( tcx: TyCtxt<'_>, (): (), ) -> Result<&'_ CrateInherentImpls, ErrorGuaranteed> { @@ -32,7 +32,7 @@ pub fn crate_inherent_impls( Ok(tcx.arena.alloc(collect.impls_map)) } -pub fn crate_incoherent_impls( +pub(crate) fn crate_incoherent_impls( tcx: TyCtxt<'_>, simp: SimplifiedType, ) -> Result<&[DefId], ErrorGuaranteed> { @@ -43,7 +43,10 @@ pub fn crate_incoherent_impls( } /// On-demand query: yields a vector of the inherent impls for a specific type. -pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> Result<&[DefId], ErrorGuaranteed> { +pub(crate) fn inherent_impls( + tcx: TyCtxt<'_>, + ty_def_id: LocalDefId, +) -> Result<&[DefId], ErrorGuaranteed> { let crate_map = tcx.crate_inherent_impls(())?; Ok(match crate_map.inherent_impls.get(&ty_def_id) { Some(v) => &v[..], @@ -90,7 +93,8 @@ impl<'tcx> InherentCollect<'tcx> { } } - if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) { + if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::InstantiateWithInfer) + { self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id); } else { bug!("unexpected self type: {:?}", self_ty); @@ -129,7 +133,7 @@ impl<'tcx> InherentCollect<'tcx> { } } - if let Some(simp) = simplify_type(self.tcx, ty, TreatParams::AsCandidateKey) { + if let Some(simp) = simplify_type(self.tcx, ty, TreatParams::InstantiateWithInfer) { self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id); } else { bug!("unexpected primitive type: {:?}", ty); diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs index cd5cc33d65a..6825c2e33fe 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs @@ -10,8 +10,12 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_span::{ErrorGuaranteed, Symbol}; use rustc_trait_selection::traits::{self, SkipLeakCheck}; use smallvec::SmallVec; +use tracing::debug; -pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, (): ()) -> Result<(), ErrorGuaranteed> { +pub(crate) fn crate_inherent_impls_overlap_check( + tcx: TyCtxt<'_>, + (): (), +) -> Result<(), ErrorGuaranteed> { let mut inherent_overlap_checker = InherentOverlapChecker { tcx }; let mut res = Ok(()); for id in tcx.hir().items() { diff --git a/compiler/rustc_hir_analysis/src/coherence/mod.rs b/compiler/rustc_hir_analysis/src/coherence/mod.rs index 8e4da90ca26..db809e4837d 100644 --- a/compiler/rustc_hir_analysis/src/coherence/mod.rs +++ b/compiler/rustc_hir_analysis/src/coherence/mod.rs @@ -13,6 +13,7 @@ use rustc_middle::query::Providers; use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; use rustc_session::parse::feature_err; use rustc_span::{sym, ErrorGuaranteed}; +use tracing::debug; use crate::errors; @@ -123,7 +124,7 @@ fn enforce_empty_impls_for_marker_traits( .emit()) } -pub fn provide(providers: &mut Providers) { +pub(crate) fn provide(providers: &mut Providers) { use self::builtin::coerce_unsized_info; use self::inherent_impls::{crate_incoherent_impls, crate_inherent_impls, inherent_impls}; use self::inherent_impls_overlap::crate_inherent_impls_overlap_check; diff --git a/compiler/rustc_hir_analysis/src/coherence/orphan.rs b/compiler/rustc_hir_analysis/src/coherence/orphan.rs index dcd0e3111a4..5fdaba41fb2 100644 --- a/compiler/rustc_hir_analysis/src/coherence/orphan.rs +++ b/compiler/rustc_hir_analysis/src/coherence/orphan.rs @@ -14,6 +14,7 @@ use rustc_span::def_id::{DefId, LocalDefId}; use rustc_trait_selection::traits::{ self, IsFirstInputType, OrphanCheckErr, OrphanCheckMode, UncoveredTyParams, }; +use tracing::{debug, instrument}; use crate::errors; diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index f75954c9edf..92111805ab4 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -34,7 +34,7 @@ use rustc_infer::traits::ObligationCause; use rustc_middle::hir::nested_filter; use rustc_middle::query::Providers; use rustc_middle::ty::util::{Discr, IntTypeExt}; -use rustc_middle::ty::{self, AdtKind, Const, IsSuggestable, Ty, TyCtxt, Upcast}; +use rustc_middle::ty::{self, AdtKind, Const, IsSuggestable, Ty, TyCtxt}; use rustc_middle::{bug, span_bug}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -42,6 +42,7 @@ use rustc_target::spec::abi; use rustc_trait_selection::error_reporting::traits::suggestions::NextTypeParamName; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::ObligationCtxt; +use tracing::{debug, instrument}; use crate::check::intrinsic::intrinsic_operation_unsafety; use crate::errors; @@ -70,7 +71,6 @@ pub fn provide(providers: &mut Providers) { impl_super_outlives: item_bounds::impl_super_outlives, generics_of: generics_of::generics_of, predicates_of: predicates_of::predicates_of, - predicates_defined_on, explicit_predicates_of: predicates_of::explicit_predicates_of, explicit_super_predicates_of: predicates_of::explicit_super_predicates_of, explicit_implied_predicates_of: predicates_of::explicit_implied_predicates_of, @@ -392,7 +392,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> { } fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> { - if let RegionInferReason::BorrowedObjectLifetimeDefault = reason { + if let RegionInferReason::ObjectLifetimeDefault = reason { let e = struct_span_code_err!( self.dcx(), span, @@ -421,7 +421,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> { span: Span, def_id: LocalDefId, assoc_name: Ident, - ) -> ty::GenericPredicates<'tcx> { + ) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> { self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name)) } @@ -1037,7 +1037,7 @@ impl<'tcx> FieldUniquenessCheckContext<'tcx> { /// Check the uniqueness of fields in a struct variant, and recursively /// check the nested fields if it is an unnamed field with type of an - /// annoymous adt. + /// anonymous adt. fn check_field(&mut self, field: &hir::FieldDef<'_>) { if field.ident.name != kw::Underscore { self.check_field_decl(field.ident, field.span.into()); @@ -1491,7 +1491,7 @@ fn infer_return_ty_for_fn_sig<'tcx>( Some(ty) => { let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id]; // Typeck doesn't expect erased regions to be returned from `type_of`. - // This is a heuristic approach. If the scope has region paramters, + // This is a heuristic approach. If the scope has region parameters, // we should change fn_sig's lifetime from `ReErased` to `ReError`, // otherwise to `ReStatic`. let has_region_params = generics.params.iter().any(|param| match param.kind { @@ -1775,34 +1775,6 @@ fn early_bound_lifetimes_from_generics<'a, 'tcx: 'a>( }) } -/// Returns a list of type predicates for the definition with ID `def_id`, including inferred -/// lifetime constraints. This includes all predicates returned by `explicit_predicates_of`, plus -/// inferred constraints concerning which regions outlive other regions. -#[instrument(level = "debug", skip(tcx))] -fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> { - let mut result = tcx.explicit_predicates_of(def_id); - debug!("predicates_defined_on: explicit_predicates_of({:?}) = {:?}", def_id, result); - let inferred_outlives = tcx.inferred_outlives_of(def_id); - if !inferred_outlives.is_empty() { - debug!( - "predicates_defined_on: inferred_outlives_of({:?}) = {:?}", - def_id, inferred_outlives, - ); - let inferred_outlives_iter = - inferred_outlives.iter().map(|(clause, span)| ((*clause).upcast(tcx), *span)); - if result.predicates.is_empty() { - result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter); - } else { - result.predicates = tcx.arena.alloc_from_iter( - result.predicates.into_iter().copied().chain(inferred_outlives_iter), - ); - } - } - - debug!("predicates_defined_on({:?}) = {:?}", def_id, result); - result -} - fn compute_sig_of_foreign_fn_decl<'tcx>( tcx: TyCtxt<'tcx>, def_id: LocalDefId, diff --git a/compiler/rustc_hir_analysis/src/collect/generics_of.rs b/compiler/rustc_hir_analysis/src/collect/generics_of.rs index e5f3d68caa5..ba01ea3f512 100644 --- a/compiler/rustc_hir_analysis/src/collect/generics_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/generics_of.rs @@ -10,6 +10,7 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_session::lint; use rustc_span::symbol::{kw, Symbol}; use rustc_span::Span; +use tracing::{debug, instrument}; use crate::delegation::inherit_generics_for_delegation_item; use crate::middle::resolve_bound_vars as rbv; diff --git a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs index ec48c781c0e..d62727e76b5 100644 --- a/compiler/rustc_hir_analysis/src/collect/item_bounds.rs +++ b/compiler/rustc_hir_analysis/src/collect/item_bounds.rs @@ -8,6 +8,7 @@ use rustc_middle::{bug, span_bug}; use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::Span; use rustc_type_ir::Upcast; +use tracing::{debug, instrument}; use super::ItemCtxt; use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter}; diff --git a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs index 61ba4346c61..7243e85ce98 100644 --- a/compiler/rustc_hir_analysis/src/collect/predicates_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/predicates_of.rs @@ -10,6 +10,7 @@ use rustc_middle::ty::{self, GenericPredicates, ImplTraitInTraitData, Ty, TyCtxt use rustc_middle::{bug, span_bug}; use rustc_span::symbol::Ident; use rustc_span::{Span, DUMMY_SP}; +use tracing::{debug, instrument, trace}; use crate::bounds::Bounds; use crate::collect::ItemCtxt; @@ -18,10 +19,26 @@ use crate::delegation::inherit_predicates_for_delegation_item; use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter, RegionInferReason}; /// Returns a list of all type predicates (explicit and implicit) for the definition with -/// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus -/// `Self: Trait` predicates for traits. +/// ID `def_id`. This includes all predicates returned by `explicit_predicates_of`, plus +/// inferred constraints concerning which regions outlive other regions. +#[instrument(level = "debug", skip(tcx))] pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> { - let mut result = tcx.predicates_defined_on(def_id); + let mut result = tcx.explicit_predicates_of(def_id); + debug!("predicates_of: explicit_predicates_of({:?}) = {:?}", def_id, result); + + let inferred_outlives = tcx.inferred_outlives_of(def_id); + if !inferred_outlives.is_empty() { + debug!("predicates_of: inferred_outlives_of({:?}) = {:?}", def_id, inferred_outlives,); + let inferred_outlives_iter = + inferred_outlives.iter().map(|(clause, span)| ((*clause).upcast(tcx), *span)); + if result.predicates.is_empty() { + result.predicates = tcx.arena.alloc_from_iter(inferred_outlives_iter); + } else { + result.predicates = tcx.arena.alloc_from_iter( + result.predicates.into_iter().copied().chain(inferred_outlives_iter), + ); + } + } if tcx.is_trait(def_id) { // For traits, add `Self: Trait` predicate. This is @@ -51,7 +68,8 @@ pub(super) fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredic .chain(std::iter::once((ty::TraitRef::identity(tcx, def_id).upcast(tcx), span))), ); } - debug!("predicates_of(def_id={:?}) = {:?}", def_id, result); + + debug!("predicates_of({:?}) = {:?}", def_id, result); result } @@ -559,24 +577,24 @@ pub(super) fn explicit_predicates_of<'tcx>( /// Ensures that the super-predicates of the trait with a `DefId` /// of `trait_def_id` are lowered and stored. This also ensures that /// the transitive super-predicates are lowered. -pub(super) fn explicit_super_predicates_of( - tcx: TyCtxt<'_>, +pub(super) fn explicit_super_predicates_of<'tcx>( + tcx: TyCtxt<'tcx>, trait_def_id: LocalDefId, -) -> ty::GenericPredicates<'_> { +) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> { implied_predicates_with_filter(tcx, trait_def_id.to_def_id(), PredicateFilter::SelfOnly) } -pub(super) fn explicit_supertraits_containing_assoc_item( - tcx: TyCtxt<'_>, +pub(super) fn explicit_supertraits_containing_assoc_item<'tcx>( + tcx: TyCtxt<'tcx>, (trait_def_id, assoc_name): (DefId, Ident), -) -> ty::GenericPredicates<'_> { +) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> { implied_predicates_with_filter(tcx, trait_def_id, PredicateFilter::SelfThatDefines(assoc_name)) } -pub(super) fn explicit_implied_predicates_of( - tcx: TyCtxt<'_>, +pub(super) fn explicit_implied_predicates_of<'tcx>( + tcx: TyCtxt<'tcx>, trait_def_id: LocalDefId, -) -> ty::GenericPredicates<'_> { +) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> { implied_predicates_with_filter( tcx, trait_def_id.to_def_id(), @@ -591,11 +609,11 @@ pub(super) fn explicit_implied_predicates_of( /// Ensures that the super-predicates of the trait with a `DefId` /// of `trait_def_id` are lowered and stored. This also ensures that /// the transitive super-predicates are lowered. -pub(super) fn implied_predicates_with_filter( - tcx: TyCtxt<'_>, +pub(super) fn implied_predicates_with_filter<'tcx>( + tcx: TyCtxt<'tcx>, trait_def_id: DefId, filter: PredicateFilter, -) -> ty::GenericPredicates<'_> { +) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> { let Some(trait_def_id) = trait_def_id.as_local() else { // if `assoc_name` is None, then the query should've been redirected to an // external provider @@ -658,20 +676,16 @@ pub(super) fn implied_predicates_with_filter( _ => {} } - ty::GenericPredicates { - parent: None, - predicates: implied_bounds, - effects_min_tys: ty::List::empty(), - } + ty::EarlyBinder::bind(implied_bounds) } /// Returns the predicates defined on `item_def_id` of the form /// `X: Foo` where `X` is the type parameter `def_id`. #[instrument(level = "trace", skip(tcx))] -pub(super) fn type_param_predicates( - tcx: TyCtxt<'_>, +pub(super) fn type_param_predicates<'tcx>( + tcx: TyCtxt<'tcx>, (item_def_id, def_id, assoc_name): (LocalDefId, LocalDefId, Ident), -) -> ty::GenericPredicates<'_> { +) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> { use rustc_hir::*; use rustc_middle::ty::Ty; @@ -692,18 +706,20 @@ pub(super) fn type_param_predicates( tcx.generics_of(item_def_id).parent.map(|def_id| def_id.expect_local()) }; - let mut result = parent - .map(|parent| { - let icx = ItemCtxt::new(tcx, parent); - icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_name) - }) - .unwrap_or_default(); + let result = if let Some(parent) = parent { + let icx = ItemCtxt::new(tcx, parent); + icx.probe_ty_param_bounds(DUMMY_SP, def_id, assoc_name) + } else { + ty::EarlyBinder::bind(&[] as &[_]) + }; let mut extend = None; let item_hir_id = tcx.local_def_id_to_hir_id(item_def_id); let hir_node = tcx.hir_node(item_hir_id); - let Some(hir_generics) = hir_node.generics() else { return result }; + let Some(hir_generics) = hir_node.generics() else { + return result; + }; if let Node::Item(item) = hir_node && let ItemKind::Trait(..) = item.kind // Implied `Self: Trait` and supertrait bounds. @@ -727,9 +743,10 @@ pub(super) fn type_param_predicates( _ => false, }), ); - result.predicates = - tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(extra_predicates)); - result + + ty::EarlyBinder::bind( + tcx.arena.alloc_from_iter(result.skip_binder().iter().copied().chain(extra_predicates)), + ) } impl<'tcx> ItemCtxt<'tcx> { diff --git a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs index ae0c70d2326..fe46eb583f1 100644 --- a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs +++ b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs @@ -13,7 +13,6 @@ use rustc_ast::visit::walk_list; use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{GenericArg, GenericParam, GenericParamKind, HirId, HirIdMap, LifetimeName, Node}; use rustc_macros::extension; @@ -22,9 +21,10 @@ use rustc_middle::middle::resolve_bound_vars::*; use rustc_middle::query::Providers; use rustc_middle::ty::{self, TyCtxt, TypeSuperVisitable, TypeVisitor}; use rustc_middle::{bug, span_bug}; -use rustc_span::def_id::DefId; +use rustc_span::def_id::{DefId, LocalDefId}; use rustc_span::symbol::{sym, Ident}; use rustc_span::Span; +use tracing::{debug, debug_span, instrument}; use crate::errors; @@ -32,7 +32,7 @@ use crate::errors; impl ResolvedArg { fn early(param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) { debug!("ResolvedArg::early: def_id={:?}", param.def_id); - (param.def_id, ResolvedArg::EarlyBound(param.def_id.to_def_id())) + (param.def_id, ResolvedArg::EarlyBound(param.def_id)) } fn late(idx: u32, param: &GenericParam<'_>) -> (LocalDefId, ResolvedArg) { @@ -41,10 +41,10 @@ impl ResolvedArg { "ResolvedArg::late: idx={:?}, param={:?} depth={:?} def_id={:?}", idx, param, depth, param.def_id, ); - (param.def_id, ResolvedArg::LateBound(depth, idx, param.def_id.to_def_id())) + (param.def_id, ResolvedArg::LateBound(depth, idx, param.def_id)) } - fn id(&self) -> Option<DefId> { + fn id(&self) -> Option<LocalDefId> { match *self { ResolvedArg::StaticLifetime | ResolvedArg::Error(_) => None, @@ -288,13 +288,14 @@ fn late_arg_as_bound_arg<'tcx>( ) -> ty::BoundVariableKind { match arg { ResolvedArg::LateBound(_, _, def_id) => { - let name = tcx.hir().name(tcx.local_def_id_to_hir_id(def_id.expect_local())); + let def_id = def_id.to_def_id(); + let name = tcx.item_name(def_id); match param.kind { GenericParamKind::Lifetime { .. } => { - ty::BoundVariableKind::Region(ty::BrNamed(*def_id, name)) + ty::BoundVariableKind::Region(ty::BrNamed(def_id, name)) } GenericParamKind::Type { .. } => { - ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(*def_id, name)) + ty::BoundVariableKind::Ty(ty::BoundTyKind::Param(def_id, name)) } GenericParamKind::Const { .. } => ty::BoundVariableKind::Const, } @@ -717,7 +718,6 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> { // In the future, this should be fixed and this error should be removed. let def = self.map.defs.get(&lifetime.hir_id).copied(); let Some(ResolvedArg::LateBound(_, _, lifetime_def_id)) = def else { continue }; - let Some(lifetime_def_id) = lifetime_def_id.as_local() else { continue }; let lifetime_hir_id = self.tcx.local_def_id_to_hir_id(lifetime_def_id); let bad_place = match self.tcx.hir_node(self.tcx.parent_hir_id(lifetime_hir_id)) @@ -1150,7 +1150,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { .param_def_id_to_index(self.tcx, region_def_id.to_def_id()) .is_some() { - break Some(ResolvedArg::EarlyBound(region_def_id.to_def_id())); + break Some(ResolvedArg::EarlyBound(region_def_id)); } break None; } @@ -1259,7 +1259,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { kind => span_bug!( use_span, "did not expect to resolve lifetime to {}", - kind.descr(param_def_id) + kind.descr(param_def_id.to_def_id()) ), }; def = ResolvedArg::Error(guar); @@ -1277,10 +1277,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { kind: hir::ImplItemKind::Fn(..), .. }) => { - def = ResolvedArg::Free(owner_id.to_def_id(), def.id().unwrap()); + def = ResolvedArg::Free(owner_id.def_id, def.id().unwrap()); } Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(closure), .. }) => { - def = ResolvedArg::Free(closure.def_id.to_def_id(), def.id().unwrap()); + def = ResolvedArg::Free(closure.def_id, def.id().unwrap()); } _ => {} } @@ -1351,7 +1351,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { .param_def_id_to_index(self.tcx, param_def_id.to_def_id()) .is_some() { - break Some(ResolvedArg::EarlyBound(param_def_id.to_def_id())); + break Some(ResolvedArg::EarlyBound(param_def_id)); } break None; } @@ -1762,7 +1762,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { break Some((bound_vars.into_iter().collect(), assoc_item)); } let predicates = tcx.explicit_supertraits_containing_assoc_item((def_id, assoc_name)); - let obligations = predicates.predicates.iter().filter_map(|&(pred, _)| { + let obligations = predicates.iter_identity_copied().filter_map(|(pred, _)| { let bound_predicate = pred.kind(); match bound_predicate.skip_binder() { ty::ClauseKind::Trait(data) => { @@ -2072,7 +2072,7 @@ fn is_late_bound_map( } } -pub fn deny_non_region_late_bound( +fn deny_non_region_late_bound( tcx: TyCtxt<'_>, bound_vars: &mut FxIndexMap<LocalDefId, ResolvedArg>, where_: &str, diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index 8cb4ba6c669..5cb90e97eef 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -11,6 +11,7 @@ use rustc_middle::ty::{self, Article, IsSuggestable, Ty, TyCtxt, TypeVisitableEx use rustc_middle::{bug, span_bug}; use rustc_span::symbol::Ident; use rustc_span::{Span, DUMMY_SP}; +use tracing::debug; use super::{bad_placeholder, ItemCtxt}; use crate::errors::TypeofReservedKeywordUsed; @@ -725,7 +726,7 @@ fn check_feature_inherent_assoc_ty(tcx: TyCtxt<'_>, span: Span) { } } -pub fn type_alias_is_lazy<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool { +pub(crate) fn type_alias_is_lazy<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool { use hir::intravisit::Visitor; if tcx.features().lazy_type_alias { return true; diff --git a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs index d1048b742a0..7f4a8208faa 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs @@ -7,6 +7,7 @@ use rustc_middle::bug; use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt}; use rustc_span::DUMMY_SP; +use tracing::{debug, instrument, trace}; use crate::errors::{TaitForwardCompat, TaitForwardCompat2, UnconstrainedOpaqueType}; @@ -331,7 +332,7 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>( tcx: TyCtxt<'tcx>, def_id: LocalDefId, owner_def_id: LocalDefId, -) -> Ty<'_> { +) -> Ty<'tcx> { let tables = tcx.typeck(owner_def_id); // Check that all of the opaques we inferred during HIR are compatible. diff --git a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs index 620170164f5..6a9ae0de1c1 100644 --- a/compiler/rustc_hir_analysis/src/constrained_generic_params.rs +++ b/compiler/rustc_hir_analysis/src/constrained_generic_params.rs @@ -4,9 +4,10 @@ use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitor}; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::Span; use rustc_type_ir::fold::TypeFoldable; +use tracing::debug; #[derive(Clone, PartialEq, Eq, Hash, Debug)] -pub struct Parameter(pub u32); +pub(crate) struct Parameter(pub u32); impl From<ty::ParamTy> for Parameter { fn from(param: ty::ParamTy) -> Self { @@ -27,7 +28,7 @@ impl From<ty::ParamConst> for Parameter { } /// Returns the set of parameters constrained by the impl header. -pub fn parameters_for_impl<'tcx>( +pub(crate) fn parameters_for_impl<'tcx>( tcx: TyCtxt<'tcx>, impl_self_ty: Ty<'tcx>, impl_trait_ref: Option<ty::TraitRef<'tcx>>, @@ -44,7 +45,7 @@ pub fn parameters_for_impl<'tcx>( /// uniquely determined by `value` (see RFC 447). If it is true, return the list /// of parameters whose values are needed in order to constrain `value` - these /// differ, with the latter being a superset, in the presence of projections. -pub fn parameters_for<'tcx>( +pub(crate) fn parameters_for<'tcx>( tcx: TyCtxt<'tcx>, value: impl TypeFoldable<TyCtxt<'tcx>>, include_nonconstraining: bool, @@ -102,7 +103,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector { } } -pub fn identify_constrained_generic_params<'tcx>( +pub(crate) fn identify_constrained_generic_params<'tcx>( tcx: TyCtxt<'tcx>, predicates: ty::GenericPredicates<'tcx>, impl_trait_ref: Option<ty::TraitRef<'tcx>>, @@ -156,7 +157,7 @@ pub fn identify_constrained_generic_params<'tcx>( /// which is determined by 1, which requires `U`, that is determined /// by 0. I should probably pick a less tangled example, but I can't /// think of any. -pub fn setup_constraining_predicates<'tcx>( +pub(crate) fn setup_constraining_predicates<'tcx>( tcx: TyCtxt<'tcx>, predicates: &mut [(ty::Clause<'tcx>, Span)], impl_trait_ref: Option<ty::TraitRef<'tcx>>, diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index 7034735aec0..39df18ff658 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -11,15 +11,15 @@ use rustc_span::{Span, Symbol}; use crate::fluent_generated as fluent; mod pattern_types; -pub use pattern_types::*; -pub mod wrong_number_of_generic_args; +pub(crate) use pattern_types::*; +pub(crate) mod wrong_number_of_generic_args; mod precise_captures; pub(crate) use precise_captures::*; #[derive(Diagnostic)] #[diag(hir_analysis_ambiguous_assoc_item)] -pub struct AmbiguousAssocItem<'a> { +pub(crate) struct AmbiguousAssocItem<'a> { #[primary_span] #[label] pub span: Span, @@ -30,7 +30,7 @@ pub struct AmbiguousAssocItem<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_assoc_kind_mismatch)] -pub struct AssocKindMismatch { +pub(crate) struct AssocKindMismatch { #[primary_span] #[label] pub span: Span, @@ -52,7 +52,7 @@ pub struct AssocKindMismatch { hir_analysis_assoc_kind_mismatch_wrap_in_braces_sugg, applicability = "maybe-incorrect" )] -pub struct AssocKindMismatchWrapInBracesSugg { +pub(crate) struct AssocKindMismatchWrapInBracesSugg { #[suggestion_part(code = "{{ ")] pub lo: Span, #[suggestion_part(code = " }}")] @@ -61,7 +61,7 @@ pub struct AssocKindMismatchWrapInBracesSugg { #[derive(Diagnostic)] #[diag(hir_analysis_assoc_item_is_private, code = E0624)] -pub struct AssocItemIsPrivate { +pub(crate) struct AssocItemIsPrivate { #[primary_span] #[label] pub span: Span, @@ -73,7 +73,7 @@ pub struct AssocItemIsPrivate { #[derive(Diagnostic)] #[diag(hir_analysis_assoc_item_not_found, code = E0220)] -pub struct AssocItemNotFound<'a> { +pub(crate) struct AssocItemNotFound<'a> { #[primary_span] pub span: Span, pub assoc_name: Ident, @@ -86,7 +86,7 @@ pub struct AssocItemNotFound<'a> { } #[derive(Subdiagnostic)] -pub enum AssocItemNotFoundLabel<'a> { +pub(crate) enum AssocItemNotFoundLabel<'a> { #[label(hir_analysis_assoc_item_not_found_label)] NotFound { #[primary_span] @@ -105,7 +105,7 @@ pub enum AssocItemNotFoundLabel<'a> { #[derive(Subdiagnostic)] -pub enum AssocItemNotFoundSugg<'a> { +pub(crate) enum AssocItemNotFoundSugg<'a> { #[suggestion( hir_analysis_assoc_item_not_found_similar_sugg, code = "{suggested_name}", @@ -162,7 +162,7 @@ pub enum AssocItemNotFoundSugg<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_unrecognized_atomic_operation, code = E0092)] -pub struct UnrecognizedAtomicOperation<'a> { +pub(crate) struct UnrecognizedAtomicOperation<'a> { #[primary_span] #[label] pub span: Span, @@ -171,7 +171,7 @@ pub struct UnrecognizedAtomicOperation<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_wrong_number_of_generic_arguments_to_intrinsic, code = E0094)] -pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> { +pub(crate) struct WrongNumberOfGenericArgumentsToIntrinsic<'a> { #[primary_span] #[label] pub span: Span, @@ -183,7 +183,7 @@ pub struct WrongNumberOfGenericArgumentsToIntrinsic<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_unrecognized_intrinsic_function, code = E0093)] #[help] -pub struct UnrecognizedIntrinsicFunction { +pub(crate) struct UnrecognizedIntrinsicFunction { #[primary_span] #[label] pub span: Span, @@ -192,7 +192,7 @@ pub struct UnrecognizedIntrinsicFunction { #[derive(Diagnostic)] #[diag(hir_analysis_lifetimes_or_bounds_mismatch_on_trait, code = E0195)] -pub struct LifetimesOrBoundsMismatchOnTrait { +pub(crate) struct LifetimesOrBoundsMismatchOnTrait { #[primary_span] #[label] pub span: Span, @@ -208,14 +208,14 @@ pub struct LifetimesOrBoundsMismatchOnTrait { #[derive(Diagnostic)] #[diag(hir_analysis_drop_impl_on_wrong_item, code = E0120)] -pub struct DropImplOnWrongItem { +pub(crate) struct DropImplOnWrongItem { #[primary_span] #[label] pub span: Span, } #[derive(Diagnostic)] -pub enum FieldAlreadyDeclared { +pub(crate) enum FieldAlreadyDeclared { #[diag(hir_analysis_field_already_declared, code = E0124)] NotNested { field_name: Symbol, @@ -272,14 +272,14 @@ pub enum FieldAlreadyDeclared { #[derive(Subdiagnostic)] #[help(hir_analysis_field_already_declared_nested_help)] -pub struct FieldAlreadyDeclaredNestedHelp { +pub(crate) struct FieldAlreadyDeclaredNestedHelp { #[primary_span] pub span: Span, } #[derive(Diagnostic)] #[diag(hir_analysis_copy_impl_on_type_with_dtor, code = E0184)] -pub struct CopyImplOnTypeWithDtor { +pub(crate) struct CopyImplOnTypeWithDtor { #[primary_span] #[label] pub span: Span, @@ -287,14 +287,14 @@ pub struct CopyImplOnTypeWithDtor { #[derive(Diagnostic)] #[diag(hir_analysis_multiple_relaxed_default_bounds, code = E0203)] -pub struct MultipleRelaxedDefaultBounds { +pub(crate) struct MultipleRelaxedDefaultBounds { #[primary_span] pub spans: Vec<Span>, } #[derive(Diagnostic)] #[diag(hir_analysis_copy_impl_on_non_adt, code = E0206)] -pub struct CopyImplOnNonAdt { +pub(crate) struct CopyImplOnNonAdt { #[primary_span] #[label] pub span: Span, @@ -302,7 +302,7 @@ pub struct CopyImplOnNonAdt { #[derive(Diagnostic)] #[diag(hir_analysis_const_param_ty_impl_on_unsized)] -pub struct ConstParamTyImplOnUnsized { +pub(crate) struct ConstParamTyImplOnUnsized { #[primary_span] #[label] pub span: Span, @@ -310,7 +310,7 @@ pub struct ConstParamTyImplOnUnsized { #[derive(Diagnostic)] #[diag(hir_analysis_const_param_ty_impl_on_non_adt)] -pub struct ConstParamTyImplOnNonAdt { +pub(crate) struct ConstParamTyImplOnNonAdt { #[primary_span] #[label] pub span: Span, @@ -318,7 +318,7 @@ pub struct ConstParamTyImplOnNonAdt { #[derive(Diagnostic)] #[diag(hir_analysis_trait_object_declared_with_no_traits, code = E0224)] -pub struct TraitObjectDeclaredWithNoTraits { +pub(crate) struct TraitObjectDeclaredWithNoTraits { #[primary_span] pub span: Span, #[label(hir_analysis_alias_span)] @@ -327,14 +327,14 @@ pub struct TraitObjectDeclaredWithNoTraits { #[derive(Diagnostic)] #[diag(hir_analysis_ambiguous_lifetime_bound, code = E0227)] -pub struct AmbiguousLifetimeBound { +pub(crate) struct AmbiguousLifetimeBound { #[primary_span] pub span: Span, } #[derive(Diagnostic)] #[diag(hir_analysis_assoc_item_constraints_not_allowed_here, code = E0229)] -pub struct AssocItemConstraintsNotAllowedHere { +pub(crate) struct AssocItemConstraintsNotAllowedHere { #[primary_span] #[label] pub span: Span, @@ -383,7 +383,7 @@ pub(crate) struct EscapingBoundVarInTyOfAssocConstBinding<'tcx> { #[derive(Subdiagnostic)] #[help(hir_analysis_parenthesized_fn_trait_expansion)] -pub struct ParenthesizedFnTraitExpansion { +pub(crate) struct ParenthesizedFnTraitExpansion { #[primary_span] pub span: Span, @@ -392,7 +392,7 @@ pub struct ParenthesizedFnTraitExpansion { #[derive(Diagnostic)] #[diag(hir_analysis_typeof_reserved_keyword_used, code = E0516)] -pub struct TypeofReservedKeywordUsed<'tcx> { +pub(crate) struct TypeofReservedKeywordUsed<'tcx> { pub ty: Ty<'tcx>, #[primary_span] #[label] @@ -403,7 +403,7 @@ pub struct TypeofReservedKeywordUsed<'tcx> { #[derive(Diagnostic)] #[diag(hir_analysis_value_of_associated_struct_already_specified, code = E0719)] -pub struct ValueOfAssociatedStructAlreadySpecified { +pub(crate) struct ValueOfAssociatedStructAlreadySpecified { #[primary_span] #[label] pub span: Span, @@ -416,7 +416,7 @@ pub struct ValueOfAssociatedStructAlreadySpecified { #[derive(Diagnostic)] #[diag(hir_analysis_unconstrained_opaque_type)] #[note] -pub struct UnconstrainedOpaqueType { +pub(crate) struct UnconstrainedOpaqueType { #[primary_span] pub span: Span, pub name: Symbol, @@ -426,7 +426,7 @@ pub struct UnconstrainedOpaqueType { #[derive(Diagnostic)] #[diag(hir_analysis_tait_forward_compat)] #[note] -pub struct TaitForwardCompat { +pub(crate) struct TaitForwardCompat { #[primary_span] pub span: Span, #[note] @@ -436,7 +436,7 @@ pub struct TaitForwardCompat { #[derive(Diagnostic)] #[diag(hir_analysis_tait_forward_compat2)] #[note] -pub struct TaitForwardCompat2 { +pub(crate) struct TaitForwardCompat2 { #[primary_span] pub span: Span, #[note(hir_analysis_opaque)] @@ -444,7 +444,7 @@ pub struct TaitForwardCompat2 { pub opaque_type: String, } -pub struct MissingTypeParams { +pub(crate) struct MissingTypeParams { pub span: Span, pub def_span: Span, pub span_snippet: Option<String>, @@ -512,7 +512,7 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for MissingTypeParams { #[derive(Diagnostic)] #[diag(hir_analysis_manual_implementation, code = E0183)] #[help] -pub struct ManualImplementation { +pub(crate) struct ManualImplementation { #[primary_span] #[label] pub span: Span, @@ -521,14 +521,14 @@ pub struct ManualImplementation { #[derive(Diagnostic)] #[diag(hir_analysis_generic_args_on_overridden_impl)] -pub struct GenericArgsOnOverriddenImpl { +pub(crate) struct GenericArgsOnOverriddenImpl { #[primary_span] pub span: Span, } #[derive(Diagnostic)] #[diag(hir_analysis_const_impl_for_non_const_trait)] -pub struct ConstImplForNonConstTrait { +pub(crate) struct ConstImplForNonConstTrait { #[primary_span] pub trait_ref_span: Span, pub trait_name: String, @@ -542,7 +542,7 @@ pub struct ConstImplForNonConstTrait { #[derive(Diagnostic)] #[diag(hir_analysis_const_bound_for_non_const_trait)] -pub struct ConstBoundForNonConstTrait { +pub(crate) struct ConstBoundForNonConstTrait { #[primary_span] pub span: Span, pub modifier: &'static str, @@ -550,7 +550,7 @@ pub struct ConstBoundForNonConstTrait { #[derive(Diagnostic)] #[diag(hir_analysis_self_in_impl_self)] -pub struct SelfInImplSelf { +pub(crate) struct SelfInImplSelf { #[primary_span] pub span: MultiSpan, #[note] @@ -567,7 +567,7 @@ pub(crate) struct LinkageType { #[derive(Diagnostic)] #[help] #[diag(hir_analysis_auto_deref_reached_recursion_limit, code = E0055)] -pub struct AutoDerefReachedRecursionLimit<'a> { +pub(crate) struct AutoDerefReachedRecursionLimit<'a> { #[primary_span] #[label] pub span: Span, @@ -736,7 +736,7 @@ pub(crate) struct InvalidUnionField { #[derive(Diagnostic)] #[diag(hir_analysis_invalid_unnamed_field_ty)] -pub struct InvalidUnnamedFieldTy { +pub(crate) struct InvalidUnnamedFieldTy { #[primary_span] pub span: Span, } @@ -894,7 +894,7 @@ pub(crate) struct SIMDFFIHighlyExperimental { } #[derive(Diagnostic)] -pub enum ImplNotMarkedDefault { +pub(crate) enum ImplNotMarkedDefault { #[diag(hir_analysis_impl_not_marked_default, code = E0520)] #[note] Ok { @@ -1137,7 +1137,7 @@ pub(crate) enum LateBoundInApit { #[derive(LintDiagnostic)] #[diag(hir_analysis_unused_associated_type_bounds)] #[note] -pub struct UnusedAssociatedTypeBounds { +pub(crate) struct UnusedAssociatedTypeBounds { #[suggestion(code = "")] pub span: Span, } @@ -1162,7 +1162,7 @@ pub(crate) struct ReturnPositionImplTraitInTraitRefined<'tcx> { #[derive(Diagnostic)] #[diag(hir_analysis_inherent_ty_outside, code = E0390)] #[help] -pub struct InherentTyOutside { +pub(crate) struct InherentTyOutside { #[primary_span] #[help(hir_analysis_span_help)] pub span: Span, @@ -1170,7 +1170,7 @@ pub struct InherentTyOutside { #[derive(Diagnostic)] #[diag(hir_analysis_coerce_unsized_may, code = E0378)] -pub struct DispatchFromDynCoercion<'a> { +pub(crate) struct DispatchFromDynCoercion<'a> { #[primary_span] pub span: Span, pub trait_name: &'a str, @@ -1182,7 +1182,7 @@ pub struct DispatchFromDynCoercion<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_dispatch_from_dyn_repr, code = E0378)] -pub struct DispatchFromDynRepr { +pub(crate) struct DispatchFromDynRepr { #[primary_span] pub span: Span, } @@ -1190,7 +1190,7 @@ pub struct DispatchFromDynRepr { #[derive(Diagnostic)] #[diag(hir_analysis_inherent_ty_outside_relevant, code = E0390)] #[help] -pub struct InherentTyOutsideRelevant { +pub(crate) struct InherentTyOutsideRelevant { #[primary_span] pub span: Span, #[help(hir_analysis_span_help)] @@ -1200,7 +1200,7 @@ pub struct InherentTyOutsideRelevant { #[derive(Diagnostic)] #[diag(hir_analysis_inherent_ty_outside_new, code = E0116)] #[note] -pub struct InherentTyOutsideNew { +pub(crate) struct InherentTyOutsideNew { #[primary_span] #[label] pub span: Span, @@ -1209,7 +1209,7 @@ pub struct InherentTyOutsideNew { #[derive(Diagnostic)] #[diag(hir_analysis_inherent_ty_outside_primitive, code = E0390)] #[help] -pub struct InherentTyOutsidePrimitive { +pub(crate) struct InherentTyOutsidePrimitive { #[primary_span] pub span: Span, #[help(hir_analysis_span_help)] @@ -1219,7 +1219,7 @@ pub struct InherentTyOutsidePrimitive { #[derive(Diagnostic)] #[diag(hir_analysis_inherent_primitive_ty, code = E0390)] #[help] -pub struct InherentPrimitiveTy<'a> { +pub(crate) struct InherentPrimitiveTy<'a> { #[primary_span] pub span: Span, #[subdiagnostic] @@ -1228,14 +1228,14 @@ pub struct InherentPrimitiveTy<'a> { #[derive(Subdiagnostic)] #[note(hir_analysis_inherent_primitive_ty_note)] -pub struct InherentPrimitiveTyNote<'a> { +pub(crate) struct InherentPrimitiveTyNote<'a> { pub subty: Ty<'a>, } #[derive(Diagnostic)] #[diag(hir_analysis_inherent_dyn, code = E0785)] #[note] -pub struct InherentDyn { +pub(crate) struct InherentDyn { #[primary_span] #[label] pub span: Span, @@ -1244,7 +1244,7 @@ pub struct InherentDyn { #[derive(Diagnostic)] #[diag(hir_analysis_inherent_nominal, code = E0118)] #[note] -pub struct InherentNominal { +pub(crate) struct InherentNominal { #[primary_span] #[label] pub span: Span, @@ -1253,7 +1253,7 @@ pub struct InherentNominal { #[derive(Diagnostic)] #[diag(hir_analysis_dispatch_from_dyn_zst, code = E0378)] #[note] -pub struct DispatchFromDynZST<'a> { +pub(crate) struct DispatchFromDynZST<'a> { #[primary_span] pub span: Span, pub name: Symbol, @@ -1262,7 +1262,7 @@ pub struct DispatchFromDynZST<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_coerce_unsized_may, code = E0378)] -pub struct DispatchFromDynSingle<'a> { +pub(crate) struct DispatchFromDynSingle<'a> { #[primary_span] pub span: Span, pub trait_name: &'a str, @@ -1273,7 +1273,7 @@ pub struct DispatchFromDynSingle<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_dispatch_from_dyn_multi, code = E0378)] #[note] -pub struct DispatchFromDynMulti { +pub(crate) struct DispatchFromDynMulti { #[primary_span] pub span: Span, #[note(hir_analysis_coercions_note)] @@ -1284,7 +1284,7 @@ pub struct DispatchFromDynMulti { #[derive(Diagnostic)] #[diag(hir_analysis_coerce_unsized_may, code = E0376)] -pub struct DispatchFromDynStruct<'a> { +pub(crate) struct DispatchFromDynStruct<'a> { #[primary_span] pub span: Span, pub trait_name: &'a str, @@ -1292,7 +1292,7 @@ pub struct DispatchFromDynStruct<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_coerce_unsized_may, code = E0377)] -pub struct DispatchFromDynSame<'a> { +pub(crate) struct DispatchFromDynSame<'a> { #[primary_span] pub span: Span, pub trait_name: &'a str, @@ -1304,7 +1304,7 @@ pub struct DispatchFromDynSame<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_coerce_unsized_may, code = E0374)] -pub struct CoerceUnsizedOneField<'a> { +pub(crate) struct CoerceUnsizedOneField<'a> { #[primary_span] pub span: Span, pub trait_name: &'a str, @@ -1315,7 +1315,7 @@ pub struct CoerceUnsizedOneField<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_coerce_unsized_multi, code = E0375)] #[note] -pub struct CoerceUnsizedMulti { +pub(crate) struct CoerceUnsizedMulti { #[primary_span] #[label] pub span: Span, @@ -1327,7 +1327,7 @@ pub struct CoerceUnsizedMulti { #[derive(Diagnostic)] #[diag(hir_analysis_coerce_unsized_may, code = E0378)] -pub struct CoerceUnsizedMay<'a> { +pub(crate) struct CoerceUnsizedMay<'a> { #[primary_span] pub span: Span, pub trait_name: &'a str, @@ -1335,7 +1335,7 @@ pub struct CoerceUnsizedMay<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_trait_cannot_impl_for_ty, code = E0204)] -pub struct TraitCannotImplForTy { +pub(crate) struct TraitCannotImplForTy { #[primary_span] pub span: Span, pub trait_name: String, @@ -1347,7 +1347,7 @@ pub struct TraitCannotImplForTy { #[derive(Subdiagnostic)] #[note(hir_analysis_requires_note)] -pub struct ImplForTyRequires { +pub(crate) struct ImplForTyRequires { #[primary_span] pub span: MultiSpan, pub error_predicate: String, @@ -1358,7 +1358,7 @@ pub struct ImplForTyRequires { #[derive(Diagnostic)] #[diag(hir_analysis_traits_with_defualt_impl, code = E0321)] #[note] -pub struct TraitsWithDefaultImpl<'a> { +pub(crate) struct TraitsWithDefaultImpl<'a> { #[primary_span] pub span: Span, pub traits: String, @@ -1368,7 +1368,7 @@ pub struct TraitsWithDefaultImpl<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_cross_crate_traits, code = E0321)] -pub struct CrossCrateTraits<'a> { +pub(crate) struct CrossCrateTraits<'a> { #[primary_span] #[label] pub span: Span, @@ -1378,7 +1378,7 @@ pub struct CrossCrateTraits<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_cross_crate_traits_defined, code = E0321)] -pub struct CrossCrateTraitsDefined { +pub(crate) struct CrossCrateTraitsDefined { #[primary_span] #[label] pub span: Span, @@ -1390,7 +1390,7 @@ pub struct CrossCrateTraitsDefined { #[derive(Diagnostic)] #[diag(hir_analysis_ty_param_first_local, code = E0210)] #[note] -pub struct TyParamFirstLocal<'tcx> { +pub(crate) struct TyParamFirstLocal<'tcx> { #[primary_span] #[label] pub span: Span, @@ -1403,7 +1403,7 @@ pub struct TyParamFirstLocal<'tcx> { #[derive(LintDiagnostic)] #[diag(hir_analysis_ty_param_first_local, code = E0210)] #[note] -pub struct TyParamFirstLocalLint<'tcx> { +pub(crate) struct TyParamFirstLocalLint<'tcx> { #[label] pub span: Span, #[note(hir_analysis_case_note)] @@ -1415,7 +1415,7 @@ pub struct TyParamFirstLocalLint<'tcx> { #[derive(Diagnostic)] #[diag(hir_analysis_ty_param_some, code = E0210)] #[note] -pub struct TyParamSome { +pub(crate) struct TyParamSome { #[primary_span] #[label] pub span: Span, @@ -1427,7 +1427,7 @@ pub struct TyParamSome { #[derive(LintDiagnostic)] #[diag(hir_analysis_ty_param_some, code = E0210)] #[note] -pub struct TyParamSomeLint { +pub(crate) struct TyParamSomeLint { #[label] pub span: Span, #[note(hir_analysis_only_note)] @@ -1436,7 +1436,7 @@ pub struct TyParamSomeLint { } #[derive(Diagnostic)] -pub enum OnlyCurrentTraits { +pub(crate) enum OnlyCurrentTraits { #[diag(hir_analysis_only_current_traits_outside, code = E0117)] Outside { #[primary_span] @@ -1465,20 +1465,20 @@ pub enum OnlyCurrentTraits { #[derive(Subdiagnostic)] #[label(hir_analysis_only_current_traits_opaque)] -pub struct OnlyCurrentTraitsOpaque { +pub(crate) struct OnlyCurrentTraitsOpaque { #[primary_span] pub span: Span, } #[derive(Subdiagnostic)] #[label(hir_analysis_only_current_traits_foreign)] -pub struct OnlyCurrentTraitsForeign { +pub(crate) struct OnlyCurrentTraitsForeign { #[primary_span] pub span: Span, } #[derive(Subdiagnostic)] #[label(hir_analysis_only_current_traits_name)] -pub struct OnlyCurrentTraitsName<'a> { +pub(crate) struct OnlyCurrentTraitsName<'a> { #[primary_span] pub span: Span, pub name: &'a str, @@ -1486,7 +1486,7 @@ pub struct OnlyCurrentTraitsName<'a> { #[derive(Subdiagnostic)] #[label(hir_analysis_only_current_traits_pointer)] -pub struct OnlyCurrentTraitsPointer<'a> { +pub(crate) struct OnlyCurrentTraitsPointer<'a> { #[primary_span] pub span: Span, pub pointer: Ty<'a>, @@ -1494,7 +1494,7 @@ pub struct OnlyCurrentTraitsPointer<'a> { #[derive(Subdiagnostic)] #[label(hir_analysis_only_current_traits_ty)] -pub struct OnlyCurrentTraitsTy<'a> { +pub(crate) struct OnlyCurrentTraitsTy<'a> { #[primary_span] pub span: Span, pub ty: Ty<'a>, @@ -1502,7 +1502,7 @@ pub struct OnlyCurrentTraitsTy<'a> { #[derive(Subdiagnostic)] #[label(hir_analysis_only_current_traits_adt)] -pub struct OnlyCurrentTraitsAdt { +pub(crate) struct OnlyCurrentTraitsAdt { #[primary_span] pub span: Span, pub name: String, @@ -1513,11 +1513,11 @@ pub struct OnlyCurrentTraitsAdt { hir_analysis_only_current_traits_pointer_sugg, applicability = "maybe-incorrect" )] -pub struct OnlyCurrentTraitsPointerSugg<'a> { +pub(crate) struct OnlyCurrentTraitsPointerSugg<'a> { #[suggestion_part(code = "WrapperType")] pub wrapper_span: Span, #[suggestion_part(code = "struct WrapperType(*{mut_key}{ptr_ty});\n\n")] - pub struct_span: Span, + pub(crate) struct_span: Span, pub mut_key: &'a str, pub ptr_ty: Ty<'a>, } @@ -1525,7 +1525,7 @@ pub struct OnlyCurrentTraitsPointerSugg<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_static_mut_ref, code = E0796)] #[note] -pub struct StaticMutRef<'a> { +pub(crate) struct StaticMutRef<'a> { #[primary_span] #[label] pub span: Span, @@ -1535,7 +1535,7 @@ pub struct StaticMutRef<'a> { } #[derive(Subdiagnostic)] -pub enum MutRefSugg { +pub(crate) enum MutRefSugg { #[multipart_suggestion( hir_analysis_suggestion, style = "verbose", @@ -1565,7 +1565,7 @@ pub enum MutRefSugg { #[diag(hir_analysis_static_mut_refs_lint)] #[note] #[note(hir_analysis_why_note)] -pub struct RefOfMutStatic<'a> { +pub(crate) struct RefOfMutStatic<'a> { #[label] pub span: Span, #[subdiagnostic] @@ -1575,7 +1575,7 @@ pub struct RefOfMutStatic<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_not_supported_delegation)] -pub struct UnsupportedDelegation<'a> { +pub(crate) struct UnsupportedDelegation<'a> { #[primary_span] pub span: Span, pub descr: &'a str, @@ -1585,7 +1585,7 @@ pub struct UnsupportedDelegation<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_method_should_return_future)] -pub struct MethodShouldReturnFuture { +pub(crate) struct MethodShouldReturnFuture { #[primary_span] pub span: Span, pub method_name: Symbol, @@ -1606,7 +1606,7 @@ pub(crate) struct UnusedGenericParameter { #[subdiagnostic] pub help: UnusedGenericParameterHelp, #[help(hir_analysis_const_param_help)] - pub const_param_help: Option<()>, + pub const_param_help: bool, } #[derive(Diagnostic)] @@ -1643,13 +1643,13 @@ pub(crate) struct UnconstrainedGenericParameter { pub param_name: Symbol, pub param_def_kind: &'static str, #[note(hir_analysis_const_param_note)] - pub const_param_note: Option<()>, + pub const_param_note: bool, #[note(hir_analysis_const_param_note2)] - pub const_param_note2: Option<()>, + pub const_param_note2: bool, } #[derive(Diagnostic)] -pub enum UnnamedFieldsRepr<'a> { +pub(crate) enum UnnamedFieldsRepr<'a> { #[diag(hir_analysis_unnamed_fields_repr_missing_repr_c)] MissingReprC { #[primary_span] @@ -1678,14 +1678,14 @@ pub enum UnnamedFieldsRepr<'a> { #[derive(Subdiagnostic)] #[note(hir_analysis_unnamed_fields_repr_field_defined)] -pub struct UnnamedFieldsReprFieldDefined { +pub(crate) struct UnnamedFieldsReprFieldDefined { #[primary_span] pub span: Span, } #[derive(Diagnostic)] #[diag(hir_analysis_opaque_captures_higher_ranked_lifetime, code = E0657)] -pub struct OpaqueCapturesHigherRankedLifetime { +pub(crate) struct OpaqueCapturesHigherRankedLifetime { #[primary_span] pub span: Span, #[label] @@ -1697,7 +1697,7 @@ pub struct OpaqueCapturesHigherRankedLifetime { #[derive(Diagnostic)] #[diag(hir_analysis_pattern_type_non_const_range)] -pub struct NonConstRange { +pub(crate) struct NonConstRange { #[primary_span] pub span: Span, } @@ -1706,7 +1706,7 @@ pub struct NonConstRange { #[diag(hir_analysis_invalid_receiver_ty, code = E0307)] #[note] #[help(hir_analysis_invalid_receiver_ty_help)] -pub struct InvalidReceiverTy<'tcx> { +pub(crate) struct InvalidReceiverTy<'tcx> { #[primary_span] pub span: Span, pub receiver_ty: Ty<'tcx>, @@ -1716,12 +1716,12 @@ pub struct InvalidReceiverTy<'tcx> { #[diag(hir_analysis_effects_without_next_solver)] #[note] #[help] -pub struct EffectsWithoutNextSolver; +pub(crate) struct EffectsWithoutNextSolver; #[derive(Diagnostic)] #[diag(hir_analysis_cmse_call_inputs_stack_spill, code = E0798)] #[note] -pub struct CmseCallInputsStackSpill { +pub(crate) struct CmseCallInputsStackSpill { #[primary_span] #[label] pub span: Span, @@ -1732,7 +1732,7 @@ pub struct CmseCallInputsStackSpill { #[diag(hir_analysis_cmse_call_output_stack_spill, code = E0798)] #[note(hir_analysis_note1)] #[note(hir_analysis_note2)] -pub struct CmseCallOutputStackSpill { +pub(crate) struct CmseCallOutputStackSpill { #[primary_span] #[label] pub span: Span, @@ -1740,7 +1740,7 @@ pub struct CmseCallOutputStackSpill { #[derive(Diagnostic)] #[diag(hir_analysis_cmse_call_generic, code = E0798)] -pub struct CmseCallGeneric { +pub(crate) struct CmseCallGeneric { #[primary_span] pub span: Span, } diff --git a/compiler/rustc_hir_analysis/src/errors/pattern_types.rs b/compiler/rustc_hir_analysis/src/errors/pattern_types.rs index 008d2698989..bb771d6ea17 100644 --- a/compiler/rustc_hir_analysis/src/errors/pattern_types.rs +++ b/compiler/rustc_hir_analysis/src/errors/pattern_types.rs @@ -3,7 +3,7 @@ use rustc_span::Span; #[derive(Diagnostic)] #[diag(hir_analysis_pattern_type_wild_pat)] -pub struct WildPatTy { +pub(crate) struct WildPatTy { #[primary_span] pub span: Span, } diff --git a/compiler/rustc_hir_analysis/src/errors/precise_captures.rs b/compiler/rustc_hir_analysis/src/errors/precise_captures.rs index 8a9b5fe6369..af2bb053c0a 100644 --- a/compiler/rustc_hir_analysis/src/errors/precise_captures.rs +++ b/compiler/rustc_hir_analysis/src/errors/precise_captures.rs @@ -4,7 +4,7 @@ use rustc_span::{Span, Symbol}; #[derive(Diagnostic)] #[diag(hir_analysis_param_not_captured)] #[note] -pub struct ParamNotCaptured { +pub(crate) struct ParamNotCaptured { #[primary_span] pub opaque_span: Span, #[label] @@ -15,7 +15,7 @@ pub struct ParamNotCaptured { #[derive(Diagnostic)] #[diag(hir_analysis_self_ty_not_captured)] #[note] -pub struct SelfTyNotCaptured { +pub(crate) struct SelfTyNotCaptured { #[primary_span] pub opaque_span: Span, #[label] @@ -24,7 +24,7 @@ pub struct SelfTyNotCaptured { #[derive(Diagnostic)] #[diag(hir_analysis_lifetime_not_captured)] -pub struct LifetimeNotCaptured { +pub(crate) struct LifetimeNotCaptured { #[primary_span] pub use_span: Span, #[label(hir_analysis_param_label)] @@ -35,7 +35,7 @@ pub struct LifetimeNotCaptured { #[derive(Diagnostic)] #[diag(hir_analysis_bad_precise_capture)] -pub struct BadPreciseCapture { +pub(crate) struct BadPreciseCapture { #[primary_span] pub span: Span, pub kind: &'static str, @@ -44,7 +44,7 @@ pub struct BadPreciseCapture { #[derive(Diagnostic)] #[diag(hir_analysis_precise_capture_self_alias)] -pub struct PreciseCaptureSelfAlias { +pub(crate) struct PreciseCaptureSelfAlias { #[primary_span] pub span: Span, #[label] @@ -54,7 +54,7 @@ pub struct PreciseCaptureSelfAlias { #[derive(Diagnostic)] #[diag(hir_analysis_duplicate_precise_capture)] -pub struct DuplicatePreciseCapture { +pub(crate) struct DuplicatePreciseCapture { #[primary_span] pub first_span: Span, pub name: Symbol, @@ -64,7 +64,7 @@ pub struct DuplicatePreciseCapture { #[derive(Diagnostic)] #[diag(hir_analysis_lifetime_must_be_first)] -pub struct LifetimesMustBeFirst { +pub(crate) struct LifetimesMustBeFirst { #[primary_span] pub lifetime_span: Span, pub name: Symbol, diff --git a/compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs b/compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs index 8ecf53bfacb..da89f5769d1 100644 --- a/compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs +++ b/compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs @@ -5,10 +5,11 @@ use rustc_errors::{pluralize, Applicability, Diag, Diagnostic, EmissionGuarantee use rustc_hir as hir; use rustc_middle::ty::{self as ty, AssocItems, AssocKind, TyCtxt}; use rustc_span::def_id::DefId; +use tracing::debug; use GenericArgsInfo::*; /// Handles the `wrong number of type / lifetime / ... arguments` family of error messages. -pub struct WrongNumberOfGenericArgs<'a, 'tcx> { +pub(crate) struct WrongNumberOfGenericArgs<'a, 'tcx> { pub(crate) tcx: TyCtxt<'tcx>, pub(crate) angle_brackets: AngleBrackets, @@ -49,7 +50,7 @@ pub(crate) enum AngleBrackets { // Information about the kind of arguments that are either missing or are unexpected #[derive(Debug)] -pub enum GenericArgsInfo { +pub(crate) enum GenericArgsInfo { MissingLifetimes { num_missing_args: usize, }, @@ -87,7 +88,7 @@ pub enum GenericArgsInfo { } impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { - pub fn new( + pub(crate) fn new( tcx: TyCtxt<'tcx>, gen_args_info: GenericArgsInfo, path_segment: &'a hir::PathSegment<'_>, diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs index 7f4c75d3a6a..bffe68f9b74 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs @@ -13,6 +13,7 @@ use rustc_span::{sym, ErrorGuaranteed, Span, Symbol}; use rustc_trait_selection::traits; use rustc_type_ir::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor}; use smallvec::SmallVec; +use tracing::{debug, instrument}; use crate::bounds::Bounds; use crate::errors; @@ -528,7 +529,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { /// Detect and reject early-bound & escaping late-bound generic params in the type of assoc const bindings. /// -/// FIXME(const_generics): This is a temporary and semi-artifical restriction until the +/// FIXME(const_generics): This is a temporary and semi-artificial restriction until the /// arrival of *generic const generics*[^1]. /// /// It might actually be possible that we can already support early-bound generic params diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs index e99717ce00f..1b73cecd666 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs @@ -11,7 +11,7 @@ use crate::errors; /// Check conditions on inputs and outputs that the cmse ABIs impose: arguments and results MUST be /// returned via registers (i.e. MUST NOT spill to the stack). LLVM will also validate these /// conditions, but by checking them here rustc can emit nicer error messages. -pub fn validate_cmse_abi<'tcx>( +pub(crate) fn validate_cmse_abi<'tcx>( tcx: TyCtxt<'tcx>, dcx: DiagCtxtHandle<'_>, hir_id: HirId, diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index d77cbe30536..af319fd53bd 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -608,7 +608,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| { let msg = format!("`{}`", if obligation.len() > 50 { quiet } else { obligation }); - match &self_ty.kind() { + match self_ty.kind() { // Point at the type that couldn't satisfy the bound. ty::Adt(def, _) => { bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs index a59e9aa85fd..8d5f98c7372 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs @@ -11,6 +11,7 @@ use rustc_middle::ty::{ use rustc_session::lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS; use rustc_span::symbol::{kw, sym}; use smallvec::SmallVec; +use tracing::{debug, instrument}; use super::{HirTyLowerer, IsMethodCall}; use crate::errors::wrong_number_of_generic_args::{GenericArgsInfo, WrongNumberOfGenericArgs}; diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs index 6aff518390f..149bc6d2698 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs @@ -15,11 +15,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { /// /// *Bare* trait object types are ones that aren't preceded by the keyword `dyn`. /// In edition 2021 and onward we emit a hard error for them. - pub(super) fn prohibit_or_lint_bare_trait_object_ty( - &self, - self_ty: &hir::Ty<'_>, - in_path: bool, - ) { + pub(super) fn prohibit_or_lint_bare_trait_object_ty(&self, self_ty: &hir::Ty<'_>) { let tcx = self.tcx(); let hir::TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) = @@ -28,6 +24,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { return; }; + let in_path = match tcx.parent_hir_node(self_ty.hir_id) { + hir::Node::Ty(hir::Ty { + kind: hir::TyKind::Path(hir::QPath::TypeRelative(qself, _)), + .. + }) + | hir::Node::Expr(hir::Expr { + kind: hir::ExprKind::Path(hir::QPath::TypeRelative(qself, _)), + .. + }) + | hir::Node::Pat(hir::Pat { + kind: hir::PatKind::Path(hir::QPath::TypeRelative(qself, _)), + .. + }) if qself.hir_id == self_ty.hir_id => true, + _ => false, + }; let needs_bracket = in_path && !tcx .sess @@ -122,9 +133,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { return; }; let sugg = self.add_generic_param_suggestion(generics, self_ty.span, &impl_trait_name); - if sugg.is_empty() { - return; - }; diag.multipart_suggestion( format!( "alternatively use a blanket implementation to implement `{of_trait_name}` for \ @@ -159,6 +167,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let parent_id = tcx.hir().get_parent_item(self_ty.hir_id).def_id; // FIXME: If `type_alias_impl_trait` is enabled, also look for `Trait0<Ty = Trait1>` // and suggest `Trait0<Ty = impl Trait1>`. + // Functions are found in three different contexts. + // 1. Independent functions + // 2. Functions inside trait blocks + // 3. Functions inside impl blocks let (sig, generics, owner) = match tcx.hir_node_by_def_id(parent_id) { hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, generics, _), .. }) => { (sig, generics, None) @@ -169,6 +181,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { owner_id, .. }) => (sig, generics, Some(tcx.parent(owner_id.to_def_id()))), + hir::Node::ImplItem(hir::ImplItem { + kind: hir::ImplItemKind::Fn(sig, _), + generics, + owner_id, + .. + }) => (sig, generics, Some(tcx.parent(owner_id.to_def_id()))), _ => return false, }; let Ok(trait_name) = tcx.sess.source_map().span_to_snippet(self_ty.span) else { @@ -176,6 +194,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { }; let impl_sugg = vec![(self_ty.span.shrink_to_lo(), "impl ".to_string())]; let mut is_downgradable = true; + + // Check if trait object is safe for suggesting dynamic dispatch. let is_object_safe = match self_ty.kind { hir::TyKind::TraitObject(objects, ..) => { objects.iter().all(|(o, _)| match o.trait_ref.path.res { @@ -191,8 +211,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } _ => false, }; + + let borrowed = matches!( + tcx.parent_hir_node(self_ty.hir_id), + hir::Node::Ty(hir::Ty { kind: hir::TyKind::Ref(..), .. }) + ); + + // Suggestions for function return type. if let hir::FnRetTy::Return(ty) = sig.decl.output - && ty.hir_id == self_ty.hir_id + && ty.peel_refs().hir_id == self_ty.hir_id { let pre = if !is_object_safe { format!("`{trait_name}` is not object safe, ") @@ -203,14 +230,26 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { "{pre}use `impl {trait_name}` to return an opaque type, as long as you return a \ single underlying type", ); + diag.multipart_suggestion_verbose(msg, impl_sugg, Applicability::MachineApplicable); + + // Suggest `Box<dyn Trait>` for return type if is_object_safe { - diag.multipart_suggestion_verbose( - "alternatively, you can return an owned trait object", + // If the return type is `&Trait`, we don't want + // the ampersand to be displayed in the `Box<dyn Trait>` + // suggestion. + let suggestion = if borrowed { + vec![(ty.span, format!("Box<dyn {trait_name}>"))] + } else { vec![ (ty.span.shrink_to_lo(), "Box<dyn ".to_string()), (ty.span.shrink_to_hi(), ">".to_string()), - ], + ] + }; + + diag.multipart_suggestion_verbose( + "alternatively, you can return an owned trait object", + suggestion, Applicability::MachineApplicable, ); } else if is_downgradable { @@ -219,24 +258,24 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } return true; } + + // Suggestions for function parameters. for ty in sig.decl.inputs { - if ty.hir_id != self_ty.hir_id { + if ty.peel_refs().hir_id != self_ty.hir_id { continue; } let sugg = self.add_generic_param_suggestion(generics, self_ty.span, &trait_name); - if !sugg.is_empty() { - diag.multipart_suggestion_verbose( - format!("use a new generic type parameter, constrained by `{trait_name}`"), - sugg, - Applicability::MachineApplicable, - ); - diag.multipart_suggestion_verbose( - "you can also use an opaque type, but users won't be able to specify the type \ - parameter when calling the `fn`, having to rely exclusively on type inference", - impl_sugg, - Applicability::MachineApplicable, - ); - } + diag.multipart_suggestion_verbose( + format!("use a new generic type parameter, constrained by `{trait_name}`"), + sugg, + Applicability::MachineApplicable, + ); + diag.multipart_suggestion_verbose( + "you can also use an opaque type, but users won't be able to specify the type \ + parameter when calling the `fn`, having to rely exclusively on type inference", + impl_sugg, + Applicability::MachineApplicable, + ); if !is_object_safe { diag.note(format!("`{trait_name}` it is not object safe, so it can't be `dyn`")); if is_downgradable { @@ -244,14 +283,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { diag.downgrade_to_delayed_bug(); } } else { + // No ampersand in suggestion if it's borrowed already + let (dyn_str, paren_dyn_str) = + if borrowed { ("dyn ", "(dyn ") } else { ("&dyn ", "&(dyn ") }; + let sugg = if let hir::TyKind::TraitObject([_, _, ..], _, _) = self_ty.kind { // There are more than one trait bound, we need surrounding parentheses. vec![ - (self_ty.span.shrink_to_lo(), "&(dyn ".to_string()), + (self_ty.span.shrink_to_lo(), paren_dyn_str.to_string()), (self_ty.span.shrink_to_hi(), ")".to_string()), ] } else { - vec![(self_ty.span.shrink_to_lo(), "&dyn ".to_string())] + vec![(self_ty.span.shrink_to_lo(), dyn_str.to_string())] }; diag.multipart_suggestion_verbose( format!( diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index d865357b829..ac5bd825b18 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -50,6 +50,7 @@ use rustc_target::spec::abi; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::wf::object_region_bounds; use rustc_trait_selection::traits::{self, ObligationCtxt}; +use tracing::{debug, debug_span, instrument}; use crate::bounds::Bounds; use crate::errors::{AmbiguousLifetimeBound, WildPatTy}; @@ -85,10 +86,9 @@ pub enum PredicateFilter { #[derive(Debug)] pub enum RegionInferReason<'a> { - /// Lifetime on a trait object behind a reference. - /// This allows inferring information from the reference. - BorrowedObjectLifetimeDefault, - /// A trait object's lifetime. + /// Lifetime on a trait object that is spelled explicitly, e.g. `+ 'a` or `+ '_`. + ExplicitObjectLifetime, + /// A trait object's lifetime when it is elided, e.g. `dyn Any`. ObjectLifetimeDefault, /// Generic lifetime parameter Param(&'a ty::GenericParamDef), @@ -137,7 +137,7 @@ pub trait HirTyLowerer<'tcx> { span: Span, def_id: LocalDefId, assoc_name: Ident, - ) -> ty::GenericPredicates<'tcx>; + ) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]>; /// Lower an associated type to a projection. /// @@ -296,25 +296,29 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { Some(rbv::ResolvedArg::StaticLifetime) => tcx.lifetimes.re_static, Some(rbv::ResolvedArg::LateBound(debruijn, index, def_id)) => { - let name = lifetime_name(def_id.expect_local()); + let name = lifetime_name(def_id); let br = ty::BoundRegion { var: ty::BoundVar::from_u32(index), - kind: ty::BrNamed(def_id, name), + kind: ty::BrNamed(def_id.to_def_id(), name), }; ty::Region::new_bound(tcx, debruijn, br) } Some(rbv::ResolvedArg::EarlyBound(def_id)) => { - let name = tcx.hir().ty_param_name(def_id.expect_local()); - let item_def_id = tcx.hir().ty_param_owner(def_id.expect_local()); + let name = tcx.hir().ty_param_name(def_id); + let item_def_id = tcx.hir().ty_param_owner(def_id); let generics = tcx.generics_of(item_def_id); - let index = generics.param_def_id_to_index[&def_id]; + let index = generics.param_def_id_to_index[&def_id.to_def_id()]; ty::Region::new_early_param(tcx, ty::EarlyParamRegion { index, name }) } Some(rbv::ResolvedArg::Free(scope, id)) => { - let name = lifetime_name(id.expect_local()); - ty::Region::new_late_param(tcx, scope, ty::BrNamed(id, name)) + let name = lifetime_name(id); + ty::Region::new_late_param( + tcx, + scope.to_def_id(), + ty::BrNamed(id.to_def_id(), name), + ) // (*) -- not late-bound, won't change } @@ -828,13 +832,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { debug!(?ty_param_def_id, ?assoc_name, ?span); let tcx = self.tcx(); - let predicates = &self.probe_ty_param_bounds(span, ty_param_def_id, assoc_name).predicates; + let predicates = &self.probe_ty_param_bounds(span, ty_param_def_id, assoc_name); debug!("predicates={:#?}", predicates); self.probe_single_bound_for_assoc_item( || { let trait_refs = predicates - .iter() + .iter_identity_copied() .filter_map(|(p, _)| Some(p.as_trait_clause()?.map_bound(|t| t.trait_ref))); traits::transitive_bounds_that_define_assoc_item(tcx, trait_refs, assoc_name) }, @@ -959,11 +963,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { \n where\n T: {qself_str},\n{}", where_bounds.join(",\n"), )); - } - let reported = err.emit(); - if !where_bounds.is_empty() { + let reported = err.emit(); return Err(reported); } + err.emit(); } Ok(bound) @@ -983,7 +986,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { /// We **don't** support paths whose self type is an arbitrary type like `Struct::Ty` where /// struct `Struct` impls an in-scope trait that defines an associated type called `Ty`. /// For the latter case, we report ambiguity. - /// While desirable to support, the implemention would be non-trivial. Tracked in [#22519]. + /// While desirable to support, the implementation would be non-trivial. Tracked in [#22519]. /// /// At the time of writing, *inherent associated types* are also resolved here. This however /// is [problematic][iat]. A proper implementation would be as non-trivial as the one @@ -1054,7 +1057,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { // Find the type of the associated item, and the trait where the associated // item is declared. - let bound = match (&qself_ty.kind(), qself_res) { + let bound = match (qself_ty.kind(), qself_res) { (_, Res::SelfTyAlias { alias_to: impl_def_id, is_trait_impl: true, .. }) => { // `Self` in an impl of a trait -- we have a concrete self type and a // trait reference. @@ -1954,15 +1957,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let tcx = self.tcx(); match tcx.named_bound_var(hir_id) { Some(rbv::ResolvedArg::LateBound(debruijn, index, def_id)) => { - let name = tcx.item_name(def_id); + let name = tcx.item_name(def_id.to_def_id()); let br = ty::BoundTy { var: ty::BoundVar::from_u32(index), - kind: ty::BoundTyKind::Param(def_id, name), + kind: ty::BoundTyKind::Param(def_id.to_def_id(), name), }; Ty::new_bound(tcx, debruijn, br) } Some(rbv::ResolvedArg::EarlyBound(def_id)) => { - let def_id = def_id.expect_local(); let item_def_id = tcx.hir().ty_param_owner(def_id); let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id.to_def_id()]; @@ -1983,10 +1985,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { Some(rbv::ResolvedArg::EarlyBound(def_id)) => { // Find the name and index of the const parameter by indexing the generics of // the parent item and construct a `ParamConst`. - let item_def_id = tcx.parent(def_id); + let item_def_id = tcx.local_parent(def_id); let generics = tcx.generics_of(item_def_id); - let index = generics.param_def_id_to_index[&def_id]; - let name = tcx.item_name(def_id); + let index = generics.param_def_id_to_index[&def_id.to_def_id()]; + let name = tcx.item_name(def_id.to_def_id()); ty::Const::new_param(tcx, ty::ParamConst::new(index, name)) } Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => { @@ -1997,16 +1999,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } } - /// Lower a type from the HIR to our internal notion of a type. - pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> { - self.lower_ty_common(hir_ty, false, false) - } - - /// Lower a type inside of a path from the HIR to our internal notion of a type. - pub fn lower_ty_in_path(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> { - self.lower_ty_common(hir_ty, false, true) - } - fn lower_delegation_ty(&self, idx: hir::InferDelegationKind) -> Ty<'tcx> { let delegation_sig = self.tcx().inherit_sig_for_delegation_item(self.item_def_id()); match idx { @@ -2024,7 +2016,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { /// 2. `in_path`: Whether the type appears inside of a path. /// Used to provide correct diagnostics for bare trait object types. #[instrument(level = "debug", skip(self), ret)] - fn lower_ty_common(&self, hir_ty: &hir::Ty<'tcx>, borrowed: bool, in_path: bool) -> Ty<'tcx> { + pub fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> { let tcx = self.tcx(); let result_ty = match &hir_ty.kind { @@ -2034,7 +2026,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { hir::TyKind::Ref(region, mt) => { let r = self.lower_lifetime(region, RegionInferReason::Reference); debug!(?r); - let t = self.lower_ty_common(mt.ty, true, false); + let t = self.lower_ty(mt.ty); Ty::new_ref(tcx, r, t, mt.mutbl) } hir::TyKind::Never => tcx.types.never, @@ -2063,20 +2055,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ) } hir::TyKind::TraitObject(bounds, lifetime, repr) => { - self.prohibit_or_lint_bare_trait_object_ty(hir_ty, in_path); + self.prohibit_or_lint_bare_trait_object_ty(hir_ty); let repr = match repr { TraitObjectSyntax::Dyn | TraitObjectSyntax::None => ty::Dyn, TraitObjectSyntax::DynStar => ty::DynStar, }; - self.lower_trait_object_ty( - hir_ty.span, - hir_ty.hir_id, - bounds, - lifetime, - borrowed, - repr, - ) + self.lower_trait_object_ty(hir_ty.span, hir_ty.hir_id, bounds, lifetime, repr) } hir::TyKind::Path(hir::QPath::Resolved(maybe_qself, path)) => { debug!(?maybe_qself, ?path); @@ -2104,7 +2089,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } hir::TyKind::Path(hir::QPath::TypeRelative(qself, segment)) => { debug!(?qself, ?segment); - let ty = self.lower_ty_common(qself, false, true); + let ty = self.lower_ty(qself); self.lower_assoc_path(hir_ty.hir_id, hir_ty.span, ty, qself, segment, false) .map(|(ty, _, _)| ty) .unwrap_or_else(|guar| Ty::new_error(tcx, guar)) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs index 31d1750f33d..88538863710 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/object_safety.rs @@ -14,6 +14,7 @@ use rustc_span::{ErrorGuaranteed, Span}; use rustc_trait_selection::error_reporting::traits::report_object_safety_error; use rustc_trait_selection::traits::{self, hir_ty_lowering_object_safety_violations}; use smallvec::{smallvec, SmallVec}; +use tracing::{debug, instrument}; use super::HirTyLowerer; use crate::bounds::Bounds; @@ -30,7 +31,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { hir_id: hir::HirId, hir_trait_bounds: &[(hir::PolyTraitRef<'tcx>, hir::TraitBoundModifier)], lifetime: &hir::Lifetime, - borrowed: bool, representation: DynKind, ) -> Ty<'tcx> { let tcx = self.tcx(); @@ -325,22 +325,32 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { v.dedup(); let existential_predicates = tcx.mk_poly_existential_predicates(&v); - // Use explicitly-specified region bound. + // Use explicitly-specified region bound, unless the bound is missing. let region_bound = if !lifetime.is_elided() { - self.lower_lifetime(lifetime, RegionInferReason::ObjectLifetimeDefault) + self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime) } else { self.compute_object_lifetime_bound(span, existential_predicates).unwrap_or_else(|| { + // Curiously, we prefer object lifetime default for `+ '_`... if tcx.named_bound_var(lifetime.hir_id).is_some() { - self.lower_lifetime(lifetime, RegionInferReason::ObjectLifetimeDefault) + self.lower_lifetime(lifetime, RegionInferReason::ExplicitObjectLifetime) } else { - self.re_infer( - span, - if borrowed { - RegionInferReason::ObjectLifetimeDefault + let reason = + if let hir::LifetimeName::ImplicitObjectLifetimeDefault = lifetime.res { + if let hir::Node::Ty(hir::Ty { + kind: hir::TyKind::Ref(parent_lifetime, _), + .. + }) = tcx.parent_hir_node(hir_id) + && tcx.named_bound_var(parent_lifetime.hir_id).is_none() + { + // Parent lifetime must have failed to resolve. Don't emit a redundant error. + RegionInferReason::ExplicitObjectLifetime + } else { + RegionInferReason::ObjectLifetimeDefault + } } else { - RegionInferReason::BorrowedObjectLifetimeDefault - }, - ) + RegionInferReason::ExplicitObjectLifetime + }; + self.re_infer(span, reason) } }) }; diff --git a/compiler/rustc_hir_analysis/src/hir_wf_check.rs b/compiler/rustc_hir_analysis/src/hir_wf_check.rs index 7d2cabd3f2c..2fa4ca68073 100644 --- a/compiler/rustc_hir_analysis/src/hir_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/hir_wf_check.rs @@ -8,10 +8,11 @@ use rustc_middle::query::Providers; use rustc_middle::ty::{self, TyCtxt}; use rustc_span::def_id::LocalDefId; use rustc_trait_selection::traits::{self, ObligationCtxt}; +use tracing::debug; use crate::collect::ItemCtxt; -pub fn provide(providers: &mut Providers) { +pub(crate) fn provide(providers: &mut Providers) { *providers = Providers { diagnostic_hir_wf_check, ..*providers }; } diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check.rs b/compiler/rustc_hir_analysis/src/impl_wf_check.rs index ab441ed4cde..7f183324f04 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check.rs @@ -53,7 +53,10 @@ mod min_specialization; /// impl<'a> Trait<Foo> for Bar { type X = &'a i32; } /// // ^ 'a is unused and appears in assoc type, error /// ``` -pub fn check_impl_wf(tcx: TyCtxt<'_>, impl_def_id: LocalDefId) -> Result<(), ErrorGuaranteed> { +pub(crate) fn check_impl_wf( + tcx: TyCtxt<'_>, + impl_def_id: LocalDefId, +) -> Result<(), ErrorGuaranteed> { let min_specialization = tcx.features().min_specialization; let mut res = Ok(()); debug_assert_matches!(tcx.def_kind(impl_def_id), DefKind::Impl { .. }); @@ -137,8 +140,7 @@ fn enforce_impl_params_are_constrained( } }; if err { - let const_param_note = - matches!(param.kind, ty::GenericParamDefKind::Const { .. }).then_some(()); + let const_param_note = matches!(param.kind, ty::GenericParamDefKind::Const { .. }); let mut diag = tcx.dcx().create_err(UnconstrainedGenericParameter { span: tcx.def_span(param.def_id), param_name: param.name, diff --git a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs index f44a78bac4d..147646930dd 100644 --- a/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs +++ b/compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs @@ -77,6 +77,7 @@ use rustc_span::{ErrorGuaranteed, Span}; use rustc_trait_selection::error_reporting::InferCtxtErrorExt; use rustc_trait_selection::traits::outlives_bounds::InferCtxtExt as _; use rustc_trait_selection::traits::{self, translate_args_with_cause, wf, ObligationCtxt}; +use tracing::{debug, instrument}; use crate::errors::GenericArgsOnOverriddenImpl; use crate::{constrained_generic_params as cgp, errors}; diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs index 291d57f2a17..1481a4dd141 100644 --- a/compiler/rustc_hir_analysis/src/lib.rs +++ b/compiler/rustc_hir_analysis/src/lib.rs @@ -72,11 +72,9 @@ This API is completely unstable and subject to change. #![feature(slice_partition_dedup)] #![feature(try_blocks)] #![feature(unwrap_infallible)] +#![warn(unreachable_pub)] // tidy-alphabetical-end -#[macro_use] -extern crate tracing; - // These are used by Clippy. pub mod check; @@ -200,10 +198,6 @@ pub fn check_crate(tcx: TyCtxt<'_>) { } }); - // Freeze definitions as we don't add new ones at this point. This improves performance by - // allowing lock-free access to them. - tcx.untracked().definitions.freeze(); - // FIXME: Remove this when we implement creating `DefId`s // for anon constants during their parents' typeck. // Typeck all body owners in parallel will produce queries diff --git a/compiler/rustc_hir_analysis/src/outlives/explicit.rs b/compiler/rustc_hir_analysis/src/outlives/explicit.rs index bbfadbb5c30..f576499ecac 100644 --- a/compiler/rustc_hir_analysis/src/outlives/explicit.rs +++ b/compiler/rustc_hir_analysis/src/outlives/explicit.rs @@ -5,12 +5,12 @@ use rustc_middle::ty::{self, OutlivesPredicate, TyCtxt}; use super::utils::*; #[derive(Debug)] -pub struct ExplicitPredicatesMap<'tcx> { +pub(crate) struct ExplicitPredicatesMap<'tcx> { map: FxIndexMap<DefId, ty::EarlyBinder<'tcx, RequiredPredicates<'tcx>>>, } impl<'tcx> ExplicitPredicatesMap<'tcx> { - pub fn new() -> ExplicitPredicatesMap<'tcx> { + pub(crate) fn new() -> ExplicitPredicatesMap<'tcx> { ExplicitPredicatesMap { map: FxIndexMap::default() } } diff --git a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs index 454c20d3e64..c2377b4781c 100644 --- a/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs +++ b/compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs @@ -3,6 +3,7 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::DefId; use rustc_middle::ty::{self, GenericArg, GenericArgKind, Ty, TyCtxt}; use rustc_span::Span; +use tracing::debug; use super::explicit::ExplicitPredicatesMap; use super::utils::*; diff --git a/compiler/rustc_hir_analysis/src/outlives/mod.rs b/compiler/rustc_hir_analysis/src/outlives/mod.rs index cb61ef7c64d..e3cdb1bf5f7 100644 --- a/compiler/rustc_hir_analysis/src/outlives/mod.rs +++ b/compiler/rustc_hir_analysis/src/outlives/mod.rs @@ -9,7 +9,7 @@ mod explicit; mod implicit_infer; mod utils; -pub fn provide(providers: &mut Providers) { +pub(crate) fn provide(providers: &mut Providers) { *providers = Providers { inferred_outlives_of, inferred_outlives_crate, ..*providers }; } diff --git a/compiler/rustc_hir_analysis/src/variance/constraints.rs b/compiler/rustc_hir_analysis/src/variance/constraints.rs index ce9e73bf245..415b23d812b 100644 --- a/compiler/rustc_hir_analysis/src/variance/constraints.rs +++ b/compiler/rustc_hir_analysis/src/variance/constraints.rs @@ -8,11 +8,12 @@ use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, Ty, TyCtxt}; use rustc_middle::{bug, span_bug}; +use tracing::{debug, instrument}; use super::terms::VarianceTerm::*; use super::terms::*; -pub struct ConstraintContext<'a, 'tcx> { +pub(crate) struct ConstraintContext<'a, 'tcx> { pub terms_cx: TermsContext<'a, 'tcx>, // These are pointers to common `ConstantTerm` instances @@ -27,7 +28,7 @@ pub struct ConstraintContext<'a, 'tcx> { /// Declares that the variable `decl_id` appears in a location with /// variance `variance`. #[derive(Copy, Clone)] -pub struct Constraint<'a> { +pub(crate) struct Constraint<'a> { pub inferred: InferredIndex, pub variance: &'a VarianceTerm<'a>, } @@ -41,11 +42,11 @@ pub struct Constraint<'a> { /// ``` /// then while we are visiting `Bar<T>`, the `CurrentItem` would have /// the `DefId` and the start of `Foo`'s inferreds. -pub struct CurrentItem { +struct CurrentItem { inferred_start: InferredIndex, } -pub fn add_constraints_from_crate<'a, 'tcx>( +pub(crate) fn add_constraints_from_crate<'a, 'tcx>( terms_cx: TermsContext<'a, 'tcx>, ) -> ConstraintContext<'a, 'tcx> { let tcx = terms_cx.tcx; diff --git a/compiler/rustc_hir_analysis/src/variance/dump.rs b/compiler/rustc_hir_analysis/src/variance/dump.rs index 1a17dabb677..ace183986bd 100644 --- a/compiler/rustc_hir_analysis/src/variance/dump.rs +++ b/compiler/rustc_hir_analysis/src/variance/dump.rs @@ -1,18 +1,36 @@ +use std::fmt::Write; + use rustc_hir::def::DefKind; -use rustc_hir::def_id::CRATE_DEF_ID; -use rustc_middle::ty::TyCtxt; +use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; +use rustc_middle::ty::{GenericArgs, TyCtxt}; use rustc_span::symbol::sym; +fn format_variances(tcx: TyCtxt<'_>, def_id: LocalDefId) -> String { + let variances = tcx.variances_of(def_id); + let generics = GenericArgs::identity_for_item(tcx, def_id); + // 7 = 2-letter parameter + ": " + 1-letter variance + ", " + let mut ret = String::with_capacity(2 + 7 * variances.len()); + ret.push('['); + for (arg, variance) in generics.iter().zip(variances.iter()) { + write!(ret, "{arg}: {variance:?}, ").unwrap(); + } + // Remove trailing `, `. + if !variances.is_empty() { + ret.pop(); + ret.pop(); + } + ret.push(']'); + ret +} + pub(crate) fn variances(tcx: TyCtxt<'_>) { if tcx.has_attr(CRATE_DEF_ID, sym::rustc_variance_of_opaques) { for id in tcx.hir().items() { let DefKind::OpaqueTy = tcx.def_kind(id.owner_id) else { continue }; - let variances = tcx.variances_of(id.owner_id); - tcx.dcx().emit_err(crate::errors::VariancesOf { span: tcx.def_span(id.owner_id), - variances: format!("{variances:?}"), + variances: format_variances(tcx, id.owner_id.def_id), }); } } @@ -22,11 +40,9 @@ pub(crate) fn variances(tcx: TyCtxt<'_>) { continue; } - let variances = tcx.variances_of(id.owner_id); - tcx.dcx().emit_err(crate::errors::VariancesOf { span: tcx.def_span(id.owner_id), - variances: format!("{variances:?}"), + variances: format_variances(tcx, id.owner_id.def_id), }); } } diff --git a/compiler/rustc_hir_analysis/src/variance/mod.rs b/compiler/rustc_hir_analysis/src/variance/mod.rs index 8a4114c3e4b..12bb9a3f9e0 100644 --- a/compiler/rustc_hir_analysis/src/variance/mod.rs +++ b/compiler/rustc_hir_analysis/src/variance/mod.rs @@ -12,6 +12,7 @@ use rustc_middle::span_bug; use rustc_middle::ty::{ self, CrateVariancesMap, GenericArgsRef, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable, }; +use tracing::{debug, instrument}; /// Defines the `TermsContext` basically houses an arena where we can /// allocate terms. @@ -28,7 +29,7 @@ pub(crate) mod dump; /// Code for transforming variances. mod xform; -pub fn provide(providers: &mut Providers) { +pub(crate) fn provide(providers: &mut Providers) { *providers = Providers { variances_of, crate_variances, ..*providers }; } diff --git a/compiler/rustc_hir_analysis/src/variance/solve.rs b/compiler/rustc_hir_analysis/src/variance/solve.rs index e64c6721fe0..d0bdca86779 100644 --- a/compiler/rustc_hir_analysis/src/variance/solve.rs +++ b/compiler/rustc_hir_analysis/src/variance/solve.rs @@ -7,6 +7,7 @@ use rustc_hir::def_id::DefIdMap; use rustc_middle::ty; +use tracing::debug; use super::constraints::*; use super::terms::VarianceTerm::*; @@ -21,7 +22,7 @@ struct SolveContext<'a, 'tcx> { solutions: Vec<ty::Variance>, } -pub fn solve_constraints<'tcx>( +pub(crate) fn solve_constraints<'tcx>( constraints_cx: ConstraintContext<'_, 'tcx>, ) -> ty::CrateVariancesMap<'tcx> { let ConstraintContext { terms_cx, constraints, .. } = constraints_cx; diff --git a/compiler/rustc_hir_analysis/src/variance/terms.rs b/compiler/rustc_hir_analysis/src/variance/terms.rs index 36bff60e019..cf38957bf24 100644 --- a/compiler/rustc_hir_analysis/src/variance/terms.rs +++ b/compiler/rustc_hir_analysis/src/variance/terms.rs @@ -15,16 +15,17 @@ use rustc_arena::DroplessArena; use rustc_hir::def::DefKind; use rustc_hir::def_id::{LocalDefId, LocalDefIdMap}; use rustc_middle::ty::{self, TyCtxt}; +use tracing::debug; use self::VarianceTerm::*; -pub type VarianceTermPtr<'a> = &'a VarianceTerm<'a>; +pub(crate) type VarianceTermPtr<'a> = &'a VarianceTerm<'a>; #[derive(Copy, Clone, Debug)] -pub struct InferredIndex(pub usize); +pub(crate) struct InferredIndex(pub usize); #[derive(Copy, Clone)] -pub enum VarianceTerm<'a> { +pub(crate) enum VarianceTerm<'a> { ConstantTerm(ty::Variance), TransformTerm(VarianceTermPtr<'a>, VarianceTermPtr<'a>), InferredTerm(InferredIndex), @@ -45,7 +46,7 @@ impl<'a> fmt::Debug for VarianceTerm<'a> { /// The first pass over the crate simply builds up the set of inferreds. -pub struct TermsContext<'a, 'tcx> { +pub(crate) struct TermsContext<'a, 'tcx> { pub tcx: TyCtxt<'tcx>, pub arena: &'a DroplessArena, @@ -62,7 +63,7 @@ pub struct TermsContext<'a, 'tcx> { pub inferred_terms: Vec<VarianceTermPtr<'a>>, } -pub fn determine_parameters_to_be_inferred<'a, 'tcx>( +pub(crate) fn determine_parameters_to_be_inferred<'a, 'tcx>( tcx: TyCtxt<'tcx>, arena: &'a DroplessArena, ) -> TermsContext<'a, 'tcx> { diff --git a/compiler/rustc_hir_analysis/src/variance/xform.rs b/compiler/rustc_hir_analysis/src/variance/xform.rs index 027f0859fcd..2e9964788e6 100644 --- a/compiler/rustc_hir_analysis/src/variance/xform.rs +++ b/compiler/rustc_hir_analysis/src/variance/xform.rs @@ -1,6 +1,6 @@ use rustc_middle::ty; -pub fn glb(v1: ty::Variance, v2: ty::Variance) -> ty::Variance { +pub(crate) fn glb(v1: ty::Variance, v2: ty::Variance) -> ty::Variance { // Greatest lower bound of the variance lattice as // defined in The Paper: // |
