Commit Graph
3522 Commits
Author SHA1 Message Date
josh11bandJosh L cb2257e678 Should-fail tests using abstract tuples and structs (#4985)
Currently the toolchain does not recognize that tuples and structs with
abstract elements should be considered abstract.

Also make the existing `fail_abstract` test into a `no_prelude` test.

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
2025-02-20 16:56:41 +00:00
Dana JansensandGeoff Romer 53e4367c58 Add and correct tests of impl lookup on generic interfaces (#4974)
These tests expose cycles during deduction, when the generic parameters
in an impl statement require deduction and the impl clause that
satisfies them comes after the one containing the generic parameters.
This causes the same impl to be looked at repeatedly, and produces a
cycle diagnostic.

---------

Co-authored-by: Geoff Romer <gromer@google.com>
2025-02-20 15:48:15 +00:00
Dana Jansens 4b45caa8e0 The Core.Array type for direct-storage immutably-sized buffers (#4682)
We propose to add `Core.Array(T, N)` as a library type in the `Core`
package. Since arrays are a very frequent type, we propose to privilege
use of this type by including it in the `prelude` library of the
package.

We would like to see a shorthand where `Core.Array` is automatically
imported into the file scope, and this proposal includes future work to
this effect.
2025-02-20 15:28:59 +00:00
Geoff RomerandJon Ross-Perkins 74e1a9949f Support tuple patterns outside parameter lists (#4923)
Parameter lists need substantially different treatment than tuple
patterns in other contexts, so this change splits them into separate
parse node kinds.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2025-02-20 03:17:45 +00:00
Richard Smith 35f5a7f115 Reorder the token in a parse node to match its actual location. (#4984)
Also add a TODO for another parse node whose typed representation is
imprecise. Just clarifications; no behavior change is intended.
2025-02-20 02:25:05 +00:00
Richard Smith 1be726d3a2 Fix NodeCategory printing. (#4983)
Rearrange `NodeCategory` printing so we get a compile-time error for
missing switch cases if it's missing any categories. Add several missing
categories.

In passing, fix some minor things in the `NodeCategory` class
definition, and fix an overly-permissive typed node.
2025-02-20 01:50:42 +00:00
Geoff Romerandjosh11b 09b06b4234 Document grammar of field and associated constant decls (#4980)
Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
v0.0.0-0.nightly.2025.02.20
2025-02-20 01:28:01 +00:00
Chandler CarruthandJon Ross-Perkins 5b30888dd8 Fix clang runner to avoid leaking memory (#4972)
Sadly, the Clang driver unconditionally injects the `-disable-free` flag
to CC1 invocations. =/ This ends up with us leaking memory when invoking
Clang programmatically, for example in unit tests.

I've fixed this by post-processing the flags. The alternatives I see all
involve duplicating even more code from within Clang's internals into
our runner, and we already have a lot of that. I have left a TODO to try
and follow up with upstream about fixing this in a more sustainable way,
but wanted to get the testing in place anyways.

I've also threaded a flag through the various layers so that when we're
on the command line we can actually skip this and get the same compile
time benefits that Clang itself gets from disabling freeing all of the
internal data structures.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2025-02-20 00:25:18 +00:00
Jon Ross-Perkins 5c6f27904f Remove FileTestBase::ValidateRun (#4979)
`ValidateRun` is explorer-specific, so move out the error production to
be specific within explorer. This is related to other work I'm trying to
do which would make this require more special-casing to maintain; since
the toolchain doesn't need it, it's easier to drop.
2025-02-20 00:20:47 +00:00
eb69d7420e First iteration of completing and resolving facet types (#4920)
* Add `RequireCompleteFacetType` and `ResolveFacetTypeImplWitness` to
`check::Context`. Goal was to move code from `impl.cpp` (mostly) without
functional changes.
* Complete type information is cached with the facet type, and is stored
in a `complete_facet_types()` table.
* Main functional change is to diagnose attempts to use a rewrite
constraint on an associated function. Some existing diagnostics have
been updated.
* Remove `check::Context::RequireDefinedType`:
  * For class types, use `RequireCompleteType`
  * For facet types, use `RequireCompleteFacetType`
* Introduce a `SemIR::SpecificInterface` to hold an interface and
specific id pair.
* Keep the specific interface ids in the impl object.
* Avoid some extra copies in `Dump` functions.
* Future work missing from this PR:
  * Resolving for member access or actions that require impl lookup.
  * Resolving rewrites constraints that refer to non-concrete values.
* Any support for adding implied constraints that result from a `where`
clause (though TODOs have been added).

---------

Co-authored-by: Josh L <josh11b@users.noreply.github.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Dana Jansens <danakj@orodu.net>
2025-02-19 22:10:11 +00:00
Dana Jansens 7c7e169994 Avoid diagnosing conversion errors inside deduction of impl arguments (#4976)
When conversion fails, the impl should simply not match, no error should
be generated.
2025-02-19 21:46:46 +00:00
Jon Ross-Perkins 524a6337f4 Improve decl_name_stack comments (#4977)
Trying to make it clearer what these correspond to. Had suggested a
small edit on
https://github.com/carbon-language/carbon-lang/pull/4902/files#r1960682518,
but since that was missed, suggesting an incrementally larger edit since
`name_id` and `loc_id` are now more tied.
2025-02-19 19:46:23 +00:00
Jon Ross-Perkins 186ca0e505 Remove Context::DumpFormattedFile (#4978)
Asked on
[#toolchain](https://discord.com/channels/655572317891461132/655578254970716160/1339388316977729627),
it sound like this is unused. Also with the `Dump` methods, having
`Dump(context)` might be more helpful for findability (could just move
it here if that's desired).
2025-02-19 18:33:59 +00:00
Dana JansensandRichard Smith 11aba70c1d Add enumerate() for ValueStore and ImplStore (#4975)
This allows iterating on all values in a store along with the Id for
each value, instead of `llvm::enumerate(store.array_ref())` which would
give you the indices.

While the indices are really the same as the Ids, this provides a
typesafe way to enumerate() over a store.

There's no use for this right now, but I thought I needed this, and it
helped me debug, and it was a pain to write correctly without dangling
references.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2025-02-19 18:32:13 +00:00
Dana Jansens 3f01310039 Make choice work for alternatives without parameters (#4815)
This adds support for choice types at a similar level to that of a C
enum, where each alternative has a name but no additional
data/parameters attached to it. We generate a TODO diagnostic if
parameters are specified.

Because there's no extra data, the storage is a simple unsigned integer
discriminant of the smallest possible size.

A choice without any alternatives is not constructible. A choice with a
single alternative is, and has an empty tuple in place of a discriminant
since it has only one state. The empty tuple is used to make the class
non-constructible. This can be improved.

Each alternative is turned into a let binding on the choice that is a
value of the choice with that alternative set as the active one in the
discriminant. This isn't possible to write in user code with a class
right now, since the let binding has the same type as the choice
(which is a class) it is within. It's possible to generate it in semir
however by adding the binding after the class is marked complete.
2025-02-19 16:37:36 +00:00
Boaz Brickner 6a99c4e970 When diagnosing a duplicated name, add the name to the diagnosis (#4902)
In order to have the name available for diagnostics, we now always set
`NameId` in `NameContext` and put `poisoning_loc_id` as part of the
union with `resolved_inst_id` instead (since we never need both).
2025-02-19 07:19:23 +00:00
Richard Smith e0b2f5d772 Add and propagate template phase for constants. (#4964)
Treat template bindings as introducing template phase, and propagate it
in the same way we propagate the checked generic phase.

Rename "symbolic" to "checked symbolic" to make room for "template
symbolic". Also rename "phase" to "dependence".
v0.0.0-0.nightly.2025.02.19
2025-02-18 18:57:34 +00:00
Dana Jansensandjosh11b 382094b725 Support conversion of facet-ish values for type deduction (#4956)
A value of type FacetAccessType can convert to a facet value of a target
FacetType if the value's underlying facet value's FacetType is 
compatible.

A value of type FacetType can convert to another value of type FacetType
if the value's FacetType is compatible with the target FacetType.

During impl lookup, the comparison of the lookup type and the impl's
type needs to consider more than strict equality. If the impl's self
type is a FacetAccessType, we instead need to verify that the FacetType
of the lookup type and of the impl's self type are compatible (which is
like a problem of another impl lookup). For now we check that they are
equal by unwrapping the FacetAccessType to the constant facet value
within, and compare that with the lookup type.

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
2025-02-18 18:14:03 +00:00
Jon Ross-Perkins 3d6f14fca5 Refactor FileTestBase to split logic between multiple files (#4968)
I'm refactoring logic to try to make these files more manageable,
particularly as I'm looking at ways to use more threads. I'm renaming
FileTestBase::TestContext to TestFile and FileTestBase::TestFile to
TestFile::Split to try to be more consistent in how we talk about test
files and file splits in general. This change is not expected to change
any behavior, it's just refactoring.

This PR has two commits:

- Copying files for viewing deltas in GH
- Moving logic

The delta of the two commits is the main PR. The second commit can be
viewed on its own to see the delta versus file_test_base.* files where
the logic currently rests (there's still a reordering in run_test.cpp as
part of making one function static).
2025-02-18 17:10:23 +00:00
Chandler Carruth cf29449faf Move test file writing to our common testing file_helpers (#4971)
This will make it easy to share across tests. Factored out of a change
adding new uses of the file.
2025-02-18 03:27:49 +00:00
Chandler Carruth 151bd14fd3 Refactor stdout and stderr capturing to a library (#4970)
This should make it easy to add more tests which need the specific
behavior here. It also isolates where we reach into GoogleTest's
internals to a single common place.
v0.0.0-0.nightly.2025.02.18 v0.0.0-0.nightly.2025.02.17
2025-02-16 16:12:14 +00:00
fe2c0b48cf Begin packaging builtin Clang headers (#4959)
This digs the Clang builtin headers out of the Clang package and
reconfigures them to install into our installation prefix, under the
LLVM installation subtree.

---------

Co-authored-by: Ilya Biryukov <ibiryukov@google.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
v0.0.0-0.nightly.2025.02.16
2025-02-15 23:03:32 +00:00
a881e21432 Language Server - implement incremental document sync (#4926)
The language server is now able to subscribe to and apply incremental
changes to the document. Source code is assumed to be utf-8. It does not
currently handle utf-16 code points.

---------

Co-authored-by: Carbon Infra Bot <carbon-external-infra@google.com>
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2025-02-15 02:05:56 +00:00
Jon Ross-Perkins 9cd3f0aa3d Remove obsolete '...' hints on node kind macros (#4958)
We used to have more arguments, but they've mostly been removed now.
2025-02-15 01:36:46 +00:00
Jon Ross-Perkins e3764ff6f3 Clean up Context API (#4969)
- Add a better class comment.
- Remove the obsolete `type_ids_for_type_constants_` and `TypeNode`.
- For `bind_name_map`, fix declaration order and move comment to be
consistent with other members.
- Reorder accessors to better match order of data members.
  - This is how I noticed `type_ids_for_type_constants_`
- Put a bigger notice so that `sem_ir` helper functions don't end up in
the middle of other members again.
v0.0.0-0.nightly.2025.02.15
2025-02-15 01:18:54 +00:00
Jon Ross-PerkinsandRichard Smith 38d25cf622 Share ReadFile in tests (#4967)
Small cleanup for code sharing. Note, `*ReadFile` will trigger a
CHECK-failure on error; the relevant implementations would previously
have failed silently (empty string).

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2025-02-15 00:17:01 +00:00
Jon Ross-Perkins d4f15ab26e Publish empty diagnostics on close (#4954)
Without this, diagnostics will linger after closing a file.

This refactors towards a pattern of putting outgoing calls as methods on
`Context`. I'm mixed on this, mainly thinking it's an improvement on
using `outgoing` directly (because it shares the name and structure),
might want to move it to a side-class later that is _only_ LSP wrappers.

I could also make inheritance private on OutgoingMessages and these
kinds of methods public there, but I'm hesitant to adopt that approach
versus a type separation.
2025-02-15 00:09:20 +00:00
Dana Jansensandjosh11b f038aead4c Diagnose cycles in impl lookup (#4947)
Cycles are defined as reaching two independent lookups in a chain that
have all the same types involved. The acyclic rule states that this is
not possible and results in an error:
https://docs.carbon-lang.dev/docs/design/generics/details.html#acyclic-rule

To do this we need to track the types involved in impl lookup. The
interface constant includes the whole facet type being looked up, which
includes any specific types for generics or where constraints. Thus we
just need to compare the constant ids to look for this condition.

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
2025-02-14 22:21:53 +00:00
Boaz Brickner 4a93b6667e Add the used name to the NameUseBeforeDecl diagnostic (#4901)
Part of #4622.
2025-02-14 21:54:35 +00:00
Boaz Brickner d65f0d959d Add a test for the case that extend poisons a class member (#4960)
Part of #4622.
2025-02-14 20:31:58 +00:00
Jon Ross-Perkins 95fd890698 Allow pre-commit to talk to googleapis (#4966)
Example:
-
https://app.stepsecurity.io/github/carbon-language/carbon-lang/actions/runs/13320074606
-
https://github.com/carbon-language/carbon-lang/actions/runs/13320074606/job/37247235705

I'm not sure what it's being used for (even blocked, nothing unexpected
failed) but if bazel or similar is trying to talk to this it should be
fine.
2025-02-14 20:21:20 +00:00
Dana Jansensandjosh11b d5f3d3365a Allow checking to continue after 'impl as' outside class (#4937)
Currently it returns false which just ends typechecking. Instead handle
the error state later and avoid firing overlapping diagnostics in
'extend impl as'.

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
2025-02-14 20:02:10 +00:00
Jon Ross-Perkins 311b4ff03d Refactor AddInst-family functions to their own file (#4941)
This in particular uses free functions because it's likely to end up
more consistent with types (versus a wrapper object for InstStore).
Note, this is unlikely to have a performance impact, but if it does, we
can look into related approaches (and we've already discussed using
LTO).

Renames `PendingBlock::AddInst` to `PendingBlock::Add` because
`MakeElementAccessInst` expects the matching name to exist.
2025-02-14 19:44:36 +00:00
Boaz Brickner dd7c64bad0 When diagnosing a duplicate name, point to the name instead of the instruction (#4953)
Left TODOs where more work is necessary before this change can be
applied or its impact can be verified.

Includes #4952, to avoid regression in some cases.

Similar to #4938. See
https://discord.com/channels/655572317891461132/655578254970716160/1339007384361762857.
2025-02-14 19:21:50 +00:00
Boaz Brickner 986a2a064c Add poisoned names to the format (#4961)
Part of #4622.
2025-02-14 19:18:51 +00:00
Jon Ross-Perkins 44a5e371b2 Reduce clangd-displayed errors for def files (#4957)
When I open a .def file, there are often 4 errors:

- The #error
- The #define is not defined
- Missing `;`
- Identifier naming

This PR is meant to disable all of these, since they can be distracting
from fixable diagnostics.
2025-02-14 17:54:39 +00:00
DavidLoftus 9cf5306c01 Update rules_cc to 0.1.1 (#4965)
rules_cc@0.1.0 was yanked from
[BCR](https://registry.bazel.build/modules/rules_cc) due to prematurely
removing cc_proto_library, this inconsistently causes the following
build error:

> ERROR: Error computing the main repository mapping: Yanked version
detected in your resolved dependency graph: rules_cc@0.1.0, for the
reason: rules_cc 0.1.0 is yanked due to incompatible change (prematurely
removing cc_proto_library from defs.bzl), please upgrade to 0.1.1.
Yanked versions may contain serious vulnerabilities and should not be
used. To fix this, use a bazel_dep on a newer version of this module. To
continue using this version, allow it using the --allow_yanked_versions
flag or the BZLMOD_ALLOW_YANKED_VERSIONS env variable.

This PR updates to 0.1.1 as recomended in warning and
[bazelbuild/rules_cc#268](https://github.com/bazelbuild/rules_cc/issues/268#issuecomment-2651269117).
2025-02-14 17:02:03 +00:00
Boaz Brickner 809bcf10ed Fix bad merge of comments introduced in #4884 (#4955)
Part of #4622.
v0.0.0-0.nightly.2025.02.14
2025-02-14 00:34:48 +00:00
Jon Ross-Perkins dc8f47e6ad Move type functions off Context (#4951)
This creates a new check/type.h for most logic, and also moves some
functions to TypeStore in sem_ir/type.h. My approach for TypeStore is to
focus on moving the read-only functions there.
2025-02-13 23:02:38 +00:00
Boaz Brickner 23e5677c8e Avoid poisoning non identifier names (#4884)
There are different use cases where we call
`Context::LookupQualifiedName()` on non identifiers, like
`NameId::SelfType`, which implicitly triggers poisoning these names. I
don't think poisoning non identifiers like`Self` is ever useful.

Use cases where `Self` is being poisoned:
* Checking allowed access:
https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/toolchain/check/member_access.cpp#L62,
for example, in
https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/toolchain/check/testdata/alias/no_prelude/fail_aliased_name_in_diag.carbon#L21
* Using the type `Self` as a parameter type:
https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/core/prelude/operators/arithmetic.carbon#L78

Part of #4622.
2025-02-13 20:00:54 +00:00
David BlaikieandJon Ross-Perkins aa71f31787 Refactor implicit Self param into a member on SemIR::Function (#4928)
This ensures the data is available for more uses (specifically for
diagnosing virtual/abstract/impl functions on non-instance methods).

It still doesn't quite address the TODO to move the Self param search
all the way back to the param walk in all cases. To do that in the case
that still has a separate search loop, I think we'd have to change
`Check::NameComponent` to carry this information (as it carries the
implicit_param_patterns-id) - though there's comments in NameComponent
suggesting it shouldn't carry function-specific things like
`call_params_id` and `return_slot_pattern_id` - so I wasn't sure if it
was suitable to add more there, but I can - possibly in a follow-up
change.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2025-02-13 19:07:17 +00:00
Jon Ross-Perkins e70f9cd71d Move diagnostic helpers from Context to other files (#4949)
Trying to find more specific homes for shared diagnostic function calls.
2025-02-13 18:08:11 +00:00
Richard Smith 6dda094928 Superficial support for template modifier on symbolic bindings. (#4948)
Change parse tree from `template (T:! type)` to `(template T):! type`,
so that we have information about whether a binding is a template
binding available when forming the representation of the binding
pattern. This incidentally fixes a bug that we would accept `template
addr A:! B` instead of the intended `addr template A:! B`.

Track whether a symbolic binding is a template binding on the
`EntityName` object. I'm borrowing a bit from the `CompileTimeBindIndex`
for this in order to avoid making `EntityName`s larger. Longer-term, we
should think about using a different representation for symbolic
bindings, to avoid including these fields in all `EntityName`s, but
that's out of scope for this change.

So far, template bindings are treated as having the same phase as
checked bindings, but that will change in a future PR.
2025-02-13 02:15:22 +00:00
Geoff RomerandJon Ross-Perkins f502e8d6ff Avoid speculatively pushing a pattern block in impl handling (#4943)
To do this, we restructure the parse tree to make `forall` a leaf node
that comes before the parameter list.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
v0.0.0-0.nightly.2025.02.13
2025-02-12 23:21:57 +00:00
Jon Ross-Perkins 95f2140a04 Update compiler explorer mentions in README (#4946)
The explorer interpreter is still there, but it's non-default so it's a
little hard to link to (we could do a shortlink like
https://carbon.compiler-explorer.com/z/8P1WE97nn if you want to keep
that). As is, I assume it's okay to just state that this is now the
toolchain.
2025-02-12 23:15:43 +00:00
Jon Ross-Perkins 0c37ce6908 Delete unused ParamPatternInfo::GetNameId (#4942)
Perhaps GetPrettyName replaced all uses?

As long as I'm here, also fix struct declaration order.
2025-02-12 22:38:36 +00:00
Jon Ross-Perkins b628fd80ef Remove explorer from issue templates (#4945) 2025-02-12 22:24:25 +00:00
Jon Ross-Perkins afef6cd940 Refactor name lookup logic out of Context (#4930)
This is a pretty straight move of name lookup functionality to
name_lookup.*
2025-02-12 22:03:08 +00:00
Richard Smith c6d35e1c4a Rename template constant -> concrete constant. (#4939)
This implements a direction decided in a
[recent
discussion](https://docs.google.com/document/d/1Iut5f2TQBrtBNIduF4vJYOKfw7MbS8xH_J01_Q4e6Rk/edit?resourcekey=0-mc_vh5UzrzXfU4kO-3tOjA&tab=t.0#heading=h.mas1g68xx9ct)
to switch away from "template constant" when naming a constant that
doesn't depend on any generic parameters, because that creates confusion
with template-dependent constant values that depend on a template
parameter.
2025-02-12 21:24:51 +00:00
Jon Ross-Perkins 188821ba1a Fold Context::GetCurrentScopeAs back into ScopeStack (#4936)
This adds a `SemIR::File` pointer to `scope_stack` so that
`GetCurrentScopeAs` doesn't require the argument, which would be why the
helper existed on `context`.
2025-02-12 21:01:19 +00:00