Skip to content

Commit 9853f61

Browse files
committed
Validate #[stable(feature = "…")] identifier
1 parent e2068cd commit 9853f61

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use rustc_ast::{self as ast, attr};
44
use rustc_ast::{Attribute, LitKind, MetaItem, MetaItemKind, MetaItemLit, NestedMetaItem, NodeId};
55
use rustc_ast_pretty::pprust;
6+
use rustc_errors::ErrorGuaranteed;
67
use rustc_feature::{find_gated_cfg, is_builtin_attr_name, Features, GatedCfg};
78
use rustc_macros::HashStable_Generic;
89
use rustc_session::config::ExpectedValues;
@@ -367,19 +368,23 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
367368
since = Some(rust_version_symbol());
368369
}
369370

371+
let feature = match feature {
372+
Some(feature) if rustc_lexer::is_ident(feature.as_str()) => Ok(feature),
373+
Some(_bad_feature) => {
374+
Err(sess.emit_err(session_diagnostics::NonIdentFeature { span: attr.span }))
375+
}
376+
None => Err(sess.emit_err(session_diagnostics::MissingFeature { span: attr.span })),
377+
};
378+
379+
let since =
380+
since.ok_or_else(|| sess.emit_err(session_diagnostics::MissingSince { span: attr.span }));
381+
370382
match (feature, since) {
371-
(Some(feature), Some(since)) => {
383+
(Ok(feature), Ok(since)) => {
372384
let level = StabilityLevel::Stable { since, allowed_through_unstable_modules: false };
373385
Some((feature, level))
374386
}
375-
(None, _) => {
376-
sess.emit_err(session_diagnostics::MissingFeature { span: attr.span });
377-
None
378-
}
379-
_ => {
380-
sess.emit_err(session_diagnostics::MissingSince { span: attr.span });
381-
None
382-
}
387+
(Err(ErrorGuaranteed { .. }), _) | (_, Err(ErrorGuaranteed { .. })) => None,
383388
}
384389
}
385390

@@ -451,12 +456,19 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
451456
}
452457
}
453458

454-
match (feature, reason, issue) {
455-
(Some(feature), reason, Some(_)) => {
456-
if !rustc_lexer::is_ident(feature.as_str()) {
457-
sess.emit_err(session_diagnostics::NonIdentFeature { span: attr.span });
458-
return None;
459-
}
459+
let feature = match feature {
460+
Some(feature) if rustc_lexer::is_ident(feature.as_str()) => Ok(feature),
461+
Some(_bad_feature) => {
462+
Err(sess.emit_err(session_diagnostics::NonIdentFeature { span: attr.span }))
463+
}
464+
None => Err(sess.emit_err(session_diagnostics::MissingFeature { span: attr.span })),
465+
};
466+
467+
let issue =
468+
issue.ok_or_else(|| sess.emit_err(session_diagnostics::MissingIssue { span: attr.span }));
469+
470+
match (feature, issue) {
471+
(Ok(feature), Ok(_)) => {
460472
let level = StabilityLevel::Unstable {
461473
reason: UnstableReason::from_opt_reason(reason),
462474
issue: issue_num,
@@ -465,14 +477,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
465477
};
466478
Some((feature, level))
467479
}
468-
(None, _, _) => {
469-
sess.emit_err(session_diagnostics::MissingFeature { span: attr.span });
470-
return None;
471-
}
472-
_ => {
473-
sess.emit_err(session_diagnostics::MissingIssue { span: attr.span });
474-
return None;
475-
}
480+
(Err(ErrorGuaranteed { .. }), _) | (_, Err(ErrorGuaranteed { .. })) => None,
476481
}
477482
}
478483

0 commit comments

Comments
 (0)