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

Document the consequences of various PhantomData forms #30069

Closed
elinorbgr opened this issue Nov 26, 2015 · 12 comments
Closed

Document the consequences of various PhantomData forms #30069

elinorbgr opened this issue Nov 26, 2015 · 12 comments

Comments

@elinorbgr
Copy link
Contributor

To work around the parameter T is never used [E0392] error, one if often suggested to use PhantomData.

However, the documentation of PhantomData explains that various forms are possible, but is quite vague about the consequences of the choice of a form (and also only handles a small subset of the possible forms).

From my understanding given this reddit thread I opened these various forms can have different consequences (regarding dopck and/or lifetime variance ?):

  • PhantomData<T>
  • PhantomData<&'a T>
  • PhantomData<&'a mut T>
  • PhantomData<*const T>
  • PhantomData<*mut T>
  • PhantomData<fn(T) -> ()>
  • PhantomData<fn() -> T>
  • PhantomData<fn(T) -> T>

But I have no real idea about what using one or another changes.

@GuillaumeGomez
Copy link
Member

cc @Manishearth
cc @steveklabnik

@Manishearth
Copy link
Member

cc @gankro

@elinorbgr
Copy link
Contributor Author

I had forgotten to check the rustonomicon, and this page seems relevant.

Could be worth explaining the relation of PhantomData with variance and add a link to it in the doc, maybe ?

@Gankra
Copy link
Contributor

Gankra commented Nov 26, 2015

  • PhantomData<T> - drop check, variant over T
  • PhantomData<&'a T> - variant over 'a, variant over T
  • PhantomData<&'a mut T> - variant over 'a, invariant over T
  • PhantomData<*const T> - variant over T
  • PhantomData<*mut T> - invariant over T
  • PhantomData<fn(T) -> ()> - contravariant over T, assuming we keep that -- invariant otherwise
  • PhantomData<fn() -> T> - drop check, variant over T (a function that produces T from nothing is isomorphic to T)
  • PhantomData<fn(T) -> T> - invariant over T?

The standard widget for "invariant over 'a" is Cell<&'a ()>.

@durka
Copy link
Contributor

durka commented Nov 26, 2015

I wonder if there is already a crate that provides aliases to those various forms of PhantomData, appropriately named for a bit more self-documentation. If not I have a sudden desire to publish one :)

@Gankra
Copy link
Contributor

Gankra commented Nov 26, 2015

I wouldn't recommend it -- it's what we used to have, but it was really confusing and no one could remember. The current system of "use what you semantically have" works a lot better.

@bluss
Copy link
Member

bluss commented Nov 26, 2015

According to the dropck rfc, PhantomData<fn() -> T> has no ownership / dropck implications. link

@steveklabnik
Copy link
Member

Yeah, so I'm not sure what the best way is to expand this. The first sentence is

PhantomData allows you to describe that a type acts as if it stores a value of type T, even though it does not.

Which does cover all of these forms. It's just that.

@Manishearth
Copy link
Member

Yeah, but it doesn't explain why you would use it, or what features can be inherited from those types.

There's some dropck and variance stuff, but there's nowhere that people can go to to read about it.

@Gankra
Copy link
Contributor

Gankra commented Jan 4, 2016

Well, there's stuff in the nomicon.

@Manishearth
Copy link
Member

We should link to it from the phantomdata docs then

steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 7, 2016
steveklabnik added a commit to steveklabnik/rust that referenced this issue Jan 8, 2016
@Rufflewind
Copy link
Contributor

It’d be nice if the nomicon had Gankro’s neat table though!

Rufflewind added a commit to Rufflewind/rust that referenced this issue Feb 25, 2017
Original: rust-lang#30069 (comment)

Testing confirms that:

  - PhantomData<fn() -> T> does not actually enable drop checking.
  - PhantomData<fn(T) -> T> is neither variant nor contravariant.
eddyb added a commit to eddyb/rust that referenced this issue Feb 25, 2017
Add Gankro's table to nomicon/src/phantom-data.md

Original: rust-lang#30069 (comment)

Testing confirms that:

  - `PhantomData<fn() -> T>` does not actually enable drop checking.
  - `PhantomData<fn(T) -> T>` is neither variant nor contravariant.
eddyb added a commit to eddyb/rust that referenced this issue Feb 25, 2017
Add Gankro's table to nomicon/src/phantom-data.md

Original: rust-lang#30069 (comment)

Testing confirms that:

  - `PhantomData<fn() -> T>` does not actually enable drop checking.
  - `PhantomData<fn(T) -> T>` is neither variant nor contravariant.
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

No branches or pull requests

8 participants