From af3c6f9a0395315720ee7c9a16fe9f1d5bdddc62 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 13 Oct 2022 03:10:00 +0000 Subject: [PATCH] Delay intrinsic call until after we've determined the callee is a function --- .../src/transform/check_consts/check.rs | 4 +-- .../rfc-2632-const-trait-impl/issue-102985.rs | 11 ++++++++ .../issue-102985.stderr | 26 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/rfc-2632-const-trait-impl/issue-102985.rs create mode 100644 src/test/ui/rfc-2632-const-trait-impl/issue-102985.stderr diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs index 80ca412b32a..22a61774e8c 100644 --- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs +++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs @@ -909,8 +909,6 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { return; } - let is_intrinsic = tcx.is_intrinsic(callee); - if !tcx.is_const_fn_raw(callee) { if !tcx.is_const_default_method(callee) { // To get to here we must have already found a const impl for the @@ -970,7 +968,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> { // We do not use `const` modifiers for intrinsic "functions", as intrinsics are // `extern` functions, and these have no way to get marked `const`. So instead we // use `rustc_const_(un)stable` attributes to mean that the intrinsic is `const` - if self.ccx.is_const_stable_const_fn() || is_intrinsic { + if self.ccx.is_const_stable_const_fn() || tcx.is_intrinsic(callee) { self.check_op(ops::FnCallUnstable(callee, None)); return; } diff --git a/src/test/ui/rfc-2632-const-trait-impl/issue-102985.rs b/src/test/ui/rfc-2632-const-trait-impl/issue-102985.rs new file mode 100644 index 00000000000..e0df7200384 --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/issue-102985.rs @@ -0,0 +1,11 @@ +#![feature(const_trait_impl)] + +struct Bug { + inner: [(); match || 1 { + n => n(), + //~^ ERROR the trait bound + //~| ERROR cannot call non-const fn `Bug::inner::{constant#0}::{closure#0}` in constants + }], +} + +fn main() {} diff --git a/src/test/ui/rfc-2632-const-trait-impl/issue-102985.stderr b/src/test/ui/rfc-2632-const-trait-impl/issue-102985.stderr new file mode 100644 index 00000000000..14d87e7cdc6 --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/issue-102985.stderr @@ -0,0 +1,26 @@ +error[E0277]: the trait bound `[closure@$DIR/issue-102985.rs:4:23: 4:25]: ~const Fn<()>` is not satisfied + --> $DIR/issue-102985.rs:5:14 + | +LL | n => n(), + | ^^^ expected an `Fn<()>` closure, found `[closure@$DIR/issue-102985.rs:4:23: 4:25]` + | + = help: the trait `~const Fn<()>` is not implemented for closure `[closure@$DIR/issue-102985.rs:4:23: 4:25]` +note: the trait `Fn<()>` is implemented for `[closure@$DIR/issue-102985.rs:4:23: 4:25]`, but that implementation is not `const` + --> $DIR/issue-102985.rs:5:14 + | +LL | n => n(), + | ^^^ + = note: wrap the `[closure@$DIR/issue-102985.rs:4:23: 4:25]` in a closure with no arguments: `|| { /* code */ }` + +error[E0015]: cannot call non-const fn `Bug::inner::{constant#0}::{closure#0}` in constants + --> $DIR/issue-102985.rs:5:14 + | +LL | n => n(), + | ^^^ + | + = note: calls in constants are limited to constant functions, tuple structs and tuple variants + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0015, E0277. +For more information about an error, try `rustc --explain E0015`.