@@ -15,6 +15,7 @@ use std::{fmt, iter, mem};
1515
1616use rustc_abi:: { ExternAbi , FieldIdx , Layout , LayoutData , TargetDataLayout , VariantIdx } ;
1717use rustc_ast as ast;
18+ use rustc_attr_parsing:: { ConstStability , StabilityLevel } ;
1819use rustc_data_structures:: defer;
1920use rustc_data_structures:: fingerprint:: Fingerprint ;
2021use rustc_data_structures:: fx:: FxHashMap ;
@@ -3132,6 +3133,35 @@ impl<'tcx> TyCtxt<'tcx> {
31323133 && self . impl_trait_header ( def_id) . unwrap ( ) . constness == hir:: Constness :: Const
31333134 }
31343135
3136+ pub fn enforce_trait_const_stability ( self , trait_def_id : DefId , span : Span ) {
3137+ match self . lookup_const_stability ( trait_def_id) {
3138+ Some ( ConstStability {
3139+ level : StabilityLevel :: Unstable { implied_by : implied_feature, .. } ,
3140+ feature,
3141+ ..
3142+ } ) => {
3143+ if span. allows_unstable ( feature)
3144+ || implied_feature. is_some_and ( |f| span. allows_unstable ( f) )
3145+ {
3146+ return ;
3147+ }
3148+ let feature_enabled = trait_def_id. is_local ( )
3149+ || self . features ( ) . enabled ( feature)
3150+ || implied_feature. is_some_and ( |f| self . features ( ) . enabled ( f) ) ;
3151+
3152+ if !feature_enabled {
3153+ let mut diag = self . dcx ( ) . create_err ( crate :: error:: UnstableConstTrait {
3154+ span,
3155+ def_path : self . def_path_str ( trait_def_id) ,
3156+ } ) ;
3157+ self . disabled_nightly_features ( & mut diag, None , [ ( String :: new ( ) , feature) ] ) ;
3158+ diag. emit ( ) ;
3159+ }
3160+ }
3161+ _ => { }
3162+ }
3163+ }
3164+
31353165 pub fn intrinsic ( self , def_id : impl IntoQueryParam < DefId > + Copy ) -> Option < ty:: IntrinsicDef > {
31363166 match self . def_kind ( def_id) {
31373167 DefKind :: Fn | DefKind :: AssocFn => { }
0 commit comments