Skip to content

Commit 6fed735

Browse files
committed
Auto merge of #26061 - Gankro:inherit-dep, r=brson
Uncertain if this is the desired effect/strategy/testing. r? @aturon
2 parents 2ad26e8 + 2962b26 commit 6fed735

File tree

5 files changed

+93
-12
lines changed

5 files changed

+93
-12
lines changed

src/librustc/middle/stability.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use syntax::feature_gate::emit_feature_err;
3030
use util::nodemap::{DefIdMap, FnvHashSet, FnvHashMap};
3131

3232
use std::mem::replace;
33+
use std::cmp::Ordering;
3334

3435
/// A stability index, giving the stability level for items and methods.
3536
pub struct Index<'tcx> {
@@ -59,9 +60,57 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
5960
if self.index.staged_api[&ast::LOCAL_CRATE] {
6061
debug!("annotate(id = {:?}, attrs = {:?})", id, attrs);
6162
match attr::find_stability(self.tcx.sess.diagnostic(), attrs, item_sp) {
62-
Some(stab) => {
63+
Some(mut stab) => {
6364
debug!("annotate: found {:?}", stab);
65+
// if parent is deprecated and we're not, inherit this by merging
66+
// deprecated_since and its reason.
67+
if let Some(parent_stab) = self.parent {
68+
if parent_stab.deprecated_since.is_some()
69+
&& stab.deprecated_since.is_none() {
70+
stab.deprecated_since = parent_stab.deprecated_since.clone();
71+
stab.reason = parent_stab.reason.clone();
72+
}
73+
}
74+
6475
let stab = self.tcx.intern_stability(stab);
76+
77+
// Check if deprecated_since < stable_since. If it is,
78+
// this is *almost surely* an accident.
79+
let deprecated_predates_stable = match (stab.deprecated_since.as_ref(),
80+
stab.since.as_ref()) {
81+
(Some(dep_since), Some(stab_since)) => {
82+
// explicit version of iter::order::lt to handle parse errors properly
83+
let mut is_less = false;
84+
for (dep_v, stab_v) in dep_since.split(".").zip(stab_since.split(".")) {
85+
match (dep_v.parse::<u64>(), stab_v.parse::<u64>()) {
86+
(Ok(dep_v), Ok(stab_v)) => match dep_v.cmp(&stab_v) {
87+
Ordering::Less => {
88+
is_less = true;
89+
break;
90+
}
91+
Ordering::Equal => { continue; }
92+
Ordering::Greater => { break; }
93+
},
94+
_ => {
95+
self.tcx.sess.span_err(item_sp,
96+
"Invalid stability or deprecation version found");
97+
// act like it isn't less because the question is now
98+
// nonsensical, and this makes us not do anything else
99+
// interesting.
100+
break;
101+
}
102+
}
103+
}
104+
is_less
105+
},
106+
_ => false,
107+
};
108+
109+
if deprecated_predates_stable {
110+
self.tcx.sess.span_err(item_sp,
111+
"An API can't be stabilized after it is deprecated");
112+
}
113+
65114
self.index.map.insert(local_def(id), Some(stab));
66115

67116
// Don't inherit #[stable(feature = "rust1", since = "1.0.0")]

src/libsyntax/attr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ fn find_stability_generic<'a,
397397
-> (Option<Stability>, Vec<&'a AM>) {
398398

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

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

487-
deprecated = Some((since.unwrap_or(intern_and_get_ident("bogus")), reason));
487+
deprecated = Some((since, reason));
488488
}
489489
}
490490

@@ -493,7 +493,7 @@ fn find_stability_generic<'a,
493493
match stab {
494494
Some(ref mut s) => {
495495
let (since, reason) = deprecated.unwrap();
496-
s.deprecated_since = Some(since);
496+
s.deprecated_since = since;
497497
s.reason = reason;
498498
}
499499
None => {

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,20 @@ mod cross_crate {
116116
//~^ ERROR use of deprecated item
117117
//~^^ ERROR use of unstable
118118
override1: 2,
119-
override2: 3, //~ ERROR use of unstable
119+
//~^ ERROR use of deprecated item
120+
override2: 3,
121+
//~^ ERROR use of deprecated item
122+
//~^^ ERROR use of unstable
120123
};
121124

122125
let _ = x.inherit;
123126
//~^ ERROR use of deprecated item
124127
//~^^ ERROR use of unstable
125128
let _ = x.override1;
126-
let _ = x.override2; //~ ERROR use of unstable
129+
//~^ ERROR use of deprecated item
130+
let _ = x.override2;
131+
//~^ ERROR use of deprecated item
132+
//~^^ ERROR use of unstable
127133

128134
let Deprecated {
129135
//~^ ERROR use of deprecated item
@@ -132,7 +138,10 @@ mod cross_crate {
132138
//~^ ERROR use of deprecated item
133139
//~^^ ERROR use of unstable
134140
override1: _,
135-
override2: _ //~ ERROR use of unstable
141+
//~^ ERROR use of deprecated item
142+
override2: _
143+
//~^ ERROR use of unstable
144+
//~^^ ERROR use of deprecated item
136145
} = x;
137146

138147
let Deprecated
@@ -149,7 +158,10 @@ mod cross_crate {
149158
//~^ ERROR use of deprecated item
150159
//~^^ ERROR use of unstable
151160
let _ = x.1;
152-
let _ = x.2; //~ ERROR use of unstable
161+
//~^ ERROR use of deprecated item
162+
let _ = x.2;
163+
//~^ ERROR use of deprecated item
164+
//~^^ ERROR use of unstable
153165

154166
let Deprecated2
155167
//~^ ERROR use of deprecated item
@@ -158,7 +170,10 @@ mod cross_crate {
158170
//~^ ERROR use of deprecated item
159171
//~^^ ERROR use of unstable
160172
_,
161-
_) //~ ERROR use of unstable
173+
//~^ ERROR use of deprecated item
174+
_)
175+
//~^ ERROR use of deprecated item
176+
//~^^ ERROR use of unstable
162177
= x;
163178
let Deprecated2
164179
//~^ ERROR use of deprecated item
@@ -300,20 +315,26 @@ mod this_crate {
300315
inherit: 1,
301316
//~^ ERROR use of deprecated item
302317
override1: 2,
318+
//~^ ERROR use of deprecated item
303319
override2: 3,
320+
//~^ ERROR use of deprecated item
304321
};
305322

306323
let _ = x.inherit;
307324
//~^ ERROR use of deprecated item
308325
let _ = x.override1;
326+
//~^ ERROR use of deprecated item
309327
let _ = x.override2;
328+
//~^ ERROR use of deprecated item
310329

311330
let Deprecated {
312331
//~^ ERROR use of deprecated item
313332
inherit: _,
314333
//~^ ERROR use of deprecated item
315334
override1: _,
335+
//~^ ERROR use of deprecated item
316336
override2: _
337+
//~^ ERROR use of deprecated item
317338
} = x;
318339

319340
let Deprecated
@@ -327,14 +348,18 @@ mod this_crate {
327348
let _ = x.0;
328349
//~^ ERROR use of deprecated item
329350
let _ = x.1;
351+
//~^ ERROR use of deprecated item
330352
let _ = x.2;
353+
//~^ ERROR use of deprecated item
331354

332355
let Deprecated2
333356
//~^ ERROR use of deprecated item
334357
(_,
335358
//~^ ERROR use of deprecated item
336359
_,
360+
//~^ ERROR use of deprecated item
337361
_)
362+
//~^ ERROR use of deprecated item
338363
= x;
339364
let Deprecated2
340365
//~^ ERROR use of deprecated item

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ mod cross_crate {
128128
<Foo as Trait>::trait_stable_text(&foo);
129129

130130
let _ = DeprecatedStruct { i: 0 }; //~ ERROR use of deprecated item
131-
let _ = DeprecatedUnstableStruct { i: 0 }; //~ ERROR use of deprecated item
132-
//~^ ERROR use of unstable library feature
131+
let _ = DeprecatedUnstableStruct {
132+
//~^ ERROR use of deprecated item
133+
//~^^ ERROR use of unstable library feature
134+
i: 0 //~ ERROR use of deprecated item
135+
};
133136
let _ = UnstableStruct { i: 0 }; //~ ERROR use of unstable library feature
134137
let _ = StableStruct { i: 0 };
135138

@@ -417,7 +420,10 @@ mod this_crate {
417420
<Foo>::trait_stable_text(&foo);
418421
<Foo as Trait>::trait_stable_text(&foo);
419422

420-
let _ = DeprecatedStruct { i: 0 }; //~ ERROR use of deprecated item
423+
let _ = DeprecatedStruct {
424+
//~^ ERROR use of deprecated item
425+
i: 0 //~ ERROR use of deprecated item
426+
};
421427
let _ = UnstableStruct { i: 0 };
422428
let _ = StableStruct { i: 0 };
423429

src/test/compile-fail/stability-attribute-sanity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ fn multiple3() { } //~ ERROR multiple stability levels
8888
#[deprecated(since = "b")]
8989
#[deprecated(since = "b")]
9090
fn multiple4() { } //~ ERROR multiple deprecated attributes
91+
//~^ ERROR Invalid stability or deprecation version found
9192

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

0 commit comments

Comments
 (0)