Skip to content

rustc: do not inherit #[stable] #18887

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use syntax::{attr, visit};
use syntax::ast;
use syntax::ast::{Attribute, Block, Crate, DefId, FnDecl, NodeId, Variant};
use syntax::ast::{Item, RequiredMethod, ProvidedMethod, TraitItem};
use syntax::ast::{TypeMethod, Method, Generics, StructDef, StructField};
use syntax::ast::{Ident, TypeTraitItem};
use syntax::ast::{TypeMethod, Method, Generics, StructField, TypeTraitItem};
use syntax::ast_util::is_local;
use syntax::attr::Stability;
use syntax::visit::{FnKind, FkMethod, Visitor};
Expand Down Expand Up @@ -48,9 +47,15 @@ impl Annotator {
match attr::find_stability(attrs.as_slice()) {
Some(stab) => {
self.index.local.insert(id, stab.clone());
let parent = replace(&mut self.parent, Some(stab));
f(self);
self.parent = parent;

// Don't inherit #[stable]
if stab.level != attr::Stable {
let parent = replace(&mut self.parent, Some(stab));
f(self);
self.parent = parent;
} else {
f(self);
}
}
None => {
self.parent.clone().map(|stab| self.index.local.insert(id, stab));
Expand All @@ -63,6 +68,15 @@ impl Annotator {
impl<'v> Visitor<'v> for Annotator {
fn visit_item(&mut self, i: &Item) {
self.annotate(i.id, &i.attrs, |v| visit::walk_item(v, i));

match i.node {
ast::ItemStruct(ref sd, _) => {
sd.ctor_id.map(|id| {
self.annotate(id, &i.attrs, |_| {})
});
}
_ => {}
}
}

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

fn visit_struct_def(&mut self, s: &StructDef, _: Ident, _: &Generics, _: NodeId) {
match s.ctor_id {
Some(id) => self.annotate(id, &vec![], |v| visit::walk_struct_def(v, s)),
None => visit::walk_struct_def(self, s)
}
}

fn visit_struct_field(&mut self, s: &StructField) {
self.annotate(s.node.id, &s.node.attrs, |v| visit::walk_struct_field(v, s));
}
Expand Down
12 changes: 10 additions & 2 deletions src/test/auxiliary/inherited_stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ pub fn stable() {}

#[stable]
pub mod stable_mod {
#[experimental]
pub fn experimental() {}

#[stable]
pub fn stable() {}
}

#[unstable]
pub mod unstable_mod {
#[experimental]
pub fn experimental() {}

pub fn unstable() {}
}

pub mod experimental_mod {
pub fn experimental() {}

Expand All @@ -33,9 +41,9 @@ pub mod experimental_mod {

#[stable]
pub trait Stable {
#[experimental]
fn experimental(&self);

#[stable]
fn stable(&self);
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/compile-fail/lint-stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ mod inheritance {
stable_mod::experimental(); //~ ERROR use of experimental item
stable_mod::stable();

unstable_mod::experimental(); //~ ERROR use of experimental item
unstable_mod::unstable(); //~ ERROR use of unstable item

experimental_mod::experimental(); //~ ERROR use of experimental item
experimental_mod::stable();

Expand Down