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

*mut "uses" type params, but *const (and therefore NonNull) does not #118976

Closed
eggyal opened this issue Dec 15, 2023 · 9 comments · Fixed by #127871
Closed

*mut "uses" type params, but *const (and therefore NonNull) does not #118976

eggyal opened this issue Dec 15, 2023 · 9 comments · Fixed by #127871
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eggyal
Copy link
Contributor

eggyal commented Dec 15, 2023

Update(fmease): See #118976 (comment) and #118976 (comment).


As first mentioned on URLO, and demonstrated on quick playground:

struct Foo<T>(*mut Self); // works
struct Bar<T>(*const Self); // error[E0392]: parameter `T` is never used

Is this a bug, or is there some obscure reason for it?

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 15, 2023
@Darksonn
Copy link
Contributor

Note that the use of Self is not the problem:

struct Foo<T>(*mut Foo<T>); // works
struct Bar<T>(*const Bar<T>); // error[E0392]: parameter `T` is never used

@fmease fmease added the A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) label Dec 15, 2023
@fmease
Copy link
Member

fmease commented Dec 15, 2023

*mut is invariant over its pointee type while *const is covariant over it. Foo is invariant over T because invariance “overrules” bivariance (all to-be-inferred terms start out as bivariant). Bar is bivariant over T (i.e., T is “unused”) since covariance gets “overruled” by bivariance. Sorry for the super technical explanation. I don't really see a bug though 🤔

Similar example:

struct Foo<T: 'static>(&'static mut Foo<T>); // works
struct Bar<T: 'static>(&'static Bar<T>); // errors

You can remedy this with PhantomData and a wrapper type that renders T invariant, e.g., struct Bar<T>(*const Bar<T>, PhantomData<*mut T>); should be just fine.

@eggyal
Copy link
Contributor Author

eggyal commented Dec 15, 2023

Ah, thank you. I had thought it might be variance-related and did refresh my memory with a read of the chapters on subtyping in the reference and nomicon but neither mention "bivariance" at all nor how variance relates to whether a type parameter is deemed to be "used" in a type definition.

Perhaps this is already documented somewhere that I missed, but if not maybe this is a documentation bug?

@fmease
Copy link
Member

fmease commented Dec 15, 2023

The rustc dev guide mentions bivariance but that one isn't really user-facing as its name suggests. Not sure why the Reference and the Nomicon don't mention it, maybe because it's considered to be an implementation detail?

@eggyal
Copy link
Contributor Author

eggyal commented Dec 15, 2023

Can it really be an implementation detail if its effect is user facing ?

@fmease
Copy link
Member

fmease commented Dec 15, 2023

Yeah, makes sense. Probably worth opening an issue over at the Reference and/or the Nomicon. It's an implementation detail insofar that rustc never mentions bivariance in any error messages for example and only talks about generic parameters “that must be used”.

@eggyal
Copy link
Contributor Author

eggyal commented Dec 15, 2023

Okay, thanks again. Will open an issue as advised. Closing here.

@eggyal eggyal closed this as not planned Won't fix, can't repro, duplicate, stale Dec 15, 2023
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 17, 2023
@eggyal
Copy link
Contributor Author

eggyal commented Dec 20, 2023

Yes, that's a helpful clarification. Perhaps that could be added into the explanation of E0392?

@fmease fmease added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-error-codes Area: Explanation of an error code (--explain) labels Dec 20, 2023
@fmease fmease reopened this Dec 20, 2023
@fmease fmease added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 20, 2023
@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints and removed A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-error-codes Area: Explanation of an error code (--explain) labels Jul 17, 2024
@bors bors closed this as completed in 65de5d0 Jul 19, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jul 19, 2024
Rollup merge of rust-lang#127871 - compiler-errors:recursive, r=estebank

Mention that type parameters are used recursively on bivariance error

Right now when a type parameter is used recursively, even with indirection (so it has a finite size) we say that the type parameter is unused:

```
struct B<T>(Box<B<T>>);
```

This is confusing, because the type parameter is *used*, it just doesn't have its variance constrained. This PR tweaks that message to mention that it must be used *non-recursively*.

Not sure if we should actually mention "variance" here, but also I'd somewhat prefer we don't keep the power users in the dark w.r.t the real underlying issue, which is that the variance isn't constrained. That technical detail is reserved for a note, though.

cc `@fee1-dead`

Fixes rust-lang#118976
Fixes rust-lang#26283
Fixes rust-lang#53191
Fixes rust-lang#105740
Fixes rust-lang#110466
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants