Skip to content

Commit 394804b

Browse files
committed
Auto merge of #86857 - fee1-dead:add-attr, r=oli-obk
Add #[default_method_body_is_const] `@rustbot` label F-const_trait_impl
2 parents 1f0db5e + 7c9e214 commit 394804b

File tree

19 files changed

+265
-34
lines changed

19 files changed

+265
-34
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
349349
),
350350

351351
gated!(cmse_nonsecure_entry, AssumedUsed, template!(Word), experimental!(cmse_nonsecure_entry)),
352+
// RFC 2632
353+
gated!(
354+
default_method_body_is_const, AssumedUsed, template!(Word), const_trait_impl,
355+
"`default_method_body_is_const` is a temporary placeholder for declaring default bodies \
356+
as `const`, which may be removed or renamed in the future."
357+
),
352358

353359
// ==========================================================================
354360
// Internal attributes: Stability, deprecation, and unsafe:

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
952952
self.get_impl_data(id).defaultness
953953
}
954954

955+
fn get_impl_constness(&self, id: DefIndex) -> hir::Constness {
956+
self.get_impl_data(id).constness
957+
}
958+
955959
fn get_coerce_unsized_info(&self, id: DefIndex) -> Option<ty::adjustment::CoerceUnsizedInfo> {
956960
self.get_impl_data(id).coerce_unsized_info
957961
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
168168
is_no_builtins => { cdata.root.no_builtins }
169169
symbol_mangling_version => { cdata.root.symbol_mangling_version }
170170
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
171+
impl_constness => { cdata.get_impl_constness(def_id.index) }
171172
reachable_non_generics => {
172173
let reachable_non_generics = tcx
173174
.exported_symbols(cdata.cnum)

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ impl EncodeContext<'a, 'tcx> {
14121412
adt_def.repr,
14131413
)
14141414
}
1415-
hir::ItemKind::Impl(hir::Impl { defaultness, .. }) => {
1415+
hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => {
14161416
let trait_ref = self.tcx.impl_trait_ref(def_id);
14171417
let polarity = self.tcx.impl_polarity(def_id);
14181418
let parent = if let Some(trait_ref) = trait_ref {
@@ -1437,8 +1437,13 @@ impl EncodeContext<'a, 'tcx> {
14371437
}
14381438
});
14391439

1440-
let data =
1441-
ImplData { polarity, defaultness, parent_impl: parent, coerce_unsized_info };
1440+
let data = ImplData {
1441+
polarity,
1442+
defaultness,
1443+
constness,
1444+
parent_impl: parent,
1445+
coerce_unsized_info,
1446+
};
14421447

14431448
EntryKind::Impl(self.lazy(data))
14441449
}

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ struct TraitData {
390390
#[derive(TyEncodable, TyDecodable)]
391391
struct ImplData {
392392
polarity: ty::ImplPolarity,
393+
constness: hir::Constness,
393394
defaultness: hir::Defaultness,
394395
parent_impl: Option<DefId>,
395396

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_index::vec::Idx;
1818
use rustc_span::def_id::StableCrateId;
1919
use rustc_span::hygiene::MacroKind;
2020
use rustc_span::source_map::Spanned;
21-
use rustc_span::symbol::{kw, Ident, Symbol};
21+
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2222
use rustc_span::Span;
2323
use rustc_target::spec::abi::Abi;
2424

@@ -465,6 +465,9 @@ impl<'hir> Map<'hir> {
465465
/// Returns the `ConstContext` of the body associated with this `LocalDefId`.
466466
///
467467
/// Panics if `LocalDefId` does not have an associated body.
468+
///
469+
/// This should only be used for determining the context of a body, a return
470+
/// value of `Some` does not always suggest that the owner of the body is `const`.
468471
pub fn body_const_context(&self, did: LocalDefId) -> Option<ConstContext> {
469472
let hir_id = self.local_def_id_to_hir_id(did);
470473
let ccx = match self.body_owner_kind(hir_id) {
@@ -473,6 +476,11 @@ impl<'hir> Map<'hir> {
473476

474477
BodyOwnerKind::Fn if self.tcx.is_constructor(did.to_def_id()) => return None,
475478
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(did.to_def_id()) => ConstContext::ConstFn,
479+
BodyOwnerKind::Fn
480+
if self.tcx.has_attr(did.to_def_id(), sym::default_method_body_is_const) =>
481+
{
482+
ConstContext::ConstFn
483+
}
476484
BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,
477485
};
478486

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,10 @@ rustc_queries! {
11441144
desc { |tcx| "looking up whether `{}` is a default impl", tcx.def_path_str(def_id) }
11451145
}
11461146

1147+
query impl_constness(def_id: DefId) -> hir::Constness {
1148+
desc { |tcx| "looking up whether `{}` is a const impl", tcx.def_path_str(def_id) }
1149+
}
1150+
11471151
query check_item_well_formed(key: LocalDefId) -> () {
11481152
desc { |tcx| "checking that `{}` is well-formed", tcx.def_path_str(key.to_def_id()) }
11491153
}

compiler/rustc_mir/src/const_eval/machine.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,15 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
235235
// sensitive check here. But we can at least rule out functions that are not const
236236
// at all.
237237
if !ecx.tcx.is_const_fn_raw(def.did) {
238-
// Some functions we support even if they are non-const -- but avoid testing
239-
// that for const fn!
240-
ecx.hook_panic_fn(instance, args)?;
241-
// We certainly do *not* want to actually call the fn
242-
// though, so be sure we return here.
243-
throw_unsup_format!("calling non-const function `{}`", instance)
238+
// allow calling functions marked with #[default_method_body_is_const].
239+
if !ecx.tcx.has_attr(def.did, sym::default_method_body_is_const) {
240+
// Some functions we support even if they are non-const -- but avoid testing
241+
// that for const fn!
242+
ecx.hook_panic_fn(instance, args)?;
243+
// We certainly do *not* want to actually call the fn
244+
// though, so be sure we return here.
245+
throw_unsup_format!("calling non-const function `{}`", instance)
246+
}
244247
}
245248
}
246249
// This is a const fn. Call it.

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,34 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
886886
}
887887

888888
if !tcx.is_const_fn_raw(callee) {
889-
self.check_op(ops::FnCallNonConst);
890-
return;
889+
let mut permitted = false;
890+
891+
let callee_trait = tcx.trait_of_item(callee);
892+
if let Some(trait_id) = callee_trait {
893+
if tcx.has_attr(caller, sym::default_method_body_is_const) {
894+
// permit call to non-const fn when caller has default_method_body_is_const..
895+
if tcx.trait_of_item(caller) == callee_trait {
896+
// ..and caller and callee are in the same trait.
897+
permitted = true;
898+
}
899+
}
900+
let mut const_impls = true;
901+
tcx.for_each_relevant_impl(trait_id, substs.type_at(0), |imp| {
902+
if const_impls {
903+
if let hir::Constness::NotConst = tcx.impl_constness(imp) {
904+
const_impls = false;
905+
}
906+
}
907+
});
908+
if const_impls {
909+
permitted = true;
910+
}
911+
}
912+
913+
if !permitted {
914+
self.check_op(ops::FnCallNonConst);
915+
return;
916+
}
891917
}
892918

893919
// If the `const fn` we are trying to call is not const-stable, ensure that we have

compiler/rustc_passes/src/check_attr.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ impl CheckAttrVisitor<'tcx> {
9898
| sym::rustc_if_this_changed
9999
| sym::rustc_then_this_would_need => self.check_rustc_dirty_clean(&attr),
100100
sym::cmse_nonsecure_entry => self.check_cmse_nonsecure_entry(attr, span, target),
101+
sym::default_method_body_is_const => {
102+
self.check_default_method_body_is_const(attr, span, target)
103+
}
101104
_ => true,
102105
};
103106
// lint-only checks
@@ -1465,6 +1468,29 @@ impl CheckAttrVisitor<'tcx> {
14651468
}
14661469
}
14671470
}
1471+
1472+
/// default_method_body_is_const should only be applied to trait methods with default bodies.
1473+
fn check_default_method_body_is_const(
1474+
&self,
1475+
attr: &Attribute,
1476+
span: &Span,
1477+
target: Target,
1478+
) -> bool {
1479+
match target {
1480+
Target::Method(MethodKind::Trait { body: true }) => true,
1481+
_ => {
1482+
self.tcx
1483+
.sess
1484+
.struct_span_err(
1485+
attr.span,
1486+
"attribute should be applied to a trait method with body",
1487+
)
1488+
.span_label(*span, "not a trait method or missing a body")
1489+
.emit();
1490+
false
1491+
}
1492+
}
1493+
}
14681494
}
14691495

14701496
impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {

0 commit comments

Comments
 (0)