Add ast::ExprKind::Dummy

This commit is contained in:
Lieselotte 2024-02-25 22:22:09 +01:00
parent 8c0b1fcd29
commit a3fce72a27
No known key found for this signature in database
GPG Key ID: 43A6A32F83A6F9B1
18 changed files with 37 additions and 24 deletions

View File

@ -1296,23 +1296,10 @@ pub fn precedence(&self) -> ExprPrecedence {
ExprKind::Yeet(..) => ExprPrecedence::Yeet,
ExprKind::FormatArgs(..) => ExprPrecedence::FormatArgs,
ExprKind::Become(..) => ExprPrecedence::Become,
ExprKind::Err => ExprPrecedence::Err,
ExprKind::Err | ExprKind::Dummy => ExprPrecedence::Err,
}
}
pub fn take(&mut self) -> Self {
mem::replace(
self,
Expr {
id: DUMMY_NODE_ID,
kind: ExprKind::Err,
span: DUMMY_SP,
attrs: AttrVec::new(),
tokens: None,
},
)
}
/// To a first-order approximation, is this a pattern?
pub fn is_approximately_pattern(&self) -> bool {
matches!(
@ -1532,6 +1519,9 @@ pub enum ExprKind {
/// Placeholder for an expression that wasn't syntactically well formed in some way.
Err,
/// Acts as a null expression. Lowering it will always emit a bug.
Dummy,
}
/// Used to differentiate between `for` loops and `for await` loops.

View File

@ -1526,7 +1526,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
}
ExprKind::Try(expr) => vis.visit_expr(expr),
ExprKind::TryBlock(body) => vis.visit_block(body),
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err => {}
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err | ExprKind::Dummy => {}
}
vis.visit_id(id);
vis.visit_span(span);
@ -1642,7 +1642,7 @@ impl DummyAstNode for Expr {
fn dummy() -> Self {
Expr {
id: DUMMY_NODE_ID,
kind: ExprKind::Err,
kind: ExprKind::Dummy,
span: Default::default(),
attrs: Default::default(),
tokens: Default::default(),

View File

@ -89,7 +89,8 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
| Paren(_)
| Try(_)
| Yeet(None)
| Err => break None,
| Err
| Dummy => break None,
}
}
}

View File

@ -1063,7 +1063,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) -> V
}
ExprKind::Try(subexpression) => try_visit!(visitor.visit_expr(subexpression)),
ExprKind::TryBlock(body) => try_visit!(visitor.visit_block(body)),
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err => {}
ExprKind::Lit(_) | ExprKind::IncludedBytes(..) | ExprKind::Err | ExprKind::Dummy => {}
}
visitor.visit_expr_post(expression)

View File

@ -14,6 +14,7 @@
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_middle::span_bug;
use rustc_session::errors::report_lit_error;
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@ -331,6 +332,11 @@ pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
ExprKind::Err => {
hir::ExprKind::Err(self.dcx().span_delayed_bug(e.span, "lowered ExprKind::Err"))
}
ExprKind::Dummy => {
span_bug!(e.span, "lowered ExprKind::Dummy")
}
ExprKind::Try(sub_expr) => self.lower_expr_try(e.span, sub_expr),
ExprKind::Paren(_) | ExprKind::ForLoop { .. } => {

View File

@ -331,7 +331,8 @@ fn lower_expr_within_pat(&mut self, expr: &Expr, allow_paths: bool) -> &'hir hir
ExprKind::Lit(..)
| ExprKind::ConstBlock(..)
| ExprKind::IncludedBytes(..)
| ExprKind::Err => {}
| ExprKind::Err
| ExprKind::Dummy => {}
ExprKind::Path(..) if allow_paths => {}
ExprKind::Unary(UnOp::Neg, inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
_ => {

View File

@ -898,6 +898,11 @@ pub(super) fn print_expr_outer_attr_style(
self.word("/*ERROR*/");
self.pclose()
}
ast::ExprKind::Dummy => {
self.popen();
self.word("/*DUMMY*/");
self.pclose();
}
}
self.ann.post(self, AnnNode::Expr(expr));

View File

@ -303,6 +303,7 @@ fn manage_cond_expr(&mut self, expr: &mut P<Expr>) {
| ExprKind::Closure(_)
| ExprKind::ConstBlock(_)
| ExprKind::Continue(_)
| ExprKind::Dummy
| ExprKind::Err
| ExprKind::Field(_, _)
| ExprKind::ForLoop { .. }

View File

@ -68,6 +68,7 @@ pub fn expand_concat(
ast::ExprKind::Err => {
has_errors = true;
}
ast::ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
_ => {
missing_literal.push(e.span);
}

View File

@ -176,6 +176,7 @@ pub fn expand_concat_bytes(
ast::ExprKind::Err => {
has_errors = true;
}
ast::ExprKind::Dummy => cx.dcx().span_bug(e.span, "concatenating `ExprKind::Dummy`"),
_ => {
missing_literals.push(e.span);
}

View File

@ -1279,6 +1279,9 @@ pub fn expr_to_spanned_string<'a>(
_ => Some((cx.dcx().struct_span_err(expr.span, err_msg), false)),
},
ast::ExprKind::Err => None,
ast::ExprKind::Dummy => {
cx.dcx().span_bug(expr.span, "tried to get a string literal from `ExprKind::Dummy`")
}
_ => Some((cx.dcx().struct_span_err(expr.span, err_msg), false)),
})
}

View File

@ -3949,7 +3949,8 @@ fn visit_expr(&mut self, e: &mut P<Expr>) {
| ExprKind::Become(_)
| ExprKind::IncludedBytes(_)
| ExprKind::FormatArgs(_)
| ExprKind::Err => {
| ExprKind::Err
| ExprKind::Dummy => {
// These would forbid any let expressions they contain already.
}
}

View File

@ -388,7 +388,7 @@ fn maybe_recover_trailing_expr(
// Parse `?`, `.f`, `(arg0, arg1, ...)` or `[expr]` until they've all been eaten.
if let Ok(expr) = snapshot
.parse_expr_dot_or_call_with(
self.mk_expr_err(pat_span), // equivalent to transforming the parsed pattern into an `Expr`
self.mk_expr(pat_span, ExprKind::Dummy), // equivalent to transforming the parsed pattern into an `Expr`
pat_span,
AttrVec::new(),
)

View File

@ -589,7 +589,7 @@ fn visit_expr(&mut self, e: &'v ast::Expr) {
If, While, ForLoop, Loop, Match, Closure, Block, Await, TryBlock, Assign,
AssignOp, Field, Index, Range, Underscore, Path, AddrOf, Break, Continue, Ret,
InlineAsm, FormatArgs, OffsetOf, MacCall, Struct, Repeat, Paren, Try, Yield, Yeet,
Become, IncludedBytes, Gen, Err
Become, IncludedBytes, Gen, Err, Dummy
]
);
ast_visit::walk_expr(self, e)

View File

@ -144,6 +144,7 @@ pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
(Paren(l), _) => eq_expr(l, r),
(_, Paren(r)) => eq_expr(l, r),
(Err, Err) => true,
(Dummy, _) | (_, Dummy) => unreachable!("comparing `ExprKind::Dummy`"),
(Try(l), Try(r)) | (Await(l, _), Await(r, _)) => eq_expr(l, r),
(Array(l), Array(r)) => over(l, r, |l, r| eq_expr(l, r)),
(Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),

View File

@ -222,7 +222,8 @@ pub fn ast(
| ast::ExprKind::Array(..)
| ast::ExprKind::While(..)
| ast::ExprKind::Await(..)
| ast::ExprKind::Err => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0),
| ast::ExprKind::Err
| ast::ExprKind::Dummy => Sugg::NonParen(snippet_with_context(cx, expr.span, ctxt, default, app).0),
ast::ExprKind::Range(ref lhs, ref rhs, RangeLimits::HalfOpen) => Sugg::BinOp(
AssocOp::DotDot,
lhs.as_ref().map_or("".into(), |lhs| {

View File

@ -404,7 +404,7 @@ fn needs_space_after_range(rhs: &ast::Expr) -> bool {
// These do not occur in the AST because macros aren't expanded.
unreachable!()
}
ast::ExprKind::Err => None,
ast::ExprKind::Err | ast::ExprKind::Dummy => None,
};
expr_rw

View File

@ -497,6 +497,7 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
| ast::ExprKind::Break(..)
| ast::ExprKind::Cast(..)
| ast::ExprKind::Continue(..)
| ast::ExprKind::Dummy
| ast::ExprKind::Err
| ast::ExprKind::Field(..)
| ast::ExprKind::IncludedBytes(..)