1
Fork 0

Add a helper function for checking whether a default function in a trait can be treated as const

This commit is contained in:
Oli Scherer 2022-05-11 16:02:20 +00:00
parent 257f06587c
commit 4d390de4a3
7 changed files with 20 additions and 18 deletions

View file

@ -278,8 +278,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
// at all.
if !ecx.tcx.is_const_fn_raw(def.did) {
// allow calling functions inside a trait marked with #[const_trait].
if !matches!(ecx.tcx.trait_of_item(def.did), Some(trait_id) if ecx.tcx.has_attr(trait_id, sym::const_trait))
{
if !ecx.tcx.is_const_default_method(def.did) {
// We certainly do *not* want to actually call the fn
// though, so be sure we return here.
throw_unsup_format!("calling non-const function `{}`", instance)

View file

@ -872,13 +872,11 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
let is_intrinsic = tcx.is_intrinsic(callee);
if !tcx.is_const_fn_raw(callee) {
if tcx.trait_of_item(callee).is_some() {
if let Some(callee_trait) = tcx.trait_of_item(callee) && tcx.has_attr(callee_trait, sym::const_trait) {
// To get to here we must have already found a const impl for the
// trait, but for it to still be non-const can be that the impl is
// using default method bodies.
nonconst_call_permission = true;
}
if tcx.is_const_default_method(callee) {
// To get to here we must have already found a const impl for the
// trait, but for it to still be non-const can be that the impl is
// using default method bodies.
nonconst_call_permission = true;
}
if !nonconst_call_permission {

View file

@ -9,7 +9,7 @@ use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::mir;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::{sym, Symbol};
use rustc_span::Symbol;
pub use self::qualifs::Qualif;
@ -87,7 +87,7 @@ pub fn is_const_stable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
// A default body in a `#[const_trait]` is not const-stable because const
// trait fns currently cannot be const-stable. We shouldn't
// restrict default bodies to only call const-stable functions.
if let Some(trait_id) = tcx.trait_of_item(def_id) && tcx.has_attr(trait_id, sym::const_trait) {
if tcx.is_const_default_method(def_id) {
return false;
}