Skip to content

Commit 8f15022

Browse files
authored
Merge pull request #2453 from shepmaster/standard-rust-style
Correct typos and use standard Rust style for `dyn Trait` RFCs
2 parents 9922ce4 + e84191b commit 8f15022

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

text/2113-dyn-trait-syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Because it makes traits and trait objects appear indistinguishable. Some specifi
2525
- `impl MyTrait {}` is valid syntax, which can easily be mistaken for adding default impls of methods or adding extension methods or some other useful operation on the trait itself. In reality, it adds inherent methods to the trait object.
2626
- Function types and function traits only differ in the capitalization of one letter. This leads to function pointers `&fn ...` and function trait objects `&Fn ...` differing only in one letter, making it very easy to mistake one for the other.
2727

28-
Making one of these mistakes typically leads to an error about the trait not implemeting Sized, which is at best misleading and unhelpful. It may be possible to produce better error messages today, but the compiler can only do so much when most of this "obviously wrong" syntax is technically legal.
28+
Making one of these mistakes typically leads to an error about the trait not implementing Sized, which is at best misleading and unhelpful. It may be possible to produce better error messages today, but the compiler can only do so much when most of this "obviously wrong" syntax is technically legal.
2929

3030
### favors a feature that is not more frequently used than its alternatives
3131

text/2250-finalize-impl-dyn-syntax.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ stabilization of these features.
1313
[motivation]: #motivation
1414

1515
Current priority of `+` in `impl Trait1 + Trait2` / `dyn Trait1 + Trait2` brings
16-
inconsistency in the type grammar.
16+
inconsistency in the type grammar.
1717
This RFC outlines possible syntactic
1818
alternatives and suggests one of them for stabilization.
1919

@@ -28,7 +28,7 @@ contexts where they are used inside of unary operators `&(impl Trait1 + Trait2)`
2828
prefix, e.g. `&(Trait1 + Trait2)`.
2929

3030
Additionally, parentheses are required in all cases where `+` in `impl` or `dyn`
31-
is ambiguous.
31+
is ambiguous.
3232
For example, `Fn() -> impl A + B` can be interpreted as both
3333
`(Fn() -> impl A) + B` (low priority plus) or `Fn() -> (impl A + B)` (high
3434
priority plus), so we are refusing to disambiguate and require explicit
@@ -67,8 +67,8 @@ bring inconsistency and may break intuition.
6767

6868
Pros:
6969
- The greedy parsing with high priority of `+` after `impl` / `dyn`
70-
has one benefit - it requires the least amout of parentheses from all the
71-
alternatives.
70+
has one benefit - it requires the least amount of parentheses from all the
71+
alternatives.
7272
Parentheses are needed only when the greedy behaviour needs to be prevented,
7373
e.g. `Fn() -> &(dyn Write) + Send`, this doesn't happen often.
7474

@@ -86,7 +86,7 @@ work, as if `impl` were an unary operator.
8686
Basically, `impl A + B` is parsed using same rules as `A + B`.
8787

8888
If `impl A + B` is located inside a higher priority operator like `&` it has
89-
to be parenthesized.
89+
to be parenthesized.
9090
If it is located at intersection of type and expressions
9191
grammars like `expr1 as Type + expr2`, it has to be parenthesized as well.
9292

@@ -96,23 +96,21 @@ grammars like `expr1 as Type + expr2`, it has to be parenthesized as well.
9696
One location must be mentioned specially, the location in a function return
9797
type:
9898
```rust
99-
fn f() -> impl A + B
100-
{
99+
fn f() -> impl A + B {
101100
// Do things
102101
}
103102
```
104103
This is probably the most common location for `impl Trait` types.
105104
In theory, it doesn't require parentheses in any way - it's not inside of an
106-
unary operator and it doesn't cross expression boundaries.
107-
However, it creates a bit of percieved inconsistency with function-like traits
105+
unary operator and it doesn't cross expression boundaries.
106+
However, it creates a bit of perceived inconsistency with function-like traits
108107
and function pointers that do require parentheses for `impl Trait` in return
109108
types (`Fn() -> (impl A + B)` / `fn() -> (impl A + B)`) because they, in their
110-
turn, can appear inside of unary operators and casts.
109+
turn, can appear inside of unary operators and casts.
111110
So, if avoiding this is considered more important than ergonomics, then
112111
we can require parentheses in function definitions as well.
113112
```rust
114-
fn f() -> (impl A + B)
115-
{
113+
fn f() -> (impl A + B) {
116114
// Do things
117115
}
118116
```
@@ -130,7 +128,7 @@ is not a valid syntax.
130128
## Alternative 3: Unary operator
131129

132130
`impl` and `dyn` can become usual unary operators in type grammar like `&` or
133-
`*const`.
131+
`*const`.
134132
Their application to any other types except for (possibly parenthesized) paths
135133
(single `A`) or "legacy trait objects" (`A + B`) becomes an error, but this
136134
could be changed in the future if some other use is found.
@@ -140,8 +138,7 @@ could be changed in the future if some other use is found.
140138

141139
Function definitions with `impl A + B` in return type have to be rewritten too.
142140
```rust
143-
fn f() -> impl(A + B)
144-
{
141+
fn f() -> impl(A + B) {
145142
// Do things
146143
}
147144
```
@@ -151,7 +148,7 @@ Pros:
151148
- `impl` / `dyn` are usual unary operators, `dyn (A + B)` is a valid syntax.
152149

153150
Cons:
154-
- The largest amount of parentheses, parentheses are always required.
151+
- The largest amount of parentheses, parentheses are always required.
155152
Parentheses are noise, there may be even less desire to use `dyn` in trait
156153
objects now, if something like `Box<Write + Send>` turns into
157154
`Box<dyn(Write + Send)>`.
@@ -175,15 +172,15 @@ after `impl` / `dyn`.
175172

176173
Alternative 2 can be backward-compatibly extended to "relaxed 3" in which
177174
parentheses like `dyn (A + B)` are permitted, but technically unnecessary.
178-
Such parens may keep people expecting `dyn (A + B)` to work happy, but
175+
Such parenthesis may keep people expecting `dyn (A + B)` to work happy, but
179176
complicate parsing by introducing more ambiguities to the grammar.
180177

181178
While unary operators like `&` "obviously" have higher priority than `+`,
182-
cases like `Fn() -> impl A + B` are not so obvious.
179+
cases like `Fn() -> impl A + B` are not so obvious.
183180
The Alternative 2 considers "low priority plus" to have lower priority than `Fn`
184181
, so `Fn() -> impl A + B` can be treated as `(Fn() -> impl A) + B`, however
185182
it may be more intuitive and consistent with `fn` items to make `+` have higher
186-
priority than `Fn` (but still lower priority than `&`).
183+
priority than `Fn` (but still lower priority than `&`).
187184
As an immediate solution we refuse to disambiguate this case and treat
188185
`Fn() -> impl A + B` as an error, so we can change the rules in the future and
189186
interpret `Fn() -> impl A + B` (and maybe even `Fn() -> A + B` after long
@@ -199,9 +196,9 @@ changes required to move to Alternatives 2 and 3. Alternative 2 requires fewer
199196
changes compared to Alternative 3.
200197

201198
As the RFC author interprets it, the Alternative 3 turns out to be impractical
202-
due to common use of `Box`es and other contexts where the parens are technically
203-
unnecessary, but required by Alternative 3.
204-
The number of parens required by Alternative 2 is limited and they seem
199+
due to common use of `Box`es and other contexts where the parenthesis are technically
200+
unnecessary, but required by Alternative 3.
201+
The number of parenthesis required by Alternative 2 is limited and they seem
205202
appropriate because they follow "normal" priorities for unary and binary
206203
operators.
207204

0 commit comments

Comments
 (0)