Skip to content

Commit 82ed3f5

Browse files
committed
Validate since value in stable attribute
1 parent 01b9091 commit 82ed3f5

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

compiler/rustc_attr/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ attr_invalid_repr_hint_no_paren =
5858
attr_invalid_repr_hint_no_value =
5959
invalid representation hint: `{$name}` does not take a value
6060
61+
attr_invalid_since =
62+
'since' must be a Rust version number, such as "1.31.0"
63+
6164
attr_missing_feature =
6265
missing 'feature'
6366

compiler/rustc_attr/src/builtin.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,6 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
362362
}
363363
}
364364

365-
if let Some(s) = since
366-
&& s.as_str() == VERSION_PLACEHOLDER
367-
{
368-
since = Some(rust_version_symbol());
369-
}
370-
371365
let feature = match feature {
372366
Some(feature) if rustc_lexer::is_ident(feature.as_str()) => Ok(feature),
373367
Some(_bad_feature) => {
@@ -376,8 +370,17 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
376370
None => Err(sess.emit_err(session_diagnostics::MissingFeature { span: attr.span })),
377371
};
378372

379-
let since =
380-
since.ok_or_else(|| sess.emit_err(session_diagnostics::MissingSince { span: attr.span }));
373+
let since = if let Some(since) = since {
374+
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)
378+
} else {
379+
Err(sess.emit_err(session_diagnostics::InvalidSince { span: attr.span }))
380+
}
381+
} else {
382+
Err(sess.emit_err(session_diagnostics::MissingSince { span: attr.span }))
383+
};
381384

382385
match (feature, since) {
383386
(Ok(feature), Ok(since)) => {

compiler/rustc_attr/src/session_diagnostics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,13 @@ pub(crate) struct ExpectsFeatures {
370370
pub name: String,
371371
}
372372

373+
#[derive(Diagnostic)]
374+
#[diag(attr_invalid_since)]
375+
pub(crate) struct InvalidSince {
376+
#[primary_span]
377+
pub span: Span,
378+
}
379+
373380
#[derive(Diagnostic)]
374381
#[diag(attr_soft_no_args)]
375382
pub(crate) struct SoftNoArgs {

0 commit comments

Comments
 (0)