Skip to content

Commit a8ca499

Browse files
josh11bzygoloid
andauthored
Updates to generics design details, part 1 (#3231)
First step in updating `docs/design/generics/details.md`. It incorporates changes from proposals: #989 #2138 #2173 #2200 #2360 #2964 #3162 , but there are still more changes from those proposals to be made. It also switches away from suggesting static-dispatch witness tables, and creates an appendix to describe that decision. --------- Co-authored-by: Richard Smith <[email protected]>
1 parent e3d3122 commit a8ca499

File tree

9 files changed

+1122
-847
lines changed

9 files changed

+1122
-847
lines changed

docs/design/README.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,8 +2569,7 @@ class ContactInfo {
25692569
>
25702570
> - [Aliases](aliases.md)
25712571
> - ["Aliasing" in "Code and name organization"](code_and_name_organization/README.md#aliasing)
2572-
> - <!-- [`alias` a name from an interface impl](generics/details.md#avoiding-name-collisions) -->
2573-
> [`alias` a name from an interface impl](generics/details.md#external-impl)
2572+
> - [`alias` a name from an interface impl](generics/details.md#avoiding-name-collisions)
25742573
> - [`alias` a name in a named constraint](generics/details.md#named-constraints)
25752574
> - Proposal
25762575
> [#107: Code and name organization](https://github.com/carbon-language/carbon-lang/pull/107)
@@ -2803,8 +2802,7 @@ In addition to function requirements, interfaces can contain:
28032802
- [requirements that other interfaces be implemented](generics/details.md#interface-requiring-other-interfaces)
28042803
or
28052804
[interfaces that this interface extends](generics/details.md#interface-extension)
2806-
- <!-- [associated facets](generics/details.md#associated-facets) -->
2807-
[associated facets](generics/details.md#associated-types) and other
2805+
- [associated facets](generics/details.md#associated-facets) and other
28082806
[associated constants](generics/details.md#associated-constants)
28092807
- [interface defaults](generics/details.md#interface-defaults)
28102808
- [`final` interface members](generics/details.md#final-members)
@@ -2857,16 +2855,14 @@ In this case, `Print` is not a direct member of `Circle`, but:
28572855
}
28582856
```
28592857
2860-
<!-- [`extend`](generics/details.md#extend-impl) keyword... -->
2861-
28622858
To include the members of the interface as direct members of the type, use the
2863-
`extend` keyword, as in `extend impl as Printable`. This is only permitted on
2864-
`impl` declarations in the body of a class definition.
2859+
[`extend`](generics/details.md#extend-impl) keyword, as in
2860+
`extend impl as Printable`. This is only permitted on `impl` declarations in the
2861+
body of a class definition.
28652862
28662863
Without `extend`, implementations don't have to be in the same library as the
2867-
type definition, subject to the orphan rule
2868-
([1](generics/details.md#impl-lookup), [2](generics/details.md#orphan-rule)) for
2869-
[coherence](generics/terminology.md#coherence).
2864+
type definition, subject to the [orphan rule](generics/details.md#orphan-rule)
2865+
for [coherence](generics/terminology.md#coherence).
28702866
28712867
Interfaces and implementations may be
28722868
[forward declared](generics/details.md#forward-declarations-and-cyclic-references)
@@ -2928,8 +2924,7 @@ fn DrawTies[T:! Renderable & GameResult](x: T) {
29282924

29292925
> References:
29302926
>
2931-
> - <!-- [Combining interfaces by anding facet types](generics/details.md#combining-interfaces-by-anding-facet-types) -->
2932-
> [Combining interfaces by anding type-of-types](generics/details.md#combining-interfaces-by-anding-type-of-types)
2927+
> - [Combining interfaces by anding facet types](generics/details.md#combining-interfaces-by-anding-facet-types)
29332928
> - Question-for-leads issue
29342929
> [#531: Combine interfaces with `+` or `&`](https://github.com/carbon-language/carbon-lang/issues/531)
29352930
> - Proposal
@@ -3559,12 +3554,11 @@ function.
35593554

35603555
Carbon interfaces with no C++ equivalent, such as
35613556
[`CommonTypeWith(U)`](#common-type), may be implemented for C++ types
3562-
out-of-line in Carbon code. To satisfy the orphan rule
3563-
([1](generics/details.md#impl-lookup), [2](generics/details.md#orphan-rule)),
3564-
each C++ library will have a corresponding Carbon wrapper library that must be
3565-
imported instead of the C++ library if the Carbon wrapper exists. **TODO:**
3566-
Perhaps it will automatically be imported, so a wrapper may be added without
3567-
requiring changes to importers?
3557+
out-of-line in Carbon code. To satisfy the
3558+
[orphan rule](generics/details.md#orphan-rule), each C++ library will have a
3559+
corresponding Carbon wrapper library that must be imported instead of the C++
3560+
library if the Carbon wrapper exists. **TODO:** Perhaps it will automatically be
3561+
imported, so a wrapper may be added without requiring changes to importers?
35683562

35693563
### Templates
35703564

docs/design/classes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,11 +1174,11 @@ methods whose implementation may be overridden in a derived class.
11741174

11751175
Only methods defined in the scope of the class definition may be virtual, not
11761176
any defined in
1177-
[external interface `impl` declarations](/docs/design/generics/details.md#external-impl).
1177+
[out-of-line interface `impl` declarations](/docs/design/generics/details.md#out-of-line-impl).
11781178
Interface methods may be implemented using virtual methods when the
1179-
[impl is internal](/docs/design/generics/details.md#implementing-interfaces),
1180-
and calls to those methods by way of the interface will do virtual dispatch just
1181-
like a direct call to the method does.
1179+
[impl is inline](/docs/design/generics/details.md#inline-impl), and calls to
1180+
those methods by way of the interface will do virtual dispatch just like a
1181+
direct call to the method does.
11821182

11831183
[Class functions](#class-functions) may not be declared virtual.
11841184

docs/design/expressions/implicit_conversions.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
1919
- [Data types](#data-types)
2020
- [Same type](#same-type)
2121
- [Pointer conversions](#pointer-conversions)
22-
- [Type-of-types](#type-of-types)
22+
- [Facet types](#facet-types)
2323
- [Consistency with `as`](#consistency-with-as)
2424
- [Extensibility](#extensibility)
2525
- [Alternatives considered](#alternatives-considered)
@@ -182,11 +182,11 @@ var r: Base** = &p;
182182
*r = q;
183183
```
184184

185-
### Type-of-types
185+
### Facet types
186186

187-
A type `T` with [type-of-type](../generics/terminology.md#facet-type) `TT1` can
188-
be implicitly converted to the type-of-type `TT2` if `T`
189-
[satisfies the requirements](../generics/details.md#subtyping-between-type-of-types)
187+
A type `T` with [facet type](../generics/terminology.md#facet-type) `TT1` can be
188+
implicitly converted to the facet type `TT2` if `T`
189+
[satisfies the requirements](../generics/details.md#subtyping-between-facet-types)
190190
of `TT2`.
191191

192192
## Consistency with `as`

docs/design/expressions/member_access.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ resolution.
385385
Multiple lookups can be performed when resolving a member access expression with
386386
a [template binding](#compile-time-bindings). We resolve this the same way as
387387
when looking in multiple interfaces that are
388-
[combined with `&`](/docs/design/generics/details.md#combining-interfaces-by-anding-type-of-types):
388+
[combined with `&`](/docs/design/generics/details.md#combining-interfaces-by-anding-facet-types):
389389

390390
- If more than one distinct member is found, after performing
391391
[`impl` lookup](#impl-lookup) if necessary, the lookup is ambiguous, and the

docs/design/generics/appendix-coherence.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ implements interfaces. There are a few main problematic use cases to consider:
3636
`Song` type to support "by title", "by artist", and "by album" orderings.
3737
- Implementing an interface for a type when there is no relationship between
3838
the libraries defining the interface and the type.
39-
- When the implementation of an interface for a type uses an associated type
40-
that can't be referenced from the file or files where the implementation is
39+
- When the implementation of an interface for a type relies on something that
40+
can't be referenced from the file or files where the implementation is
4141
allowed to be defined.
4242

4343
These last two cases are highlighted as concerns in Rust in
@@ -208,9 +208,9 @@ This has some downsides:
208208
varies instead of being known statically.
209209
- It is slower to execute from dynamic dispatch and the inability to inline.
210210
- In some cases it may not be feasible to use dynamic dispatch. For example,
211-
if an interface method returns an associated type, we might not know the
212-
calling convention of the function without knowing some details about the
213-
type.
211+
if the return type of an interface method involves an associated constant,
212+
we might not know the calling convention of the function without knowing
213+
some details about the value of that constant.
214214
215215
As a result, this doesn't make sense as the default behavior for Carbon based on
216216
its [goals](/docs/project/goals.md). That being said, this could be a feature

0 commit comments

Comments
 (0)