rust-lang / rust-lang/rust

Tracking Issue for `#![feature(phantom_variance_markers)]`

Open
#135,806 11 comments 21 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-tracking-issue T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Feature gate: #![feature(phantom_variance_markers)]

This is a tracking issue for phantom variance markers, which are identical to PhantomData but provide a self-documenting variance rather than contrived types such as PhantomData<fn() -> T> to indicate contravariance.

Public API
// in core::marker

pub struct PhantomCovariant<T: ?Sized>(/* ... */);
pub struct PhantomInvariant<T: ?Sized>(/* ... */);
pub struct PhantomContravariant<T: ?Sized>(/* ... */);
pub struct PhantomCovariantLifetime<'a>(/* ... */);
pub struct PhantomInvariantLifetime<'a>(/* ... */);
pub struct PhantomContravariantLifetime<'a>(/* ... */);

pub trait Variance: Sealed {}
impl<T: ?Sized> Variance for PhantomCovariant<T> {}
impl<T: ?Sized> Variance for PhantomInvariant<T> {}
impl<T: ?Sized> Variance for PhantomContravaiant<T> {}
impl<T: ?Sized> Variance for PhantomCovariantLifetime<T> {}
impl<T: ?Sized> Variance for PhantomInvariantLifetime<T> {}
impl<T: ?Sized> Variance for PhantomContravaiantLifetime<T> {}

pub fn variance<T: Variance>() -> T {}

// also the trait impls you would expect; identical to PhantomData
Steps / History
Unresolved Questions
  • Should there be a single Lifetime type or one for each possible variance? The former would mean writing PhantomCovariant<Lifetime<'a>> instead of PhantomCovariantLifetime<'a>, with the API otherwise being identical.
  • Should the values be directly constructable, eliminating the need for ::new()? This would presumably mean adding 4–6 lang items, depending on the previous bullet point.
  • Are we happy exposing the (unstable) rustc_pub_transparent on these, allowing indirect inclusion in repr(transparent) types?

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 the feature-gate description, ACP rust-lang/libs-team#488, and implementation PR #135807. Review the unresolved API questions and the stabilization process linked in the issue. Done means reaching a decision on the listed questions, completing the final comment period, and preparing or reviewing the stabilization PR.

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
Active
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.