Skip to content

Commit 5b895a8

Browse files
committed
rustc: do not inherit #[stable]
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
1 parent 8f87538 commit 5b895a8

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
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
}

0 commit comments

Comments
 (0)