Skip to content

Commit 4cb089b

Browse files
committed
Inherit #[stable(..)] annotations in enum variants and fields from its item
1 parent 178108b commit 4cb089b

13 files changed

+207
-157
lines changed

compiler/rustc_passes/src/lib_features.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl LibFeatureCollector<'tcx> {
109109
}
110110

111111
fn span_feature_error(&self, span: Span, msg: &str) {
112-
struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg,).emit();
112+
struct_span_err!(self.tcx.sess, span, E0711, "{}", &msg).emit();
113113
}
114114
}
115115

compiler/rustc_passes/src/stability.rs

+37-11
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ impl InheritConstStability {
7070
}
7171
}
7272

73+
enum InheritStability {
74+
Yes,
75+
No,
76+
}
77+
78+
impl InheritStability {
79+
fn yes(&self) -> bool {
80+
matches!(self, InheritStability::Yes)
81+
}
82+
}
83+
7384
// A private tree-walker for producing an Index.
7485
struct Annotator<'a, 'tcx> {
7586
tcx: TyCtxt<'tcx>,
@@ -91,6 +102,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
91102
kind: AnnotationKind,
92103
inherit_deprecation: InheritDeprecation,
93104
inherit_const_stability: InheritConstStability,
105+
inherit_from_parent: InheritStability,
94106
visit_children: F,
95107
) where
96108
F: FnOnce(&mut Self),
@@ -131,12 +143,13 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
131143
}
132144

133145
if self.tcx.features().staged_api {
134-
if let Some(..) = attrs.iter().find(|a| self.tcx.sess.check_name(a, sym::deprecated)) {
135-
self.tcx.sess.span_err(
136-
item_sp,
137-
"`#[deprecated]` cannot be used in staged API; \
138-
use `#[rustc_deprecated]` instead",
139-
);
146+
if let Some(a) = attrs.iter().find(|a| self.tcx.sess.check_name(a, sym::deprecated)) {
147+
self.tcx
148+
.sess
149+
.struct_span_err(a.span, "`#[deprecated]` cannot be used in staged API")
150+
.span_label(a.span, "use `#[rustc_deprecated]` instead")
151+
.span_label(item_sp, "")
152+
.emit();
140153
}
141154
} else {
142155
self.recurse_with_stability_attrs(
@@ -185,7 +198,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
185198
if kind == AnnotationKind::Prohibited
186199
|| (kind == AnnotationKind::Container && stab.level.is_stable() && is_deprecated)
187200
{
188-
self.tcx.sess.span_err(item_sp, "This stability annotation is useless");
201+
self.tcx.sess.span_err(item_sp, "this stability annotation is useless");
189202
}
190203

191204
debug!("annotate: found {:?}", stab);
@@ -202,15 +215,15 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
202215
{
203216
match stab_v.parse::<u64>() {
204217
Err(_) => {
205-
self.tcx.sess.span_err(item_sp, "Invalid stability version found");
218+
self.tcx.sess.span_err(item_sp, "invalid stability version found");
206219
break;
207220
}
208221
Ok(stab_vp) => match dep_v.parse::<u64>() {
209222
Ok(dep_vp) => match dep_vp.cmp(&stab_vp) {
210223
Ordering::Less => {
211224
self.tcx.sess.span_err(
212225
item_sp,
213-
"An API can't be stabilized after it is deprecated",
226+
"an API can't be stabilized after it is deprecated",
214227
);
215228
break;
216229
}
@@ -221,7 +234,7 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
221234
if dep_v != "TBD" {
222235
self.tcx
223236
.sess
224-
.span_err(item_sp, "Invalid deprecation version found");
237+
.span_err(item_sp, "invalid deprecation version found");
225238
}
226239
break;
227240
}
@@ -237,7 +250,9 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
237250
if stab.is_none() {
238251
debug!("annotate: stab not found, parent = {:?}", self.parent_stab);
239252
if let Some(stab) = self.parent_stab {
240-
if inherit_deprecation.yes() && stab.level.is_unstable() {
253+
if inherit_deprecation.yes() && stab.level.is_unstable()
254+
|| inherit_from_parent.yes()
255+
{
241256
self.index.stab_map.insert(hir_id, stab);
242257
}
243258
}
@@ -368,6 +383,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
368383
AnnotationKind::Required,
369384
InheritDeprecation::Yes,
370385
InheritConstStability::No,
386+
InheritStability::Yes,
371387
|_| {},
372388
)
373389
}
@@ -382,6 +398,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
382398
kind,
383399
InheritDeprecation::Yes,
384400
const_stab_inherit,
401+
InheritStability::No,
385402
|v| intravisit::walk_item(v, i),
386403
);
387404
self.in_trait_impl = orig_in_trait_impl;
@@ -395,6 +412,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
395412
AnnotationKind::Required,
396413
InheritDeprecation::Yes,
397414
InheritConstStability::No,
415+
InheritStability::No,
398416
|v| {
399417
intravisit::walk_trait_item(v, ti);
400418
},
@@ -411,6 +429,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
411429
kind,
412430
InheritDeprecation::Yes,
413431
InheritConstStability::No,
432+
InheritStability::No,
414433
|v| {
415434
intravisit::walk_impl_item(v, ii);
416435
},
@@ -425,6 +444,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
425444
AnnotationKind::Required,
426445
InheritDeprecation::Yes,
427446
InheritConstStability::No,
447+
InheritStability::Yes,
428448
|v| {
429449
if let Some(ctor_hir_id) = var.data.ctor_hir_id() {
430450
v.annotate(
@@ -434,6 +454,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
434454
AnnotationKind::Required,
435455
InheritDeprecation::Yes,
436456
InheritConstStability::No,
457+
InheritStability::No,
437458
|_| {},
438459
);
439460
}
@@ -451,6 +472,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
451472
AnnotationKind::Required,
452473
InheritDeprecation::Yes,
453474
InheritConstStability::No,
475+
InheritStability::Yes,
454476
|v| {
455477
intravisit::walk_struct_field(v, s);
456478
},
@@ -465,6 +487,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
465487
AnnotationKind::Required,
466488
InheritDeprecation::Yes,
467489
InheritConstStability::No,
490+
InheritStability::No,
468491
|v| {
469492
intravisit::walk_foreign_item(v, i);
470493
},
@@ -479,6 +502,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
479502
AnnotationKind::Required,
480503
InheritDeprecation::Yes,
481504
InheritConstStability::No,
505+
InheritStability::No,
482506
|_| {},
483507
);
484508
}
@@ -499,6 +523,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Annotator<'a, 'tcx> {
499523
kind,
500524
InheritDeprecation::No,
501525
InheritConstStability::No,
526+
InheritStability::No,
502527
|v| {
503528
intravisit::walk_generic_param(v, p);
504529
},
@@ -669,6 +694,7 @@ fn new_index(tcx: TyCtxt<'tcx>) -> Index<'tcx> {
669694
AnnotationKind::Required,
670695
InheritDeprecation::Yes,
671696
InheritConstStability::No,
697+
InheritStability::No,
672698
|v| intravisit::walk_crate(v, krate),
673699
);
674700
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
// #[deprecated] cannot be used in staged API
2-
31
#![feature(staged_api)]
4-
52
#![stable(feature = "stable_test_feature", since = "1.0.0")]
6-
7-
#[deprecated]
8-
fn main() { } //~ ERROR `#[deprecated]` cannot be used in staged API
3+
#[deprecated] //~ ERROR `#[deprecated]` cannot be used in staged API
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: `#[deprecated]` cannot be used in staged API; use `#[rustc_deprecated]` instead
2-
--> $DIR/deprecation-in-staged-api.rs:8:1
1+
error: `#[deprecated]` cannot be used in staged API
2+
--> $DIR/deprecation-in-staged-api.rs:3:1
33
|
4-
LL | fn main() { }
5-
| ^^^^^^^^^^^^^
4+
LL | #[deprecated]
5+
| ^^^^^^^^^^^^^ use `#[rustc_deprecated]` instead
6+
LL | fn main() {}
7+
| ------------
68

79
error: aborting due to previous error
810

src/test/ui/lint/auxiliary/lint_stability_fields.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,35 @@
33

44
#[stable(feature = "rust1", since = "1.0.0")]
55
pub struct Stable {
6-
#[stable(feature = "rust1", since = "1.0.0")]
7-
pub inherit: u8, // it's a lie (stable doesn't inherit)
6+
pub inherit: u8,
87
#[unstable(feature = "unstable_test_feature", issue = "none")]
98
pub override1: u8,
109
#[rustc_deprecated(since = "1.0.0", reason = "text")]
1110
#[unstable(feature = "unstable_test_feature", issue = "none")]
1211
pub override2: u8,
12+
#[stable(feature = "rust2", since = "2.0.0")]
13+
pub override3: u8,
1314
}
1415

1516
#[stable(feature = "rust1", since = "1.0.0")]
16-
pub struct Stable2(#[stable(feature = "rust1", since = "1.0.0")] pub u8,
17+
pub struct Stable2(#[stable(feature = "rust2", since = "2.0.0")] pub u8,
1718
#[unstable(feature = "unstable_test_feature", issue = "none")] pub u8,
1819
#[unstable(feature = "unstable_test_feature", issue = "none")]
19-
#[rustc_deprecated(since = "1.0.0", reason = "text")] pub u8);
20+
#[rustc_deprecated(since = "1.0.0", reason = "text")] pub u8,
21+
pub u8);
22+
23+
#[stable(feature = "rust1", since = "1.0.0")]
24+
pub enum Stable3 {
25+
Inherit(u8),
26+
InheritOverride(#[stable(feature = "rust2", since = "2.0.0")] u8),
27+
#[stable(feature = "rust2", since = "2.0.0")]
28+
Override1,
29+
#[unstable(feature = "unstable_test_feature", issue = "none")]
30+
Override2,
31+
#[rustc_deprecated(since = "1.0.0", reason = "text")]
32+
#[unstable(feature = "unstable_test_feature", issue = "none")]
33+
Override3,
34+
}
2035

2136
#[unstable(feature = "unstable_test_feature", issue = "none")]
2237
pub struct Unstable {

src/test/ui/lint/lint-stability-fields-deprecated.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,38 @@ mod cross_crate {
1717
override1: 2,
1818
override2: 3,
1919
//~^ ERROR use of deprecated field
20+
override3: 4,
2021
};
2122

2223
let _ = x.inherit;
2324
let _ = x.override1;
2425
let _ = x.override2;
2526
//~^ ERROR use of deprecated field
27+
let _ = x.override3;
2628

2729
let Stable {
2830
inherit: _,
2931
override1: _,
30-
override2: _
32+
override2: _,
3133
//~^ ERROR use of deprecated field
34+
override3: _,
3235
} = x;
3336
// all fine
3437
let Stable { .. } = x;
3538

36-
let x = Stable2(1, 2, 3);
39+
let x = Stable2(1, 2, 3, 4);
3740

3841
let _ = x.0;
3942
let _ = x.1;
4043
let _ = x.2;
4144
//~^ ERROR use of deprecated field
45+
let _ = x.3;
4246

4347
let Stable2(_,
4448
_,
49+
_,
50+
//~^ ERROR use of deprecated field
4551
_)
46-
//~^ ERROR use of deprecated field
4752
= x;
4853
// all fine
4954
let Stable2(..) = x;

0 commit comments

Comments
 (0)