rust-lang / rust-lang/reference

The reference lacks the specification about the variance of lifetime parameters that have different variances in each position

Open
#1,801 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

subtyping.variance.user-composite-types has a wording

If the parameter is used in positions with different variances then the parameter is invariant.

However, it applies only to struct, enum, and union types

subtyping.variance.builtin-composite-types specifies the rules

When used outside of an struct, enum, or union, the variance for parameters is checked at each location separately.

However, it seems to only apply to tuple and function types.

Consider this type

struct A<'a>(&'a ());

type F<'b> = &'b mut A<'b>

The reference lacks the wording to specify the variance of lifetime 'b. That is, in &'b T, 'b is covariant and T is invariant.

It seems that 'b is expected to be invariant in type &'b mut A<'b>.

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 with the linked subtyping.variance.user-composite-types and subtyping.variance.builtin-composite-types sections in the Rust Reference, comparing their stated scope and rules. Clarify how variance is specified for &'b mut A<'b>, including the lifetime and type parameter positions, so the reference fully covers this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.