-
Notifications
You must be signed in to change notification settings - Fork 13k
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
make pub type foo = bar
and pub use bar as foo
interchangable in next edition
#73191
Comments
It it possible there's anyone relying on this for the opposite, exporting a type while intentionally not exporting the constructor? (And not providing a function of the same name.) I guess it doesn't hide |
Maybe a clippy rule suggesting to use pub use instead of pub type for trivial tuple struct reexports would help people avoid these pitfalls in the current edition? |
@scottmcm If someone is relying on that, they could avoid exporting the tuple struct's fields, which makes the constructor private. |
Doesn't |
Based on discussion in the lang team meeting today: We'd like to take this through the new lang team process. (That should just involve moving this issue to the rust-lang/lang-team repository using the major change process template.) This may potentially interact with name resolution. One challenge is that the name resolution code doesn’t (today) have to look at the RHS of a type alias. cc @petrochenkov One common use case for this involves aliases that fill in specific type parameters for generic types: struct Foo<T>(T);
type Bar = Foo<u32>; The result doesn't currently work as a constructor ( |
This won't just interact with name resolution, this is almost entirely a name resolution feature.
will become ill-formed. |
My opinion is that the value side of pub pat PubType(x: u32) = MyStruct(x); That is, pattern aliases, which are always usable as patterns (expanding to the RHS pattern), but also could be allowed to be called as (constructor) functions, if there are no I can't fin a RFCs repo issue/PR about "pattern aliases", but I remember discussing them with several people, and it felt like all of them thought the feature would be useful but it never ended up going through the RFC pipeline. It's probably (far) too late to try and cram this into the 2021 edition, but long-term this is what I would prefer. |
What's the motivation for this? This proposal doesn't make |
The motivation was to reduce edge cases in the language and make the language more intuitive, at least based on my mental model, which is that I'm not going to argue that they aren't different in practice, but any complexity there wasn't apparent to me when I wrote this issue. I've already updated the reference to try to clarify this so maybe this isn't an issue in practice anymore, but I'm guessing we could still put more effort into clarifying the semantic differences between these items. |
Another edge case: glob imports of enum variants work through fn main() {
// Works
// enum Foo {A, B}
enum SecretFoo {
A,
B,
}
type Foo = SecretFoo;
// Works fine
let x = Foo::A;
// Doesn't work
use Foo::*;
let y = A;
} Replacing the glob import with a direct import of the variant ( To be honest, I found this quite surprising. It seems worth documenting at least. Related: obi1kenobi/cargo-semver-checks#413 |
This exact issue just happened in I'm hoping this can still make it into the 2024 edition. If not (and assuming I can find more funding) I'm hoping to add a check for this into |
This is just because of rust-lang/rust#73191. Otherwise, creating instances of the type-aliased versions doesn't work.
This is just because of rust-lang/rust#73191. Otherwise, creating instances of the type-aliased versions doesn't work.
This is just because of rust-lang/rust#73191. Otherwise, creating instances of the type-aliased versions doesn't work.
This is just because of rust-lang/rust#73191. Otherwise, creating instances of the type-aliased versions doesn't work.
Right now renaming a type and re-exporting it via a type alias can be a breaking change whereas re exporting it under the new name is not a breaking change. As far as I know the only difference between the two is how they interact with tuple-struct constructors:
This happens because tuple struct constructors are just free functions with the same ident as the type they're constructing.
use
statements bring in items from all 3 namespaces that match the given ident buttype
items only alias the type itself.This can be fixed by making it so that the compiler generates a constructor for the alias when it sees that the
RHS
of the type alias is a tuple-struct. This would be a breaking change because users can already define a free function with the same name as the type alias, which people do currently do to add the necessary constructor to work around this very issue.To resolve this @joshtriplett has recommended the following steps:
The text was updated successfully, but these errors were encountered: