Skip to content

Commit f5429a0

Browse files
committed
Eliminate rustc_attr::builtin::handle_errors
1 parent 3c92dcc commit f5429a0

File tree

1 file changed

+84
-128
lines changed

1 file changed

+84
-128
lines changed

compiler/rustc_attr/src/builtin.rs

+84-128
Original file line numberDiff line numberDiff line change
@@ -31,54 +31,13 @@ pub fn is_builtin_attr(attr: &Attribute) -> bool {
3131
attr.is_doc_comment() || attr.ident().is_some_and(|ident| is_builtin_attr_name(ident.name))
3232
}
3333

34-
enum AttrError {
35-
MultipleItem(String),
36-
UnknownMetaItem(String, &'static [&'static str]),
37-
MissingSince,
38-
NonIdentFeature,
39-
MissingFeature,
40-
MultipleStabilityLevels,
41-
UnsupportedLiteral(UnsupportedLiteralReason, /* is_bytestr */ bool),
42-
}
43-
4434
pub(crate) enum UnsupportedLiteralReason {
4535
Generic,
4636
CfgString,
4737
DeprecatedString,
4838
DeprecatedKvPair,
4939
}
5040

51-
fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
52-
match error {
53-
AttrError::MultipleItem(item) => {
54-
sess.emit_err(session_diagnostics::MultipleItem { span, item });
55-
}
56-
AttrError::UnknownMetaItem(item, expected) => {
57-
sess.emit_err(session_diagnostics::UnknownMetaItem { span, item, expected });
58-
}
59-
AttrError::MissingSince => {
60-
sess.emit_err(session_diagnostics::MissingSince { span });
61-
}
62-
AttrError::NonIdentFeature => {
63-
sess.emit_err(session_diagnostics::NonIdentFeature { span });
64-
}
65-
AttrError::MissingFeature => {
66-
sess.emit_err(session_diagnostics::MissingFeature { span });
67-
}
68-
AttrError::MultipleStabilityLevels => {
69-
sess.emit_err(session_diagnostics::MultipleStabilityLevels { span });
70-
}
71-
AttrError::UnsupportedLiteral(reason, is_bytestr) => {
72-
sess.emit_err(session_diagnostics::UnsupportedLiteral {
73-
span,
74-
reason,
75-
is_bytestr,
76-
start_point_span: sess.source_map().start_point(span),
77-
});
78-
}
79-
}
80-
}
81-
8241
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
8342
pub enum InlineAttr {
8443
None,
@@ -241,7 +200,8 @@ pub fn find_stability(
241200
sym::rustc_allowed_through_unstable_modules => allowed_through_unstable_modules = true,
242201
sym::unstable => {
243202
if stab.is_some() {
244-
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
203+
sess.parse_sess
204+
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
245205
break;
246206
}
247207

@@ -251,7 +211,8 @@ pub fn find_stability(
251211
}
252212
sym::stable => {
253213
if stab.is_some() {
254-
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
214+
sess.parse_sess
215+
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
255216
break;
256217
}
257218
if let Some((feature, level)) = parse_stability(sess, attr) {
@@ -295,7 +256,8 @@ pub fn find_const_stability(
295256
sym::rustc_promotable => promotable = true,
296257
sym::rustc_const_unstable => {
297258
if const_stab.is_some() {
298-
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
259+
sess.parse_sess
260+
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
299261
break;
300262
}
301263

@@ -306,7 +268,8 @@ pub fn find_const_stability(
306268
}
307269
sym::rustc_const_stable => {
308270
if const_stab.is_some() {
309-
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
271+
sess.parse_sess
272+
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
310273
break;
311274
}
312275
if let Some((feature, level)) = parse_stability(sess, attr) {
@@ -340,7 +303,8 @@ pub fn find_body_stability(
340303
for attr in attrs {
341304
if attr.has_name(sym::rustc_default_body_unstable) {
342305
if body_stab.is_some() {
343-
handle_errors(&sess.parse_sess, attr.span, AttrError::MultipleStabilityLevels);
306+
sess.parse_sess
307+
.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span });
344308
break;
345309
}
346310

@@ -355,11 +319,10 @@ pub fn find_body_stability(
355319

356320
fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option<Symbol>) -> Option<()> {
357321
if item.is_some() {
358-
handle_errors(
359-
&sess.parse_sess,
360-
meta.span,
361-
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
362-
);
322+
sess.parse_sess.emit_err(session_diagnostics::MultipleItem {
323+
span: meta.span,
324+
item: pprust::path_to_string(&meta.path),
325+
});
363326
None
364327
} else if let Some(v) = meta.value_str() {
365328
*item = Some(v);
@@ -380,26 +343,24 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
380343
let mut since = None;
381344
for meta in metas {
382345
let Some(mi) = meta.meta_item() else {
383-
handle_errors(
384-
&sess.parse_sess,
385-
meta.span(),
386-
AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
387-
);
346+
sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral {
347+
span: meta.span(),
348+
reason: UnsupportedLiteralReason::Generic,
349+
is_bytestr: false,
350+
start_point_span: sess.parse_sess.source_map().start_point(meta.span()),
351+
});
388352
return None;
389353
};
390354

391355
match mi.name_or_empty() {
392356
sym::feature => insert_or_error(sess, mi, &mut feature)?,
393357
sym::since => insert_or_error(sess, mi, &mut since)?,
394358
_ => {
395-
handle_errors(
396-
&sess.parse_sess,
397-
meta.span(),
398-
AttrError::UnknownMetaItem(
399-
pprust::path_to_string(&mi.path),
400-
&["feature", "since"],
401-
),
402-
);
359+
sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem {
360+
span: meta.span(),
361+
item: pprust::path_to_string(&mi.path),
362+
expected: &["feature", "since"],
363+
});
403364
return None;
404365
}
405366
}
@@ -417,11 +378,11 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
417378
Some((feature, level))
418379
}
419380
(None, _) => {
420-
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
381+
sess.parse_sess.emit_err(session_diagnostics::MissingFeature { span: attr.span });
421382
None
422383
}
423384
_ => {
424-
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
385+
sess.parse_sess.emit_err(session_diagnostics::MissingSince { span: attr.span });
425386
None
426387
}
427388
}
@@ -441,11 +402,12 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
441402
let mut implied_by = None;
442403
for meta in metas {
443404
let Some(mi) = meta.meta_item() else {
444-
handle_errors(
445-
&sess.parse_sess,
446-
meta.span(),
447-
AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
448-
);
405+
sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral {
406+
span: meta.span(),
407+
reason: UnsupportedLiteralReason::Generic,
408+
is_bytestr: false,
409+
start_point_span: sess.parse_sess.source_map().start_point(meta.span()),
410+
});
449411
return None;
450412
};
451413

@@ -484,14 +446,11 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
484446
}
485447
sym::implied_by => insert_or_error(sess, mi, &mut implied_by)?,
486448
_ => {
487-
handle_errors(
488-
&sess.parse_sess,
489-
meta.span(),
490-
AttrError::UnknownMetaItem(
491-
pprust::path_to_string(&mi.path),
492-
&["feature", "reason", "issue", "soft", "implied_by"],
493-
),
494-
);
449+
sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem {
450+
span: meta.span(),
451+
item: pprust::path_to_string(&mi.path),
452+
expected: &["feature", "reason", "issue", "soft", "implied_by"],
453+
});
495454
return None;
496455
}
497456
}
@@ -500,7 +459,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
500459
match (feature, reason, issue) {
501460
(Some(feature), reason, Some(_)) => {
502461
if !rustc_lexer::is_ident(feature.as_str()) {
503-
handle_errors(&sess.parse_sess, attr.span, AttrError::NonIdentFeature);
462+
sess.parse_sess.emit_err(session_diagnostics::NonIdentFeature { span: attr.span });
504463
return None;
505464
}
506465
let level = StabilityLevel::Unstable {
@@ -512,7 +471,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
512471
Some((feature, level))
513472
}
514473
(None, _, _) => {
515-
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature);
474+
sess.parse_sess.emit_err(session_diagnostics::MissingFeature { span: attr.span });
516475
return None;
517476
}
518477
_ => {
@@ -659,11 +618,12 @@ pub fn eval_condition(
659618
ast::MetaItemKind::List(mis) => {
660619
for mi in mis.iter() {
661620
if !mi.is_meta_item() {
662-
handle_errors(
663-
sess,
664-
mi.span(),
665-
AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false),
666-
);
621+
sess.emit_err(session_diagnostics::UnsupportedLiteral {
622+
span: mi.span(),
623+
reason: UnsupportedLiteralReason::Generic,
624+
is_bytestr: false,
625+
start_point_span: sess.source_map().start_point(mi.span()),
626+
});
667627
return false;
668628
}
669629
}
@@ -731,14 +691,12 @@ pub fn eval_condition(
731691
true
732692
}
733693
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
734-
handle_errors(
735-
sess,
736-
lit.span,
737-
AttrError::UnsupportedLiteral(
738-
UnsupportedLiteralReason::CfgString,
739-
lit.kind.is_bytestr(),
740-
),
741-
);
694+
sess.emit_err(session_diagnostics::UnsupportedLiteral {
695+
span: lit.span,
696+
reason: UnsupportedLiteralReason::CfgString,
697+
is_bytestr: lit.kind.is_bytestr(),
698+
start_point_span: sess.source_map().start_point(lit.span),
699+
});
742700
true
743701
}
744702
ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => {
@@ -795,26 +753,26 @@ pub fn find_deprecation(
795753
MetaItemKind::List(list) => {
796754
let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
797755
if item.is_some() {
798-
handle_errors(
799-
&sess.parse_sess,
800-
meta.span,
801-
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
802-
);
756+
sess.parse_sess.emit_err(session_diagnostics::MultipleItem {
757+
span: meta.span,
758+
item: pprust::path_to_string(&meta.path),
759+
});
803760
return false;
804761
}
805762
if let Some(v) = meta.value_str() {
806763
*item = Some(v);
807764
true
808765
} else {
809766
if let Some(lit) = meta.name_value_literal() {
810-
handle_errors(
811-
&sess.parse_sess,
812-
lit.span,
813-
AttrError::UnsupportedLiteral(
814-
UnsupportedLiteralReason::DeprecatedString,
815-
lit.kind.is_bytestr(),
816-
),
817-
);
767+
sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral {
768+
span: lit.span,
769+
reason: UnsupportedLiteralReason::DeprecatedString,
770+
is_bytestr: lit.kind.is_bytestr(),
771+
start_point_span: sess
772+
.parse_sess
773+
.source_map()
774+
.start_point(lit.span),
775+
});
818776
} else {
819777
sess.emit_err(session_diagnostics::IncorrectMetaItem {
820778
span: meta.span,
@@ -852,30 +810,28 @@ pub fn find_deprecation(
852810
}
853811
}
854812
_ => {
855-
handle_errors(
856-
&sess.parse_sess,
857-
meta.span(),
858-
AttrError::UnknownMetaItem(
859-
pprust::path_to_string(&mi.path),
860-
if features.deprecated_suggestion {
861-
&["since", "note", "suggestion"]
862-
} else {
863-
&["since", "note"]
864-
},
865-
),
866-
);
813+
sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem {
814+
span: meta.span(),
815+
item: pprust::path_to_string(&mi.path),
816+
expected: if features.deprecated_suggestion {
817+
&["since", "note", "suggestion"]
818+
} else {
819+
&["since", "note"]
820+
},
821+
});
867822
continue 'outer;
868823
}
869824
},
870825
NestedMetaItem::Lit(lit) => {
871-
handle_errors(
872-
&sess.parse_sess,
873-
lit.span,
874-
AttrError::UnsupportedLiteral(
875-
UnsupportedLiteralReason::DeprecatedKvPair,
876-
false,
877-
),
878-
);
826+
sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral {
827+
span: lit.span,
828+
reason: UnsupportedLiteralReason::DeprecatedKvPair,
829+
is_bytestr: false,
830+
start_point_span: sess
831+
.parse_sess
832+
.source_map()
833+
.start_point(lit.span),
834+
});
879835
continue 'outer;
880836
}
881837
}
@@ -885,7 +841,7 @@ pub fn find_deprecation(
885841

886842
if is_rustc {
887843
if since.is_none() {
888-
handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince);
844+
sess.parse_sess.emit_err(session_diagnostics::MissingSince { span: attr.span });
889845
continue;
890846
}
891847

0 commit comments

Comments
 (0)