Skip to content

Commit 4483c2b

Browse files
committed
dont support blocks
1 parent 9b29138 commit 4483c2b

File tree

5 files changed

+16
-54
lines changed

5 files changed

+16
-54
lines changed

compiler/rustc_middle/src/mir/abstract_const.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub enum Node<'tcx> {
1717
Binop(mir::BinOp, NodeId, NodeId),
1818
UnaryOp(mir::UnOp, NodeId),
1919
FunctionCall(NodeId, &'tcx [NodeId]),
20-
Block(&'tcx [NodeId], Option<NodeId>),
2120
Cast(NodeId, Ty<'tcx>),
2221
}
2322

compiler/rustc_privacy/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ where
159159
self.visit_const(leaf)
160160
}
161161
ACNode::Cast(_, ty) => self.visit_ty(ty),
162-
ACNode::Block(_, _)
163-
| ACNode::Binop(..)
162+
ACNode::Binop(..)
164163
| ACNode::UnaryOp(..)
165164
| ACNode::FunctionCall(_, _) => ControlFlow::CONTINUE,
166165
})

compiler/rustc_trait_selection/src/traits/const_evaluatable.rs

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
102102

103103
ControlFlow::CONTINUE
104104
}
105-
Node::Block(_, _)
106-
| Node::Binop(_, _, _)
105+
Node::Binop(_, _, _)
107106
| Node::UnaryOp(_, _)
108107
| Node::FunctionCall(_, _) => ControlFlow::CONTINUE,
109108
});
@@ -288,10 +287,6 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
288287
self.nodes[func].used = true;
289288
nodes.iter().for_each(|&n| self.nodes[n].used = true);
290289
}
291-
Node::Block(stmts, opt_expr) => {
292-
stmts.iter().for_each(|&id| self.nodes[id].used = true);
293-
opt_expr.map(|e| self.nodes[e].used = true);
294-
}
295290
Node::Cast(operand, _) => {
296291
self.nodes[operand].used = true;
297292
}
@@ -378,22 +373,14 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
378373
let arg = self.recurse_build(arg)?;
379374
self.add_node(Node::UnaryOp(op, arg), node.span)
380375
},
381-
ExprKind::Block { body } => {
382-
let mut stmts = Vec::with_capacity(body.stmts.len());
383-
for &id in body.stmts.iter() {
384-
match &self.body.stmts[id].kind {
385-
thir::StmtKind::Let { .. } => return self.error(
386-
Some(node.span),
387-
"let statements are not supported in generic constants",
388-
).map(|never| never),
389-
thir::StmtKind::Expr { expr, .. } => stmts.push(self.recurse_build(*expr)?),
390-
}
391-
};
392-
let stmts = self.tcx.arena.alloc_slice(&stmts);
393-
let opt_expr = body.expr.map(|e| self.recurse_build(e)).transpose()?;
394-
self.add_node(Node::Block(stmts, opt_expr), node.span)
395-
}
396-
376+
// this is necessary so that the following compiles:
377+
//
378+
// ```
379+
// fn foo<const N: usize>(a: [(); N + 1]) {
380+
// bar::<{ N + 1 }>();
381+
// }
382+
// ```
383+
ExprKind::Block { body: thir::Block { stmts: box [], expr: Some(e), .. }} => self.recurse_build(*e)?,
397384
// ExprKind::Use happens when a `hir::ExprKind::Cast` is a
398385
// "coercion cast" i.e. using a coercion or is a no-op.
399386
// this is important so that `N as usize as usize` doesnt unify with `N as usize`
@@ -411,6 +398,7 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
411398
| ExprKind::Deref { .. }
412399
| ExprKind::Repeat { .. }
413400
| ExprKind::Array { .. }
401+
| ExprKind::Block { .. }
414402
| ExprKind::Tuple { .. }
415403
| ExprKind::Index { .. }
416404
| ExprKind::Field { .. }
@@ -521,12 +509,6 @@ where
521509
recurse(tcx, ct.subtree(func), f)?;
522510
args.iter().try_for_each(|&arg| recurse(tcx, ct.subtree(arg), f))
523511
}
524-
Node::Block(stmts, opt_expr) => {
525-
for id in stmts.iter().copied().chain(opt_expr) {
526-
recurse(tcx, ct.subtree(id), f)?;
527-
}
528-
ControlFlow::CONTINUE
529-
}
530512
Node::Cast(operand, _) => recurse(tcx, ct.subtree(operand), f),
531513
}
532514
}
@@ -615,19 +597,8 @@ pub(super) fn try_unify<'tcx>(
615597
{
616598
try_unify(tcx, a.subtree(a_operand), b.subtree(b_operand))
617599
}
618-
(Node::Block(a_stmts, a_opt_expr), Node::Block(b_stmts, b_opt_expr))
619-
if a_stmts.len() == b_stmts.len() => {
620-
a_stmts.iter().zip(b_stmts.iter()).all(|(&a_stmt, &b_stmt)| {
621-
try_unify(tcx, a.subtree(a_stmt), b.subtree(b_stmt))
622-
}) && match (a_opt_expr, b_opt_expr) {
623-
(Some(a_expr), Some(b_expr)) => try_unify(tcx, a.subtree(a_expr), b.subtree(b_expr)),
624-
(None, None) => true,
625-
_ => false,
626-
}
627-
}
628600
// use this over `_ => false` to make adding variants to `Node` less error prone
629-
(Node::Block(..), _)
630-
| (Node::Cast(..), _)
601+
(Node::Cast(..), _)
631602
| (Node::FunctionCall(..), _)
632603
| (Node::UnaryOp(..), _)
633604
| (Node::Binop(..), _)

compiler/rustc_trait_selection/src/traits/object_safety.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,7 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeFoldable<'tcx>>(
844844
self.visit_const(leaf)
845845
}
846846
Node::Cast(_, ty) => self.visit_ty(ty),
847-
Node::Block(_, _)
848-
| Node::Binop(..)
847+
Node::Binop(..)
849848
| Node::UnaryOp(..)
850849
| Node::FunctionCall(_, _) => ControlFlow::CONTINUE,
851850
})

src/test/ui/const-generics/generic_const_exprs/unused_expr.stderr

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,23 @@ error: overly complex generic constant
22
--> $DIR/unused_expr.rs:4:34
33
|
44
LL | fn add<const N: usize>() -> [u8; { N + 1; 5 }] {
5-
| ^^-----^^^^^
6-
| |
7-
| dead code
5+
| ^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
86
|
97
= help: consider moving this anonymous constant into a `const` function
108

119
error: overly complex generic constant
1210
--> $DIR/unused_expr.rs:9:34
1311
|
1412
LL | fn div<const N: usize>() -> [u8; { N / 1; 5 }] {
15-
| ^^-----^^^^^
16-
| |
17-
| dead code
13+
| ^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
1814
|
1915
= help: consider moving this anonymous constant into a `const` function
2016

2117
error: overly complex generic constant
2218
--> $DIR/unused_expr.rs:16:38
2319
|
2420
LL | fn fn_call<const N: usize>() -> [u8; { foo(N); 5 }] {
25-
| ^^------^^^^^
26-
| |
27-
| dead code
21+
| ^^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
2822
|
2923
= help: consider moving this anonymous constant into a `const` function
3024

0 commit comments

Comments
 (0)