Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion compiler/rustc_middle/src/hir/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,17 @@ impl<'tcx> TyCtxt<'tcx> {
BodyOwnerKind::Fn if self.is_constructor(def_id) => return None,
// Const closures use their parent's const context
BodyOwnerKind::Closure if self.is_const_fn(def_id) => {
return self.hir_body_const_context(self.local_parent(local_def_id));
return Some(
self.hir_body_const_context(self.local_parent(local_def_id)).unwrap_or_else(
|| {
assert!(
self.dcx().has_errors().is_some(),
"`const` closure with no enclosing const context",
);
ConstContext::ConstFn
},
),
);
}
BodyOwnerKind::Fn if self.is_const_fn(def_id) => ConstContext::ConstFn,
BodyOwnerKind::Fn | BodyOwnerKind::Closure | BodyOwnerKind::GlobalAsm => return None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(const_trait_impl)]
#![feature(const_closures)]

// Regression test for https://github.com/rust-lang/rust/issues/153891

const trait Foo {
fn test() -> impl [const] Fn();
}

impl<T: Foo> Foo for &mut T {
const fn test() -> impl [const] Fn() {
//~^ ERROR functions in trait impls cannot be declared const
const move || {}
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0379]: functions in trait impls cannot be declared const
--> $DIR/const-closure-in-non-const-trait-impl-method.rs:11:5
|
LL | const fn test() -> impl [const] Fn() {
| ^^^^^ functions in trait impls cannot be const
|
help: remove the `const` ...
|
LL - const fn test() -> impl [const] Fn() {
LL + fn test() -> impl [const] Fn() {
|
help: ... and declare the impl to be const instead
|
LL | impl<T: Foo> const Foo for &mut T {
| +++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0379`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(const_closures)]

// Regression test for https://github.com/rust-lang/rust/issues/153891

trait Tr {
const fn test() {
//~^ ERROR functions in traits cannot be declared const
(const || {})()
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0379]: functions in traits cannot be declared const
--> $DIR/const-closure-in-non-const-trait-method.rs:6:5
|
LL | const fn test() {
| ^^^^^-
| |
| functions in traits cannot be const
| help: remove the `const`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0379`.
Loading