Treat trait fns marked with the attr as const
This commit is contained in:
parent
3660a4e972
commit
d8d4cc3b98
2 changed files with 36 additions and 1 deletions
|
@ -3,7 +3,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
|
||||||
use rustc_middle::hir::map::blocks::FnLikeNode;
|
use rustc_middle::hir::map::blocks::FnLikeNode;
|
||||||
use rustc_middle::ty::query::Providers;
|
use rustc_middle::ty::query::Providers;
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_span::symbol::Symbol;
|
use rustc_span::symbol::{sym, Symbol};
|
||||||
use rustc_target::spec::abi::Abi;
|
use rustc_target::spec::abi::Abi;
|
||||||
|
|
||||||
/// Whether the `def_id` counts as const fn in your current crate, considering all active
|
/// Whether the `def_id` counts as const fn in your current crate, considering all active
|
||||||
|
@ -60,6 +60,9 @@ fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if tcx.has_attr(def_id, sym::default_method_body_is_const) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
// If the function itself is not annotated with `const`, it may still be a `const fn`
|
// If the function itself is not annotated with `const`, it may still be a `const fn`
|
||||||
// if it resides in a const trait impl.
|
// if it resides in a const trait impl.
|
||||||
is_parent_const_impl_raw(tcx, hir_id)
|
is_parent_const_impl_raw(tcx, hir_id)
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
// TODO fix this test
|
||||||
|
|
||||||
|
#![feature(const_trait_impl)]
|
||||||
|
#![allow(incomplete_features)]
|
||||||
|
|
||||||
|
trait ConstDefaultFn: Sized {
|
||||||
|
fn b(self);
|
||||||
|
|
||||||
|
#[default_method_body_is_const]
|
||||||
|
fn a(self) {
|
||||||
|
self.b();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct NonConstImpl;
|
||||||
|
struct ConstImpl;
|
||||||
|
|
||||||
|
impl ConstDefaultFn for NonConstImpl {
|
||||||
|
fn b(self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl const ConstDefaultFn for ConstImpl {
|
||||||
|
fn b(self) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn test() {
|
||||||
|
NonConstImpl.a();
|
||||||
|
//~^ ERROR calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
||||||
|
ConstImpl.a();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
Loading…
Add table
Reference in a new issue