From ababa2625133610f7193dd92d2a8fa683c3ce637 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 22 Jul 2025 10:52:54 +0000 Subject: [PATCH] Collect const_conditions for inherent impls --- compiler/rustc_const_eval/src/const_eval/fn_queries.rs | 5 ++--- compiler/rustc_metadata/src/rmeta/encoder.rs | 6 +++++- compiler/rustc_middle/src/ty/mod.rs | 2 +- .../ui/traits/const-traits/const-impl-inherent-bounds.rs | 4 +++- .../const-traits/const-impl-inherent-bounds.stderr | 9 --------- 5 files changed, 11 insertions(+), 15 deletions(-) delete mode 100644 tests/ui/traits/const-traits/const-impl-inherent-bounds.stderr diff --git a/compiler/rustc_const_eval/src/const_eval/fn_queries.rs b/compiler/rustc_const_eval/src/const_eval/fn_queries.rs index 4d95b8fec5c8..3e11541aace9 100644 --- a/compiler/rustc_const_eval/src/const_eval/fn_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/fn_queries.rs @@ -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 { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 2c256ee9b702..a21968cedfe5 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -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 diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 5eb8f1713a13..c57eb3e2222f 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -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 diff --git a/tests/ui/traits/const-traits/const-impl-inherent-bounds.rs b/tests/ui/traits/const-traits/const-impl-inherent-bounds.rs index a0f5adc78f70..0781da1cc0af 100644 --- a/tests/ui/traits/const-traits/const-impl-inherent-bounds.rs +++ b/tests/ui/traits/const-traits/const-impl-inherent-bounds.rs @@ -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); @@ -11,7 +14,6 @@ const impl Trait for () {} const impl Foo { fn bar() { T::method(); - //~^ ERROR: the trait bound `T: [const] Trait` is not satisfied } } diff --git a/tests/ui/traits/const-traits/const-impl-inherent-bounds.stderr b/tests/ui/traits/const-traits/const-impl-inherent-bounds.stderr deleted file mode 100644 index eb5142993d55..000000000000 --- a/tests/ui/traits/const-traits/const-impl-inherent-bounds.stderr +++ /dev/null @@ -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`.