diff options
| author | Deadbeef <ent3rm4n@gmail.com> | 2021-07-06 13:05:24 +0800 |
|---|---|---|
| committer | Deadbeef <ent3rm4n@gmail.com> | 2021-07-10 20:54:48 +0800 |
| commit | d8d4cc3b98b78fae879b9540f237ad31268d6430 (patch) | |
| tree | 577458c82caa58fd62ae092f9538df4fa65a20f3 | |
| parent | 3660a4e97259a23b9a24c30aa097932ae6f0eb18 (diff) | |
| download | rust-d8d4cc3b98b78fae879b9540f237ad31268d6430.tar.gz rust-d8d4cc3b98b78fae879b9540f237ad31268d6430.zip | |
Treat trait fns marked with the attr as const
| -rw-r--r-- | compiler/rustc_mir/src/const_eval/fn_queries.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.rs | 32 |
2 files changed, 36 insertions, 1 deletions
diff --git a/compiler/rustc_mir/src/const_eval/fn_queries.rs b/compiler/rustc_mir/src/const_eval/fn_queries.rs index 40419a4d201..fc7877b0a0b 100644 --- a/compiler/rustc_mir/src/const_eval/fn_queries.rs +++ b/compiler/rustc_mir/src/const_eval/fn_queries.rs @@ -3,7 +3,7 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_middle::hir::map::blocks::FnLikeNode; use rustc_middle::ty::query::Providers; use rustc_middle::ty::TyCtxt; -use rustc_span::symbol::Symbol; +use rustc_span::symbol::{sym, Symbol}; use rustc_target::spec::abi::Abi; /// 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; } + 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 it resides in a const trait impl. is_parent_const_impl_raw(tcx, hir_id) diff --git a/src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.rs b/src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.rs new file mode 100644 index 00000000000..bc2fcf81c63 --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/const-default-method-bodies.rs @@ -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() {} |
