1
Fork 0

review comment and one more test

This commit is contained in:
Vishnunarayan K I 2020-12-01 20:12:22 +05:30
parent b5b811aab4
commit a6c4cbd46a
4 changed files with 29 additions and 18 deletions

View file

@ -38,6 +38,10 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
if instance.def.requires_caller_location(self.tcx()) { if instance.def.requires_caller_location(self.tcx()) {
return Ok(false); return Ok(false);
} }
// only memoize instrinsics
if !matches!(instance.def, InstanceDef::Intrinsic(_)) {
return Ok(false);
}
// For the moment we only do this for functions which take no arguments // For the moment we only do this for functions which take no arguments
// (or all arguments are ZSTs) so that we don't memoize too much. // (or all arguments are ZSTs) so that we don't memoize too much.
if args.iter().any(|a| !a.layout.is_zst()) { if args.iter().any(|a| !a.layout.is_zst()) {
@ -232,13 +236,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
if ecx.tcx.is_const_fn_raw(def.did) { if ecx.tcx.is_const_fn_raw(def.did) {
// If this function is a `const fn` then under certain circumstances we // If this function is a `const fn` then under certain circumstances we
// can evaluate call via the query system, thus memoizing all future calls. // can evaluate call via the query system, thus memoizing all future calls.
match instance.def { if ecx.try_eval_const_fn_call(instance, ret, args)? {
InstanceDef::Intrinsic(_) => { return Ok(None);
if ecx.try_eval_const_fn_call(instance, ret, args)? {
return Ok(None);
}
}
_ => {}
} }
} else { } else {
// Some functions we support even if they are non-const -- but avoid testing // Some functions we support even if they are non-const -- but avoid testing

View file

@ -1,11 +1,11 @@
// run-pass
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(const_heap)] #![feature(const_heap)]
#![feature(const_raw_ptr_deref)] #![feature(const_raw_ptr_deref)]
#![feature(const_mut_refs)] #![feature(const_mut_refs)]
use std::intrinsics; use std::intrinsics;
const FOO: *const i32 = foo(); const FOO: &i32 = foo();
//~^ error: untyped pointers are not allowed in constant
const fn foo() -> &'static i32 { const fn foo() -> &'static i32 {
let t = unsafe { let t = unsafe {
@ -16,5 +16,5 @@ const fn foo() -> &'static i32 {
unsafe { &*t } unsafe { &*t }
} }
fn main() { fn main() {
assert_eq!(unsafe { *FOO }, 20) assert_eq!(*FOO, 20)
} }

View file

@ -1,8 +0,0 @@
error: untyped pointers are not allowed in constant
--> $DIR/alloc_intrinsic_nontransient.rs:7:1
|
LL | const FOO: *const i32 = foo();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error

View file

@ -0,0 +1,20 @@
// run-pass
#![feature(core_intrinsics)]
#![feature(const_heap)]
#![feature(const_raw_ptr_deref)]
#![feature(const_mut_refs)]
use std::intrinsics;
const FOO: &i32 = foo();
const fn foo() -> &'static i32 {
let t = unsafe {
let i = intrinsics::const_allocate(4, 4) as * mut i32;
*i = 20;
i
};
unsafe { &*t }
}
fn main() {
assert_eq!(*FOO, 20)
}