Skip to content

offset_of_slice exposes whether a custom DST has a private slice field #138327

Open
@kpreid

Description

@kpreid

The following program compiles:

#![feature(offset_of_slice)]

mod foo {
    pub struct Inner {
        foo: u8,
        tail: [u8],
        // tail: dyn core::fmt::Debug,
    }
    pub struct Outer {
        pub dst: Inner,
    }
}

fn main() {
    assert_eq!(core::mem::offset_of!(foo::Outer, dst), 0);
}

It does not compile if the type of tail is changed to a dyn type. I believe neither version should compile, because tail is a private field, and therefore information about its type should not leak to places where it is not visible.

rustc version: 1.87.0-nightly (2025-03-09 3ea711f)

@rustbot label F-offset_of_slice C-bug

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.F-offset_of_slice`#![feature(offset_of_slice)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions