@@ -9,6 +9,7 @@ use rustc_hir::def::{DefKind, Res};
9
9
use rustc_hir:: def_id:: { DefId , LocalDefId , CRATE_DEF_ID , CRATE_DEF_INDEX } ;
10
10
use rustc_hir:: hir_id:: CRATE_HIR_ID ;
11
11
use rustc_hir:: intravisit:: { self , Visitor } ;
12
+ use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
12
13
use rustc_hir:: { FieldDef , Generics , HirId , Item , TraitRef , Ty , TyKind , Variant } ;
13
14
use rustc_middle:: hir:: nested_filter;
14
15
use rustc_middle:: middle:: privacy:: AccessLevels ;
@@ -530,7 +531,8 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
530
531
return ;
531
532
}
532
533
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 ( ) ) ;
534
536
let is_stable = self
535
537
. tcx
536
538
. lookup_stability ( def_id)
@@ -604,6 +606,44 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
604
606
// stable (assuming they have not inherited instability from their parent).
605
607
}
606
608
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
+
607
647
fn stability_index ( tcx : TyCtxt < ' _ > , ( ) : ( ) ) -> Index {
608
648
let mut index = Index {
609
649
stab_map : Default :: default ( ) ,
@@ -824,6 +864,17 @@ impl<'tcx> Visitor<'tcx> for CheckTraitImplStable<'tcx> {
824
864
}
825
865
}
826
866
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
+
827
878
/// Given the list of enabled features that were not language features (i.e., that
828
879
/// were expected to be library features), and the list of features used from
829
880
/// libraries, identify activated features that don't exist and error about them.
0 commit comments