Skip to content

Commit 3596a25

Browse files
committed
Collect const_conditions for inherent impls
1 parent 2b4c98a commit 3596a25

File tree

5 files changed

+11
-15
lines changed

5 files changed

+11
-15
lines changed

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ 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-
}
11+
DefKind::Impl { of_trait: false } => tcx.constness(parent_id),
1412
DefKind::Trait => {
1513
if tcx.is_const_trait(parent_id.into()) {
1614
hir::Constness::Const
@@ -33,6 +31,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
3331
hir::Constness::NotConst
3432
}
3533
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
34+
hir::Node::Item(i) if let hir::ItemKind::Impl(impl_) = i.kind => impl_.constness,
3635
_ => {
3736
if let Some(fn_kind) = node.fn_kind() {
3837
if fn_kind.constness() == hir::Constness::Const {

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,7 +1315,11 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
13151315

13161316
fn should_encode_constness(def_kind: DefKind) -> bool {
13171317
match def_kind {
1318-
DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Ctor(_, CtorKind::Fn) => true,
1318+
DefKind::Fn
1319+
| DefKind::AssocFn
1320+
| DefKind::Closure
1321+
| DefKind::Ctor(_, CtorKind::Fn)
1322+
| DefKind::Impl { of_trait: false } => true,
13191323

13201324
DefKind::Struct
13211325
| DefKind::Union

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,7 @@ impl<'tcx> TyCtxt<'tcx> {
21512151
header.constness == hir::Constness::Const
21522152
&& self.is_const_trait(header.trait_ref.skip_binder().def_id)
21532153
}
2154+
DefKind::Impl { of_trait: false } => self.constness(def_id) == hir::Constness::Const,
21542155
DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) => {
21552156
self.constness(def_id) == hir::Constness::Const
21562157
}
@@ -2189,7 +2190,6 @@ impl<'tcx> TyCtxt<'tcx> {
21892190
false
21902191
}
21912192
DefKind::Ctor(_, CtorKind::Const)
2192-
| DefKind::Impl { of_trait: false }
21932193
| DefKind::Mod
21942194
| DefKind::Struct
21952195
| DefKind::Union

tests/ui/traits/const-traits/const-impl-inherent-bounds.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#![feature(const_trait_impl)]
2+
//! Test that we can actually use `[const] Trait` bounds written on the impl block
3+
4+
//@ check-pass
25

36
struct Foo<T>(T);
47

@@ -11,7 +14,6 @@ const impl Trait for () {}
1114
const impl<T: [const] Trait> Foo<T> {
1215
fn bar() {
1316
T::method();
14-
//~^ ERROR: the trait bound `T: [const] Trait` is not satisfied
1517
}
1618
}
1719

tests/ui/traits/const-traits/const-impl-inherent-bounds.stderr

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

0 commit comments

Comments
 (0)