Eliminate rustc_attr::builtin::handle_errors
This commit is contained in:
parent
3c92dcc6f3
commit
f5429a0186
1 changed files with 84 additions and 128 deletions
|
@ -31,16 +31,6 @@ pub fn is_builtin_attr(attr: &Attribute) -> bool {
|
||||||
attr.is_doc_comment() || attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
|
attr.is_doc_comment() || attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
|
||||||
}
|
}
|
||||||
|
|
||||||
enum AttrError {
|
|
||||||
MultipleItem(String),
|
|
||||||
UnknownMetaItem(String, &'static [&'static str]),
|
|
||||||
MissingSince,
|
|
||||||
NonIdentFeature,
|
|
||||||
MissingFeature,
|
|
||||||
MultipleStabilityLevels,
|
|
||||||
UnsupportedLiteral(UnsupportedLiteralReason, /* is_bytestr */ bool),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) enum UnsupportedLiteralReason {
|
pub(crate) enum UnsupportedLiteralReason {
|
||||||
Generic,
|
Generic,
|
||||||
CfgString,
|
CfgString,
|
||||||
|
@ -48,37 +38,6 @@ pub(crate) enum UnsupportedLiteralReason {
|
||||||
DeprecatedKvPair,
|
DeprecatedKvPair,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
|
|
||||||
match error {
|
|
||||||
AttrError::MultipleItem(item) => {
|
|
||||||
sess.emit_err(session_diagnostics::MultipleItem { span, item });
|
|
||||||
}
|
|
||||||
AttrError::UnknownMetaItem(item, expected) => {
|
|
||||||
sess.emit_err(session_diagnostics::UnknownMetaItem { span, item, expected });
|
|
||||||
}
|
|
||||||
AttrError::MissingSince => {
|
|
||||||
sess.emit_err(session_diagnostics::MissingSince { span });
|
|
||||||
}
|
|
||||||
AttrError::NonIdentFeature => {
|
|
||||||
sess.emit_err(session_diagnostics::NonIdentFeature { span });
|
|
||||||
}
|
|
||||||
AttrError::MissingFeature => {
|
|
||||||
sess.emit_err(session_diagnostics::MissingFeature { span });
|
|
||||||
}
|
|
||||||
AttrError::MultipleStabilityLevels => {
|
|
||||||
sess.emit_err(session_diagnostics::MultipleStabilityLevels { span });
|
|
||||||
}
|
|
||||||
AttrError::UnsupportedLiteral(reason, is_bytestr) => {
|
|
||||||
sess.emit_err(session_diagnostics::UnsupportedLiteral {
|
|
||||||
span,
|
|
||||||
reason,
|
|
||||||
is_bytestr,
|
|
||||||
start_point_span: sess.source_map().start_point(span),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
|
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
|
||||||
pub enum InlineAttr {
|
pub enum InlineAttr {
|
||||||
None,
|
None,
|
||||||
|
@ -241,7 +200,8 @@ pub fn find_stability(
|
||||||
sym::rustc_allowed_through_unstable_modules => allowed_through_unstable_modules = true,
|
sym::rustc_allowed_through_unstable_modules => allowed_through_unstable_modules = true,
|
||||||
sym::unstable => {
|
sym::unstable => {
|
||||||
if stab.is_some() {
|
if stab.is_some() {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
|
sess.parse_sess
|
||||||
|
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,7 +211,8 @@ pub fn find_stability(
|
||||||
}
|
}
|
||||||
sym::stable => {
|
sym::stable => {
|
||||||
if stab.is_some() {
|
if stab.is_some() {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
|
sess.parse_sess
|
||||||
|
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if let Some((feature, level)) = parse_stability(sess, attr) {
|
if let Some((feature, level)) = parse_stability(sess, attr) {
|
||||||
|
@ -295,7 +256,8 @@ pub fn find_const_stability(
|
||||||
sym::rustc_promotable => promotable = true,
|
sym::rustc_promotable => promotable = true,
|
||||||
sym::rustc_const_unstable => {
|
sym::rustc_const_unstable => {
|
||||||
if const_stab.is_some() {
|
if const_stab.is_some() {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
|
sess.parse_sess
|
||||||
|
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -306,7 +268,8 @@ pub fn find_const_stability(
|
||||||
}
|
}
|
||||||
sym::rustc_const_stable => {
|
sym::rustc_const_stable => {
|
||||||
if const_stab.is_some() {
|
if const_stab.is_some() {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
|
sess.parse_sess
|
||||||
|
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if let Some((feature, level)) = parse_stability(sess, attr) {
|
if let Some((feature, level)) = parse_stability(sess, attr) {
|
||||||
|
@ -340,7 +303,8 @@ pub fn find_body_stability(
|
||||||
for attr in attrs {
|
for attr in attrs {
|
||||||
if attr.has_name(sym::rustc_default_body_unstable) {
|
if attr.has_name(sym::rustc_default_body_unstable) {
|
||||||
if body_stab.is_some() {
|
if body_stab.is_some() {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
|
sess.parse_sess
|
||||||
|
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -355,11 +319,10 @@ pub fn find_body_stability(
|
||||||
|
|
||||||
fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option<Symbol>) -> Option<()> {
|
fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option<Symbol>) -> Option<()> {
|
||||||
if item.is_some() {
|
if item.is_some() {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::MultipleItem {
|
||||||
&sess.parse_sess,
|
span: meta.span,
|
||||||
meta.span,
|
item: pprust::path_to_string(&meta.path),
|
||||||
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
|
});
|
||||||
);
|
|
||||||
None
|
None
|
||||||
} else if let Some(v) = meta.value_str() {
|
} else if let Some(v) = meta.value_str() {
|
||||||
*item = Some(v);
|
*item = Some(v);
|
||||||
|
@ -380,11 +343,12 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
|
||||||
let mut since = None;
|
let mut since = None;
|
||||||
for meta in metas {
|
for meta in metas {
|
||||||
let Some(mi) = meta.meta_item() else {
|
let Some(mi) = meta.meta_item() else {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral {
|
||||||
&sess.parse_sess,
|
span: meta.span(),
|
||||||
meta.span(),
|
reason: UnsupportedLiteralReason::Generic,
|
||||||
AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
|
is_bytestr: false,
|
||||||
);
|
start_point_span: sess.parse_sess.source_map().start_point(meta.span()),
|
||||||
|
});
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -392,14 +356,11 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
|
||||||
sym::feature => insert_or_error(sess, mi, &mut feature)?,
|
sym::feature => insert_or_error(sess, mi, &mut feature)?,
|
||||||
sym::since => insert_or_error(sess, mi, &mut since)?,
|
sym::since => insert_or_error(sess, mi, &mut since)?,
|
||||||
_ => {
|
_ => {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem {
|
||||||
&sess.parse_sess,
|
span: meta.span(),
|
||||||
meta.span(),
|
item: pprust::path_to_string(&mi.path),
|
||||||
AttrError::UnknownMetaItem(
|
expected: &["feature", "since"],
|
||||||
pprust::path_to_string(&mi.path),
|
});
|
||||||
&["feature", "since"],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -417,11 +378,11 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
|
||||||
Some((feature, level))
|
Some((feature, level))
|
||||||
}
|
}
|
||||||
(None, _) => {
|
(None, _) => {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
|
sess.parse_sess.emit_err(session_diagnostics::MissingFeature { span: attr.span });
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
|
sess.parse_sess.emit_err(session_diagnostics::MissingSince { span: attr.span });
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -441,11 +402,12 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
|
||||||
let mut implied_by = None;
|
let mut implied_by = None;
|
||||||
for meta in metas {
|
for meta in metas {
|
||||||
let Some(mi) = meta.meta_item() else {
|
let Some(mi) = meta.meta_item() else {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral {
|
||||||
&sess.parse_sess,
|
span: meta.span(),
|
||||||
meta.span(),
|
reason: UnsupportedLiteralReason::Generic,
|
||||||
AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
|
is_bytestr: false,
|
||||||
);
|
start_point_span: sess.parse_sess.source_map().start_point(meta.span()),
|
||||||
|
});
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -484,14 +446,11 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
|
||||||
}
|
}
|
||||||
sym::implied_by => insert_or_error(sess, mi, &mut implied_by)?,
|
sym::implied_by => insert_or_error(sess, mi, &mut implied_by)?,
|
||||||
_ => {
|
_ => {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem {
|
||||||
&sess.parse_sess,
|
span: meta.span(),
|
||||||
meta.span(),
|
item: pprust::path_to_string(&mi.path),
|
||||||
AttrError::UnknownMetaItem(
|
expected: &["feature", "reason", "issue", "soft", "implied_by"],
|
||||||
pprust::path_to_string(&mi.path),
|
});
|
||||||
&["feature", "reason", "issue", "soft", "implied_by"],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -500,7 +459,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
|
||||||
match (feature, reason, issue) {
|
match (feature, reason, issue) {
|
||||||
(Some(feature), reason, Some(_)) => {
|
(Some(feature), reason, Some(_)) => {
|
||||||
if !rustc_lexer::is_ident(feature.as_str()) {
|
if !rustc_lexer::is_ident(feature.as_str()) {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::NonIdentFeature);
|
sess.parse_sess.emit_err(session_diagnostics::NonIdentFeature { span: attr.span });
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let level = StabilityLevel::Unstable {
|
let level = StabilityLevel::Unstable {
|
||||||
|
@ -512,7 +471,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
|
||||||
Some((feature, level))
|
Some((feature, level))
|
||||||
}
|
}
|
||||||
(None, _, _) => {
|
(None, _, _) => {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
|
sess.parse_sess.emit_err(session_diagnostics::MissingFeature { span: attr.span });
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -659,11 +618,12 @@ pub fn eval_condition(
|
||||||
ast::MetaItemKind::List(mis) => {
|
ast::MetaItemKind::List(mis) => {
|
||||||
for mi in mis.iter() {
|
for mi in mis.iter() {
|
||||||
if !mi.is_meta_item() {
|
if !mi.is_meta_item() {
|
||||||
handle_errors(
|
sess.emit_err(session_diagnostics::UnsupportedLiteral {
|
||||||
sess,
|
span: mi.span(),
|
||||||
mi.span(),
|
reason: UnsupportedLiteralReason::Generic,
|
||||||
AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
|
is_bytestr: false,
|
||||||
);
|
start_point_span: sess.source_map().start_point(mi.span()),
|
||||||
|
});
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -731,14 +691,12 @@ pub fn eval_condition(
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
|
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
|
||||||
handle_errors(
|
sess.emit_err(session_diagnostics::UnsupportedLiteral {
|
||||||
sess,
|
span: lit.span,
|
||||||
lit.span,
|
reason: UnsupportedLiteralReason::CfgString,
|
||||||
AttrError::UnsupportedLiteral(
|
is_bytestr: lit.kind.is_bytestr(),
|
||||||
UnsupportedLiteralReason::CfgString,
|
start_point_span: sess.source_map().start_point(lit.span),
|
||||||
lit.kind.is_bytestr(),
|
});
|
||||||
),
|
|
||||||
);
|
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
|
ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
|
||||||
|
@ -795,11 +753,10 @@ pub fn find_deprecation(
|
||||||
MetaItemKind::List(list) => {
|
MetaItemKind::List(list) => {
|
||||||
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
|
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
|
||||||
if item.is_some() {
|
if item.is_some() {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::MultipleItem {
|
||||||
&sess.parse_sess,
|
span: meta.span,
|
||||||
meta.span,
|
item: pprust::path_to_string(&meta.path),
|
||||||
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
|
});
|
||||||
);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if let Some(v) = meta.value_str() {
|
if let Some(v) = meta.value_str() {
|
||||||
|
@ -807,14 +764,15 @@ pub fn find_deprecation(
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
if let Some(lit) = meta.name_value_literal() {
|
if let Some(lit) = meta.name_value_literal() {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral {
|
||||||
&sess.parse_sess,
|
span: lit.span,
|
||||||
lit.span,
|
reason: UnsupportedLiteralReason::DeprecatedString,
|
||||||
AttrError::UnsupportedLiteral(
|
is_bytestr: lit.kind.is_bytestr(),
|
||||||
UnsupportedLiteralReason::DeprecatedString,
|
start_point_span: sess
|
||||||
lit.kind.is_bytestr(),
|
.parse_sess
|
||||||
),
|
.source_map()
|
||||||
);
|
.start_point(lit.span),
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
sess.emit_err(session_diagnostics::IncorrectMetaItem {
|
sess.emit_err(session_diagnostics::IncorrectMetaItem {
|
||||||
span: meta.span,
|
span: meta.span,
|
||||||
|
@ -852,30 +810,28 @@ pub fn find_deprecation(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem {
|
||||||
&sess.parse_sess,
|
span: meta.span(),
|
||||||
meta.span(),
|
item: pprust::path_to_string(&mi.path),
|
||||||
AttrError::UnknownMetaItem(
|
expected: if features.deprecated_suggestion {
|
||||||
pprust::path_to_string(&mi.path),
|
&["since", "note", "suggestion"]
|
||||||
if features.deprecated_suggestion {
|
} else {
|
||||||
&["since", "note", "suggestion"]
|
&["since", "note"]
|
||||||
} else {
|
},
|
||||||
&["since", "note"]
|
});
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
continue 'outer;
|
continue 'outer;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
NestedMetaItem::Lit(lit) => {
|
NestedMetaItem::Lit(lit) => {
|
||||||
handle_errors(
|
sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral {
|
||||||
&sess.parse_sess,
|
span: lit.span,
|
||||||
lit.span,
|
reason: UnsupportedLiteralReason::DeprecatedKvPair,
|
||||||
AttrError::UnsupportedLiteral(
|
is_bytestr: false,
|
||||||
UnsupportedLiteralReason::DeprecatedKvPair,
|
start_point_span: sess
|
||||||
false,
|
.parse_sess
|
||||||
),
|
.source_map()
|
||||||
);
|
.start_point(lit.span),
|
||||||
|
});
|
||||||
continue 'outer;
|
continue 'outer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -885,7 +841,7 @@ pub fn find_deprecation(
|
||||||
|
|
||||||
if is_rustc {
|
if is_rustc {
|
||||||
if since.is_none() {
|
if since.is_none() {
|
||||||
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
|
sess.parse_sess.emit_err(session_diagnostics::MissingSince { span: attr.span });
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue