Skip to content

Clarify layout of tuples when last field is unsized #1952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Darksonn
Copy link
Contributor

@Darksonn Darksonn commented Jul 29, 2025

This is a follow-up to rust-lang/rust#137728.

Note that we only removed coercions, which is what relied on the layout matching. Tuple types with unsized last field are still legal in the language:

use core::ptr::slice_from_raw_parts;

fn main() {
    let array = [1u8; 20];

    let ptr = slice_from_raw_parts(array.as_ptr(), 20 - size_of::<i32>());
    println!("{:?}", unsafe { &*(ptr as *const (i32, [u8])) });
}
(16843009, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

playground

One way to rationalize this is that it's not different from the fact that these two types might have incompatible layouts:

// A and B may have incompatible layouts

struct A(i32, [u8; 20]);
struct B(i32, [u8]);

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: The marked PR is awaiting review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants