From 2b2f28d0eafe2358ef95c9624d60870dd0191e5b Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Sat, 25 Apr 2026 09:47:07 +0200 Subject: [PATCH] Add regression test --- .../ui/consts/const-closure-in-trait-impl.rs | 25 +++++++++++++++++++ .../consts/const-closure-in-trait-impl.stderr | 23 +++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/ui/consts/const-closure-in-trait-impl.rs create mode 100644 tests/ui/consts/const-closure-in-trait-impl.stderr diff --git a/tests/ui/consts/const-closure-in-trait-impl.rs b/tests/ui/consts/const-closure-in-trait-impl.rs new file mode 100644 index 000000000000..51e2b2dba377 --- /dev/null +++ b/tests/ui/consts/const-closure-in-trait-impl.rs @@ -0,0 +1,25 @@ +#![feature(const_closures, const_destruct, const_trait_impl)] + +use std::marker::Destruct; +use std::num::NonZero; + +const trait T { + fn a(&mut self, f: impl [const] Fn() + [const] Destruct); + fn b(&mut self); +} + +struct S; + +impl const T for S { + fn a(&mut self, f: impl [const] Fn() + [const] Destruct) { + f() + } + + fn b(&mut self) { + self.a(const || {}); + //~^ ERROR: cannot use `const` closures outside of const contexts + //~| ERROR: [const] Fn()` is not satisfied + } +} + +fn main() {} diff --git a/tests/ui/consts/const-closure-in-trait-impl.stderr b/tests/ui/consts/const-closure-in-trait-impl.stderr new file mode 100644 index 000000000000..99b90bba092f --- /dev/null +++ b/tests/ui/consts/const-closure-in-trait-impl.stderr @@ -0,0 +1,23 @@ +error: cannot use `const` closures outside of const contexts + --> $DIR/const-closure-in-trait-impl.rs:19:16 + | +LL | self.a(const || {}); + | ^^^^^ + +error[E0277]: the trait bound `{closure@$DIR/const-closure-in-trait-impl.rs:19:16: 19:24}: [const] Fn()` is not satisfied + --> $DIR/const-closure-in-trait-impl.rs:19:16 + | +LL | self.a(const || {}); + | - ^^^^^^^^^^^ + | | + | required by a bound introduced by this call + | +note: required by a bound in `T::a` + --> $DIR/const-closure-in-trait-impl.rs:7:29 + | +LL | fn a(&mut self, f: impl [const] Fn() + [const] Destruct); + | ^^^^^^^^^^^^ required by this bound in `T::a` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0277`.