Skip to content

Commit f2ae7b5

Browse files
committed
Store 'since' attribute as parsed Version
1 parent 151256b commit f2ae7b5

File tree

4 files changed

+50
-53
lines changed

4 files changed

+50
-53
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,22 @@ pub enum StabilityLevel {
144144
/// `#[stable]`
145145
Stable {
146146
/// Rust release which stabilized this feature.
147-
since: Symbol,
147+
since: Since,
148148
/// Is this item allowed to be referred to on stable, despite being contained in unstable
149149
/// modules?
150150
allowed_through_unstable_modules: bool,
151151
},
152152
}
153153

154+
/// Rust release in which a feature is stabilized.
155+
#[derive(Encodable, Decodable, PartialEq, Copy, Clone, Debug, Eq, Hash)]
156+
#[derive(HashStable_Generic)]
157+
pub enum Since {
158+
Version(Version),
159+
/// Stabilized in the upcoming version, whatever number that is.
160+
Current,
161+
}
162+
154163
impl StabilityLevel {
155164
pub fn is_unstable(&self) -> bool {
156165
matches!(self, StabilityLevel::Unstable { .. })
@@ -372,9 +381,9 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
372381

373382
let since = if let Some(since) = since {
374383
if since.as_str() == VERSION_PLACEHOLDER {
375-
Ok(rust_version_symbol())
376-
} else if parse_version(since.as_str(), false).is_some() {
377-
Ok(since)
384+
Ok(Since::Current)
385+
} else if let Some(version) = parse_version(since.as_str(), false) {
386+
Ok(Since::Version(version))
378387
} else {
379388
Err(sess.emit_err(session_diagnostics::InvalidSince { span: attr.span }))
380389
}
@@ -556,11 +565,12 @@ fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &ParseSess, features: &F
556565
}
557566
}
558567

559-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
560-
struct Version {
561-
major: u16,
562-
minor: u16,
563-
patch: u16,
568+
#[derive(Encodable, Decodable, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
569+
#[derive(HashStable_Generic)]
570+
pub struct Version {
571+
pub major: u16,
572+
pub minor: u16,
573+
pub patch: u16,
564574
}
565575

566576
fn parse_version(s: &str, allow_appendix: bool) -> Option<Version> {

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,6 @@ passes_invalid_macro_export_arguments = `{$name}` isn't a valid `#[macro_export]
402402
403403
passes_invalid_macro_export_arguments_too_many_items = `#[macro_export]` can only take 1 or 0 arguments
404404
405-
passes_invalid_stability =
406-
invalid stability version found
407-
.label = invalid stability version
408-
.item = the stability attribute annotates this item
409-
410405
passes_lang_item_fn_with_target_feature =
411406
`{$name}` language item function is not allowed to have `#[target_feature]`
412407
.label = `{$name}` language item function is not allowed to have `#[target_feature]`

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,16 +1504,6 @@ pub struct UselessStability {
15041504
pub item_sp: Span,
15051505
}
15061506

1507-
#[derive(Diagnostic)]
1508-
#[diag(passes_invalid_stability)]
1509-
pub struct InvalidStability {
1510-
#[primary_span]
1511-
#[label]
1512-
pub span: Span,
1513-
#[label(passes_item)]
1514-
pub item_sp: Span,
1515-
}
1516-
15171507
#[derive(Diagnostic)]
15181508
#[diag(passes_cannot_stabilize_deprecated)]
15191509
pub struct CannotStabilizeDeprecated {

compiler/rustc_passes/src/stability.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use crate::errors;
55
use rustc_attr::{
6-
self as attr, rust_version_symbol, ConstStability, Stability, StabilityLevel, Unstable,
6+
self as attr, rust_version_symbol, ConstStability, Since, Stability, StabilityLevel, Unstable,
77
UnstableReason, VERSION_PLACEHOLDER,
88
};
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
@@ -226,37 +226,39 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
226226
if let (&Some(dep_since), &attr::Stable { since: stab_since, .. }) =
227227
(&depr.as_ref().and_then(|(d, _)| d.since), &stab.level)
228228
{
229-
// Explicit version of iter::order::lt to handle parse errors properly
230-
for (dep_v, stab_v) in
231-
iter::zip(dep_since.as_str().split('.'), stab_since.as_str().split('.'))
232-
{
233-
match stab_v.parse::<u64>() {
234-
Err(_) => {
235-
self.tcx.sess.emit_err(errors::InvalidStability { span, item_sp });
236-
break;
237-
}
238-
Ok(stab_vp) => match dep_v.parse::<u64>() {
239-
Ok(dep_vp) => match dep_vp.cmp(&stab_vp) {
240-
Ordering::Less => {
241-
self.tcx.sess.emit_err(errors::CannotStabilizeDeprecated {
242-
span,
243-
item_sp,
244-
});
229+
match stab_since {
230+
Since::Current => {
231+
self.tcx.sess.emit_err(errors::CannotStabilizeDeprecated { span, item_sp });
232+
}
233+
Since::Version(stab_since) => {
234+
// Explicit version of iter::order::lt to handle parse errors properly
235+
for (dep_v, stab_v) in iter::zip(
236+
dep_since.as_str().split('.'),
237+
[stab_since.major, stab_since.minor, stab_since.patch],
238+
) {
239+
match dep_v.parse::<u64>() {
240+
Ok(dep_vp) => match dep_vp.cmp(&u64::from(stab_v)) {
241+
Ordering::Less => {
242+
self.tcx.sess.emit_err(errors::CannotStabilizeDeprecated {
243+
span,
244+
item_sp,
245+
});
246+
break;
247+
}
248+
Ordering::Equal => continue,
249+
Ordering::Greater => break,
250+
},
251+
Err(_) => {
252+
if dep_v != "TBD" {
253+
self.tcx.sess.emit_err(errors::InvalidDeprecationVersion {
254+
span,
255+
item_sp,
256+
});
257+
}
245258
break;
246259
}
247-
Ordering::Equal => continue,
248-
Ordering::Greater => break,
249-
},
250-
Err(_) => {
251-
if dep_v != "TBD" {
252-
self.tcx.sess.emit_err(errors::InvalidDeprecationVersion {
253-
span,
254-
item_sp,
255-
});
256-
}
257-
break;
258260
}
259-
},
261+
}
260262
}
261263
}
262264
}

0 commit comments

Comments
 (0)