Skip to content

Commit a3fce72

Browse files
committed
Add ast::ExprKind::Dummy
1 parent 8c0b1fc commit a3fce72

File tree

18 files changed

+37
-24
lines changed

18 files changed

+37
-24
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -1296,23 +1296,10 @@ impl Expr {
12961296
ExprKind::Yeet(..) => ExprPrecedence::Yeet,
12971297
ExprKind::FormatArgs(..) => ExprPrecedence::FormatArgs,
12981298
ExprKind::Become(..) => ExprPrecedence::Become,
1299-
ExprKind::Err => ExprPrecedence::Err,
1299+
ExprKind::Err | ExprKind::Dummy => ExprPrecedence::Err,
13001300
}
13011301
}
13021302

1303-
pub fn take(&mut self) -> Self {
1304-
mem::replace(
1305-
self,
1306-
Expr {
1307-
id: DUMMY_NODE_ID,
1308-
kind: ExprKind::Err,
1309-
span: DUMMY_SP,
1310-
attrs: AttrVec::new(),
1311-
tokens: None,
1312-
},
1313-
)
1314-
}
1315-
13161303
/// To a first-order approximation, is this a pattern?
13171304
pub fn is_approximately_pattern(&self) -> bool {
13181305
matches!(
@@ -1532,6 +1519,9 @@ pub enum ExprKind {
15321519

15331520
/// Placeholder for an expression that wasn't syntactically well formed in some way.
15341521
Err,
1522+
1523+
/// Acts as a null expression. Lowering it will always emit a bug.
1524+
Dummy,
15351525
}
15361526

15371527
/// Used to differentiate between `for` loops and `for await` loops.

Diff for: compiler/rustc_ast/src/mut_visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
15261526
}
15271527
ExprKind::Try(expr) => vis.visit_expr(expr),
15281528
ExprKind::TryBlock(body) => vis.visit_block(body),
1529-
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err => {}
1529+
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err | ExprKind::Dummy => {}
15301530
}
15311531
vis.visit_id(id);
15321532
vis.visit_span(span);
@@ -1642,7 +1642,7 @@ impl DummyAstNode for Expr {
16421642
fn dummy() -> Self {
16431643
Expr {
16441644
id: DUMMY_NODE_ID,
1645-
kind: ExprKind::Err,
1645+
kind: ExprKind::Dummy,
16461646
span: Default::default(),
16471647
attrs: Default::default(),
16481648
tokens: Default::default(),

Diff for: compiler/rustc_ast/src/util/classify.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
8989
| Paren(_)
9090
| Try(_)
9191
| Yeet(None)
92-
| Err => break None,
92+
| Err
93+
| Dummy => break None,
9394
}
9495
}
9596
}

Diff for: compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
10631063
}
10641064
ExprKind::Try(subexpression) => try_visit!(visitor.visit_expr(subexpression)),
10651065
ExprKind::TryBlock(body) => try_visit!(visitor.visit_block(body)),
1066-
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err => {}
1066+
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err | ExprKind::Dummy => {}
10671067
}
10681068

10691069
visitor.visit_expr_post(expression)

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_ast::*;
1414
use rustc_data_structures::stack::ensure_sufficient_stack;
1515
use rustc_hir as hir;
1616
use rustc_hir::def::{DefKind, Res};
17+
use rustc_middle::span_bug;
1718
use rustc_session::errors::report_lit_error;
1819
use rustc_span::source_map::{respan, Spanned};
1920
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -331,6 +332,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
331332
ExprKind::Err => {
332333
hir::ExprKind::Err(self.dcx().span_delayed_bug(e.span, "lowered ExprKind::Err"))
333334
}
335+
336+
ExprKind::Dummy => {
337+
span_bug!(e.span, "lowered ExprKind::Dummy")
338+
}
339+
334340
ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr),
335341

336342
ExprKind::Paren(_) | ExprKind::ForLoop { .. } => {

Diff for: compiler/rustc_ast_lowering/src/pat.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
331331
ExprKind::Lit(..)
332332
| ExprKind::ConstBlock(..)
333333
| ExprKind::IncludedBytes(..)
334-
| ExprKind::Err => {}
334+
| ExprKind::Err
335+
| ExprKind::Dummy => {}
335336
ExprKind::Path(..) if allow_paths => {}
336337
ExprKind::Unary(UnOp::Neg, inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
337338
_ => {

Diff for: compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+5
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,11 @@ impl<'a> State<'a> {
898898
self.word("/*ERROR*/");
899899
self.pclose()
900900
}
901+
ast::ExprKind::Dummy => {
902+
self.popen();
903+
self.word("/*DUMMY*/");
904+
self.pclose();
905+
}
901906
}
902907

903908
self.ann.post(self, AnnNode::Expr(expr));

Diff for: compiler/rustc_builtin_macros/src/assert/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
303303
| ExprKind::Closure(_)
304304
| ExprKind::ConstBlock(_)
305305
| ExprKind::Continue(_)
306+
| ExprKind::Dummy
306307
| ExprKind::Err
307308
| ExprKind::Field(_, _)
308309
| ExprKind::ForLoop { .. }

Diff for: compiler/rustc_builtin_macros/src/concat.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ pub fn expand_concat(
6868
ast::ExprKind::Err => {
6969
has_errors = true;
7070
}
71+
ast::ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
7172
_ => {
7273
missing_literal.push(e.span);
7374
}

Diff for: compiler/rustc_builtin_macros/src/concat_bytes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ pub fn expand_concat_bytes(
176176
ast::ExprKind::Err => {
177177
has_errors = true;
178178
}
179+
ast::ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
179180
_ => {
180181
missing_literals.push(e.span);
181182
}

Diff for: compiler/rustc_expand/src/base.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,9 @@ pub fn expr_to_spanned_string<'a>(
12791279
_ => Some((cx.dcx().struct_span_err(expr.span, err_msg), false)),
12801280
},
12811281
ast::ExprKind::Err => None,
1282+
ast::ExprKind::Dummy => {
1283+
cx.dcx().span_bug(expr.span, "tried to get a string literal from `ExprKind::Dummy`")
1284+
}
12821285
_ => Some((cx.dcx().struct_span_err(expr.span, err_msg), false)),
12831286
})
12841287
}

Diff for: compiler/rustc_parse/src/parser/expr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3949,7 +3949,8 @@ impl MutVisitor for CondChecker<'_> {
39493949
| ExprKind::Become(_)
39503950
| ExprKind::IncludedBytes(_)
39513951
| ExprKind::FormatArgs(_)
3952-
| ExprKind::Err => {
3952+
| ExprKind::Err
3953+
| ExprKind::Dummy => {
39533954
// These would forbid any let expressions they contain already.
39543955
}
39553956
}

Diff for: compiler/rustc_parse/src/parser/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl<'a> Parser<'a> {
388388
// Parse `?`, `.f`, `(arg0, arg1, ...)` or `[expr]` until they've all been eaten.
389389
if let Ok(expr) = snapshot
390390
.parse_expr_dot_or_call_with(
391-
self.mk_expr_err(pat_span), // equivalent to transforming the parsed pattern into an `Expr`
391+
self.mk_expr(pat_span, ExprKind::Dummy), // equivalent to transforming the parsed pattern into an `Expr`
392392
pat_span,
393393
AttrVec::new(),
394394
)

Diff for: compiler/rustc_passes/src/hir_stats.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> {
589589
If, While, ForLoop, Loop, Match, Closure, Block, Await, TryBlock, Assign,
590590
AssignOp, Field, Index, Range, Underscore, Path, AddrOf, Break, Continue, Ret,
591591
InlineAsm, FormatArgs, OffsetOf, MacCall, Struct, Repeat, Paren, Try, Yield, Yeet,
592-
Become, IncludedBytes, Gen, Err
592+
Become, IncludedBytes, Gen, Err, Dummy
593593
]
594594
);
595595
ast_visit::walk_expr(self, e)

Diff for: src/tools/clippy/clippy_utils/src/ast_utils.rs

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
144144
(Paren(l), _) => eq_expr(l, r),
145145
(_, Paren(r)) => eq_expr(l, r),
146146
(Err, Err) => true,
147+
(Dummy, _) | (_, Dummy) => unreachable!("comparing `ExprKind::Dummy`"),
147148
(Try(l), Try(r)) | (Await(l, _), Await(r, _)) => eq_expr(l, r),
148149
(Array(l), Array(r)) => over(l, r, |l, r| eq_expr(l, r)),
149150
(Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),

Diff for: src/tools/clippy/clippy_utils/src/sugg.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ impl<'a> Sugg<'a> {
222222
| ast::ExprKind::Array(..)
223223
| ast::ExprKind::While(..)
224224
| ast::ExprKind::Await(..)
225-
| ast::ExprKind::Err => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0),
225+
| ast::ExprKind::Err
226+
| ast::ExprKind::Dummy => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0),
226227
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
227228
AssocOp::DotDot,
228229
lhs.as_ref().map_or("".into(), |lhs| {

Diff for: src/tools/rustfmt/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ pub(crate) fn format_expr(
404404
// These do not occur in the AST because macros aren't expanded.
405405
unreachable!()
406406
}
407-
ast::ExprKind::Err => None,
407+
ast::ExprKind::Err | ast::ExprKind::Dummy => None,
408408
};
409409

410410
expr_rw

Diff for: src/tools/rustfmt/src/utils.rs

+1
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
497497
| ast::ExprKind::Break(..)
498498
| ast::ExprKind::Cast(..)
499499
| ast::ExprKind::Continue(..)
500+
| ast::ExprKind::Dummy
500501
| ast::ExprKind::Err
501502
| ast::ExprKind::Field(..)
502503
| ast::ExprKind::IncludedBytes(..)

0 commit comments

Comments
 (0)