Skip to content

Commit 003667b

Browse files
Remove should_emit from stage
1 parent 7ed0247 commit 003667b

7 files changed

Lines changed: 32 additions & 37 deletions

File tree

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::sync::Arc;
4141
use rustc_ast::node_id::NodeMap;
4242
use rustc_ast::visit::Visitor;
4343
use rustc_ast::{self as ast, *};
44-
use rustc_attr_parsing::{AttributeParser, EmitAttribute, Late, OmitDoc};
44+
use rustc_attr_parsing::{AttributeParser, EmitAttribute, OmitDoc, Recovery, ShouldEmit};
4545
use rustc_data_structures::fingerprint::Fingerprint;
4646
use rustc_data_structures::fx::FxIndexSet;
4747
use rustc_data_structures::sorted_map::SortedMap;
@@ -212,7 +212,7 @@ impl<'a, 'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'a, 'hir, R> {
212212
tcx.sess,
213213
tcx.features(),
214214
registered_tools,
215-
Late,
215+
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
216216
),
217217
delayed_lints: Vec::new(),
218218
}

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_unknown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl OnUnknownParser {
3131

3232
// At early parsing we get passed `Target::Crate` regardless of the item we're on.
3333
// Only do target checking if we're late.
34-
let early = matches!(cx.stage.should_emit(), ShouldEmit::Nothing);
34+
let early = matches!(cx.should_emit, ShouldEmit::Nothing);
3535

3636
if !early && !matches!(cx.target, Target::Use) {
3737
let target_span = cx.target_span;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,6 @@ pub trait Stage: Sized + 'static + Sealed {
355355
type Id: Copy;
356356

357357
fn parsers() -> &'static GroupType<Self>;
358-
359-
fn should_emit(&self) -> ShouldEmit;
360358
}
361359

362360
// allow because it's a sealed trait
@@ -367,10 +365,6 @@ impl Stage for Early {
367365
fn parsers() -> &'static GroupType<Self> {
368366
&early::ATTRIBUTE_PARSERS
369367
}
370-
371-
fn should_emit(&self) -> ShouldEmit {
372-
self.emit_errors
373-
}
374368
}
375369

376370
// allow because it's a sealed trait
@@ -381,19 +375,11 @@ impl Stage for Late {
381375
fn parsers() -> &'static GroupType<Self> {
382376
&late::ATTRIBUTE_PARSERS
383377
}
384-
385-
fn should_emit(&self) -> ShouldEmit {
386-
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed }
387-
}
388378
}
389379

390380
/// Used when parsing attributes for miscellaneous things *before* ast lowering
391-
pub struct Early {
392-
/// Whether to emit errors or delay them as a bug.
393-
/// For most attributes, the attribute will be parsed again in the `Late` stage and in this case the errors should be delayed.
394-
/// But for some, such as `cfg`, the attribute will be removed before the `Late` stage so errors must be emitted.
395-
pub emit_errors: ShouldEmit,
396-
}
381+
pub struct Early;
382+
397383
/// used when parsing attributes during ast lowering
398384
pub struct Late;
399385

@@ -473,7 +459,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
473459
span: impl Into<MultiSpan>,
474460
) {
475461
if !matches!(
476-
self.stage.should_emit(),
462+
self.should_emit,
477463
ShouldEmit::ErrorsAndLints { .. } | ShouldEmit::EarlyFatal { also_emit_lints: true }
478464
) {
479465
return;

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::convert::identity;
2+
use std::marker::PhantomData;
23

34
use rustc_ast as ast;
45
use rustc_ast::token::DocFragmentKind;
@@ -11,7 +12,7 @@ use rustc_hir::lints::AttributeLintKind;
1112
use rustc_hir::{AttrArgs, AttrItem, AttrPath, Attribute, HashIgnoredAttrId, Target};
1213
use rustc_session::Session;
1314
use rustc_session::lint::LintId;
14-
use rustc_span::{DUMMY_SP, Span, Symbol, sym, ErrorGuaranteed};
15+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1516

1617
use crate::attributes::AttributeSafety;
1718
use crate::context::{AcceptContext, FinalizeContext, FinalizeFn, SharedContext, Stage};
@@ -35,7 +36,8 @@ pub struct AttributeParser<'sess, S: Stage = Late> {
3536
pub(crate) tools: Vec<Symbol>,
3637
pub(crate) features: Option<&'sess Features>,
3738
pub(crate) sess: &'sess Session,
38-
pub(crate) stage: S,
39+
pub(crate) stage: PhantomData<S>,
40+
pub(crate) should_emit: ShouldEmit,
3941

4042
/// *Only* parse attributes with this symbol.
4143
///
@@ -117,10 +119,10 @@ impl<'sess> AttributeParser<'sess, Early> {
117119
target_span: Span,
118120
target_node_id: NodeId,
119121
features: Option<&'sess Features>,
120-
emit_errors: ShouldEmit,
122+
should_emit: ShouldEmit,
121123
) -> Vec<Attribute> {
122124
let mut p =
123-
Self { features, tools: Vec::new(), parse_only, sess, stage: Early { emit_errors } };
125+
Self { features, tools: Vec::new(), parse_only, sess, stage: PhantomData, should_emit };
124126
p.parse_attribute_list(
125127
attrs,
126128
target_span,
@@ -202,7 +204,7 @@ impl<'sess> AttributeParser<'sess, Early> {
202204
target_node_id: NodeId,
203205
target: Target,
204206
features: Option<&'sess Features>,
205-
emit_errors: ShouldEmit,
207+
should_emit: ShouldEmit,
206208
args: &I,
207209
parse_fn: fn(cx: &mut AcceptContext<'_, '_, Early>, item: &I) -> T,
208210
template: &AttributeTemplate,
@@ -212,7 +214,8 @@ impl<'sess> AttributeParser<'sess, Early> {
212214
tools: Vec::new(),
213215
parse_only: None,
214216
sess,
215-
stage: Early { emit_errors },
217+
stage: PhantomData,
218+
should_emit,
216219
};
217220
let mut emit_lint = |lint_id: LintId, span: MultiSpan, kind: EmitAttribute| match kind {
218221
EmitAttribute::Static(kind) => {
@@ -254,9 +257,16 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
254257
sess: &'sess Session,
255258
features: &'sess Features,
256259
tools: Vec<Symbol>,
257-
stage: S,
260+
should_emit: ShouldEmit,
258261
) -> Self {
259-
Self { features: Some(features), tools, parse_only: None, sess, stage }
262+
Self {
263+
features: Some(features),
264+
tools,
265+
parse_only: None,
266+
sess,
267+
should_emit,
268+
stage: PhantomData,
269+
}
260270
}
261271

262272
pub(crate) fn sess(&self) -> &'sess Session {
@@ -276,7 +286,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
276286
}
277287

278288
pub(crate) fn emit_err(&self, diag: impl for<'x> Diagnostic<'x>) -> ErrorGuaranteed {
279-
self.stage.should_emit().emit_err(self.sess.dcx().create_err(diag))
289+
self.should_emit.emit_err(self.sess.dcx().create_err(diag))
280290
}
281291

282292
/// Parse a list of attributes.
@@ -364,7 +374,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
364374
args,
365375
&parts,
366376
&self.sess.psess,
367-
self.stage.should_emit(),
377+
self.should_emit,
368378
AllowExprMetavar::No,
369379
) else {
370380
continue;
@@ -419,7 +429,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
419429
(accept.accept_fn)(&mut cx, &args);
420430
finalizers.push(&accept.finalizer);
421431

422-
if !matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
432+
if !matches!(cx.should_emit, ShouldEmit::Nothing) {
423433
Self::check_target(&accept.allowed_targets, target, &mut cx);
424434
}
425435
} else {
@@ -440,7 +450,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
440450
&mut emit_lint,
441451
);
442452

443-
if !matches!(self.stage.should_emit(), ShouldEmit::Nothing)
453+
if !matches!(self.should_emit, ShouldEmit::Nothing)
444454
&& target == Target::Crate
445455
{
446456
self.check_invalid_crate_level_attr_item(&attr, n.item.span());
@@ -473,9 +483,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
473483
}
474484
}
475485

476-
if !matches!(self.stage.should_emit(), ShouldEmit::Nothing)
477-
&& target == Target::WherePredicate
478-
{
486+
if !matches!(self.should_emit, ShouldEmit::Nothing) && target == Target::WherePredicate {
479487
self.check_invalid_where_predicate_attrs(attributes.iter().chain(&dropped_attributes));
480488
}
481489

compiler/rustc_attr_parsing/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,5 @@ pub use attributes::cfg_select::*;
114114
pub use attributes::util::{is_builtin_attr, parse_version};
115115
pub use context::{Early, Late, OmitDoc, ShouldEmit};
116116
pub use interface::{AttributeParser, EmitAttribute};
117+
pub use rustc_parse::parser::Recovery;
117118
pub use session_diagnostics::ParsedDescription;

compiler/rustc_attr_parsing/src/safety.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
1818
expected_safety: AttributeSafety,
1919
emit_lint: &mut impl FnMut(LintId, MultiSpan, EmitAttribute),
2020
) {
21-
if matches!(self.stage.should_emit(), ShouldEmit::Nothing) {
21+
if matches!(self.should_emit, ShouldEmit::Nothing) {
2222
return;
2323
}
2424

compiler/rustc_resolve/src/def_collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
150150
&self.r.tcx.sess,
151151
self.r.tcx.features(),
152152
Vec::new(),
153-
Early { emit_errors: ShouldEmit::Nothing },
153+
ShouldEmit::Nothing,
154154
);
155155
let attrs = parser.parse_attribute_list(
156156
&i.attrs,

0 commit comments

Comments
 (0)