1
1
//! A pass that annotates every item and method with its stability level,
2
2
//! propagating default levels lexically from parent to children ast nodes.
3
3
4
+ use hir:: { Constness , ItemKind } ;
4
5
use rustc_ast:: Attribute ;
5
6
use rustc_attr:: { self as attr, ConstStability , Stability } ;
6
7
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
@@ -20,6 +21,7 @@ use rustc_session::parse::feature_err;
20
21
use rustc_session:: Session ;
21
22
use rustc_span:: symbol:: { sym, Symbol } ;
22
23
use rustc_span:: { Span , DUMMY_SP } ;
24
+ use rustc_target:: spec:: abi:: Abi ;
23
25
24
26
use std:: cmp:: Ordering ;
25
27
use std:: iter;
@@ -99,6 +101,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
99
101
& mut self ,
100
102
hir_id : HirId ,
101
103
item_sp : Span ,
104
+ item_kind : Option < & ItemKind < ' _ > > ,
102
105
kind : AnnotationKind ,
103
106
inherit_deprecation : InheritDeprecation ,
104
107
inherit_const_stability : InheritConstStability ,
@@ -164,8 +167,24 @@ 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
+ match item_kind {
173
+ Some ( ItemKind :: Fn ( ref fn_sig, _, _) ) => {
174
+ if !( fn_sig. header . constness == Constness :: Const
175
+ && fn_sig. header . abi != Abi :: RustIntrinsic
176
+ && fn_sig. header . abi != Abi :: PlatformIntrinsic )
177
+ {
178
+ self . tcx
179
+ . sess
180
+ . struct_span_err ( fn_sig. span , "Attributes `#[rustc_const_unstable]` and `#[rustc_const_stable]` require the function to be marked `const`" )
181
+ . span_label ( const_span, "Const stability attribute specified here" )
182
+ . emit ( ) ;
183
+ }
184
+ }
185
+ _ => { }
186
+ }
187
+
169
188
self . index . const_stab_map . insert ( hir_id, const_stab) ;
170
189
const_stab
171
190
} ) ;
@@ -186,7 +205,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
186
205
struct_span_err ! (
187
206
self . tcx. sess,
188
207
* span,
189
- E0549 ,
208
+ e0549 ,
190
209
"rustc_deprecated attribute must be paired with \
191
210
either stable or unstable attribute"
192
211
)
@@ -387,6 +406,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
387
406
self . annotate (
388
407
ctor_hir_id,
389
408
i. span ,
409
+ Some ( & i. kind ) ,
390
410
AnnotationKind :: Required ,
391
411
InheritDeprecation :: Yes ,
392
412
InheritConstStability :: No ,
@@ -401,6 +421,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
401
421
self . annotate (
402
422
i. hir_id ( ) ,
403
423
i. span ,
424
+ Some ( & i. kind ) ,
404
425
kind,
405
426
InheritDeprecation :: Yes ,
406
427
const_stab_inherit,
@@ -414,6 +435,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
414
435
self . annotate (
415
436
ti. hir_id ( ) ,
416
437
ti. span ,
438
+ None ,
417
439
AnnotationKind :: Required ,
418
440
InheritDeprecation :: Yes ,
419
441
InheritConstStability :: No ,
@@ -430,6 +452,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
430
452
self . annotate (
431
453
ii. hir_id ( ) ,
432
454
ii. span ,
455
+ None ,
433
456
kind,
434
457
InheritDeprecation :: Yes ,
435
458
InheritConstStability :: No ,
@@ -444,6 +467,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
444
467
self . annotate (
445
468
var. id ,
446
469
var. span ,
470
+ None ,
447
471
AnnotationKind :: Required ,
448
472
InheritDeprecation :: Yes ,
449
473
InheritConstStability :: No ,
@@ -453,6 +477,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
453
477
v. annotate (
454
478
ctor_hir_id,
455
479
var. span ,
480
+ None ,
456
481
AnnotationKind :: Required ,
457
482
InheritDeprecation :: Yes ,
458
483
InheritConstStability :: No ,
@@ -470,6 +495,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
470
495
self . annotate (
471
496
s. hir_id ,
472
497
s. span ,
498
+ None ,
473
499
AnnotationKind :: Required ,
474
500
InheritDeprecation :: Yes ,
475
501
InheritConstStability :: No ,
@@ -484,6 +510,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
484
510
self . annotate (
485
511
i. hir_id ( ) ,
486
512
i. span ,
513
+ None ,
487
514
AnnotationKind :: Required ,
488
515
InheritDeprecation :: Yes ,
489
516
InheritConstStability :: No ,
@@ -498,6 +525,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
498
525
self . annotate (
499
526
md. hir_id ( ) ,
500
527
md. span ,
528
+ None ,
501
529
AnnotationKind :: Required ,
502
530
InheritDeprecation :: Yes ,
503
531
InheritConstStability :: No ,
@@ -517,6 +545,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
517
545
self . annotate (
518
546
p. hir_id ,
519
547
p. span ,
548
+ None ,
520
549
kind,
521
550
InheritDeprecation :: No ,
522
551
InheritConstStability :: No ,
@@ -687,6 +716,7 @@ fn stability_index(tcx: TyCtxt<'tcx>, (): ()) -> Index<'tcx> {
687
716
annotator. annotate (
688
717
hir:: CRATE_HIR_ID ,
689
718
krate. item . inner ,
719
+ None ,
690
720
AnnotationKind :: Required ,
691
721
InheritDeprecation :: Yes ,
692
722
InheritConstStability :: No ,
0 commit comments