Skip to content

Commit 939afab

Browse files
committed
Treat inherent methods in const impl blocks as const
1 parent 00157d4 commit 939afab

File tree

8 files changed

+67
-27
lines changed

8 files changed

+67
-27
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.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(const_trait_impl)]
2+
3+
//@check-pass
4+
5+
const trait Foo {}
6+
7+
const impl Foo for i32 {}
8+
9+
const trait Bar {}
10+
11+
const impl<T: Foo> Bar for T {}
12+
13+
const fn still_implements<T: Bar>() {}
14+
15+
const _: () = still_implements::<i32>();
16+
17+
fn main() {}

tests/ui/traits/const-traits/trait-fn-const.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ LL + fn fun();
4949
|
5050
help: ... and declare the trait to be const instead
5151
|
52-
LL | const trait NonConst {
52+
LL | const fn main() {}
5353
| +++++
5454

5555
error: aborting due to 4 previous errors

0 commit comments

Comments
 (0)