@@ -20,6 +20,7 @@ use rustc_session::parse::feature_err;
20
20
use rustc_session:: Session ;
21
21
use rustc_span:: symbol:: { sym, Symbol } ;
22
22
use rustc_span:: { Span , DUMMY_SP } ;
23
+ use rustc_target:: spec:: abi:: Abi ;
23
24
24
25
use std:: cmp:: Ordering ;
25
26
use std:: iter;
@@ -95,10 +96,12 @@ struct Annotator<'a, 'tcx> {
95
96
impl < ' a , ' tcx > Annotator < ' a , ' tcx > {
96
97
// Determine the stability for a node based on its attributes and inherited
97
98
// 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
98
100
fn annotate < F > (
99
101
& mut self ,
100
102
hir_id : HirId ,
101
103
item_sp : Span ,
104
+ fn_sig : Option < & ' tcx hir:: FnSig < ' tcx > > ,
102
105
kind : AnnotationKind ,
103
106
inherit_deprecation : InheritDeprecation ,
104
107
inherit_const_stability : InheritConstStability ,
@@ -164,8 +167,27 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
164
167
165
168
let ( stab, const_stab) = attr:: find_stability ( & self . tcx . sess , attrs, item_sp) ;
166
169
167
- let const_stab = const_stab. map ( |( const_stab, _ ) | {
170
+ let const_stab = const_stab. map ( |( const_stab, const_span ) | {
168
171
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
+
169
191
self . index . const_stab_map . insert ( hir_id, const_stab) ;
170
192
const_stab
171
193
} ) ;
@@ -367,6 +389,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
367
389
let orig_in_trait_impl = self . in_trait_impl ;
368
390
let mut kind = AnnotationKind :: Required ;
369
391
let mut const_stab_inherit = InheritConstStability :: No ;
392
+ let mut fn_sig = None ;
393
+
370
394
match i. kind {
371
395
// Inherent impls and foreign modules serve only as containers for other items,
372
396
// 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> {
387
411
self . annotate (
388
412
ctor_hir_id,
389
413
i. span ,
414
+ None ,
390
415
AnnotationKind :: Required ,
391
416
InheritDeprecation :: Yes ,
392
417
InheritConstStability :: No ,
@@ -395,12 +420,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
395
420
)
396
421
}
397
422
}
423
+ hir:: ItemKind :: Fn ( ref item_fn_sig, _, _) => {
424
+ fn_sig = Some ( item_fn_sig) ;
425
+ }
398
426
_ => { }
399
427
}
400
428
401
429
self . annotate (
402
430
i. hir_id ( ) ,
403
431
i. span ,
432
+ fn_sig,
404
433
kind,
405
434
InheritDeprecation :: Yes ,
406
435
const_stab_inherit,
@@ -411,9 +440,15 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
411
440
}
412
441
413
442
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
+
414
448
self . annotate (
415
449
ti. hir_id ( ) ,
416
450
ti. span ,
451
+ fn_sig,
417
452
AnnotationKind :: Required ,
418
453
InheritDeprecation :: Yes ,
419
454
InheritConstStability :: No ,
@@ -427,9 +462,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
427
462
fn visit_impl_item ( & mut self , ii : & ' tcx hir:: ImplItem < ' tcx > ) {
428
463
let kind =
429
464
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
+
430
471
self . annotate (
431
472
ii. hir_id ( ) ,
432
473
ii. span ,
474
+ fn_sig,
433
475
kind,
434
476
InheritDeprecation :: Yes ,
435
477
InheritConstStability :: No ,
@@ -444,6 +486,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
444
486
self . annotate (
445
487
var. id ,
446
488
var. span ,
489
+ None ,
447
490
AnnotationKind :: Required ,
448
491
InheritDeprecation :: Yes ,
449
492
InheritConstStability :: No ,
@@ -453,6 +496,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
453
496
v. annotate (
454
497
ctor_hir_id,
455
498
var. span ,
499
+ None ,
456
500
AnnotationKind :: Required ,
457
501
InheritDeprecation :: Yes ,
458
502
InheritConstStability :: No ,
@@ -470,6 +514,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
470
514
self . annotate (
471
515
s. hir_id ,
472
516
s. span ,
517
+ None ,
473
518
AnnotationKind :: Required ,
474
519
InheritDeprecation :: Yes ,
475
520
InheritConstStability :: No ,
@@ -484,6 +529,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
484
529
self . annotate (
485
530
i. hir_id ( ) ,
486
531
i. span ,
532
+ None ,
487
533
AnnotationKind :: Required ,
488
534
InheritDeprecation :: Yes ,
489
535
InheritConstStability :: No ,
@@ -498,6 +544,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
498
544
self . annotate (
499
545
md. hir_id ( ) ,
500
546
md. span ,
547
+ None ,
501
548
AnnotationKind :: Required ,
502
549
InheritDeprecation :: Yes ,
503
550
InheritConstStability :: No ,
@@ -517,6 +564,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
517
564
self . annotate (
518
565
p. hir_id ,
519
566
p. span ,
567
+ None ,
520
568
kind,
521
569
InheritDeprecation :: No ,
522
570
InheritConstStability :: No ,
@@ -687,6 +735,7 @@ fn stability_index(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> {
687
735
annotator. annotate (
688
736
hir:: CRATE_HIR_ID ,
689
737
krate. item . inner ,
738
+ None ,
690
739
AnnotationKind :: Required ,
691
740
InheritDeprecation :: Yes ,
692
741
InheritConstStability :: No ,
0 commit comments