@@ -55,6 +55,21 @@ impl InheritDeprecation {
55
55
}
56
56
}
57
57
58
+ /// Whether to inherit const stability flags for nested items. In most cases, we do not want to
59
+ /// inherit const stability: just because an enclosing `fn` is const-stable does not mean
60
+ /// all `extern` imports declared in it should be const-stable! However, trait methods
61
+ /// inherit const stability attributes from their parent and do not have their own.
62
+ enum InheritConstStability {
63
+ Yes ,
64
+ No ,
65
+ }
66
+
67
+ impl InheritConstStability {
68
+ fn yes ( & self ) -> bool {
69
+ matches ! ( self , InheritConstStability :: Yes )
70
+ }
71
+ }
72
+
58
73
// A private tree-walker for producing an Index.
59
74
struct Annotator < ' a , ' tcx > {
60
75
tcx : TyCtxt < ' tcx > ,
@@ -75,6 +90,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
75
90
item_sp : Span ,
76
91
kind : AnnotationKind ,
77
92
inherit_deprecation : InheritDeprecation ,
93
+ inherit_const_stability : InheritConstStability ,
78
94
visit_children : F ,
79
95
) where
80
96
F : FnOnce ( & mut Self ) ,
@@ -140,6 +156,8 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
140
156
const_stab
141
157
} ) ;
142
158
159
+ // `impl const Trait for Type` items forward their const stability to their
160
+ // immediate children.
143
161
if const_stab. is_none ( ) {
144
162
debug ! ( "annotate: const_stab not found, parent = {:?}" , self . parent_const_stab) ;
145
163
if let Some ( parent) = self . parent_const_stab {
@@ -228,7 +246,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
228
246
self . recurse_with_stability_attrs (
229
247
depr. map ( |( d, _) | DeprecationEntry :: local ( d, hir_id) ) ,
230
248
stab,
231
- const_stab,
249
+ if inherit_const_stability . yes ( ) { const_stab } else { None } ,
232
250
visit_children,
233
251
) ;
234
252
}
@@ -325,6 +343,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
325
343
fn visit_item ( & mut self , i : & ' tcx Item < ' tcx > ) {
326
344
let orig_in_trait_impl = self . in_trait_impl ;
327
345
let mut kind = AnnotationKind :: Required ;
346
+ let mut const_stab_inherit = InheritConstStability :: No ;
328
347
match i. kind {
329
348
// Inherent impls and foreign modules serve only as containers for other items,
330
349
// they don't have their own stability. They still can be annotated as unstable
@@ -338,6 +357,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
338
357
hir:: ItemKind :: Impl ( hir:: Impl { of_trait : Some ( _) , .. } ) => {
339
358
self . in_trait_impl = true ;
340
359
kind = AnnotationKind :: DeprecationProhibited ;
360
+ const_stab_inherit = InheritConstStability :: Yes ;
341
361
}
342
362
hir:: ItemKind :: Struct ( ref sd, _) => {
343
363
if let Some ( ctor_hir_id) = sd. ctor_hir_id ( ) {
@@ -347,16 +367,23 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
347
367
i. span ,
348
368
AnnotationKind :: Required ,
349
369
InheritDeprecation :: Yes ,
370
+ InheritConstStability :: No ,
350
371
|_| { } ,
351
372
)
352
373
}
353
374
}
354
375
_ => { }
355
376
}
356
377
357
- self . annotate ( i. hir_id , & i. attrs , i. span , kind, InheritDeprecation :: Yes , |v| {
358
- intravisit:: walk_item ( v, i)
359
- } ) ;
378
+ self . annotate (
379
+ i. hir_id ,
380
+ & i. attrs ,
381
+ i. span ,
382
+ kind,
383
+ InheritDeprecation :: Yes ,
384
+ const_stab_inherit,
385
+ |v| intravisit:: walk_item ( v, i) ,
386
+ ) ;
360
387
self . in_trait_impl = orig_in_trait_impl;
361
388
}
362
389
@@ -367,6 +394,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
367
394
ti. span ,
368
395
AnnotationKind :: Required ,
369
396
InheritDeprecation :: Yes ,
397
+ InheritConstStability :: No ,
370
398
|v| {
371
399
intravisit:: walk_trait_item ( v, ti) ;
372
400
} ,
@@ -376,9 +404,17 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
376
404
fn visit_impl_item ( & mut self , ii : & ' tcx hir:: ImplItem < ' tcx > ) {
377
405
let kind =
378
406
if self . in_trait_impl { AnnotationKind :: Prohibited } else { AnnotationKind :: Required } ;
379
- self . annotate ( ii. hir_id , & ii. attrs , ii. span , kind, InheritDeprecation :: Yes , |v| {
380
- intravisit:: walk_impl_item ( v, ii) ;
381
- } ) ;
407
+ self . annotate (
408
+ ii. hir_id ,
409
+ & ii. attrs ,
410
+ ii. span ,
411
+ kind,
412
+ InheritDeprecation :: Yes ,
413
+ InheritConstStability :: No ,
414
+ |v| {
415
+ intravisit:: walk_impl_item ( v, ii) ;
416
+ } ,
417
+ ) ;
382
418
}
383
419
384
420
fn visit_variant ( & mut self , var : & ' tcx Variant < ' tcx > , g : & ' tcx Generics < ' tcx > , item_id : HirId ) {
@@ -388,6 +424,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
388
424
var. span ,
389
425
AnnotationKind :: Required ,
390
426
InheritDeprecation :: Yes ,
427
+ InheritConstStability :: No ,
391
428
|v| {
392
429
if let Some ( ctor_hir_id) = var. data . ctor_hir_id ( ) {
393
430
v. annotate (
@@ -396,6 +433,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
396
433
var. span ,
397
434
AnnotationKind :: Required ,
398
435
InheritDeprecation :: Yes ,
436
+ InheritConstStability :: No ,
399
437
|_| { } ,
400
438
) ;
401
439
}
@@ -412,6 +450,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
412
450
s. span ,
413
451
AnnotationKind :: Required ,
414
452
InheritDeprecation :: Yes ,
453
+ InheritConstStability :: No ,
415
454
|v| {
416
455
intravisit:: walk_struct_field ( v, s) ;
417
456
} ,
@@ -425,6 +464,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
425
464
i. span ,
426
465
AnnotationKind :: Required ,
427
466
InheritDeprecation :: Yes ,
467
+ InheritConstStability :: No ,
428
468
|v| {
429
469
intravisit:: walk_foreign_item ( v, i) ;
430
470
} ,
@@ -438,6 +478,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
438
478
md. span ,
439
479
AnnotationKind :: Required ,
440
480
InheritDeprecation :: Yes ,
481
+ InheritConstStability :: No ,
441
482
|_| { } ,
442
483
) ;
443
484
}
@@ -451,9 +492,17 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
451
492
_ => AnnotationKind :: Prohibited ,
452
493
} ;
453
494
454
- self . annotate ( p. hir_id , & p. attrs , p. span , kind, InheritDeprecation :: No , |v| {
455
- intravisit:: walk_generic_param ( v, p) ;
456
- } ) ;
495
+ self . annotate (
496
+ p. hir_id ,
497
+ & p. attrs ,
498
+ p. span ,
499
+ kind,
500
+ InheritDeprecation :: No ,
501
+ InheritConstStability :: No ,
502
+ |v| {
503
+ intravisit:: walk_generic_param ( v, p) ;
504
+ } ,
505
+ ) ;
457
506
}
458
507
}
459
508
@@ -618,6 +667,7 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {
618
667
krate. item . span ,
619
668
AnnotationKind :: Required ,
620
669
InheritDeprecation :: Yes ,
670
+ InheritConstStability :: No ,
621
671
|v| intravisit:: walk_crate ( v, krate) ,
622
672
) ;
623
673
}
0 commit comments