Skip to content

Variance's PrivateItems assoc const is accessible in subtraits #158654

Description

@obi1kenobi

A surprising new SemVer breakage case was discovered earlier this week, affecting sealed traits' associated items (consts, types, functions with or without receivers — everything) being accessible to subtraits. This is non-obvious and usually isn't intended by the authors of such code, which can lead to accidental breakage.

The standard library has an at-risk pattern here, where the sealing trait's associated item is accessible downstream: playground

#![feature(phantom_variance_markers)]

use core::marker::{PhantomCovariant, Variance};

fn leaked_value<T: Variance>() -> T {
    // This is actually `private_items::PrivateItems::VALUE`,
    // which we can't actually name directly.
    T::VALUE
}

fn main() {
    let _: PhantomCovariant<u8> = leaked_value();
}

If Variance were stabilized, PrivateItems::VALUE might also become stabilized. This seems possibly unintentional. Linking the tracking issue to get a backlink here: #135806.

portable-simd has the same issue, filed separately here.

Hat tip to @jhpratt for looping me in to look at the SemVer breakage and suggesting that we scan the standard library for this pattern. I used AI tools for the scan, and to generate the code snippet above. I wrote this issue fully by hand otherwise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-crate-compatArea: Impacting SemVer compatibility of crates in the ecosystemA-visibilityArea: Visibility / privacyC-discussionCategory: Discussion or questions that doesn't represent real issues.I-lang-radarItems that are on lang's radar and will need eventual work or consideration.T-langRelevant to the language teamT-libs-apiRelevant to the library API 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