Skip to content

Commit bcd03b2

Browse files
committed
rfc, tuple-struct-self-ctor: reword reference a bit and simplify pattern semantics.
1 parent d7272b4 commit bcd03b2

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

text/0000-tuple-struct-self-ctor.md

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,6 @@ should work and will probably try at some point.
224224
# Reference-level explanation
225225
[reference-level-explanation]: #reference-level-explanation
226226

227-
With this RFC implemented,
228-
rust allows usage of `Self(v0, v1, ..)` inside inherent and
229-
trait `impl`s of tuple structs, either when mentioning the tuple struct
230-
directly in the `impl` header, or via a type alias.
231-
This usage is permitted both in expression and pattern contexts.
232-
233-
Furthermore, `Self`, when referring to a tuple struct, can be legally coerced
234-
into an `fn` pointer which accepts and returns expressions of the same type as
235-
the function pointer `Self` is referring to accepts.
236-
237-
A unit struct defined as `struct $ident;` can similarly be referred
238-
to as by `Self` in expression and pattern contexts inside inherent
239-
and trait `impl`s.
240-
241227
## Grammar
242228

243229
Given:
@@ -262,30 +248,24 @@ expr : ... // <-- Original grammar of `expr`.
262248
;
263249
```
264250

265-
## Semantics - Expression contexts
266-
267-
The semantics of the syntax `Self(x_0, .., x_n)` is defined by adding into the
268-
value namespace, if `Self` refers to a tuple struct, a function:
269-
270-
```rust
271-
const fn Self(x_0: τ_0, .. x: τ_n) -> Self {
272-
Self { 0: x_0, .. n: x_n }
273-
}
274-
```
251+
## Semantics
275252

276-
The semantics of `Self` is defined by adding into the value namespace,
277-
if `Self` refers to a unit struct:
253+
When entering one of the following contexts, a Rust compiler will extend
254+
the value namespace with `Self` which maps to the tuple constructor `fn`
255+
in the case of tuple struct, or a constant, in the case of a unit struct:
278256

279-
```rust
280-
const Self: Self = Self {};
281-
```
257+
+ inherent `impl`s where the `Self` type is a tuple or unit struct
258+
+ `trait` `impl`s where the `Self` type is a tuple or unit struct
282259

283-
## Semantics - Pattern contexts
260+
As a result, when referring to a tuple struct, `Self` can be legally coerced
261+
into an `fn` pointer which accepts and returns expressions of the same type as
262+
the function pointer `Self` is referring to accepts.
284263

285-
In a pattern context, the pattern `Self(x_0, .., x_n)` is desugared to the
286-
pattern `Self { 0: v0, 1: v1, .. }`. Similarly, the pattern `Self` is desugared
287-
to `Self {}`. A rust compiler is free to use different mechanics as these
288-
semantics are preserved.
264+
Another consequence is that `Self(p_0, .., p_n)` and `Self` become
265+
legal patterns. This works since `TupleCtor(p_0, .., p_n)` patterns are
266+
handled by resolving them in the value namespace and checking that they
267+
resolve to a tuple constructor. Since by definition, `Self` referring
268+
to a tuple struct resolves to a tuple constructor, this is OK.
289269

290270
## Implementation notes
291271

0 commit comments

Comments
 (0)