Skip to content

Commit e1e1e94

Browse files
committed
Auto merge of #124650 - oli-obk:pattern_types_syntax, r=nnethercote
Create const block DefIds in typeck instead of ast lowering this is a prerequisite for cleaning up pattern types and the range pattern HIR nodes in general. Right now they contain expressions, but they are supposed to only contain constants. In order to generate the anonymous constants lazily during typeck, we need to support generating new items with bodies during typeck in general. Transforming const blocks was the simplest change I could find to allow us to do that (everything else is much more invasive if we haven't already done it for const blocks).
2 parents dc1076a + ee43047 commit e1e1e94

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

clippy_utils/src/consts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{clip, is_direct_expn_of, sext, unsext};
66
use rustc_ast::ast::{self, LitFloatType, LitKind};
77
use rustc_data_structures::sync::Lrc;
88
use rustc_hir::def::{DefKind, Res};
9-
use rustc_hir::{BinOp, BinOpKind, Block, ConstBlock, Expr, ExprKind, HirId, Item, ItemKind, Node, QPath, UnOp};
9+
use rustc_hir::{BinOp, BinOpKind, Block, Expr, ExprKind, HirId, Item, ItemKind, Node, QPath, UnOp};
1010
use rustc_lexer::tokenize;
1111
use rustc_lint::LateContext;
1212
use rustc_middle::mir::interpret::{alloc_range, Scalar};
@@ -412,7 +412,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
412412
/// Simple constant folding: Insert an expression, get a constant or none.
413413
pub fn expr(&mut self, e: &Expr<'_>) -> Option<Constant<'tcx>> {
414414
match e.kind {
415-
ExprKind::ConstBlock(ConstBlock { body, .. }) => self.expr(self.lcx.tcx.hir().body(body).value),
415+
ExprKind::ConstBlock(e) |
416416
ExprKind::DropTemps(e) => self.expr(e),
417417
ExprKind::Path(ref qpath) => {
418418
self.fetch_path_and_apply(qpath, e.hir_id, self.typeck_results.expr_ty(e), |this, result| {
@@ -491,7 +491,7 @@ impl<'a, 'tcx> ConstEvalLateContext<'a, 'tcx> {
491491
/// leaves the local crate.
492492
pub fn expr_is_empty(&mut self, e: &Expr<'_>) -> Option<bool> {
493493
match e.kind {
494-
ExprKind::ConstBlock(ConstBlock { body, .. }) => self.expr_is_empty(self.lcx.tcx.hir().body(body).value),
494+
ExprKind::ConstBlock(e) |
495495
ExprKind::DropTemps(e) => self.expr_is_empty(e),
496496
ExprKind::Path(ref qpath) => {
497497
if !self

clippy_utils/src/hir_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl HirEqInterExpr<'_, '_, '_> {
295295
self.eq_expr(lx, rx) && self.eq_ty(lt, rt)
296296
},
297297
(&ExprKind::Closure(_l), &ExprKind::Closure(_r)) => false,
298-
(&ExprKind::ConstBlock(lb), &ExprKind::ConstBlock(rb)) => self.eq_body(lb.body, rb.body),
298+
(&ExprKind::ConstBlock(lb), &ExprKind::ConstBlock(rb)) => self.eq_expr(lb, rb),
299299
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
300300
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
301301
},
@@ -770,7 +770,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
770770
self.hash_expr(self.cx.tcx.hir().body(body).value);
771771
},
772772
ExprKind::ConstBlock(ref l_id) => {
773-
self.hash_body(l_id.body);
773+
self.hash_expr(l_id);
774774
},
775775
ExprKind::DropTemps(e) | ExprKind::Yield(e, _) => {
776776
self.hash_expr(e);

tests/ui/arithmetic_side_effects.stderr

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
error: arithmetic operation that can potentially result in unexpected side-effects
2-
--> tests/ui/arithmetic_side_effects.rs:304:5
2+
--> tests/ui/arithmetic_side_effects.rs:188:36
33
|
4-
LL | _n += 1;
5-
| ^^^^^^^
4+
LL | let _ = const { let mut n = 1; n += 1; n };
5+
| ^^^^^^
66
|
77
= note: `-D clippy::arithmetic-side-effects` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::arithmetic_side_effects)]`
99

10+
error: arithmetic operation that can potentially result in unexpected side-effects
11+
--> tests/ui/arithmetic_side_effects.rs:191:40
12+
|
13+
LL | let _ = const { let mut n = 1; n = n + 1; n };
14+
| ^^^^^
15+
16+
error: arithmetic operation that can potentially result in unexpected side-effects
17+
--> tests/ui/arithmetic_side_effects.rs:194:40
18+
|
19+
LL | let _ = const { let mut n = 1; n = 1 + n; n };
20+
| ^^^^^
21+
22+
error: arithmetic operation that can potentially result in unexpected side-effects
23+
--> tests/ui/arithmetic_side_effects.rs:200:59
24+
|
25+
LL | let _ = const { let mut n = 1; n = -1; n = -(-1); n = -n; n };
26+
| ^^
27+
28+
error: arithmetic operation that can potentially result in unexpected side-effects
29+
--> tests/ui/arithmetic_side_effects.rs:304:5
30+
|
31+
LL | _n += 1;
32+
| ^^^^^^^
33+
1034
error: arithmetic operation that can potentially result in unexpected side-effects
1135
--> tests/ui/arithmetic_side_effects.rs:305:5
1236
|
@@ -727,5 +751,5 @@ error: arithmetic operation that can potentially result in unexpected side-effec
727751
LL | one.sub_assign(1);
728752
| ^^^^^^^^^^^^^^^^^
729753

730-
error: aborting due to 121 previous errors
754+
error: aborting due to 125 previous errors
731755

0 commit comments

Comments
 (0)