Skip to content

Stabilize fixed-length slice patterns (excluding ..) #48836

Closed

Description

@nikomatsakis

This is a sub-issue of the larger tracking issue for slice patterns (#23121). It specifically tracks the plans to stabilize fixed-length slice patterns -- that is, excluding the .. operator (which is entangled with some syntactic questions). There is an implementation PR in #48516.

Summary

Using this feature, a pattern like [a, b, c] can be used to match against fixed-length arrays:

let arr = [1, 2, 3];
let [a, b, c] = arr; // where arr: [T;3]
let [a, ref b, c] = arr; // where arr: [T;3]

One can also use such a pattern to match against slices:

let arr = &[1, 2];
match arr {
    [a, b, c] => { /* this arm will not apply */ }
    [a, b] => { /* this arm will */ }
    _ => { /* this wildcard is required, since we don't know length statically */
}

Notable points and tests

Here are some of the things I think we should be testing. I didn't have time to find if we have all these tests, perhaps someone can help (cc @petrochenkov, @mikhail-m1 ?)

  • test showing irrefutable matches (let [a, b] = ) applied to fixed-length to move things out
  • test showing irrefutable matches (let [a, b] = ) applied to create reference
  • test showing attempt to move from a borrowed array or slice gives error
  • test that wildcard is required when matching slice with fixed-length arrays
  • match-vec-fixed tests that we get warnings for two slice patterns of same length
  • other cases?

Things not stabilized

  • The main thing not stabilized is the ability to create a slice of unmatched items.

Activity

  1. added
    T-langRelevant to the language team
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    on Mar 8, 2018
  2. nikomatsakis commented on Mar 8, 2018

    @nikomatsakis
    ContributorAuthor

    @rfcbot fcp merge

    I propose we merge the subset above. @petrochenkov has a PR here:

    #48516

    I'd like to see us document the tests better, but I think that we have them =)

  3. rfcbot commented on Mar 8, 2018

    @rfcbot

    Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged teams:

    No concerns currently listed.

    Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

    See this document for info about what commands tagged team members can give me.

  4. added
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    on Mar 8, 2018
  5. petrochenkov commented on Mar 8, 2018

    @petrochenkov
    Contributor

    Here are some of the things I think we should be testing. I didn't have time to find if we have all these tests, perhaps someone can help (cc @petrochenkov, @mikhail-m1 ?)

    There are many tests testing slice patterns (at least 67 of them contain feature(slice_patters)), it's hard to find specific examples.
    compile-fail/borrowck contains tests for moving out elements in array patterns erroneously.
    match-vec-*/match-slice-* tests in compile-fail test various pattern properties like exhaustiveness and number of elements.
    #47926 added tests for legally moving out elements from fixed-size arrays and dynamic drops.
    irrefutable-slice-patterns.rs creates references with irrefutable slice patterns.

  6. rfcbot commented on Mar 8, 2018

    @rfcbot

    🔔 This is now entering its final comment period, as per the review above. 🔔

  7. added
    final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
    and removed
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    on Mar 8, 2018
  8. nikomatsakis commented on Mar 9, 2018

    @nikomatsakis
    ContributorAuthor

    @petrochenkov good enough, thanks

  9. rfcbot commented on Mar 18, 2018

    @rfcbot

    The final comment period is now complete.

  10. added a commit that references this issue on Mar 20, 2018
  11. added a commit that references this issue on Jan 18, 2020
  12. added a commit that references this issue on Jan 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language teamfinal-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions