Skip to content

Commit 2b4c98a

Browse files
committed
Treat inherent methods in const impl blocks as const
1 parent f8ebb53 commit 2b4c98a

File tree

7 files changed

+49
-26
lines changed

7 files changed

+49
-26
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ fn parent_impl_or_trait_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::C
88
let parent_id = tcx.local_parent(def_id);
99
match tcx.def_kind(parent_id) {
1010
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).constness,
11+
DefKind::Impl { of_trait: false } => {
12+
tcx.hir_node_by_def_id(parent_id).expect_item().expect_impl().constness
13+
}
1114
DefKind::Trait => {
1215
if tcx.is_const_trait(parent_id.into()) {
1316
hir::Constness::Const
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(const_trait_impl)]
2+
3+
struct Foo<T>(T);
4+
5+
const trait Trait {
6+
fn method() {}
7+
}
8+
9+
const impl Trait for () {}
10+
11+
const impl<T: [const] Trait> Foo<T> {
12+
fn bar() {
13+
T::method();
14+
//~^ ERROR: the trait bound `T: [const] Trait` is not satisfied
15+
}
16+
}
17+
18+
const _: () = Foo::<()>::bar();
19+
20+
fn main() {
21+
Foo::<()>::bar();
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0277]: the trait bound `T: [const] Trait` is not satisfied
2+
--> $DIR/const-impl-inherent-bounds.rs:13:9
3+
|
4+
LL | T::method();
5+
| ^
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(const_trait_impl)]
2+
3+
//@ check-pass
4+
5+
struct Foo;
6+
7+
const impl Foo {
8+
fn bar() {}
9+
}
10+
11+
const _: () = Foo::bar();
12+
13+
fn main() {
14+
Foo::bar();
15+
}

tests/ui/traits/const-traits/const-impl-norecover.rs

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/ui/traits/const-traits/const-impl-norecover.stderr

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)