Commit Graph
1489 Commits
Author SHA1 Message Date
Adrien Leravat 7222f349d6 Explorer: move deallocation to StepCleanUp (#2547)
Move deallocation logic to `StepCleanUp` to have all necessary cleanup actions in the same place. Currently this means `DestroyAction` and `heap_.Deallocate`.
2023-01-24 12:24:05 -08:00
Adrien Leravat b4e3a3e6cc Explorer: fix class destructor not called with heap.Delete (#2546)
This change ensures that DestroyAction is executed for the value being deallocated when calling heap.Delete.

Relates to #2521
2023-01-24 11:34:47 -05:00
Jon Ross-Perkins 82f7d06855 Fix function parameter parsing past 2 params. (#2542)
The comment on FunctionParameterFinish is actually correct (`1. FunctionParameter`), just a typo in the implementation. It specifically failed parsing at the comma after the 2nd param, regardless of whether there were more params.
2023-01-23 13:28:34 -08:00
Adrien Leravat ec683d1ab2 Explorer: fix variables not cleaned up if declared before unformed (#2544)
Fix variables not being properly cleaned up if declared before an unformed variable.

### Details

Currently the following example / test
```
package ExplorerTest api;

class A {
  var i: i32;
  destructor[self: Self] {
    Print("Destructor A {0}", self.i);
  }
}

fn Main() -> i32 {
  var a0: A;
  var a1: A = {.i = 1};
  var a2: A;
  var a3: A = {.i = 3};
  return 0;
}
```

prints
```
Destructor A 3
```

instead of 

```
Destructor A 3
Destructor A 1
```

This PR fixes the issue in the `CleanUp` logic.
2023-01-23 08:59:14 -08:00
Richard Smith 2fd7e2b65d Add support for compound assignment and increment (#2526)
Add support for user-defined assignment, as well as compound assignment and increment, following the design direction in pending proposal #2511.

Some of this isn't fully testable yet: because explorer doesn't properly support `impl` specialization, the blanket `impl`s in the prelude prevent types from customizing assignment.
2023-01-20 18:48:13 -08:00
Jon Ross-Perkins f1a0645551 Set LLVM_SYMBOLIZER_PATH in lit_autoupdate (#2541)
Granted we should never merge crashes, but this means crashes when running lit_autoupdate will be properly symbolized.

This reuses what I'm currently doing for cc_env() in:
https://github.com/carbon-language/carbon-lang/blob/trunk/bazel/cc_toolchains/defs.bzl
2023-01-20 12:14:29 -08:00
Jon Ross-Perkins 2e6bf0dea6 Support using llvm-15 if GH images provide it (#2543)
A migration by GH that drops LLVM 14 is breaking builds. It seems to still be getting canaried, so this is showing as flaky behavior. I think it's https://github.com/actions/runner-images/pull/6871

The set of runs at https://github.com/carbon-language/carbon-lang/actions/runs/3970825811/jobs/6807006626 show the fix behavior.
2023-01-20 12:07:40 -08:00
Jon Ross-Perkins 421883ec8f Rename ToString to mention Operator (#2539)
Naming this ToString within the Carbon namespace encourages incidental overloading of the function, and that doesn't seem to be intentional; this feels questionable [under overloading style](https://google.github.io/styleguide/cppguide.html#Function_Overloading). OperatorToString seems helpful in that it makes it easy to see at a glance where it's being used.
2023-01-20 11:32:37 -08:00
Jon Ross-Perkins 09ac000305 Add some ls commands to help diagnose tool issues (#2540)
We're seeing some test runs where `clang` isn't in the path on macos. I suspect it's a bad image, and can't reproduce it, but want to add these commands to help provide debug info for potential future problems.

e.g., the `ls` output: https://github.com/carbon-language/carbon-lang/actions/runs/3963366092/jobs/6791110228

e.g., the bad image: https://github.com/carbon-language/carbon-lang/actions/runs/3963270727/jobs/6790912681
2023-01-19 18:24:13 -08:00
Richard Smith 52b11c3cb8 Fix lit_autoupdate to use consistent relative paths in FileCheck stanzas regardless of where it's run from. (#2538)
Avoids putting absolute paths in FileCheck lines, resulting in tests which fail when run in a different checkout.
2023-01-19 15:10:18 -08:00
Richard Smith c8f18446f4 Support explicit conversion of tuples. (#2537)
Allow explicit conversion from a tuple of `T1`, `T2`, ... to a tuple of `U1`, `U2`, ... if each element has an explicit conversion.

Also add a comment to existing `ImplicitAs` impl for tuples explaining its purpose.
2023-01-19 14:56:01 -08:00
Adrien Leravat 35989c5283 Explorer: prevent creating invalid class with missing parent (#2536)
Add a check to prevent creating a class from a struct with a missing parent.
This is already type-checked for carbon / user code, but not when using `Convert` manually.
2023-01-19 08:56:57 -08:00
Jon Ross-Perkins 3d90a85f24 Change DiagnosticKind to use EnumBase (#2532)
Although the implementation is similar in size, I think the consistency benefits are helpful. registry.def -> kind.def is also consistent with other implementations.

Also switching int32_t -> uint16_t because I think we it might be enough space (?). We use uint8_t elsewhere, but that's certainly going to be too small. Sticking with int32_t felt inconsistent.
2023-01-18 17:47:28 -08:00
Jon Ross-Perkins a1f2d6341f Switch SemanticsIR dumps to produce YAML (#2517)
The parser and lexer already produce YAML, so this is fundamentally a consistency issue. I've been thinking about this, and was looking again because I'm working on adding callables, and figured I'd just fix it now.
2023-01-18 17:43:17 -08:00
Richard SmithandJon Ross-Perkins 0d279b388a Make built-in conversions visible to ImplicitAs. (#2525)
Add a blanket `ImplicitAs` implementation to perform the conversions that explorer can perform as built-in conversions. This allows those conversions to be detected by constraints and to be used as part of other user-defined conversions. For now, a single monolithic conversion is exposed. I intend to split this up into multiple smaller conversion kinds for each kind of conversion in a follow-up change.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2023-01-18 17:36:55 -08:00
Jon Ross-Perkins 94872ef6da Change TokenKind's Print overload to a format_provider. (#2534)
Fundamentally this `.Print()` is wrong for debug output at present because `.fixed_spelling()` can be empty. It's also inconsistent with other enums to use it. We frequently print tokens for debugging, and it's easy to forget to specify `.name()` there.

Diagnostics use formatv, so we can provide a format_provider and address it in one spot that way. It also makes it harder to just forget to do the right thing.
2023-01-18 12:20:56 -08:00
Jon Ross-Perkins b936bf9e04 Remove cstdint from enum_base.h (#2533)
Enums are generally using uint8_t right now. enum_base.h doesn't use cstdint directly, and direct includes are preferred.
2023-01-18 10:53:33 -08:00
Richard Smith 74aae0911f Basic support for match_first declarations. (#2523)
Add basic support for `match_first` declarations to explorer, as described in https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/generics/details.md#prioritization-rule. The concrete syntax here is not yet approved, so `match_first` is renamed to `__match_first` for now, but the semantics are necessary to implement other approved features in explorer so we're intentionally getting a little ahead of the design here.
2023-01-17 13:08:38 -08:00
Carson Radtke 08a289753c [explorer] clarify warning when declaring arguments for 'Main' (#2518)
Improves the error produced when 'Main' is declared with parameters. Resolves a TODO in the typechecker. Explorer only. 

PR also adds a test for this error message.
2023-01-10 19:24:35 -08:00
Adrien Leravat 7936bfa019 Explorer: fix case for CleanUpAction enum kind (#2520)
Currently, only the action class uses the noun case (`Cleanup`), while the kind enum and functions are spelled `CleanUpAction`. Other actions use consistent casing between class and enum names. This change makes the name consistent, and unsurprising for developers. Mirrors `DestroyAction` (verb).
2023-01-10 19:22:50 -08:00
d70e237026 Pattern matching syntax and semantics (#2188)
This paper proposes concrete syntax and semantic choices for Carbon patterns.

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: David Sankel <camior@gmail.com>
2023-01-06 18:30:37 -08:00
Jon Ross-Perkins 2a163ca6cd Refactor the node stack into its own class. (#2505)
Currently, there's a mix of accessing node_stack_ both directly and indirectly, and there are already several Push/Pop methods to help wrap the behavior. However, there's also direct access because of shifting over time, as well as variations in _how_ the stack is used.

This migrates to a separate class in order to make a more specific contract for the API. It cleans up existing uses and adds APIs where needed.
2023-01-05 15:16:05 -08:00
Jon Ross-PerkinsandChandler Carruth 78ac6cb7d1 Switch TokenKind to EnumBase (#2509)
This shouldn't have any behavior change, it's just using #2504

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-01-05 14:11:30 -08:00
Jon Ross-Perkins 97634a5e91 Switch ParseNodeKind to EnumBase (#2510)
This shouldn't have any behavior change, it's just using #2504
2023-01-05 13:33:32 -08:00
Carson Radtke 0af5c55f55 Fix typo in extended final class error message (#2516)
added the word "be" and updated test result.
2023-01-05 11:01:32 -08:00
Chandler Carruth d61531c82c Hack clang-format config to format our macros better. (#2514)
This somewhat abuses control-flow macro handling in `clang-format` to get the behavior we want. Fortunately, I don't think we're ever likely to want this for its intended purpose so it seems harmless to co-opt it like this. If we really wanted, we can narrow it to `if`-macros, but I picked the simpler option to start.

FWIW, I tried all the other macro formatting special cases to see if one would work but it didn't. Might be worth filing a feature request to get a `TypeDefinitionMacros` setting to compliment `TypenameMacros` and format like this does, but it seems (very) low priority.
2023-01-05 09:38:21 -08:00
Chandler Carruth 2f84751d2b Follow-up from #2504 review to add a comment and static_assert. (#2515)
This helps document (and check) that we can define things as `constexpr`
despite declaring them differently.
2023-01-05 09:24:22 -08:00
Adrien Leravat d7b5e537d6 Explorer: drop unused NominalClassType constructor (#2513)
This constructor was previously used for instantiated vs non-instantiated generics, but is not necessary anymore.
2023-01-05 09:18:19 -08:00
Priyananda Shenoy c87d271c53 Issue #2121: Support new block string literal syntax in explorer (#2399) 2023-01-04 15:53:47 -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
Chandler CarruthandJon Ross-Perkins a1ad39fa29 Introduce helpers to build enum-wrapping classes. (#2504)
The goal here is to (significantly) reduce the boilerplate needed when defining classes that wrap enums, especially those managed with the `.def`-file style X-macros that are common in the toolchain.

This should also provide both better and more consistent functionality to those classes once ported over to it.

Initially, only `ParserState`, `SemanticsNodeKind`, and `SemanticsBuiltinKind` are ported as these were also the three that JonMeow ported in his original pull/2453 "option 5". This is heavily based on that version of the code.

Goals I was considering that influenced the design:

- Keep the individual enum-wrapping classes as simple and easy to read as possible. Especially important is keeping the `.def` files that are often filled with really important documentation clean and easy to maintain over time.

- Don't rely on computed `#include`s as that is an especially dark corner of the preprocessor and breaks some build systems.

- Have a really good API of the enum-wrapping class, including nice constant names for the values, easy printing, and even easy debugger-callable methods to get the name (as opposed to the integer value).

- Keep the API that users interact with in the base class as clean and easy to read as possible.

- Reduce the boiler plate for each instance of these as much as possible.

- Avoid excessive inline generated code or constants that would result in steady growth in object file sizes and linker effort doing deduplication.

These goals aren't always compatible, so we end up needing to pick a compromise between them when in tension. I think this version is a pretty good compromise.

The original version I started with already pull most of the API into a CRTP-style base class. This version pulls *all* of the common API. This is the main tool for getting consistency and avoiding duplication. However, connecting this base class to the individual enum wrappers is still difficult. Some specific changes here that try to do as much as possible there:

- Use a slightly fancier macro pattern to reduce the boilerplate of defining the raw `enum class` prior to the wrapper class.

- Use a macro to simplify naming the base class.

- Move the name table to a `.cpp` file to avoid every inclusion generating a complete copy of the strings (that the linker has to deduplicate). This is done with some care to sharply reduce the boilerplate needed in that `.cpp` file.

- Sink the name _API_ fully into the CRTP base class. This requires some significant complexity in the implementation, but all of that is hidden behind a single implementation detail macro, and the API itself is simple and readable. This also makes it much more reasonable to test the entire system a single time next to the base class.

This version also moves from constant factory functions to normal constants. This requires two batches -- first a declaration, and then a definition -- but the API result is significantly better and similar to the original option, the macro structure reduces the cost of these. Unfortunately that makes the adoption a bit noisy, but I think its worth the churn.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2023-01-04 12:33:06 -08:00
Jon Ross-Perkins 92e6e5f6f5 Fix missing include in element.h (#2506)
Without this, some compile setups can get a missing symbol for llvm::raw_ostream; this is relying on ostream.h through other code paths at present, so should be explicit about it.
2023-01-04 11:29:30 -08:00
Adrien Leravat 798a40c886 Basic support for impl virtual override keyword (#2493)
Features:
* Add basic support for `impl` virtual methods (override virtual method)
* Error on invalid declaration for `impl` and `virtual`, covering simple use cases
* Add `abstract` fn parser-only support

Changes:
* Modify parser to handle new function specifiers, resolve conflicts
    * Group `virtual_override` and `FN`, and group `impl_kind` and `IMPL` to avoid ambiguities with around `impl` token parsing
* Add new `VirtualOverride` enum for function declarations, and matching `virt_override() -> VirtualOverride` getter
* Update function declaration logic

Depends on #2462
2023-01-04 11:09:23 -08:00
Adrien Leravat 8301258ef8 Explorer: support virtual class methods (#2462)
Features:
* Add `virtual` virtual override keyword for functions
* Support `virtual` class methods using dynamic dispatch

Changes:
* Add `vtable` in `NominalClassType`, 
* Add `NominalClassValue**` in class values pointing to  descendant-most class
* Resolve virtual methods during member lookup

Limitations:
* Does not include yet `impl`, `abstract` virtual override keywords, or the complete logic for virtual function declaration

Depends on #2460 
Relates to #1881 
Relates to #2493
2023-01-02 12:37:49 -08:00
Chandler Carruth dd26ea6a15 Update Bazel & protobufs, then narrow warnings to Carbon. (#2500)
Protobufs code hits a warning with the latest system headers on macOS.
I figured this may have been fixed so I updated protobufs and Bazel to
the latest releases. This generally cleaned things up.

However, it actually added *more* warnings. This clearly isn't a really
well tested path. In fact, we already have a disabled warning that we'd
like for Carbon code because LLVM isn't clean for that warning.

So I've switched our warning strategy to a more durable approach of
suppressing all warnings for external repository headers and source
files. This lets us re-enable the missing warning and should fix the
protobuf warning that started me down this twisty path.

Sadly, we *have* to update to Bazel 6 in order to have the necessary
flag to use this approach to suppressing warnings, so I couldn't do this
as two PRs cleanly. =/ That's why I've bundled both the Bazel (and
protobuf) updates with the warning strategy change.

Last but not least, I've fixed several unused parameters in Carbon's
code that our warnings now catch.
2022-12-28 16:58:01 -08:00
Jon Ross-PerkinsandChandler Carruth 04d3901b7f Switch Diagnostic structure to use DiagnosticMessage to avoid pointers (#2502)
Followup for #2490 

The switch of DiagnosticMessage to have DiagnosticMessage means we don't need to use unique_ptr. This means that copy constructors are implicit again and don't need to be avoided, but per discussion still keeping with moves. Comments on HandleDiagnostic try to capture the use of moves there.

I'm keeping DiagnosticLevel at the top-level, and adding some checking that notes are actually Notes.

Also, adding MakeMessage to unify some of the logic (this has particularly been bugging me around format_fn, and I ran into it here because of the Diagnostic -> DiagnosticMessage change). The addition of NoTypeDeduction is intended to avoid some duplicative comments that'd been piling up.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-12-28 16:47:33 -08:00
Jon Ross-Perkins 11deb14dc6 Handle var init-with-self situations. (#2488)
The problem I'm trying to solve is: `var x: i32 = x;`. This change makes it so that name lookup fails, by removing `x` from name lookup between the `=` and `;`.

`var x: i32` still adds to name lookup to handle future situations like `var (x: i32, x: i32);` which is still a redefinition of `x`; if we don't add `x` to name lookup, it gets harder to catch that example.

The VariableDeclaration/VariableInitializer refactor in ParseTree supports this by given a bracketing-like structure for semantics to cue that it's entering an initialization expression. With this, VariableInitializer can remove the name lookup and queue it to be restored. VariableDeclaration doesn't need to change too much since it's still bracketed by VariableIntroducer, and so we just traverse slightly differently.

Note this also incidentally changes a little about NameReference, that it's returning the storage consistently instead of the name. You can see this e.g. in global_lookup.carbon, `Assign(node8, node4): node2;` using node4 (VarStorage) instead of Node5 (BindName). Really either _could_ work, since from a BindName we can get to the VarStorage, and that may be reason to switch later if we find it preferable to have the BindName for whatever reason.

But the *actual* value in NameLookup is a BindName so that errors can associate with the _name_ instead of the "storage" parse node, which is currently the `:`. This is mainly for fail_duplicate_decl.carbon, which has a "Previous definition" note that points at the storage's parse node.
2022-12-28 12:55:40 -08:00
Jon Ross-PerkinsandChandler Carruth 6c9b7cba55 Add a DiagnosticBuilder to support context on diagnostics. (#2490)
This is currently used once for PreviousDefinition in semantics.

This PR doesn't just add a builder, it also adds support to the emitter itself to collect notes attached to a diagnostic, and to the consumers and emitters to print all of them.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2022-12-28 10:24:19 -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
SADIK KUZUandJon Ross-Perkins a0763413cb Update pre-commit config (#2498)
Updating https://github.com/pre-commit/pre-commit-hooks ... updating 3298ddab3c13dd77d6ce1fc0baf97691430d84b0 -> v4.4.0 (frozen).
Updating https://github.com/google/pre-commit-tool-hooks ... already up to date.
Updating https://github.com/psf/black ... updating 2018e667a6a36ee3fbfa8041cd36512f92f60d49 -> 22.12.0 (frozen).
Updating https://github.com/pre-commit/mirrors-prettier ... updating d0a4882e1c96eca274f90b273f0f809ab3d98aff -> v3.0.0-alpha.4 (frozen).
Updating https://github.com/PyCQA/flake8 ... updating f8e1b317742036ff11ff86356fd2b68147e169f7 -> 6.0.0 (frozen).
Updating https://github.com/pre-commit/mirrors-mypy ... updating fde4bb992b03943ecb94207a52739ba07957bd06 -> v0.991 (frozen).
Updating https://github.com/codespell-project/codespell ... updating c6ecb9fc51571a77bc92e6c265c358aef7cb6c38 -> v2.2.2 (frozen).
Updating https://github.com/google/pre-commit-tool-hooks ... already up to date.

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-12-27 13:37:51 -08:00
Jon Ross-Perkins 733965704a Start building some checking of diagnostic use. (#2487)
In theory we're doing a central registry so that we can ensure there's at least one test for each. This isn't doing that, but I'm trying to validate that the central registry isn't leading to duplicates or abandoned checks (and catching a couple of each).
2022-12-27 08:43:58 -08:00
Jon Ross-Perkins 88905b99d8 Add a location translator for ParseTree::Node. (#2491)
SemanticsIR emits in terms of parse tree nodes, doing this to echo TokenLocationTranslator.
2022-12-27 08:43:33 -08:00
Jon Ross-Perkins 60b45e3d30 The typed_linked_list test trace output is too slow, so stop testing it. (#2494)
Another case of flakiness from trace output performance:
https://github.com/carbon-language/carbon-lang/actions/runs/3760820974/jobs/6391950894
2022-12-23 06:44:42 -08:00
Chandler Carruth 2c198865ff Mark some other issue categories as not-stale. (#2496)
While we have a generic 'long term' label, it seems redundant for some
issues that are already labeled with something that clearly is
open-ended and not something we should expect to have a bounded
timeline. For example, we want to actively curate a backlog of design
ideas and good first issues for folks to browse and pick up, so we
shouldn't be marking them as inactive after any fixed time frame.
2022-12-22 19:17:51 -08:00
16dcdc2a34 Types are values of type type (#2360)
Define a "type" to be a value of type `type`. Contexts expecting a type perform an implicit conversion to `type`. Values like `()` and `(i32, i32)` and `{}` are no longer types, but instead implicitly convert to `type`. Values of interface or constraint type (now called "facets") are similarly not formally types but implicitly convert to `type`.

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
Co-authored-by: josh11b <josh11b@users.noreply.github.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2022-12-22 13:49:55 -08:00
Adrien Leravat 026c4b9dc3 Explorer: move subtyping logic to TypeChecker (#2484)
Addresses comments from this discussion: https://github.com/carbon-language/carbon-lang/pull/2460#discussion_r1046444433

Features:
* Move subtyping logic from Interpreter to TypeChecker, exposing subtyping as a series of access to `.base`.
    * Excludes function parameter conversion, which is still done in ::Convert due to parameters conversion being handled differently.

Changes:
* Add new `class BaseAccessExpression : public MemberAccessExpression`, allowing rewrites
* Handle `BaseAccessExpression` expression type in Interpreter
* Move subtyping logic to `TypeChecker::ImplicitlyConvert`
2022-12-21 19:01:02 -08:00
Jon Ross-Perkins 6accdfff77 Replace the toolchain README with a docs link. (#2482)
The doc is more up-to-date than the readme right now, and I'm not ready to migrate it back for the moment, but this should at least make it clear what the status is.
2022-12-21 13:30:52 -08:00
Jon Ross-Perkins e5d49f5989 Store SemanticsNode in a single list instead of per-block (#2475)
This switches to single list storage of SemanticsNode. The driving motivation behind this is to simplify cross-references within a given IR. Types of nodes will frequently refer to other blocks. This causes a significant increase in the number of cross-references, which can become difficult to manage (and reason about). By reducing to a single list of nodes, cross-references are only needed when crossing IR boundaries.

Because cross-references now only have 2 things to track (IR and index), they can be a regular SemanticsNode and don't need further indirection. This wasn't motivating, but feels like it reinforces the simplification.

Note this isn't being used to deduplicate nodes, at least right now. That could lead to difficult-to-update situations, but also most nodes are associated with the underlying ParseTree::Node in order to track sources for diagnostics; as a consequence, nodes representing equal text in different source locations wouldn't be the same node. There may be future opportunities here, discussed with @zygoloid, but no action is taken at present.

We may eventually want to switch the storage of NodeBlocks to have `[start, end)` ranges instead of individual numbers, but I'm leaving that alone for now.

As an aside, I noticed I was accidentally overloading the copy constructor on SemanticsIR. I've added some disambiguation on that, but am not deleting the copy constructor per style advice (even though the type should never be copied due to storage size).

codespell tries to change `CrossReference -> cross-reference` so disabling it there.
2022-12-21 13:13:13 -08:00
Adrien Leravat 34ec3ce74b Explorer: add missing Nonnull<> (#2486)
Trivial change adding a missing `Nonnull<>` to `RewritableMixin`.
2022-12-21 12:48:27 -08:00
Jon Ross-Perkins c5f4e65fdd Add parentheses to remove ambiguity for %. (#2478) 2022-12-20 17:51:05 -08:00