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

Member access expressions #989

Merged
merged 39 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
df43587
Initial design changes for member access expressions
zygoloid Dec 15, 2021
b72d322
Filling out template with PR 989
zygoloid Dec 15, 2021
efe4d73
Rules for member access naming interface members.
zygoloid Dec 16, 2021
9a5da20
Merge branch 'trunk' into proposal-member-access-expres
zygoloid Jan 18, 2022
9e7e021
Text improvements
zygoloid Jan 19, 2022
4ea4f73
Add proposal text.
zygoloid Jan 19, 2022
46dde7f
Undo some damage done by `prettier`.
zygoloid Jan 19, 2022
138470d
Switch from blockquote to bullets to work around prettier bug.
zygoloid Jan 20, 2022
5413d9b
Add some cross-references to proposal background.
zygoloid Jan 21, 2022
93d9563
Address review comments.
zygoloid Jan 22, 2022
9ac1f4d
Apply suggestions from code review
zygoloid Jan 22, 2022
5cf7eb1
Address review comments.
zygoloid Jan 22, 2022
ea19037
Add missing `external`.
zygoloid Jan 22, 2022
0cb68a7
Add another couple of `external`s.
zygoloid Jan 22, 2022
f7f78d2
Address review comment.
zygoloid Jan 22, 2022
5ad6bc3
Summarize member access in a table and use more meaningful names in
zygoloid Jan 24, 2022
6120907
Flesh out "alternatives considered".
zygoloid Jan 24, 2022
aa8d643
Apply suggestions from code review
zygoloid Jan 25, 2022
0652f27
Missing update `a` -> `n` when switching example to nicer names.
zygoloid Jan 25, 2022
eeee8ee
Restructure: add better summary and overview at the start and explain
zygoloid Jan 26, 2022
70903f8
Attempt to clarify in response to review comments.
zygoloid Jan 26, 2022
735abac
Reflow to appease prettier.
zygoloid Jan 26, 2022
c6afd28
Apply suggestions from code review
zygoloid Jan 26, 2022
3ed3a43
Respond to review comments.
zygoloid Jan 26, 2022
a241782
Add elided word back for clarity.
zygoloid Jan 27, 2022
0d169e4
Attempt to clarify that lookup never considers the values of in-scope…
zygoloid Jan 27, 2022
05c41ef
Apply suggestions from code review
zygoloid Feb 1, 2022
babfee7
Clarify that we're not giving an example of a rewrite rule, just an
zygoloid Feb 1, 2022
4d9c39c
Response to review comments.
zygoloid Feb 1, 2022
b9427a4
Remove table of cases; it doesn't seem to pull its weight.
zygoloid Feb 1, 2022
27335b7
Factor out wording about combining lookup results and say that it takes
zygoloid Feb 1, 2022
2c09a73
Expand explanation in example.
zygoloid Feb 2, 2022
207347b
Add discussion of alternative interpretation of `Type.Interface`.
zygoloid Feb 2, 2022
e8a820c
Merge branch 'trunk' into proposal-member-access-expres
zygoloid Feb 2, 2022
751f02e
Fix trailing whitespace.
zygoloid Feb 2, 2022
a3bddec
Respond to review comments.
zygoloid Mar 2, 2022
dfab250
Merge branch 'trunk' into proposal-member-access-expres
zygoloid Mar 2, 2022
3f80189
Attempt to fix mermaid error: don't use underscore in node name.
zygoloid Mar 2, 2022
3ae955c
Fix '->' typo in mermaid diagram.
zygoloid Mar 2, 2022
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
86 changes: 85 additions & 1 deletion docs/design/expressions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

- [Overview](#overview)
- [Precedence](#precedence)
- [Names](#names)
- [Unqualified names](#unqualified-names)
- [Qualified names and member access](#qualified-names-and-member-access)
- [Operators](#operators)
- [Conversions and casts](#conversions-and-casts)
- [`if` expressions](#if-expressions)
Expand Down Expand Up @@ -54,6 +57,13 @@ graph BT
braces["{...}"]
click braces "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/classes.md#literals"

unqualifiedName["x"]
click unqualifiedName "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/README.md#unqualified-names"

memberAccess>"x.y<br>
x.(...)"]
click memberAccess "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/member_access.md"

as["x as T"]
click as "https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/implicit_conversions.md"

Expand All @@ -79,7 +89,8 @@ graph BT

expressionEnd["x;"]

as & not --> parens & braces
memberAccess --> parens & braces & unqualifiedName
as & not --> memberAccess
comparison --> as
and & or --> comparison & not
if & expressionEnd --> and & or
Expand Down Expand Up @@ -113,6 +124,79 @@ The diagram's attributes are:
- For example, `+` and `-` are left-associative and in the same precedence
group, so `a + b + c - d` is treated as `((a + b) + c) - d`.

## Names

### Unqualified names

An _unqualified name_ is a [word](../lexical_conventions/words.md) that is not a
keyword and is not preceded by a period (`.`).

**TODO:** Name lookup rules for unqualified names.

### Qualified names and member access

A _qualified name_ is a word that is prefixed by a period. Qualified names
appear in the following contexts:

- [Designators](/docs/design/classes.md#literals): `.` _word_
- [Direct member access expressions](member_access.md): _expression_ `.`
_word_

```
var x: auto = {.hello = 1, .world = 2};
^^^^^ ^^^^^ qualified name
^^^^^^ ^^^^^^ designator

x.hello = x.world;
^^^^^ ^^^^^ qualified name
^^^^^^^ ^^^^^^^ member access expression
```

Qualified names refer to members of an entity determined by the context in which
the expression appears. For a member access, the entity is named by the
expression preceding the period. In a struct literal, the entity is the struct
type. For example:

```
package Foo api;
namespace N;
fn N.F() {}

fn G() {
// Same as `(Foo.N).F()`.
// `Foo.N` names namespace `N` in package `Foo`.
// `(Foo.N).F` names function `F` in namespace `N`.
Foo.N.F();
josh11b marked this conversation as resolved.
Show resolved Hide resolved
}

// `.n` refers to the member `n` of `{.n: i32}`.
fn H(a: {.n: i32}) -> i32 {
// `a.n` is resolved to the member `{.n: i32}.n`,
// and names the corresponding subobject of `a`.
return a.n;
}

fn J() {
// `.n` refers to the member `n of `{.n: i32}`.
H({.n = 5 as i32});
}
```

Member access expressions associate left-to-right. If the member name is more
complex than a single _word_, an indirect member access expression can be used,
with parentheses around the member name:
Comment on lines +185 to +187
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

Suggested change
Member access expressions associate left-to-right. If the member name is more
complex than a single _word_, an indirect member access expression can be used,
with parentheses around the member name:
Member access expressions associate left-to-right. If the member name is more
complex than a single _word_, a member access _expression_ can be used,
with parentheses around the member name:

The word "indirect" for me adds more confusion than help, but the fact that it becomes an expression helps me. WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see you really use these terms more heavily in the detailed design. I'll follow up there. This may not make sense to deal with until that is sorted.


- _expression_ `.` `(` _member-access-expression_ `)`

```
interface I { fn F[me: Self](); }
class X {}
external impl X as I { fn F[me: Self]() {} }

// `x.I.F()` would mean `(x.I).F()`.
fn Q(x: X) { x.(I.F)(); }
```

## Operators

Most expressions are modeled as operators:
Expand Down
Loading