Skip to content

Commit

Permalink
Auto merge of #121072 - nnethercote:TyKind-Err-guar, r=<try>
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Feb 14, 2024
2 parents bb89df6 + 3a97622 commit e2d5da3
Show file tree
Hide file tree
Showing 19 changed files with 48 additions and 113 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2136,10 +2136,10 @@ pub enum TyKind {
ImplicitSelf,
/// A macro in the type position.
MacCall(P<MacCall>),
/// Placeholder for a kind that has failed to be defined.
Err,
/// Placeholder for a `va_list`.
CVarArgs,
/// Placeholder for a kind that has failed to be defined.
Err(ErrorGuaranteed),
}

impl TyKind {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,11 @@ pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
let Ty { id, kind, span, tokens } = ty.deref_mut();
vis.visit_id(id);
match kind {
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err | TyKind::Never | TyKind::CVarArgs => {}
TyKind::Infer
| TyKind::ImplicitSelf
| TyKind::Err(_)
| TyKind::Never
| TyKind::CVarArgs => {}
TyKind::Slice(ty) => vis.visit_ty(ty),
TyKind::Ptr(mt) => vis.visit_mt(mt),
TyKind::Ref(lt, mt) => {
Expand Down Expand Up @@ -1649,7 +1653,7 @@ impl DummyAstNode for Ty {
fn dummy() -> Self {
Ty {
id: DUMMY_NODE_ID,
kind: TyKind::Err,
kind: TyKind::Tup(ThinVec::new()),
span: Default::default(),
tokens: Default::default(),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ pub fn walk_ty<'a, V: Visitor<'a>>(visitor: &mut V, typ: &'a Ty) {
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Impl);
}
TyKind::Typeof(expression) => visitor.visit_anon_const(expression),
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err => {}
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err(_) => {}
TyKind::MacCall(mac) => visitor.visit_mac_call(mac),
TyKind::Never | TyKind::CVarArgs => {}
TyKind::AnonStruct(_, ref fields) | TyKind::AnonUnion(_, ref fields) => {
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,9 +1285,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_ty_direct(&mut self, t: &Ty, itctx: ImplTraitContext) -> hir::Ty<'hir> {
let kind = match &t.kind {
TyKind::Infer => hir::TyKind::Infer,
TyKind::Err => {
hir::TyKind::Err(self.dcx().span_delayed_bug(t.span, "TyKind::Err lowered"))
}
TyKind::Err(guar) => hir::TyKind::Err(*guar),
// Lower the anonymous structs or unions in a nested lowering context.
//
// ```
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
&item.vis,
errors::VisibilityNotPermittedNote::TraitImpl,
);
if let TyKind::Err = self_ty.kind {
if let TyKind::Err(_) = self_ty.kind {
this.dcx().emit_err(errors::ObsoleteAuto { span: item.span });
}
if let (&Unsafe::Yes(span), &ImplPolarity::Negative(sp)) = (unsafety, polarity)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ impl<'a> State<'a> {
ast::TyKind::Infer => {
self.word("_");
}
ast::TyKind::Err => {
ast::TyKind::Err(_) => {
self.popen();
self.word("/*ERROR*/");
self.pclose();
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,10 +567,10 @@ impl DummyResult {
}

/// A plain dummy type.
pub fn raw_ty(sp: Span, is_error: bool) -> P<ast::Ty> {
pub fn raw_ty(sp: Span) -> P<ast::Ty> {
P(ast::Ty {
id: ast::DUMMY_NODE_ID,
kind: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(ThinVec::new()) },
kind: ast::TyKind::Tup(ThinVec::new()),
span: sp,
tokens: None,
})
Expand Down Expand Up @@ -611,7 +611,7 @@ impl MacResult for DummyResult {
}

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

fn make_arms(self: Box<DummyResult>) -> Option<SmallVec<[ast::Arm; 1]>> {
Expand Down
20 changes: 11 additions & 9 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ use std::ops::{Deref, DerefMut};
use thin_vec::{thin_vec, ThinVec};

/// Creates a placeholder argument.
pub(super) fn dummy_arg(ident: Ident) -> Param {
pub(super) fn dummy_arg(ident: Ident, guar: ErrorGuaranteed) -> Param {
let pat = P(Pat {
id: ast::DUMMY_NODE_ID,
kind: PatKind::Ident(BindingAnnotation::NONE, ident, None),
span: ident.span,
tokens: None,
});
let ty = Ty { kind: TyKind::Err, span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };
let ty = Ty { kind: TyKind::Err(guar), span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };
Param {
attrs: AttrVec::default(),
id: ast::DUMMY_NODE_ID,
Expand Down Expand Up @@ -1540,14 +1540,14 @@ impl<'a> Parser<'a> {
pub(super) fn maybe_recover_from_question_mark(&mut self, ty: P<Ty>) -> P<Ty> {
if self.token == token::Question {
self.bump();
self.dcx().emit_err(QuestionMarkInType {
let guar = self.dcx().emit_err(QuestionMarkInType {
span: self.prev_token.span,
sugg: QuestionMarkInTypeSugg {
left: ty.span.shrink_to_lo(),
right: self.prev_token.span,
},
});
self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err)
self.mk_ty(ty.span.to(self.prev_token.span), TyKind::Err(guar))
} else {
ty
}
Expand Down Expand Up @@ -2304,8 +2304,8 @@ impl<'a> Parser<'a> {

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

Expand Down Expand Up @@ -2437,7 +2437,7 @@ impl<'a> Parser<'a> {
pub(super) fn deduplicate_recovered_params_names(&self, fn_inputs: &mut ThinVec<Param>) {
let mut seen_inputs = FxHashSet::default();
for input in fn_inputs.iter_mut() {
let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err) =
let opt_ident = if let (PatKind::Ident(_, ident, _), TyKind::Err(_)) =
(&input.pat.kind, &input.ty.kind)
{
Some(*ident)
Expand Down Expand Up @@ -2644,8 +2644,10 @@ impl<'a> Parser<'a> {
"::",
Applicability::MaybeIncorrect,
);
err.emit();
return Ok(GenericArg::Type(self.mk_ty(start.to(expr.span), TyKind::Err)));
let guar = err.emit();
return Ok(GenericArg::Type(
self.mk_ty(start.to(expr.span), TyKind::Err(guar)),
));
} else if token::Comma == self.token.kind || self.token.kind.should_end_const_arg()
{
// Avoid the following output by checking that we consumed a full const arg:
Expand Down
19 changes: 6 additions & 13 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,15 +588,8 @@ impl<'a> Parser<'a> {
let has_for = self.eat_keyword(kw::For);
let missing_for_span = self.prev_token.span.between(self.token.span);

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

generics.where_clause = self.parse_where_clause()?;

Expand Down Expand Up @@ -2628,13 +2621,13 @@ impl<'a> Parser<'a> {
p.recover_diff_marker();
let snapshot = p.create_snapshot_for_diagnostic();
let param = p.parse_param_general(req_name, first_param).or_else(|e| {
e.emit();
let guar = e.emit();
let lo = p.prev_token.span;
p.restore_snapshot(snapshot);
// Skip every token until next possible arg or end.
p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]);
// Create a placeholder argument for proper arg count (issue #34264).
Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span))))
Ok(dummy_arg(Ident::new(kw::Empty, lo.to(p.prev_token.span)), guar))
});
// ...now that we've parsed the first argument, `self` is no longer allowed.
first_param = false;
Expand Down Expand Up @@ -2671,8 +2664,8 @@ impl<'a> Parser<'a> {
return if let Some(ident) =
this.parameter_without_type(&mut err, pat, is_name_required, first_param)
{
err.emit();
Ok((dummy_arg(ident), TrailingToken::None))
let guar = err.emit();
Ok((dummy_arg(ident, guar), TrailingToken::None))
} else {
Err(err)
};
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_parse/src/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,9 @@ impl<'a> Parser<'a> {
c.into()
}
Some(GenericArg::Lifetime(lt)) => {
self.dcx().emit_err(errors::AssocLifetime { span, lifetime: lt.ident.span });
self.mk_ty(span, ast::TyKind::Err).into()
let guar =
self.dcx().emit_err(errors::AssocLifetime { span, lifetime: lt.ident.span });
self.mk_ty(span, ast::TyKind::Err(guar)).into()
}
None => {
let after_eq = eq.shrink_to_hi();
Expand Down Expand Up @@ -779,7 +780,7 @@ impl<'a> Parser<'a> {
// type to determine if error recovery has occurred and if the input is not a
// syntactically valid type after all.
if let ast::TyKind::Slice(inner_ty) | ast::TyKind::Array(inner_ty, _) = &ty.kind
&& let ast::TyKind::Err = inner_ty.kind
&& let ast::TyKind::Err(_) = inner_ty.kind
&& let Some(snapshot) = snapshot
&& let Some(expr) =
self.recover_unbraced_const_arg_that_can_begin_ty(snapshot)
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,10 @@ impl<'a> Parser<'a> {
AllowCVariadic::No => {
// FIXME(Centril): Should we just allow `...` syntactically
// anywhere in a type and use semantic restrictions instead?
self.dcx().emit_err(NestedCVariadicType { span: lo.to(self.prev_token.span) });
TyKind::Err
let guar = self
.dcx()
.emit_err(NestedCVariadicType { span: lo.to(self.prev_token.span) });
TyKind::Err(guar)
}
}
} else {
Expand Down Expand Up @@ -493,8 +495,8 @@ impl<'a> Parser<'a> {
{
// Recover from `[LIT; EXPR]` and `[LIT]`
self.bump();
err.emit();
self.mk_ty(self.prev_token.span, TyKind::Err)
let guar = err.emit();
self.mk_ty(self.prev_token.span, TyKind::Err(guar))
}
Err(err) => return Err(err),
};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_utils/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
match (&l.kind, &r.kind) {
(Paren(l), _) => eq_ty(l, r),
(_, Paren(r)) => eq_ty(l, r),
(Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
(Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err(_), Err(_)) | (CVarArgs, CVarArgs) => true,
(Slice(l), Slice(r)) => eq_ty(l, r),
(Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
(Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rustfmt/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ impl Rewrite for ast::Ty {
})
}
ast::TyKind::CVarArgs => Some("...".to_owned()),
ast::TyKind::Err => Some(context.snippet(self.span).to_owned()),
ast::TyKind::Err(_) => Some(context.snippet(self.span).to_owned()),
ast::TyKind::Typeof(ref anon_const) => rewrite_call(
context,
"typeof",
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/parser/impl-parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ impl ! {} // OK
impl ! where u8: Copy {} // OK

impl Trait Type {} //~ ERROR missing `for` in a trait impl
impl Trait .. {} //~ ERROR missing `for` in a trait impl
impl ?Sized for Type {} //~ ERROR expected a trait, found type
impl ?Sized for .. {} //~ ERROR expected a trait, found type

default unsafe FAIL //~ ERROR expected item, found keyword `unsafe`
//~^ ERROR `default` is not followed by an item
20 changes: 4 additions & 16 deletions tests/ui/parser/impl-parsing.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,27 @@ error: missing `for` in a trait impl
LL | impl Trait Type {}
| ^ help: add `for` here

error: missing `for` in a trait impl
--> $DIR/impl-parsing.rs:5:11
|
LL | impl Trait .. {}
| ^ help: add `for` here

error: expected a trait, found type
--> $DIR/impl-parsing.rs:6:6
--> $DIR/impl-parsing.rs:5:6
|
LL | impl ?Sized for Type {}
| ^^^^^^

error: expected a trait, found type
--> $DIR/impl-parsing.rs:7:6
|
LL | impl ?Sized for .. {}
| ^^^^^^

error: `default` is not followed by an item
--> $DIR/impl-parsing.rs:9:1
--> $DIR/impl-parsing.rs:7:1
|
LL | default unsafe FAIL
| ^^^^^^^ the `default` qualifier
|
= note: only `fn`, `const`, `type`, or `impl` items may be prefixed by `default`

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

error: aborting due to 6 previous errors
error: aborting due to 4 previous errors

10 changes: 0 additions & 10 deletions tests/ui/parser/issues/issue-27255.rs

This file was deleted.

22 changes: 0 additions & 22 deletions tests/ui/parser/issues/issue-27255.stderr

This file was deleted.

9 changes: 0 additions & 9 deletions tests/ui/parser/obsolete-syntax-impl-for-dotdot.rs

This file was deleted.

10 changes: 0 additions & 10 deletions tests/ui/parser/obsolete-syntax-impl-for-dotdot.stderr

This file was deleted.

0 comments on commit e2d5da3

Please sign in to comment.