Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit e2d5da3

Browse files
committed
Auto merge of rust-lang#121072 - nnethercote:TyKind-Err-guar, r=<try>
Add an `ErrorGuaranteed` to `ast::TyKind::Err`. This makes it more like `hir::TyKind::Err`, and avoids a `span_delayed_bug` call in `LoweringContext::lower_ty_direct`. r? `@oli-obk`
2 parents bb89df6 + 3a97622 commit e2d5da3

File tree

19 files changed

+48
-113
lines changed

19 files changed

+48
-113
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,10 +2136,10 @@ 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+
/// Placeholder for a kind that has failed to be defined.
2142+
Err(ErrorGuaranteed),
21432143
}
21442144

21452145
impl TyKind {

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,11 @@ 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::Never
488+
| TyKind::CVarArgs => {}
485489
TyKind::Slice(ty) => vis.visit_ty(ty),
486490
TyKind::Ptr(mt) => vis.visit_mt(mt),
487491
TyKind::Ref(lt, mt) => {
@@ -1649,7 +1653,7 @@ impl DummyAstNode for Ty {
16491653
fn dummy() -> Self {
16501654
Ty {
16511655
id: DUMMY_NODE_ID,
1652-
kind: TyKind::Err,
1656+
kind: TyKind::Tup(ThinVec::new()),
16531657
span: Default::default(),
16541658
tokens: Default::default(),
16551659
}

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 1 deletion
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::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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,9 +1285,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12851285
fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
12861286
let kind = match &t.kind {
12871287
TyKind::Infer => hir::TyKind::Infer,
1288-
TyKind::Err => {
1289-
hir::TyKind::Err(self.dcx().span_delayed_bug(t.span, "TyKind::Err lowered"))
1290-
}
1288+
TyKind::Err(guar) => hir::TyKind::Err(*guar),
12911289
// Lower the anonymous structs or unions in a nested lowering context.
12921290
//
12931291
// ```

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
881881
&item.vis,
882882
errors::VisibilityNotPermittedNote::TraitImpl,
883883
);
884-
if let TyKind::Err = self_ty.kind {
884+
if let TyKind::Err(_) = self_ty.kind {
885885
this.dcx().emit_err(errors::ObsoleteAuto { span: item.span });
886886
}
887887
if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity)

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ 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();

compiler/rustc_expand/src/base.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,10 @@ 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> {
571571
P(ast::Ty {
572572
id: ast::DUMMY_NODE_ID,
573-
kind: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(ThinVec::new()) },
573+
kind: ast::TyKind::Tup(ThinVec::new()),
574574
span: sp,
575575
tokens: None,
576576
})
@@ -611,7 +611,7 @@ impl MacResult for DummyResult {
611611
}
612612

613613
fn make_ty(self: Box<DummyResult>) -> Option<P<ast::Ty>> {
614-
Some(DummyResult::raw_ty(self.span, self.is_error))
614+
Some(DummyResult::raw_ty(self.span))
615615
}
616616

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

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 11 additions & 9 deletions
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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -588,15 +588,8 @@ impl<'a> Parser<'a> {
588588
let has_for = self.eat_keyword(kw::For);
589589
let missing_for_span = self.prev_token.span.between(self.token.span);
590590

591-
let ty_second = if self.token == token::DotDot {
592-
// We need to report this error after `cfg` expansion for compatibility reasons
593-
self.bump(); // `..`, do not add it to expected tokens
594-
Some(self.mk_ty(self.prev_token.span, TyKind::Err))
595-
} else if has_for || self.token.can_begin_type() {
596-
Some(self.parse_ty()?)
597-
} else {
598-
None
599-
};
591+
let ty_second =
592+
if has_for || self.token.can_begin_type() { Some(self.parse_ty()?) } else { None };
600593

601594
generics.where_clause = self.parse_where_clause()?;
602595

@@ -2628,13 +2621,13 @@ impl<'a> Parser<'a> {
26282621
p.recover_diff_marker();
26292622
let snapshot = p.create_snapshot_for_diagnostic();
26302623
let param = p.parse_param_general(req_name, first_param).or_else(|e| {
2631-
e.emit();
2624+
let guar = e.emit();
26322625
let lo = p.prev_token.span;
26332626
p.restore_snapshot(snapshot);
26342627
// Skip every token until next possible arg or end.
26352628
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]);
26362629
// Create a placeholder argument for proper arg count (issue #34264).
2637-
Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span))))
2630+
Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span)), guar))
26382631
});
26392632
// ...now that we've parsed the first argument, `self` is no longer allowed.
26402633
first_param = false;
@@ -2671,8 +2664,8 @@ impl<'a> Parser<'a> {
26712664
return if let Some(ident) =
26722665
this.parameter_without_type(&mut err, pat, is_name_required, first_param)
26732666
{
2674-
err.emit();
2675-
Ok((dummy_arg(ident), TrailingToken::None))
2667+
let guar = err.emit();
2668+
Ok((dummy_arg(ident, guar), TrailingToken::None))
26762669
} else {
26772670
Err(err)
26782671
};

compiler/rustc_parse/src/parser/path.rs

Lines changed: 4 additions & 3 deletions
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

Lines changed: 6 additions & 4 deletions
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
};

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ impl Rewrite for ast::Ty {
848848
})
849849
}
850850
ast::TyKind::CVarArgs => Some("...".to_owned()),
851-
ast::TyKind::Err => Some(context.snippet(self.span).to_owned()),
851+
ast::TyKind::Err(_) => Some(context.snippet(self.span).to_owned()),
852852
ast::TyKind::Typeof(ref anon_const) => rewrite_call(
853853
context,
854854
"typeof",

tests/ui/parser/impl-parsing.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ impl ! {} // OK
22
impl ! where u8: Copy {} // OK
33

44
impl Trait Type {} //~ ERROR missing `for` in a trait impl
5-
impl Trait .. {} //~ ERROR missing `for` in a trait impl
65
impl ?Sized for Type {} //~ ERROR expected a trait, found type
7-
impl ?Sized for .. {} //~ ERROR expected a trait, found type
86

97
default unsafe FAIL //~ ERROR expected item, found keyword `unsafe`
108
//~^ ERROR `default` is not followed by an item

tests/ui/parser/impl-parsing.stderr

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,27 @@ error: missing `for` in a trait impl
44
LL | impl Trait Type {}
55
| ^ help: add `for` here
66

7-
error: missing `for` in a trait impl
8-
--> $DIR/impl-parsing.rs:5:11
9-
|
10-
LL | impl Trait .. {}
11-
| ^ help: add `for` here
12-
137
error: expected a trait, found type
14-
--> $DIR/impl-parsing.rs:6:6
8+
--> $DIR/impl-parsing.rs:5:6
159
|
1610
LL | impl ?Sized for Type {}
1711
| ^^^^^^
1812

19-
error: expected a trait, found type
20-
--> $DIR/impl-parsing.rs:7:6
21-
|
22-
LL | impl ?Sized for .. {}
23-
| ^^^^^^
24-
2513
error: `default` is not followed by an item
26-
--> $DIR/impl-parsing.rs:9:1
14+
--> $DIR/impl-parsing.rs:7:1
2715
|
2816
LL | default unsafe FAIL
2917
| ^^^^^^^ the `default` qualifier
3018
|
3119
= note: only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`
3220

3321
error: expected item, found keyword `unsafe`
34-
--> $DIR/impl-parsing.rs:9:9
22+
--> $DIR/impl-parsing.rs:7:9
3523
|
3624
LL | default unsafe FAIL
3725
| ^^^^^^ expected item
3826
|
3927
= note: for a full list of items that can appear in modules, see <https://doc.rust-lang.org/reference/items.html>
4028

41-
error: aborting due to 6 previous errors
29+
error: aborting due to 4 previous errors
4230

tests/ui/parser/issues/issue-27255.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/ui/parser/issues/issue-27255.stderr

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/ui/parser/obsolete-syntax-impl-for-dotdot.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/ui/parser/obsolete-syntax-impl-for-dotdot.stderr

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)