Skip to content

Commit e7955b0

Browse files
committed
emphasize that deref patterns are only a proposal, and fix typo
1 parent 3ec08d4 commit e7955b0

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

text/3637-guard-patterns.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,10 @@ This can also be seen as a special case of the previous argument, as pattern mac
252252

253253
## Deref and Const Patterns Must Be Pure, But Not Guards
254254

255-
It may seem odd that we explicitly require `deref!` and const patterns to use pure `Deref` and `PartialEq` implementations, respectively, but allow arbitrary side effects in guards. The ultimate reason for this is that, unlike `deref!` and const patterns, guard patterns are always refutable.
255+
It may seem odd that we explicitly require const patterns to use pure `PartialEq` implementations (and the upcoming [proposal](https://hackmd.io/4qDDMcvyQ-GDB089IPcHGg) for deref patterns to use pure `Deref` implementations), but allow arbitrary side effects in guards. The ultimate reason for this is that, unlike const patterns and the proposed deref patterns, guard patterns are always refutable.
256256

257-
With `deref!` patterns, we can write an impure `Deref` impl which alternates between returning `true` or `false` to get UB:
258-
```rust
259-
match EvilBox::new(false) {
260-
deref!(true) => {} // Here the `EvilBox` dereferences to `false`.
261-
deref!(false) => {} // And here to `true`.
262-
}
263-
```
264257

265-
And similarly, without the requirement of `StructuralPartialEq` we could write a `PartialEq` implementation which always returns `false`:
258+
Without the requirement of `StructuralPartialEq` we could write a `PartialEq` implementation which always returns `false`, resulting either in UB or a failure to ensure match exhaustiveness:
266259

267260
```rust
268261
const FALSE: EvilBool = EvilBool(false);
@@ -274,7 +267,16 @@ match EvilBool(false) {
274267
}
275268
```
276269

277-
However, this is not a problem with guard patterns because they already need a irrefutable alternative anyway.
270+
And similarly, with an impure version of the proposed deref patterns, we could write a `Deref` impl which alternates between returning `true` or `false` to get UB:
271+
272+
```rust
273+
match EvilBox::new(false) {
274+
deref!(true) => {} // Here the `EvilBox` dereferences to `false`.
275+
deref!(false) => {} // And here to `true`.
276+
}
277+
```
278+
279+
However, this is not a problem with guard patterns because they already need an irrefutable alternative anyway.
278280
For example, we could rewrite the const pattern example with guard patterns as follows:
279281

280282
```rust

0 commit comments

Comments
 (0)