Skip to content

Commit 15ba87f

Browse files
committed
auto merge of #18887 : aturon/rust/controlled-inherit, r=alexcrichton
This patch tweaks the stability inheritance infrastructure so that `#{stable]` attributes are not inherited. Doing so solves two problems: 1. It allows us to mark module *names* as stable without accidentally marking the items they contain as stable. 2. It means that a `#[stable]` attribution must always appear directly on the item it applies to, which makes it easier for reviewers to catch changes to stable APIs. Fixes #17484
2 parents 37ea270 + 8352195 commit 15ba87f

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/librustc/middle/stability.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ use syntax::{attr, visit};
1717
use syntax::ast;
1818
use syntax::ast::{Attribute, Block, Crate, DefId, FnDecl, NodeId, Variant};
1919
use syntax::ast::{Item, RequiredMethod, ProvidedMethod, TraitItem};
20-
use syntax::ast::{TypeMethod, Method, Generics, StructDef, StructField};
21-
use syntax::ast::{Ident, TypeTraitItem};
20+
use syntax::ast::{TypeMethod, Method, Generics, StructField, TypeTraitItem};
2221
use syntax::ast_util::is_local;
2322
use syntax::attr::Stability;
2423
use syntax::visit::{FnKind, FkMethod, Visitor};
@@ -48,9 +47,15 @@ impl Annotator {
4847
match attr::find_stability(attrs.as_slice()) {
4948
Some(stab) => {
5049
self.index.local.insert(id, stab.clone());
51-
let parent = replace(&mut self.parent, Some(stab));
52-
f(self);
53-
self.parent = parent;
50+
51+
// Don't inherit #[stable]
52+
if stab.level != attr::Stable {
53+
let parent = replace(&mut self.parent, Some(stab));
54+
f(self);
55+
self.parent = parent;
56+
} else {
57+
f(self);
58+
}
5459
}
5560
None => {
5661
self.parent.clone().map(|stab| self.index.local.insert(id, stab));
@@ -63,6 +68,15 @@ impl Annotator {
6368
impl<'v> Visitor<'v> for Annotator {
6469
fn visit_item(&mut self, i: &Item) {
6570
self.annotate(i.id, &i.attrs, |v| visit::walk_item(v, i));
71+
72+
match i.node {
73+
ast::ItemStruct(ref sd, _) => {
74+
sd.ctor_id.map(|id| {
75+
self.annotate(id, &i.attrs, |_| {})
76+
});
77+
}
78+
_ => {}
79+
}
6680
}
6781

6882
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl,
@@ -95,13 +109,6 @@ impl<'v> Visitor<'v> for Annotator {
95109
self.annotate(var.node.id, &var.node.attrs, |v| visit::walk_variant(v, var, g))
96110
}
97111

98-
fn visit_struct_def(&mut self, s: &StructDef, _: Ident, _: &Generics, _: NodeId) {
99-
match s.ctor_id {
100-
Some(id) => self.annotate(id, &vec![], |v| visit::walk_struct_def(v, s)),
101-
None => visit::walk_struct_def(self, s)
102-
}
103-
}
104-
105112
fn visit_struct_field(&mut self, s: &StructField) {
106113
self.annotate(s.node.id, &s.node.attrs, |v| visit::walk_struct_field(v, s));
107114
}

src/test/auxiliary/inherited_stability.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ pub fn stable() {}
1818

1919
#[stable]
2020
pub mod stable_mod {
21-
#[experimental]
2221
pub fn experimental() {}
2322

23+
#[stable]
2424
pub fn stable() {}
2525
}
2626

27+
#[unstable]
28+
pub mod unstable_mod {
29+
#[experimental]
30+
pub fn experimental() {}
31+
32+
pub fn unstable() {}
33+
}
34+
2735
pub mod experimental_mod {
2836
pub fn experimental() {}
2937

@@ -33,9 +41,9 @@ pub mod experimental_mod {
3341

3442
#[stable]
3543
pub trait Stable {
36-
#[experimental]
3744
fn experimental(&self);
3845

46+
#[stable]
3947
fn stable(&self);
4048
}
4149

src/test/compile-fail/lint-stability.rs

+3
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ mod inheritance {
165165
stable_mod::experimental(); //~ ERROR use of experimental item
166166
stable_mod::stable();
167167

168+
unstable_mod::experimental(); //~ ERROR use of experimental item
169+
unstable_mod::unstable(); //~ ERROR use of unstable item
170+
168171
experimental_mod::experimental(); //~ ERROR use of experimental item
169172
experimental_mod::stable();
170173

0 commit comments

Comments
 (0)