-
Notifications
You must be signed in to change notification settings - Fork 40
feat: add first draft of drop order in or patterns clarification #631
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
base: main
Are you sure you want to change the base?
Changes from all commits
2dbb847
59e9df0
c7f7717
98f4d99
d72e16e
b7e026b
900088b
aba5ea5
c236af6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3741,6 +3741,15 @@ tests for a less-than-or-equals relationship. | |||||
| :dp:`fls_ft5aeo4ilgwc` | ||||||
| See :s:`LessThanOrEqualsExpression`. | ||||||
|
|
||||||
|
|
||||||
| .. _fls_DdZ1ZwjLZTeG: | ||||||
|
|
||||||
| let binding | ||||||
| ^^^^^^^^^^^ | ||||||
|
|
||||||
| :dp:`fls_sw6HrsxsnG2y` | ||||||
| A :dt:`let binding` is a :t:`construct` that introduces :t:`[binding]s` by matching a :t:`value` against a :t:`pattern` using the keyword ``let``. :t:`[let binding]s` appear in :t:`[let statement]s`, :t:`[if let expression]s`, :t:`[while let loop expression]s`, and :t:`[let initializer]s`. | ||||||
|
|
||||||
| .. _fls_hqj80jHcxEBB: | ||||||
|
|
||||||
| let initializer | ||||||
|
|
@@ -4713,6 +4722,18 @@ opt-out trait bound | |||||
| An :dt:`opt-out trait bound` is a :t:`trait bound` with :s:`Punctuation` ``?`` | ||||||
| that nullifies an implicitly added :t:`trait bound`. | ||||||
|
|
||||||
|
|
||||||
| .. _fls_LnPDQW3bnNUw: | ||||||
|
|
||||||
| or-pattern | ||||||
| ^^^^^^^^^^ | ||||||
|
|
||||||
| :dp:`fls_LnPDQW3bnNUw` | ||||||
| An :dt:`or pattern` is a :t:`pattern` that matches on one of two or more :t:`[pattern-without-alternation]s` and or-s two or more :t:`[subpattern]s` using character 0x7C (vertical line). | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe
Suggested change
I realized that I suspect that we have other such misuses in the Patterns chapter 😢 |
||||||
|
|
||||||
| :dp:`fls_urIJ5JNHLhm6` | ||||||
| See :s:`Pattern`. | ||||||
|
|
||||||
| .. _fls_gllzixm9yt9w: | ||||||
|
|
||||||
| outer attribute | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -695,7 +695,12 @@ When a :t:`drop scope` is left, all :t:`[value]s` associated with that | |||||
| :t:`drop scope` are :t:`dropped` as follows: | ||||||
|
|
||||||
| * :dp:`fls_g07zq3n55094` | ||||||
| :t:`[Binding]s` are :t:`dropped` in reverse declaration order. | ||||||
| All other :t:`bindings` are :t:`dropped` in reverse declaration order. | ||||||
|
|
||||||
| * :dp:`fls_W2S2FrkuedYC` | ||||||
| :t:`[Binding]s` introduced by an :t:`or-pattern` are dropped in reverse | ||||||
| declaration order, where the declaration order is defined by the first | ||||||
| :t:`subpattern`. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe
Suggested change
depending on what we agree above. |
||||||
|
|
||||||
| * :dp:`fls_a5tmilqxdb6f` | ||||||
| :t:`Temporaries <temporary>` are :t:`dropped` in reverse creation order. | ||||||
|
|
@@ -748,3 +753,27 @@ proceeds as follows: | |||||
| } | ||||||
| let c = PrintOnDrop("2"); | ||||||
|
|
||||||
| :dp:`fls_THzA0QFdMMJB` | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try to rework into step-by-step explanation as in the example under Drop Order.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Attempted this here: aba5ea5
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks good to me. 👍 What do others think? |
||||||
| When an :t:`or pattern` is used, the drop order of :t:`[binding]s` is determined by the first :t:`pattern-without-alternation`, regardless of which matches at runtime. | ||||||
|
|
||||||
| :dp:`fls_dhfIPP4yR3Tt` | ||||||
| In the following example, the drop order is ``b``, ``a`` for both calls. | ||||||
| Dropping proceeds as follows: | ||||||
|
|
||||||
| #. :dp:`fls_zxFM7EoE2Xq8` | ||||||
| The first :t:`pattern-without-alternation` ``Ok([a, b])`` declares ``a`` before ``b``. | ||||||
|
|
||||||
| #. :dp:`fls_093YxG6YXQz2` | ||||||
| When the first call matches ``Ok([a, b])``, the :t:`[binding]s` are dropped in reverse declaration order: ``b`` then ``a``. | ||||||
|
|
||||||
| #. :dp:`fls_gNWXh61ZXXt8` | ||||||
| When the second call matches ``Err([b, a])``, the drop order remains ``b`` then ``a``, determined by the first :t:`pattern-without-alternation`. | ||||||
|
|
||||||
| .. code-block:: rust | ||||||
|
|
||||||
| fn drop_order<T>((Ok([a, b]) | Err([b, a])): Result<[T; 2], [T; 2]>) {} | ||||||
|
|
||||||
| drop_order(Ok([PrintOnDrop("1"), PrintOnDrop("2")])); | ||||||
|
|
||||||
| drop_order(Err([PrintOnDrop("2"), PrintOnDrop("1")])); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,6 +41,9 @@ Patterns | |||||||||||||||||||||||||||
| A :t:`pattern` is a :t:`construct` that matches a :t:`value` which satisfies all | ||||||||||||||||||||||||||||
| the criteria of the :t:`pattern`. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| :dp:`fls_VQMmveZUfNTn` | ||||||||||||||||||||||||||||
| An :t:`or pattern` is a :t:`pattern` that matches on one of two or more :t:`[pattern-without-alternation]s` and or-s two or more :t:`[subpattern]s` using character 0x7C (vertical line). | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line may need to be updated, depending on what we agree for the glossary entry. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| :dp:`fls_mp6i4blzexnu` | ||||||||||||||||||||||||||||
| A :t:`pattern-without-alternation` is a :t:`pattern` that cannot be alternated. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -65,18 +68,24 @@ of a :t:`pattern-without-range`, except for when the :t:`pattern` is ``&mut`` | |||||||||||||||||||||||||||
| :s:`Identifier`. Such a :t:`pattern` is interpreted as a :t:`reference pattern` | ||||||||||||||||||||||||||||
| with :t:`keyword` ``mut`` containing an :t:`identifier pattern`. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| :dp:`fls_72JHo343O7jp` | ||||||||||||||||||||||||||||
| An or-pattern shall not appear in the :t:`pattern-without-alternation` of a | ||||||||||||||||||||||||||||
| :t:`closure parameter`, a :t:`function parameter`, or a :t:`let binding`. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| :dp:`fls_8luyomzppck` | ||||||||||||||||||||||||||||
| Any two :t:`[pattern-without-alternation]s` that are or-ed using character 0x7C | ||||||||||||||||||||||||||||
| (vertical line) are subject to the following restrictions: | ||||||||||||||||||||||||||||
| Any two :t:`[subpattern]s` of an :t:`or-pattern` are subject to the following | ||||||||||||||||||||||||||||
| restrictions: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| * :dp:`fls_rpvdfmy3n05a` | ||||||||||||||||||||||||||||
| The :t:`[type]s` of the two :t:`[pattern-without-alternation]s` shall be | ||||||||||||||||||||||||||||
| The :t:`[type]s` of the two :t:`[subpattern]s` shall be | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe
Suggested change
I realize I may have mislead you during the first review. Sorry for that! 😁 |
||||||||||||||||||||||||||||
| :t:`unifiable`. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| * :dp:`fls_YDVgFaTQwcL8` | ||||||||||||||||||||||||||||
| The set of :t:`[binding]s` introduced by the two :t:`[subpattern]s` shall be the same. | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| * :dp:`fls_kv533rntni1x` | ||||||||||||||||||||||||||||
| The :t:`[binding]s` of the two :t:`[pattern-without-alternation]s` shall | ||||||||||||||||||||||||||||
| be the same, shall have :t:`[unifiable type]s`, and shall have the same | ||||||||||||||||||||||||||||
| :t:`[binding mode]s`. | ||||||||||||||||||||||||||||
| Any two :t:`[binding]s` with the same name in the two :t:`[subpattern]s` shall have | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here.
Suggested change
|
||||||||||||||||||||||||||||
| :t:`[unifiable type]s` and shall have the same :t:`[binding mode]s`. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| .. _fls_uh76pw6ykd57: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -1296,6 +1305,9 @@ Pattern Matching | |||||||||||||||||||||||||||
| :t:`Pattern matching` that involves a :t:`pattern` and a context :t:`value` | ||||||||||||||||||||||||||||
| proceeds as follows: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #. :dp:`fls_tZJgZDWVChJV` | ||||||||||||||||||||||||||||
| If the pattern is an :t:`or-pattern`, then perform or-pattern matching. | ||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #. :dp:`fls_67ajub7d2b4c` | ||||||||||||||||||||||||||||
| For each :t:`pattern-without-alternation` of the :t:`pattern`: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
@@ -1350,6 +1362,25 @@ proceeds as follows: | |||||||||||||||||||||||||||
| Only the :t:`[binding]s` of a matched :t:`pattern-without-alternation` are | ||||||||||||||||||||||||||||
| introduced into a :t:`binding scope`. | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| .. _fls_VsBXBj4AqCj1: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Or-pattern Matching | ||||||||||||||||||||||||||||
| ~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| :dp:`fls_njpiXkgi8Ryb` | ||||||||||||||||||||||||||||
| Or-pattern matching of an :t:`or-pattern` of the form `constructor(or-pattern, rest)`, | ||||||||||||||||||||||||||||
|
Comment on lines
+1368
to
+1373
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
| where `constructor` is an arbitrary constructor, `or-pattern` is the :t:`or-pattern`, | ||||||||||||||||||||||||||||
| and `rest` is optionally a remaining pattern, proceeds as follows: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #. :dp:`fls_nE7qZpHy9TPu` | ||||||||||||||||||||||||||||
| Perform pattern matching of the form `constructor(subpattern, rest)`, where | ||||||||||||||||||||||||||||
| `subpattern` is a :t:`subpattern` of the :t:`or-pattern`, starting from the | ||||||||||||||||||||||||||||
| first such :t:`subpattern` and proceeding in declarative order. | ||||||||||||||||||||||||||||
| #. :dp:`fls_P8yB2b5enpw7` | ||||||||||||||||||||||||||||
| Otherwise pattern matching fails. | ||||||||||||||||||||||||||||
|
Comment on lines
+1377
to
+1382
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(reads a little better at the source level) |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| .. _fls_vnai6ag4qrdb: | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| Identifier Pattern Matching | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of things here:
I do not think that a
let bindingis a construct. The construct (the "syntactic thing") is actuallyPatternWithoutAlternation. "let binding" is a semantic notion for certainPatternWithoutAlternations (those that appear in let statements, if let expressions, etc).I do not think that
let initializershould be included in the context list since the syntax ofLetInitializerdoes not contain aPatternWithoutAlternation.How about something along the lines of:
A
let bindingis thebindingintroduced by alet statement, anif let expression, or awhile let loop expression.