Skip to content

Commit b5b811a

Browse files
committed
review comments
1 parent 528355c commit b5b811a

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

compiler/rustc_mir/src/const_eval/machine.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_middle::mir;
22
use rustc_middle::ty::layout::HasTyCtxt;
3+
use rustc_middle::ty::InstanceDef;
34
use rustc_middle::ty::{self, Ty};
45
use std::borrow::Borrow;
56
use std::collections::hash_map::Entry;
@@ -231,8 +232,13 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
231232
if ecx.tcx.is_const_fn_raw(def.did) {
232233
// If this function is a `const fn` then under certain circumstances we
233234
// can evaluate call via the query system, thus memoizing all future calls.
234-
if ecx.try_eval_const_fn_call(instance, ret, args)? {
235-
return Ok(None);
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+
_ => {}
236242
}
237243
} else {
238244
// Some functions we support even if they are non-const -- but avoid testing

compiler/rustc_mir/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
344344

345345
let align = match Align::from_bytes(align) {
346346
Ok(a) => a,
347-
Err(err) => bug!("align has to power of 2, {}", err),
347+
Err(err) => throw_ub_format!("align has to be a power of 2, {}", err),
348348
};
349349

350350
let ptr =

compiler/rustc_mir/src/interpret/memory.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<T: MayLeak> MayLeak for MemoryKind<T> {
4242
fn may_leak(self) -> bool {
4343
match self {
4444
MemoryKind::Stack => false,
45-
MemoryKind::Heap => true,
45+
MemoryKind::Heap => false,
4646
MemoryKind::Vtable => true,
4747
MemoryKind::CallerLocation => true,
4848
MemoryKind::Machine(k) => k.may_leak(),
@@ -54,7 +54,7 @@ impl<T: fmt::Display> fmt::Display for MemoryKind<T> {
5454
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5555
match self {
5656
MemoryKind::Stack => write!(f, "stack variable"),
57-
MemoryKind::Heap => write!(f, "heap variable"),
57+
MemoryKind::Heap => write!(f, "heap allocation"),
5858
MemoryKind::Vtable => write!(f, "vtable"),
5959
MemoryKind::CallerLocation => write!(f, "caller location"),
6060
MemoryKind::Machine(m) => write!(f, "{}", m),

library/core/src/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ extern "rust-intrinsic" {
17341734
pub fn ptr_guaranteed_ne<T>(ptr: *const T, other: *const T) -> bool;
17351735

17361736
/// Allocate at compile time. Should not be called at runtime.
1737-
#[rustc_const_unstable(feature = "const_heap", issue = "none")]
1737+
#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
17381738
#[cfg(not(bootstrap))]
17391739
pub fn const_allocate(size: usize, align: usize) -> *mut u8;
17401740
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(const_heap)]
3+
#![feature(const_raw_ptr_deref)]
4+
#![feature(const_mut_refs)]
5+
use std::intrinsics;
6+
7+
const FOO: i32 = foo();
8+
const fn foo() -> i32 {
9+
unsafe {
10+
let _ = intrinsics::const_allocate(4, 3) as * mut i32;
11+
//~^ error: any use of this value will cause an error [const_err]
12+
}
13+
1
14+
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/alloc_intrinsic_errors.rs:10:17
3+
|
4+
LL | const FOO: i32 = foo();
5+
| -----------------------
6+
...
7+
LL | let _ = intrinsics::const_allocate(4, 3) as * mut i32;
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
| |
10+
| align has to be a power of 2, `3` is not a power of 2
11+
| inside `foo` at $DIR/alloc_intrinsic_errors.rs:10:17
12+
| inside `FOO` at $DIR/alloc_intrinsic_errors.rs:7:18
13+
|
14+
= note: `#[deny(const_err)]` on by default
15+
16+
error: aborting due to previous error
17+

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// run-pass
21
#![feature(core_intrinsics)]
32
#![feature(const_heap)]
43
#![feature(const_raw_ptr_deref)]
54
#![feature(const_mut_refs)]
65
use std::intrinsics;
76

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

1010
const fn foo() -> &'static i32 {
1111
let t = unsafe {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: untyped pointers are not allowed in constant
2+
--> $DIR/alloc_intrinsic_nontransient.rs:7:1
3+
|
4+
LL | const FOO: *const i32 = foo();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)