1
Fork 0

dont support blocks

This commit is contained in:
Ellen 2021-09-06 18:20:09 +01:00
parent 9b2913814b
commit 4483c2bdf6
5 changed files with 16 additions and 54 deletions

View file

@ -17,7 +17,6 @@ pub enum Node<'tcx> {
Binop(mir::BinOp, NodeId, NodeId), Binop(mir::BinOp, NodeId, NodeId),
UnaryOp(mir::UnOp, NodeId), UnaryOp(mir::UnOp, NodeId),
FunctionCall(NodeId, &'tcx [NodeId]), FunctionCall(NodeId, &'tcx [NodeId]),
Block(&'tcx [NodeId], Option<NodeId>),
Cast(NodeId, Ty<'tcx>), Cast(NodeId, Ty<'tcx>),
} }

View file

@ -159,8 +159,7 @@ where
self.visit_const(leaf) self.visit_const(leaf)
} }
ACNode::Cast(_, ty) => self.visit_ty(ty), ACNode::Cast(_, ty) => self.visit_ty(ty),
ACNode::Block(_, _) ACNode::Binop(..)
| ACNode::Binop(..)
| ACNode::UnaryOp(..) | ACNode::UnaryOp(..)
| ACNode::FunctionCall(_, _) => ControlFlow::CONTINUE, | ACNode::FunctionCall(_, _) => ControlFlow::CONTINUE,
}) })

View file

@ -102,8 +102,7 @@ pub fn is_const_evaluatable<'cx, 'tcx>(
ControlFlow::CONTINUE ControlFlow::CONTINUE
} }
Node::Block(_, _) Node::Binop(_, _, _)
| Node::Binop(_, _, _)
| Node::UnaryOp(_, _) | Node::UnaryOp(_, _)
| Node::FunctionCall(_, _) => ControlFlow::CONTINUE, | Node::FunctionCall(_, _) => ControlFlow::CONTINUE,
}); });
@ -288,10 +287,6 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
self.nodes[func].used = true; self.nodes[func].used = true;
nodes.iter().for_each(|&n| self.nodes[n].used = true); nodes.iter().for_each(|&n| self.nodes[n].used = true);
} }
Node::Block(stmts, opt_expr) => {
stmts.iter().for_each(|&id| self.nodes[id].used = true);
opt_expr.map(|e| self.nodes[e].used = true);
}
Node::Cast(operand, _) => { Node::Cast(operand, _) => {
self.nodes[operand].used = true; self.nodes[operand].used = true;
} }
@ -378,22 +373,14 @@ impl<'a, 'tcx> AbstractConstBuilder<'a, 'tcx> {
let arg = self.recurse_build(arg)?; let arg = self.recurse_build(arg)?;
self.add_node(Node::UnaryOp(op, arg), node.span) self.add_node(Node::UnaryOp(op, arg), node.span)
}, },
ExprKind::Block { body } => { // this is necessary so that the following compiles:
let mut stmts = Vec::with_capacity(body.stmts.len()); //
for &id in body.stmts.iter() { // ```
match &self.body.stmts[id].kind { // fn foo<const N: usize>(a: [(); N + 1]) {
thir::StmtKind::Let { .. } => return self.error( // bar::<{ N + 1 }>();
Some(node.span), // }
"let statements are not supported in generic constants", // ```
).map(|never| never), ExprKind::Block { body: thir::Block { stmts: box [], expr: Some(e), .. }} => self.recurse_build(*e)?,
thir::StmtKind::Expr { expr, .. } => stmts.push(self.recurse_build(*expr)?),
}
};
let stmts = self.tcx.arena.alloc_slice(&stmts);
let opt_expr = body.expr.map(|e| self.recurse_build(e)).transpose()?;
self.add_node(Node::Block(stmts, opt_expr), node.span)
}
// ExprKind::Use happens when a `hir::ExprKind::Cast` is a // ExprKind::Use happens when a `hir::ExprKind::Cast` is a
// "coercion cast" i.e. using a coercion or is a no-op. // "coercion cast" i.e. using a coercion or is a no-op.
// this is important so that `N as usize as usize` doesnt unify with `N as usize` // 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> {
| ExprKind::Deref { .. } | ExprKind::Deref { .. }
| ExprKind::Repeat { .. } | ExprKind::Repeat { .. }
| ExprKind::Array { .. } | ExprKind::Array { .. }
| ExprKind::Block { .. }
| ExprKind::Tuple { .. } | ExprKind::Tuple { .. }
| ExprKind::Index { .. } | ExprKind::Index { .. }
| ExprKind::Field { .. } | ExprKind::Field { .. }
@ -521,12 +509,6 @@ where
recurse(tcx, ct.subtree(func), f)?; recurse(tcx, ct.subtree(func), f)?;
args.iter().try_for_each(|&arg| recurse(tcx, ct.subtree(arg), f)) args.iter().try_for_each(|&arg| recurse(tcx, ct.subtree(arg), f))
} }
Node::Block(stmts, opt_expr) => {
for id in stmts.iter().copied().chain(opt_expr) {
recurse(tcx, ct.subtree(id), f)?;
}
ControlFlow::CONTINUE
}
Node::Cast(operand, _) => recurse(tcx, ct.subtree(operand), f), Node::Cast(operand, _) => recurse(tcx, ct.subtree(operand), f),
} }
} }
@ -615,19 +597,8 @@ pub(super) fn try_unify<'tcx>(
{ {
try_unify(tcx, a.subtree(a_operand), b.subtree(b_operand)) try_unify(tcx, a.subtree(a_operand), b.subtree(b_operand))
} }
(Node::Block(a_stmts, a_opt_expr), Node::Block(b_stmts, b_opt_expr))
if a_stmts.len() == b_stmts.len() => {
a_stmts.iter().zip(b_stmts.iter()).all(|(&a_stmt, &b_stmt)| {
try_unify(tcx, a.subtree(a_stmt), b.subtree(b_stmt))
}) && match (a_opt_expr, b_opt_expr) {
(Some(a_expr), Some(b_expr)) => try_unify(tcx, a.subtree(a_expr), b.subtree(b_expr)),
(None, None) => true,
_ => false,
}
}
// use this over `_ => false` to make adding variants to `Node` less error prone // use this over `_ => false` to make adding variants to `Node` less error prone
(Node::Block(..), _) (Node::Cast(..), _)
| (Node::Cast(..), _)
| (Node::FunctionCall(..), _) | (Node::FunctionCall(..), _)
| (Node::UnaryOp(..), _) | (Node::UnaryOp(..), _)
| (Node::Binop(..), _) | (Node::Binop(..), _)

View file

@ -844,8 +844,7 @@ fn contains_illegal_self_type_reference<'tcx, T: TypeFoldable<'tcx>>(
self.visit_const(leaf) self.visit_const(leaf)
} }
Node::Cast(_, ty) => self.visit_ty(ty), Node::Cast(_, ty) => self.visit_ty(ty),
Node::Block(_, _) Node::Binop(..)
| Node::Binop(..)
| Node::UnaryOp(..) | Node::UnaryOp(..)
| Node::FunctionCall(_, _) => ControlFlow::CONTINUE, | Node::FunctionCall(_, _) => ControlFlow::CONTINUE,
}) })

View file

@ -2,9 +2,7 @@ error: overly complex generic constant
--> $DIR/unused_expr.rs:4:34 --> $DIR/unused_expr.rs:4:34
| |
LL | fn add<const N: usize>() -> [u8; { N + 1; 5 }] { LL | fn add<const N: usize>() -> [u8; { N + 1; 5 }] {
| ^^-----^^^^^ | ^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
| |
| dead code
| |
= help: consider moving this anonymous constant into a `const` function = help: consider moving this anonymous constant into a `const` function
@ -12,9 +10,7 @@ error: overly complex generic constant
--> $DIR/unused_expr.rs:9:34 --> $DIR/unused_expr.rs:9:34
| |
LL | fn div<const N: usize>() -> [u8; { N / 1; 5 }] { LL | fn div<const N: usize>() -> [u8; { N / 1; 5 }] {
| ^^-----^^^^^ | ^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
| |
| dead code
| |
= help: consider moving this anonymous constant into a `const` function = help: consider moving this anonymous constant into a `const` function
@ -22,9 +18,7 @@ error: overly complex generic constant
--> $DIR/unused_expr.rs:16:38 --> $DIR/unused_expr.rs:16:38
| |
LL | fn fn_call<const N: usize>() -> [u8; { foo(N); 5 }] { LL | fn fn_call<const N: usize>() -> [u8; { foo(N); 5 }] {
| ^^------^^^^^ | ^^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
| |
| dead code
| |
= help: consider moving this anonymous constant into a `const` function = help: consider moving this anonymous constant into a `const` function