1
Fork 0

Auto merge of #96964 - oli-obk:const_trait_mvp, r=compiler-errors

Replace `#[default_method_body_is_const]` with `#[const_trait]`

pulled out of #96077

related issues:  #67792 and #92158

cc `@fee1-dead`

This is groundwork to only allowing `impl const Trait` for traits that are marked with `#[const_trait]`. This is necessary to prevent adding a new default method from becoming a breaking change (as it could be a non-const fn).
This commit is contained in:
bors 2022-05-30 09:19:03 +00:00
commit 5c780b98d1
32 changed files with 82 additions and 260 deletions

View file

@ -494,9 +494,7 @@ impl<'hir> Map<'hir> {
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(def_id.to_def_id()) => {
ConstContext::ConstFn
}
BodyOwnerKind::Fn
if self.tcx.has_attr(def_id.to_def_id(), sym::default_method_body_is_const) =>
{
BodyOwnerKind::Fn if self.tcx.is_const_default_method(def_id.to_def_id()) => {
ConstContext::ConstFn
}
BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,

View file

@ -2303,6 +2303,11 @@ impl<'tcx> TyCtxt<'tcx> {
matches!(self.def_kind(def_id), DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(..))
&& self.impl_constness(def_id) == hir::Constness::Const
}
#[inline]
pub fn is_const_default_method(self, def_id: DefId) -> bool {
matches!(self.trait_of_item(def_id), Some(trait_id) if self.has_attr(trait_id, sym::const_trait))
}
}
/// Yields the parent function's `LocalDefId` if `def_id` is an `impl Trait` definition.