Files
carbon-lang/toolchain/check/testdata/named_constraint/generic.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

226 lines
13 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/none.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/named_constraint/generic.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/named_constraint/generic.carbon
// --- generic.carbon
library "[[@TEST_NAME]]";
//@include-in-dumps
constraint GenericType(T: type) {}
interface Z {}
constraint GenericZ(U: Z) {}
constraint ForwardDeclaredGeneric(T: type);
constraint ForwardDeclaredGeneric(T: type) {}
// --- fail_mismatched_generic_param_types.carbon
library "[[@TEST_NAME]]";
constraint ForwardDeclaredGeneric(T: ());
// CHECK:STDERR: fail_mismatched_generic_param_types.carbon:[[@LINE+3]]:35-41: error: type `<pattern for type>` of parameter 1 in redeclaration differs from previous parameter type `<pattern for ()>` [RedeclParamDiffersType]
// CHECK:STDERR: fail_mismatched_generic_param_types.carbon:[[@LINE-2]]:35-39: note: previous declaration's corresponding parameter here [RedeclParamPrevious]
// CHECK:STDERR:
constraint ForwardDeclaredGeneric(T: type) {}
// --- fail_mismatched_generic_param_arity.carbon
library "[[@TEST_NAME]]";
constraint ForwardDeclaredGeneric(T: type, U: type);
// CHECK:STDERR: fail_mismatched_generic_param_arity.carbon:[[@LINE+3]]:1-44: error: redeclaration differs because of parameter count of 1 [RedeclParamCountDiffers]
// CHECK:STDERR: fail_mismatched_generic_param_arity.carbon:[[@LINE-2]]:1-52: note: previously declared with parameter count of 2 [RedeclParamCountPrevious]
// CHECK:STDERR:
constraint ForwardDeclaredGeneric(T: type) {}
// --- fail_inside_definition.carbon
library "[[@TEST_NAME]]";
constraint Generic(T: type) {}
// CHECK:STDERR: fail_inside_definition.carbon:[[@LINE+3]]:1-29: error: redefinition of `constraint Generic` [RedeclRedef]
// CHECK:STDERR: fail_inside_definition.carbon:[[@LINE-2]]:1-29: note: previously defined here [RedeclPrevDef]
// CHECK:STDERR:
constraint Generic(T: type) {}
// --- fail_args_count_mismatch.carbon
library "[[@TEST_NAME]]";
constraint Generic(T: type) {}
// CHECK:STDERR: fail_args_count_mismatch.carbon:[[@LINE+3]]:24-38: error: 2 arguments passed to generic constraint expecting 1 argument [CallArgCountMismatch]
// CHECK:STDERR: fail_args_count_mismatch.carbon:[[@LINE-3]]:1-29: note: calling generic constraint declared here [InCallToEntity]
// CHECK:STDERR:
fn F(unused generic T: Generic((), ())) {}
// CHECK:STDOUT: --- generic.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %.Self.frozen: type = symbolic_binding .Self [symbolic_self]
// CHECK:STDOUT: %pattern_type.9a5: type = pattern_type type [concrete]
// CHECK:STDOUT: %T.patt: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic]
// CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
// CHECK:STDOUT: %GenericType.type.d1d: type = generic_named_constaint_type @GenericType [concrete]
// CHECK:STDOUT: %empty_struct.b7e: %GenericType.type.d1d = struct_value () [concrete]
// CHECK:STDOUT: %GenericType.type.d68: type = facet_type <@GenericType, @GenericType(%T)> [symbolic]
// CHECK:STDOUT: %Self.22bbf7.1: %GenericType.type.d68 = symbolic_binding Self, 1 [symbolic]
// CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
// CHECK:STDOUT: %Self.cb6: %Z.type = symbolic_binding Self, 0 [symbolic]
// CHECK:STDOUT: %pattern_type.5d7: type = pattern_type %Z.type [concrete]
// CHECK:STDOUT: %U.patt: %pattern_type.5d7 = symbolic_binding_pattern U, 0 [symbolic]
// CHECK:STDOUT: %U: %Z.type = symbolic_binding U, 0 [symbolic]
// CHECK:STDOUT: %GenericZ.type.c42: type = generic_named_constaint_type @GenericZ [concrete]
// CHECK:STDOUT: %empty_struct.a73: %GenericZ.type.c42 = struct_value () [concrete]
// CHECK:STDOUT: %GenericZ.type.d68: type = facet_type <@GenericZ, @GenericZ(%U)> [symbolic]
// CHECK:STDOUT: %Self.22bbf7.2: %GenericZ.type.d68 = symbolic_binding Self, 1 [symbolic]
// CHECK:STDOUT: %ForwardDeclaredGeneric.type.650: type = generic_named_constaint_type @ForwardDeclaredGeneric [concrete]
// CHECK:STDOUT: %empty_struct.43d: %ForwardDeclaredGeneric.type.650 = struct_value () [concrete]
// CHECK:STDOUT: %ForwardDeclaredGeneric.type.d68: type = facet_type <@ForwardDeclaredGeneric, @ForwardDeclaredGeneric(%T)> [symbolic]
// CHECK:STDOUT: %Self.22bbf7.3: %ForwardDeclaredGeneric.type.d68 = symbolic_binding Self, 1 [symbolic]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .GenericType = %GenericType.decl
// CHECK:STDOUT: .Z = %Z.decl
// CHECK:STDOUT: .GenericZ = %GenericZ.decl
// CHECK:STDOUT: .ForwardDeclaredGeneric = %ForwardDeclaredGeneric.decl.loc9
// CHECK:STDOUT: }
// CHECK:STDOUT: %GenericType.decl: %GenericType.type.d1d = constraint_decl @GenericType [concrete = constants.%empty_struct.b7e] {
// CHECK:STDOUT: %T.patt.loc4_25.1: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc4_25.2 (constants.%T.patt)]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc4_27.1: type = splice_block %.loc4_27.2 [concrete = type] {
// CHECK:STDOUT: %.Self.frozen: type = symbolic_binding .Self [symbolic_self = constants.%.Self.frozen]
// CHECK:STDOUT: %.loc4_27.2: type = type_literal type [concrete = type]
// CHECK:STDOUT: }
// CHECK:STDOUT: %T.loc4_25.2: type = symbolic_binding T, 0 [symbolic = %T.loc4_25.1 (constants.%T)]
// CHECK:STDOUT: }
// CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {}
// CHECK:STDOUT: %GenericZ.decl: %GenericZ.type.c42 = constraint_decl @GenericZ [concrete = constants.%empty_struct.a73] {
// CHECK:STDOUT: %U.patt.loc7_22.1: %pattern_type.5d7 = symbolic_binding_pattern U, 0 [symbolic = %U.patt.loc7_22.2 (constants.%U.patt)]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc7: type = splice_block %Z.ref [concrete = constants.%Z.type] {
// CHECK:STDOUT: %.Self.frozen: type = symbolic_binding .Self [symbolic_self = constants.%.Self.frozen]
// CHECK:STDOUT: %Z.ref: type = name_ref Z, file.%Z.decl [concrete = constants.%Z.type]
// CHECK:STDOUT: }
// CHECK:STDOUT: %U.loc7_22.2: %Z.type = symbolic_binding U, 0 [symbolic = %U.loc7_22.1 (constants.%U)]
// CHECK:STDOUT: }
// CHECK:STDOUT: %ForwardDeclaredGeneric.decl.loc9: %ForwardDeclaredGeneric.type.650 = constraint_decl @ForwardDeclaredGeneric [concrete = constants.%empty_struct.43d] {
// CHECK:STDOUT: %T.patt.loc10: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc9 (constants.%T.patt)]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc9_38.1: type = splice_block %.loc9_38.2 [concrete = type] {
// CHECK:STDOUT: %.Self.frozen.loc9: type = symbolic_binding .Self [symbolic_self = constants.%.Self.frozen]
// CHECK:STDOUT: %.loc9_38.2: type = type_literal type [concrete = type]
// CHECK:STDOUT: }
// CHECK:STDOUT: %T.loc9_36.2: type = symbolic_binding T, 0 [symbolic = %T.loc9_36.1 (constants.%T)]
// CHECK:STDOUT: }
// CHECK:STDOUT: %ForwardDeclaredGeneric.decl.loc10: %ForwardDeclaredGeneric.type.650 = constraint_decl @ForwardDeclaredGeneric [concrete = constants.%empty_struct.43d] {
// CHECK:STDOUT: %T.patt.loc10: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc9 (constants.%T.patt)]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc10_38.1: type = splice_block %.loc10_38.2 [concrete = type] {
// CHECK:STDOUT: %.Self.frozen.loc10: type = symbolic_binding .Self [symbolic_self = constants.%.Self.frozen]
// CHECK:STDOUT: %.loc10_38.2: type = type_literal type [concrete = type]
// CHECK:STDOUT: }
// CHECK:STDOUT: %T.loc10: type = symbolic_binding T, 0 [symbolic = %T.loc9_36.1 (constants.%T)]
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: interface @Z {
// CHECK:STDOUT: %Self: %Z.type = symbolic_binding Self, 0 [symbolic = constants.%Self.cb6]
// CHECK:STDOUT: %Z.WithSelf.decl = interface_with_self_decl @Z [concrete]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = %Self
// CHECK:STDOUT: witness = ()
// CHECK:STDOUT:
// CHECK:STDOUT: !requires:
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic constraint @GenericType(%T.loc4_25.2: type) {
// CHECK:STDOUT: %T.patt.loc4_25.2: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc4_25.2 (constants.%T.patt)]
// CHECK:STDOUT: %T.loc4_25.1: type = symbolic_binding T, 0 [symbolic = %T.loc4_25.1 (constants.%T)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %GenericType.type: type = facet_type <@GenericType, @GenericType(%T.loc4_25.1)> [symbolic = %GenericType.type (constants.%GenericType.type.d68)]
// CHECK:STDOUT: %Self.loc4_33.2: @GenericType.%GenericType.type (%GenericType.type.d68) = symbolic_binding Self, 1 [symbolic = %Self.loc4_33.2 (constants.%Self.22bbf7.1)]
// CHECK:STDOUT:
// CHECK:STDOUT: constraint {
// CHECK:STDOUT: %Self.loc4_33.1: @GenericType.%GenericType.type (%GenericType.type.d68) = symbolic_binding Self, 1 [symbolic = %Self.loc4_33.2 (constants.%Self.22bbf7.1)]
// CHECK:STDOUT: %GenericType.WithSelf.decl = constraint_with_self_decl @GenericType [concrete]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = %Self.loc4_33.1
// CHECK:STDOUT:
// CHECK:STDOUT: !requires:
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic constraint @GenericZ(%U.loc7_22.2: %Z.type) {
// CHECK:STDOUT: %U.patt.loc7_22.2: %pattern_type.5d7 = symbolic_binding_pattern U, 0 [symbolic = %U.patt.loc7_22.2 (constants.%U.patt)]
// CHECK:STDOUT: %U.loc7_22.1: %Z.type = symbolic_binding U, 0 [symbolic = %U.loc7_22.1 (constants.%U)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %GenericZ.type: type = facet_type <@GenericZ, @GenericZ(%U.loc7_22.1)> [symbolic = %GenericZ.type (constants.%GenericZ.type.d68)]
// CHECK:STDOUT: %Self.loc7_27.2: @GenericZ.%GenericZ.type (%GenericZ.type.d68) = symbolic_binding Self, 1 [symbolic = %Self.loc7_27.2 (constants.%Self.22bbf7.2)]
// CHECK:STDOUT:
// CHECK:STDOUT: constraint {
// CHECK:STDOUT: %Self.loc7_27.1: @GenericZ.%GenericZ.type (%GenericZ.type.d68) = symbolic_binding Self, 1 [symbolic = %Self.loc7_27.2 (constants.%Self.22bbf7.2)]
// CHECK:STDOUT: %GenericZ.WithSelf.decl = constraint_with_self_decl @GenericZ [concrete]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = %Self.loc7_27.1
// CHECK:STDOUT:
// CHECK:STDOUT: !requires:
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic constraint @ForwardDeclaredGeneric(%T.loc9_36.2: type) {
// CHECK:STDOUT: %T.patt.loc9: %pattern_type.9a5 = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc9 (constants.%T.patt)]
// CHECK:STDOUT: %T.loc9_36.1: type = symbolic_binding T, 0 [symbolic = %T.loc9_36.1 (constants.%T)]
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %ForwardDeclaredGeneric.type: type = facet_type <@ForwardDeclaredGeneric, @ForwardDeclaredGeneric(%T.loc9_36.1)> [symbolic = %ForwardDeclaredGeneric.type (constants.%ForwardDeclaredGeneric.type.d68)]
// CHECK:STDOUT: %Self.loc10_44.2: @ForwardDeclaredGeneric.%ForwardDeclaredGeneric.type (%ForwardDeclaredGeneric.type.d68) = symbolic_binding Self, 1 [symbolic = %Self.loc10_44.2 (constants.%Self.22bbf7.3)]
// CHECK:STDOUT:
// CHECK:STDOUT: constraint {
// CHECK:STDOUT: %Self.loc10_44.1: @ForwardDeclaredGeneric.%ForwardDeclaredGeneric.type (%ForwardDeclaredGeneric.type.d68) = symbolic_binding Self, 1 [symbolic = %Self.loc10_44.2 (constants.%Self.22bbf7.3)]
// CHECK:STDOUT: %ForwardDeclaredGeneric.WithSelf.decl = constraint_with_self_decl @ForwardDeclaredGeneric [concrete]
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = %Self.loc10_44.1
// CHECK:STDOUT:
// CHECK:STDOUT: !requires:
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @GenericType(constants.%T) {
// CHECK:STDOUT: %T.patt.loc4_25.2 => constants.%T.patt
// CHECK:STDOUT: %T.loc4_25.1 => constants.%T
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @GenericType.WithSelf(constants.%T, constants.%Self.22bbf7.1) {}
// CHECK:STDOUT:
// CHECK:STDOUT: specific @Z.WithSelf(constants.%Self.cb6) {}
// CHECK:STDOUT:
// CHECK:STDOUT: specific @GenericZ(constants.%U) {
// CHECK:STDOUT: %U.patt.loc7_22.2 => constants.%U.patt
// CHECK:STDOUT: %U.loc7_22.1 => constants.%U
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @GenericZ.WithSelf(constants.%U, constants.%Self.22bbf7.2) {}
// CHECK:STDOUT:
// CHECK:STDOUT: specific @ForwardDeclaredGeneric(constants.%T) {
// CHECK:STDOUT: %T.patt.loc9 => constants.%T.patt
// CHECK:STDOUT: %T.loc9_36.1 => constants.%T
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @ForwardDeclaredGeneric.WithSelf(constants.%T, constants.%Self.22bbf7.3) {}
// CHECK:STDOUT: