]> git.lizzy.rs Git - rust.git/commitdiff
dont support blocks
authorEllen <supbscripter@gmail.com>
Mon, 6 Sep 2021 17:20:09 +0000 (18:20 +0100)
committerEllen <supbscripter@gmail.com>
Thu, 9 Sep 2021 00:32:03 +0000 (01:32 +0100)
compiler/rustc_middle/src/mir/abstract_const.rs
compiler/rustc_privacy/src/lib.rs
compiler/rustc_trait_selection/src/traits/const_evaluatable.rs
compiler/rustc_trait_selection/src/traits/object_safety.rs
src/test/ui/const-generics/generic_const_exprs/unused_expr.stderr

index 1158a9f4f80082ce26f14d1189914418f216d430..27849e4bdb0bf96014feeafeae44726286b97b39 100644 (file)
@@ -17,7 +17,6 @@ pub enum Node<'tcx> {
     Binop(mir::BinOp, NodeId, NodeId),
     UnaryOp(mir::UnOp, NodeId),
     FunctionCall(NodeId, &'tcx [NodeId]),
-    Block(&'tcx [NodeId], Option<NodeId>),
     Cast(NodeId, Ty<'tcx>),
 }
 
index 7d0f6767900a3019ab9bc84bc48daf0363dd18c2..02ea34ea9743f93d1906421577fde829ac355e8e 100644 (file)
@@ -159,8 +159,7 @@ fn visit_abstract_const_expr(
                 self.visit_const(leaf)
             }
             ACNode::Cast(_, ty) => self.visit_ty(ty),
-            ACNode::Block(_, _)
-            | ACNode::Binop(..)
+            ACNode::Binop(..)
             | ACNode::UnaryOp(..)
             | ACNode::FunctionCall(_, _) => ControlFlow::CONTINUE,
         })
index 461ebba58d9853f7a4034465ba054065b4b53da6..77fe1f514d9019d11178d182c6e350c1061dcdf9 100644 (file)
@@ -102,8 +102,7 @@ enum FailureKind {
 
                         ControlFlow::CONTINUE
                     }
-                    Node::Block(_, _)
-                    | Node::Binop(_, _, _)
+                    Node::Binop(_, _, _)
                     | Node::UnaryOp(_, _)
                     | Node::FunctionCall(_, _) => ControlFlow::CONTINUE,
                 });
@@ -288,10 +287,6 @@ fn add_node(&mut self, node: Node<'tcx>, span: Span) -> NodeId {
                 self.nodes[func].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, _) => {
                 self.nodes[operand].used = true;
             }
@@ -378,22 +373,14 @@ fn recurse_build(&mut self, node: thir::ExprId) -> Result<NodeId, ErrorReported>
                 let arg = self.recurse_build(arg)?;
                 self.add_node(Node::UnaryOp(op, arg), node.span)
             },
-            ExprKind::Block { body } => {
-                let mut stmts = Vec::with_capacity(body.stmts.len());
-                for &id in body.stmts.iter() {
-                    match &self.body.stmts[id].kind {
-                        thir::StmtKind::Let { .. } => return self.error(
-                                Some(node.span),
-                                "let statements are not supported in generic constants",
-                            ).map(|never| never),
-                        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)
-            }
-            
+            // this is necessary so that the following compiles:
+            //
+            // ```
+            // fn foo<const N: usize>(a: [(); N + 1]) {
+            //     bar::<{ N + 1 }>();
+            // }
+            // ```
+            ExprKind::Block { body: thir::Block { stmts: box [], expr: Some(e), .. }} => self.recurse_build(*e)?,
             // ExprKind::Use happens when a `hir::ExprKind::Cast` is a 
             // "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`
@@ -411,6 +398,7 @@ fn recurse_build(&mut self, node: thir::ExprId) -> Result<NodeId, ErrorReported>
             | ExprKind::Deref { .. }
             | ExprKind::Repeat { .. }
             | ExprKind::Array { .. }
+            | ExprKind::Block { .. }
             | ExprKind::Tuple { .. }
             | ExprKind::Index { .. }
             | ExprKind::Field { .. }
@@ -521,12 +509,6 @@ fn recurse<'tcx, R>(
                 recurse(tcx, ct.subtree(func), 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),
         }
     }
@@ -615,19 +597,8 @@ pub(super) fn try_unify<'tcx>(
         {
             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
-        (Node::Block(..), _) 
-        | (Node::Cast(..), _) 
+        (Node::Cast(..), _) 
         | (Node::FunctionCall(..), _) 
         | (Node::UnaryOp(..), _) 
         | (Node::Binop(..), _) 
index 3527aede609a4336c58dae562256ae95960f9745..d9ea2591553582a1e91e09eaf3c0a2e1e61903c4 100644 (file)
@@ -844,8 +844,7 @@ fn visit_unevaluated_const(
                         self.visit_const(leaf)
                     }
                     Node::Cast(_, ty) => self.visit_ty(ty),
-                    Node::Block(_, _)
-                    | Node::Binop(..)
+                    Node::Binop(..)
                     | Node::UnaryOp(..)
                     | Node::FunctionCall(_, _) => ControlFlow::CONTINUE,
                 })
index 1687dbbcbe3f8c3fedef497409a5aec01dfb0586..3da91b19a5ed95c95dc85830599f0972d10a7de3 100644 (file)
@@ -2,9 +2,7 @@ error: overly complex generic constant
   --> $DIR/unused_expr.rs:4:34
    |
 LL | fn add<const N: usize>() -> [u8; { N + 1; 5 }] {
-   |                                  ^^-----^^^^^
-   |                                    |
-   |                                    dead code
+   |                                  ^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
    |
    = 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
    |
 LL | fn div<const N: usize>() -> [u8; { N / 1; 5 }] {
-   |                                  ^^-----^^^^^
-   |                                    |
-   |                                    dead code
+   |                                  ^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
    |
    = 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
    |
 LL | fn fn_call<const N: usize>() -> [u8; { foo(N); 5 }] {
-   |                                      ^^------^^^^^
-   |                                        |
-   |                                        dead code
+   |                                      ^^^^^^^^^^^^^ unsupported operation in generic constant, this may be supported in the future
    |
    = help: consider moving this anonymous constant into a `const` function