Skip to content

Commit 4d390de

Browse files
committed
Add a helper function for checking whether a default function in a trait can be treated as const
1 parent 257f065 commit 4d390de

File tree

7 files changed

+20
-18
lines changed

7 files changed

+20
-18
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
278278
// at all.
279279
if !ecx.tcx.is_const_fn_raw(def.did) {
280280
// allow calling functions inside a trait marked with #[const_trait].
281-
if !matches!(ecx.tcx.trait_of_item(def.did), Some(trait_id) if ecx.tcx.has_attr(trait_id, sym::const_trait))
282-
{
281+
if !ecx.tcx.is_const_default_method(def.did) {
283282
// We certainly do *not* want to actually call the fn
284283
// though, so be sure we return here.
285284
throw_unsup_format!("calling non-const function `{}`", instance)

compiler/rustc_const_eval/src/transform/check_consts/check.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -872,13 +872,11 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
872872
let is_intrinsic = tcx.is_intrinsic(callee);
873873

874874
if !tcx.is_const_fn_raw(callee) {
875-
if tcx.trait_of_item(callee).is_some() {
876-
if let Some(callee_trait) = tcx.trait_of_item(callee) && tcx.has_attr(callee_trait, sym::const_trait) {
877-
// To get to here we must have already found a const impl for the
878-
// trait, but for it to still be non-const can be that the impl is
879-
// using default method bodies.
880-
nonconst_call_permission = true;
881-
}
875+
if tcx.is_const_default_method(callee) {
876+
// To get to here we must have already found a const impl for the
877+
// trait, but for it to still be non-const can be that the impl is
878+
// using default method bodies.
879+
nonconst_call_permission = true;
882880
}
883881

884882
if !nonconst_call_permission {

compiler/rustc_const_eval/src/transform/check_consts/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir as hir;
99
use rustc_hir::def_id::{DefId, LocalDefId};
1010
use rustc_middle::mir;
1111
use rustc_middle::ty::{self, TyCtxt};
12-
use rustc_span::{sym, Symbol};
12+
use rustc_span::Symbol;
1313

1414
pub use self::qualifs::Qualif;
1515

@@ -87,7 +87,7 @@ pub fn is_const_stable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
8787
// A default body in a `#[const_trait]` is not const-stable because const
8888
// trait fns currently cannot be const-stable. We shouldn't
8989
// restrict default bodies to only call const-stable functions.
90-
if let Some(trait_id) = tcx.trait_of_item(def_id) && tcx.has_attr(trait_id, sym::const_trait) {
90+
if tcx.is_const_default_method(def_id) {
9191
return false;
9292
}
9393

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,8 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
892892
|| tcx.codegen_fn_attrs(def_id).requests_inline())
893893
&& tcx.sess.opts.output_types.should_codegen();
894894
// The function has a `const` modifier or is in a `#[const_trait]`.
895-
let mut is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id());
896-
if let Some(trait_) = tcx.trait_of_item(def_id.to_def_id()) {
897-
is_const_fn = is_const_fn || tcx.has_attr(trait_, sym::const_trait);
898-
}
895+
let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id())
896+
|| tcx.is_const_default_method(def_id.to_def_id());
899897
let always_encode_mir = tcx.sess.opts.debugging_opts.always_encode_mir;
900898
(is_const_fn, needs_inline || always_encode_mir)
901899
}

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ impl<'hir> Map<'hir> {
494494
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(def_id.to_def_id()) => {
495495
ConstContext::ConstFn
496496
}
497-
BodyOwnerKind::Fn if matches!(self.tcx.trait_of_item(def_id.to_def_id()), Some(trait_id) if self.tcx.has_attr(trait_id, sym::const_trait)) => {
497+
BodyOwnerKind::Fn if self.tcx.is_const_default_method(def_id.to_def_id()) => {
498498
ConstContext::ConstFn
499499
}
500500
BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,11 @@ impl<'tcx> TyCtxt<'tcx> {
23012301
matches!(self.def_kind(def_id), DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..))
23022302
&& self.impl_constness(def_id) == hir::Constness::Const
23032303
}
2304+
2305+
#[inline]
2306+
pub fn is_const_default_method(self, def_id: DefId) -> bool {
2307+
matches!(self.trait_of_item(def_id), Some(trait_id) if self.has_attr(trait_id, sym::const_trait))
2308+
}
23042309
}
23052310

23062311
/// Yields the parent function's `LocalDefId` if `def_id` is an `impl Trait` definition.

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::ty::subst::Subst;
55
use rustc_middle::ty::{
66
self, Binder, EarlyBinder, Predicate, PredicateKind, ToPredicate, Ty, TyCtxt,
77
};
8-
use rustc_span::{sym, Span};
8+
use rustc_span::Span;
99
use rustc_trait_selection::traits;
1010

1111
fn sized_constraint_for_ty<'tcx>(
@@ -152,7 +152,9 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
152152

153153
let constness = match hir_id {
154154
Some(hir_id) => match tcx.hir().get(hir_id) {
155-
hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. }) if matches!(tcx.trait_of_item(def_id), Some(trait_id) if tcx.has_attr(trait_id, sym::const_trait)) => {
155+
hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. })
156+
if tcx.is_const_default_method(def_id) =>
157+
{
156158
hir::Constness::Const
157159
}
158160

0 commit comments

Comments
 (0)