From f5429a018665d59f16bfbfc8b64282b3f8c18207 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 22 Oct 2023 12:21:51 -0700 Subject: [PATCH 1/2] Eliminate rustc_attr::builtin::handle_errors --- compiler/rustc_attr/src/builtin.rs | 212 ++++++++++++----------------- 1 file changed, 84 insertions(+), 128 deletions(-) diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index 46683d2d25867..e7317fe1c2ed2 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -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)) } -enum AttrError { - MultipleItem(String), - UnknownMetaItem(String, &'static [&'static str]), - MissingSince, - NonIdentFeature, - MissingFeature, - MultipleStabilityLevels, - UnsupportedLiteral(UnsupportedLiteralReason, /* is_bytestr */ bool), -} - pub(crate) enum UnsupportedLiteralReason { Generic, CfgString, @@ -48,37 +38,6 @@ pub(crate) enum UnsupportedLiteralReason { 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)] pub enum InlineAttr { None, @@ -241,7 +200,8 @@ pub fn find_stability( sym::rustc_allowed_through_unstable_modules => allowed_through_unstable_modules = true, sym::unstable => { 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; } @@ -251,7 +211,8 @@ pub fn find_stability( } sym::stable => { 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; } 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_const_unstable => { 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; } @@ -306,7 +268,8 @@ pub fn find_const_stability( } sym::rustc_const_stable => { 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; } if let Some((feature, level)) = parse_stability(sess, attr) { @@ -340,7 +303,8 @@ pub fn find_body_stability( for attr in attrs { if attr.has_name(sym::rustc_default_body_unstable) { 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; } @@ -355,11 +319,10 @@ pub fn find_body_stability( fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option) -> Option<()> { if item.is_some() { - handle_errors( - &sess.parse_sess, - meta.span, - AttrError::MultipleItem(pprust::path_to_string(&meta.path)), - ); + sess.parse_sess.emit_err(session_diagnostics::MultipleItem { + span: meta.span, + item: pprust::path_to_string(&meta.path), + }); None } else if let Some(v) = meta.value_str() { *item = Some(v); @@ -380,11 +343,12 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit let mut since = None; for meta in metas { let Some(mi) = meta.meta_item() else { - handle_errors( - &sess.parse_sess, - meta.span(), - AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false), - ); + sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral { + span: meta.span(), + reason: UnsupportedLiteralReason::Generic, + is_bytestr: false, + start_point_span: sess.parse_sess.source_map().start_point(meta.span()), + }); 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::since => insert_or_error(sess, mi, &mut since)?, _ => { - handle_errors( - &sess.parse_sess, - meta.span(), - AttrError::UnknownMetaItem( - pprust::path_to_string(&mi.path), - &["feature", "since"], - ), - ); + sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem { + span: meta.span(), + item: pprust::path_to_string(&mi.path), + expected: &["feature", "since"], + }); return None; } } @@ -417,11 +378,11 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit Some((feature, level)) } (None, _) => { - handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature); + sess.parse_sess.emit_err(session_diagnostics::MissingFeature { span: attr.span }); None } _ => { - handle_errors(&sess.parse_sess, attr.span, AttrError::MissingSince); + sess.parse_sess.emit_err(session_diagnostics::MissingSince { span: attr.span }); None } } @@ -441,11 +402,12 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil let mut implied_by = None; for meta in metas { let Some(mi) = meta.meta_item() else { - handle_errors( - &sess.parse_sess, - meta.span(), - AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false), - ); + sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral { + span: meta.span(), + reason: UnsupportedLiteralReason::Generic, + is_bytestr: false, + start_point_span: sess.parse_sess.source_map().start_point(meta.span()), + }); 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)?, _ => { - handle_errors( - &sess.parse_sess, - meta.span(), - AttrError::UnknownMetaItem( - pprust::path_to_string(&mi.path), - &["feature", "reason", "issue", "soft", "implied_by"], - ), - ); + sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem { + span: meta.span(), + item: pprust::path_to_string(&mi.path), + expected: &["feature", "reason", "issue", "soft", "implied_by"], + }); return None; } } @@ -500,7 +459,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil match (feature, reason, issue) { (Some(feature), reason, Some(_)) => { 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; } let level = StabilityLevel::Unstable { @@ -512,7 +471,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil Some((feature, level)) } (None, _, _) => { - handle_errors(&sess.parse_sess, attr.span, AttrError::MissingFeature); + sess.parse_sess.emit_err(session_diagnostics::MissingFeature { span: attr.span }); return None; } _ => { @@ -659,11 +618,12 @@ pub fn eval_condition( ast::MetaItemKind::List(mis) => { for mi in mis.iter() { if !mi.is_meta_item() { - handle_errors( - sess, - mi.span(), - AttrError::UnsupportedLiteral(UnsupportedLiteralReason::Generic, false), - ); + sess.emit_err(session_diagnostics::UnsupportedLiteral { + span: mi.span(), + reason: UnsupportedLiteralReason::Generic, + is_bytestr: false, + start_point_span: sess.source_map().start_point(mi.span()), + }); return false; } } @@ -731,14 +691,12 @@ pub fn eval_condition( true } MetaItemKind::NameValue(lit) if !lit.kind.is_str() => { - handle_errors( - sess, - lit.span, - AttrError::UnsupportedLiteral( - UnsupportedLiteralReason::CfgString, - lit.kind.is_bytestr(), - ), - ); + sess.emit_err(session_diagnostics::UnsupportedLiteral { + span: lit.span, + reason: UnsupportedLiteralReason::CfgString, + is_bytestr: lit.kind.is_bytestr(), + start_point_span: sess.source_map().start_point(lit.span), + }); true } ast::MetaItemKind::Word | ast::MetaItemKind::NameValue(..) => { @@ -795,11 +753,10 @@ pub fn find_deprecation( MetaItemKind::List(list) => { let get = |meta: &MetaItem, item: &mut Option| { if item.is_some() { - handle_errors( - &sess.parse_sess, - meta.span, - AttrError::MultipleItem(pprust::path_to_string(&meta.path)), - ); + sess.parse_sess.emit_err(session_diagnostics::MultipleItem { + span: meta.span, + item: pprust::path_to_string(&meta.path), + }); return false; } if let Some(v) = meta.value_str() { @@ -807,14 +764,15 @@ pub fn find_deprecation( true } else { if let Some(lit) = meta.name_value_literal() { - handle_errors( - &sess.parse_sess, - lit.span, - AttrError::UnsupportedLiteral( - UnsupportedLiteralReason::DeprecatedString, - lit.kind.is_bytestr(), - ), - ); + sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral { + span: lit.span, + reason: UnsupportedLiteralReason::DeprecatedString, + is_bytestr: lit.kind.is_bytestr(), + start_point_span: sess + .parse_sess + .source_map() + .start_point(lit.span), + }); } else { sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span, @@ -852,30 +810,28 @@ pub fn find_deprecation( } } _ => { - handle_errors( - &sess.parse_sess, - meta.span(), - AttrError::UnknownMetaItem( - pprust::path_to_string(&mi.path), - if features.deprecated_suggestion { - &["since", "note", "suggestion"] - } else { - &["since", "note"] - }, - ), - ); + sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem { + span: meta.span(), + item: pprust::path_to_string(&mi.path), + expected: if features.deprecated_suggestion { + &["since", "note", "suggestion"] + } else { + &["since", "note"] + }, + }); continue 'outer; } }, NestedMetaItem::Lit(lit) => { - handle_errors( - &sess.parse_sess, - lit.span, - AttrError::UnsupportedLiteral( - UnsupportedLiteralReason::DeprecatedKvPair, - false, - ), - ); + sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral { + span: lit.span, + reason: UnsupportedLiteralReason::DeprecatedKvPair, + is_bytestr: false, + start_point_span: sess + .parse_sess + .source_map() + .start_point(lit.span), + }); continue 'outer; } } @@ -885,7 +841,7 @@ pub fn find_deprecation( if is_rustc { 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; } From 28785291f95e933829c4e86bcf7d6547f5ff3aa8 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 22 Oct 2023 12:39:08 -0700 Subject: [PATCH 2/2] Eliminate redundant .parse_sess in rustc_attr::builtin errors --- compiler/rustc_attr/src/builtin.rs | 57 ++++++++++++------------------ 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/compiler/rustc_attr/src/builtin.rs b/compiler/rustc_attr/src/builtin.rs index e7317fe1c2ed2..60eacde1c723d 100644 --- a/compiler/rustc_attr/src/builtin.rs +++ b/compiler/rustc_attr/src/builtin.rs @@ -200,8 +200,7 @@ pub fn find_stability( sym::rustc_allowed_through_unstable_modules => allowed_through_unstable_modules = true, sym::unstable => { if stab.is_some() { - sess.parse_sess - .emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); + sess.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); break; } @@ -211,8 +210,7 @@ pub fn find_stability( } sym::stable => { if stab.is_some() { - sess.parse_sess - .emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); + sess.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); break; } if let Some((feature, level)) = parse_stability(sess, attr) { @@ -256,8 +254,7 @@ pub fn find_const_stability( sym::rustc_promotable => promotable = true, sym::rustc_const_unstable => { if const_stab.is_some() { - sess.parse_sess - .emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); + sess.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); break; } @@ -268,8 +265,7 @@ pub fn find_const_stability( } sym::rustc_const_stable => { if const_stab.is_some() { - sess.parse_sess - .emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); + sess.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); break; } if let Some((feature, level)) = parse_stability(sess, attr) { @@ -303,8 +299,7 @@ pub fn find_body_stability( for attr in attrs { if attr.has_name(sym::rustc_default_body_unstable) { if body_stab.is_some() { - sess.parse_sess - .emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); + sess.emit_err(session_diagnostics::MultipleStabilityLevels { span: attr.span }); break; } @@ -319,7 +314,7 @@ pub fn find_body_stability( fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option) -> Option<()> { if item.is_some() { - sess.parse_sess.emit_err(session_diagnostics::MultipleItem { + sess.emit_err(session_diagnostics::MultipleItem { span: meta.span, item: pprust::path_to_string(&meta.path), }); @@ -343,11 +338,11 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit let mut since = None; for meta in metas { let Some(mi) = meta.meta_item() else { - sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral { + sess.emit_err(session_diagnostics::UnsupportedLiteral { span: meta.span(), reason: UnsupportedLiteralReason::Generic, is_bytestr: false, - start_point_span: sess.parse_sess.source_map().start_point(meta.span()), + start_point_span: sess.source_map().start_point(meta.span()), }); return None; }; @@ -356,7 +351,7 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit sym::feature => insert_or_error(sess, mi, &mut feature)?, sym::since => insert_or_error(sess, mi, &mut since)?, _ => { - sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem { + sess.emit_err(session_diagnostics::UnknownMetaItem { span: meta.span(), item: pprust::path_to_string(&mi.path), expected: &["feature", "since"], @@ -378,11 +373,11 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit Some((feature, level)) } (None, _) => { - sess.parse_sess.emit_err(session_diagnostics::MissingFeature { span: attr.span }); + sess.emit_err(session_diagnostics::MissingFeature { span: attr.span }); None } _ => { - sess.parse_sess.emit_err(session_diagnostics::MissingSince { span: attr.span }); + sess.emit_err(session_diagnostics::MissingSince { span: attr.span }); None } } @@ -402,11 +397,11 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil let mut implied_by = None; for meta in metas { let Some(mi) = meta.meta_item() else { - sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral { + sess.emit_err(session_diagnostics::UnsupportedLiteral { span: meta.span(), reason: UnsupportedLiteralReason::Generic, is_bytestr: false, - start_point_span: sess.parse_sess.source_map().start_point(meta.span()), + start_point_span: sess.source_map().start_point(meta.span()), }); return None; }; @@ -446,7 +441,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil } sym::implied_by => insert_or_error(sess, mi, &mut implied_by)?, _ => { - sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem { + sess.emit_err(session_diagnostics::UnknownMetaItem { span: meta.span(), item: pprust::path_to_string(&mi.path), expected: &["feature", "reason", "issue", "soft", "implied_by"], @@ -459,7 +454,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil match (feature, reason, issue) { (Some(feature), reason, Some(_)) => { if !rustc_lexer::is_ident(feature.as_str()) { - sess.parse_sess.emit_err(session_diagnostics::NonIdentFeature { span: attr.span }); + sess.emit_err(session_diagnostics::NonIdentFeature { span: attr.span }); return None; } let level = StabilityLevel::Unstable { @@ -471,7 +466,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil Some((feature, level)) } (None, _, _) => { - sess.parse_sess.emit_err(session_diagnostics::MissingFeature { span: attr.span }); + sess.emit_err(session_diagnostics::MissingFeature { span: attr.span }); return None; } _ => { @@ -753,7 +748,7 @@ pub fn find_deprecation( MetaItemKind::List(list) => { let get = |meta: &MetaItem, item: &mut Option| { if item.is_some() { - sess.parse_sess.emit_err(session_diagnostics::MultipleItem { + sess.emit_err(session_diagnostics::MultipleItem { span: meta.span, item: pprust::path_to_string(&meta.path), }); @@ -764,14 +759,11 @@ pub fn find_deprecation( true } else { if let Some(lit) = meta.name_value_literal() { - sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral { + sess.emit_err(session_diagnostics::UnsupportedLiteral { span: lit.span, reason: UnsupportedLiteralReason::DeprecatedString, is_bytestr: lit.kind.is_bytestr(), - start_point_span: sess - .parse_sess - .source_map() - .start_point(lit.span), + start_point_span: sess.source_map().start_point(lit.span), }); } else { sess.emit_err(session_diagnostics::IncorrectMetaItem { @@ -810,7 +802,7 @@ pub fn find_deprecation( } } _ => { - sess.parse_sess.emit_err(session_diagnostics::UnknownMetaItem { + sess.emit_err(session_diagnostics::UnknownMetaItem { span: meta.span(), item: pprust::path_to_string(&mi.path), expected: if features.deprecated_suggestion { @@ -823,14 +815,11 @@ pub fn find_deprecation( } }, NestedMetaItem::Lit(lit) => { - sess.parse_sess.emit_err(session_diagnostics::UnsupportedLiteral { + sess.emit_err(session_diagnostics::UnsupportedLiteral { span: lit.span, reason: UnsupportedLiteralReason::DeprecatedKvPair, is_bytestr: false, - start_point_span: sess - .parse_sess - .source_map() - .start_point(lit.span), + start_point_span: sess.source_map().start_point(lit.span), }); continue 'outer; } @@ -841,7 +830,7 @@ pub fn find_deprecation( if is_rustc { if since.is_none() { - sess.parse_sess.emit_err(session_diagnostics::MissingSince { span: attr.span }); + sess.emit_err(session_diagnostics::MissingSince { span: attr.span }); continue; }