Skip to content

Commit 2676860

Browse files
authored
Rollup merge of #119708 - compiler-errors:pointer-like, r=Nilstrieb
Unions are not `PointerLike` I introduced the `PointerLike` trait to enforce `dyn*` coercions only from types that share the same ABI as a pointer. On top of needing to be scalar, they also should not be unions, since CTFE chokes on scalar reads for union types. Fixes #119695
2 parents 39b3ef1 + 68bb766 commit 2676860

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

compiler/rustc_target/src/abi/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ impl<'a> Layout<'a> {
120120
/// Whether the layout is from a type that implements [`std::marker::PointerLike`].
121121
///
122122
/// Currently, that means that the type is pointer-sized, pointer-aligned,
123-
/// and has a scalar ABI.
123+
/// and has a initialized (non-union), scalar ABI.
124124
pub fn is_pointer_like(self, data_layout: &TargetDataLayout) -> bool {
125125
self.size() == data_layout.pointer_size
126126
&& self.align().abi == data_layout.pointer_align.abi
127-
&& matches!(self.abi(), Abi::Scalar(..))
127+
&& matches!(self.abi(), Abi::Scalar(Scalar::Initialized { .. }))
128128
}
129129
}
130130

tests/ui/dyn-star/union.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(dyn_star)]
2+
//~^ WARN the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes
3+
4+
union Union {
5+
x: usize,
6+
}
7+
8+
trait Trait {}
9+
impl Trait for Union {}
10+
11+
fn bar(_: dyn* Trait) {}
12+
13+
fn main() {
14+
bar(Union { x: 0usize });
15+
//~^ ERROR `Union` needs to have the same ABI as a pointer
16+
}

tests/ui/dyn-star/union.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/union.rs:1:12
3+
|
4+
LL | #![feature(dyn_star)]
5+
| ^^^^^^^^
6+
|
7+
= note: see issue #102425 <https://github.com/rust-lang/rust/issues/102425> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0277]: `Union` needs to have the same ABI as a pointer
11+
--> $DIR/union.rs:14:9
12+
|
13+
LL | bar(Union { x: 0usize });
14+
| ^^^^^^^^^^^^^^^^^^^ `Union` needs to be a pointer-like type
15+
|
16+
= help: the trait `PointerLike` is not implemented for `Union`
17+
18+
error: aborting due to 1 previous error; 1 warning emitted
19+
20+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)