-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Change operator precedence #4075
Merged
+168
−7
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
19541c9
Filling out template with PR 4075
josh11b 9593472
Update precedence diagram
josh11b efd9637
Checkpoint progress.
josh11b f7cd6a0
Ready for review
josh11b 27352bd
Merge remote-tracking branch 'upstream/trunk' into prec
josh11b 8d838d1
Add an alternative
josh11b 832a082
Update proposals/p4075.md
josh11b 6dc325c
Incorporate feedback.
josh11b c60c96e
Add alternative considered 06-13
josh11b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# Change operator precedence | ||
|
||
<!-- | ||
Part of the Carbon Language project, under the Apache License v2.0 with LLVM | ||
Exceptions. See /LICENSE for license information. | ||
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
--> | ||
|
||
[Pull request](https://github.com/carbon-language/carbon-lang/pull/4075) | ||
|
||
<!-- toc --> | ||
|
||
## Table of contents | ||
|
||
- [Abstract](#abstract) | ||
- [Problem](#problem) | ||
- [Background](#background) | ||
- [Proposal](#proposal) | ||
- [Details](#details) | ||
- [Rationale](#rationale) | ||
- [Alternatives considered](#alternatives-considered) | ||
- [`as` and `where` could be peers of `if`...`then`...`else`](#as-and-where-could-be-peers-of-ifthenelse) | ||
- [Make `T as I where R` mean `T as (I where R)`](#make-t-as-i-where-r-mean-t-as-i-where-r) | ||
- [Make fewer changes](#make-fewer-changes) | ||
|
||
<!-- tocstop --> | ||
|
||
## Abstract | ||
|
||
Update the operator precedence to achieve a few goals: | ||
|
||
- Form operators into groups which behave similarly | ||
- Make the group of operators ("top-level operators") that capture everything | ||
to the right, like `if`...`then`...`else`, behave similarly to the left, so | ||
that rearranging expressions won't change how they group. | ||
- Add the `where` operator, used to specify constraints on facet types, to the | ||
precedence chart, to define how it interacts with other operators. | ||
- Make the operator precedence diagram prettier, so that it eventually can be | ||
made into a poster that Carbon programmers can hang on their walls. | ||
|
||
## Problem | ||
|
||
The `where` operator is particularly tricky: | ||
|
||
- It is used in an `impl` declaration to specify the values of associated | ||
constants (such as associated types). In that context, `impl T as I where R` | ||
is interpreted conceptually as `impl T as (I where R)`. It would be nice if | ||
`T as I where R` would mean the same thing in other contexts. | ||
- The `where` operator will frequently be used with the binary `&` operator, | ||
since that is how facet types are combined. It is desirable that | ||
`I & J where R` be interpreted as `(I & J) where R`. This is expected to be | ||
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. Likewise here: considered in isolation, we probably prefer 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. Done. |
||
more common than combinging `where` and `as` outside of an `impl` | ||
josh11b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
declaration. | ||
- The "restriction" on the right side of a `where` uses operators that mean | ||
something else in an expression context: `and`, `==`, `=`. We would like to | ||
minimize the confusion when both kinds of uses of those operators appear in | ||
the same expression. | ||
|
||
These goals are in conflict with the current precedence partial order. | ||
|
||
## Background | ||
|
||
The initial operator precedence approach, including using a partial precedence | ||
ordering instead of a total ordering as found in most languages, was established | ||
by [propsoal #555](https://github.com/carbon-language/carbon-lang/pull/555). | ||
[PR #1070](https://github.com/carbon-language/carbon-lang/pull/1070) established | ||
the current precedence chart, which has been incrementally added to since then. | ||
|
||
## Proposal | ||
|
||
We are making a number of changes: | ||
|
||
- `x as T` is no longer allowed on either side of a comparison operator, or | ||
the short-circuiting operators `and` & `or`. | ||
- `x where R` is a peer to `as`, but its arguments can be binary operators | ||
(like `&`). This matches the comparison operators, which are either illegal | ||
or reinterpreted as an argument to `where`. | ||
- The type constructors `T*` and `const T` are no longer separate from the | ||
other unary operators, and can now be the argument of any binary operator. | ||
|
||
## Details | ||
|
||
Please see the new precedence diagram in | ||
[docs/design/expressions/README.md](/docs/design/expressions/README.md). | ||
|
||
## Rationale | ||
|
||
Precedence is about | ||
[Code that is easy to read, understand, and write](/docs/project/goals.md#code-that-is-easy-to-read-understand-and-write). | ||
We don't want to require parentheses too often since that makes the code harder | ||
to write, and if it goes too far even reading becomes difficult. However, we do | ||
want parentheses to mark code that would otherwise be misinterpreted. This is a | ||
balancing act we expect to have to refine with experience. | ||
|
||
## Alternatives considered | ||
|
||
### `as` and `where` could be peers of `if`...`then`...`else` | ||
|
||
We considered making all the "top-level" operators act the same for precedence, | ||
but we expect users to want to use `as` to force the two branches of an | ||
`if`...`then`...`else` expression to a common type often enough, and we didn't | ||
expect the result of doing that to be confusing to read. | ||
|
||
### Make `T as I where R` mean `T as (I where R)` | ||
|
||
We wanted to make `T as I where R` mean the same facet type as that same | ||
sequence of tokens in an `impl` declaration. However, this was in conflict with | ||
the arguments to `where` being the same as the arguments to comparison | ||
operators. We didn't want to allow an expression mixing binary operators with | ||
`as` since we expected users to expect that to mean performing the operation | ||
with that casted-to type. For example, `x + y as i64` would mean | ||
`(x + y) as i64`, which would perform the addition and only then cast to `i64`, | ||
which is probably not what would be intended by that expression. We thought it | ||
better to make `x + y as i64` illegal to force users to use parentheses, even if | ||
that meant also using parentheses with `T as I where R` in an expression | ||
context. | ||
|
||
### Make fewer changes | ||
|
||
We considered making fewer changes to precedence, but that lead to an operator | ||
precedence diagram with crossing edges (it was | ||
[non-planar](https://en.wikipedia.org/wiki/Planar_graph)). This was felt to be a | ||
sign that the graph was too complex, making it harder for humans to understand | ||
and remember. It was suggested that developers using Carbon may want to have the | ||
precedence graph posted for reference, and a planar graph would make a | ||
more-appealing poster. | ||
|
||
This was | ||
[discussed in open discussion on 2024-06-20](https://docs.google.com/document/d/1s3mMCupmuSpWOFJGnvjoElcBIe2aoaysTIdyczvKX84/edit?resourcekey=0-G095Wc3sR6pW1hLJbGgE0g&tab=t.0#heading=h.p524bg7cnd32). |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe worth noting here: if we don't get the "would be nice" version, we want it to be invalid rather than meaning
(T as I) where R
. That is, considered in isolation, we would preferT as (I where R)
> invalid >(T as I) where R
.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.
Done.