Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement variance RFC #738 #22286

Closed
wants to merge 34 commits into from

Conversation

nikomatsakis
Copy link
Contributor

This PR:

  • converts the markers to the new PhantomData/PhantomFn scheme;
  • corrects various errors in variance inference, which was not always taking the correct things into account;
  • integrates results variance into trait matching, coherence, subtyping.

This is a [breaking-change]:

  • The old markers are removed in favor of PhantomData. It is possible to simulate all of the old markers:
    • ContravariantLifetime<'a> -> PhantomData<&'a ()> (or just &'a (), actually)
    • InvariantLifetime<'a> -> PhantomData<Cell<&'a ()>>
    • CovariantType<T> -> PhantomData<T>
    • ContravariantType<T> -> PhantomData<fn(T)>
    • InvariantType<T> -> PhantomData<Cell<T>>
  • It is now illegal to have unused type and lifetime parameters. Typically the best fix is either to remove the parameter or to add an instance of PhantomData to your struct (or PhantomFn / MarkerTrait to your trait). See Support variance for type parameters rfcs#738 for more background.
  • Technically, variance can cause some impls that used to be legal to be illegal due to subtyping. This is highly unlikely to affect you.

Fixes #22212.

@rust-highfive
Copy link
Collaborator

r? @Aatch

(rust_highfive has picked a reviewer for you, use r? to override)

@nikomatsakis
Copy link
Contributor Author

r? @pnkfelix has been my go-to variance guy.

@rust-highfive rust-highfive assigned pnkfelix and unassigned Aatch Feb 13, 2015
@nikomatsakis
Copy link
Contributor Author

Grr, something in the last rebase started causing some (minor looking) test failures. I'll track those down.

@@ -96,7 +96,7 @@ pub struct Arena<'longer_than_self> {
head: RefCell<Chunk>,
copy_head: RefCell<Chunk>,
chunks: RefCell<Vec<Chunk>>,
_invariant: marker::InvariantLifetime<'longer_than_self>,
_marker: marker::PhantomData<*mut &'longer_than_self()>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space after lifetime name is missing ?

@nikomatsakis
Copy link
Contributor Author

Rebased. Make check passes locally now.

@frewsxcv
Copy link
Member

Needs a rebase

/// FIXME. Better documentation needed here!
#[cfg(not(stage0))]
#[lang="phantom_data"]
pub struct PhantomData<T:?Sized>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you go ahead an mark this #[stable] as well? I think it'd been debated enough that it's probably earned it. (you may want to leave PhantomFn explicitly #[unstable] for now though).

@pnkfelix
Copy link
Member

@nikomatsakis okay i'm done with the review. Some nits and minor questions, but I would not object to you telling bors that this PR as-is is r=pnkfelix.

into variance inference; fix various bugs in variance inference
so that it considers the correct set of constraints; modify infer to
consider the results of variance inference for type arguments.
…nd ownership,

and also follows the API of `NonZero` a bit more closely. More to do
here I think (including perhaps a new name).
here.  Some of this may have been poorly rebased, though I tried to be
careful and preserve the spirit of the test.
@aturon aturon mentioned this pull request Feb 18, 2015
91 tasks
@alexcrichton
Copy link
Member

@bors: r=pnkfelix 9f8b9d6

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Feb 19, 2015
Conflicts:
	src/librustc/middle/infer/combine.rs
	src/librustc_typeck/check/wf.rs
@alexcrichton
Copy link
Member

This was merged in #22541

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking issue for Support variance for type parameters (RFC 738)
7 participants