Skip to content

Commit 0f16d2c

Browse files
committed
Auto merge of #155775 - JonathanBrouwer:cleanup_stage, r=<try>
Cleanup `Stage`
2 parents fb76025 + 003667b commit 0f16d2c

7 files changed

Lines changed: 50 additions & 78 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;
@@ -223,7 +223,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
223223
tcx.sess,
224224
tcx.features(),
225225
registered_tools,
226-
Late,
226+
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
227227
),
228228
delayed_lints: Vec::new(),
229229
}

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: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rustc_hir::attrs::AttributeKind;
1414
use rustc_hir::lints::AttributeLintKind;
1515
use rustc_hir::{AttrPath, HirId};
1616
use rustc_parse::parser::Recovery;
17-
use rustc_session::Session;
1817
use rustc_session::lint::{Lint, LintId};
1918
use rustc_span::{ErrorGuaranteed, Span, Symbol};
2019

@@ -356,14 +355,6 @@ pub trait Stage: Sized + 'static + Sealed {
356355
type Id: Copy;
357356

358357
fn parsers() -> &'static GroupType<Self>;
359-
360-
fn emit_err<'sess>(
361-
&self,
362-
sess: &'sess Session,
363-
diag: impl for<'x> Diagnostic<'x>,
364-
) -> ErrorGuaranteed;
365-
366-
fn should_emit(&self) -> ShouldEmit;
367358
}
368359

369360
// allow because it's a sealed trait
@@ -374,17 +365,6 @@ impl Stage for Early {
374365
fn parsers() -> &'static GroupType<Self> {
375366
&early::ATTRIBUTE_PARSERS
376367
}
377-
fn emit_err<'sess>(
378-
&self,
379-
sess: &'sess Session,
380-
diag: impl for<'x> Diagnostic<'x>,
381-
) -> ErrorGuaranteed {
382-
self.should_emit().emit_err(sess.dcx().create_err(diag))
383-
}
384-
385-
fn should_emit(&self) -> ShouldEmit {
386-
self.emit_errors
387-
}
388368
}
389369

390370
// allow because it's a sealed trait
@@ -395,26 +375,11 @@ impl Stage for Late {
395375
fn parsers() -> &'static GroupType<Self> {
396376
&late::ATTRIBUTE_PARSERS
397377
}
398-
fn emit_err<'sess>(
399-
&self,
400-
tcx: &'sess Session,
401-
diag: impl for<'x> Diagnostic<'x>,
402-
) -> ErrorGuaranteed {
403-
tcx.dcx().emit_err(diag)
404-
}
405-
406-
fn should_emit(&self) -> ShouldEmit {
407-
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed }
408-
}
409378
}
410379

411380
/// Used when parsing attributes for miscellaneous things *before* ast lowering
412-
pub struct Early {
413-
/// Whether to emit errors or delay them as a bug.
414-
/// For most attributes, the attribute will be parsed again in the `Late` stage and in this case the errors should be delayed.
415-
/// But for some, such as `cfg`, the attribute will be removed before the `Late` stage so errors must be emitted.
416-
pub emit_errors: ShouldEmit,
417-
}
381+
pub struct Early;
382+
418383
/// used when parsing attributes during ast lowering
419384
pub struct Late;
420385

@@ -458,7 +423,7 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
458423

459424
impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
460425
pub(crate) fn emit_err(&self, diag: impl for<'x> Diagnostic<'x>) -> ErrorGuaranteed {
461-
self.stage.emit_err(&self.sess, diag)
426+
self.cx.emit_err(diag)
462427
}
463428

464429
/// Emit a lint. This method is somewhat special, since lints emitted during attribute parsing
@@ -494,7 +459,7 @@ impl<'f, 'sess: 'f, S: Stage> SharedContext<'f, 'sess, S> {
494459
span: impl Into<MultiSpan>,
495460
) {
496461
if !matches!(
497-
self.stage.should_emit(),
462+
self.should_emit,
498463
ShouldEmit::ErrorsAndLints { .. } | ShouldEmit::EarlyFatal { also_emit_lints: true }
499464
) {
500465
return;

compiler/rustc_attr_parsing/src/interface.rs

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

34
use rustc_ast as ast;
45
use rustc_ast::token::DocFragmentKind;
56
use rustc_ast::{AttrItemKind, AttrStyle, CRATE_NODE_ID, NodeId, Safety};
67
use rustc_data_structures::sync::{DynSend, DynSync};
7-
use rustc_errors::{Diag, DiagCtxtHandle, Level, MultiSpan};
8+
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan};
89
use rustc_feature::{AttributeTemplate, Features};
910
use rustc_hir::attrs::AttributeKind;
1011
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};
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 {
@@ -275,6 +285,10 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
275285
self.sess().dcx()
276286
}
277287

288+
pub(crate) fn emit_err(&self, diag: impl for<'x> Diagnostic<'x>) -> ErrorGuaranteed {
289+
self.should_emit.emit_err(self.sess.dcx().create_err(diag))
290+
}
291+
278292
/// Parse a list of attributes.
279293
///
280294
/// `target_span` is the span of the thing this list of attributes is applied to,
@@ -360,7 +374,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
360374
args,
361375
&parts,
362376
&self.sess.psess,
363-
self.stage.should_emit(),
377+
self.should_emit,
364378
AllowExprMetavar::No,
365379
) else {
366380
continue;
@@ -415,7 +429,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
415429
(accept.accept_fn)(&mut cx, &args);
416430
finalizers.push(&accept.finalizer);
417431

418-
if !matches!(cx.stage.should_emit(), ShouldEmit::Nothing) {
432+
if !matches!(cx.should_emit, ShouldEmit::Nothing) {
419433
Self::check_target(&accept.allowed_targets, target, &mut cx);
420434
}
421435
} else {
@@ -436,7 +450,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
436450
&mut emit_lint,
437451
);
438452

439-
if !matches!(self.stage.should_emit(), ShouldEmit::Nothing)
453+
if !matches!(self.should_emit, ShouldEmit::Nothing)
440454
&& target == Target::Crate
441455
{
442456
self.check_invalid_crate_level_attr_item(&attr, n.item.span());
@@ -469,9 +483,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
469483
}
470484
}
471485

472-
if !matches!(self.stage.should_emit(), ShouldEmit::Nothing)
473-
&& target == Target::WherePredicate
474-
{
486+
if !matches!(self.should_emit, ShouldEmit::Nothing) && target == Target::WherePredicate {
475487
self.check_invalid_where_predicate_attrs(attributes.iter().chain(&dropped_attributes));
476488
}
477489

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: 14 additions & 20 deletions
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

@@ -63,18 +63,15 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
6363
}
6464

6565
if emit_error {
66-
self.stage.emit_err(
67-
self.sess,
68-
crate::session_diagnostics::UnsafeAttrOutsideUnsafe {
69-
span: path_span,
70-
suggestion: not_from_proc_macro.then(|| {
71-
crate::session_diagnostics::UnsafeAttrOutsideUnsafeSuggestion {
72-
left: diag_span.shrink_to_lo(),
73-
right: diag_span.shrink_to_hi(),
74-
}
75-
}),
76-
},
77-
);
66+
self.emit_err(crate::session_diagnostics::UnsafeAttrOutsideUnsafe {
67+
span: path_span,
68+
suggestion: not_from_proc_macro.then(|| {
69+
crate::session_diagnostics::UnsafeAttrOutsideUnsafeSuggestion {
70+
left: diag_span.shrink_to_lo(),
71+
right: diag_span.shrink_to_hi(),
72+
}
73+
}),
74+
});
7875
} else {
7976
emit_lint(
8077
LintId::of(UNSAFE_ATTR_OUTSIDE_UNSAFE),
@@ -97,13 +94,10 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
9794
// - Normal builtin attribute
9895
// - Writing `#[unsafe(..)]` is not permitted on normal builtin attributes
9996
(AttributeSafety::Normal, Safety::Unsafe(unsafe_span)) => {
100-
self.stage.emit_err(
101-
self.sess,
102-
crate::session_diagnostics::InvalidAttrUnsafe {
103-
span: unsafe_span,
104-
name: attr_path.clone(),
105-
},
106-
);
97+
self.emit_err(crate::session_diagnostics::InvalidAttrUnsafe {
98+
span: unsafe_span,
99+
name: attr_path.clone(),
100+
});
107101
}
108102

109103
// - Normal builtin attribute

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)