rust-lang / rust-lang/rust

`offset_of_slice` exposes whether a custom DST has a private slice field

Open
#138,327 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug F-offset_of_slice T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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 3ea711f17e3946ac3f4d)

@rustbot label F-offset_of_slice C-bug

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by reproducing the provided program on the referenced nightly compiler, then trace the compiler's handling of offset_of! and privacy for custom DST fields. Done means both the private slice and private dyn-tail variants are rejected, with a regression test covering the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.