diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index bfb42efb684f1..ec57720387c02 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -112,7 +112,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } // `a::b::Trait(Args)::TraitItem` Res::Def(DefKind::AssocFn, _) - | Res::Def(DefKind::AssocConst, _) + | Res::Def(DefKind::AssocConst { .. }, _) | Res::Def(DefKind::AssocTy, _) if i + 2 == proj_start => { diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs index c2c07614bc0dd..7c12737776949 100644 --- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs +++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs @@ -1157,7 +1157,10 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> { let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = base.kind else { return }; let (hir::def::Res::Local(_) | hir::def::Res::Def( - DefKind::Const | DefKind::ConstParam | DefKind::Static { .. } | DefKind::AssocConst, + DefKind::Const { .. } + | DefKind::ConstParam + | DefKind::Static { .. } + | DefKind::AssocConst { .. }, _, )) = path.res else { diff --git a/compiler/rustc_borrowck/src/type_check/free_region_relations.rs b/compiler/rustc_borrowck/src/type_check/free_region_relations.rs index c4d964441b1d2..41eb5f0302f4f 100644 --- a/compiler/rustc_borrowck/src/type_check/free_region_relations.rs +++ b/compiler/rustc_borrowck/src/type_check/free_region_relations.rs @@ -296,7 +296,8 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> { // - We must compute the normalized signature and then compute implied bounds from that // in order to connect any unconstrained region vars created during normalization to // the types of the locals corresponding to the inputs and outputs of the item. (#136547) - if matches!(tcx.def_kind(defining_ty_def_id), DefKind::AssocFn | DefKind::AssocConst) { + if matches!(tcx.def_kind(defining_ty_def_id), DefKind::AssocFn | DefKind::AssocConst { .. }) + { for &(ty, _) in tcx.assumed_wf_types(tcx.local_parent(defining_ty_def_id)) { let result: Result<_, ErrorGuaranteed> = param_env .and(DeeplyNormalize { value: ty }) diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index d2b7e9a6b84fe..2dbf2cc910580 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -34,12 +34,12 @@ fn setup_for_eval<'tcx>( cid.promoted.is_some() || matches!( ecx.tcx.def_kind(cid.instance.def_id()), - DefKind::Const + DefKind::Const { .. } | DefKind::Static { .. } | DefKind::ConstParam | DefKind::AnonConst | DefKind::InlineConst - | DefKind::AssocConst + | DefKind::AssocConst { .. } ), "Unexpected DefKind: {:?}", ecx.tcx.def_kind(cid.instance.def_id()) diff --git a/compiler/rustc_hir/src/def.rs b/compiler/rustc_hir/src/def.rs index dd2cd4939432b..394622654cf88 100644 --- a/compiler/rustc_hir/src/def.rs +++ b/compiler/rustc_hir/src/def.rs @@ -120,7 +120,9 @@ pub enum DefKind { // Value namespace Fn, - Const, + Const { + is_type_const: bool, + }, /// Constant generic parameter: `struct Foo { ... }` ConstParam, Static { @@ -147,7 +149,9 @@ pub enum DefKind { /// or `trait Foo { fn associated() {} }` AssocFn, /// Associated constant: `trait MyTrait { const ASSOC: usize; }` - AssocConst, + AssocConst { + is_type_const: bool, + }, // Macro namespace Macro(MacroKinds), @@ -222,8 +226,8 @@ impl DefKind { DefKind::Trait => "trait", DefKind::ForeignTy => "foreign type", DefKind::AssocFn => "associated function", - DefKind::Const => "constant", - DefKind::AssocConst => "associated constant", + DefKind::Const { .. } => "constant", + DefKind::AssocConst { .. } => "associated constant", DefKind::TyParam => "type parameter", DefKind::ConstParam => "const parameter", DefKind::Macro(kinds) => kinds.descr(), @@ -249,7 +253,7 @@ impl DefKind { pub fn article(&self) -> &'static str { match *self { DefKind::AssocTy - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::Enum | DefKind::OpaqueTy @@ -277,12 +281,12 @@ impl DefKind { | DefKind::TyParam => Some(Namespace::TypeNS), DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } | DefKind::Ctor(..) | DefKind::AssocFn - | DefKind::AssocConst => Some(Namespace::ValueNS), + | DefKind::AssocConst { .. } => Some(Namespace::ValueNS), DefKind::Macro(..) => Some(Namespace::MacroNS), @@ -323,11 +327,11 @@ impl DefKind { DefKind::AssocTy => DefPathData::TypeNs(name.unwrap()), DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Field => DefPathData::ValueNs(name.unwrap()), DefKind::Macro(..) => DefPathData::MacroNs(name.unwrap()), DefKind::LifetimeParam => DefPathData::LifetimeNs(name.unwrap()), @@ -345,7 +349,7 @@ impl DefKind { } pub fn is_assoc(self) -> bool { - matches!(self, DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy) + matches!(self, DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy) } /// This is a "module" in name resolution sense. @@ -371,11 +375,11 @@ impl DefKind { pub fn has_generics(self) -> bool { match self { DefKind::AnonConst - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy | DefKind::Closure - | DefKind::Const + | DefKind::Const { .. } | DefKind::Ctor(..) | DefKind::Enum | DefKind::Field @@ -423,8 +427,8 @@ impl DefKind { | DefKind::ForeignTy | DefKind::TraitAlias | DefKind::AssocTy - | DefKind::Const - | DefKind::AssocConst + | DefKind::Const { .. } + | DefKind::AssocConst { .. } | DefKind::Macro(..) | DefKind::Use | DefKind::ForeignMod @@ -459,12 +463,12 @@ impl DefKind { | DefKind::AssocTy | DefKind::TyParam | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } | DefKind::Ctor(_, _) | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Macro(_) | DefKind::ExternCrate | DefKind::Use diff --git a/compiler/rustc_hir/src/target.rs b/compiler/rustc_hir/src/target.rs index b76c48fbe8ac2..dd685c44ec477 100644 --- a/compiler/rustc_hir/src/target.rs +++ b/compiler/rustc_hir/src/target.rs @@ -148,7 +148,7 @@ impl Target { DefKind::ExternCrate => Target::ExternCrate, DefKind::Use => Target::Use, DefKind::Static { .. } => Target::Static, - DefKind::Const => Target::Const, + DefKind::Const { .. } => Target::Const, DefKind::Fn => Target::Fn, DefKind::Macro(..) => Target::MacroDef, DefKind::Mod => Target::Mod, diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 349ad4f7fc43b..5828ceea6f7db 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -900,7 +900,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), // avoids this query from having a direct dependency edge on the HIR return res; } - DefKind::Const => { + DefKind::Const { .. } => { tcx.ensure_ok().generics_of(def_id); tcx.ensure_ok().type_of(def_id); tcx.ensure_ok().predicates_of(def_id); @@ -1062,7 +1062,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), // avoids this query from having a direct dependency edge on the HIR return res; } - DefKind::AssocConst => { + DefKind::AssocConst { .. } => { tcx.ensure_ok().type_of(def_id); tcx.ensure_ok().predicates_of(def_id); res = res.and(check_associated_item(tcx, def_id)); diff --git a/compiler/rustc_hir_analysis/src/check/mod.rs b/compiler/rustc_hir_analysis/src/check/mod.rs index 074501b8ebe47..2c47c91d5192b 100644 --- a/compiler/rustc_hir_analysis/src/check/mod.rs +++ b/compiler/rustc_hir_analysis/src/check/mod.rs @@ -545,7 +545,7 @@ fn suggestion_signature<'tcx>( ); format!("type {}{generics} = /* Type */{where_clauses};", assoc.name()) } - ty::AssocKind::Const { name } => { + ty::AssocKind::Const { name, .. } => { let ty = tcx.type_of(assoc.def_id).instantiate_identity(); let val = tcx .infer_ctxt() diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index ed951015a4f19..7d1c829397bd7 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -2360,11 +2360,11 @@ fn lint_redundant_lifetimes<'tcx>( | DefKind::Trait | DefKind::TraitAlias | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::Impl { of_trait: _ } => { // Proceed } - DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => { + DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst { .. } => { if tcx.trait_impl_of_assoc(owner_id.to_def_id()).is_some() { // Don't check for redundant lifetimes for associated items of trait // implementations, since the signature is required to be compatible diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 276bc697eaa96..f4c7234cd8f83 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -92,7 +92,6 @@ pub(crate) fn provide(providers: &mut Providers) { const_param_default, anon_const_kind, const_of_item, - is_rhs_type_const, ..*providers }; } @@ -1702,22 +1701,3 @@ fn const_of_item<'tcx>( ty::EarlyBinder::bind(ct) } } - -/// Check if a Const or AssocConst is a type const (mgca) -fn is_rhs_type_const<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> bool { - match tcx.hir_node_by_def_id(def) { - hir::Node::Item(hir::Item { - kind: hir::ItemKind::Const(_, _, _, hir::ConstItemRhs::TypeConst(_)), - .. - }) - | hir::Node::ImplItem(hir::ImplItem { - kind: hir::ImplItemKind::Const(_, hir::ConstItemRhs::TypeConst(_)), - .. - }) - | hir::Node::TraitItem(hir::TraitItem { - kind: hir::TraitItemKind::Const(_, _, hir::IsTypeConst::Yes), - .. - }) => return true, - _ => return false, - } -} 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 3f82375308927..31b5995688371 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -265,10 +265,11 @@ impl LowerTypeRelativePathMode { } } - fn def_kind(self) -> DefKind { + ///NOTE: use `assoc_tag` for any important logic + fn def_kind_for_diagnostics(self) -> DefKind { match self { Self::Type(_) => DefKind::AssocTy, - Self::Const => DefKind::AssocConst, + Self::Const => DefKind::AssocConst { is_type_const: false }, } } @@ -1547,7 +1548,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { }; could_refer_to(DefKind::Variant, variant_def_id, ""); - could_refer_to(mode.def_kind(), item_def_id, " also"); + could_refer_to(mode.def_kind_for_diagnostics(), item_def_id, " also"); lint.span_suggestion( span, @@ -2080,12 +2081,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { } // Case 3. Reference to a top-level value. - DefKind::Fn | DefKind::Const | DefKind::ConstParam | DefKind::Static { .. } => { + DefKind::Fn | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } => { generic_segments.push(GenericPathSegment(def_id, last)); } // Case 4. Reference to a method or associated const. - DefKind::AssocFn | DefKind::AssocConst => { + DefKind::AssocFn | DefKind::AssocConst { .. } => { if segments.len() >= 2 { let generics = tcx.generics_of(def_id); generic_segments.push(GenericPathSegment(generics.parent.unwrap(), last - 1)); @@ -2658,7 +2659,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { ); self.lower_const_param(def_id, hir_id) } - Res::Def(DefKind::Const, did) => { + Res::Def(DefKind::Const { .. }, did) => { if let Err(guar) = self.require_type_const_attribute(did, span) { return Const::new_error(self.tcx(), guar); } @@ -2699,7 +2700,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { let args = self.lower_generic_args_of_path_segment(span, generics_did, segment); ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args)) } - Res::Def(DefKind::AssocConst, did) => { + Res::Def(DefKind::AssocConst { .. }, did) => { let trait_segment = if let [modules @ .., trait_, _item] = path.segments { let _ = self.prohibit_generic_args(modules.iter(), GenericsArgsErrExtend::None); Some(trait_) diff --git a/compiler/rustc_hir_analysis/src/hir_wf_check.rs b/compiler/rustc_hir_analysis/src/hir_wf_check.rs index 08da8d19344da..87b52c57dc6a2 100644 --- a/compiler/rustc_hir_analysis/src/hir_wf_check.rs +++ b/compiler/rustc_hir_analysis/src/hir_wf_check.rs @@ -104,7 +104,7 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>( if self.depth >= self.cause_depth { self.cause = Some(error.obligation.cause); if let hir::TyKind::TraitObject(..) = ty.kind - && let DefKind::AssocTy | DefKind::AssocConst | DefKind::AssocFn = + && let DefKind::AssocTy | DefKind::AssocConst { .. } | DefKind::AssocFn = self.tcx.def_kind(self.def_id) { self.cause = Some(ObligationCause::new( diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs index 40e79234d0201..a1c8b8421e7c1 100644 --- a/compiler/rustc_hir_analysis/src/lib.rs +++ b/compiler/rustc_hir_analysis/src/lib.rs @@ -231,7 +231,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) { tcx.ensure_ok().eval_static_initializer(item_def_id); check::maybe_check_static_with_link_section(tcx, item_def_id); } - DefKind::Const + DefKind::Const { .. } if !tcx.generics_of(item_def_id).own_requires_monomorphization() && !tcx.is_type_const(item_def_id) => { diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index d9604ceedb150..e5abcd34ad1fe 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -723,7 +723,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { hir::Path { res: hir::def::Res::Def( - hir::def::DefKind::Static { .. } | hir::def::DefKind::Const, + hir::def::DefKind::Static { .. } + | hir::def::DefKind::Const { .. }, def_id, ), .. diff --git a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs index 77ecfc2fb0bee..94f682a8859e9 100644 --- a/compiler/rustc_hir_typeck/src/expr_use_visitor.rs +++ b/compiler/rustc_hir_typeck/src/expr_use_visitor.rs @@ -908,7 +908,8 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx let res = self.cx.typeck_results().qpath_res(qpath, *hir_id); match res { - Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => { + Res::Def(DefKind::Const { .. }, _) + | Res::Def(DefKind::AssocConst { .. }, _) => { // Named constants have to be equated with the value // being matched, so that's a read of the value being matched. // @@ -1405,9 +1406,9 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx match res { Res::Def( DefKind::Ctor(..) - | DefKind::Const + | DefKind::Const { .. } | DefKind::ConstParam - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Fn | DefKind::AssocFn, _, diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 1931222bba30e..d2152b763a8e2 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -988,7 +988,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Res::Def(DefKind::Ctor(CtorOf::Variant, _), _) => { err_extend = GenericsArgsErrExtend::DefVariant(segments); } - Res::Def(DefKind::AssocFn | DefKind::AssocConst, def_id) => { + Res::Def(DefKind::AssocFn | DefKind::AssocConst { .. }, def_id) => { let assoc_item = tcx.associated_item(def_id); let container = assoc_item.container; let container_id = assoc_item.container_id(tcx); @@ -1314,7 +1314,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ) }); - let args_for_user_type = if let Res::Def(DefKind::AssocConst, def_id) = res { + let args_for_user_type = if let Res::Def(DefKind::AssocConst { .. }, def_id) = res { self.transform_args_for_inherent_type_const(def_id, args_raw) } else { args_raw @@ -1362,7 +1362,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { debug!("instantiate_value_path: type of {:?} is {:?}", hir_id, ty_instantiated); - let args = if let Res::Def(DefKind::AssocConst, def_id) = res { + let args = if let Res::Def(DefKind::AssocConst { .. }, def_id) = res { self.transform_args_for_inherent_type_const(def_id, args) } else { args diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 43695095b8d40..967f255361b59 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -82,7 +82,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { hir::ExprKind::ConstBlock(..) => return None, hir::ExprKind::Path(qpath) => { let res = self.typeck_results.borrow().qpath_res(qpath, element.hir_id); - if let Res::Def(DefKind::Const | DefKind::AssocConst, _) = res { + if let Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, _) = res + { return None; } } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index c82a779ca8326..801f5acf9d2f3 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1834,7 +1834,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } _ => return false, }; - if item.def_id == old_def_id || self.tcx.def_kind(item.def_id) != DefKind::AssocConst { + if item.def_id == old_def_id + || !matches!(self.tcx.def_kind(item.def_id), DefKind::AssocConst { .. }) + { // Same item return false; } @@ -2379,7 +2381,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { && match expr.kind { ExprKind::Path(QPath::Resolved( None, - Path { res: Res::Def(DefKind::Const, _), .. }, + Path { res: Res::Def(DefKind::Const { .. }, _), .. }, )) => true, ExprKind::Call( Expr { diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index b98c9de92ca9b..b6162f2556d36 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -1846,7 +1846,7 @@ impl<'tcx> Pick<'tcx> { tcx.def_path_str(self.item.def_id), )); } - (ty::AssocKind::Const { name }, ty::AssocContainer::Trait) => { + (ty::AssocKind::Const { name, .. }, ty::AssocContainer::Trait) => { let def_id = self.item.container_id(tcx); lint.span_suggestion( span, diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index 8f78faddba0e7..ea4edb3ccf3cd 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -312,7 +312,7 @@ enum ResolvedPatKind<'tcx> { impl<'tcx> ResolvedPat<'tcx> { fn adjust_mode(&self) -> AdjustMode { if let ResolvedPatKind::Path { res, .. } = self.kind - && matches!(res, Res::Def(DefKind::Const | DefKind::AssocConst, _)) + && matches!(res, Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, _)) { // These constants can be of a reference type, e.g. `const X: &u8 = &0;`. // Peeling the reference types too early will cause type checking failures. @@ -1567,8 +1567,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } Res::Def( DefKind::Ctor(_, CtorKind::Const) - | DefKind::Const - | DefKind::AssocConst + | DefKind::Const { .. } + | DefKind::AssocConst { .. } | DefKind::ConstParam, _, ) => {} // OK @@ -1660,7 +1660,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { _ => { let (type_def_id, item_def_id) = match resolved_pat.ty.kind() { ty::Adt(def, _) => match res { - Res::Def(DefKind::Const, def_id) => (Some(def.did()), Some(def_id)), + Res::Def(DefKind::Const { .. }, def_id) => { + (Some(def.did()), Some(def_id)) + } _ => (None, None), }, _ => (None, None), @@ -1733,7 +1735,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Res::Err => { self.dcx().span_bug(pat.span, "`Res::Err` but no error emitted"); } - Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) => { + Res::Def(DefKind::AssocConst { .. } | DefKind::AssocFn, _) => { return report_unexpected_res(res); } Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) => tcx.expect_variant_res(res), diff --git a/compiler/rustc_lint/src/interior_mutable_consts.rs b/compiler/rustc_lint/src/interior_mutable_consts.rs index 807a121abd9e6..904048d27ee89 100644 --- a/compiler/rustc_lint/src/interior_mutable_consts.rs +++ b/compiler/rustc_lint/src/interior_mutable_consts.rs @@ -75,7 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for InteriorMutableConsts { }; if let ExprKind::Path(qpath) = &receiver.kind - && let Res::Def(DefKind::Const | DefKind::AssocConst, const_did) = + && let Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, const_did) = typeck.qpath_res(qpath, receiver.hir_id) // Don't consider derefs as those can do arbitrary things // like using thread local (see rust-lang/rust#150157) diff --git a/compiler/rustc_lint/src/non_local_def.rs b/compiler/rustc_lint/src/non_local_def.rs index 2b9a65f84a026..ac7155eabfdc9 100644 --- a/compiler/rustc_lint/src/non_local_def.rs +++ b/compiler/rustc_lint/src/non_local_def.rs @@ -78,7 +78,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { // Per RFC we (currently) ignore anon-const (`const _: Ty = ...`) in top-level module. if self.body_depth == 1 - && parent_def_kind == DefKind::Const + && matches!(parent_def_kind, DefKind::Const { .. }) && parent_opt_item_name == Some(kw::Underscore) { return; @@ -159,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { // for impl, otherwise the item-def and impl-def won't have the same parent. let outermost_impl_parent = peel_parent_while(cx.tcx, parent, |tcx, did| { tcx.def_kind(did) == DefKind::Mod - || (tcx.def_kind(did) == DefKind::Const + || (matches!(tcx.def_kind(did), DefKind::Const { .. }) && tcx.opt_item_name(did) == Some(kw::Underscore)) }); @@ -179,20 +179,22 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions { // Get the span of the parent const item ident (if it's a not a const anon). // // Used to suggest changing the const item to a const anon. - let span_for_const_anon_suggestion = if parent_def_kind == DefKind::Const - && parent_opt_item_name != Some(kw::Underscore) - && let Some(parent) = parent.as_local() - && let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent) - && let ItemKind::Const(ident, _, ty, _) = item.kind - && let TyKind::Tup(&[]) = ty.kind - { - Some(ident.span) - } else { - None - }; + let span_for_const_anon_suggestion = + if matches!(parent_def_kind, DefKind::Const { .. }) + && parent_opt_item_name != Some(kw::Underscore) + && let Some(parent) = parent.as_local() + && let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent) + && let ItemKind::Const(ident, _, ty, _) = item.kind + && let TyKind::Tup(&[]) = ty.kind + { + Some(ident.span) + } else { + None + }; - let const_anon = matches!(parent_def_kind, DefKind::Const | DefKind::Static { .. }) - .then_some(span_for_const_anon_suggestion); + let const_anon = + matches!(parent_def_kind, DefKind::Const { .. } | DefKind::Static { .. }) + .then_some(span_for_const_anon_suggestion); let impl_span = item.span.shrink_to_lo().to(impl_.self_ty.span); let mut ms = MultiSpan::from_span(impl_span); @@ -315,7 +317,7 @@ fn did_has_local_parent( peel_parent_while(tcx, parent_did, |tcx, did| { tcx.def_kind(did) == DefKind::Mod - || (tcx.def_kind(did) == DefKind::Const + || (matches!(tcx.def_kind(did), DefKind::Const { .. }) && tcx.opt_item_name(did) == Some(kw::Underscore)) }) .map(|parent_did| parent_did == impl_parent || Some(parent_did) == outermost_impl_parent) diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index 1496e3974bd75..d7db55b58d00f 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -587,7 +587,7 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals { .. }) = p.kind { - if let Res::Def(DefKind::Const, _) = path.res + if let Res::Def(DefKind::Const { .. }, _) = path.res && let [segment] = path.segments { NonUpperCaseGlobals::check_upper_case( diff --git a/compiler/rustc_lint/src/transmute.rs b/compiler/rustc_lint/src/transmute.rs index e4716c869c5f1..4846982a3a849 100644 --- a/compiler/rustc_lint/src/transmute.rs +++ b/compiler/rustc_lint/src/transmute.rs @@ -216,7 +216,7 @@ fn check_ptr_transmute_in_const<'tcx>( dst: Ty<'tcx>, ) { if matches!(const_context, Some(hir::ConstContext::ConstFn)) - || matches!(cx.tcx.def_kind(body_owner_def_id), DefKind::AssocConst) + || matches!(cx.tcx.def_kind(body_owner_def_id), DefKind::AssocConst { .. }) { if src.is_raw_ptr() && dst.is_integral() { cx.tcx.emit_node_span_lint( diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index bd5b3893e4e8a..2fa8e19984b4c 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1329,7 +1329,9 @@ impl<'a> CrateMetadataRef<'a> { fn get_associated_item(self, tcx: TyCtxt<'_>, id: DefIndex) -> ty::AssocItem { let kind = match self.def_kind(tcx, id) { - DefKind::AssocConst => ty::AssocKind::Const { name: self.item_name(id) }, + DefKind::AssocConst { is_type_const } => { + ty::AssocKind::Const { name: self.item_name(id), is_type_const } + } DefKind::AssocFn => ty::AssocKind::Fn { name: self.item_name(id), has_self: self.get_fn_has_self_parameter(tcx, id), diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 8c4f2e4a36b12..01e6d49a51c72 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -418,7 +418,6 @@ provide! { tcx, def_id, other, cdata, } anon_const_kind => { table } const_of_item => { table } - is_rhs_type_const => { table } } pub(in crate::rmeta) fn provide(providers: &mut Providers) { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 9b0a114fecea8..885fa891892e6 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -904,11 +904,11 @@ fn should_encode_span(def_kind: DefKind) -> bool { | DefKind::ConstParam | DefKind::LifetimeParam | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { .. } | DefKind::Ctor(..) | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Macro(_) | DefKind::ExternCrate | DefKind::Use @@ -936,10 +936,10 @@ fn should_encode_attrs(def_kind: DefKind) -> bool { | DefKind::TraitAlias | DefKind::AssocTy | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { nested: false, .. } | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Macro(_) | DefKind::Field | DefKind::Impl { .. } => true, @@ -979,12 +979,12 @@ fn should_encode_expn_that_defined(def_kind: DefKind) -> bool { | DefKind::AssocTy | DefKind::TyParam | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } | DefKind::Ctor(..) | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Macro(_) | DefKind::ExternCrate | DefKind::Use @@ -1013,11 +1013,11 @@ fn should_encode_visibility(def_kind: DefKind) -> bool { | DefKind::TraitAlias | DefKind::AssocTy | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { nested: false, .. } | DefKind::Ctor(..) | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Macro(..) | DefKind::Field => true, DefKind::Use @@ -1046,11 +1046,11 @@ fn should_encode_stability(def_kind: DefKind) -> bool { | DefKind::Struct | DefKind::AssocTy | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::TyParam | DefKind::ConstParam | DefKind::Static { .. } - | DefKind::Const + | DefKind::Const { .. } | DefKind::Fn | DefKind::ForeignMod | DefKind::TyAlias @@ -1102,9 +1102,10 @@ fn should_encode_mir( // instance_mir uses mir_for_ctfe rather than optimized_mir for constructors DefKind::Ctor(_, _) => (true, false), // Constants - DefKind::AnonConst | DefKind::InlineConst | DefKind::AssocConst | DefKind::Const => { - (true, false) - } + DefKind::AnonConst { .. } + | DefKind::InlineConst + | DefKind::AssocConst { .. } + | DefKind::Const { .. } => (true, false), // Coroutines require optimized MIR to compute layout. DefKind::Closure if tcx.is_coroutine(def_id.to_def_id()) => (false, true), DefKind::SyntheticCoroutineBody => (false, true), @@ -1140,11 +1141,11 @@ fn should_encode_variances<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: Def DefKind::Mod | DefKind::Variant | DefKind::Field - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::TyParam | DefKind::ConstParam | DefKind::Static { .. } - | DefKind::Const + | DefKind::Const { .. } | DefKind::ForeignMod | DefKind::Impl { .. } | DefKind::Trait @@ -1175,11 +1176,11 @@ fn should_encode_generics(def_kind: DefKind) -> bool { | DefKind::TraitAlias | DefKind::AssocTy | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { .. } | DefKind::Ctor(..) | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::AnonConst | DefKind::InlineConst | DefKind::OpaqueTy @@ -1208,13 +1209,13 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> | DefKind::Ctor(..) | DefKind::Field | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { nested: false, .. } | DefKind::TyAlias | DefKind::ForeignTy | DefKind::Impl { .. } | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Closure | DefKind::ConstParam | DefKind::AnonConst @@ -1269,14 +1270,14 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool { | DefKind::Enum | DefKind::Variant | DefKind::Field - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { .. } | DefKind::Ctor(..) | DefKind::TyAlias | DefKind::OpaqueTy | DefKind::ForeignTy | DefKind::Impl { .. } - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Closure | DefKind::ConstParam | DefKind::AnonConst @@ -1308,8 +1309,8 @@ fn should_encode_constness(def_kind: DefKind) -> bool { | DefKind::Union | DefKind::Enum | DefKind::Field - | DefKind::Const - | DefKind::AssocConst + | DefKind::Const { .. } + | DefKind::AssocConst { .. } | DefKind::AnonConst | DefKind::Static { .. } | DefKind::TyAlias @@ -1338,7 +1339,10 @@ fn should_encode_constness(def_kind: DefKind) -> bool { fn should_encode_const(def_kind: DefKind) -> bool { match def_kind { // FIXME(mgca): should we remove Const and AssocConst here? - DefKind::Const | DefKind::AssocConst | DefKind::AnonConst | DefKind::InlineConst => true, + DefKind::Const { .. } + | DefKind::AssocConst { .. } + | DefKind::AnonConst + | DefKind::InlineConst => true, DefKind::Struct | DefKind::Union @@ -1373,7 +1377,7 @@ fn should_encode_const(def_kind: DefKind) -> bool { fn should_encode_const_of_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, def_kind: DefKind) -> bool { // AssocConst ==> assoc item has value tcx.is_type_const(def_id) - && (!matches!(def_kind, DefKind::AssocConst) || assoc_item_has_value(tcx, def_id)) + && (!matches!(def_kind, DefKind::AssocConst { .. }) || assoc_item_has_value(tcx, def_id)) } fn assoc_item_has_value<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool { @@ -1627,9 +1631,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let table = tcx.associated_types_for_impl_traits_in_trait_or_impl(def_id); record!(self.tables.associated_types_for_impl_traits_in_trait_or_impl[def_id] <- table); } - if let DefKind::AssocConst | DefKind::Const = def_kind { - record!(self.tables.is_rhs_type_const[def_id] <- self.tcx.is_rhs_type_const(def_id)); - } } for (def_id, impls) in &tcx.crate_inherent_impls(()).0.inherent_impls { diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 408b50ae48df1..af6df0cd6eb61 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -476,7 +476,6 @@ define_tables! { anon_const_kind: Table>, const_of_item: Table>>>, associated_types_for_impl_traits_in_trait_or_impl: Table>>>, - is_rhs_type_const: Table>, } #[derive(TyEncodable, TyDecodable)] diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index 3b5a38181d58b..5e256438ad953 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -175,10 +175,12 @@ fixed_size_enum! { ( AssocTy ) ( TyParam ) ( Fn ) - ( Const ) + ( Const { is_type_const: true} ) + ( Const { is_type_const: false} ) ( ConstParam ) ( AssocFn ) - ( AssocConst ) + ( AssocConst { is_type_const:true } ) + ( AssocConst { is_type_const:false } ) ( ExternCrate ) ( Use ) ( ForeignMod ) diff --git a/compiler/rustc_middle/src/hir/map.rs b/compiler/rustc_middle/src/hir/map.rs index 67dd26c8a7d31..cf1b863f754c8 100644 --- a/compiler/rustc_middle/src/hir/map.rs +++ b/compiler/rustc_middle/src/hir/map.rs @@ -289,7 +289,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn hir_body_owner_kind(self, def_id: impl Into) -> BodyOwnerKind { let def_id = def_id.into(); match self.def_kind(def_id) { - DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => { + DefKind::Const { .. } | DefKind::AssocConst { .. } | DefKind::AnonConst => { BodyOwnerKind::Const { inline: false } } DefKind::InlineConst => BodyOwnerKind::Const { inline: true }, diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index e5fe01781e96d..5d525f67bd1b7 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -623,7 +623,7 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io: }; match (kind, body.source.promoted) { (_, Some(_)) => write!(w, "const ")?, // promoteds are the closest to consts - (DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?, + (DefKind::Const { .. } | DefKind::AssocConst { .. }, _) => write!(w, "const ")?, (DefKind::Static { safety: _, mutability: hir::Mutability::Not, nested: false }, _) => { write!(w, "static ")? } diff --git a/compiler/rustc_middle/src/queries.rs b/compiler/rustc_middle/src/queries.rs index 3a3a2743ec4f8..756292b5a4bea 100644 --- a/compiler/rustc_middle/src/queries.rs +++ b/compiler/rustc_middle/src/queries.rs @@ -2771,12 +2771,6 @@ rustc_queries! { cache_on_disk_if { *cnum == LOCAL_CRATE } separate_provide_extern } - - query is_rhs_type_const(def_id: DefId) -> bool { - desc { "checking whether `{}` is a rhs type const", tcx.def_path_str(def_id) } - cache_on_disk_if { def_id.is_local() } - separate_provide_extern - } } rustc_with_all_queries! { define_callbacks! } diff --git a/compiler/rustc_middle/src/ty/assoc.rs b/compiler/rustc_middle/src/ty/assoc.rs index 8c560df4e1626..4cc255cba6358 100644 --- a/compiler/rustc_middle/src/ty/assoc.rs +++ b/compiler/rustc_middle/src/ty/assoc.rs @@ -30,7 +30,7 @@ impl AssocItem { match self.kind { ty::AssocKind::Type { data: AssocTypeData::Normal(name) } => Some(name), ty::AssocKind::Type { data: AssocTypeData::Rpitit(_) } => None, - ty::AssocKind::Const { name } => Some(name), + ty::AssocKind::Const { name, .. } => Some(name), ty::AssocKind::Fn { name, .. } => Some(name), } } @@ -119,7 +119,7 @@ impl AssocItem { tcx.fn_sig(self.def_id).instantiate_identity().skip_binder().to_string() } ty::AssocKind::Type { .. } => format!("type {};", self.name()), - ty::AssocKind::Const { name } => { + ty::AssocKind::Const { name, .. } => { format!("const {}: {:?};", name, tcx.type_of(self.def_id).instantiate_identity()) } } @@ -173,7 +173,7 @@ pub enum AssocTypeData { #[derive(Copy, Clone, PartialEq, Debug, HashStable, Eq, Hash, Encodable, Decodable)] pub enum AssocKind { - Const { name: Symbol }, + Const { name: Symbol, is_type_const: bool }, Fn { name: Symbol, has_self: bool }, Type { data: AssocTypeData }, } @@ -196,7 +196,9 @@ impl AssocKind { pub fn as_def_kind(&self) -> DefKind { match self { - Self::Const { .. } => DefKind::AssocConst, + Self::Const { is_type_const, .. } => { + DefKind::AssocConst { is_type_const: *is_type_const } + } Self::Fn { .. } => DefKind::AssocFn, Self::Type { .. } => DefKind::AssocTy, } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index b57de30201876..f8491928e312d 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -914,8 +914,8 @@ impl<'tcx> TyCtxt<'tcx> { } else if matches!( def_kind, DefKind::AnonConst - | DefKind::AssocConst - | DefKind::Const + | DefKind::AssocConst { .. } + | DefKind::Const { .. } | DefKind::InlineConst | DefKind::GlobalAsm ) { @@ -1114,13 +1114,13 @@ impl<'tcx> TyCtxt<'tcx> { /// Check if the given `def_id` is a `type const` (mgca) pub fn is_type_const>(self, def_id: I) -> bool { // No need to call the query directly in this case always false. - if !(matches!( - self.def_kind(def_id.into_query_param()), - DefKind::Const | DefKind::AssocConst - )) { - return false; + let def_kind = self.def_kind(def_id.into_query_param()); + match def_kind { + DefKind::Const { is_type_const } | DefKind::AssocConst { is_type_const } => { + is_type_const + } + _ => false, } - self.is_rhs_type_const(def_id) } /// Returns the movability of the coroutine of `def_id`, or panics @@ -2151,9 +2151,9 @@ impl<'tcx> TyCtxt<'tcx> { // ATPITs) do not. let is_inherent_assoc_ty = matches!(self.def_kind(def_id), DefKind::AssocTy) && matches!(self.def_kind(self.parent(def_id)), DefKind::Impl { of_trait: false }); - let is_inherent_assoc_type_const = matches!(self.def_kind(def_id), DefKind::AssocConst) - && matches!(self.def_kind(self.parent(def_id)), DefKind::Impl { of_trait: false }) - && self.is_type_const(def_id); + let is_inherent_assoc_type_const = + matches!(self.def_kind(def_id), DefKind::AssocConst { is_type_const: true }) + && matches!(self.def_kind(self.parent(def_id)), DefKind::Impl { of_trait: false }); let own_args = if !nested && (is_inherent_assoc_ty || is_inherent_assoc_type_const) { if generics.own_params.len() + 1 != args.len() { return false; @@ -2198,9 +2198,12 @@ impl<'tcx> TyCtxt<'tcx> { if cfg!(debug_assertions) && !self.check_args_compatible(def_id, args) { let is_inherent_assoc_ty = matches!(self.def_kind(def_id), DefKind::AssocTy) && matches!(self.def_kind(self.parent(def_id)), DefKind::Impl { of_trait: false }); - let is_inherent_assoc_type_const = matches!(self.def_kind(def_id), DefKind::AssocConst) - && matches!(self.def_kind(self.parent(def_id)), DefKind::Impl { of_trait: false }) - && self.is_type_const(def_id); + let is_inherent_assoc_type_const = + matches!(self.def_kind(def_id), DefKind::AssocConst { is_type_const: true }) + && matches!( + self.def_kind(self.parent(def_id)), + DefKind::Impl { of_trait: false } + ); if is_inherent_assoc_ty || is_inherent_assoc_type_const { bug!( "args not compatible with generics for {}: args={:#?}, generics={:#?}", diff --git a/compiler/rustc_middle/src/ty/context/impl_interner.rs b/compiler/rustc_middle/src/ty/context/impl_interner.rs index a4aeaacbf05db..083eb6c482400 100644 --- a/compiler/rustc_middle/src/ty/context/impl_interner.rs +++ b/compiler/rustc_middle/src/ty/context/impl_interner.rs @@ -214,7 +214,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { ty::AliasTermKind::ProjectionTy } } - DefKind::AssocConst => { + DefKind::AssocConst { .. } => { if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id)) { ty::AliasTermKind::InherentConst @@ -224,7 +224,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { } DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy, DefKind::TyAlias => ty::AliasTermKind::FreeTy, - DefKind::Const => ty::AliasTermKind::FreeConst, + DefKind::Const { .. } => ty::AliasTermKind::FreeConst, DefKind::AnonConst | DefKind::Ctor(_, CtorKind::Const) => { ty::AliasTermKind::UnevaluatedConst } @@ -237,7 +237,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> { def_id: DefId, args: ty::GenericArgsRef<'tcx>, ) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) { - debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst); + debug_assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst { .. }); let trait_def_id = self.parent(def_id); debug_assert_matches!(self.def_kind(trait_def_id), DefKind::Trait); let trait_ref = ty::TraitRef::from_assoc(self, trait_def_id, args); diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index 2444046c604b8..3e9e800ca4bac 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -508,8 +508,8 @@ impl<'tcx> Instance<'tcx> { tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn - | DefKind::Const - | DefKind::AssocConst + | DefKind::Const { .. } + | DefKind::AssocConst { .. } | DefKind::AnonConst | DefKind::InlineConst | DefKind::Static { .. } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 8511f227c6699..d576b749f5bca 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1577,7 +1577,9 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn opt_associated_item(self, def_id: DefId) -> Option { - if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) { + if let DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy = + self.def_kind(def_id) + { Some(self.associated_item(def_id)) } else { None @@ -1679,9 +1681,9 @@ impl<'tcx> TyCtxt<'tcx> { let def_kind = self.def_kind(def); debug!("returned from def_kind: {:?}", def_kind); match def_kind { - DefKind::Const + DefKind::Const { .. } | DefKind::Static { .. } - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Ctor(..) | DefKind::AnonConst | DefKind::InlineConst => self.mir_for_ctfe(def), @@ -2128,10 +2130,10 @@ impl<'tcx> TyCtxt<'tcx> { | DefKind::TyAlias | DefKind::ForeignTy | DefKind::TyParam - | DefKind::Const + | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Macro(_) | DefKind::ExternCrate | DefKind::Use diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 02b804c1ab29c..e8608a89b083c 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -404,7 +404,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { return Ok(true); } if let Some(symbol) = key.get_opt_name() { - if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = kind + if let DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy = kind && let Some(parent) = self.tcx().opt_parent(def_id) && let parent_key = self.tcx().def_key(parent) && let Some(symbol) = parent_key.get_opt_name() @@ -429,7 +429,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { | DefKind::Trait | DefKind::TyAlias | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { .. } = kind { } else { @@ -1541,7 +1541,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { match ct.kind() { ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args }) => { match self.tcx().def_kind(def) { - DefKind::Const | DefKind::AssocConst => { + DefKind::Const { .. } | DefKind::AssocConst { .. } => { self.pretty_print_value_path(def, args)?; } DefKind::AnonConst => { diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 4be30d8b6c918..cd97c34e4b862 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -613,12 +613,12 @@ impl<'tcx> Ty<'tcx> { | DefKind::AssocTy | DefKind::TyParam | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } | DefKind::Ctor(..) | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Macro(..) | DefKind::ExternCrate | DefKind::Use diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index c29f3e7458e54..c620b67ad26d8 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -165,7 +165,7 @@ impl<'tcx> TyCtxt<'tcx> { | DefKind::AssocTy | DefKind::Fn | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Impl { .. }, def_id, ) => Some(def_id), diff --git a/compiler/rustc_mir_build/src/builder/mod.rs b/compiler/rustc_mir_build/src/builder/mod.rs index dbd7809c2e651..b03b61ce5f09c 100644 --- a/compiler/rustc_mir_build/src/builder/mod.rs +++ b/compiler/rustc_mir_build/src/builder/mod.rs @@ -618,8 +618,8 @@ fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) - let hir_id = tcx.local_def_id_to_hir_id(def_id); let (inputs, output, coroutine) = match tcx.def_kind(def_id) { - DefKind::Const - | DefKind::AssocConst + DefKind::Const { .. } + | DefKind::AssocConst { .. } | DefKind::AnonConst | DefKind::InlineConst | DefKind::Static { .. } diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 3d94ee701f564..b63feb022ff0c 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -1117,8 +1117,8 @@ impl<'tcx> ThirBuildCx<'tcx> { Res::Def(DefKind::Fn, _) | Res::Def(DefKind::AssocFn, _) | Res::Def(DefKind::Ctor(_, CtorKind::Fn), _) - | Res::Def(DefKind::Const, _) - | Res::Def(DefKind::AssocConst, _) => { + | Res::Def(DefKind::Const { .. }, _) + | Res::Def(DefKind::AssocConst { .. }, _) => { self.typeck_results.user_provided_types().get(hir_id).copied().map(Box::new) } @@ -1207,7 +1207,8 @@ impl<'tcx> ThirBuildCx<'tcx> { ExprKind::ConstParam { param, def_id } } - Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => { + Res::Def(DefKind::Const { .. }, def_id) + | Res::Def(DefKind::AssocConst { .. }, def_id) => { let user_ty = self.user_args_applied_to_res(expr.hir_id, res); ExprKind::NamedConst { def_id, args, user_ty } } diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 9231b425c4c37..f4645604e6efd 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -957,7 +957,7 @@ fn find_fallback_pattern_typo<'tcx>( continue; }; if let Some(value_ns) = path.res.value_ns - && let Res::Def(DefKind::Const, id) = value_ns + && let Res::Def(DefKind::Const { .. }, id) = value_ns && infcx.can_eq(param_env, ty, cx.tcx.type_of(id).instantiate_identity()) { if cx.tcx.visibility(id).is_accessible_from(parent, cx.tcx) { @@ -974,7 +974,7 @@ fn find_fallback_pattern_typo<'tcx>( } } } - if let DefKind::Const = cx.tcx.def_kind(item.owner_id) + if let DefKind::Const { .. } = cx.tcx.def_kind(item.owner_id) && infcx.can_eq(param_env, ty, cx.tcx.type_of(item.owner_id).instantiate_identity()) { // Look for local consts. @@ -1114,7 +1114,7 @@ fn is_const_pat_that_looks_like_binding<'tcx>(tcx: TyCtxt<'tcx>, pat: &Pat<'tcx> // the pattern's source text must resemble a plain identifier without any // `::` namespace separators or other non-identifier characters. if let Some(def_id) = try { pat.extra.as_deref()?.expanded_const? } - && matches!(tcx.def_kind(def_id), DefKind::Const) + && matches!(tcx.def_kind(def_id), DefKind::Const { .. }) && let Ok(snippet) = tcx.sess.source_map().span_to_snippet(pat.span) && snippet.chars().all(|c| c.is_alphanumeric() || c == '_') { diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index ae24605c39b43..ff9d456f7e708 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -74,13 +74,14 @@ impl<'tcx> ConstToPat<'tcx> { fn mk_err(&self, mut err: Diag<'_>, ty: Ty<'tcx>) -> Box> { if let ty::ConstKind::Unevaluated(uv) = self.c.kind() { let def_kind = self.tcx.def_kind(uv.def); - if let hir::def::DefKind::AssocConst = def_kind + if let hir::def::DefKind::AssocConst { .. } = def_kind && let Some(def_id) = uv.def.as_local() { // Include the container item in the output. err.span_label(self.tcx.def_span(self.tcx.local_parent(def_id)), ""); } - if let hir::def::DefKind::Const | hir::def::DefKind::AssocConst = def_kind { + if let hir::def::DefKind::Const { .. } | hir::def::DefKind::AssocConst { .. } = def_kind + { err.span_label(self.tcx.def_span(uv.def), msg!("constant defined here")); } } @@ -116,7 +117,7 @@ impl<'tcx> ConstToPat<'tcx> { // We've emitted an error on the original const, it would be redundant to complain // on its use as well. if let ty::ConstKind::Unevaluated(uv) = self.c.kind() - && let hir::def::DefKind::Const | hir::def::DefKind::AssocConst = + && let hir::def::DefKind::Const { .. } | hir::def::DefKind::AssocConst { .. } = self.tcx.def_kind(uv.def) { err.downgrade_to_delayed_bug(); diff --git a/compiler/rustc_mir_build/src/thir/pattern/mod.rs b/compiler/rustc_mir_build/src/thir/pattern/mod.rs index acf20cb092e22..0bb08d64fb0f6 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/mod.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/mod.rs @@ -635,7 +635,8 @@ impl<'tcx> PatCtxt<'tcx> { let res = self.typeck_results.qpath_res(qpath, id); let (def_id, user_ty) = match res { - Res::Def(DefKind::Const, def_id) | Res::Def(DefKind::AssocConst, def_id) => { + Res::Def(DefKind::Const { .. }, def_id) + | Res::Def(DefKind::AssocConst { .. }, def_id) => { (def_id, self.typeck_results.user_provided_types().get(id)) } diff --git a/compiler/rustc_mir_transform/src/known_panics_lint.rs b/compiler/rustc_mir_transform/src/known_panics_lint.rs index 873f6a01ec360..c4994417ed7ea 100644 --- a/compiler/rustc_mir_transform/src/known_panics_lint.rs +++ b/compiler/rustc_mir_transform/src/known_panics_lint.rs @@ -35,7 +35,7 @@ impl<'tcx> crate::MirLint<'tcx> for KnownPanicsLint { let def_id = body.source.def_id().expect_local(); let def_kind = tcx.def_kind(def_id); let is_fn_like = def_kind.is_fn_like(); - let is_assoc_const = def_kind == DefKind::AssocConst; + let is_assoc_const = matches!(def_kind, DefKind::AssocConst { .. }); // Only run const prop on functions, methods, closures and associated constants if !is_fn_like && !is_assoc_const { diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 7dd198ed9f9cc..92966bcb4795e 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -444,8 +444,8 @@ fn mir_promoted( { tcx.mir_const_qualif(def) } - DefKind::AssocConst - | DefKind::Const + DefKind::AssocConst { .. } + | DefKind::Const { .. } | DefKind::Static { .. } | DefKind::InlineConst | DefKind::AnonConst => tcx.mir_const_qualif(def), @@ -562,8 +562,8 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> & DefKind::Fn | DefKind::AssocFn | DefKind::Static { .. } - | DefKind::Const - | DefKind::AssocConst => { + | DefKind::Const { .. } + | DefKind::AssocConst { .. } => { if let Err(guar) = tcx.ensure_ok().check_well_formed(root.expect_local()) { body.tainted_by_errors = Some(guar); } diff --git a/compiler/rustc_mir_transform/src/liveness.rs b/compiler/rustc_mir_transform/src/liveness.rs index 3fbe1e398a181..af246735b95a7 100644 --- a/compiler/rustc_mir_transform/src/liveness.rs +++ b/compiler/rustc_mir_transform/src/liveness.rs @@ -201,7 +201,7 @@ fn maybe_suggest_unit_pattern_typo<'tcx>( let constants = tcx .hir_body_owners() .filter(|&def_id| { - matches!(tcx.def_kind(def_id), DefKind::Const) + matches!(tcx.def_kind(def_id), DefKind::Const { .. }) && tcx.type_of(def_id).instantiate_identity() == ty && tcx.visibility(def_id).is_accessible_from(body_def_id, tcx) }) diff --git a/compiler/rustc_mir_transform/src/trivial_const.rs b/compiler/rustc_mir_transform/src/trivial_const.rs index 3b80ae30be42d..529c8c65acfc0 100644 --- a/compiler/rustc_mir_transform/src/trivial_const.rs +++ b/compiler/rustc_mir_transform/src/trivial_const.rs @@ -52,7 +52,10 @@ where F: FnOnce() -> B, B: Deref>, { - if !matches!(tcx.def_kind(def), DefKind::AssocConst | DefKind::Const | DefKind::AnonConst) { + if !matches!( + tcx.def_kind(def), + DefKind::AssocConst { .. } | DefKind::Const { .. } | DefKind::AnonConst + ) { return None; } diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 4f6e2cc005160..3aa55cc8eb9fb 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -1559,7 +1559,7 @@ impl<'v> RootCollector<'_, 'v> { debug!("RootCollector: ItemKind::Static({})", self.tcx.def_path_str(def_id)); self.output.push(dummy_spanned(MonoItem::Static(def_id))); } - DefKind::Const => { + DefKind::Const { .. } => { // Const items only generate mono items if they are actually used somewhere. // Just declaring them is insufficient. diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index f6d00f5342f27..15c91deef247d 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -43,10 +43,10 @@ fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { | DefKind::TraitAlias | DefKind::AssocTy | DefKind::Fn - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { .. } | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::Macro(_) | DefKind::GlobalAsm | DefKind::Impl { .. } @@ -120,7 +120,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { fn handle_res(&mut self, res: Res) { match res { Res::Def( - DefKind::Const | DefKind::AssocConst | DefKind::AssocTy | DefKind::TyAlias, + DefKind::Const { .. } + | DefKind::AssocConst { .. } + | DefKind::AssocTy + | DefKind::TyAlias, def_id, ) => { self.check_def_id(def_id); @@ -485,7 +488,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> { fn check_impl_or_impl_item_live(&mut self, local_def_id: LocalDefId) -> bool { let (impl_block_id, trait_def_id) = match self.tcx.def_kind(local_def_id) { // assoc impl items of traits are live if the corresponding trait items are live - DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => { + DefKind::AssocConst { .. } | DefKind::AssocTy | DefKind::AssocFn => { let trait_item_id = self.tcx.trait_item_of(local_def_id).and_then(|def_id| def_id.as_local()); (self.tcx.local_parent(local_def_id), trait_item_id) @@ -766,7 +769,7 @@ fn maybe_record_as_seed<'tcx>( ); } } - DefKind::AssocFn | DefKind::AssocConst | DefKind::AssocTy => { + DefKind::AssocFn | DefKind::AssocConst { .. } | DefKind::AssocTy => { if allow_dead_code.is_none() { let parent = tcx.local_parent(owner_id.def_id); match tcx.def_kind(parent) { @@ -809,7 +812,7 @@ fn maybe_record_as_seed<'tcx>( // global_asm! is always live. worklist.push((owner_id.def_id, ComesFromAllowExpect::No)); } - DefKind::Const => { + DefKind::Const { .. } => { if tcx.item_name(owner_id.def_id) == kw::Underscore { // `const _` is always live, as that syntax only exists for the side effects // of type checking and evaluating the constant expression, and marking them @@ -1069,7 +1072,7 @@ impl<'tcx> DeadVisitor<'tcx> { let enum_variants_with_same_name = dead_codes .iter() .filter_map(|dead_item| { - if let DefKind::AssocFn | DefKind::AssocConst = + if let DefKind::AssocFn | DefKind::AssocConst { .. } = tcx.def_kind(dead_item.def_id) && let impl_did = tcx.local_parent(dead_item.def_id) && let DefKind::Impl { of_trait: false } = tcx.def_kind(impl_did) @@ -1142,12 +1145,12 @@ impl<'tcx> DeadVisitor<'tcx> { return; } match self.tcx.def_kind(def_id) { - DefKind::AssocConst + DefKind::AssocConst { .. } | DefKind::AssocTy | DefKind::AssocFn | DefKind::Fn | DefKind::Static { .. } - | DefKind::Const + | DefKind::Const { .. } | DefKind::TyAlias | DefKind::Enum | DefKind::Union diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 225487cc989f0..f72899a66cae0 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -360,7 +360,7 @@ impl<'tcx> ReachableContext<'tcx> { } // Reachable constants and reachable statics can have their contents inlined // into other crates. Mark them as reachable and recurse into their body. - DefKind::Const | DefKind::AssocConst | DefKind::Static { .. } => { + DefKind::Const { .. } | DefKind::AssocConst { .. } | DefKind::Static { .. } => { self.worklist.push(def_id); } _ => { diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index 6fd28c78740cd..3138914a6a95f 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -52,7 +52,7 @@ fn inherit_deprecation(def_kind: DefKind) -> bool { fn inherit_const_stability(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool { let def_kind = tcx.def_kind(def_id); match def_kind { - DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => { + DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst { .. } => { match tcx.def_kind(tcx.local_parent(def_id)) { DefKind::Impl { .. } => true, _ => false, @@ -84,7 +84,7 @@ fn annotation_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> AnnotationKind { } // Impl items in trait impls cannot have stability. - DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst => { + DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst { .. } => { match tcx.def_kind(tcx.local_parent(def_id)) { DefKind::Impl { of_trait: true } => AnnotationKind::Prohibited, _ => AnnotationKind::Required, diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index 59fd908756b02..287e8579e080a 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -574,7 +574,10 @@ impl<'tcx> EmbargoVisitor<'tcx> { self.update(def_id, macro_ev, Level::Reachable); match def_kind { // No type privacy, so can be directly marked as reachable. - DefKind::Const | DefKind::Static { .. } | DefKind::TraitAlias | DefKind::TyAlias => { + DefKind::Const { .. } + | DefKind::Static { .. } + | DefKind::TraitAlias + | DefKind::TyAlias => { if vis.is_accessible_from(module, self.tcx) { self.update(def_id, macro_ev, Level::Reachable); } @@ -621,7 +624,7 @@ impl<'tcx> EmbargoVisitor<'tcx> { // These have type privacy, so are not reachable unless they're // public, or are not namespaced at all. - DefKind::AssocConst + DefKind::AssocConst { .. } | DefKind::AssocTy | DefKind::ConstParam | DefKind::Ctor(_, _) @@ -679,7 +682,7 @@ impl<'tcx> EmbargoVisitor<'tcx> { } } DefKind::ForeignTy - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { .. } | DefKind::Fn | DefKind::TyAlias => { @@ -801,7 +804,7 @@ impl<'tcx> EmbargoVisitor<'tcx> { | DefKind::Variant | DefKind::AssocFn | DefKind::AssocTy - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::TyParam | DefKind::AnonConst | DefKind::InlineConst @@ -1287,7 +1290,10 @@ impl<'tcx> Visitor<'tcx> for TypePrivacyVisitor<'tcx> { let def = def.filter(|(kind, _)| { matches!( kind, - DefKind::AssocFn | DefKind::AssocConst | DefKind::AssocTy | DefKind::Static { .. } + DefKind::AssocFn + | DefKind::AssocConst { .. } + | DefKind::AssocTy + | DefKind::Static { .. } ) }); if let Some((kind, def_id)) = def { @@ -1613,7 +1619,7 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'_, 'tcx> { let def_kind = tcx.def_kind(def_id); match def_kind { - DefKind::Const | DefKind::Static { .. } | DefKind::Fn | DefKind::TyAlias => { + DefKind::Const { .. } | DefKind::Static { .. } | DefKind::Fn | DefKind::TyAlias => { if let DefKind::TyAlias = def_kind { self.check_unnameable(def_id, effective_vis); } diff --git a/compiler/rustc_public/src/unstable/convert/stable/ty.rs b/compiler/rustc_public/src/unstable/convert/stable/ty.rs index 70cdae43126c4..4bf98c4f0731f 100644 --- a/compiler/rustc_public/src/unstable/convert/stable/ty.rs +++ b/compiler/rustc_public/src/unstable/convert/stable/ty.rs @@ -1054,7 +1054,7 @@ impl<'tcx> Stable<'tcx> for ty::AssocKind { ) -> Self::T { use crate::ty::{AssocKind, AssocTypeData}; match *self { - ty::AssocKind::Const { name } => AssocKind::Const { name: name.to_string() }, + ty::AssocKind::Const { name, .. } => AssocKind::Const { name: name.to_string() }, ty::AssocKind::Fn { name, has_self } => { AssocKind::Fn { name: name.to_string(), has_self } } diff --git a/compiler/rustc_public/src/unstable/mod.rs b/compiler/rustc_public/src/unstable/mod.rs index 583493c23d66f..fa5ef5a9ca6d6 100644 --- a/compiler/rustc_public/src/unstable/mod.rs +++ b/compiler/rustc_public/src/unstable/mod.rs @@ -121,9 +121,10 @@ pub(crate) fn new_item_kind(kind: DefKind) -> ItemKind { DefKind::Closure | DefKind::AssocFn | DefKind::Fn | DefKind::SyntheticCoroutineBody => { ItemKind::Fn } - DefKind::Const | DefKind::InlineConst | DefKind::AssocConst | DefKind::AnonConst => { - ItemKind::Const - } + DefKind::Const { .. } + | DefKind::InlineConst + | DefKind::AssocConst { .. } + | DefKind::AnonConst => ItemKind::Const, DefKind::Static { .. } => ItemKind::Static, DefKind::Ctor(_, rustc_hir::def::CtorKind::Const) => ItemKind::Ctor(CtorKind::Const), DefKind::Ctor(_, rustc_hir::def::CtorKind::Fn) => ItemKind::Ctor(CtorKind::Fn), diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index c4a0cde2837a0..f9c3f470e9bb8 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -331,8 +331,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { DefKind::Fn | DefKind::AssocFn | DefKind::Static { .. } - | DefKind::Const - | DefKind::AssocConst + | DefKind::Const { .. } + | DefKind::AssocConst { .. } | DefKind::Ctor(..), _, ) => define_extern(ValueNS), diff --git a/compiler/rustc_resolve/src/def_collector.rs b/compiler/rustc_resolve/src/def_collector.rs index e2fea9146c798..81820e049716b 100644 --- a/compiler/rustc_resolve/src/def_collector.rs +++ b/compiler/rustc_resolve/src/def_collector.rs @@ -131,7 +131,11 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { mutability: s.mutability, nested: false, }, - ItemKind::Const(..) | ItemKind::ConstBlock(..) => DefKind::Const, + ItemKind::Const(citem) => { + let is_type_const = matches!(citem.rhs_kind, ConstItemRhsKind::TypeConst { .. }); + DefKind::Const { is_type_const } + } + ItemKind::ConstBlock(..) => DefKind::Const { is_type_const: false }, ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn, ItemKind::MacroDef(ident, def) => { let edition = i.span.edition(); @@ -347,7 +351,12 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> { let (ident, def_kind) = match &i.kind { AssocItemKind::Fn(box Fn { ident, .. }) | AssocItemKind::Delegation(box Delegation { ident, .. }) => (*ident, DefKind::AssocFn), - AssocItemKind::Const(box ConstItem { ident, .. }) => (*ident, DefKind::AssocConst), + AssocItemKind::Const(box ConstItem { ident, rhs_kind, .. }) => ( + *ident, + DefKind::AssocConst { + is_type_const: matches!(rhs_kind, ConstItemRhsKind::TypeConst { .. }), + }, + ), AssocItemKind::Type(box TyAlias { ident, .. }) => (*ident, DefKind::AssocTy), AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => { return self.visit_macro_invoc(i.id); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index b01afe550e74f..bc8e438389ed2 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -557,7 +557,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { DefKind::Static { .. } => { Some(errs::GenericParamsFromOuterItemStaticOrConst::Static) } - DefKind::Const => Some(errs::GenericParamsFromOuterItemStaticOrConst::Const), + DefKind::Const { .. } => { + Some(errs::GenericParamsFromOuterItemStaticOrConst::Const) + } _ => None, }; let is_self = @@ -720,8 +722,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { Res::Def( DefKind::Ctor(CtorOf::Variant, CtorKind::Const) | DefKind::Ctor(CtorOf::Struct, CtorKind::Const) - | DefKind::Const - | DefKind::AssocConst, + | DefKind::Const { .. } + | DefKind::AssocConst { .. }, _, ) ) @@ -729,11 +731,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { ); if import_suggestions.is_empty() && !suggested_typo { - let kinds = [ - DefKind::Ctor(CtorOf::Variant, CtorKind::Const), - DefKind::Ctor(CtorOf::Struct, CtorKind::Const), - DefKind::Const, - DefKind::AssocConst, + let kind_matches: [fn(DefKind) -> bool; 4] = [ + |kind| matches!(kind, DefKind::Ctor(CtorOf::Variant, CtorKind::Const)), + |kind| matches!(kind, DefKind::Ctor(CtorOf::Struct, CtorKind::Const)), + |kind| matches!(kind, DefKind::Const { .. }), + |kind| matches!(kind, DefKind::AssocConst { .. }), ]; let mut local_names = vec![]; self.add_module_candidates( @@ -752,13 +754,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { let mut local_suggestions = vec![]; let mut suggestions = vec![]; - for kind in kinds { + for matches_kind in kind_matches { if let Some(suggestion) = self.early_lookup_typo_candidate( ScopeSet::All(Namespace::ValueNS), &parent_scope, name, &|res: Res| match res { - Res::Def(k, _) => k == kind, + Res::Def(k, _) => matches_kind(k), _ => false, }, ) && let Res::Def(kind, mut def_id) = suggestion.res diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index f72abaa37201e..3e9476b124f67 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -577,11 +577,11 @@ impl PathSource<'_, '_, '_> { res, Res::Def( DefKind::Ctor(_, CtorKind::Const | CtorKind::Fn) - | DefKind::Const + | DefKind::Const { .. } | DefKind::Static { .. } | DefKind::Fn | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::ConstParam, _, ) | Res::Local(..) @@ -589,7 +589,10 @@ impl PathSource<'_, '_, '_> { ), PathSource::Pat => { res.expected_in_unit_struct_pat() - || matches!(res, Res::Def(DefKind::Const | DefKind::AssocConst, _)) + || matches!( + res, + Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, _) + ) } PathSource::TupleStruct(..) => res.expected_in_tuple_struct_pat(), PathSource::Struct(_) => matches!( @@ -605,7 +608,7 @@ impl PathSource<'_, '_, '_> { | Res::SelfTyAlias { .. } ), PathSource::TraitItem(ns, _) => match res { - Res::Def(DefKind::AssocConst | DefKind::AssocFn, _) if ns == ValueNS => true, + Res::Def(DefKind::AssocConst { .. } | DefKind::AssocFn, _) if ns == ValueNS => true, Res::Def(DefKind::AssocTy, _) if ns == TypeNS => true, _ => false, }, @@ -3739,7 +3742,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { match (def_kind, kind) { (DefKind::AssocTy, AssocItemKind::Type(..)) | (DefKind::AssocFn, AssocItemKind::Fn(..)) - | (DefKind::AssocConst, AssocItemKind::Const(..)) + | (DefKind::AssocConst { .. }, AssocItemKind::Const(..)) | (DefKind::AssocFn, AssocItemKind::Delegation(..)) => { self.r.record_partial_res(id, PartialRes::new(res)); return; @@ -4321,7 +4324,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { match res { Res::SelfCtor(_) // See #70549. | Res::Def( - DefKind::Ctor(_, CtorKind::Const) | DefKind::Const | DefKind::AssocConst | DefKind::ConstParam, + DefKind::Ctor(_, CtorKind::Const) | DefKind::Const { .. } | DefKind::AssocConst { .. } | DefKind::ConstParam, _, ) if is_syntactic_ambiguity => { // Disambiguate in favor of a unit struct/variant or constant pattern. @@ -4330,7 +4333,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { } Some(res) } - Res::Def(DefKind::Ctor(..) | DefKind::Const | DefKind::AssocConst | DefKind::Static { .. }, _) => { + Res::Def(DefKind::Ctor(..) | DefKind::Const { .. } | DefKind::AssocConst { .. } | DefKind::Static { .. }, _) => { // This is unambiguously a fresh binding, either syntactically // (e.g., `IDENT @ PAT` or `ref IDENT`) or because `IDENT` resolves // to something unusable as a pattern (e.g., constructor function), @@ -4351,7 +4354,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { None } Res::Def(DefKind::ConstParam, def_id) => { - // Same as for DefKind::Const above, but here, `binding` is `None`, so we + // Same as for DefKind::Const { .. } above, but here, `binding` is `None`, so we // have to construct the error differently self.report_error( ident.span, diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 9f5196d894893..e25e5b7b0bb2e 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -414,7 +414,10 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { .is_ok_and(|snippet| snippet.ends_with(')')) } Res::Def( - DefKind::Ctor(..) | DefKind::AssocFn | DefKind::Const | DefKind::AssocConst, + DefKind::Ctor(..) + | DefKind::AssocFn + | DefKind::Const { .. } + | DefKind::AssocConst { .. }, _, ) | Res::SelfCtor(_) @@ -2732,7 +2735,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { .iter() .filter_map(|(key, res)| res.borrow().best_decl().map(|binding| (key, binding.res()))) .filter(|(_, res)| match (kind, res) { - (AssocItemKind::Const(..), Res::Def(DefKind::AssocConst, _)) => true, + (AssocItemKind::Const(..), Res::Def(DefKind::AssocConst { .. }, _)) => true, (AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true, (AssocItemKind::Type(..), Res::Def(DefKind::AssocTy, _)) => true, (AssocItemKind::Delegation(_), Res::Def(DefKind::AssocFn, _)) => true, @@ -2840,7 +2843,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { return Some(AssocSuggestion::AssocFn { called }); } } - Res::Def(DefKind::AssocConst, _) => { + Res::Def(DefKind::AssocConst { .. }, _) => { return Some(AssocSuggestion::AssocConst); } Res::Def(DefKind::AssocTy, _) => { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 1368e5deb6487..a5addc35d3fa5 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1051,7 +1051,10 @@ impl<'ra> DeclData<'ra> { } fn is_assoc_item(&self) -> bool { - matches!(self.res(), Res::Def(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy, _)) + matches!( + self.res(), + Res::Def(DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy, _) + ) } fn macro_kinds(&self) -> Option { diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs index ab19df5620cdb..adb2d2c185cac 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs @@ -357,9 +357,9 @@ impl Trait for X { tcx.def_kind(body_owner_def_id), DefKind::Fn | DefKind::Static { .. } - | DefKind::Const + | DefKind::Const { .. } | DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } ) && matches!( tcx.opaque_ty_origin(opaque_ty.def_id), diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 077b8a6cd7f26..a5d33c7f61a59 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -745,7 +745,7 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a, 'tcx> { ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)) if matches!( infcx.tcx.def_kind(proj.projection_term.def_id), - DefKind::AssocTy | DefKind::AssocConst + DefKind::AssocTy | DefKind::AssocConst { .. } ) => { proj.projection_term.trait_ref(infcx.tcx) diff --git a/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs b/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs index 489b2d160a51c..f1ab59abc6aef 100644 --- a/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs +++ b/compiler/rustc_trait_selection/src/traits/dyn_compatibility.rs @@ -327,7 +327,7 @@ pub fn dyn_compatibility_violations_for_assoc_item( let span = || item.ident(tcx).span; match item.kind { - ty::AssocKind::Const { name } => { + ty::AssocKind::Const { name, is_type_const } => { // We will permit type associated consts if they are explicitly mentioned in the // trait object type. We can't check this here, as here we only check if it is // guaranteed to not be possible. @@ -337,7 +337,7 @@ pub fn dyn_compatibility_violations_for_assoc_item( if tcx.features().min_generic_const_args() { if !tcx.generics_of(item.def_id).is_own_empty() { errors.push(AssocConstViolation::Generic); - } else if !tcx.is_type_const(item.def_id) { + } else if !is_type_const { errors.push(AssocConstViolation::NonType); } diff --git a/compiler/rustc_trait_selection/src/traits/fulfill.rs b/compiler/rustc_trait_selection/src/traits/fulfill.rs index 681f015c17990..71a8e081146e9 100644 --- a/compiler/rustc_trait_selection/src/traits/fulfill.rs +++ b/compiler/rustc_trait_selection/src/traits/fulfill.rs @@ -685,7 +685,11 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> { use rustc_hir::def::DefKind; match (c1.kind(), c2.kind()) { (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) - if a.def == b.def && tcx.def_kind(a.def) == DefKind::AssocConst => + if a.def == b.def + && matches!( + tcx.def_kind(a.def), + DefKind::AssocConst { .. } + ) => { if let Ok(new_obligations) = infcx .at(&obligation.cause, obligation.param_env) diff --git a/compiler/rustc_trait_selection/src/traits/normalize.rs b/compiler/rustc_trait_selection/src/traits/normalize.rs index 1475e5f5f2433..19a80893e898d 100644 --- a/compiler/rustc_trait_selection/src/traits/normalize.rs +++ b/compiler/rustc_trait_selection/src/traits/normalize.rs @@ -456,7 +456,7 @@ impl<'a, 'b, 'tcx> TypeFolder> for AssocTypeNormalizer<'a, 'b, 'tcx // if it was marked with `type const`. Using this attribute without the mgca // feature gate causes a parse error. let ct = match tcx.def_kind(uv.def) { - DefKind::AssocConst => match tcx.def_kind(tcx.parent(uv.def)) { + DefKind::AssocConst { .. } => match tcx.def_kind(tcx.parent(uv.def)) { DefKind::Trait => self.normalize_trait_projection(uv.into()).expect_const(), DefKind::Impl { of_trait: false } => { self.normalize_inherent_projection(uv.into()).expect_const() @@ -466,7 +466,7 @@ impl<'a, 'b, 'tcx> TypeFolder> for AssocTypeNormalizer<'a, 'b, 'tcx kind ), }, - DefKind::Const => self.normalize_free_alias(uv.into()).expect_const(), + DefKind::Const { .. } => self.normalize_free_alias(uv.into()).expect_const(), DefKind::AnonConst => { let ct = ct.super_fold_with(self); super::with_replaced_escaping_bound_vars( diff --git a/compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs b/compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs index 91a0251318550..c4353fe25b85b 100644 --- a/compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs +++ b/compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs @@ -99,7 +99,7 @@ fn relate_mir_and_user_args<'tcx>( // For IACs, the user args are in the format [SelfTy, GAT_args...] but type_of expects [impl_args..., GAT_args...]. // We need to infer the impl args by equating the impl's self type with the user-provided self type. - let is_inherent_assoc_const = tcx.def_kind(def_id) == DefKind::AssocConst + let is_inherent_assoc_const = matches!(tcx.def_kind(def_id), DefKind::AssocConst { .. }) && tcx.def_kind(tcx.parent(def_id)) == DefKind::Impl { of_trait: false } && tcx.is_type_const(def_id); diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 29a300b041293..120333c6cfbb3 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -877,7 +877,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { use rustc_hir::def::DefKind; match (c1.kind(), c2.kind()) { (ty::ConstKind::Unevaluated(a), ty::ConstKind::Unevaluated(b)) - if a.def == b.def && tcx.def_kind(a.def) == DefKind::AssocConst => + if a.def == b.def + && matches!( + tcx.def_kind(a.def), + DefKind::AssocConst { .. } + ) => { if let Ok(InferOk { obligations, value: () }) = self .infcx diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index 358f14221a0dc..91e002137941f 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -1028,7 +1028,7 @@ impl<'a, 'tcx> TypeVisitor> for WfPredicates<'a, 'tcx> { predicate, )); - if tcx.def_kind(uv.def) == DefKind::AssocConst + if matches!(tcx.def_kind(uv.def), DefKind::AssocConst { .. }) && tcx.def_kind(tcx.parent(uv.def)) == (DefKind::Impl { of_trait: false }) { self.add_wf_preds_for_inherent_projection(uv.into()); diff --git a/compiler/rustc_ty_utils/src/assoc.rs b/compiler/rustc_ty_utils/src/assoc.rs index 84f52e7fc9d26..497f9205eb19d 100644 --- a/compiler/rustc_ty_utils/src/assoc.rs +++ b/compiler/rustc_ty_utils/src/assoc.rs @@ -2,7 +2,7 @@ use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId}; use rustc_hir::definitions::{DefPathData, DisambiguatorState}; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::{self as hir, ImplItemImplKind, ItemKind}; +use rustc_hir::{self as hir, ConstItemRhs, ImplItemImplKind, ItemKind}; use rustc_middle::query::Providers; use rustc_middle::ty::{self, ImplTraitInTraitData, TyCtxt}; use rustc_middle::{bug, span_bug}; @@ -88,7 +88,9 @@ fn associated_item_from_trait_item( let owner_id = trait_item.owner_id; let name = trait_item.ident.name; let kind = match trait_item.kind { - hir::TraitItemKind::Const { .. } => ty::AssocKind::Const { name }, + hir::TraitItemKind::Const(_, _, is_type_const) => { + ty::AssocKind::Const { name, is_type_const: is_type_const.into() } + } hir::TraitItemKind::Fn { .. } => { ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) } } @@ -104,7 +106,9 @@ fn associated_item_from_impl_item(tcx: TyCtxt<'_>, impl_item: &hir::ImplItem<'_> let owner_id = impl_item.owner_id; let name = impl_item.ident.name; let kind = match impl_item.kind { - hir::ImplItemKind::Const { .. } => ty::AssocKind::Const { name }, + hir::ImplItemKind::Const(_, rhs) => { + ty::AssocKind::Const { name, is_type_const: matches!(rhs, ConstItemRhs::TypeConst(_)) } + } hir::ImplItemKind::Fn { .. } => { ty::AssocKind::Fn { name, has_self: fn_has_self_parameter(tcx, owner_id) } } diff --git a/compiler/rustc_ty_utils/src/implied_bounds.rs b/compiler/rustc_ty_utils/src/implied_bounds.rs index 990120db9726b..09e7eb93504c3 100644 --- a/compiler/rustc_ty_utils/src/implied_bounds.rs +++ b/compiler/rustc_ty_utils/src/implied_bounds.rs @@ -123,9 +123,11 @@ fn assumed_wf_types<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx [(Ty<' } } } - DefKind::AssocConst | DefKind::AssocTy => tcx.assumed_wf_types(tcx.local_parent(def_id)), + DefKind::AssocConst { .. } | DefKind::AssocTy => { + tcx.assumed_wf_types(tcx.local_parent(def_id)) + } DefKind::Static { .. } - | DefKind::Const + | DefKind::Const { .. } | DefKind::AnonConst | DefKind::InlineConst | DefKind::Struct diff --git a/compiler/rustc_ty_utils/src/opaque_types.rs b/compiler/rustc_ty_utils/src/opaque_types.rs index 445021801beff..027f26cff6238 100644 --- a/compiler/rustc_ty_utils/src/opaque_types.rs +++ b/compiler/rustc_ty_utils/src/opaque_types.rs @@ -40,7 +40,7 @@ enum CollectionMode { impl<'tcx> OpaqueTypeCollector<'tcx> { fn new(tcx: TyCtxt<'tcx>, item: LocalDefId) -> Self { let mode = match tcx.def_kind(item) { - DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy => { + DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy => { CollectionMode::ImplTraitInAssocTypes } DefKind::TyAlias => CollectionMode::Taits, @@ -302,8 +302,8 @@ fn opaque_types_defined_by<'tcx>( DefKind::AssocFn | DefKind::Fn | DefKind::Static { .. } - | DefKind::Const - | DefKind::AssocConst + | DefKind::Const { .. } + | DefKind::AssocConst { .. } | DefKind::AnonConst => { collector.collect_taits_declared_in_body(); } diff --git a/compiler/rustc_ty_utils/src/sig_types.rs b/compiler/rustc_ty_utils/src/sig_types.rs index 07f379d3f9a8f..796aa2093bd5f 100644 --- a/compiler/rustc_ty_utils/src/sig_types.rs +++ b/compiler/rustc_ty_utils/src/sig_types.rs @@ -44,7 +44,7 @@ pub fn walk_types<'tcx, V: SpannedTypeVisitor<'tcx>>( // Walk over the type behind the alias DefKind::TyAlias { .. } | DefKind::AssocTy | // Walk over the type of the item - DefKind::Static { .. } | DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => { + DefKind::Static { .. } | DefKind::Const { .. } | DefKind::AssocConst { .. } | DefKind::AnonConst => { if let Some(ty) = tcx.hir_node_by_def_id(item).ty() { // If the type of the item uses `_`, we're gonna error out anyway, but // typeck (which type_of invokes below), will call back into opaque_types_defined_by diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index fb0dbac7c41df..09b2bc5dcef1d 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -129,7 +129,7 @@ pub(crate) fn try_inline( clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did))) }) } - Res::Def(DefKind::Const, did) => { + Res::Def(DefKind::Const { .. }, did) => { record_extern_fqn(cx, did, ItemType::Constant); cx.with_param_env(did, |cx| { let ct = build_const_item(cx, did); diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 05272c083e692..2f5f2a3f5a68d 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -504,7 +504,7 @@ pub(crate) fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId { Res::Def( AssocTy | AssocFn - | AssocConst + | AssocConst { .. } | Variant | Fn | TyAlias @@ -514,7 +514,7 @@ pub(crate) fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId { | Union | Mod | ForeignTy - | Const + | Const { .. } | Static { .. } | Macro(..) | TraitAlias, diff --git a/src/librustdoc/formats/item_type.rs b/src/librustdoc/formats/item_type.rs index d3923e3e31166..6830c1ec6560d 100644 --- a/src/librustdoc/formats/item_type.rs +++ b/src/librustdoc/formats/item_type.rs @@ -156,7 +156,7 @@ impl ItemType { DefKind::Enum => Self::Enum, DefKind::Fn => Self::Function, DefKind::Mod => Self::Module, - DefKind::Const => Self::Constant, + DefKind::Const { .. } => Self::Constant, DefKind::Static { .. } => Self::Static, DefKind::Struct => Self::Struct, DefKind::Union => Self::Union, @@ -180,7 +180,7 @@ impl ItemType { } DefKind::Ctor(CtorOf::Struct, _) => Self::Struct, DefKind::Ctor(CtorOf::Variant, _) => Self::Variant, - DefKind::AssocConst => Self::AssocConst, + DefKind::AssocConst { .. } => Self::AssocConst, DefKind::TyParam | DefKind::ConstParam | DefKind::ExternCrate diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 720eb8e5e61df..3648da4822c7d 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -545,7 +545,7 @@ pub(crate) fn href_with_root_path( let tcx = cx.tcx(); let def_kind = tcx.def_kind(original_did); let did = match def_kind { - DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => { + DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst { .. } | DefKind::Variant => { // documented on their parent's page tcx.parent(original_did) } @@ -837,7 +837,7 @@ pub(crate) fn fragment(did: DefId, tcx: TyCtxt<'_>) -> impl Display { fmt::from_fn(move |f| { let def_kind = tcx.def_kind(did); match def_kind { - DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => { + DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst { .. } | DefKind::Variant => { let item_type = ItemType::from_def_id(did, tcx); write!(f, "#{}.{}", item_type.as_str(), tcx.item_name(did)) } diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 1da191c903871..580c6d6b20beb 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -125,9 +125,10 @@ impl Res { DefKind::Trait => "trait", DefKind::Union => "union", DefKind::Mod => "mod", - DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst => { - "const" - } + DefKind::Const { .. } + | DefKind::ConstParam + | DefKind::AssocConst { .. } + | DefKind::AnonConst => "const", DefKind::Static { .. } => "static", DefKind::Field => "field", DefKind::Variant | DefKind::Ctor(..) => "variant", @@ -393,7 +394,10 @@ impl<'tcx> LinkCollector<'_, 'tcx> { if let Some(res) = self.resolve_path(path_str, ns, item_id, module_id) { return Ok(match res { Res::Def( - DefKind::AssocFn | DefKind::AssocConst | DefKind::AssocTy | DefKind::Variant, + DefKind::AssocFn + | DefKind::AssocConst { .. } + | DefKind::AssocTy + | DefKind::Variant, def_id, ) => { vec![(Res::from_def_id(self.cx.tcx, self.cx.tcx.parent(def_id)), Some(def_id))] @@ -490,7 +494,7 @@ fn resolve_self_ty<'tcx>( let self_id = match tcx.def_kind(item_id) { def_kind @ (DefKind::AssocFn - | DefKind::AssocConst + | DefKind::AssocConst { .. } | DefKind::AssocTy | DefKind::Variant | DefKind::Field) => { @@ -1210,7 +1214,7 @@ impl LinkCollector<'_, '_> { let tcx = self.cx.tcx; let def_kind = tcx.def_kind(original_did); let did = match def_kind { - DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => { + DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst { .. } | DefKind::Variant => { // documented on their parent's page tcx.parent(original_did) } @@ -1398,7 +1402,13 @@ impl LinkCollector<'_, '_> { // Disallow e.g. linking to enums with `struct@` debug!("saw kind {kind:?} with disambiguator {disambiguator:?}"); match (kind, disambiguator) { - | (DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst, Some(Disambiguator::Kind(DefKind::Const))) + | ( + DefKind::Const { .. } + | DefKind::ConstParam + | DefKind::AssocConst { .. } + | DefKind::AnonConst, + Some(Disambiguator::Kind(DefKind::Const { .. })), + ) // NOTE: this allows 'method' to mean both normal functions and associated functions // This can't cause ambiguity because both are in the same namespace. | (DefKind::Fn | DefKind::AssocFn, Some(Disambiguator::Kind(DefKind::Fn))) @@ -1721,7 +1731,7 @@ impl Disambiguator { "trait" => Kind(DefKind::Trait), "union" => Kind(DefKind::Union), "module" | "mod" => Kind(DefKind::Mod), - "const" | "constant" => Kind(DefKind::Const), + "const" | "constant" => Kind(DefKind::Const { is_type_const: false }), "static" => Kind(DefKind::Static { mutability: Mutability::Not, nested: false, @@ -2122,11 +2132,11 @@ fn resolution_failure( | Field | Closure | AssocTy - | AssocConst + | AssocConst { .. } | AssocFn | Fn | Macro(_) - | Const + | Const { .. } | ConstParam | ExternCrate | Use diff --git a/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs b/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs index a0de464ff7f81..f1b7d4bdfa932 100644 --- a/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs +++ b/src/tools/clippy/clippy_lints/src/loops/needless_range_loop.rs @@ -308,7 +308,7 @@ impl<'tcx> VarVisitor<'_, 'tcx> { } return false; // no need to walk further *on the variable* }, - Res::Def(DefKind::Static { .. } | DefKind::Const, ..) => { + Res::Def(DefKind::Static { .. } | DefKind::Const { .. }, ..) => { if index_used_directly { self.indexed_directly.insert( seqvar.segments[0].ident.name, diff --git a/src/tools/clippy/clippy_lints/src/loops/same_item_push.rs b/src/tools/clippy/clippy_lints/src/loops/same_item_push.rs index 4135c63d4d7ff..ba309d94608c4 100644 --- a/src/tools/clippy/clippy_lints/src/loops/same_item_push.rs +++ b/src/tools/clippy/clippy_lints/src/loops/same_item_push.rs @@ -82,7 +82,7 @@ pub(super) fn check<'tcx>( ExprKind::Lit(..) => emit_lint(cx, vec, pushed_item, ctxt, msrv), // immutable bindings that are initialized with constant ExprKind::Path(ref path) => { - if let Res::Def(DefKind::Const, ..) = cx.qpath_res(path, init.hir_id) { + if let Res::Def(DefKind::Const { .. }, ..) = cx.qpath_res(path, init.hir_id) { emit_lint(cx, vec, pushed_item, ctxt, msrv); } }, @@ -91,7 +91,7 @@ pub(super) fn check<'tcx>( } }, // constant - Res::Def(DefKind::Const, ..) => emit_lint(cx, vec, pushed_item, ctxt, msrv), + Res::Def(DefKind::Const { .. }, ..) => emit_lint(cx, vec, pushed_item, ctxt, msrv), _ => {}, } }, diff --git a/src/tools/clippy/clippy_lints/src/manual_float_methods.rs b/src/tools/clippy/clippy_lints/src/manual_float_methods.rs index a81c4dc6a7931..8a3acde166c52 100644 --- a/src/tools/clippy/clippy_lints/src/manual_float_methods.rs +++ b/src/tools/clippy/clippy_lints/src/manual_float_methods.rs @@ -121,11 +121,11 @@ fn is_not_const(tcx: TyCtxt<'_>, def_id: DefId) -> bool { DefKind::AnonConst | DefKind::InlineConst - | DefKind::Const + | DefKind::Const { .. } | DefKind::ConstParam | DefKind::Static { .. } | DefKind::Ctor(..) - | DefKind::AssocConst => false, + | DefKind::AssocConst { .. } => false, DefKind::Fn | DefKind::AssocFn | DefKind::Closure => tcx.constness(def_id) == Constness::NotConst, } diff --git a/src/tools/clippy/clippy_lints/src/manual_main_separator_str.rs b/src/tools/clippy/clippy_lints/src/manual_main_separator_str.rs index 93a0ab5b60af0..d4a7c190feca3 100644 --- a/src/tools/clippy/clippy_lints/src/manual_main_separator_str.rs +++ b/src/tools/clippy/clippy_lints/src/manual_main_separator_str.rs @@ -1,8 +1,8 @@ use clippy_config::Conf; use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::msrvs::{self, Msrv}; -use clippy_utils::{peel_hir_expr_refs, sym}; use clippy_utils::res::{MaybeDef, MaybeTypeckRes}; +use clippy_utils::{peel_hir_expr_refs, sym}; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; use rustc_hir::{Expr, ExprKind, Mutability, QPath}; @@ -51,7 +51,7 @@ impl LateLintPass<'_> for ManualMainSeparatorStr { if let ExprKind::MethodCall(path, receiver, &[], _) = target.kind && path.ident.name == sym::to_string && let ExprKind::Path(QPath::Resolved(None, path)) = receiver.kind - && let Res::Def(DefKind::Const, receiver_def_id) = path.res + && let Res::Def(DefKind::Const { .. }, receiver_def_id) = path.res && cx.ty_based_def(target).opt_parent(cx).is_diag_item(cx, sym::ToString) && cx.tcx.is_diagnostic_item(sym::path_main_separator, receiver_def_id) && let ty::Ref(_, ty, Mutability::Not) = cx.typeck_results().expr_ty_adjusted(expr).kind() diff --git a/src/tools/clippy/clippy_lints/src/matches/match_wild_enum.rs b/src/tools/clippy/clippy_lints/src/matches/match_wild_enum.rs index 768d0e8e268cc..6b46791eb117a 100644 --- a/src/tools/clippy/clippy_lints/src/matches/match_wild_enum.rs +++ b/src/tools/clippy/clippy_lints/src/matches/match_wild_enum.rs @@ -68,7 +68,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) { // FIXME(clippy): don't you want to use the hir id of the peeled pat? let id = match cx.qpath_res(path, *hir_id) { Res::Def( - DefKind::Const | DefKind::ConstParam | DefKind::AnonConst | DefKind::InlineConst, + DefKind::Const { .. } | DefKind::ConstParam | DefKind::AnonConst | DefKind::InlineConst, _, ) => return, Res::Def(_, id) => id, diff --git a/src/tools/clippy/clippy_lints/src/non_copy_const.rs b/src/tools/clippy/clippy_lints/src/non_copy_const.rs index 79fcc5decfa15..f591a1be1f80d 100644 --- a/src/tools/clippy/clippy_lints/src/non_copy_const.rs +++ b/src/tools/clippy/clippy_lints/src/non_copy_const.rs @@ -20,10 +20,10 @@ use clippy_config::Conf; use clippy_utils::consts::{ConstEvalCtxt, Constant, const_item_rhs_to_expr}; use clippy_utils::diagnostics::{span_lint, span_lint_and_then}; -use clippy_utils::{is_in_const_context, sym}; use clippy_utils::macros::macro_backtrace; use clippy_utils::paths::{PathNS, lookup_path_str}; use clippy_utils::ty::{get_field_idx_by_name, implements_trait}; +use clippy_utils::{is_in_const_context, sym}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::{DefId, DefIdSet}; @@ -394,14 +394,14 @@ impl<'tcx> NonCopyConst<'tcx> { let res = typeck.qpath_res(p, e.hir_id); let gen_args = EarlyBinder::bind(typeck.node_args(e.hir_id)).instantiate(tcx, gen_args); match res { - Res::Def(DefKind::Const | DefKind::AssocConst, did) + Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, did) if let Ok(val) = tcx.const_eval_resolve(typing_env, UnevaluatedConst::new(did, gen_args), DUMMY_SP) && let Ok(is_freeze) = self.is_value_freeze(tcx, typing_env, ty, val) => { is_freeze }, - Res::Def(DefKind::Const | DefKind::AssocConst, did) + Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, did) if let Some((typeck, init)) = get_const_hir_value(tcx, typing_env, did, gen_args) => { self.is_init_expr_freeze(tcx, typing_env, typeck, gen_args, init) @@ -588,7 +588,7 @@ impl<'tcx> NonCopyConst<'tcx> { EarlyBinder::bind(init_typeck.node_args(init_expr.hir_id)).instantiate(tcx, init_args); match init_typeck.qpath_res(init_path, init_expr.hir_id) { Res::Def(DefKind::Ctor(..), _) => return None, - Res::Def(DefKind::Const | DefKind::AssocConst, did) + Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, did) if let Ok(val) = tcx.const_eval_resolve( typing_env, UnevaluatedConst::new(did, next_init_args), @@ -598,7 +598,7 @@ impl<'tcx> NonCopyConst<'tcx> { { return res; }, - Res::Def(DefKind::Const | DefKind::AssocConst, did) + Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, did) if let Some((next_typeck, value)) = get_const_hir_value(tcx, typing_env, did, next_init_args) => { @@ -831,7 +831,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> { fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { if let ExprKind::Path(qpath) = &e.kind && let typeck = cx.typeck_results() - && let Res::Def(DefKind::Const | DefKind::AssocConst, did) = typeck.qpath_res(qpath, e.hir_id) + && let Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, did) = typeck.qpath_res(qpath, e.hir_id) // As of `1.80` constant contexts can't borrow any type with interior mutability && !is_in_const_context(cx) && !self.is_ty_freeze(cx.tcx, cx.typing_env(), typeck.expr_ty(e)).is_freeze() diff --git a/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs b/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs index c5eb7d24c30d6..97b3c0d02e84e 100644 --- a/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs +++ b/src/tools/clippy/clippy_lints/src/operators/float_equality_without_abs.rs @@ -35,7 +35,7 @@ pub(crate) fn check<'tcx>( // right hand side matches _::EPSILON && let ExprKind::Path(ref epsilon_path) = rhs.kind - && let Res::Def(DefKind::AssocConst, def_id) = cx.qpath_res(epsilon_path, rhs.hir_id) + && let Res::Def(DefKind::AssocConst { .. }, def_id) = cx.qpath_res(epsilon_path, rhs.hir_id) && let Some(sym) = cx.tcx.get_diagnostic_name(def_id) && matches!(sym, sym::f16_epsilon | sym::f32_epsilon | sym::f64_epsilon | sym::f128_epsilon) diff --git a/src/tools/clippy/clippy_lints_internal/src/symbols.rs b/src/tools/clippy/clippy_lints_internal/src/symbols.rs index 74712097e7166..38c95666e211d 100644 --- a/src/tools/clippy/clippy_lints_internal/src/symbols.rs +++ b/src/tools/clippy/clippy_lints_internal/src/symbols.rs @@ -113,7 +113,7 @@ impl<'tcx> LateLintPass<'tcx> for Symbols { } for item in cx.tcx.module_children(def_id) { - if let Res::Def(DefKind::Const, item_def_id) = item.res + if let Res::Def(DefKind::Const { .. }, item_def_id) = item.res && let ty = cx.tcx.type_of(item_def_id).instantiate_identity() && internal_paths::SYMBOL.matches_ty(cx, ty) && let Ok(ConstValue::Scalar(value)) = cx.tcx.const_eval_poly(item_def_id) diff --git a/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs b/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs index 239f721ae05be..67ae1dcef81be 100644 --- a/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs +++ b/src/tools/clippy/clippy_utils/src/ast_utils/mod.rs @@ -820,8 +820,8 @@ pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool { matches!( (l, r), (Defaultness::Implicit, Defaultness::Implicit) - | (Defaultness::Default(_), Defaultness::Default(_)) - | (Defaultness::Final(_), Defaultness::Final(_)) + | (Defaultness::Default(_), Defaultness::Default(_)) + | (Defaultness::Final(_), Defaultness::Final(_)) ) } diff --git a/src/tools/clippy/clippy_utils/src/consts.rs b/src/tools/clippy/clippy_utils/src/consts.rs index 8177a5806d787..1a75bdff81249 100644 --- a/src/tools/clippy/clippy_utils/src/consts.rs +++ b/src/tools/clippy/clippy_utils/src/consts.rs @@ -753,7 +753,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> { QPath::Resolved(None, path) if path.span.ctxt() == self.ctxt.get() && path.segments.iter().all(|s| self.ctxt.get() == s.ident.span.ctxt()) - && let Res::Def(DefKind::Const, did) = path.res + && let Res::Def(DefKind::Const { .. }, did) = path.res && (matches!( self.tcx.get_diagnostic_name(did), Some( @@ -841,11 +841,13 @@ impl<'tcx> ConstEvalCtxt<'tcx> { && ty.span.ctxt() == self.ctxt.get() && ty_name.ident.span.ctxt() == self.ctxt.get() && matches!(ty_path.res, Res::PrimTy(_)) - && let Some((DefKind::AssocConst, did)) = self.typeck.type_dependent_def(id) => + && let Some((DefKind::AssocConst { .. }, did)) = self.typeck.type_dependent_def(id) => { did }, - _ if let Res::Def(DefKind::Const | DefKind::AssocConst, did) = self.typeck.qpath_res(qpath, id) => { + _ if let Res::Def(DefKind::Const { .. } | DefKind::AssocConst { .. }, did) = + self.typeck.qpath_res(qpath, id) => + { self.source.set(ConstantSource::NonLocal); did }, diff --git a/src/tools/clippy/clippy_utils/src/hir_utils.rs b/src/tools/clippy/clippy_utils/src/hir_utils.rs index 3ff40f2fb722b..2da25f7452a0d 100644 --- a/src/tools/clippy/clippy_utils/src/hir_utils.rs +++ b/src/tools/clippy/clippy_utils/src/hir_utils.rs @@ -799,7 +799,7 @@ impl HirEqInterExpr<'_, '_, '_> { (Res::Local(_), _) | (_, Res::Local(_)) => false, (Res::Def(l_kind, l), Res::Def(r_kind, r)) if l_kind == r_kind - && let DefKind::Const + && let DefKind::Const { .. } | DefKind::Static { .. } | DefKind::Fn | DefKind::TyAlias @@ -1035,7 +1035,7 @@ pub fn eq_expr_value(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>) -> /// item, in which case it is the last two fn generic_path_segments<'tcx>(segments: &'tcx [PathSegment<'tcx>]) -> Option<&'tcx [PathSegment<'tcx>]> { match segments.last()?.res { - Res::Def(DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy, _) => { + Res::Def(DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy, _) => { // >::assoc:: // ^^^^^^^^^^^^^^^^ ^^^^^^^^^^ segments: [module, Trait, assoc] Some(&segments[segments.len().checked_sub(2)?..]) diff --git a/src/tools/clippy/clippy_utils/src/lib.rs b/src/tools/clippy/clippy_utils/src/lib.rs index fdd270c2774af..ab946f96e9955 100644 --- a/src/tools/clippy/clippy_utils/src/lib.rs +++ b/src/tools/clippy/clippy_utils/src/lib.rs @@ -2337,7 +2337,7 @@ fn with_test_item_names(tcx: TyCtxt<'_>, module: LocalModDefId, f: impl FnOnce(& Entry::Vacant(entry) => { let mut names = Vec::new(); for id in tcx.hir_module_free_items(module) { - if matches!(tcx.def_kind(id.owner_id), DefKind::Const) + if matches!(tcx.def_kind(id.owner_id), DefKind::Const { .. }) && let item = tcx.hir_item(id) && let ItemKind::Const(ident, _generics, ty, _body) = item.kind && let TyKind::Path(QPath::Resolved(_, path)) = ty.kind diff --git a/src/tools/clippy/clippy_utils/src/res.rs b/src/tools/clippy/clippy_utils/src/res.rs index e890a73620a58..23afb25f1bbfe 100644 --- a/src/tools/clippy/clippy_utils/src/res.rs +++ b/src/tools/clippy/clippy_utils/src/res.rs @@ -519,7 +519,7 @@ pub trait MaybeDef: Copy { #[inline] fn assoc_parent<'tcx>(self, tcx: &impl HasTyCtxt<'tcx>) -> Option { match self.opt_def(tcx) { - Some((DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy, id)) => tcx.tcx().opt_parent(id), + Some((DefKind::AssocConst { .. } | DefKind::AssocFn | DefKind::AssocTy, id)) => tcx.tcx().opt_parent(id), _ => None, } } diff --git a/src/tools/clippy/clippy_utils/src/visitors.rs b/src/tools/clippy/clippy_utils/src/visitors.rs index 84e4f043e34f1..6ac979d595f49 100644 --- a/src/tools/clippy/clippy_utils/src/visitors.rs +++ b/src/tools/clippy/clippy_utils/src/visitors.rs @@ -367,8 +367,8 @@ pub fn is_const_evaluatable<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> if matches!( self.cx.qpath_res(p, e.hir_id), Res::Def( - DefKind::Const - | DefKind::AssocConst + DefKind::Const { .. } + | DefKind::AssocConst { .. } | DefKind::AnonConst | DefKind::ConstParam | DefKind::Ctor(..) diff --git a/tests/ui-fulldeps/rustc_public/crate-info.rs b/tests/ui-fulldeps/rustc_public/crate-info.rs index 3729aa7609b51..0a8facdcf716c 100644 --- a/tests/ui-fulldeps/rustc_public/crate-info.rs +++ b/tests/ui-fulldeps/rustc_public/crate-info.rs @@ -139,7 +139,8 @@ fn test_stable_mir() -> ControlFlow<()> { } } - let foo_const = get_item(&items, (DefKind::Const, "input::FOO")).unwrap(); + let foo_const = + get_item(&items, (DefKind::Const { is_type_const: false }, "input::FOO")).unwrap(); // Ensure we don't panic trying to get the body of a constant. foo_const.expect_body(); @@ -181,7 +182,8 @@ fn get_item<'a>( items.iter().find(|crate_item| { matches!( (item.0, crate_item.kind()), - (DefKind::Fn, ItemKind::Fn) | (DefKind::Const, ItemKind::Const) + (DefKind::Fn, ItemKind::Fn) + | (DefKind::Const { is_type_const: false }, ItemKind::Const) ) && crate_item.name() == item.1 }) }