-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Inconsistent Self behavior with generic structs #69306
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
The |
To be 100% clear here: I believe what @petrochenkov is saying is that the compiler has a bug, and that the bug is that the code impl<T> _Bar<T> {
fn _map<U>(x: U) -> _Bar<U> {
Self(x)
}
} should be treated as a short-hand for: impl<T> _Bar<T> {
fn _map<U>(x: U) -> _Bar<U> {
_Bar::<T>(x)
}
} and thus should be yielding the same error that one sees for the However, the |
T-compiler triage: leaving nominated for T-lang. Tagging as P-high priority to resolve whether the semantics described by @petrochenkov is indeed what we want, and if so, how to address deploying a breaking-change to fix it. |
cc @eddyb I thought we had fixed this bug in #61896 (and to be clear, it's clearly a bug (as noted by @petrochenkov in #69306 (comment)). It should not compile. |
Just to simplify a bit (still passes but shouldn't): struct _Bar<T>(T);
impl<T> _Bar<T> {
fn _map1(x: u8) -> _Bar<u8> {
Self(x)
}
} |
Just checked on godbolt and it seems to compile in every stable release since it was stabilized ( |
That this code was accepted was a compiler bug. See: rust-lang/rust#69306
Uh oh!
There was an error while loading. Please reload this page.
Summary from @pnkfelix :
Self
used as a tuple-struct constructor function should use the fully type-substituted form from theimpl
block, but does not.Thus, today this code is accepted, but it should not be.
(Fixing this would be a breaking-change, so we should take care in how we deploy it.)
Original bug report follows:
I tried this code:
In nightly rustc,
_Bar
compiles while_Foo
yields a type mismatch error:The text was updated successfully, but these errors were encountered: