Skip to content

Commit b13efa6

Browse files
Improve should_emit & add parse_limited_all
1 parent 17c5895 commit b13efa6

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ mod private {
213213
#[allow(private_interfaces)]
214214
pub trait Stage: Sized + 'static + Sealed {
215215
type Id: Copy;
216-
const SHOULD_EMIT_LINTS: bool;
217216

218217
fn parsers() -> &'static group_type!(Self);
219218

@@ -222,13 +221,14 @@ pub trait Stage: Sized + 'static + Sealed {
222221
sess: &'sess Session,
223222
diag: impl for<'x> Diagnostic<'x>,
224223
) -> ErrorGuaranteed;
224+
225+
fn should_emit(&self) -> ShouldEmit;
225226
}
226227

227228
// allow because it's a sealed trait
228229
#[allow(private_interfaces)]
229230
impl Stage for Early {
230231
type Id = NodeId;
231-
const SHOULD_EMIT_LINTS: bool = false;
232232

233233
fn parsers() -> &'static group_type!(Self) {
234234
&early::ATTRIBUTE_PARSERS
@@ -244,13 +244,16 @@ impl Stage for Early {
244244
sess.dcx().create_err(diag).delay_as_bug()
245245
}
246246
}
247+
248+
fn should_emit(&self) -> ShouldEmit {
249+
self.emit_errors
250+
}
247251
}
248252

249253
// allow because it's a sealed trait
250254
#[allow(private_interfaces)]
251255
impl Stage for Late {
252256
type Id = HirId;
253-
const SHOULD_EMIT_LINTS: bool = true;
254257

255258
fn parsers() -> &'static group_type!(Self) {
256259
&late::ATTRIBUTE_PARSERS
@@ -262,6 +265,10 @@ impl Stage for Late {
262265
) -> ErrorGuaranteed {
263266
tcx.dcx().emit_err(diag)
264267
}
268+
269+
fn should_emit(&self) -> ShouldEmit {
270+
ShouldEmit::ErrorsAndLints
271+
}
265272
}
266273

267274
/// used when parsing attributes for miscellaneous things *before* ast lowering
@@ -300,7 +307,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
300307
/// must be delayed until after HIR is built. This method will take care of the details of
301308
/// that.
302309
pub(crate) fn emit_lint(&mut self, lint: AttributeLintKind, span: Span) {
303-
if !S::SHOULD_EMIT_LINTS {
310+
if !self.stage.should_emit().should_emit() {
304311
return;
305312
}
306313
let id = self.target_id;
@@ -657,14 +664,31 @@ impl<'sess> AttributeParser<'sess, Early> {
657664
target_node_id: NodeId,
658665
features: Option<&'sess Features>,
659666
) -> Option<Attribute> {
660-
let mut p = Self {
661-
features,
662-
tools: Vec::new(),
663-
parse_only: Some(sym),
667+
let mut parsed = Self::parse_limited_all(
664668
sess,
665-
stage: Early { emit_errors: ShouldEmit::Nothing },
666-
};
667-
let mut parsed = p.parse_attribute_list(
669+
attrs,
670+
Some(sym),
671+
target_span,
672+
target_node_id,
673+
features,
674+
ShouldEmit::Nothing,
675+
);
676+
assert!(parsed.len() <= 1);
677+
parsed.pop()
678+
}
679+
680+
pub fn parse_limited_all(
681+
sess: &'sess Session,
682+
attrs: &[ast::Attribute],
683+
parse_only: Option<Symbol>,
684+
target_span: Span,
685+
target_node_id: NodeId,
686+
features: Option<&'sess Features>,
687+
emit_errors: ShouldEmit,
688+
) -> Vec<Attribute> {
689+
let mut p =
690+
Self { features, tools: Vec::new(), parse_only, sess, stage: Early { emit_errors } };
691+
p.parse_attribute_list(
668692
attrs,
669693
target_span,
670694
target_node_id,
@@ -673,10 +697,7 @@ impl<'sess> AttributeParser<'sess, Early> {
673697
|_lint| {
674698
panic!("can't emit lints here for now (nothing uses this atm)");
675699
},
676-
);
677-
assert!(parsed.len() <= 1);
678-
679-
parsed.pop()
700+
)
680701
}
681702

682703
pub fn parse_single<T>(

0 commit comments

Comments
 (0)