Skip to content

infer or permit declarations of variance for type parameters #3598

Closed
@nikomatsakis

Description

@nikomatsakis
Contributor

UPDATE

Variance inference is complete.

What is still missing is:

ORIGINAL

Today, we treat all type parameters as invariant, but infer variance for region parameters. We can use that same code which infers the variance for region parameters to infer the suitable variance for type parameters if we like.

Alternatively, if we decide that inference here is too magical, we could require explicit declaration of variance. In that case, i'd favor covariance as the default, and the possibility to write struct Foo<mut T> to indicate an invariant type parameter T (which can appear in mutable locations). This seems more intuitive than the usual + and -. We probably don't need a notation for contravariance, but in that case perhaps fn T (to indicate a type parameter that appears only in function arguments, far and away the most common place to have a contravariant type parameter).

Still, I think I favor inference. It's inline with what we do for regions and I think it will basically match up with user expectations.

Activity

nikomatsakis

nikomatsakis commented on Mar 8, 2013

@nikomatsakis
ContributorAuthor

Do not forget: current region parameterization inference codes does not consider supertrait inheritance.

graydon

graydon commented on Mar 12, 2013

@graydon
Contributor

Can you be a little more concrete about use cases and current failure modes? I'm having a hard time picturing the implications of this.

brson

brson commented on Mar 31, 2013

@brson
Contributor

Doesn't look like 0.6

catamorphism

catamorphism commented on May 24, 2013

@catamorphism
Contributor

Nominating for milestone 3, feature-complete

bblum

bblum commented on Jun 13, 2013

@bblum
Contributor

@graydon, I ran into this today. I had something like:

struct Foo<'self> {
    field1: &'self Bar
    field2: Option<&'self Baz>,
}

...and I needed to update such a struct that I already had with a new value for field2, and it wouldn't let me intersect the lifetimes (even though the subtyping was safe, it needed them to be equal due to invariance). I have a FIXME in a local branch which should show up here when I push it.

As a workaround, I wrote a hand-monomorphized enum that's the same as option but didn't have a type parameter. (I can imagine that would be impossible for someone implementing a generic data structure, though.)

pnkfelix

pnkfelix commented on Jun 27, 2013

@pnkfelix
Member

punting to later triage mtg with niko present.

nikomatsakis

nikomatsakis commented on Jul 11, 2013

@nikomatsakis
ContributorAuthor

Related to #5781

graydon

graydon commented on Jul 11, 2013

@graydon
Contributor

accepted for feature-complete milestone

nikomatsakis

nikomatsakis commented on Jul 11, 2013

@nikomatsakis
ContributorAuthor

@pcwalton thinks this isn't necessary for 1.0. I'm not sure I agree,
particularly because I encountered a lot of compilation errors trying
to fix the whole with the Self type parameter (referenced in my earlier comment).

nikomatsakis

nikomatsakis commented on Oct 28, 2013

@nikomatsakis
ContributorAuthor

As part of the code for #4846 I implemented a general variance inference algorithm, but I do not yet apply it to type parameters.

35 remaining items

modified the milestone: 1.0 on May 8, 2014
brson

brson commented on May 8, 2014

@brson
Contributor

P-high, not 1.0.

added a commit that references this issue on Jul 8, 2014
rust-highfive

rust-highfive commented on Sep 24, 2014

@rust-highfive
Contributor

This issue has been moved to the RFCs repo: rust-lang/rfcs#281

added a commit that references this issue on May 11, 2024

Auto merge of rust-lang#3598 - RalfJung:heap, r=RalfJung

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemE-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.P-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @graydon@brson@nikomatsakis@pcwalton@pnkfelix

      Issue actions

        infer or permit declarations of variance for type parameters · Issue #3598 · rust-lang/rust