Skip to content

Commit 749afe5

Browse files
committed
fix #103751: Fix capacity overflow issue during transmutability check
1 parent 126dbdc commit 749afe5

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

Diff for: compiler/rustc_transmute/src/layout/tree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ pub(crate) mod rustc {
435435

436436
// finally: padding
437437
let padding_span = trace_span!("adding trailing padding").entered();
438-
let padding_needed = layout_summary.total_size - variant_layout.size();
439-
if padding_needed > 0 {
438+
if layout_summary.total_size > variant_layout.size() {
439+
let padding_needed = layout_summary.total_size - variant_layout.size();
440440
tree = tree.then(Self::padding(padding_needed));
441441
};
442442
drop(padding_span);

Diff for: src/test/ui/transmute/transmute-padding-ice.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![crate_type = "lib"]
2+
#![feature(transmutability)]
3+
#![allow(dead_code)]
4+
5+
mod assert {
6+
use std::mem::{Assume, BikeshedIntrinsicFrom};
7+
pub struct Context;
8+
9+
pub fn is_maybe_transmutable<Src, Dst>()
10+
where
11+
Dst: BikeshedIntrinsicFrom<
12+
Src,
13+
Context,
14+
{ Assume { alignment: true, lifetimes: true, safety: true, validity: true } },
15+
>,
16+
{
17+
}
18+
}
19+
20+
fn test() {
21+
#[repr(C, align(2))]
22+
struct A(u8, u8);
23+
24+
#[repr(C)]
25+
struct B(u8, u8);
26+
27+
assert::is_maybe_transmutable::<B, A>();
28+
//~^ ERROR cannot be safely transmuted
29+
}

Diff for: src/test/ui/transmute/transmute-padding-ice.stderr

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0277]: `B` cannot be safely transmuted into `A` in the defining scope of `assert::Context`.
2+
--> $DIR/transmute-padding-ice.rs:27:40
3+
|
4+
LL | assert::is_maybe_transmutable::<B, A>();
5+
| ^ `B` cannot be safely transmuted into `A` in the defining scope of `assert::Context`.
6+
|
7+
= help: the trait `BikeshedIntrinsicFrom<B, assert::Context, Assume { alignment: true, lifetimes: true, safety: true, validity: true }>` is not implemented for `A`
8+
note: required by a bound in `is_maybe_transmutable`
9+
--> $DIR/transmute-padding-ice.rs:11:14
10+
|
11+
LL | pub fn is_maybe_transmutable<Src, Dst>()
12+
| --------------------- required by a bound in this
13+
LL | where
14+
LL | Dst: BikeshedIntrinsicFrom<
15+
| ______________^
16+
LL | | Src,
17+
LL | | Context,
18+
LL | | { Assume { alignment: true, lifetimes: true, safety: true, validity: true } },
19+
LL | | >,
20+
| |_________^ required by this bound in `is_maybe_transmutable`
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)