Commit Graph
182 Commits
Author SHA1 Message Date
770279e02d Arithmetic expressions (#1083)
Add support for arithmetic operators:

- Unary `+`.
- Binary `+`, `-`, `*`, `/`, `%`.

Specify their behavior for integer and floating-point types. Signed integer overflow is a programming error, handled in various ways. Unsigned integer overflow is specified as wrapping around, intended for hashing / crypto / PRNG use cases.

Co-authored-by: jonmeow <46229924+jonmeow@users.noreply.github.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2022-03-24 17:51:17 -07:00
josh11b 5238a8d37a Work around prettier bug (#1150)
Looks like this might be related to prettier/prettier#6112 , but the observed behavior is more broken than that bug indicates.
2022-03-23 13:49:48 -07:00
josh11b 3a18ab70f2 Terminology: qualified member access expression (#1142) 2022-03-17 14:08:24 -07:00
31fa2608d4 Updated README.md (#1075)
Revised landing page, including updated README.

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Jon Meow <jperkins@google.com>
2022-03-16 17:33:45 -07:00
Richard Smith 48d5b18026 Initial rough framework for specification. (#140)
Provide an initial rough structure for the specification so we can add things
as they are decided.

Split the specification into a language and a library section. In the language
section, use one file per broad area of functionality. Divide the language up
based on the intended layering of the language design.
2022-03-16 15:43:09 -07:00
Richard SmithandJon Meow a0a4146bcf Roadmap updates for 2022 (#1025)
Roadmap updates for 2022. Key goals:

* Reach the point where the first draft of the core language design is complete, with some test programs and an approximate implementation in executable semantics.
* Go public, and improve public participation.

Co-authored-by: Jon Meow <46229924+jonmeow@users.noreply.github.com>
2022-03-16 14:43:03 -07:00
Richard Smith 35a0bf1c7e Principle: information accumulation (#875)
This proposal addresses the question of whether we should perform a fully top-down compilation (like in C), a mostly top-down compilation (like in C++), or whether we should allow information from later in the same source file to be used in earlier program constructs (like in Rust, Swift, Java, C#, Haskell, and so on).

The proposed direction is:

-   Entities declared later in the same source file cannot be used earlier; top-down semantics apply everywhere.
    -    As an exception, class member function bodies are parsed as if they appeared after the class.
-   Forward declarations can be used to separate interface from implementation and to allow entities to be used before they are defined.
-   The behavior of the program is nonetheless required to be the same as if we had a globally-consistent rule: it's always a hard error to depend on any information that is not known or that is provided later.
2022-03-16 14:41:57 -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
Richard Smith 9384077abd Direct/indirect member access -> simple/compound member access. (#1119)
Plus some minor fixups to improve the readability and precision of the
summary in README.md.
2022-03-10 13:36:12 -08:00
josh11b 81c5eef3b8 Fix copy-paste error (#1123) 2022-03-08 10:15:24 -08:00
Richard Smithandjosh11b 18b423dc5f Member access expressions (#989)
Support for member access expressions with syntax `container.member`, covering cases such as:

* `object.field`
* `object.method(args)`
* `package.namespace.class.member`
* `object.(interface.member)`
* `object.(class.member)`

... and so on. Includes the rule for template name lookup as decided in #949.

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2022-03-02 13:01:01 -08:00
Jon MeowandRichard Smith fae7f0d007 Refining the precedence chart with if, struct literals, and links. (#1089)
This is intended to be a more iterative edit to the chart:

- Adding `if` and struct literals, since it came up in arithmetic.
- Adding links to address a comment of mine about wanting references
- Rephrasing slightly to reduce the implication that it's only for operators (particularly since I don't think we want to call `if` an operator)
- Shifting operators below because similar 

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-02-24 18:00:55 -08:00
Jon Meow 6fe8411122 Refactor common script functionality and reimplement the buildifier pre-commit (#1080)
Moves common script logic into utils.py (not a great name, but couldn't come up with better). This is in particular to make the buildifier.py script really trivial, allowing that pre-commit to be easily added. However, scripts have also been diverging on how we find bazel, so I'm trying to unify that.

The advantage of reimplementing buildifier's pre-commit is that (a) we can now run buildifier server-side, and (b) we can stop advising installing it manually. Then the only Linux-specific package manager is Cargo, which is only used for watchman, which is optional -- so stop highlighting Linux-specific package managers in the tool instructions.
2022-02-22 10:10:41 -08:00
Jon Meow 4f0c8786b0 Add precedence docs (#1070)
Drawing upon #555.
2022-02-18 08:35:36 -08:00
Richard Smith 9af354e10f Remove references to facet types from the design (#1072)
Update and simplify design/expressions to reflect removal of facet types.
2022-02-09 18:07:09 -08:00
Jon MeowandRichard Smith 654ad75c8d Merge comparison ops #702 into the design (#1055)
Mostly pulling in the text of #702, but with some small textual edits and adjusting links.


Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-02-07 14:47:05 -08:00
f6cbd2231e Conditional expressions (#911)
This proposal introduces a conditional operator of the form:

```
if cond then value1 else value2
```

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-01-28 20:07:45 -08:00
Jon Meow 80f402e2b6 Warn about watchman (#1052) 2022-01-28 18:20:05 -08:00
Jon Meow 979fe2cd89 Merge naming conventions #861 into the design (#1056)
A pretty straight copy of #861. Cutting down README.md example text since it seemed a little redundant with examples in the bullets.
2022-01-28 18:14:31 -08:00
Jon Meow 90f538f1b1 Remove submodule mentions in tools (#1054) 2022-01-28 18:12:18 -08:00
a2728f82bb Updating function and variable docs (#1017)
I was doing this for #851 initially, but I think #438 and #826 hadn't made it in (possibly intentionally due to #851? I don't recall). 

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-28 11:36:47 -08:00
josh11b 39571f715e Fix "block block" typo (#1053) 2022-01-27 16:22:11 -08:00
Jon Meow eda43faa5a Note namespace and static recommendations in C++ style guide (#1041) 2022-01-27 11:26:47 -08:00
6218aff2ba Document and, or, and not from #680 (#1032)
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-27 11:19:21 -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
Jon Meow adee1d7c94 Remove operators.md in favor of expressions/ (#1031) 2022-01-18 15:13:36 -08:00
josh11b ad5072b7b8 Link to Swift re: origin of witness table term (#1029) 2022-01-18 11:17:09 -08:00
josh11bandRichard Smith c08cbdb0bd Clarify class declaration syntax (#1026)
Clarify class declaration syntax

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-14 14:23:43 -08:00
josh11bandRichard Smith 00a178769f Principle: One static open extension mechanism (#998)
This is a proposal to adopt interfaces as the single static open extension mechanism for things like selecting how operators are implemented for types.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-07 15:54:27 -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
josh11bandRichard Smith 1534970146 Implicit conversions for aggregates (#981)
Define initialization, assignment, comparison, and implicit conversion between data classes with different field orders and/or types.

Implements the decision made in #710 .

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2022-01-06 11:16:41 -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
josh11bandChandler Carruth 3f12316d6b Generics details 7: final impls (#983)
Add support for marking impls as `final` to say they can't be specialized. This allows generic functions that see that the impl applies to determine the values for its associated types. For example this allows us to say that the implementation of the `Deref` interface for pointers can't be specialized. Otherwise, `*p` could have unknown type in a generic function.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-01-04 14:17:41 -08:00
josh11b 3cb3ee32dd Double -> f64 (#991)
Replace `Double` with `f64` for the 64-bit floating point type.
2021-12-15 13:33:47 -08:00
josh11bandRichard Smith dcc287cb6e Generic parameterized impls (details 5) (#920)
There are cases where an impl definition should apply to more than a single type and interface combination. The solution is to parameterize the impl definition, so it applies to a family of types, interfaces, or both. This includes:

-   Declare an impl for a parameterized type, which may be external or declared out-of-line.
    ```
    external impl [T:! Type] Vector(T) as Iterable { ... }
    external impl Vector(T:! Type) as Iterable { ... }
    ```
-   "Conditional conformance" where a parameterized type implements some interface if the parameter to the type satisfies some criteria, like implementing the same interface.
    ```
    external impl [T:! Type] Pair(T, T) as Foo(T) { ... }
    class Array(T:! Type, template N:! Int) {
      impl [P:! Printable] Array(P, N) as Printable { ... }
      impl Array(P:! Printable, N) as Printable { ... }
    }
    ```
-   "Blanket" impls where an interface is implemented for all types that implement another interface, or some other criteria beyond being a specific type.
    ```
    external impl [T:! Ordered] T as PartiallyOrdered { ... }
    ```
-   "Wildcard" impls where a family of interfaces are implemented for single type.
    ```
    class BigInt {
      external impl [T:! ImplicitAs(i32)] as AddTo(T) { ... }
      external impl as AddTo(T:! ImplicitAs(i32)) { ... }
    }
    external impl [T:! ImplicitAs(i32)] BigInt as AddTo(T) { ... }
    external impl BigInt as AddTo(T:! ImplicitAs(i32)) { ... }
    ```

In addition to a syntax for defining parameterized impls, we need rules for coherence:

-   Orphan rules that ensure that impls are imported in any code that might use it.
-   We need overlap rules that pick a specific impl when more than one impl declaration matches a specific query about whether a type implements an interface.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-12-09 14:29:54 -08:00
634148c39f Coherence: terminology, rationale, alternatives considered (#624)
Add an appendix with the rationale for and alternatives to coherence, along with an entry in the terminology and updates to the coherence discussion in the goals.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-12-09 13:27:15 -08:00
josh11bandRichard Smith be8d0a993b Generic impls access (details 4) (#931)
Implementations of interfaces are as public as the names used in their signature. No access control modifiers are allowed on `impl` declarations.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-12-07 09:28:08 -08:00
josh11bandRichard Smith 3f759ed58c Update and edit pass through generic design docs (#965)
Includes a variety of changes:

Int -> i32
this -> me
expand terminology doc and add links to it
fix text to reflect inline external impl introduced in Support external impl in class and adapter scopes. #905
no longer have plans for runtime type parameters
style updates like removing parentheticals and "we"
observe is a "declaration" not a "statement", since it can appear outside function bodies
many individual updates, clean-ups, and fixes

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-12-07 09:17:23 -08:00
89a829b8c7 Constraints for generics (generics details 3) (#818)
This proposal describes `where` clauses that can add constraints on a type-of-type, for example define restrictions on its associated types. Example:

```
fn FindFirstPrime[T:! Container where .Element = i32]
    (c: T) -> Optional(i32) {
  // The elements of `c` have type `T.Element`, which is `i32`.
  ...
}

fn PrintContainer[T:! Container where .Element is Printable](c: T) {
  // The type of the elements of `c` is not known, but we do know
  // that type satisfies the `Printable` interface.
  ...
}
```

Some other constraints, such as `Sized` are defined as type-of-types directly, possibly parameterized.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2021-10-29 20:32:22 -07:00
05efb278c2 as expressions (#845)
This proposal provides an `as` expression for casting. This supports implicit conversions plus some safe and unsurprising conversions that we do not support implicitly:

* lossy but fully defined conversions to floating-point types
* conversion from `bool` to integer types
* conversion between adaptors and their adapted type, and more generally between compatible types

This facility can be extended by implementing the `As(TargetType)` interface for a type.

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2021-10-29 16:46:19 -07:00
josh11b b08e3fedeb Support external impl in class and adapter scopes. (#905) 2021-10-26 09:25:30 -07:00
josh11b 8d051e2b6b Update constructor syntax (#913) 2021-10-25 13:05:39 -07:00
josh11b 629cfb8c4d Indent code blocks in bulletted list (#901) 2021-10-19 15:57:42 -07:00
josh11b dc9c36660b Facet relationship between parameterized types (#900) 2021-10-19 13:57:29 -07:00
josh11b cfb36b85e5 Merge overview sections (#899) 2021-10-19 11:58:48 -07:00
Jon Meow 9c17b72ddd Clean up titles for a few docs, mainly to remove 'Carbon' prefixes (#894)
Note, most principle docs already have the `Principle:` prefix, context sensitivity and `Safety strategy` are/were outliers.
2021-10-15 15:03:36 -07:00
josh11bandRichard Smith c7c653adba Generics: cleanups and updates (#881)
Add references, update syntax, merge future work

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2021-10-15 12:52:12 -07:00
Richard Smith 93a002a21a Avoid confusing description of "equivalent". (#843) 2021-10-06 11:06:34 -07:00