rust-lang / rust-lang/rust

There should be a way to make a type as "stably a 1-ZST" for repr(transparent) purposes

Open
#155,925 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Currently, if one crate defines a 1-ZST with private fields, and then another crate tries to use that in a repr(transparent) type, that produces an error:

// Crate A
pub struct MyMarkerType((());
// Crate B

#[repr(transparent)]
struct Transparent(i32, A::MyMarkerType);

This warning is caused by https://github.com/rust-lang/rust/issues/78586. The intention of the warning is to guard against a semver hazard: if crate A adds non-zero-sized fields to MyMarkerType in the future, that would break compilation of crate B.

However, what if crate A actually wants to promise that it will never add such fields? Currently it has no way to express this, the lint will always fire for types with private fields in other crates. This is on track to becoming a hard error (https://github.com/rust-lang/rust/issues/78586).

See here for where @CAD97 originally brought up this issue: a type like this is defined by the generativity crate.

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

Start by reading issue #78586 and the linked discussion in pull request #155299, then examine how repr(transparent) handles private fields across crates. Define a way for a crate to promise that a type remains a 1-ZST without weakening the semver protection. Done means the proposal is specified and the compiler accepts the intended cross-crate use while still rejecting unpromised types.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.