Skip to content

Commit eb6ab90

Browse files
committed
WIP
1 parent c9ec73b commit eb6ab90

File tree

3 files changed

+116
-30
lines changed

3 files changed

+116
-30
lines changed

‎compiler/rustc_transmute/src/layout/tree.rs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ pub(crate) mod rustc {
299299
let target = cx.data_layout();
300300
let pointer_size = target.pointer_size();
301301

302+
let kind = format!("{:?}", ty.kind());
303+
tracing::debug!("kind: {}", kind);
304+
302305
match ty.kind() {
303306
ty::Bool => Ok(Self::bool()),
304307

@@ -335,9 +338,28 @@ pub(crate) mod rustc {
335338

336339
use core::ops::Bound::*;
337340
let is_transparent = adt_def.repr().transparent();
341+
let ty_name = format!("{:?}", ty);
342+
tracing::debug!("Inspecting type: {}", ty_name);
338343
match (adt_def.adt_kind(), lo, hi) {
339344
(AdtKind::Struct, Unbounded, Unbounded) => {
340-
Self::from_struct((ty, layout), *adt_def, cx)
345+
if is_transparent {
346+
let variant = adt_def.non_enum_variant();
347+
// For now, only support `repr(transparent)`
348+
// types with a single field. Eventually, we'll
349+
// need to add support for all
350+
// `repr(transparent)` types.
351+
let [field] = &variant.fields.as_slice().raw else {
352+
return Err(Err::NotYetSupported);
353+
};
354+
355+
Ok(Self::seq([
356+
Self::def(Def::Adt(adt_def.clone())),
357+
Self::def(Def::Field(field)),
358+
Self::from_ty(field.ty(cx.tcx(), args_ref), cx)?,
359+
]))
360+
} else {
361+
Self::from_struct((ty, layout), *adt_def, cx)
362+
}
341363
}
342364
(AdtKind::Struct, Included(1), hi_val) if is_transparent => {
343365
let variant = adt_def.non_enum_variant();
@@ -403,6 +425,18 @@ pub(crate) mod rustc {
403425

404426
ty::Char => Ok(Self::char(cx.tcx().data_layout.endian.into())),
405427

428+
ty::Alias(alias_kind, aliased_ty) if *alias_kind == ty::AliasTyKind::Projection => {
429+
// Seems that, when we get here, `kind` is:
430+
//
431+
// "Alias(Projection, AliasTy { args: [u8], def_id: DefId(2:1179 ~ core[2936]::num::nonzero::ZeroablePrimitive::NonZeroInner), .. })"
432+
//
433+
// ...and `aliased_ty.self_ty()` is `u8`.
434+
let aliased_ty = aliased_ty.self_ty();
435+
let s = format!("{aliased_ty:?}");
436+
tracing::debug!("aliased ty: {s}");
437+
Self::from_ty(aliased_ty, cx)
438+
}
439+
406440
_ => Err(Err::NotYetSupported),
407441
}
408442
}

‎tests/ui/transmutability/nonzero.rs‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,17 @@ mod assert {
1919
}
2020

2121
fn main() {
22-
// FIXME: Replace this with `core::num::NonZeroU8` once we support it.
23-
#[rustc_layout_scalar_valid_range_start(1)]
24-
#[repr(transparent)]
25-
struct NonZeroU8(u8);
26-
27-
// FIXME: Replace this with `core::num::NonZeroU16` once we support it.
28-
#[rustc_layout_scalar_valid_range_start(1)]
29-
#[repr(transparent)]
30-
struct NonZeroU16(u16);
22+
use std::num::{NonZeroU8, NonZeroU16};
3123

3224
assert::is_transmutable_assume_safety::<u8, NonZeroU8>(); //~ ERROR: cannot be safely transmuted
33-
assert::is_transmutable_assume_safety::<NonZeroU8, u8>();
25+
// assert::is_transmutable_assume_safety::<NonZeroU8, u8>();
3426

35-
assert::is_transmutable_assume_nothing::<NonZeroU8, NonZeroU8>(); //~ ERROR: cannot be safely transmuted
36-
assert::is_transmutable_assume_safety::<NonZeroU8, NonZeroU8>();
27+
// assert::is_transmutable_assume_nothing::<NonZeroU8, NonZeroU8>(); //~ ERROR: cannot be safely transmuted
28+
// assert::is_transmutable_assume_safety::<NonZeroU8, NonZeroU8>();
3729

38-
assert::is_transmutable_assume_safety::<u16, NonZeroU16>(); //~ ERROR: cannot be safely transmuted
39-
assert::is_transmutable_assume_safety::<NonZeroU16, u16>();
30+
// assert::is_transmutable_assume_safety::<u16, NonZeroU16>(); //~ ERROR: cannot be safely transmuted
31+
// assert::is_transmutable_assume_safety::<NonZeroU16, u16>();
4032

41-
assert::is_transmutable_assume_nothing::<NonZeroU16, NonZeroU16>(); //~ ERROR: cannot be safely transmuted
42-
assert::is_transmutable_assume_safety::<NonZeroU16, NonZeroU16>();
33+
// assert::is_transmutable_assume_nothing::<NonZeroU16, NonZeroU16>(); //~ ERROR: cannot be safely transmuted
34+
// assert::is_transmutable_assume_safety::<NonZeroU16, NonZeroU16>();
4335
}
Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0277]: `u8` cannot be safely transmuted into `NonZeroU8`
2-
--> $DIR/nonzero.rs:32:49
1+
error[E0277]: `u8` cannot be safely transmuted into `NonZero<u8>`
2+
--> $DIR/nonzero.rs:34:49
33
|
44
LL | assert::is_transmutable_assume_safety::<u8, NonZeroU8>();
5-
| ^^^^^^^^^ at least one value of `u8` isn't a bit-valid value of `NonZeroU8`
5+
| ^^^^^^^^^ analyzing the transmutability of `NonZero<u8>` is not yet supported
66
|
77
note: required by a bound in `is_transmutable_assume_safety`
88
--> $DIR/nonzero.rs:16:14
@@ -13,11 +13,26 @@ LL | where
1313
LL | Dst: TransmuteFrom<Src, { Assume::SAFETY }>,
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_safety`
1515

16-
error[E0277]: `NonZeroU8` cannot be safely transmuted into `NonZeroU8`
17-
--> $DIR/nonzero.rs:35:57
16+
error[E0277]: `NonZero<u8>` cannot be safely transmuted into `u8`
17+
--> $DIR/nonzero.rs:35:56
18+
|
19+
LL | assert::is_transmutable_assume_safety::<NonZeroU8, u8>();
20+
| ^^ analyzing the transmutability of `NonZero<u8>` is not yet supported
21+
|
22+
note: required by a bound in `is_transmutable_assume_safety`
23+
--> $DIR/nonzero.rs:16:14
24+
|
25+
LL | pub fn is_transmutable_assume_safety<Src, Dst>()
26+
| ----------------------------- required by a bound in this function
27+
LL | where
28+
LL | Dst: TransmuteFrom<Src, { Assume::SAFETY }>,
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_safety`
30+
31+
error[E0277]: `NonZero<u8>` cannot be safely transmuted into `NonZero<u8>`
32+
--> $DIR/nonzero.rs:37:57
1833
|
1934
LL | assert::is_transmutable_assume_nothing::<NonZeroU8, NonZeroU8>();
20-
| ^^^^^^^^^ `NonZeroU8` may carry safety invariants
35+
| ^^^^^^^^^ analyzing the transmutability of `NonZero<u8>` is not yet supported
2136
|
2237
note: required by a bound in `is_transmutable_assume_nothing`
2338
--> $DIR/nonzero.rs:10:14
@@ -28,11 +43,26 @@ LL | where
2843
LL | Dst: TransmuteFrom<Src, { Assume::NOTHING }>,
2944
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_nothing`
3045

31-
error[E0277]: `u16` cannot be safely transmuted into `NonZeroU16`
32-
--> $DIR/nonzero.rs:38:50
46+
error[E0277]: `NonZero<u8>` cannot be safely transmuted into `NonZero<u8>`
47+
--> $DIR/nonzero.rs:38:56
48+
|
49+
LL | assert::is_transmutable_assume_safety::<NonZeroU8, NonZeroU8>();
50+
| ^^^^^^^^^ analyzing the transmutability of `NonZero<u8>` is not yet supported
51+
|
52+
note: required by a bound in `is_transmutable_assume_safety`
53+
--> $DIR/nonzero.rs:16:14
54+
|
55+
LL | pub fn is_transmutable_assume_safety<Src, Dst>()
56+
| ----------------------------- required by a bound in this function
57+
LL | where
58+
LL | Dst: TransmuteFrom<Src, { Assume::SAFETY }>,
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_safety`
60+
61+
error[E0277]: `u16` cannot be safely transmuted into `NonZero<u16>`
62+
--> $DIR/nonzero.rs:40:50
3363
|
3464
LL | assert::is_transmutable_assume_safety::<u16, NonZeroU16>();
35-
| ^^^^^^^^^^ at least one value of `u16` isn't a bit-valid value of `NonZeroU16`
65+
| ^^^^^^^^^^ analyzing the transmutability of `NonZero<u16>` is not yet supported
3666
|
3767
note: required by a bound in `is_transmutable_assume_safety`
3868
--> $DIR/nonzero.rs:16:14
@@ -43,11 +73,26 @@ LL | where
4373
LL | Dst: TransmuteFrom<Src, { Assume::SAFETY }>,
4474
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_safety`
4575

46-
error[E0277]: `NonZeroU16` cannot be safely transmuted into `NonZeroU16`
47-
--> $DIR/nonzero.rs:41:58
76+
error[E0277]: `NonZero<u16>` cannot be safely transmuted into `u16`
77+
--> $DIR/nonzero.rs:41:57
78+
|
79+
LL | assert::is_transmutable_assume_safety::<NonZeroU16, u16>();
80+
| ^^^ analyzing the transmutability of `NonZero<u16>` is not yet supported
81+
|
82+
note: required by a bound in `is_transmutable_assume_safety`
83+
--> $DIR/nonzero.rs:16:14
84+
|
85+
LL | pub fn is_transmutable_assume_safety<Src, Dst>()
86+
| ----------------------------- required by a bound in this function
87+
LL | where
88+
LL | Dst: TransmuteFrom<Src, { Assume::SAFETY }>,
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_safety`
90+
91+
error[E0277]: `NonZero<u16>` cannot be safely transmuted into `NonZero<u16>`
92+
--> $DIR/nonzero.rs:43:58
4893
|
4994
LL | assert::is_transmutable_assume_nothing::<NonZeroU16, NonZeroU16>();
50-
| ^^^^^^^^^^ `NonZeroU16` may carry safety invariants
95+
| ^^^^^^^^^^ analyzing the transmutability of `NonZero<u16>` is not yet supported
5196
|
5297
note: required by a bound in `is_transmutable_assume_nothing`
5398
--> $DIR/nonzero.rs:10:14
@@ -58,6 +103,21 @@ LL | where
58103
LL | Dst: TransmuteFrom<Src, { Assume::NOTHING }>,
59104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_nothing`
60105

61-
error: aborting due to 4 previous errors
106+
error[E0277]: `NonZero<u16>` cannot be safely transmuted into `NonZero<u16>`
107+
--> $DIR/nonzero.rs:44:57
108+
|
109+
LL | assert::is_transmutable_assume_safety::<NonZeroU16, NonZeroU16>();
110+
| ^^^^^^^^^^ analyzing the transmutability of `NonZero<u16>` is not yet supported
111+
|
112+
note: required by a bound in `is_transmutable_assume_safety`
113+
--> $DIR/nonzero.rs:16:14
114+
|
115+
LL | pub fn is_transmutable_assume_safety<Src, Dst>()
116+
| ----------------------------- required by a bound in this function
117+
LL | where
118+
LL | Dst: TransmuteFrom<Src, { Assume::SAFETY }>,
119+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_transmutable_assume_safety`
120+
121+
error: aborting due to 8 previous errors
62122

63123
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)