Skip to content

Commit 7eb0a28

Browse files
authored
Rollup merge of rust-lang#155808 - lapla-cogito:issue_155803, r=oli-obk
Always use `ConstFn` context for `const` closures fixes rust-lang#155803 Since rust-lang@e8a4611, `hir_body_const_context()` returned the parent's const context for a `const` closure. But a `const` closure can escape its enclosing body via a `fn`-pointer-typed const item or an opaque return type and be invoked at runtime, so it must be const-checked under the most restrictive `ConstFn` context like a regular `const fn`. r? oli-obk (since you authored the commit mentioned above, feel free to reroll)
2 parents 2b99e74 + 0e72a29 commit 7eb0a28

6 files changed

Lines changed: 28 additions & 7 deletions

File tree

compiler/rustc_middle/src/hir/map.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,9 @@ impl<'tcx> TyCtxt<'tcx> {
317317
BodyOwnerKind::Static(mutability) => ConstContext::Static(mutability),
318318

319319
BodyOwnerKind::Fn if self.is_constructor(def_id) => return None,
320-
// Const closures use their parent's const context
321-
BodyOwnerKind::Closure if self.is_const_fn(def_id) => {
322-
return self.hir_body_const_context(self.local_parent(local_def_id));
320+
BodyOwnerKind::Fn | BodyOwnerKind::Closure if self.is_const_fn(def_id) => {
321+
ConstContext::ConstFn
323322
}
324-
BodyOwnerKind::Fn if self.is_const_fn(def_id) => ConstContext::ConstFn,
325323
BodyOwnerKind::Fn | BodyOwnerKind::Closure | BodyOwnerKind::GlobalAsm => return None,
326324
};
327325

tests/ui/traits/const-traits/call-const-closure.next.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: the trait bound `(): const Bar` is not satisfied
1+
error[E0277]: the trait bound `(): [const] Bar` is not satisfied
22
--> $DIR/call-const-closure.rs:16:18
33
|
44
LL | (const || ().foo())();

tests/ui/traits/const-traits/call-const-closure.old.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: the trait bound `(): const Bar` is not satisfied
1+
error[E0277]: the trait bound `(): [const] Bar` is not satisfied
22
--> $DIR/call-const-closure.rs:16:18
33
|
44
LL | (const || ().foo())();

tests/ui/traits/const-traits/call-const-closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Bar for () {
1414

1515
const FOO: () = {
1616
(const || ().foo())();
17-
//~^ ERROR the trait bound `(): const Bar` is not satisfied
17+
//~^ ERROR the trait bound `(): [const] Bar` is not satisfied
1818
};
1919

2020
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ run-pass
2+
3+
// Regression test for https://github.com/rust-lang/rust/issues/155803
4+
5+
#![feature(const_closures, const_trait_impl)]
6+
7+
const F: fn() -> i32 = const || 42;
8+
9+
fn main() {
10+
assert_eq!(F(), 42);
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ build-pass
2+
//@ compile-flags: -Clink-dead-code=true
3+
4+
// Regression test for https://github.com/rust-lang/rust/issues/155803
5+
6+
#![feature(const_closures, const_trait_impl)]
7+
8+
const _: () = {
9+
assert!((const || true)());
10+
};
11+
12+
fn main() {}

0 commit comments

Comments
 (0)