]> git.lizzy.rs Git - rust.git/commitdiff
add function calls
authorBastian Kauschke <bastian_kauschke@hotmail.de>
Fri, 11 Sep 2020 08:35:28 +0000 (10:35 +0200)
committerBastian Kauschke <bastian_kauschke@hotmail.de>
Fri, 18 Sep 2020 15:11:34 +0000 (17:11 +0200)
compiler/rustc_trait_selection/src/traits/const_evaluatable.rs
src/test/ui/const-generics/const_evaluatable_checked/fn_call.rs [new file with mode: 0644]

index f0e51511732767e17410fd4e8bd90ec2048becec..50cc2afb89c802b043f39b9b0176cd90d3cee66d 100644 (file)
@@ -242,6 +242,24 @@ fn build_terminator(
         match terminator.kind {
             TerminatorKind::Goto { target } => Some(Some(target)),
             TerminatorKind::Return => Some(None),
+            TerminatorKind::Call {
+                ref func,
+                ref args,
+                destination: Some((ref place, target)),
+                cleanup: _,
+                from_hir_call: true,
+                fn_span: _,
+            } => {
+                let local = place.as_local()?;
+                let func = self.operand_to_node(func)?;
+                let args = self.tcx.arena.alloc_from_iter(
+                    args.iter()
+                        .map(|arg| self.operand_to_node(arg))
+                        .collect::<Option<Vec<NodeId>>>()?,
+                );
+                self.locals[local] = self.nodes.push(Node::FunctionCall(func, args));
+                Some(Some(target))
+            }
             TerminatorKind::Assert { ref cond, expected: false, target, .. } => {
                 let p = match cond {
                     mir::Operand::Copy(p) | mir::Operand::Move(p) => p,
diff --git a/src/test/ui/const-generics/const_evaluatable_checked/fn_call.rs b/src/test/ui/const-generics/const_evaluatable_checked/fn_call.rs
new file mode 100644 (file)
index 0000000..1b9ec01
--- /dev/null
@@ -0,0 +1,30 @@
+// run-pass
+#![feature(const_generics, const_evaluatable_checked)]
+#![allow(incomplete_features)]
+
+const fn test_me<T>(a: usize, b: usize) -> usize {
+    if a < b {
+        std::mem::size_of::<T>()
+    } else {
+        std::usize::MAX
+    }
+}
+
+fn test_simple<T>() -> [u8; std::mem::size_of::<T>()]
+where
+    [u8; std::mem::size_of::<T>()]: Sized,
+{
+    [0; std::mem::size_of::<T>()]
+}
+
+fn test_with_args<T, const N: usize>() -> [u8; test_me::<T>(N, N + 1) + N]
+where
+    [u8; test_me::<T>(N, N + 1) + N]: Sized,
+{
+    [0; test_me::<T>(N, N + 1) + N]
+}
+
+fn main() {
+    assert_eq!([0; 8], test_simple::<u64>());
+    assert_eq!([0; 12], test_with_args::<u64, 4>());
+}