Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upRFC: Tuple struct construction with `Self(v1, v2, ..)` #2302
Conversation
Centril
added some commits
Jan 18, 2018
Centril
added
the
T-lang
label
Jan 18, 2018
Centril
changed the title
Rfc/tuple struct self ctor
RFC: Tuple struct construction with `Self(v1, v2, ..)`
Jan 18, 2018
This comment has been minimized.
This comment has been minimized.
Emerentius
commented
Jan 18, 2018
|
The proposal unearths another unexpected limitation. A type alias for a tuple struct can not be used with tuple struct init syntax right now (without struct FooBar(u8);
type BarFoo = FooBar;
impl Default for BarFoo {
fn default() -> Self {
// Self(42) // proposed in rfc
BarFoo(42) // does not compile
// expected function, found type alias `BarFoo`
// BarFoo { 0: 42 } // works
}
} |
This comment has been minimized.
This comment has been minimized.
I think that limitation isn't possible to get around without bc-breaks tho simply because EDIT: perhaps the break wouldn't be all that extensive so it could be lumped into the next epoch..? Personally I think we should improve what we can here =) |
This comment has been minimized.
This comment has been minimized.
burdges
commented
Jan 18, 2018
|
I think epochs could and should be used to deprecate any existing harmful name collisions, like |
This comment has been minimized.
This comment has been minimized.
|
Linking RFC #1506. |
petrochenkov
reviewed
Jan 20, 2018
| impl Default for TheAnswer { | ||
| fn default() -> Self { Self(42) } | ||
| } | ||
| ``` |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Jan 20, 2018
Contributor
If tuple structs are supported, then unit structs can be supported in the same way:
struct TheAnswer;
impl Default for TheAnswer {
fn default() -> Self { Self }
}
This comment has been minimized.
This comment has been minimized.
Centril
Jan 20, 2018
Contributor
I considered it initially but didn't for some reason I can't remember. I can add that to the RFC if you like.
This comment has been minimized.
This comment has been minimized.
chris-morgan
Jan 24, 2018
Member
There are three forms of instantiation (to use the word slightly fuzzily): Self { }, Self() and Self. Self { } already works. You’ve proposed adding Self() for consistency. It doesn’t seem to me that it fixes consistency properly unless Self is also made to work.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Previous discussion: https://internals.rust-lang.org/t/initialization-syntax-for-self-tuple-structs/5389 |
petrochenkov
reviewed
Jan 20, 2018
| ``` | ||
|
|
||
| the compiler will desugar the application by substituting `Self(v0, v1, ..)` | ||
| for `Self { 0: v0, 1: v1, .. }` and then continue on from there. The compiler |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Jan 20, 2018
•
Contributor
This desugaring is not entirely equivalent to what constructors of tuple struct can do. In
struct S(u8);S in value namespace is a constructor function and it can be used in all contexts where a fn(u8) -> S { ... } is accepted, not only direct calls S(arg).
(S)(0u8); // OK
opt_u8.map(S); // OK
(Self)(0u8); // Not OK, not a direct call
opt_u8.map(Self); // Not OK, not a direct callIdeally, Self should be a first-class "function alias" and refer to the constructor function (with appropriate generic parameter substitutions).
This comment has been minimized.
This comment has been minimized.
petrochenkov
Jan 20, 2018
Contributor
Selfshould be a first-class "function alias"
Or a "const alias" for unit structs. "Value alias", in general.
This comment has been minimized.
This comment has been minimized.
Centril
Jan 20, 2018
Contributor
That's strictly more powerful I guess, so I could change the desugaring to go in this direction if we want to support that. Do we want to support that? Why / why not?
This comment has been minimized.
This comment has been minimized.
|
By analogy with #2300 (comment), it's interesting to list what
Inconsistencies:
Future compatibility:
|
This comment has been minimized.
This comment has been minimized.
|
@rfcbot fcp merge |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Jan 25, 2018
•
|
Team member @withoutboats has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
rfcbot
added
the
proposed-final-comment-period
label
Jan 25, 2018
This comment has been minimized.
This comment has been minimized.
|
I'm happy as long as we think this is backwards compatible and works. (@petrochenkov -- do you?) |
This comment has been minimized.
This comment has been minimized.
|
I'm not 100% sure that Currently a struct and its constructor are almost entirely independent items, i.e. you can turn a tuple struct into a braced struct + a separate With name-based resolution you can potentially resolve On the other hand, maybe making structs and their constructors "separable" is not an important goal. |
This comment has been minimized.
This comment has been minimized.
This is interesting. You're saying that right now going from this: pub struct Foo(pub u8);To this: pub struct Foo { bar: u8 }
pub fn Foo(bar: u8) -> Foo { Foo { bar } }Would not be a breaking change. But this RFC would make it a breaking change because anyone who used the constructor as |
This comment has been minimized.
This comment has been minimized.
Well, it's still a breaking change because |
This comment has been minimized.
This comment has been minimized.
|
@scottmcm #![feature(const_fn)]
const fn Foo() -> u8 {
10
}
const C: u8 = Foo();
fn main() {
match 10u8 {
C => println!("match!"),
_ => println!("not match!"),
}
}, we just need to accept non-tuple-struct calls directly in pattern positions. The question is whether we should go in this direction, or "separability" of structs and their constructors is not an important property. |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov How do const functions help there? Wouldn't we need to be able to solve const functions backwards for that to work? e.g. |
This comment has been minimized.
This comment has been minimized.
|
Sounds like "pattern synonyms" / "pattern aliases" / "custom patterns". |
This comment has been minimized.
This comment has been minimized.
|
If its not a non-breaking change today then I'm not very concerned about this RFC changing that personally. |
This comment has been minimized.
This comment has been minimized.
No, it would substitute the body of |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov I don't think that's the case after miri integration (cc @oli-obk). |
This comment has been minimized.
This comment has been minimized.
|
Ok, I guess if we solve the much bigger pattern problem, we'll be able to come up with some solution for |
This comment has been minimized.
This comment has been minimized.
|
Similarly to #2300, I have a concern here that |
This comment has been minimized.
This comment has been minimized.
rfcbot
added
the
final-comment-period
label
Feb 16, 2018
This comment has been minimized.
This comment has been minimized.
|
@rfcbot fcp merge The companion RFC has been merged, having addressed @nrc's concerns. I believe this should be merged as well. |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Apr 5, 2018
•
|
Team member @aturon has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
rfcbot
added
the
proposed-final-comment-period
label
Apr 5, 2018
This comment has been minimized.
This comment has been minimized.
I think this is what we should do (#2302 (comment)). |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov So iirc the consensus from the last lang team meeting was that permitting As you are more familiar with the alternate proposed desugaring would you be willing to update the RFC with a PR against my branch? (Note: as written, the RFC also supports |
Centril
referenced this pull request
May 7, 2018
Open
Tracking issue for `impl Trait` (RFC 1522, RFC 1951, RFC 2071) #34511
Centril
added some commits
Jun 21, 2018
This comment has been minimized.
This comment has been minimized.
|
Patched the RFC to support unit structs and use the value namespace ( |
petrochenkov
reviewed
Jun 21, 2018
| ``` | ||
| pat : ... // <-- The original grammar of `pat` prior to this RFC. | ||
| | SELF '(' ')' | ||
| | SELF '(' pat_tup ')' |
This comment has been minimized.
This comment has been minimized.
petrochenkov
Jun 21, 2018
Contributor
The grammar doesn't change, technically, it's still PATH ( ... ) and Self is already a valid path.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
LGTM. |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Jun 21, 2018
|
|
rfcbot
added
final-comment-period
and removed
proposed-final-comment-period
labels
Jun 21, 2018
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Jul 1, 2018
|
The final comment period, with a disposition to merge, as per the review above, is now complete. |
rfcbot
added
finished-final-comment-period
and removed
final-comment-period
labels
Jul 1, 2018
Centril
referenced this pull request
Jul 2, 2018
Closed
Tracking issue for RFC 2302, Tuple struct construction with `Self(v1, v2, ..)` #51994
Centril
merged commit d391d85
into
rust-lang:master
Jul 2, 2018
This comment has been minimized.
This comment has been minimized.
|
Huzzah! This RFC is merged! Tracking issue: rust-lang/rust#51994 |
Centril commentedJan 18, 2018
•
edited
Rendered
Tracking issue
Tuple
structs can now be constructed and pattern matched withSelf(v1, v2, ..). A simple example:Similarly, unit structs can also be constructed and pattern matched with
Self.