Files
carbon-lang/toolchain/check/testdata/impl/impl_as_named_constraint.carbon
T
Chandler Carruth 2c9739799b Render diagnostics through the terminal library (#7683)
Diagnostic rendering was part of the data model: `Loc::FormatLocation`
and
`Loc::FormatSnippet` wrote plain text to a stream, so no capability,
color, or
terminal width could reach them.

A diagnostic is now one message with labels, contexts, and location
information
attached, and `Diagnostics::Renderer` draws it into a
`Terminal::Buffer`. The
design is in `toolchain/docs/diagnostics_rendering.md` (#7668),
authoring in
`toolchain/docs/diagnostics.md`.

Before:

```
example.carbon:6:3: error: 1 argument passed to function expecting 0 arguments
  Run0(1);
  ^~~~~~~
example.carbon:3:1: note: calling function declared here
fn Run0() {}
^~~~~~~~~~~
```

After:

```
error: 1 argument passed to function expecting 0 arguments
       ╭─┤ example.carbon:6:3
       ┆
     3 │ fn Run0() {}
       · ─────┬─────
       ·      ╰──┤ calling function declared here
       ┆
->   6 │   Run0(1);
       ·   ───────
       ╰────────────
```

`--no-diagnostic-snippets` gives a compact form, `example.carbon:6:3-9`.
File
tests use it; the rendering tests under `toolchain/diagnostics/testdata`
ask for
snippets.

C++ interop diagnostics are data now rather than text Clang drew:
`SemIR::ClangSourceLocId` names a `clang::SourceRange`, and a Clang
location is
marked as the point it names.

`StreamConsumer` moves to a `:renderer` target that `:emitter` does not
depend
on, keeping `common/terminal` out of `consumer.h`. Capabilities come
from the
file behind the error stream, so tests get plain ASCII.

`renderer_fuzzer` draws diagnostics built from fuzzer bytes with every
capability combination, since a shape the renderer doesn't expect must
never be
why the compiler dies while reporting a problem.

TODOs mark the follow-ups: line weights and crossing lines, wrapping
that keeps
a quoted type whole, and ordering diagnostics that share a byte offset.

Also: `IncorrectExtension`'s hint moves into its message, since its note
had no
location of its own, and `strip_noise` in `check_diagnostics.py` blanks
multi-line `#define`s instead of deleting them.

Assisted-by: Claude Code
2026-10-15 06:08:57 +00:00

426 lines
11 KiB
Plaintext

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/impl_as_named_constraint.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/impl_as_named_constraint.carbon
// --- fail_empty_constraint.carbon
library "[[@TEST_NAME]]";
constraint A {}
// CHECK:STDERR: fail_empty_constraint.carbon:[[@LINE+2]]:1-14: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
// CHECK:STDERR:
impl () as A {}
// --- fail_too_many_interfaces_in_constraint.carbon
library "[[@TEST_NAME]]";
interface A1 {}
interface A2 {}
constraint B {
extend require impls A1;
extend require impls A2;
}
// CHECK:STDERR: fail_too_many_interfaces_in_constraint.carbon:[[@LINE+2]]:1-14: error: impl as 2 interfaces, expected 1 [ImplOfNotOneInterface]
// CHECK:STDERR:
impl () as B {}
// --- one_extend_impls_interface_in_constraint.carbon
library "[[@TEST_NAME]]";
interface A {}
constraint B {
extend require impls A;
}
impl () as B {}
// --- fail_one_impls_interface_in_constraint.carbon
library "[[@TEST_NAME]]";
interface A;
constraint B {
require impls A;
}
// CHECK:STDERR: fail_one_impls_interface_in_constraint.carbon:[[@LINE+2]]:1-14: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
// CHECK:STDERR:
impl () as B {}
// --- nested_constraints.carbon
library "[[@TEST_NAME]]";
interface A {}
constraint B {
extend require impls A;
}
constraint C {
extend require impls B;
}
impl () as C {}
// --- fail_nested_constraints_not_extend_outer.carbon
library "[[@TEST_NAME]]";
interface A {}
constraint B {
extend require impls A;
}
constraint C {
require impls B;
}
// CHECK:STDERR: fail_nested_constraints_not_extend_outer.carbon:[[@LINE+2]]:1-14: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
// CHECK:STDERR:
impl () as C {}
// --- fail_nested_constraints_not_extend_inner.carbon
library "[[@TEST_NAME]]";
interface A {}
constraint B {
require impls A;
}
constraint C {
extend require impls B;
}
// CHECK:STDERR: fail_nested_constraints_not_extend_inner.carbon:[[@LINE+2]]:1-14: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
// CHECK:STDERR:
impl () as C {}
// --- fail_nested_constraints_not_extend_both.carbon
library "[[@TEST_NAME]]";
interface A {}
constraint B {
require impls A;
}
constraint C {
require impls B;
}
// CHECK:STDERR: fail_nested_constraints_not_extend_both.carbon:[[@LINE+2]]:1-14: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
// CHECK:STDERR:
impl () as C {}
// --- fail_duplicate_through_generic_constraint.carbon
library "[[@TEST_NAME]]";
interface A(T: type) {}
constraint B(X: type, Y: type) {
extend require impls A(Y);
}
// This should impl () as A({}).
impl () as B((), {}) {}
// CHECK:STDERR: fail_duplicate_through_generic_constraint.carbon:[[@LINE+3]]:1-18: error: found non-final `impl` with the same type structure as another non-final `impl` [ImplNonFinalSameTypeStructure]
// CHECK:STDERR: fail_duplicate_through_generic_constraint.carbon:[[@LINE-3]]:1-22: note: other `impl` here [ImplNonFinalSameTypeStructureNote]
// CHECK:STDERR:
impl () as A({}) {}
// --- fail_error_self_in_require.carbon
library "[[@TEST_NAME]]";
interface A(T: type) {}
// This makes the type of `Self` into an ErrorInst. No `require` is added to
// the constraint.
//@dump-sem-ir-begin
//
// CHECK:STDERR: fail_error_self_in_require.carbon:[[@LINE+2]]:17-25: error: name `Undefined` not found [NameNotFound]
// CHECK:STDERR:
constraint B(X: Undefined) {
extend require impls A(X);
}
//@dump-sem-ir-end
impl () as B(1) {}
// --- impl_as_constraint_where_rewrite.carbon
library "[[@TEST_NAME]]";
interface I {
let I1: type;
}
constraint N {
extend require impls I;
}
class C;
impl C as N where .I1 = C {}
fn F(generic _: I where .I1 = .Self) {}
fn G() {
F(C);
}
// --- impl_as_concrete_generic_constraint_where_rewrite.carbon
library "[[@TEST_NAME]]";
interface I(T: type) {
let I1: type;
}
class C;
constraint N {
extend require impls I(C);
}
impl C as N where .I1 = C {}
fn F(generic _: I(.Self) where .I1 = .Self) {}
fn G() {
F(C);
}
// --- impl_as_symbolic_generic_constraint_where_rewrite.carbon
library "[[@TEST_NAME]]";
interface I(T: type) {
let I1: type;
}
constraint N {
extend require impls I(Self);
}
class C;
impl C as N where .I1 = C {}
fn F(generic _: I(.Self) where .I1 = .Self) {}
fn G() {
F(C);
}
// --- impl_as_constraint_containing_rewrite_for_concrete_interface.carbon
library "[[@TEST_NAME]]";
interface I {
let I1: type;
}
constraint N {
extend require impls I where .I1 = Self;
}
class C;
impl C as N {}
fn F(generic _: I where .I1 = .Self) {}
fn G() {
F(C);
}
// --- impl_as_constraint_containing_rewrite_for_generic_interface.carbon
library "[[@TEST_NAME]]";
interface I(T: type) {
let I1: type;
}
constraint N {
extend require impls I(Self) where .I1 = Self;
}
class C;
impl C as N {}
fn F(generic _: I(.Self) where .I1 = .Self) {}
fn G() {
F(C);
}
// --- fail_impl_as_constraint_missing_rewrite.carbon
library "[[@TEST_NAME]]";
interface I {
let I1: type;
let I2: type;
}
constraint N {
extend require impls I where .I2 = .I1;
}
class C;
// TODO: Since these errors have the same location, it would be nice to omit
// the first one. Once rewrites can be done in the impl defn body, these errors
// won't always have the same location (with the location being the decl)
// though.
// CHECK:STDERR: fail_impl_as_constraint_missing_rewrite.carbon:[[@LINE+5]]:1-13: error: accessing member from impl before it has a defined value [ImplAccessMemberBeforeSet]
// CHECK:STDERR:
// CHECK:STDERR: fail_impl_as_constraint_missing_rewrite.carbon:[[@LINE+3]]:1-13: error: associated constant I1 not given a value in impl of interface I [ImplAssociatedConstantNeedsValue]
// CHECK:STDERR: fail_impl_as_constraint_missing_rewrite.carbon:[[@LINE-16]]:7-14: note: associated constant declared here [AssociatedConstantHere]
// CHECK:STDERR:
impl C as N {}
// --- fail_no_require_self.carbon
library "[[@TEST_NAME]]";
interface Z(T: type) {}
class C;
constraint Y {
// `Self` must appear somewhere in the require decl, so it's in the interface
// specific because we want to test a different self-type.
require C impls Z(Self);
}
// Y does not have any extend require on `Self`.
// CHECK:STDERR: fail_no_require_self.carbon:[[@LINE+2]]:1-14: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
// CHECK:STDERR:
impl () as Y {}
// --- member_access_in_extend_require.carbon
library "[[@TEST_NAME]]";
interface I { let X: type; }
interface J {}
impl () as J {}
constraint GivesI {
extend require impls I where .X impls J;
}
// The access of `.X` in the named constraint contains a witness of
// `Self as GivesI` which becomes `T as GivesI` in the context of this
// impl. This tests that the access `.X` is replaced by `()` from the
// impl, and doesnt form any cycles in checking.
impl forall [T: type] T as GivesI where .X = () {}
// --- require_specific_used_in_impl_as_then_outside.carbon
library "[[@TEST_NAME]]";
interface I { let X: type; }
interface J {}
interface K {}
class C {}
impl C as J {}
impl C as K {}
class D {}
impl D as J {}
constraint GivesI {
extend require impls I where .X impls J;
}
match_first {
impl () as I where .X = C {}
// Never selected for `() as I`. Declaring this impl forms the specific of
// the require in `GivesI` with `Self = ()`, where `.X` refers to this
// impl. The require's specific must not permanently encode that decision
// into future uses though.
impl () as GivesI where .X = D {}
}
fn F(generic _: GivesI where .X impls K) {}
fn G() {
// Reuses the specific of the require in `GivesI` with `Self = ()`. The
// `.X` should perform a fresh impl lookup here and should resolve to `C`. If
// the specific encoded the attachment to the second impl instead, it may
// resolve to `D` (which doesn't impl `K`) or it may not resolve beyond the
// symbolic `.X` at all.
F(());
}
// --- witness_access_in_named_constraint.carbon
library "[[@TEST_NAME]]";
class C {}
class G(T: type) {}
interface Z(R: type) {
let Result: type;
}
interface Y(S: type) {}
impl forall [T: type] () as Y(G(T)) {}
// This is a blanket impl that overlaps with (but is less specific than)
// `G(C) as Z(G(C))`, which is the next impl below.
impl forall [T: type, U: type] G(T) as Z(U) where .Result = {} {}
constraint N {
// Forming the specific with `Self=G(C)` must not choose a concrete value
// `.Result = {}` from the impl above, even though the query is concrete.
// The `impl G(C) as N` sets the `.Result = ()`.
//
// This specific is formed while checking the `impl G(C) as N` decl, and the
// `.Result` must remain a reference to that impl's witness table not some
// other impl.
//
// We have `.Result impls...` to ensure that we substitute `G(C)` into the
// `.Result` access while checking the impl decl, as rewrite constraints
// may be ignored until later.
extend require impls Z(G(C)) where .Result impls Y(G(C)) and .Result = ();
}
impl G(C) as N {}
fn F() -> G(C).(Z(G(C)).Result) {
return ();
}
// CHECK:STDOUT: --- fail_error_self_in_require.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %B.type: type = generic_named_constaint_type @B [concrete]
// CHECK:STDOUT: %empty_struct: %B.type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %B.decl: %B.type = constraint_decl @B [concrete = constants.%empty_struct] {
// CHECK:STDOUT: %X.patt: <error> = symbolic_binding_pattern X, 0 [concrete = <error>]
// CHECK:STDOUT: } {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT: %X: <error> = symbolic_binding X, 0 [concrete = <error>]
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic constraint @B(%X: <error>) {
// CHECK:STDOUT: !definition:
// CHECK:STDOUT:
// CHECK:STDOUT: constraint {
// CHECK:STDOUT: %Self: <error> = symbolic_binding Self, 1 [concrete = <error>]
// CHECK:STDOUT: %B.WithSelf.decl = constraint_with_self_decl @B [concrete]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = %Self
// CHECK:STDOUT: .A = <poisoned>
// CHECK:STDOUT: .X = <poisoned>
// CHECK:STDOUT: .A = <poisoned>
// CHECK:STDOUT: .X = <poisoned>
// CHECK:STDOUT: has_error
// CHECK:STDOUT:
// CHECK:STDOUT: !requires:
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @B(<error>) {}
// CHECK:STDOUT:
// CHECK:STDOUT: specific @B.WithSelf(<error>, <error>) {}
// CHECK:STDOUT: