Commit Graph
78 Commits
Author SHA1 Message Date
josh11b 09b5ab14d8 Link to Swift undecidable type-system problems (#3394) 2023-11-14 00:15:25 +00:00
josh11bandRichard Smith 88e87b86b9 Updates to generics design details, part 2 (#3253)
Continued from part 1: #3231. Second step updating
`docs/design/generics/details.md`. There remains some work to
incorporate proposal #2200.

- The biggest changes are incorporating much of the text of proposals:
  - #2173
  - #2687
- It incorporates changes from proposals:
  - #989
  - #1178
  - #2138
  - #2200
  - #2360
  - #2964
  - #3162
- It also updates the text to reflect the latest thinking from leads
issues:
  - #996
  - #2153 -- most notably deleting the section on `TypeId`.
- Update to rule for prioritization blocks with mixed type structures
from [discussion on
2023-07-18](https://docs.google.com/document/d/1gnJBTfY81fZYvI_QXjwKk1uQHYBNHGqRLI2BS_cYYNQ/edit?resourcekey=0-ql1Q1WvTcDvhycf8LbA9DQ#heading=h.7jxges9ojgy3)
- Adds reference links to proposals, issues, and discussions relevant to
the text.
- Also tries to use more precise language when talking about
implementations, to avoid confusing `impl` declaration and definitions
with the `impls` operator used in `where` clauses, an issue brought up
in
  - #2495 
  - #2483

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-10-03 00:03:07 +00:00
josh11bandChandler Carruth 58c106010c Updates to design docs to reflect accepted proposals (#3254)
Includes proposals:
- #990
- #2188
- #2138
- #2200
- #2360
- #2760
- #2964
- #3162

Also tries to use more precise language when talking about:
- implementations, to avoid confusing `impl` declaration and definitions
with the `impls` operator used in `where` clauses, an issue brought up
in #2495 and #2483;
- "binding patterns", like `x: i32`, and "bindings" like `x`.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-09-23 15:53:07 +00:00
josh11bandRichard Smith a8ca499450 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 <richard@metafoo.co.uk>
2023-09-19 19:06:47 +00:00
josh11b 9600030a05 Reduce ambiguity in terminology (#3162)
Change terminology away from terms that are ambiguous:

- Reserve "generic type" for types with (compile-time) parameters, like
`Vector` in `Vector(T:! type)`. Don't use that term to refer to `T`, as
it would with
[#2360](https://github.com/carbon-language/carbon-lang/blob/trunk/proposals/p2360.md#terminology).
- Use the term "compile-time" instead of "constant" to mean "template or
symbolic." Expand the term "constant" to include values, such as from
`let` bindings.
2023-08-30 17:13:40 +00:00
josh11bandRichard Smith e15bdbb36c Update generics overview (#3061)
This reflects changes from a number of approved proposals:

- #2138 : "generic" -> "checked generic", "template" -> "template
generic"
- #2360 : "type", "facet type", "facet". Note: I am not using the term
"generic type" from #2360 since that meaning conflicts with the
generally accepted meaning of "generic type" of a type with a
compile-time parameter.
- #2760 / #2770 : internal/external impl -> extending impl
- #2964 : "symbolic constant" and "template constant"

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-08-15 19:58:23 +00:00
0d1e6bd84d Values, variables, pointers, and references (#2006)
Introduce a concrete design for how Carbon values, objects, storage,
variables,
and pointers will work. This includes fleshing out the design for:

- The expression categories used in Carbon to represent values and
objects,
how they interact, and terminology that anchors on their expression
nature.
-   An expression category model for readonly, abstract values that can
    efficiently support function inputs.
- A customization system for value expression representations,
especially as
    seen on function boundaries in the calling convention.
- An expression category model for references instead of a type system
model.
-   How patterns match different expression categories.
-   How initialization works in conjunction with function returns.
- Specific pointer syntax, semantics, and library customization
mechanisms.
- A `const` type qualifier for use when the value expression category
system
    is too abstracted from the underlying objects in storage.

---------

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
Co-authored-by: Adrien Leravat <Pixep@users.noreply.github.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-08-08 07:27:44 +00:00
josh11b 916bb4022a Update generics goals doc (#3060)
This reflects changes from #2138 , plus a number of other updates,
including reflecting that we have decided to include templates in
Carbon.
2023-08-05 05:39:34 +00:00
587ab64d1b Update Generics terminology document (#3048)
This reflects changes from a number of approved proposals:
- #920 : concrete statements about orphan and overlap in Carbon
- #2138 : "generic" -> "checked generic", "template" -> "template
generic"
- #2188 : binding patterns are forbidden in type position
- #2360 : "type", "facet type", "facet". Note: I am not using the term
"generic type" from #2360 since that meaning conflicts with the
generally accepted meaning of "generic type" of a type with a
compile-time parameter.
- #2760 / #2770 : internal/external impl -> extending impl
- #2964 : "symbolic constant" and "template constant"

---------

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-08-02 23:22:15 +00:00
josh11b 2ae8117d62 Update design docs with syntax changes from #2760 (#2866)
This includes:

- Syntax changes from this chart:

| Before                          | After                                   |
| ------------------------------- | --------------------------------------- |
| `class D extends B { ... }`     | `class D { extend base: B; ... }`       |
| `external impl C as Sub;`       | `impl C as Sub;`                        |
| `class C { impl as Sortable; }` | `class C { extend impl as Sortable; }`  |
| `adapter A for C { ... }`       | `class A { adapt C; ... }`              |
| `adapter A extends C { ... }`   | `class A { extend adapt C; ... }`       |
| `interface I { impl as J; }`    | `interface I { require Self impls J; }` |
| `interface I { extends J; }`    | `interface I { extend J; }`             |

- Dropping the syntax for conditionally implemented internal interfaces.

This does not include:

- terminology changes from #2760 ("internal" and "external")
- changes to code, such as explorer, toolchain, language grammars, or other tooling
2023-06-16 14:22:29 -07:00
josh11b f0ead22f0c Allow overlap with a final impl if identical (#2868)
Allow an `impl` to overlap with a `final impl` if they agree on the overlap. Agreement is defined as all values comparing equal, and functions never comparing equal.  Implements the decision in question-for-leads issue #1077.
2023-06-02 16:05:46 -07:00
642fcd3b77 Replace keyword is with impls (#2483)
Use the keyword `impls` instead of `is` when writing a `where` constraint that a type variable needs to implement an interface or named constraint.

What was previously (provisionally) written:
```
fn Sort[T:! Container where .ElementType is Ordered](x: T*);
```
will now be written:
```
fn Sort[T:! Container where .ElementType impls Ordered](x: T*);
```

---------

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-03-22 21:45:32 -07:00
Amr Hesham 21c3f64564 Fix doc rename struct to class (#2648)
Fix class declaration on the doc
2023-03-02 09:48:06 -08:00
Richard Smith 4daaa4866f Rename Type -> type, per #2360. (#2507)
Also make minor updates to the skeletal design in
docs/design/name_lookup.md following #2113, as there are no longer any prelude names that are made available to unqualified name lookup by default.

Add `type` to the keyword list in
docs/design/lexical_conventions/words.md, following #2360.
2023-01-04 14:22:30 -08:00
josh11b 149107965c Clarify that named constraints in place of interfaces (#2479)
Current text was found to be confusing, see [2022-12-02 in #generics-and-templates](https://discord.com/channels/655572317891461132/941071822756143115/1048458269393424405).
2022-12-27 15:44:15 -08:00
josh11b ee998c99ff Clean up some small mistakes (#2451) 2022-12-07 15:12:08 -08:00
josh11b 9165b084f4 Can't override virtual methods in an adapter (#2450)
This is just a consequence of casting to an adapter type doesn't change the data, including the vtable pointer.
2022-12-07 15:11:31 -08:00
josh11b 9c8fd6864e Implement rename me -> self (#2444)
Implements change proposed in #1382

Replaces #1624
2022-12-06 20:17:00 -08:00
josh11bandRichard Smith 571b869f1c Constraints must use Self (#2376)
Require `impl as` constraints in an `interface` or `constraint` definition to mention `Self` implicitly or explicitly. Require `where` clauses to refer to `.Self` directly, or through a designator like `.Foo`.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-12-02 16:11:23 -08:00
josh11bandRichard Smith e29e4cfa29 What can be done with an incomplete interface (#2347)
Clarify what can and cannot be done with a incomplete interface or named constraint.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-11-09 08:37:25 -08:00
Kareem Ergawyandergawy 05b6c4f310 Fix a few typos in generics docs. (#2272)
Fixes a few tiny typos here and there.

Co-authored-by: ergawy <kareem.ergawy@guardsquare.com>
2022-10-07 09:31:38 -07:00
micttyl dc4e968373 Fix Markdown (#2208)
`explicit parameter` to have its closing rendered correctly.

Change all html-like(still supported though) notations into the Markdown style.
2022-09-22 09:01:36 -07:00
josh11b e873678c8f Link to Rust stabilizing Generic Associated Types (GATs) (#2178) 2022-09-13 16:39:19 -07:00
josh11b e4595deb4b Clarify rules around Self and .Self (#2107)
A number of smaller changes grouped together in one proposal:

-   Make `Self` a keyword.
-   Clarify that `Self` refers to the current type in a base class and in impl declarations.
-   Clarify when `.Self` is legal, and what type it has.
-   Also specify that `where` is not an associative operator.
2022-09-08 13:04:58 -07:00
josh11b 4192ee42a6 Misc small fixes (#2123) 2022-08-30 08:25:24 -07:00
David Poliakoff b342fac618 Spelling fix in the generics overview doc (#2016)
Minor usage fix in generics doc
2022-08-16 10:43:39 -07:00
90babcddc1 Revise associated types and interface parameters in Generics terminology (#1972)
Previous text was not clear, see https://discord.com/channels/655572317891461132/941071822756143115/1004607504396861540 .

Co-authored-by: Thomas Heller <thom.heller@gmail.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-08-15 15:00:00 -07:00
Adrien Leravat b7ee1dfa22 docs: fix typo in generics overview.md (#2025)
PR for tiny fix to generics overview doc, as suggest here: https://github.com/carbon-language/carbon-lang/pull/1885/files#r943743464
2022-08-15 11:22:51 -07:00
Krishna Agarwal 674f2257db grammar mistakes (#1915)
Grammar Mistakes
2022-08-05 08:33:24 -07:00
Richard Smith e26bc32343 Change remaining uses of Bool to bool, following #750. (#1901)
The most significant change here is that explorer now uses the chosen spelling
rather than the old `Bool` spelling. Also update a few documentation examples
and some skeletal design docs to use the chosen spelling.
2022-08-03 17:55:34 -07:00
josh11b 64f3284a19 Update links to visible copies of docs (#1786)
I've manually copied some Google docs that were referenced in our design .md files so they may be publicly viewable.
2022-07-27 17:45:05 -07:00
josh11bandJon Ross-Perkins eb2d6cfed3 Update the generic implementation model (#1398)
The previous implementation model involved the caller of a function knowing what impls the function will need. With the addition of blanket impls and specialization to the design, that is in general no longer possible. Instead the caller provides a type which is used as a key to look up the impl, which is expected to occur during monomorphization, though we allow the compiler to employ other strategies when the set of impls is predictable.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-07-26 19:04:26 -07:00
Josh Soref 066b103881 Spelling (#1580)
This PR corrects misspellings identified by the [check-spelling action](https://github.com/marketplace/actions/check-spelling).

The misspellings have been reported at https://github.com/jsoref/carbon-lang/commit/38a1c1640151899fd6da0442a92557f9543b6280#commitcomment-79197316

The action reports that the changes in this PR would make it happy: https://github.com/jsoref/carbon-lang/commit/173c8f9083a68aa61f7cfe94f720f1e5dc7f1ea3

Note: this PR does not include the action. If you're interested in running a spell check on every PR and push, that can be offered separately.

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2022-07-22 16:14:21 -07:00
Ryan Russell aa4ce80907 docs(design): improve readability (#1431)
Readability improvements focused on `docs/design/.md`

Signed-off-by: Ryan Russell <git@ryanrussell.org>
2022-07-19 17:45:45 -04:00
josh11b b83f5c3fc7 Generics: impl forall (#1327)
Implement the decision in #1192 to use this syntax for parameterized impls:

> `impl forall [`_generic parameters_`]` _type_ `as` _constraint_ ...
2022-06-14 19:35:53 -07:00
josh11b eab34bee6b Link to Rust blog post about implied bounds (#1275) 2022-05-17 18:26:16 -07:00
josh11bandRichard Smith 125224ab08 Generic details 10: interface-implemented requirements (#1088)
This proposal:

- Adds support for interfaces requiring other types than `Self` to implement interfaces, as in:
  ```
  interface IntLike {
    impl i32 as As(Self);
    // ...
  }
  ```
- Defines requirements on how to satisfy those requirements that have a `where` clause.
- Extends `observe` declarations to include saying a type implements an interface, so code can provide a proof instead of the compiler having to perform a recursive search.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-05-05 15:30:40 -07:00
josh11bandRichard Smith b9538129a2 Generic details 12: parameterized types (#1146)
This proposal has three main contributions:
- Types with generic parameters have an identity that consists of the types names plus the values of those parameters.
- The parameters of a type may be deduced from a function's argument.
- Types with generic parameters do not support specialization. Instead, a type can delegate to an interface to opt in to allowing specific customization points.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-04-28 16:16:00 -07:00
josh11b 9b028bb743 Destructors (#1154)
Add a way for classes to add custom destructor code:
```
class Class1 {
  // forward declaration
  destructor [me: Self] { ... }
}
// out-of-line definition
destructor Class1 [me: Self] { ... }

class Class2 {
  // can modify `me` as part of destruction
  destructor [addr me: Self*] { ... }
}
base class MyBaseClass {
  virtual destructor [addr me: Self*] { ... }
}
class MyDerivedClass extend MyBaseClass {
  impl destructor [addr me: Self*] { ... }
}
```
and constraints `Concrete`, `Deletable`, `Destructible`, and `TrivialDestructor`.
2022-04-18 17:39:33 -07:00
06174c51f4 Generics details 9: forward declarations (#1084)
Allow interfaces and implementations to be forward declared.

```
// Forward declare interface `F`
interface F;
class C {
  // Forward declare `C` implements `F`
  impl as F;
}
// Definitions corresponding to forward declarations
interface F { ... }
impl C as F { ... }
```

To allow members of interfaces with default definitions to be forward declared, prefix them with the keyword `default`, following #1082.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-04-13 12:53:16 -07:00
db66a4350e Generic details 11: operator overloading (#1144)
Operators rewrite to calls of specific operator interface functions, so you overload an operator for a type by implementing an interface for it. There is a `like` operator for defining a set if implementations for supporting implicit conversions more conveniently.

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-04-08 14:51:49 -07:00
josh11b 3a18ab70f2 Terminology: qualified member access expression (#1142) 2022-03-17 14:08:24 -07:00
josh11b f0d6e3122a Switch generics to using compound/simple member access terminology (#1138)
This is following #1119 .

* Updates terminology.md, overview.md, details.md
2022-03-16 08:29:45 -07:00
josh11b 39571f715e Fix "block block" typo (#1053) 2022-01-27 16:22:11 -08:00
josh11bandRichard Smith 2d567f5824 Generics: Set associated constants using where constraints (#1013)
Change the syntax for setting the associated constants and types in an interface implementation for a type from using `let` declarations as in:
```
class Vector(T:! Type) {
  impl as Iterable {
    let ElementType:! Type = T;
    ...
  }
}
```
to using `where` clauses as in:
```
class Vector(T:! Type) {
  impl as Iterable where .ElementType = T {
    ...
  }
}
```

This is an attempt to simplify by removing redundancy, improve consistency by removing a use of `let` that was different than other examples, and better support forward declaration that a type implements an interface while retaining the information needed for type checking.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-27 11:18:20 -08:00
josh11bandRichard Smith f4e9063b97 Generics details 8: interface default and final members (#990)
Allowing interfaces to define default values for its associated entities. This:

-   Helps with evolution by reducing the changes needed to add new members to an interface.
-   Reduces boilerplate when some value is more common than others.
-   Addresses the gap between the minimum necessary for a type to provide the desired functionality of an interface and the breadth of API that user's desire.

As an alternative, final values can be provided instead, which can't be overridden, but are more predictable for users and may avoid dynamic dispatch overhead in some cases.

Example:
```
// Interface parameter has a default of `Self`
interface Add(Right:! Type = Self) {
  // `AddWith` *always* equals `Right`
  final let AddWith:! Type = Right;
  // `Result` has a default of `Self`
  let Result:! Type = Self;
  fn DoAdd[me: Self](right: Right) -> Result;
}

impl String as Add() {
  // Right == AddWith == Result == Self == String
  fn DoAdd[me: Self](right: Self) -> Self;
}
```

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-21 14:10:20 -08:00
josh11b ad5072b7b8 Link to Swift re: origin of witness table term (#1029) 2022-01-18 11:17:09 -08:00
Jon Meow b7df523dc8 Fix cross-file links in non-proposal files (#1010) 2022-01-07 11:00:21 -08:00
b333f48697 Generics details 6: remove facets (#950)
There were some concerns about facet types leaking out of generic code in return types. Some initial fixes for this were done in [PR #900](https://github.com/carbon-language/carbon-lang/pull/900), but there remain concerns, for example when associated types are involved.

In particular, given an interface method with return type using an associated type, as in:

```
interface Deref {
  let Result:! Type;
  fn DoDeref[me: Self]() -> Result;
}

class IntHandle {
  impl as Deref {
    let Result:! Type = i32;
    fn DoDeref[me: Self]() -> Result { ... }
  }
}
```

Since `Result` has type `Type`, we had the problem that `IntHandle.DoDeref` would have to return `i32 as Type`, instead of the desired `i32`.

We also think we can simplify the model by eliminating the facet type concept and syntax.

This proposal removes facet types, introduces archetypes in their place, clarifies how associated types work outside of a generic function, and specifies how a generic `let` statement in a function body works.

Co-authored-by: Wolff Dobson <wolffg@users.noreply.github.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-06 14:08:08 -08:00
Jon Meowandjosh11b 24b763c7e8 Fix or remove invalid anchor links, adding pre-commit (#997)
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2022-01-05 15:00:02 -08:00