Skip to content

Commit 685fb54

Browse files
committed
Auto merge of #53535 - TheDarkula:master, r=oli-obk
Made std::intrinsics::transmute() const fn. r? @oli-obk tracking issue: #53605
2 parents 0e98621 + c5cae79 commit 685fb54

12 files changed

+139
-9
lines changed

src/librustc_mir/interpret/intrinsics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
103103
};
104104
self.write_scalar(out_val, dest)?;
105105
}
106+
"transmute" => {
107+
// Go through an allocation, to make sure the completely different layouts
108+
// do not pose a problem. (When the user transmutes through a union,
109+
// there will not be a layout mismatch.)
110+
let dest = self.force_allocation(dest)?;
111+
self.copy_op(args[0], dest.into())?;
112+
}
106113

107114
_ => return Ok(false),
108115
}

src/librustc_mir/transform/qualify_consts.rs

+12
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,18 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
830830
| "cttz_nonzero"
831831
| "ctlz"
832832
| "ctlz_nonzero" => is_const_fn = Some(def_id),
833+
"transmute" => {
834+
if self.mode != Mode::Fn {
835+
is_const_fn = Some(def_id);
836+
if !self.tcx.sess.features_untracked().const_transmute {
837+
emit_feature_err(
838+
&self.tcx.sess.parse_sess, "const_transmute",
839+
self.span, GateIssue::Language,
840+
&format!("The use of std::mem::transmute() \
841+
is gated in {}s", self.mode));
842+
}
843+
}
844+
}
833845

834846
name if name.starts_with("simd_shuffle") => {
835847
is_shuffle = true;

src/libsyntax/feature_gate.rs

+3
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ declare_features! (
221221
// Allows dereferencing raw pointers during const eval
222222
(active, const_raw_ptr_deref, "1.27.0", Some(51911), None),
223223

224+
// Allows reinterpretation of the bits of a value of one type as another type during const eval
225+
(active, const_transmute, "1.29.0", Some(53605), None),
226+
224227
// Allows comparing raw pointers during const eval
225228
(active, const_compare_raw_pointers, "1.27.0", Some(53020), None),
226229

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(const_transmute)]
12+
13+
use std::mem;
14+
15+
#[repr(transparent)]
16+
struct Foo(u32);
17+
18+
const TRANSMUTED_U32: u32 = unsafe { mem::transmute(Foo(3)) };
19+
20+
fn main() {
21+
assert_eq!(TRANSMUTED_U32, 3);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(const_transmute)]
12+
13+
use std::mem;
14+
15+
fn main() {
16+
let x: &'static u32 = unsafe { &mem::transmute(3.0f32) };
17+
//~^ ERROR value does not live long enough
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0597]: borrowed value does not live long enough
2+
--> $DIR/transmute-const-promotion.rs:16:37
3+
|
4+
LL | let x: &'static u32 = unsafe { &mem::transmute(3.0f32) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
6+
LL | //~^ ERROR value does not live long enough
7+
LL | }
8+
| - temporary value only lives until here
9+
|
10+
= note: borrowed value must be valid for the static lifetime...
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0597`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(const_transmute)]
12+
13+
use std::mem;
14+
15+
static FOO: bool = unsafe { mem::transmute(3u8) };
16+
//~^ ERROR this static likely exhibits undefined behavior
17+
//~^^ type validation failed: encountered 3, but expected something in the range 0..=1
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0080]: this static likely exhibits undefined behavior
2+
--> $DIR/transmute-const.rs:15:1
3+
|
4+
LL | static FOO: bool = unsafe { mem::transmute(3u8) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3, but expected something in the range 0..=1
6+
|
7+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-fn-not-safe-for-const.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
// Test that we can't call random fns in a const fn or do other bad things.
1212

13-
#![feature(const_fn)]
13+
#![feature(const_fn, const_transmute)]
1414

1515
use std::mem::transmute;
1616

1717
fn random() -> u32 { 0 }
1818

1919
const fn sub(x: &u32) -> usize {
20-
unsafe { transmute(x) } //~ ERROR E0015
20+
unsafe { transmute(x) }
2121
}
2222

2323
const fn sub1() -> u32 {

src/test/ui/consts/const-fn-not-safe-for-const.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2-
--> $DIR/const-fn-not-safe-for-const.rs:20:14
3-
|
4-
LL | unsafe { transmute(x) } //~ ERROR E0015
5-
| ^^^^^^^^^^^^
6-
71
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
82
--> $DIR/const-fn-not-safe-for-const.rs:24:5
93
|
@@ -70,7 +64,7 @@ LL | x + y
7064
|
7165
= help: add #![feature(const_let)] to the crate attributes to enable
7266

73-
error: aborting due to 10 previous errors
67+
error: aborting due to 9 previous errors
7468

7569
Some errors occurred: E0013, E0015, E0658.
7670
For more information about an error, try `rustc --explain E0013`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::mem;
12+
13+
#[repr(transparent)]
14+
struct Foo(u32);
15+
16+
const TRANSMUTED_U32: u32 = unsafe { mem::transmute(Foo(3)) };
17+
//~^ ERROR The use of std::mem::transmute() is gated in constants (see issue #53605)
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0658]: The use of std::mem::transmute() is gated in constants (see issue #53605)
2+
--> $DIR/feature-gate-const_transmute.rs:16:38
3+
|
4+
LL | const TRANSMUTED_U32: u32 = unsafe { mem::transmute(Foo(3)) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add #![feature(const_transmute)] to the crate attributes to enable
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)