1
Fork 0

Validate since value in stable attribute

This commit is contained in:
David Tolnay 2023-10-16 12:30:32 -07:00
parent 01b909174b
commit 82ed3f5e8b
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
3 changed files with 21 additions and 8 deletions

View file

@ -58,6 +58,9 @@ attr_invalid_repr_hint_no_paren =
attr_invalid_repr_hint_no_value =
invalid representation hint: `{$name}` does not take a value
attr_invalid_since =
'since' must be a Rust version number, such as "1.31.0"
attr_missing_feature =
missing 'feature'

View file

@ -362,12 +362,6 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
}
}
if let Some(s) = since
&& s.as_str() == VERSION_PLACEHOLDER
{
since = Some(rust_version_symbol());
}
let feature = match feature {
Some(feature) if rustc_lexer::is_ident(feature.as_str()) => Ok(feature),
Some(_bad_feature) => {
@ -376,8 +370,17 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
None => Err(sess.emit_err(session_diagnostics::MissingFeature { span: attr.span })),
};
let since =
since.ok_or_else(|| sess.emit_err(session_diagnostics::MissingSince { span: attr.span }));
let since = if let Some(since) = since {
if since.as_str() == VERSION_PLACEHOLDER {
Ok(rust_version_symbol())
} else if parse_version(since.as_str(), false).is_some() {
Ok(since)
} else {
Err(sess.emit_err(session_diagnostics::InvalidSince { span: attr.span }))
}
} else {
Err(sess.emit_err(session_diagnostics::MissingSince { span: attr.span }))
};
match (feature, since) {
(Ok(feature), Ok(since)) => {

View file

@ -370,6 +370,13 @@ pub(crate) struct ExpectsFeatures {
pub name: String,
}
#[derive(Diagnostic)]
#[diag(attr_invalid_since)]
pub(crate) struct InvalidSince {
#[primary_span]
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(attr_soft_no_args)]
pub(crate) struct SoftNoArgs {