Skip to content

Commit a6c4cbd

Browse files
committed
review comment and one more test
1 parent b5b811a commit a6c4cbd

File tree

4 files changed

+29
-18
lines changed

4 files changed

+29
-18
lines changed

compiler/rustc_mir/src/const_eval/machine.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
3838
if instance.def.requires_caller_location(self.tcx()) {
3939
return Ok(false);
4040
}
41+
// only memoize instrinsics
42+
if !matches!(instance.def, InstanceDef::Intrinsic(_)) {
43+
return Ok(false);
44+
}
4145
// For the moment we only do this for functions which take no arguments
4246
// (or all arguments are ZSTs) so that we don't memoize too much.
4347
if args.iter().any(|a| !a.layout.is_zst()) {
@@ -232,13 +236,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
232236
if ecx.tcx.is_const_fn_raw(def.did) {
233237
// If this function is a `const fn` then under certain circumstances we
234238
// can evaluate call via the query system, thus memoizing all future calls.
235-
match instance.def {
236-
InstanceDef::Intrinsic(_) => {
237-
if ecx.try_eval_const_fn_call(instance, ret, args)? {
238-
return Ok(None);
239-
}
240-
}
241-
_ => {}
239+
if ecx.try_eval_const_fn_call(instance, ret, args)? {
240+
return Ok(None);
242241
}
243242
} else {
244243
// Some functions we support even if they are non-const -- but avoid testing
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
// run-pass
12
#![feature(core_intrinsics)]
23
#![feature(const_heap)]
34
#![feature(const_raw_ptr_deref)]
45
#![feature(const_mut_refs)]
56
use std::intrinsics;
67

7-
const FOO: *const i32 = foo();
8-
//~^ error: untyped pointers are not allowed in constant
8+
const FOO: &i32 = foo();
99

1010
const fn foo() -> &'static i32 {
1111
let t = unsafe {
@@ -16,5 +16,5 @@ const fn foo() -> &'static i32 {
1616
unsafe { &*t }
1717
}
1818
fn main() {
19-
assert_eq!(unsafe { *FOO }, 20)
19+
assert_eq!(*FOO, 20)
2020
}

src/test/ui/consts/const-eval/heap/alloc_intrinsic_nontransient.stderr

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-pass
2+
#![feature(core_intrinsics)]
3+
#![feature(const_heap)]
4+
#![feature(const_raw_ptr_deref)]
5+
#![feature(const_mut_refs)]
6+
use std::intrinsics;
7+
8+
const FOO: &i32 = foo();
9+
10+
const fn foo() -> &'static i32 {
11+
let t = unsafe {
12+
let i = intrinsics::const_allocate(4, 4) as * mut i32;
13+
*i = 20;
14+
i
15+
};
16+
unsafe { &*t }
17+
}
18+
fn main() {
19+
assert_eq!(*FOO, 20)
20+
}

0 commit comments

Comments
 (0)