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

224 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/primitives.carbon
// TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
// EXTRA-ARGS: --dump-sem-ir-ranges=if-present
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/if_expr/fail_partial_constant.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/fail_partial_constant.carbon
// --- fail_non_constant_condition.carbon
package NonConstantCondition;
fn ConditionIsNonConstant(b: bool) {
// We choose to not accept this even if both arms evaluate to the same
// constant value, because it notionally involves evaluating a non-constant
// condition.
// CHECK:STDERR: fail_non_constant_condition.carbon:[[@LINE+2]]:17-38: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR:
var unused v: if b then i32 else i32 = 1;
}
// --- fail_non_constant_result.carbon
package NonConstantResult;
fn ChosenBranchIsNonConstant(t: type) {
// CHECK:STDERR: fail_non_constant_result.carbon:[[@LINE+2]]:17-39: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR:
var unused v: if true then t else i32 = 1;
// CHECK:STDERR: fail_non_constant_result.carbon:[[@LINE+2]]:17-40: error: cannot evaluate type expression [TypeExprEvaluationFailure]
// CHECK:STDERR:
var unused w: if false then i32 else t = 1;
}
// CHECK:STDOUT: --- fail_non_constant_condition.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
// CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern [concrete]
// CHECK:STDOUT: %b.patt: %pattern_type.831 = wrapper_binding_pattern b, %b.param_patt [concrete]
// CHECK:STDOUT: %ConditionIsNonConstant.type: type = fn_type @ConditionIsNonConstant [concrete]
// CHECK:STDOUT: %ConditionIsNonConstant: %ConditionIsNonConstant.type = struct_value () [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %Int.type: type = parameterized_class_name_type @Int [concrete]
// CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
// CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: .Bool = %Core.Bool
// CHECK:STDOUT: .Int = %Core.Int
// CHECK:STDOUT: .Destroy = %Core.Destroy
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Bool: type = import_ref Core//prelude/parts/bool, Bool, loaded [concrete = bool]
// CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
// CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %ConditionIsNonConstant.decl: %ConditionIsNonConstant.type = fn_decl @ConditionIsNonConstant [concrete = constants.%ConditionIsNonConstant] {
// CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern [concrete = constants.%b.param_patt]
// CHECK:STDOUT: %b.patt: %pattern_type.831 = wrapper_binding_pattern b, %b.param_patt [concrete = constants.%b.patt]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %b.param: bool = value_param call_param0
// CHECK:STDOUT: %.loc4: type = type_literal bool [concrete = bool]
// CHECK:STDOUT: %b: bool = wrapper_binding b, %b.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConditionIsNonConstant(%b.param: bool) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %v.var: ref <error> = var_storage %v.var_patt [concrete = <error>]
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
// CHECK:STDOUT: assign %v.var, <error>
// CHECK:STDOUT: br !.loc10_20
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc10_20:
// CHECK:STDOUT: %b.ref: bool = name_ref b, %b
// CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then:
// CHECK:STDOUT: %i32.loc10_27: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: br !if.expr.result(%i32.loc10_27)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else:
// CHECK:STDOUT: %i32.loc10_36: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: br !if.expr.result(%i32.loc10_36)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result:
// CHECK:STDOUT: %.loc10: type = block_arg !if.expr.result
// CHECK:STDOUT: br !.loc10_15
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc10_15:
// CHECK:STDOUT: %v: <error> = wrapper_binding v, <error> [concrete = <error>]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %v.patt: <error> = ref_binding_pattern v [concrete = <error>]
// CHECK:STDOUT: %v.var_patt: <error> = var_pattern %v.patt [concrete = <error>]
// CHECK:STDOUT: }
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_non_constant_result.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %pattern_type.9a5: type = pattern_type type [concrete]
// CHECK:STDOUT: %t.param_patt: %pattern_type.9a5 = value_param_pattern [concrete]
// CHECK:STDOUT: %t.patt: %pattern_type.9a5 = wrapper_binding_pattern t, %t.param_patt [concrete]
// CHECK:STDOUT: %ChosenBranchIsNonConstant.type: type = fn_type @ChosenBranchIsNonConstant [concrete]
// CHECK:STDOUT: %ChosenBranchIsNonConstant: %ChosenBranchIsNonConstant.type = struct_value () [concrete]
// CHECK:STDOUT: %true: bool = bool_literal true [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %Int.type: type = parameterized_class_name_type @Int [concrete]
// CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
// CHECK:STDOUT: %false: bool = bool_literal false [concrete]
// CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: .Int = %Core.Int
// CHECK:STDOUT: .Destroy = %Core.Destroy
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
// CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %ChosenBranchIsNonConstant.decl: %ChosenBranchIsNonConstant.type = fn_decl @ChosenBranchIsNonConstant [concrete = constants.%ChosenBranchIsNonConstant] {
// CHECK:STDOUT: %t.param_patt: %pattern_type.9a5 = value_param_pattern [concrete = constants.%t.param_patt]
// CHECK:STDOUT: %t.patt: %pattern_type.9a5 = wrapper_binding_pattern t, %t.param_patt [concrete = constants.%t.patt]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %t.param: type = value_param call_param0
// CHECK:STDOUT: %.loc4: type = type_literal type [concrete = type]
// CHECK:STDOUT: %t: type = wrapper_binding t, %t.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t.param: type) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %v.var: ref <error> = var_storage %v.var_patt [concrete = <error>]
// CHECK:STDOUT: %int_1.loc7: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
// CHECK:STDOUT: assign %v.var, <error>
// CHECK:STDOUT: br !.loc7_20
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc7_20:
// CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
// CHECK:STDOUT: if %true br !if.expr.then.loc7 else br !if.expr.else.loc7
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then.loc7:
// CHECK:STDOUT: %t.ref.loc7: type = name_ref t, %t
// CHECK:STDOUT: br !if.expr.result.loc7(%t.ref.loc7)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else.loc7:
// CHECK:STDOUT: %i32.loc7: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: br !if.expr.result.loc7(%i32.loc7)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result.loc7:
// CHECK:STDOUT: %.loc7: type = block_arg !if.expr.result.loc7
// CHECK:STDOUT: br !.loc7_15
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc7_15:
// CHECK:STDOUT: %v: <error> = wrapper_binding v, <error> [concrete = <error>]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %v.patt: <error> = ref_binding_pattern v [concrete = <error>]
// CHECK:STDOUT: %v.var_patt: <error> = var_pattern %v.patt [concrete = <error>]
// CHECK:STDOUT: }
// CHECK:STDOUT: %w.var: ref <error> = var_storage %w.var_patt [concrete = <error>]
// CHECK:STDOUT: %int_1.loc10: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
// CHECK:STDOUT: assign %w.var, <error>
// CHECK:STDOUT: br !.loc10_20
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc10_20:
// CHECK:STDOUT: %false: bool = bool_literal false [concrete = constants.%false]
// CHECK:STDOUT: if %false br !if.expr.then.loc10 else br !if.expr.else.loc10
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then.loc10:
// CHECK:STDOUT: %i32.loc10: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: br !if.expr.result.loc10(%i32.loc10)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else.loc10:
// CHECK:STDOUT: %t.ref.loc10: type = name_ref t, %t
// CHECK:STDOUT: br !if.expr.result.loc10(%t.ref.loc10)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result.loc10:
// CHECK:STDOUT: %.loc10: type = block_arg !if.expr.result.loc10
// CHECK:STDOUT: br !.loc10_15
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc10_15:
// CHECK:STDOUT: %w: <error> = wrapper_binding w, <error> [concrete = <error>]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %w.patt: <error> = ref_binding_pattern w [concrete = <error>]
// CHECK:STDOUT: %w.var_patt: <error> = var_pattern %w.patt [concrete = <error>]
// CHECK:STDOUT: }
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: