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

262 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/bool.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/if/basics.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if/basics.carbon
// --- else.carbon
library "[[@TEST_NAME]]";
fn F() {}
fn G() {}
fn H() {}
//@dump-sem-ir-begin
fn If(b: bool) {
if (b) {
F();
} else {
G();
}
H();
}
//@dump-sem-ir-end
// --- no_else.carbon
library "[[@TEST_NAME]]";
fn F() {}
fn G() {}
//@dump-sem-ir-begin
fn If(b: bool) {
if (b) {
F();
}
G();
}
//@dump-sem-ir-end
// --- unreachable_fallthrough.carbon
library "[[@TEST_NAME]]";
//@dump-sem-ir-begin
fn If(b: bool) -> bool {
if (b) {
return false;
} else {
return true;
}
// Missing return here is OK.
}
//@dump-sem-ir-end
// --- fail_reachable_fallthrough.carbon
library "[[@TEST_NAME]]";
fn If1(b: bool) -> bool {
if (b) {
return false;
} else {
}
// CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+2]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
// CHECK:STDERR:
}
fn If2(b: bool) -> bool {
if (b) {
} else {
return true;
}
// CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+2]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
// CHECK:STDERR:
}
fn If3(b: bool) -> bool {
if (b) {
return false;
}
// CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+2]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
// CHECK:STDERR:
}
// --- fail_scope.carbon
library "[[@TEST_NAME]]";
fn VarScope(b: bool) -> bool {
if (b) {
var n: bool = true;
return n;
}
// CHECK:STDERR: fail_scope.carbon:[[@LINE+2]]:10: error: name `n` not found [NameNotFound]
// CHECK:STDERR:
return n;
}
// CHECK:STDOUT: --- else.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
// CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
// CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
// CHECK:STDOUT: %H.type: type = fn_type @H [concrete]
// CHECK:STDOUT: %H: %H.type = struct_value () [concrete]
// CHECK:STDOUT: %pattern_type: type = pattern_type bool [concrete]
// CHECK:STDOUT: %b.param_patt: %pattern_type = value_param_pattern [concrete]
// CHECK:STDOUT: %b.patt: %pattern_type = wrapper_binding_pattern b, %b.param_patt [concrete]
// CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
// CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
// CHECK:STDOUT: %b.param_patt: %pattern_type = value_param_pattern [concrete = constants.%b.param_patt]
// CHECK:STDOUT: %b.patt: %pattern_type = wrapper_binding_pattern b, %b.param_patt [concrete = constants.%b.patt]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %b.param: bool = value_param call_param0
// CHECK:STDOUT: %.loc8: type = type_literal bool [concrete = bool]
// CHECK:STDOUT: %b: bool = wrapper_binding b, %b.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @If(%b.param: bool) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %b.ref: bool = name_ref b, %b
// CHECK:STDOUT: if %b.ref br !if.then else br !if.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.then:
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
// CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
// CHECK:STDOUT: br !if.done
// CHECK:STDOUT:
// CHECK:STDOUT: !if.else:
// CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
// CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref()
// CHECK:STDOUT: br !if.done
// CHECK:STDOUT:
// CHECK:STDOUT: !if.done:
// CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [concrete = constants.%H]
// CHECK:STDOUT: %H.call: init %empty_tuple.type = call %H.ref()
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- no_else.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
// CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
// CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
// CHECK:STDOUT: %pattern_type: type = pattern_type bool [concrete]
// CHECK:STDOUT: %b.param_patt: %pattern_type = value_param_pattern [concrete]
// CHECK:STDOUT: %b.patt: %pattern_type = wrapper_binding_pattern b, %b.param_patt [concrete]
// CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
// CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
// CHECK:STDOUT: %b.param_patt: %pattern_type = value_param_pattern [concrete = constants.%b.param_patt]
// CHECK:STDOUT: %b.patt: %pattern_type = wrapper_binding_pattern b, %b.param_patt [concrete = constants.%b.patt]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %b.param: bool = value_param call_param0
// CHECK:STDOUT: %.loc7: type = type_literal bool [concrete = bool]
// CHECK:STDOUT: %b: bool = wrapper_binding b, %b.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @If(%b.param: bool) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %b.ref: bool = name_ref b, %b
// CHECK:STDOUT: if %b.ref br !if.then else br !if.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.then:
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
// CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
// CHECK:STDOUT: br !if.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.else:
// CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
// CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref()
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- unreachable_fallthrough.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: %.f34: Core.Form = init_form bool [concrete]
// CHECK:STDOUT: %return.param_patt.5a1: %pattern_type.831 = out_param_pattern [concrete]
// CHECK:STDOUT: %return.patt.c0c: %pattern_type.831 = return_slot_pattern %return.param_patt.5a1, bool [concrete]
// CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
// CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
// CHECK:STDOUT: %false: bool = bool_literal false [concrete]
// CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
// CHECK:STDOUT: %Copy.impl_witness.5e1: <witness> = impl_witness imports.%Copy.impl_witness_table.bda [concrete]
// CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value bool, (%Copy.impl_witness.5e1) [concrete]
// CHECK:STDOUT: %Copy.WithSelf.Op.type.c98: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet) [concrete]
// CHECK:STDOUT: %.68a: type = fn_type_with_self_type %Copy.WithSelf.Op.type.c98, %Copy.facet [concrete]
// CHECK:STDOUT: %bool.as.Copy.impl.Op.type: type = fn_type @bool.as.Copy.impl.Op [concrete]
// CHECK:STDOUT: %bool.as.Copy.impl.Op: %bool.as.Copy.impl.Op.type = struct_value () [concrete]
// CHECK:STDOUT: %bool.as.Copy.impl.Op.bound.84b: <bound method> = bound_method %false, %bool.as.Copy.impl.Op [concrete]
// CHECK:STDOUT: %true: bool = bool_literal true [concrete]
// CHECK:STDOUT: %bool.as.Copy.impl.Op.bound.1b3: <bound method> = bound_method %true, %bool.as.Copy.impl.Op [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core.import_ref.9c3: %bool.as.Copy.impl.Op.type = import_ref Core//prelude/parts/copy, loc{{\d+_\d+}}, loaded [concrete = constants.%bool.as.Copy.impl.Op]
// CHECK:STDOUT: %Copy.impl_witness_table.bda = impl_witness_table (%Core.import_ref.9c3), @bool.as.Copy.impl [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
// 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: %return.param_patt: %pattern_type.831 = out_param_pattern [concrete = constants.%return.param_patt.5a1]
// CHECK:STDOUT: %return.patt: %pattern_type.831 = return_slot_pattern %return.param_patt, %.loc4_19.1 [concrete = constants.%return.patt.c0c]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %.loc4_19.1: type = type_literal bool [concrete = bool]
// CHECK:STDOUT: %.loc4_19.2: Core.Form = init_form %.loc4_19.1 [concrete = constants.%.f34]
// CHECK:STDOUT: %b.param: bool = value_param call_param0
// CHECK:STDOUT: %.loc4_10: type = type_literal bool [concrete = bool]
// CHECK:STDOUT: %b: bool = wrapper_binding b, %b.param
// CHECK:STDOUT: %return.param: ref bool = out_param call_param1
// CHECK:STDOUT: %return: ref bool = return_slot %return.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @If(%b.param: bool) -> out %return.param: bool {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %b.ref: bool = name_ref b, %b
// CHECK:STDOUT: if %b.ref br !if.then else br !if.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.then:
// CHECK:STDOUT: %false: bool = bool_literal false [concrete = constants.%false]
// CHECK:STDOUT: %impl.elem0.loc6: %.68a = impl_witness_access constants.%Copy.impl_witness.5e1, element0 [concrete = constants.%bool.as.Copy.impl.Op]
// CHECK:STDOUT: %bound_method.loc6: <bound method> = bound_method %false, %impl.elem0.loc6 [concrete = constants.%bool.as.Copy.impl.Op.bound.84b]
// CHECK:STDOUT: %bool.as.Copy.impl.Op.call.loc6: init bool = call %bound_method.loc6(%false) [concrete = constants.%false]
// CHECK:STDOUT: return %bool.as.Copy.impl.Op.call.loc6
// CHECK:STDOUT:
// CHECK:STDOUT: !if.else:
// CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
// CHECK:STDOUT: %impl.elem0.loc8: %.68a = impl_witness_access constants.%Copy.impl_witness.5e1, element0 [concrete = constants.%bool.as.Copy.impl.Op]
// CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %true, %impl.elem0.loc8 [concrete = constants.%bool.as.Copy.impl.Op.bound.1b3]
// CHECK:STDOUT: %bool.as.Copy.impl.Op.call.loc8: init bool = call %bound_method.loc8(%true) [concrete = constants.%true]
// CHECK:STDOUT: return %bool.as.Copy.impl.Op.call.loc8
// CHECK:STDOUT: }
// CHECK:STDOUT: