Skip to content

Commit d1e0e0a

Browse files
authored
Merge pull request #1771 from ehuss/trait-pattern-edition-rework
Rework trait parameter patterns
2 parents 96618fa + 7cdad30 commit d1e0e0a

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

src/items/traits.md

+53-25
Original file line numberDiff line numberDiff line change
@@ -287,44 +287,71 @@ The [trait implementation] must also begin with the `unsafe` keyword.
287287
r[items.traits.params]
288288
## Parameter patterns
289289

290-
r[items.traits.params.allowed-patterns]
291-
Function or method declarations without a body only allow [IDENTIFIER] or
292-
`_` [wild card][WildcardPattern] patterns. `mut` [IDENTIFIER] is currently
293-
allowed, but it is deprecated and will become a hard error in the future.
290+
r[items.traits.params.patterns-no-body]
291+
Parameters in associated functions without a body only allow [IDENTIFIER] or `_` [wild card][WildcardPattern] patterns, as well as the form allowed by [_SelfParam_]. `mut` [IDENTIFIER] is currently allowed, but it is deprecated and will become a hard error in the future.
294292
<!-- https://github.com/rust-lang/rust/issues/35203 -->
295293

296-
r[items.traits.params.edition2015]
297-
In the 2015 edition, the pattern for a trait function or method parameter is
298-
optional:
299-
300-
```rust,edition2015
301-
// 2015 Edition
294+
```rust
302295
trait T {
303-
fn f(i32); // Parameter identifiers are not required.
296+
fn f1(&self);
297+
fn f2(x: Self, _: i32);
304298
}
305299
```
306300

307-
r[items.traits.params.restriction]
308-
The kinds of patterns for parameters is limited to one of the following:
309-
310-
* [IDENTIFIER]
311-
* `mut` [IDENTIFIER]
312-
* [`_`][WildcardPattern]
313-
* `&` [IDENTIFIER]
314-
* `&&` [IDENTIFIER]
301+
```rust,compile_fail,E0642
302+
trait T {
303+
fn f2(&x: &i32); // ERROR: patterns aren't allowed in functions without bodies
304+
}
305+
```
315306

316-
r[items.traits.params.restriction.edition2018]
317-
Beginning in the 2018 edition, function or method parameter patterns are no
318-
longer optional. Also, all irrefutable patterns are allowed as long as there
319-
is a body. Without a body, the limitations listed above are still in effect.
307+
r[items.traits.params.patterns-with-body]
308+
Parameters in associated functions with a body only allow irrefutable patterns.
320309

321310
```rust
322311
trait T {
323-
fn f1((a, b): (i32, i32)) {}
324-
fn f2(_: (i32, i32)); // Cannot use tuple pattern without a body.
312+
fn f1((a, b): (i32, i32)) {} // OK: is irrefutable
325313
}
326314
```
327315

316+
```rust,compile_fail,E0005
317+
trait T {
318+
fn f1(123: i32) {} // ERROR: pattern is refutable
319+
fn f2(Some(x): Option<i32>) {} // ERROR: pattern is refutable
320+
}
321+
```
322+
323+
r[items.traits.params.pattern-required.edition2018]
324+
> [!EDITION-2018]
325+
> Prior to the 2018 edition, the pattern for an associated function parameter is optional:
326+
>
327+
> ```rust,edition2015
328+
> // 2015 Edition
329+
> trait T {
330+
> fn f(i32); // OK: parameter identifiers are not required
331+
> }
332+
> ```
333+
>
334+
> Beginning in the 2018 edition, patterns are no longer optional.
335+
336+
r[items.traits.params.restriction-patterns.edition2018]
337+
> [!EDITION-2018]
338+
> Prior to the 2018 edition, parameters in associated functions with a body are limited to the following kinds of patterns:
339+
>
340+
> * [IDENTIFIER]
341+
> * `mut` [IDENTIFIER]
342+
> * [`_`][WildcardPattern]
343+
> * `&` [IDENTIFIER]
344+
> * `&&` [IDENTIFIER]
345+
>
346+
> ```rust,edition2015,compile_fail,E0642
347+
> // 2015 Edition
348+
> trait T {
349+
> fn f1((a, b): (i32, i32)) {} // ERROR: pattern not allowed
350+
> }
351+
> ```
352+
>
353+
> Beginning in 2018, all irrefutable patterns are allowed as described in [items.traits.params.patterns-with-body].
354+
328355
r[items.traits.associated-visibility]
329356
## Item visibility
330357
@@ -368,6 +395,7 @@ fn main() {
368395
[_AssociatedItem_]: associated-items.md
369396
[_GenericParams_]: generics.md
370397
[_InnerAttribute_]: ../attributes.md
398+
[_SelfParam_]: functions.md
371399
[_TypeParamBounds_]: ../trait-bounds.md
372400
[_Visibility_]: ../visibility-and-privacy.md
373401
[_WhereClause_]: generics.md#where-clauses

0 commit comments

Comments
 (0)