Skip to content

Commit 4bb65e1

Browse files
committed
Fix default_method_body_is_const when used across crates
1 parent df89fd2 commit 4bb65e1

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,9 @@ fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) {
869869
let needs_inline = (generics.requires_monomorphization(tcx)
870870
|| tcx.codegen_fn_attrs(def_id).requests_inline())
871871
&& tcx.sess.opts.output_types.should_codegen();
872-
// Only check the presence of the `const` modifier.
873-
let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id());
872+
// The function has a `const` modifier or is annotated with `default_method_body_is_const`.
873+
let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id())
874+
|| tcx.has_attr(def_id.to_def_id(), sym::default_method_body_is_const);
874875
let always_encode_mir = tcx.sess.opts.debugging_opts.always_encode_mir;
875876
(is_const_fn, needs_inline || always_encode_mir)
876877
}

src/test/ui/rfc-2632-const-trait-impl/auxiliary/cross-crate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#![feature(const_fn_trait_bound)]
12
#![feature(const_trait_impl)]
23

34
pub trait MyTrait {
5+
#[default_method_body_is_const]
6+
fn defaulted_func(&self) {}
47
fn func(self);
58
}
69

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This tests that `default_method_body_is_const` methods can
2+
// be called from a const context when used across crates.
3+
//
4+
// check-pass
5+
6+
#![feature(const_trait_impl)]
7+
8+
// aux-build: cross-crate.rs
9+
extern crate cross_crate;
10+
11+
use cross_crate::*;
12+
13+
const _: () = {
14+
Const.func();
15+
Const.defaulted_func();
16+
};
17+
18+
fn main() {}

0 commit comments

Comments
 (0)