@@ -9,7 +9,6 @@ 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 ;
1312use rustc_hir:: { FieldDef , Generics , HirId , Item , TraitRef , Ty , TyKind , Variant } ;
1413use rustc_middle:: hir:: nested_filter;
1514use rustc_middle:: middle:: privacy:: AccessLevels ;
@@ -606,44 +605,6 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
606605 // stable (assuming they have not inherited instability from their parent).
607606}
608607
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-
647608fn stability_index ( tcx : TyCtxt < ' _ > , ( ) : ( ) ) -> Index {
648609 let mut index = Index {
649610 stab_map : Default :: default ( ) ,
@@ -748,16 +709,23 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
748709 // For implementations of traits, check the stability of each item
749710 // individually as it's possible to have a stable trait with unstable
750711 // items.
751- hir:: ItemKind :: Impl ( hir:: Impl { of_trait : Some ( ref t) , self_ty, items, .. } ) => {
752- if self . tcx . features ( ) . staged_api {
712+ hir:: ItemKind :: Impl ( hir:: Impl {
713+ of_trait : Some ( ref t) ,
714+ self_ty,
715+ items,
716+ constness,
717+ ..
718+ } ) => {
719+ let features = self . tcx . features ( ) ;
720+ if features. staged_api {
721+ let attrs = self . tcx . hir ( ) . attrs ( item. hir_id ( ) ) ;
722+ let ( stab, const_stab) = attr:: find_stability ( & self . tcx . sess , attrs, item. span ) ;
723+
753724 // If this impl block has an #[unstable] attribute, give an
754725 // error if all involved types and traits are stable, because
755726 // it will have no effect.
756727 // See: https://github.com/rust-lang/rust/issues/55436
757- let attrs = self . tcx . hir ( ) . attrs ( item. hir_id ( ) ) ;
758- if let ( Some ( ( Stability { level : attr:: Unstable { .. } , .. } , span) ) , _) =
759- attr:: find_stability ( & self . tcx . sess , attrs, item. span )
760- {
728+ if let Some ( ( Stability { level : attr:: Unstable { .. } , .. } , span) ) = stab {
761729 let mut c = CheckTraitImplStable { tcx : self . tcx , fully_stable : true } ;
762730 c. visit_ty ( self_ty) ;
763731 c. visit_trait_ref ( t) ;
@@ -773,6 +741,19 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
773741 ) ;
774742 }
775743 }
744+
745+ // `#![feature(const_trait_impl)]` is unstable, so any impl declared stable
746+ // needs to have an error emitted.
747+ if features. const_trait_impl
748+ && constness == hir:: Constness :: Const
749+ && const_stab. map_or ( false , |( stab, _) | stab. is_const_stable ( ) )
750+ {
751+ self . tcx
752+ . sess
753+ . struct_span_err ( item. span , "trait implementations cannot be const stable yet" )
754+ . note ( "see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information" )
755+ . emit ( ) ;
756+ }
776757 }
777758
778759 for impl_item_ref in items {
@@ -864,17 +845,6 @@ impl<'tcx> Visitor<'tcx> for CheckTraitImplStable<'tcx> {
864845 }
865846}
866847
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-
878848/// Given the list of enabled features that were not language features (i.e., that
879849/// were expected to be library features), and the list of features used from
880850/// libraries, identify activated features that don't exist and error about them.
0 commit comments