Skip to content

Commit b76dd8c

Browse files
committed
Add feature gate
1 parent 648a35e commit b76dd8c

File tree

9 files changed

+200
-18
lines changed

9 files changed

+200
-18
lines changed

compiler/rustc_feature/src/active.rs

+2
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ declare_features! (
516516
/// Allows dyn upcasting trait objects via supertraits.
517517
/// Dyn upcasting is casting, e.g., `dyn Foo -> dyn Bar` where `Foo: Bar`.
518518
(active, trait_upcasting, "1.56.0", Some(65991), None),
519+
/// Allows for transmuting between arrays with sizes that contain generic consts.
520+
(active, transmute_generic_consts, "CURRENT_RUSTC_VERSION", Some(109929), None),
519521
/// Allows #[repr(transparent)] on unions (RFC 2645).
520522
(active, transparent_unions, "1.37.0", Some(60405), None),
521523
/// Allows inconsistent bounds in where clauses.

compiler/rustc_hir_typeck/src/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8888
if let Some(size) = size.try_eval_target_usize(tcx, self.param_env) {
8989
format!("{size} bytes")
9090
} else {
91-
format!("generic size")
91+
format!("generic size {size}")
9292
}
9393
}
9494
Err(LayoutError::Unknown(bad)) => {

compiler/rustc_middle/src/ty/layout.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,9 @@ impl<'tcx> SizeSkeleton<'tcx> {
332332
),
333333
}
334334
}
335-
ty::Array(inner, len) if len.ty() == tcx.types.usize => {
335+
ty::Array(inner, len)
336+
if len.ty() == tcx.types.usize && tcx.features().transmute_generic_consts =>
337+
{
336338
match SizeSkeleton::compute(inner, tcx, param_env)? {
337339
// This may succeed because the multiplication of two types may overflow
338340
// but a single size of a nested array will not.
@@ -483,17 +485,17 @@ fn mul_sorted_consts<'tcx>(
483485
}
484486
let mut k = 1;
485487
let mut overflow = false;
486-
for _ in done.drain_filter(|c| {
488+
done.retain(|c| {
487489
let Some(c) = c.try_eval_target_usize(tcx, param_env) else {
488-
return false;
490+
return true;
489491
};
490492
let Some(next) = c.checked_mul(k) else {
491493
overflow = true;
492-
return true;
494+
return false;
493495
};
494-
k *= next;
495-
true
496-
}) {}
496+
k = next;
497+
false
498+
});
497499
if overflow {
498500
return None;
499501
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,7 @@ symbols! {
14931493
trait_alias,
14941494
trait_upcasting,
14951495
transmute,
1496+
transmute_generic_consts,
14961497
transmute_opts,
14971498
transmute_trait,
14981499
transparent,

tests/ui/const-generics/transmute-fail.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(transmute_generic_consts)]
12
#![feature(generic_const_exprs)]
23
#![allow(incomplete_features)]
34

tests/ui/const-generics/transmute-fail.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2-
--> $DIR/transmute-fail.rs:6:5
2+
--> $DIR/transmute-fail.rs:7:5
33
|
44
LL | std::mem::transmute(v)
55
| ^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: source type: `[[u32; H+1]; W]` (generic size)
8-
= note: target type: `[[u32; W+1]; H]` (generic size)
7+
= note: source type: `[[u32; H+1]; W]` (generic size [const expr])
8+
= note: target type: `[[u32; W+1]; H]` (generic size [const expr])
99

1010
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
11-
--> $DIR/transmute-fail.rs:15:5
11+
--> $DIR/transmute-fail.rs:16:5
1212
|
1313
LL | std::mem::transmute(v)
1414
| ^^^^^^^^^^^^^^^^^^^
@@ -17,28 +17,28 @@ LL | std::mem::transmute(v)
1717
= note: target type: `[[u32; W]; H]` (size can vary because of [u32; W])
1818

1919
error[E0308]: mismatched types
20-
--> $DIR/transmute-fail.rs:11:53
20+
--> $DIR/transmute-fail.rs:12:53
2121
|
2222
LL | fn bar<const W: bool, const H: usize>(v: [[u32; H]; W]) -> [[u32; W]; H] {
2323
| ^ expected `usize`, found `bool`
2424

2525
error[E0308]: mismatched types
26-
--> $DIR/transmute-fail.rs:11:67
26+
--> $DIR/transmute-fail.rs:12:67
2727
|
2828
LL | fn bar<const W: bool, const H: usize>(v: [[u32; H]; W]) -> [[u32; W]; H] {
2929
| ^ expected `usize`, found `bool`
3030

3131
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
32-
--> $DIR/transmute-fail.rs:22:5
32+
--> $DIR/transmute-fail.rs:23:5
3333
|
3434
LL | std::mem::transmute(v)
3535
| ^^^^^^^^^^^^^^^^^^^
3636
|
37-
= note: source type: `[[u32; H]; W]` (generic size)
38-
= note: target type: `[u32; W * H * H]` (generic size)
37+
= note: source type: `[[u32; H]; W]` (generic size [const expr])
38+
= note: target type: `[u32; W * H * H]` (generic size [const expr])
3939

4040
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
41-
--> $DIR/transmute-fail.rs:29:5
41+
--> $DIR/transmute-fail.rs:30:5
4242
|
4343
LL | std::mem::transmute(v)
4444
| ^^^^^^^^^^^^^^^^^^^

tests/ui/const-generics/transmute.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-pass
22
#![feature(generic_const_exprs)]
3+
#![feature(transmute_generic_consts)]
34
#![allow(incomplete_features)]
45

56
fn transpose<const W: usize, const H: usize>(v: [[u32;H]; W]) -> [[u32; W]; H] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// gate-test-transmute_generic_consts
2+
#![feature(generic_const_exprs)]
3+
#![allow(incomplete_features)]
4+
5+
fn transpose<const W: usize, const H: usize>(v: [[u32;H]; W]) -> [[u32; W]; H] {
6+
unsafe {
7+
std::mem::transmute(v)
8+
//~^ ERROR cannot transmute
9+
}
10+
}
11+
12+
fn ident<const W: usize, const H: usize>(v: [[u32; H]; W]) -> [[u32; H]; W] {
13+
unsafe {
14+
std::mem::transmute(v)
15+
}
16+
}
17+
18+
fn flatten<const W: usize, const H: usize>(v: [[u32; H]; W]) -> [u32; W * H] {
19+
unsafe {
20+
std::mem::transmute(v)
21+
//~^ ERROR cannot transmute
22+
}
23+
}
24+
25+
fn coagulate<const W: usize, const H: usize>(v: [u32; H*W]) -> [[u32; W];H] {
26+
unsafe {
27+
std::mem::transmute(v)
28+
//~^ ERROR cannot transmute
29+
}
30+
}
31+
32+
fn flatten_3d<const W: usize, const H: usize, const D: usize>(
33+
v: [[[u32; D]; H]; W]
34+
) -> [u32; D * W * H] {
35+
unsafe {
36+
std::mem::transmute(v)
37+
//~^ ERROR cannot transmute
38+
}
39+
}
40+
41+
fn flatten_somewhat<const W: usize, const H: usize, const D: usize>(
42+
v: [[[u32; D]; H]; W]
43+
) -> [[u32; D * W]; H] {
44+
unsafe {
45+
std::mem::transmute(v)
46+
//~^ ERROR cannot transmute
47+
}
48+
}
49+
50+
fn known_size<const L: usize>(v: [u16; L]) -> [u8; L * 2] {
51+
unsafe {
52+
std::mem::transmute(v)
53+
//~^ ERROR cannot transmute
54+
}
55+
}
56+
57+
fn condense_bytes<const L: usize>(v: [u8; L * 2]) -> [u16; L] {
58+
unsafe {
59+
std::mem::transmute(v)
60+
//~^ ERROR cannot transmute
61+
}
62+
}
63+
64+
fn singleton_each<const L: usize>(v: [u8; L]) -> [[u8;1]; L] {
65+
unsafe {
66+
std::mem::transmute(v)
67+
//~^ ERROR cannot transmute
68+
}
69+
}
70+
71+
fn transpose_with_const<const W: usize, const H: usize>(
72+
v: [[u32; 2 * H]; W + W]
73+
) -> [[u32; W + W]; 2 * H] {
74+
unsafe {
75+
std::mem::transmute(v)
76+
//~^ ERROR cannot transmute
77+
}
78+
}
79+
80+
fn main() {
81+
let _ = transpose([[0; 8]; 16]);
82+
let _ = transpose_with_const::<8,4>([[0; 8]; 16]);
83+
let _ = ident([[0; 8]; 16]);
84+
let _ = flatten([[0; 13]; 5]);
85+
let _: [[_; 5]; 13] = coagulate([0; 65]);
86+
let _ = flatten_3d([[[0; 3]; 13]; 5]);
87+
let _ = flatten_somewhat([[[0; 3]; 13]; 5]);
88+
let _ = known_size([16; 13]);
89+
let _: [u16; 5] = condense_bytes([16u8; 10]);
90+
let _ = singleton_each([16; 10]);
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2+
--> $DIR/transmute_no_gate.rs:7:5
3+
|
4+
LL | std::mem::transmute(v)
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: source type: `[[u32; H]; W]` (this type does not have a fixed size)
8+
= note: target type: `[[u32; W]; H]` (this type does not have a fixed size)
9+
10+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
11+
--> $DIR/transmute_no_gate.rs:20:5
12+
|
13+
LL | std::mem::transmute(v)
14+
| ^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: source type: `[[u32; H]; W]` (this type does not have a fixed size)
17+
= note: target type: `[u32; W * H]` (this type does not have a fixed size)
18+
19+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
20+
--> $DIR/transmute_no_gate.rs:27:5
21+
|
22+
LL | std::mem::transmute(v)
23+
| ^^^^^^^^^^^^^^^^^^^
24+
|
25+
= note: source type: `[u32; H*W]` (this type does not have a fixed size)
26+
= note: target type: `[[u32; W]; H]` (this type does not have a fixed size)
27+
28+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
29+
--> $DIR/transmute_no_gate.rs:36:5
30+
|
31+
LL | std::mem::transmute(v)
32+
| ^^^^^^^^^^^^^^^^^^^
33+
|
34+
= note: source type: `[[[u32; D]; H]; W]` (this type does not have a fixed size)
35+
= note: target type: `[u32; D * W * H]` (this type does not have a fixed size)
36+
37+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
38+
--> $DIR/transmute_no_gate.rs:45:5
39+
|
40+
LL | std::mem::transmute(v)
41+
| ^^^^^^^^^^^^^^^^^^^
42+
|
43+
= note: source type: `[[[u32; D]; H]; W]` (this type does not have a fixed size)
44+
= note: target type: `[[u32; D * W]; H]` (this type does not have a fixed size)
45+
46+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
47+
--> $DIR/transmute_no_gate.rs:52:5
48+
|
49+
LL | std::mem::transmute(v)
50+
| ^^^^^^^^^^^^^^^^^^^
51+
|
52+
= note: source type: `[u16; L]` (this type does not have a fixed size)
53+
= note: target type: `[u8; L * 2]` (this type does not have a fixed size)
54+
55+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
56+
--> $DIR/transmute_no_gate.rs:59:5
57+
|
58+
LL | std::mem::transmute(v)
59+
| ^^^^^^^^^^^^^^^^^^^
60+
|
61+
= note: source type: `[u8; L * 2]` (this type does not have a fixed size)
62+
= note: target type: `[u16; L]` (this type does not have a fixed size)
63+
64+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
65+
--> $DIR/transmute_no_gate.rs:66:5
66+
|
67+
LL | std::mem::transmute(v)
68+
| ^^^^^^^^^^^^^^^^^^^
69+
|
70+
= note: source type: `[u8; L]` (this type does not have a fixed size)
71+
= note: target type: `[[u8; 1]; L]` (this type does not have a fixed size)
72+
73+
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
74+
--> $DIR/transmute_no_gate.rs:75:5
75+
|
76+
LL | std::mem::transmute(v)
77+
| ^^^^^^^^^^^^^^^^^^^
78+
|
79+
= note: source type: `[[u32; 2 * H]; W + W]` (this type does not have a fixed size)
80+
= note: target type: `[[u32; W + W]; 2 * H]` (this type does not have a fixed size)
81+
82+
error: aborting due to 9 previous errors
83+
84+
For more information about this error, try `rustc --explain E0512`.

0 commit comments

Comments
 (0)