@@ -14,6 +14,7 @@ use std::{fmt, iter, mem};
1414
1515use rustc_abi:: { ExternAbi , FieldIdx , Layout , LayoutData , TargetDataLayout , VariantIdx } ;
1616use rustc_ast:: { self as ast, attr} ;
17+ use rustc_attr:: { ConstStability , StabilityLevel } ;
1718use rustc_data_structures:: defer;
1819use rustc_data_structures:: fingerprint:: Fingerprint ;
1920use rustc_data_structures:: fx:: FxHashMap ;
@@ -3140,6 +3141,35 @@ impl<'tcx> TyCtxt<'tcx> {
31403141 && self . constness ( def_id) == hir:: Constness :: Const
31413142 }
31423143
3144+ pub fn enforce_trait_const_stability ( self , trait_def_id : DefId , span : Span ) {
3145+ match self . lookup_const_stability ( trait_def_id) {
3146+ Some ( ConstStability {
3147+ level : StabilityLevel :: Unstable { implied_by : implied_feature, .. } ,
3148+ feature,
3149+ ..
3150+ } ) => {
3151+ if span. allows_unstable ( feature)
3152+ || implied_feature. is_some_and ( |f| span. allows_unstable ( f) )
3153+ {
3154+ return ;
3155+ }
3156+ let feature_enabled = trait_def_id. is_local ( )
3157+ || self . features ( ) . enabled ( feature)
3158+ || implied_feature. is_some_and ( |f| self . features ( ) . enabled ( f) ) ;
3159+
3160+ if !feature_enabled {
3161+ let mut diag = self . dcx ( ) . create_err ( crate :: error:: UnstableConstTrait {
3162+ span,
3163+ def_path : self . def_path_str ( trait_def_id) ,
3164+ } ) ;
3165+ self . disabled_nightly_features ( & mut diag, None , [ ( String :: new ( ) , feature) ] ) ;
3166+ diag. emit ( ) ;
3167+ }
3168+ }
3169+ _ => { }
3170+ }
3171+ }
3172+
31433173 pub fn intrinsic ( self , def_id : impl IntoQueryParam < DefId > + Copy ) -> Option < ty:: IntrinsicDef > {
31443174 match self . def_kind ( def_id) {
31453175 DefKind :: Fn | DefKind :: AssocFn => { }
0 commit comments