Collect const_conditions for inherent impls

This commit is contained in:
Oli Scherer 2025-07-22 10:52:54 +00:00 committed by Oli Scherer
parent 939afab37e
commit ababa26251
5 changed files with 11 additions and 15 deletions

View file

@ -8,9 +8,7 @@ fn parent_impl_or_trait_constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::C
let parent_id = tcx.local_parent(def_id);
match tcx.def_kind(parent_id) {
DefKind::Impl { of_trait: true } => tcx.impl_trait_header(parent_id).constness,
DefKind::Impl { of_trait: false } => {
tcx.hir_node_by_def_id(parent_id).expect_item().expect_impl().constness
}
DefKind::Impl { of_trait: false } => tcx.constness(parent_id),
DefKind::Trait => {
if tcx.is_const_trait(parent_id.into()) {
hir::Constness::Const
@ -33,6 +31,7 @@ fn constness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Constness {
hir::Constness::NotConst
}
hir::Node::Expr(e) if let hir::ExprKind::Closure(c) = e.kind => c.constness,
hir::Node::Item(i) if let hir::ItemKind::Impl(impl_) = i.kind => impl_.constness,
_ => {
if let Some(fn_kind) = node.fn_kind() {
if fn_kind.constness() == hir::Constness::Const {

View file

@ -1315,7 +1315,11 @@ fn should_encode_fn_sig(def_kind: DefKind) -> bool {
fn should_encode_constness(def_kind: DefKind) -> bool {
match def_kind {
DefKind::Fn | DefKind::AssocFn | DefKind::Closure | DefKind::Ctor(_, CtorKind::Fn) => true,
DefKind::Fn
| DefKind::AssocFn
| DefKind::Closure
| DefKind::Ctor(_, CtorKind::Fn)
| DefKind::Impl { of_trait: false } => true,
DefKind::Struct
| DefKind::Union

View file

@ -2153,6 +2153,7 @@ impl<'tcx> TyCtxt<'tcx> {
header.constness == hir::Constness::Const
&& self.is_const_trait(header.trait_ref.skip_binder().def_id)
}
DefKind::Impl { of_trait: false } => self.constness(def_id) == hir::Constness::Const,
DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn) => {
self.constness(def_id) == hir::Constness::Const
}
@ -2191,7 +2192,6 @@ impl<'tcx> TyCtxt<'tcx> {
false
}
DefKind::Ctor(_, CtorKind::Const)
| DefKind::Impl { of_trait: false }
| DefKind::Mod
| DefKind::Struct
| DefKind::Union

View file

@ -1,4 +1,7 @@
#![feature(const_trait_impl)]
//! Test that we can actually use `[const] Trait` bounds written on the impl block
//@ check-pass
struct Foo<T>(T);
@ -11,7 +14,6 @@ const impl Trait for () {}
const impl<T: [const] Trait> Foo<T> {
fn bar() {
T::method();
//~^ ERROR: the trait bound `T: [const] Trait` is not satisfied
}
}

View file

@ -1,9 +0,0 @@
error[E0277]: the trait bound `T: [const] Trait` is not satisfied
--> $DIR/const-impl-inherent-bounds.rs:13:9
|
LL | T::method();
| ^
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.