@@ -20,6 +20,7 @@ use rustc_session::parse::feature_err;
2020use rustc_session:: Session ;
2121use rustc_span:: symbol:: { sym, Symbol } ;
2222use rustc_span:: { Span , DUMMY_SP } ;
23+ use rustc_target:: spec:: abi:: Abi ;
2324
2425use std:: cmp:: Ordering ;
2526use std:: iter;
@@ -95,10 +96,12 @@ struct Annotator<'a, 'tcx> {
9596impl < ' a , ' tcx > Annotator < ' a , ' tcx > {
9697 // Determine the stability for a node based on its attributes and inherited
9798 // stability. The stability is recorded in the index and used as the parent.
99+ // If the node is a function, `fn_sig` is its signature
98100 fn annotate < F > (
99101 & mut self ,
100102 hir_id : HirId ,
101103 item_sp : Span ,
104+ fn_sig : Option < & ' tcx hir:: FnSig < ' tcx > > ,
102105 kind : AnnotationKind ,
103106 inherit_deprecation : InheritDeprecation ,
104107 inherit_const_stability : InheritConstStability ,
@@ -164,8 +167,27 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
164167
165168 let ( stab, const_stab) = attr:: find_stability ( & self . tcx . sess , attrs, item_sp) ;
166169
167- let const_stab = const_stab. map ( |( const_stab, _ ) | {
170+ let const_stab = const_stab. map ( |( const_stab, const_span ) | {
168171 let const_stab = self . tcx . intern_const_stability ( const_stab) ;
172+ if let Some ( fn_sig) = fn_sig {
173+ if !( fn_sig. header . constness == hir:: Constness :: Const
174+ && fn_sig. header . abi != Abi :: RustIntrinsic
175+ && fn_sig. header . abi != Abi :: PlatformIntrinsic )
176+ {
177+ self . tcx
178+ . sess
179+ . struct_span_err (
180+ fn_sig. span ,
181+ "attributes `#[rustc_const_unstable]` \
182+ and `#[rustc_const_stable]` require \
183+ the function or method to be marked `const`",
184+ )
185+ . span_help ( fn_sig. span , "make the function or method const" )
186+ . span_label ( const_span, "attribute specified here" )
187+ . emit ( ) ;
188+ }
189+ }
190+
169191 self . index . const_stab_map . insert ( hir_id, const_stab) ;
170192 const_stab
171193 } ) ;
@@ -367,6 +389,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
367389 let orig_in_trait_impl = self . in_trait_impl ;
368390 let mut kind = AnnotationKind :: Required ;
369391 let mut const_stab_inherit = InheritConstStability :: No ;
392+ let mut fn_sig = None ;
393+
370394 match i. kind {
371395 // Inherent impls and foreign modules serve only as containers for other items,
372396 // they don't have their own stability. They still can be annotated as unstable
@@ -387,6 +411,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
387411 self . annotate (
388412 ctor_hir_id,
389413 i. span ,
414+ None ,
390415 AnnotationKind :: Required ,
391416 InheritDeprecation :: Yes ,
392417 InheritConstStability :: No ,
@@ -395,12 +420,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
395420 )
396421 }
397422 }
423+ hir:: ItemKind :: Fn ( ref item_fn_sig, _, _) => {
424+ fn_sig = Some ( item_fn_sig) ;
425+ }
398426 _ => { }
399427 }
400428
401429 self . annotate (
402430 i. hir_id ( ) ,
403431 i. span ,
432+ fn_sig,
404433 kind,
405434 InheritDeprecation :: Yes ,
406435 const_stab_inherit,
@@ -411,9 +440,15 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
411440 }
412441
413442 fn visit_trait_item ( & mut self , ti : & ' tcx hir:: TraitItem < ' tcx > ) {
443+ let fn_sig = match ti. kind {
444+ hir:: TraitItemKind :: Fn ( ref fn_sig, _) => Some ( fn_sig) ,
445+ _ => None ,
446+ } ;
447+
414448 self . annotate (
415449 ti. hir_id ( ) ,
416450 ti. span ,
451+ fn_sig,
417452 AnnotationKind :: Required ,
418453 InheritDeprecation :: Yes ,
419454 InheritConstStability :: No ,
@@ -427,9 +462,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
427462 fn visit_impl_item ( & mut self , ii : & ' tcx hir:: ImplItem < ' tcx > ) {
428463 let kind =
429464 if self . in_trait_impl { AnnotationKind :: Prohibited } else { AnnotationKind :: Required } ;
465+
466+ let fn_sig = match ii. kind {
467+ hir:: ImplItemKind :: Fn ( ref fn_sig, _) => Some ( fn_sig) ,
468+ _ => None ,
469+ } ;
470+
430471 self . annotate (
431472 ii. hir_id ( ) ,
432473 ii. span ,
474+ fn_sig,
433475 kind,
434476 InheritDeprecation :: Yes ,
435477 InheritConstStability :: No ,
@@ -444,6 +486,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
444486 self . annotate (
445487 var. id ,
446488 var. span ,
489+ None ,
447490 AnnotationKind :: Required ,
448491 InheritDeprecation :: Yes ,
449492 InheritConstStability :: No ,
@@ -453,6 +496,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
453496 v. annotate (
454497 ctor_hir_id,
455498 var. span ,
499+ None ,
456500 AnnotationKind :: Required ,
457501 InheritDeprecation :: Yes ,
458502 InheritConstStability :: No ,
@@ -470,6 +514,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
470514 self . annotate (
471515 s. hir_id ,
472516 s. span ,
517+ None ,
473518 AnnotationKind :: Required ,
474519 InheritDeprecation :: Yes ,
475520 InheritConstStability :: No ,
@@ -484,6 +529,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
484529 self . annotate (
485530 i. hir_id ( ) ,
486531 i. span ,
532+ None ,
487533 AnnotationKind :: Required ,
488534 InheritDeprecation :: Yes ,
489535 InheritConstStability :: No ,
@@ -498,6 +544,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
498544 self . annotate (
499545 md. hir_id ( ) ,
500546 md. span ,
547+ None ,
501548 AnnotationKind :: Required ,
502549 InheritDeprecation :: Yes ,
503550 InheritConstStability :: No ,
@@ -517,6 +564,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
517564 self . annotate (
518565 p. hir_id ,
519566 p. span ,
567+ None ,
520568 kind,
521569 InheritDeprecation :: No ,
522570 InheritConstStability :: No ,
@@ -687,6 +735,7 @@ fn stability_index(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> {
687735 annotator. annotate (
688736 hir:: CRATE_HIR_ID ,
689737 krate. item . inner ,
738+ None ,
690739 AnnotationKind :: Required ,
691740 InheritDeprecation :: Yes ,
692741 InheritConstStability :: No ,
0 commit comments