Skip to content

Commit bf3b3ba

Browse files
committed
Auto merge of #164898 - JonathanBrouwer:silence-early-lint, r=<try>
Don't emit diagnostics during early lint attribute parsing
2 parents 019b59d + 76f6ef4 commit bf3b3ba

38 files changed

Lines changed: 189 additions & 515 deletions

‎compiler/rustc_attr_ir/src/diagnostic.rs‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
use std::fmt;
33
use std::fmt::Debug;
44

5-
use rustc_macros::{Decodable, Encodable, PrintAttribute, StableHash};
6-
use rustc_span::{DesugaringKind, Span, Symbol, kw};
5+
use rustc_macros::{
6+
Decodable, Decodable_NoContext, Encodable, Encodable_NoContext, PrintAttribute, StableHash,
7+
};
8+
use rustc_span::{DesugaringKind, ErrorGuaranteed, Span, Symbol, kw};
79
use thin_vec::ThinVec;
810
use tracing::debug;
911

@@ -481,3 +483,28 @@ impl FilterOptions {
481483
}
482484
}
483485
}
486+
487+
/// An `AttrErrorGuaranteed` is the same thing as a normal `ErrorGuaranteed`, except attribute parsers sometimes don't emit diagnostics.
488+
/// This will contain a `None` if and only if `ShouldEmit::NothingNoBug` is used.
489+
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
490+
#[derive(StableHash, Encodable_NoContext, Decodable_NoContext, PrintAttribute)]
491+
pub struct AttrErrorGuaranteed(Option<ErrorGuaranteed>);
492+
493+
impl AttrErrorGuaranteed {
494+
/// Constructs a `AttrErrorGuaranteed` with a `None` inside, this should only be called in `ShouldEmit::emit_err`.
495+
pub fn unchecked_attr_error_guaranteed() -> Self {
496+
Self(None)
497+
}
498+
499+
/// Retrieve the inner `ErrorGuaranteed`
500+
/// This will panic if and only if `ShouldEmit::NothingNoBug` is used.
501+
pub fn unwrap(self) -> ErrorGuaranteed {
502+
self.0.unwrap()
503+
}
504+
}
505+
506+
impl From<ErrorGuaranteed> for AttrErrorGuaranteed {
507+
fn from(value: ErrorGuaranteed) -> Self {
508+
Self(Some(value))
509+
}
510+
}

‎compiler/rustc_attr_ir/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
pub use attr::*;
1919
pub use data_structures::*;
20+
pub use diagnostic::AttrErrorGuaranteed;
2021
pub use encode_cross_crate::EncodeCrossCrate;
2122
pub use lang_items::*;
2223
pub use pretty_printing::PrintAttribute;

‎compiler/rustc_attr_ir/src/stability.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::num::NonZero;
22

33
use rustc_ast::attr::version::RustcVersion;
44
use rustc_macros::{BlobDecodable, Decodable, Encodable, PrintAttribute, StableHash};
5-
use rustc_span::{ErrorGuaranteed, Symbol, sym};
5+
use rustc_span::{Symbol, sym};
66

7-
use crate::PrintAttribute;
7+
use crate::{AttrErrorGuaranteed, PrintAttribute};
88

99
/// The version placeholder that recently stabilized features contain inside the
1010
/// `since` field of the `#[stable]` attribute.
@@ -154,7 +154,7 @@ pub enum StableSince {
154154
/// Stabilized in the upcoming version, whatever number that is.
155155
Current,
156156
/// Failed to parse a stabilization version.
157-
Err(ErrorGuaranteed),
157+
Err(AttrErrorGuaranteed),
158158
}
159159

160160
impl StabilityLevel {

‎compiler/rustc_attr_parsing/src/attributes/cfg.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::token::Delimiter;
44
use rustc_ast::tokenstream::{DelimSpan, WithTokens};
55
use rustc_ast::{AttrItem, Attribute, LitKind, ast, token};
66
use rustc_attr_ir::target::Target;
7-
use rustc_attr_ir::{AttrPath, CfgEntry, RustcVersion};
7+
use rustc_attr_ir::{AttrErrorGuaranteed, AttrPath, CfgEntry, RustcVersion};
88
use rustc_errors::{Applicability, Diagnostic, PResult, msg};
99
use rustc_feature::{Features, GatedCfg, find_gated_cfg};
1010
use rustc_lint_defs::builtin::UNEXPECTED_CFGS;
@@ -14,7 +14,7 @@ use rustc_session::Session;
1414
use rustc_session::config::ExpectedValues;
1515
use rustc_session::diagnostics::feature_err;
1616
use rustc_session::parse::ParseSess;
17-
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
17+
use rustc_span::{Span, Symbol, sym};
1818
use thin_vec::ThinVec;
1919

2020
use crate::attributes::AttributeSafety;
@@ -79,7 +79,7 @@ pub fn parse_cfg(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<Cfg
7979
pub fn parse_cfg_entry(
8080
cx: &mut AcceptContext<'_, '_>,
8181
item: &MetaItemOrLitParser,
82-
) -> Result<CfgEntry, ErrorGuaranteed> {
82+
) -> Result<CfgEntry, AttrErrorGuaranteed> {
8383
Ok(match item {
8484
MetaItemOrLitParser::MetaItemParser(meta) => match meta.args() {
8585
ArgParser::List(list) => match meta.path().word_sym() {
@@ -126,7 +126,7 @@ fn parse_cfg_entry_version(
126126
cx: &mut AcceptContext<'_, '_>,
127127
list: &MetaItemListParser,
128128
meta_span: Span,
129-
) -> Result<CfgEntry, ErrorGuaranteed> {
129+
) -> Result<CfgEntry, AttrErrorGuaranteed> {
130130
try_gate_cfg(sym::version, meta_span, cx.sess(), cx.features_option());
131131
let Some(version) = list.as_single() else {
132132
return Err(cx.emit_err(diagnostics::ExpectedSingleVersionLiteral { span: list.span }));
@@ -150,7 +150,7 @@ fn parse_cfg_entry_target(
150150
cx: &mut AcceptContext<'_, '_>,
151151
list: &MetaItemListParser,
152152
meta_span: Span,
153-
) -> Result<CfgEntry, ErrorGuaranteed> {
153+
) -> Result<CfgEntry, AttrErrorGuaranteed> {
154154
if let Some(features) = cx.features_option()
155155
&& !features.cfg_target_compact()
156156
{
@@ -188,7 +188,7 @@ pub(crate) fn parse_name_value(
188188
value: Option<&NameValueParser>,
189189
span: Span,
190190
cx: &mut AcceptContext<'_, '_>,
191-
) -> Result<CfgEntry, ErrorGuaranteed> {
191+
) -> Result<CfgEntry, AttrErrorGuaranteed> {
192192
try_gate_cfg(name, span, cx.sess(), cx.features_option());
193193

194194
let value = match value {
@@ -408,8 +408,8 @@ fn parse_cfg_attr_internal<'a>(
408408
parse_cfg_entry,
409409
&CFG_ATTR_TEMPLATE,
410410
)
411-
.map_err(|_err: ErrorGuaranteed| {
412-
// We have an `ErrorGuaranteed` so this delayed bug cannot fail, but we need a `Diag` for the `PResult` so we make one anyways
411+
.map_err(|_err: AttrErrorGuaranteed| {
412+
// We have an `AttrErrorGuaranteed` so this delayed bug cannot fail, but we need a `Diag` for the `PResult` so we make one anyways
413413
let mut diag = sess.dcx().struct_err(
414414
"cfg_entry parsing failing with `ShouldEmit::ErrorsAndLints` should emit a error.",
415415
);

‎compiler/rustc_attr_parsing/src/attributes/cfg_select.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use rustc_ast::token::Token;
22
use rustc_ast::tokenstream::TokenStream;
33
use rustc_ast::{AttrStyle, NodeId, token};
44
use rustc_attr_ir::target::Target;
5-
use rustc_attr_ir::{AttrPath, CfgEntry};
5+
use rustc_attr_ir::{AttrErrorGuaranteed, AttrPath, CfgEntry};
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_errors::Diagnostic;
88
use rustc_feature::Features;
99
use rustc_lint_defs::builtin::UNREACHABLE_CFG_SELECT_PREDICATES;
1010
use rustc_parse::exp;
1111
use rustc_parse::parser::{Parser, Recovery};
1212
use rustc_session::Session;
13-
use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
13+
use rustc_span::{Span, Symbol, sym};
1414

1515
use crate::attributes::AttributeSafety;
1616
use crate::parser::{AllowExprMetavar, MetaItemOrLitParser};
@@ -85,7 +85,7 @@ pub fn parse_cfg_select(
8585
sess: &Session,
8686
features: Option<&Features>,
8787
lint_node_id: NodeId,
88-
) -> Result<CfgSelectBranches, ErrorGuaranteed> {
88+
) -> Result<CfgSelectBranches, AttrErrorGuaranteed> {
8989
let mut branches = CfgSelectBranches::default();
9090

9191
while p.token != token::Eof {

‎compiler/rustc_attr_parsing/src/attributes/stability.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::num::NonZero;
22

33
use rustc_attr_ir::target::{AssocCtxt, MethodKind, Target};
44
use rustc_attr_ir::{
5-
DefaultBodyStability, PartialConstStability, Stability, StabilityLevel, StableSince,
6-
UnstableReason, UnstableRemovedFeature, VERSION_PLACEHOLDER,
5+
AttrErrorGuaranteed, DefaultBodyStability, PartialConstStability, Stability, StabilityLevel,
6+
StableSince, UnstableReason, UnstableRemovedFeature, VERSION_PLACEHOLDER,
77
};
8-
use rustc_errors::ErrorGuaranteed;
98
use rustc_feature::{ACCEPTED_LANG_FEATURES, AttributeStability};
109

1110
use super::prelude::*;
@@ -384,7 +383,7 @@ pub(crate) fn parse_stability(
384383
let level = StabilityLevel::Stable { since, allowed_through_unstable_modules: None };
385384
Some((feature, level))
386385
}
387-
Err(ErrorGuaranteed { .. }) => None,
386+
Err(AttrErrorGuaranteed { .. }) => None,
388387
}
389388
}
390389

@@ -483,7 +482,7 @@ pub(crate) fn parse_unstability(
483482
};
484483
Some((feature, level))
485484
}
486-
(Err(ErrorGuaranteed { .. }), _) | (_, Err(ErrorGuaranteed { .. })) => None,
485+
(Err(AttrErrorGuaranteed { .. }), _) | (_, Err(AttrErrorGuaranteed { .. })) => None,
487486
}
488487
}
489488

0 commit comments

Comments
 (0)