@@ -9,6 +9,7 @@ use rustc_hir::def::{DefKind, Res};
99use rustc_hir:: def_id:: { DefId , LocalDefId , CRATE_DEF_ID , CRATE_DEF_INDEX } ;
1010use rustc_hir:: hir_id:: CRATE_HIR_ID ;
1111use rustc_hir:: intravisit:: { self , Visitor } ;
12+ use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
1213use rustc_hir:: { FieldDef , Generics , HirId , Item , TraitRef , Ty , TyKind , Variant } ;
1314use rustc_middle:: hir:: nested_filter;
1415use rustc_middle:: middle:: privacy:: AccessLevels ;
@@ -530,7 +531,8 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
530531 return ;
531532 }
532533
533- let is_const = self . tcx . is_const_fn ( def_id. to_def_id ( ) ) ;
534+ let is_const = self . tcx . is_const_fn ( def_id. to_def_id ( ) )
535+ || self . tcx . is_const_trait_impl_raw ( def_id. to_def_id ( ) ) ;
534536 let is_stable = self
535537 . tcx
536538 . lookup_stability ( def_id)
@@ -604,6 +606,44 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
604606 // stable (assuming they have not inherited instability from their parent).
605607}
606608
609+ struct CheckStableConstImplTrait < ' tcx > {
610+ tcx : TyCtxt < ' tcx > ,
611+ }
612+
613+ impl < ' tcx > ItemLikeVisitor < ' tcx > for CheckStableConstImplTrait < ' tcx > {
614+ fn visit_item ( & mut self , item : & ' tcx Item < ' tcx > ) {
615+ if !matches ! (
616+ item. kind,
617+ hir:: ItemKind :: Impl ( hir:: Impl {
618+ of_trait: Some ( _) ,
619+ constness: hir:: Constness :: Const ,
620+ ..
621+ } )
622+ ) {
623+ return ;
624+ }
625+
626+ if self . tcx . lookup_const_stability ( item. def_id ) . map_or ( false , |stab| stab. is_const_stable ( ) )
627+ {
628+ self . tcx
629+ . sess
630+ . struct_span_err ( item. span , "trait implementations cannot be const stable yet" )
631+ . note ( "see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information" )
632+ . emit ( ) ;
633+ }
634+ }
635+
636+ fn visit_trait_item ( & mut self , _trait_item : & ' tcx hir:: TraitItem < ' tcx > ) {
637+ // Nothing to do here.
638+ }
639+ fn visit_impl_item ( & mut self , _impl_item : & ' tcx hir:: ImplItem < ' tcx > ) {
640+ // Nothing to do here.
641+ }
642+ fn visit_foreign_item ( & mut self , _foreign_item : & ' tcx hir:: ForeignItem < ' tcx > ) {
643+ // Nothing to do here.
644+ }
645+ }
646+
607647fn stability_index ( tcx : TyCtxt < ' _ > , ( ) : ( ) ) -> Index {
608648 let mut index = Index {
609649 stab_map : Default :: default ( ) ,
@@ -824,6 +864,17 @@ impl<'tcx> Visitor<'tcx> for CheckTraitImplStable<'tcx> {
824864 }
825865}
826866
867+ pub fn check_const_impl_trait ( tcx : TyCtxt < ' _ > ) {
868+ let features = tcx. features ( ) ; // FIXME How cheap is this call?
869+ // Both feature gates have to be enabled for this check to have any effect.
870+ if !features. staged_api || !features. const_trait_impl {
871+ return ;
872+ }
873+
874+ let mut visitor = CheckStableConstImplTrait { tcx } ;
875+ tcx. hir ( ) . visit_all_item_likes ( & mut visitor) ;
876+ }
877+
827878/// Given the list of enabled features that were not language features (i.e., that
828879/// were expected to be library features), and the list of features used from
829880/// libraries, identify activated features that don't exist and error about them.
0 commit comments