Skip to content

WIP inherited deprecation fixes #26040 #26061

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 3 commits into from
Jun 23, 2015
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
51 changes: 50 additions & 1 deletion src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use util::nodemap::{DefIdMap, FnvHashSet, FnvHashMap};
use util::ppaux::Repr;

use std::mem::replace;
use std::cmp::Ordering;

/// A stability index, giving the stability level for items and methods.
pub struct Index<'tcx> {
Expand Down Expand Up @@ -60,9 +61,57 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
if self.index.staged_api[&ast::LOCAL_CRATE] {
debug!("annotate(id = {:?}, attrs = {:?})", id, attrs);
match attr::find_stability(self.tcx.sess.diagnostic(), attrs, item_sp) {
Some(stab) => {
Some(mut stab) => {
debug!("annotate: found {:?}", stab);
// if parent is deprecated and we're not, inherit this by merging
// deprecated_since and its reason.
if let Some(parent_stab) = self.parent {
if parent_stab.deprecated_since.is_some()
&& stab.deprecated_since.is_none() {
stab.deprecated_since = parent_stab.deprecated_since.clone();
stab.reason = parent_stab.reason.clone();
}
}

let stab = self.tcx.intern_stability(stab);

// Check if deprecated_since < stable_since. If it is,
// this is *almost surely* an accident.
let deprecated_predates_stable = match (stab.deprecated_since.as_ref(),
stab.since.as_ref()) {
(Some(dep_since), Some(stab_since)) => {
// explicit version of iter::order::lt to handle parse errors properly
let mut is_less = false;
for (dep_v, stab_v) in dep_since.split(".").zip(stab_since.split(".")) {
match (dep_v.parse::<u64>(), stab_v.parse::<u64>()) {
(Ok(dep_v), Ok(stab_v)) => match dep_v.cmp(&stab_v) {
Ordering::Less => {
is_less = true;
break;
}
Ordering::Equal => { continue; }
Ordering::Greater => { break; }
},
_ => {
self.tcx.sess.span_err(item_sp,
"Invalid stability or deprecation version found");
// act like it isn't less because the question is now
// nonsensical, and this makes us not do anything else
// interesting.
break;
}
}
}
is_less
},
_ => false,
};

if deprecated_predates_stable {
self.tcx.sess.span_err(item_sp,
"An API can't be stabilized after it is deprecated");
}

self.index.map.insert(local_def(id), Some(stab));

// Don't inherit #[stable(feature = "rust1", since = "1.0.0")]
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ fn find_stability_generic<'a,
-> (Option<Stability>, Vec<&'a AM>) {

let mut stab: Option<Stability> = None;
let mut deprecated: Option<(InternedString, Option<InternedString>)> = None;
let mut deprecated: Option<(Option<InternedString>, Option<InternedString>)> = None;
let mut used_attrs: Vec<&'a AM> = vec![];

'outer: for attr in attrs {
Expand Down Expand Up @@ -484,7 +484,7 @@ fn find_stability_generic<'a,
diagnostic.span_err(item_sp, "multiple deprecated attributes");
}

deprecated = Some((since.unwrap_or(intern_and_get_ident("bogus")), reason));
deprecated = Some((since, reason));
}
}

Expand All @@ -493,7 +493,7 @@ fn find_stability_generic<'a,
match stab {
Some(ref mut s) => {
let (since, reason) = deprecated.unwrap();
s.deprecated_since = Some(since);
s.deprecated_since = since;
s.reason = reason;
}
None => {
Expand Down
35 changes: 30 additions & 5 deletions src/test/compile-fail/lint-stability-fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,20 @@ mod cross_crate {
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable
override1: 2,
override2: 3, //~ ERROR use of unstable
//~^ ERROR use of deprecated item
override2: 3,
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable
};

let _ = x.inherit;
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable
let _ = x.override1;
let _ = x.override2; //~ ERROR use of unstable
//~^ ERROR use of deprecated item
let _ = x.override2;
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable

let Deprecated {
//~^ ERROR use of deprecated item
Expand All @@ -132,7 +138,10 @@ mod cross_crate {
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable
override1: _,
override2: _ //~ ERROR use of unstable
//~^ ERROR use of deprecated item
override2: _
//~^ ERROR use of unstable
//~^^ ERROR use of deprecated item
} = x;

let Deprecated
Expand All @@ -149,7 +158,10 @@ mod cross_crate {
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable
let _ = x.1;
let _ = x.2; //~ ERROR use of unstable
//~^ ERROR use of deprecated item
let _ = x.2;
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable

let Deprecated2
//~^ ERROR use of deprecated item
Expand All @@ -158,7 +170,10 @@ mod cross_crate {
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable
_,
_) //~ ERROR use of unstable
//~^ ERROR use of deprecated item
_)
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable
= x;
let Deprecated2
//~^ ERROR use of deprecated item
Expand Down Expand Up @@ -300,20 +315,26 @@ mod this_crate {
inherit: 1,
//~^ ERROR use of deprecated item
override1: 2,
//~^ ERROR use of deprecated item
override2: 3,
//~^ ERROR use of deprecated item
};

let _ = x.inherit;
//~^ ERROR use of deprecated item
let _ = x.override1;
//~^ ERROR use of deprecated item
let _ = x.override2;
//~^ ERROR use of deprecated item

let Deprecated {
//~^ ERROR use of deprecated item
inherit: _,
//~^ ERROR use of deprecated item
override1: _,
//~^ ERROR use of deprecated item
override2: _
//~^ ERROR use of deprecated item
} = x;

let Deprecated
Expand All @@ -327,14 +348,18 @@ mod this_crate {
let _ = x.0;
//~^ ERROR use of deprecated item
let _ = x.1;
//~^ ERROR use of deprecated item
let _ = x.2;
//~^ ERROR use of deprecated item

let Deprecated2
//~^ ERROR use of deprecated item
(_,
//~^ ERROR use of deprecated item
_,
//~^ ERROR use of deprecated item
_)
//~^ ERROR use of deprecated item
= x;
let Deprecated2
//~^ ERROR use of deprecated item
Expand Down
12 changes: 9 additions & 3 deletions src/test/compile-fail/lint-stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,11 @@ mod cross_crate {
<Foo as Trait>::trait_stable_text(&foo);

let _ = DeprecatedStruct { i: 0 }; //~ ERROR use of deprecated item
let _ = DeprecatedUnstableStruct { i: 0 }; //~ ERROR use of deprecated item
//~^ ERROR use of unstable library feature
let _ = DeprecatedUnstableStruct {
//~^ ERROR use of deprecated item
//~^^ ERROR use of unstable library feature
i: 0 //~ ERROR use of deprecated item
};
let _ = UnstableStruct { i: 0 }; //~ ERROR use of unstable library feature
let _ = StableStruct { i: 0 };

Expand Down Expand Up @@ -417,7 +420,10 @@ mod this_crate {
<Foo>::trait_stable_text(&foo);
<Foo as Trait>::trait_stable_text(&foo);

let _ = DeprecatedStruct { i: 0 }; //~ ERROR use of deprecated item
let _ = DeprecatedStruct {
//~^ ERROR use of deprecated item
i: 0 //~ ERROR use of deprecated item
};
let _ = UnstableStruct { i: 0 };
let _ = StableStruct { i: 0 };

Expand Down
1 change: 1 addition & 0 deletions src/test/compile-fail/stability-attribute-sanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn multiple3() { } //~ ERROR multiple stability levels
#[deprecated(since = "b")]
#[deprecated(since = "b")]
fn multiple4() { } //~ ERROR multiple deprecated attributes
//~^ ERROR Invalid stability or deprecation version found

#[deprecated(since = "a")]
fn deprecated_without_unstable_or_stable() { } //~ ERROR deprecated attribute must be paired
Expand Down