Skip to content

Commit aeddd2d

Browse files
committed
Auto merge of #115529 - chenyukang:yukang-fix-115402-overflowsize, r=compiler-errors
Fix error report for size overflow from transmute Fixes #115402 The span in the error reporting always points to the `dst`, this is an old issue, I may open another PR to fix it.
2 parents 25283f4 + 00010ed commit aeddd2d

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2920,6 +2920,16 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
29202920
rustc_transmute::Reason::DstIsTooBig => {
29212921
format!("The size of `{src}` is smaller than the size of `{dst}`")
29222922
}
2923+
rustc_transmute::Reason::SrcSizeOverflow => {
2924+
format!(
2925+
"values of the type `{src}` are too big for the current architecture"
2926+
)
2927+
}
2928+
rustc_transmute::Reason::DstSizeOverflow => {
2929+
format!(
2930+
"values of the type `{dst}` are too big for the current architecture"
2931+
)
2932+
}
29232933
rustc_transmute::Reason::DstHasStricterAlignment {
29242934
src_min_align,
29252935
dst_min_align,

compiler/rustc_transmute/src/layout/tree.rs

+3
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,16 @@ pub(crate) mod rustc {
189189
Unspecified,
190190
/// This error will be surfaced elsewhere by rustc, so don't surface it.
191191
UnknownLayout,
192+
/// Overflow size
193+
SizeOverflow,
192194
TypeError(ErrorGuaranteed),
193195
}
194196

195197
impl<'tcx> From<&LayoutError<'tcx>> for Err {
196198
fn from(err: &LayoutError<'tcx>) -> Self {
197199
match err {
198200
LayoutError::Unknown(..) | LayoutError::ReferencesError(..) => Self::UnknownLayout,
201+
LayoutError::SizeOverflow(..) => Self::SizeOverflow,
199202
err => unimplemented!("{:?}", err),
200203
}
201204
}

compiler/rustc_transmute/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ pub enum Reason {
6464
SrcLayoutUnknown,
6565
/// The layout of dst is unknown
6666
DstLayoutUnknown,
67+
/// The size of src is overflow
68+
SrcSizeOverflow,
69+
/// The size of dst is overflow
70+
DstSizeOverflow,
6771
}
6872

6973
#[cfg(feature = "rustc")]

compiler/rustc_transmute/src/maybe_transmutable/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ mod rustc {
8585
(_, Err(Err::UnknownLayout)) => Answer::No(Reason::DstLayoutUnknown),
8686
(Err(Err::Unspecified), _) => Answer::No(Reason::SrcIsUnspecified),
8787
(_, Err(Err::Unspecified)) => Answer::No(Reason::DstIsUnspecified),
88+
(Err(Err::SizeOverflow), _) => Answer::No(Reason::SrcSizeOverflow),
89+
(_, Err(Err::SizeOverflow)) => Answer::No(Reason::DstSizeOverflow),
8890
(Ok(src), Ok(dst)) => {
8991
MaybeTransmutableQuery { src, dst, scope, assume, context }.answer()
9092
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![crate_type = "lib"]
2+
#![feature(transmutability)]
3+
mod assert {
4+
use std::mem::BikeshedIntrinsicFrom;
5+
struct Context;
6+
7+
pub fn is_maybe_transmutable<Src, Dst>()
8+
where
9+
Dst: BikeshedIntrinsicFrom<Src, Context>,
10+
{
11+
}
12+
}
13+
14+
fn main() {
15+
pub union Uninit {
16+
a: [u8; usize::MAX],
17+
}
18+
19+
#[repr(C)]
20+
struct ExplicitlyPadded(Uninit);
21+
22+
assert::is_maybe_transmutable::<(), ExplicitlyPadded>();
23+
//~^ ERROR `()` cannot be safely transmuted into `ExplicitlyPadded`
24+
25+
assert::is_maybe_transmutable::<ExplicitlyPadded, ()>();
26+
//~^ ERROR `ExplicitlyPadded` cannot be safely transmuted into `()`
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0277]: `()` cannot be safely transmuted into `ExplicitlyPadded` in the defining scope of `assert::Context`
2+
--> $DIR/issue-115402-overflow-size.rs:22:41
3+
|
4+
LL | assert::is_maybe_transmutable::<(), ExplicitlyPadded>();
5+
| ^^^^^^^^^^^^^^^^ values of the type `ExplicitlyPadded` are too big for the current architecture
6+
|
7+
note: required by a bound in `is_maybe_transmutable`
8+
--> $DIR/issue-115402-overflow-size.rs:9:14
9+
|
10+
LL | pub fn is_maybe_transmutable<Src, Dst>()
11+
| --------------------- required by a bound in this function
12+
LL | where
13+
LL | Dst: BikeshedIntrinsicFrom<Src, Context>,
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
15+
16+
error[E0277]: `ExplicitlyPadded` cannot be safely transmuted into `()` in the defining scope of `assert::Context`
17+
--> $DIR/issue-115402-overflow-size.rs:25:55
18+
|
19+
LL | assert::is_maybe_transmutable::<ExplicitlyPadded, ()>();
20+
| ^^ values of the type `ExplicitlyPadded` are too big for the current architecture
21+
|
22+
note: required by a bound in `is_maybe_transmutable`
23+
--> $DIR/issue-115402-overflow-size.rs:9:14
24+
|
25+
LL | pub fn is_maybe_transmutable<Src, Dst>()
26+
| --------------------- required by a bound in this function
27+
LL | where
28+
LL | Dst: BikeshedIntrinsicFrom<Src, Context>,
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `is_maybe_transmutable`
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)