Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix how trailing list subpatterns are cached #3278

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions accepted/3.0/patterns/feature-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Author: Bob Nystrom

Status: Accepted

Version 2.32 (see [CHANGELOG](#CHANGELOG) at end)
Version 2.33 (see [CHANGELOG](#CHANGELOG) at end)

Note: This proposal is broken into a couple of separate documents. See also
[records][] and [exhaustiveness][].
Expand Down Expand Up @@ -3389,8 +3389,9 @@ To bind invocation keys in a pattern `p` using parent invocation `i`:

3. Else `s` is a non-rest element after the rest element:

1. Let `e` be `i : ("tail[]", [index])` where `index` is the
zero-based index of this element subpattern.
1. Let `e` be `i : ("tail[]", [tailIndex])` where `tailIndex` is
the number of element subpatterns following this element
subpattern.

*Note the "tail" in the invocation key name. This is to
distinguish elements after a rest element at some position from
Expand All @@ -3409,6 +3410,20 @@ To bind invocation keys in a pattern `p` using parent invocation `i`:
the third element of the same list. So we use an invocation key
of "tail[]" for `c` and "[]" for `d`.*

*We use `tailIndex` and count backwards from the end so that
trailing elements can be cached across patterns, as in:*

```dart
switch (list) {
case [..., var a, var b]: ...
case [..., var c]: ...
}
```

*Here, `var b` and `var c` have the same `tailIndex` (0), so
the second case will use the previously cached list element
value for `var c`.(

2. Bind `e` to the `[]` invocation for `s`.

3. Bind invocations in the element subpattern using parent `e`.
Expand Down Expand Up @@ -3535,6 +3550,11 @@ Here is one way it could be broken down into separate pieces:

## Changelog

### 2.33 (after shipping)

- Tweak caching of trailing list elements after a rest element. The specified
behavior now follows the implementations (#2922).

### 2.32

- Prohibit variable and identifier patterns from being named `when` or `as`,
Expand Down