Skip to content

Commit

Permalink
Updates to generics design details, part 1 (#3231)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
josh11b and zygoloid authored Sep 19, 2023
1 parent e3d3122 commit a8ca499
Show file tree
Hide file tree
Showing 9 changed files with 1,122 additions and 847 deletions.
32 changes: 13 additions & 19 deletions docs/design/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2569,8 +2569,7 @@ class ContactInfo {
>
> - [Aliases](aliases.md)
> - ["Aliasing" in "Code and name organization"](code_and_name_organization/README.md#aliasing)
> - <!-- [`alias` a name from an interface impl](generics/details.md#avoiding-name-collisions) -->
> [`alias` a name from an interface impl](generics/details.md#external-impl)
> - [`alias` a name from an interface impl](generics/details.md#avoiding-name-collisions)
> - [`alias` a name in a named constraint](generics/details.md#named-constraints)
> - Proposal
> [#107: Code and name organization](https://github.com/carbon-language/carbon-lang/pull/107)
Expand Down Expand Up @@ -2803,8 +2802,7 @@ In addition to function requirements, interfaces can contain:
- [requirements that other interfaces be implemented](generics/details.md#interface-requiring-other-interfaces)
or
[interfaces that this interface extends](generics/details.md#interface-extension)
- <!-- [associated facets](generics/details.md#associated-facets) -->
[associated facets](generics/details.md#associated-types) and other
- [associated facets](generics/details.md#associated-facets) and other
[associated constants](generics/details.md#associated-constants)
- [interface defaults](generics/details.md#interface-defaults)
- [`final` interface members](generics/details.md#final-members)
Expand Down Expand Up @@ -2857,16 +2855,14 @@ In this case, `Print` is not a direct member of `Circle`, but:
}
```
<!-- [`extend`](generics/details.md#extend-impl) keyword... -->
To include the members of the interface as direct members of the type, use the
`extend` keyword, as in `extend impl as Printable`. This is only permitted on
`impl` declarations in the body of a class definition.
[`extend`](generics/details.md#extend-impl) keyword, as in
`extend impl as Printable`. This is only permitted on `impl` declarations in the
body of a class definition.
Without `extend`, implementations don't have to be in the same library as the
type definition, subject to the orphan rule
([1](generics/details.md#impl-lookup), [2](generics/details.md#orphan-rule)) for
[coherence](generics/terminology.md#coherence).
type definition, subject to the [orphan rule](generics/details.md#orphan-rule)
for [coherence](generics/terminology.md#coherence).
Interfaces and implementations may be
[forward declared](generics/details.md#forward-declarations-and-cyclic-references)
Expand Down Expand Up @@ -2928,8 +2924,7 @@ fn DrawTies[T:! Renderable & GameResult](x: T) {

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

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

### Templates

Expand Down
8 changes: 4 additions & 4 deletions docs/design/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -1174,11 +1174,11 @@ methods whose implementation may be overridden in a derived class.

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

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

Expand Down
10 changes: 5 additions & 5 deletions docs/design/expressions/implicit_conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- [Data types](#data-types)
- [Same type](#same-type)
- [Pointer conversions](#pointer-conversions)
- [Type-of-types](#type-of-types)
- [Facet types](#facet-types)
- [Consistency with `as`](#consistency-with-as)
- [Extensibility](#extensibility)
- [Alternatives considered](#alternatives-considered)
Expand Down Expand Up @@ -182,11 +182,11 @@ var r: Base** = &p;
*r = q;
```

### Type-of-types
### Facet types

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

## Consistency with `as`
Expand Down
2 changes: 1 addition & 1 deletion docs/design/expressions/member_access.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ resolution.
Multiple lookups can be performed when resolving a member access expression with
a [template binding](#compile-time-bindings). We resolve this the same way as
when looking in multiple interfaces that are
[combined with `&`](/docs/design/generics/details.md#combining-interfaces-by-anding-type-of-types):
[combined with `&`](/docs/design/generics/details.md#combining-interfaces-by-anding-facet-types):

- If more than one distinct member is found, after performing
[`impl` lookup](#impl-lookup) if necessary, the lookup is ambiguous, and the
Expand Down
10 changes: 5 additions & 5 deletions docs/design/generics/appendix-coherence.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ implements interfaces. There are a few main problematic use cases to consider:
`Song` type to support "by title", "by artist", and "by album" orderings.
- Implementing an interface for a type when there is no relationship between
the libraries defining the interface and the type.
- When the implementation of an interface for a type uses an associated type
that can't be referenced from the file or files where the implementation is
- When the implementation of an interface for a type relies on something that
can't be referenced from the file or files where the implementation is
allowed to be defined.

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

0 comments on commit a8ca499

Please sign in to comment.