@@ -97,11 +97,12 @@ struct Annotator<'a, 'tcx> {
97
97
impl < ' a , ' tcx > Annotator < ' a , ' tcx > {
98
98
// Determine the stability for a node based on its attributes and inherited
99
99
// stability. The stability is recorded in the index and used as the parent.
100
+ // fn_sig: Function signature linked with the current node
100
101
fn annotate < F > (
101
102
& mut self ,
102
103
hir_id : HirId ,
103
104
item_sp : Span ,
104
- item_kind : Option < & ItemKind < ' _ > > ,
105
+ fn_sig : Option < & hir :: FnSig < ' _ > > ,
105
106
kind : AnnotationKind ,
106
107
inherit_deprecation : InheritDeprecation ,
107
108
inherit_const_stability : InheritConstStability ,
@@ -169,20 +170,28 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
169
170
170
171
let const_stab = const_stab. map ( |( const_stab, const_span) | {
171
172
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
- }
173
+ if let Some ( fn_sig) = fn_sig {
174
+ if !( fn_sig. header . constness == hir:: 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 (
181
+ fn_sig. span ,
182
+ "attributes `#[rustc_const_unstable]` \
183
+ and `#[rustc_const_stable]` require \
184
+ the function or method to be marked `const`",
185
+ )
186
+ . span_suggestion_short (
187
+ fn_sig. span ,
188
+ "make the function or method const" ,
189
+ String :: new ( ) ,
190
+ rustc_errors:: Applicability :: Unspecified ,
191
+ )
192
+ . span_label ( const_span, "attribute specified here" )
193
+ . emit ( ) ;
184
194
}
185
- _ => { }
186
195
}
187
196
188
197
self . index . const_stab_map . insert ( hir_id, const_stab) ;
@@ -386,6 +395,8 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
386
395
let orig_in_trait_impl = self . in_trait_impl ;
387
396
let mut kind = AnnotationKind :: Required ;
388
397
let mut const_stab_inherit = InheritConstStability :: No ;
398
+ let mut fn_sig = None ;
399
+
389
400
match i. kind {
390
401
// Inherent impls and foreign modules serve only as containers for other items,
391
402
// they don't have their own stability. They still can be annotated as unstable
@@ -406,7 +417,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
406
417
self . annotate (
407
418
ctor_hir_id,
408
419
i. span ,
409
- Some ( & i . kind ) ,
420
+ None ,
410
421
AnnotationKind :: Required ,
411
422
InheritDeprecation :: Yes ,
412
423
InheritConstStability :: No ,
@@ -415,13 +426,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
415
426
)
416
427
}
417
428
}
429
+ hir:: ItemKind :: Fn ( ref item_fn_sig, _, _) => {
430
+ fn_sig = Some ( item_fn_sig) ;
431
+ }
418
432
_ => { }
419
433
}
420
434
421
435
self . annotate (
422
436
i. hir_id ( ) ,
423
437
i. span ,
424
- Some ( & i . kind ) ,
438
+ fn_sig ,
425
439
kind,
426
440
InheritDeprecation :: Yes ,
427
441
const_stab_inherit,
@@ -432,10 +446,15 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
432
446
}
433
447
434
448
fn visit_trait_item ( & mut self , ti : & ' tcx hir:: TraitItem < ' tcx > ) {
449
+ let fn_sig = match ti. kind {
450
+ hir:: TraitItemKind :: Fn ( ref fn_sig, _) => Some ( fn_sig) ,
451
+ _ => None ,
452
+ } ;
453
+
435
454
self . annotate (
436
455
ti. hir_id ( ) ,
437
456
ti. span ,
438
- None ,
457
+ fn_sig ,
439
458
AnnotationKind :: Required ,
440
459
InheritDeprecation :: Yes ,
441
460
InheritConstStability :: No ,
@@ -449,10 +468,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
449
468
fn visit_impl_item ( & mut self , ii : & ' tcx hir:: ImplItem < ' tcx > ) {
450
469
let kind =
451
470
if self . in_trait_impl { AnnotationKind :: Prohibited } else { AnnotationKind :: Required } ;
471
+
472
+ let fn_sig = match ii. kind {
473
+ hir:: ImplItemKind :: Fn ( ref fn_sig, _) => Some ( fn_sig) ,
474
+ _ => None ,
475
+ } ;
476
+
452
477
self . annotate (
453
478
ii. hir_id ( ) ,
454
479
ii. span ,
455
- None ,
480
+ fn_sig ,
456
481
kind,
457
482
InheritDeprecation :: Yes ,
458
483
InheritConstStability :: No ,
0 commit comments