Skip to content

Commit c7a8c64

Browse files
authored
Unrolled build for rust-lang#121109
Rollup merge of rust-lang#121109 - nnethercote:TyKind-Err-guar-2, r=oli-obk Add an ErrorGuaranteed to ast::TyKind::Err (attempt 2) This makes it more like `hir::TyKind::Err`, and avoids a `has_errors` assertion in `LoweringContext::lower_ty_direct`. r? ```@oli-obk```
2 parents a447249 + 5233bc9 commit c7a8c64

File tree

14 files changed

+75
-36
lines changed

14 files changed

+75
-36
lines changed

compiler/rustc_ast/src/ast.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2136,10 +2136,12 @@ pub enum TyKind {
21362136
ImplicitSelf,
21372137
/// A macro in the type position.
21382138
MacCall(P<MacCall>),
2139-
/// Placeholder for a kind that has failed to be defined.
2140-
Err,
21412139
/// Placeholder for a `va_list`.
21422140
CVarArgs,
2141+
/// Sometimes we need a dummy value when no error has occurred.
2142+
Dummy,
2143+
/// Placeholder for a kind that has failed to be defined.
2144+
Err(ErrorGuaranteed),
21432145
}
21442146

21452147
impl TyKind {

compiler/rustc_ast/src/mut_visit.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,12 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
481481
let Ty { id, kind, span, tokens } = ty.deref_mut();
482482
vis.visit_id(id);
483483
match kind {
484-
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err | TyKind::Never | TyKind::CVarArgs => {}
484+
TyKind::Infer
485+
| TyKind::ImplicitSelf
486+
| TyKind::Err(_)
487+
| TyKind::Dummy
488+
| TyKind::Never
489+
| TyKind::CVarArgs => {}
485490
TyKind::Slice(ty) => vis.visit_ty(ty),
486491
TyKind::Ptr(mt) => vis.visit_mt(mt),
487492
TyKind::Ref(lt, mt) => {
@@ -1649,7 +1654,7 @@ impl DummyAstNode for Ty {
16491654
fn dummy() -> Self {
16501655
Ty {
16511656
id: DUMMY_NODE_ID,
1652-
kind: TyKind::Err,
1657+
kind: TyKind::Dummy,
16531658
span: Default::default(),
16541659
tokens: Default::default(),
16551660
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
447447
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
448448
}
449449
TyKind::Typeof(expression) => visitor.visit_anon_const(expression),
450-
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
450+
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Dummy | TyKind::Err(_) => {}
451451
TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
452452
TyKind::Never | TyKind::CVarArgs => {}
453453
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {

compiler/rustc_ast_lowering/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12861286
fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
12871287
let kind = match &t.kind {
12881288
TyKind::Infer => hir::TyKind::Infer,
1289-
TyKind::Err => hir::TyKind::Err(self.dcx().has_errors().unwrap()),
1289+
TyKind::Err(guar) => hir::TyKind::Err(*guar),
12901290
// Lower the anonymous structs or unions in a nested lowering context.
12911291
//
12921292
// ```
@@ -1504,6 +1504,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
15041504
);
15051505
hir::TyKind::Err(guar)
15061506
}
1507+
TyKind::Dummy => panic!("`TyKind::Dummy` should never be lowered"),
15071508
};
15081509

15091510
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }

compiler/rustc_ast_passes/src/ast_validation.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,8 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
881881
&item.vis,
882882
errors::VisibilityNotPermittedNote::TraitImpl,
883883
);
884-
if let TyKind::Err = self_ty.kind {
884+
// njn: use Dummy here
885+
if let TyKind::Err(_) = self_ty.kind {
885886
this.dcx().emit_err(errors::ObsoleteAuto { span: item.span });
886887
}
887888
if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity)

compiler/rustc_ast_pretty/src/pprust/state.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1048,11 +1048,16 @@ impl<'a> State<'a> {
10481048
ast::TyKind::Infer => {
10491049
self.word("_");
10501050
}
1051-
ast::TyKind::Err => {
1051+
ast::TyKind::Err(_) => {
10521052
self.popen();
10531053
self.word("/*ERROR*/");
10541054
self.pclose();
10551055
}
1056+
ast::TyKind::Dummy => {
1057+
self.popen();
1058+
self.word("/*DUMMY*/");
1059+
self.pclose();
1060+
}
10561061
ast::TyKind::ImplicitSelf => {
10571062
self.word("Self");
10581063
}

compiler/rustc_expand/src/base.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,13 @@ impl DummyResult {
567567
}
568568

569569
/// A plain dummy type.
570-
pub fn raw_ty(sp: Span, is_error: bool) -> P<ast::Ty> {
570+
pub fn raw_ty(sp: Span) -> P<ast::Ty> {
571+
// FIXME(nnethercote): you might expect `ast::TyKind::Dummy` to be used here, but some
572+
// values produced here end up being lowered to HIR, which `ast::TyKind::Dummy` does not
573+
// support, so we use an empty tuple instead.
571574
P(ast::Ty {
572575
id: ast::DUMMY_NODE_ID,
573-
kind: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(ThinVec::new()) },
576+
kind: ast::TyKind::Tup(ThinVec::new()),
574577
span: sp,
575578
tokens: None,
576579
})
@@ -611,7 +614,7 @@ impl MacResult for DummyResult {
611614
}
612615

613616
fn make_ty(self: Box<DummyResult>) -> Option<P<ast::Ty>> {
614-
Some(DummyResult::raw_ty(self.span, self.is_error))
617+
Some(DummyResult::raw_ty(self.span))
615618
}
616619

617620
fn make_arms(self: Box<DummyResult>) -> Option<SmallVec<[ast::Arm; 1]>> {

compiler/rustc_parse/src/parser/diagnostics.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ use std::ops::{Deref, DerefMut};
4646
use thin_vec::{thin_vec, ThinVec};
4747

4848
/// Creates a placeholder argument.
49-
pub(super) fn dummy_arg(ident: Ident) -> Param {
49+
pub(super) fn dummy_arg(ident: Ident, guar: ErrorGuaranteed) -> Param {
5050
let pat = P(Pat {
5151
id: ast::DUMMY_NODE_ID,
5252
kind: PatKind::Ident(BindingAnnotation::NONE, ident, None),
5353
span: ident.span,
5454
tokens: None,
5555
});
56-
let ty = Ty { kind: TyKind::Err, span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };
56+
let ty = Ty { kind: TyKind::Err(guar), span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };
5757
Param {
5858
attrs: AttrVec::default(),
5959
id: ast::DUMMY_NODE_ID,
@@ -1540,14 +1540,14 @@ impl<'a> Parser<'a> {
15401540
pub(super) fn maybe_recover_from_question_mark(&mut self, ty: P<Ty>) -> P<Ty> {
15411541
if self.token == token::Question {
15421542
self.bump();
1543-
self.dcx().emit_err(QuestionMarkInType {
1543+
let guar = self.dcx().emit_err(QuestionMarkInType {
15441544
span: self.prev_token.span,
15451545
sugg: QuestionMarkInTypeSugg {
15461546
left: ty.span.shrink_to_lo(),
15471547
right: self.prev_token.span,
15481548
},
15491549
});
1550-
self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err)
1550+
self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err(guar))
15511551
} else {
15521552
ty
15531553
}
@@ -2304,8 +2304,8 @@ impl<'a> Parser<'a> {
23042304

23052305
pub(super) fn recover_bad_self_param(&mut self, mut param: Param) -> PResult<'a, Param> {
23062306
let span = param.pat.span;
2307-
param.ty.kind = TyKind::Err;
2308-
self.dcx().emit_err(SelfParamNotFirst { span });
2307+
let guar = self.dcx().emit_err(SelfParamNotFirst { span });
2308+
param.ty.kind = TyKind::Err(guar);
23092309
Ok(param)
23102310
}
23112311

@@ -2437,7 +2437,7 @@ impl<'a> Parser<'a> {
24372437
pub(super) fn deduplicate_recovered_params_names(&self, fn_inputs: &mut ThinVec<Param>) {
24382438
let mut seen_inputs = FxHashSet::default();
24392439
for input in fn_inputs.iter_mut() {
2440-
let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err) =
2440+
let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err(_)) =
24412441
(&input.pat.kind, &input.ty.kind)
24422442
{
24432443
Some(*ident)
@@ -2644,8 +2644,10 @@ impl<'a> Parser<'a> {
26442644
"::",
26452645
Applicability::MaybeIncorrect,
26462646
);
2647-
err.emit();
2648-
return Ok(GenericArg::Type(self.mk_ty(start.to(expr.span), TyKind::Err)));
2647+
let guar = err.emit();
2648+
return Ok(GenericArg::Type(
2649+
self.mk_ty(start.to(expr.span), TyKind::Err(guar)),
2650+
));
26492651
} else if token::Comma == self.token.kind || self.token.kind.should_end_const_arg()
26502652
{
26512653
// Avoid the following output by checking that we consumed a full const arg:

compiler/rustc_parse/src/parser/item.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,23 @@ impl<'a> Parser<'a> {
591591
let ty_second = if self.token == token::DotDot {
592592
// We need to report this error after `cfg` expansion for compatibility reasons
593593
self.bump(); // `..`, do not add it to expected tokens
594-
Some(self.mk_ty(self.prev_token.span, TyKind::Err))
594+
595+
// FIXME(nnethercote): AST validation later detects this
596+
// `TyKind::Err` and emits an errors. So why the unchecked
597+
// ErrorGuaranteed?
598+
// - A `span_delayed_bug` doesn't work here, because rustfmt can
599+
// hit this path but then not hit the follow-up path in the AST
600+
// validator that issues the error, which results in ICEs.
601+
// - `TyKind::Dummy` doesn't work, because it ends up reaching HIR
602+
// lowering, which results in ICEs. Changing `TyKind::Dummy` to
603+
// `TyKind::Err` during AST validation might fix that, but that's
604+
// not possible because AST validation doesn't allow mutability.
605+
//
606+
// #121072 will hopefully remove all this special handling of the
607+
// obsolete `impl Trait for ..` and then this can go away.
608+
#[allow(deprecated)]
609+
let guar = rustc_errors::ErrorGuaranteed::unchecked_error_guaranteed();
610+
Some(self.mk_ty(self.prev_token.span, TyKind::Err(guar)))
595611
} else if has_for || self.token.can_begin_type() {
596612
Some(self.parse_ty()?)
597613
} else {
@@ -2628,13 +2644,13 @@ impl<'a> Parser<'a> {
26282644
p.recover_diff_marker();
26292645
let snapshot = p.create_snapshot_for_diagnostic();
26302646
let param = p.parse_param_general(req_name, first_param).or_else(|e| {
2631-
e.emit();
2647+
let guar = e.emit();
26322648
let lo = p.prev_token.span;
26332649
p.restore_snapshot(snapshot);
26342650
// Skip every token until next possible arg or end.
26352651
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]);
26362652
// Create a placeholder argument for proper arg count (issue #34264).
2637-
Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span))))
2653+
Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span)), guar))
26382654
});
26392655
// ...now that we've parsed the first argument, `self` is no longer allowed.
26402656
first_param = false;
@@ -2671,8 +2687,8 @@ impl<'a> Parser<'a> {
26712687
return if let Some(ident) =
26722688
this.parameter_without_type(&mut err, pat, is_name_required, first_param)
26732689
{
2674-
err.emit();
2675-
Ok((dummy_arg(ident), TrailingToken::None))
2690+
let guar = err.emit();
2691+
Ok((dummy_arg(ident, guar), TrailingToken::None))
26762692
} else {
26772693
Err(err)
26782694
};

compiler/rustc_parse/src/parser/path.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,9 @@ impl<'a> Parser<'a> {
678678
c.into()
679679
}
680680
Some(GenericArg::Lifetime(lt)) => {
681-
self.dcx().emit_err(errors::AssocLifetime { span, lifetime: lt.ident.span });
682-
self.mk_ty(span, ast::TyKind::Err).into()
681+
let guar =
682+
self.dcx().emit_err(errors::AssocLifetime { span, lifetime: lt.ident.span });
683+
self.mk_ty(span, ast::TyKind::Err(guar)).into()
683684
}
684685
None => {
685686
let after_eq = eq.shrink_to_hi();
@@ -779,7 +780,7 @@ impl<'a> Parser<'a> {
779780
// type to determine if error recovery has occurred and if the input is not a
780781
// syntactically valid type after all.
781782
if let ast::TyKind::Slice(inner_ty) | ast::TyKind::Array(inner_ty, _) = &ty.kind
782-
&& let ast::TyKind::Err = inner_ty.kind
783+
&& let ast::TyKind::Err(_) = inner_ty.kind
783784
&& let Some(snapshot) = snapshot
784785
&& let Some(expr) =
785786
self.recover_unbraced_const_arg_that_can_begin_ty(snapshot)

compiler/rustc_parse/src/parser/ty.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ impl<'a> Parser<'a> {
346346
AllowCVariadic::No => {
347347
// FIXME(Centril): Should we just allow `...` syntactically
348348
// anywhere in a type and use semantic restrictions instead?
349-
self.dcx().emit_err(NestedCVariadicType { span: lo.to(self.prev_token.span) });
350-
TyKind::Err
349+
let guar = self
350+
.dcx()
351+
.emit_err(NestedCVariadicType { span: lo.to(self.prev_token.span) });
352+
TyKind::Err(guar)
351353
}
352354
}
353355
} else {
@@ -493,8 +495,8 @@ impl<'a> Parser<'a> {
493495
{
494496
// Recover from `[LIT; EXPR]` and `[LIT]`
495497
self.bump();
496-
err.emit();
497-
self.mk_ty(self.prev_token.span, TyKind::Err)
498+
let guar = err.emit();
499+
self.mk_ty(self.prev_token.span, TyKind::Err(guar))
498500
}
499501
Err(err) => return Err(err),
500502
};

compiler/rustc_passes/src/hir_stats.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,9 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
616616
Infer,
617617
ImplicitSelf,
618618
MacCall,
619-
Err,
620-
CVarArgs
619+
CVarArgs,
620+
Dummy,
621+
Err
621622
]
622623
);
623624

src/tools/clippy/clippy_utils/src/ast_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
690690
match (&l.kind, &r.kind) {
691691
(Paren(l), _) => eq_ty(l, r),
692692
(_, Paren(r)) => eq_ty(l, r),
693-
(Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
693+
(Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err(_), Err(_)) | (CVarArgs, CVarArgs) => true,
694694
(Slice(l), Slice(r)) => eq_ty(l, r),
695695
(Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
696696
(Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),

src/tools/rustfmt/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ impl Rewrite for ast::Ty {
859859
})
860860
}
861861
ast::TyKind::CVarArgs => Some("...".to_owned()),
862-
ast::TyKind::Err => Some(context.snippet(self.span).to_owned()),
862+
ast::TyKind::Dummy | ast::TyKind::Err(_) => Some(context.snippet(self.span).to_owned()),
863863
ast::TyKind::Typeof(ref anon_const) => rewrite_call(
864864
context,
865865
"typeof",

0 commit comments

Comments
 (0)