From ee85704c0434e8453ee7d1113012331387cb6ee0 Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Mon, 16 Aug 2021 11:29:05 +0000 Subject: [PATCH 1/2] Skip assert ICE with default_method_body_is_const functions marked with #[default_method_body_is_const] would ICE when being const checked due to it not being a const function: `tcx.is_const_fn_raw(did)` returns false. We should skip this assert when it is marked with that attribute. --- compiler/rustc_mir/src/transform/check_consts/mod.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_mir/src/transform/check_consts/mod.rs b/compiler/rustc_mir/src/transform/check_consts/mod.rs index 7e22ed22db4fe..a5cb0f4e14b17 100644 --- a/compiler/rustc_mir/src/transform/check_consts/mod.rs +++ b/compiler/rustc_mir/src/transform/check_consts/mod.rs @@ -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::Symbol; +use rustc_span::{sym, Symbol}; pub use self::qualifs::Qualif; @@ -104,6 +104,13 @@ pub fn rustc_allow_const_fn_unstable( pub fn is_const_stable_const_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool { use attr::{ConstStability, Stability, StabilityLevel}; + // A default body marked const 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 tcx.has_attr(def_id, sym::default_method_body_is_const) { + return false; + } + // Const-stability is only relevant for `const fn`. assert!(tcx.is_const_fn_raw(def_id)); From 85abdf07578f14b9c07b3aed9f6b23b1bbf23f8f Mon Sep 17 00:00:00 2001 From: Deadbeef Date: Mon, 16 Aug 2021 14:46:08 +0000 Subject: [PATCH 2/2] Add ui test --- ...ult-method-body-is-const-with-staged-api.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-with-staged-api.rs diff --git a/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-with-staged-api.rs b/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-with-staged-api.rs new file mode 100644 index 0000000000000..d5b1a9073acea --- /dev/null +++ b/src/test/ui/rfc-2632-const-trait-impl/default-method-body-is-const-with-staged-api.rs @@ -0,0 +1,18 @@ +// check-pass + +// This was an ICE, because the compiler ensures the +// function to be const when performing const checking, +// but functions marked with the attribute are not const +// *and* subject to const checking. + +#![feature(staged_api)] +#![feature(const_trait_impl)] +#![feature(const_fn_trait_bound)] +#![stable(since = "1", feature = "foo")] + +trait Tr { + #[default_method_body_is_const] + fn a() {} +} + +fn main() {}