Skip to content

Commit 27e863b

Browse files
committed
functions marked with attr are not const
1 parent 89d190f commit 27e863b

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

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

@@ -457,6 +457,9 @@ impl<'hir> Map<'hir> {
457457
/// Returns the `ConstContext` of the body associated with this `LocalDefId`.
458458
///
459459
/// Panics if `LocalDefId` does not have an associated body.
460+
///
461+
/// This should only be used for determining the context of a body, a return
462+
/// value of `Some` does not always suggest that the owner of the body is `const`.
460463
pub fn body_const_context(&self, did: LocalDefId) -> Option<ConstContext> {
461464
let hir_id = self.local_def_id_to_hir_id(did);
462465
let ccx = match self.body_owner_kind(hir_id) {
@@ -465,6 +468,11 @@ impl<'hir> Map<'hir> {
465468

466469
BodyOwnerKind::Fn if self.tcx.is_constructor(did.to_def_id()) => return None,
467470
BodyOwnerKind::Fn if self.tcx.is_const_fn_raw(did.to_def_id()) => ConstContext::ConstFn,
471+
BodyOwnerKind::Fn
472+
if self.tcx.has_attr(did.to_def_id(), sym::default_method_body_is_const) =>
473+
{
474+
ConstContext::ConstFn
475+
}
468476
BodyOwnerKind::Fn | BodyOwnerKind::Closure => return None,
469477
};
470478

compiler/rustc_mir/src/const_eval/fn_queries.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ fn is_const_fn_raw(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
6060
return true;
6161
}
6262

63-
if tcx.has_attr(def_id, sym::default_method_body_is_const) {
64-
return true;
65-
}
6663
// If the function itself is not annotated with `const`, it may still be a `const fn`
6764
// if it resides in a const trait impl.
6865
is_parent_const_impl_raw(tcx, hir_id)

0 commit comments

Comments
 (0)