You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: patterns.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,11 @@ fn is_foo(x: i32) -> bool {
13
13
```
14
14
However, that check has some loopholes, so e.g. `&T` can be used in a pattern no matter the `T`.
15
15
16
-
Any reference type const-pattern is compiled by [calling `PartialEq::eq`][compile-partial-eq] to compare the subject of the `match` with the constant. Const-patterns with other types (enum, struct, tuple, array) are treated as if the constant was inlined as a pattern (and the usual `match` tree is constructed).
16
+
The way this works is that the constant is converted to a [THIR pattern](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir_build/thir/pattern/enum.PatKind.html).
17
+
This proceeds recursively through fields of the constant until we hit a reference.
18
+
Then, `&[u8]` and `&str` are treated specially, while everything else becomes just a `PatKind::Constant`.
19
+
20
+
For further compilation, `Constant` are translated to [`PartialEq::eq`][compile-partial-eq], whereas everything else is translated as if the constant was inlined as a pattern.
17
21
[RFC 1445](https://github.com/rust-lang/rfcs/blob/master/text/1445-restrict-constants-in-patterns.md) lays the groundwork for changing this in the future to always using `PartialEq::eq`, but no decision either way has been made yet.
0 commit comments