Skip to content

Commit 53ac4f8

Browse files
committed
Auto merge of rust-lang#109588 - Nilstrieb:dropless-expr, r=compiler-errors
Alloc `hir::Lit` in an arena to remove the destructor from `Expr` This allows allocating `Expr`s into a dropless arena, which is useful for using length prefixed thing slices in HIR, since these can only be allocated in the dropless arena and not in a typed arena.
2 parents 5546cb6 + 34ed5c3 commit 53ac4f8

File tree

9 files changed

+34
-38
lines changed

9 files changed

+34
-38
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+25-30
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
121121
LitKind::Err
122122
}
123123
};
124-
hir::ExprKind::Lit(respan(self.lower_span(e.span), lit_kind))
124+
let lit = self.arena.alloc(respan(self.lower_span(e.span), lit_kind));
125+
hir::ExprKind::Lit(lit)
126+
}
127+
ExprKind::IncludedBytes(bytes) => {
128+
let lit = self.arena.alloc(respan(
129+
self.lower_span(e.span),
130+
LitKind::ByteStr(bytes.clone(), StrStyle::Cooked),
131+
));
132+
hir::ExprKind::Lit(lit)
125133
}
126-
ExprKind::IncludedBytes(bytes) => hir::ExprKind::Lit(respan(
127-
self.lower_span(e.span),
128-
LitKind::ByteStr(bytes.clone(), StrStyle::Cooked),
129-
)),
130134
ExprKind::Cast(expr, ty) => {
131135
let expr = self.lower_expr(expr);
132136
let ty =
@@ -1746,40 +1750,31 @@ impl<'hir> LoweringContext<'_, 'hir> {
17461750
}
17471751

17481752
pub(super) fn expr_usize(&mut self, sp: Span, value: usize) -> hir::Expr<'hir> {
1749-
self.expr(
1750-
sp,
1751-
hir::ExprKind::Lit(hir::Lit {
1752-
span: sp,
1753-
node: ast::LitKind::Int(
1754-
value as u128,
1755-
ast::LitIntType::Unsigned(ast::UintTy::Usize),
1756-
),
1757-
}),
1758-
)
1753+
let lit = self.arena.alloc(hir::Lit {
1754+
span: sp,
1755+
node: ast::LitKind::Int(value as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
1756+
});
1757+
self.expr(sp, hir::ExprKind::Lit(lit))
17591758
}
17601759

17611760
pub(super) fn expr_u32(&mut self, sp: Span, value: u32) -> hir::Expr<'hir> {
1762-
self.expr(
1763-
sp,
1764-
hir::ExprKind::Lit(hir::Lit {
1765-
span: sp,
1766-
node: ast::LitKind::Int(value.into(), ast::LitIntType::Unsigned(ast::UintTy::U32)),
1767-
}),
1768-
)
1761+
let lit = self.arena.alloc(hir::Lit {
1762+
span: sp,
1763+
node: ast::LitKind::Int(value.into(), ast::LitIntType::Unsigned(ast::UintTy::U32)),
1764+
});
1765+
self.expr(sp, hir::ExprKind::Lit(lit))
17691766
}
17701767

17711768
pub(super) fn expr_char(&mut self, sp: Span, value: char) -> hir::Expr<'hir> {
1772-
self.expr(sp, hir::ExprKind::Lit(hir::Lit { span: sp, node: ast::LitKind::Char(value) }))
1769+
let lit = self.arena.alloc(hir::Lit { span: sp, node: ast::LitKind::Char(value) });
1770+
self.expr(sp, hir::ExprKind::Lit(lit))
17731771
}
17741772

17751773
pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
1776-
self.expr(
1777-
sp,
1778-
hir::ExprKind::Lit(hir::Lit {
1779-
span: sp,
1780-
node: ast::LitKind::Str(value, ast::StrStyle::Cooked),
1781-
}),
1782-
)
1774+
let lit = self
1775+
.arena
1776+
.alloc(hir::Lit { span: sp, node: ast::LitKind::Str(value, ast::StrStyle::Cooked) });
1777+
self.expr(sp, hir::ExprKind::Lit(lit))
17831778
}
17841779

17851780
pub(super) fn expr_call_mut(

compiler/rustc_hir/src/arena.rs

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ macro_rules! arena_types {
5151
[] type_binding: rustc_hir::TypeBinding<'tcx>,
5252
[] variant: rustc_hir::Variant<'tcx>,
5353
[] where_predicate: rustc_hir::WherePredicate<'tcx>,
54+
[] lit: rustc_hir::Lit,
5455
]);
5556
)
5657
}

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1957,7 +1957,7 @@ pub enum ExprKind<'hir> {
19571957
/// A unary operation (e.g., `!x`, `*x`).
19581958
Unary(UnOp, &'hir Expr<'hir>),
19591959
/// A literal (e.g., `1`, `"foo"`).
1960-
Lit(Lit),
1960+
Lit(&'hir Lit),
19611961
/// A cast (e.g., `foo as f64`).
19621962
Cast(&'hir Expr<'hir>, &'hir Ty<'hir>),
19631963
/// A type reference (e.g., `Foo`).

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12521252
node: rustc_ast::LitKind::Int(lit, rustc_ast::LitIntType::Unsuffixed),
12531253
span,
12541254
}) => {
1255-
let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) else { return false; };
1255+
let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(*span) else { return false; };
12561256
if !(snippet.starts_with("0x") || snippet.starts_with("0X")) {
12571257
return false;
12581258
}
@@ -1311,7 +1311,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13111311

13121312
// We have satisfied all requirements to provide a suggestion. Emit it.
13131313
err.span_suggestion(
1314-
span,
1314+
*span,
13151315
format!("if you meant to create a null pointer, use `{null_path_str}()`"),
13161316
null_path_str + "()",
13171317
Applicability::MachineApplicable,

src/tools/clippy/clippy_lints/src/bool_assert_comparison.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn extract_bool_lit(e: &Expr<'_>) -> Option<bool> {
4141
}) = e.kind
4242
&& !e.span.from_expansion()
4343
{
44-
Some(b)
44+
Some(*b)
4545
} else {
4646
None
4747
}

src/tools/clippy/clippy_lints/src/manual_strip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn eq_pattern_length<'tcx>(cx: &LateContext<'tcx>, pattern: &Expr<'_>, expr: &'t
159159
..
160160
}) = expr.kind
161161
{
162-
constant_length(cx, pattern).map_or(false, |length| length == n)
162+
constant_length(cx, pattern).map_or(false, |length| length == *n)
163163
} else {
164164
len_arg(cx, expr).map_or(false, |arg| eq_expr_value(cx, pattern, arg))
165165
}

src/tools/clippy/clippy_lints/src/matches/match_like_matches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ fn find_bool_lit(ex: &ExprKind<'_>) -> Option<bool> {
162162
node: LitKind::Bool(b), ..
163163
}) = exp.kind
164164
{
165-
Some(b)
165+
Some(*b)
166166
} else {
167167
None
168168
}

src/tools/clippy/clippy_lints/src/methods/open_options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn get_open_options(cx: &LateContext<'_>, argument: &Expr<'_>, options: &mut Vec
4848
..
4949
} = *span
5050
{
51-
if lit { Argument::True } else { Argument::False }
51+
if *lit { Argument::True } else { Argument::False }
5252
} else {
5353
// The function is called with a literal which is not a boolean literal.
5454
// This is theoretically possible, but not very likely.

src/tools/clippy/clippy_lints/src/utils/author.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
430430
kind!("Unary(UnOp::{op:?}, {inner})");
431431
self.expr(inner);
432432
},
433-
ExprKind::Lit(ref lit) => {
433+
ExprKind::Lit(lit) => {
434434
bind!(self, lit);
435435
kind!("Lit(ref {lit})");
436436
self.lit(lit);

0 commit comments

Comments
 (0)