mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 10:48:34 +00:00
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
234 lines
9.7 KiB
Plaintext
234 lines
9.7 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
|
|
// 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/function/declaration/extern.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/declaration/extern.carbon
|
|
|
|
// --- basic.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
extern fn F();
|
|
|
|
// --- basic_use.carbon
|
|
|
|
import library "basic";
|
|
|
|
var x: () = F();
|
|
|
|
// --- fail_redecl.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
extern fn F();
|
|
// CHECK:STDERR: fail_redecl.carbon:[[@LINE+3]]:1-14: error: redeclaration of `fn F` is redundant [RedeclRedundant]
|
|
// CHECK:STDERR: fail_redecl.carbon:[[@LINE-2]]:1-14: note: previously declared here [RedeclPrevDecl]
|
|
// CHECK:STDERR:
|
|
extern fn F();
|
|
|
|
// --- fail_redecl_extern.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
extern fn F();
|
|
// CHECK:STDERR: fail_redecl_extern.carbon:[[@LINE+3]]:1-7: error: redeclaration of `fn F` is redundant [RedeclRedundant]
|
|
// CHECK:STDERR: fail_redecl_extern.carbon:[[@LINE-2]]:1-14: note: previously declared here [RedeclPrevDecl]
|
|
// CHECK:STDERR:
|
|
fn F();
|
|
|
|
// --- fail_member_extern.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class C {
|
|
// CHECK:STDERR: fail_member_extern.carbon:[[@LINE+2]]:3-8: error: `extern` not allowed; requires file or namespace scope [ModifierExternNotAllowed]
|
|
// CHECK:STDERR:
|
|
extern fn F();
|
|
// CHECK:STDERR: fail_member_extern.carbon:[[@LINE+2]]:3-8: error: `extern` not allowed; requires file or namespace scope [ModifierExternNotAllowed]
|
|
// CHECK:STDERR:
|
|
extern fn G(self);
|
|
}
|
|
|
|
// --- fail_extern_library_in_importer.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import library "basic";
|
|
|
|
// CHECK:STDERR: fail_extern_library_in_importer.carbon:[[@LINE+4]]:1-30: error: cannot declare imported `fn F` as `extern library` [ExternLibraryInImporter]
|
|
// CHECK:STDERR: imported from: fail_extern_library_in_importer.carbon:[[@LINE-3]]:1 [InImport]
|
|
// CHECK:STDERR: basic.carbon:4:1-14: note: previously declared here [RedeclPrevDecl]
|
|
// CHECK:STDERR:
|
|
extern library "basic" fn F();
|
|
|
|
// CHECK:STDOUT: --- basic.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: %F: %F.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
|
|
// CHECK:STDOUT: .F = %F.decl
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: extern fn @F();
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: --- basic_use.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
|
|
// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
|
|
// CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
|
|
// CHECK:STDOUT: %pattern_type: type = pattern_type %empty_tuple.type [concrete]
|
|
// CHECK:STDOUT: %x.patt: %pattern_type = ref_binding_pattern x [concrete]
|
|
// CHECK:STDOUT: %x.var_patt: %pattern_type = var_pattern %x.patt [concrete]
|
|
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
|
|
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: %Main.F: %F.type = import_ref Main//basic, F, loaded [concrete = constants.%F]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
|
|
// CHECK:STDOUT: .F = imports.%Main.F
|
|
// CHECK:STDOUT: .x = %x
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %default.import = import <none>
|
|
// CHECK:STDOUT: %x.var: ref %empty_tuple.type = var_storage %x.var_patt [concrete]
|
|
// CHECK:STDOUT: %.loc4_9.1: type = splice_block %.loc4_9.3 [concrete = constants.%empty_tuple.type] {
|
|
// CHECK:STDOUT: %.loc4_9.2: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
|
|
// CHECK:STDOUT: %.loc4_9.3: type = converted %.loc4_9.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %x: ref %empty_tuple.type = wrapper_binding x, %x.var [concrete = %x.var]
|
|
// CHECK:STDOUT: name_binding_decl {
|
|
// CHECK:STDOUT: %x.patt: %pattern_type = ref_binding_pattern x [concrete = constants.%x.patt]
|
|
// CHECK:STDOUT: %x.var_patt: %pattern_type = var_pattern %x.patt [concrete = constants.%x.var_patt]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: extern fn @F [from "basic.carbon"];
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @__global_init() {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, imports.%Main.F [concrete = constants.%F]
|
|
// CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
|
|
// CHECK:STDOUT: assign file.%x.var, %F.call
|
|
// CHECK:STDOUT: return
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: --- fail_redecl.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: %F: %F.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
|
|
// CHECK:STDOUT: .F = %F.decl.loc4
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %F.decl.loc4: %F.type = fn_decl @F [concrete = constants.%F] {} {}
|
|
// CHECK:STDOUT: %F.decl.loc8: %F.type = fn_decl @F [concrete = constants.%F] {} {}
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: extern fn @F();
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: --- fail_redecl_extern.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: %F: %F.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
|
|
// CHECK:STDOUT: .F = %F.decl.loc4
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %F.decl.loc4: %F.type = fn_decl @F [concrete = constants.%F] {} {}
|
|
// CHECK:STDOUT: %F.decl.loc8: %F.type = fn_decl @F [concrete = constants.%F] {} {}
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: extern fn @F();
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: --- fail_member_extern.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
|
|
// CHECK:STDOUT: %C: type = class_type @C [concrete]
|
|
// CHECK:STDOUT: %C.F.type: type = fn_type @C.F [concrete]
|
|
// CHECK:STDOUT: %C.F: %C.F.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: %pattern_type: type = pattern_type %C [concrete]
|
|
// CHECK:STDOUT: %self.param_patt: %pattern_type = value_param_pattern [concrete]
|
|
// CHECK:STDOUT: %self.patt: %pattern_type = wrapper_binding_pattern self, %self.param_patt [concrete]
|
|
// CHECK:STDOUT: %C.G.type: type = fn_type @C.G [concrete]
|
|
// CHECK:STDOUT: %C.G: %C.G.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
|
|
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
|
|
// CHECK:STDOUT: .C = %C.decl
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: class @C {
|
|
// CHECK:STDOUT: %C.F.decl: %C.F.type = fn_decl @C.F [concrete = constants.%C.F] {} {}
|
|
// CHECK:STDOUT: %C.G.decl: %C.G.type = fn_decl @C.G [concrete = constants.%C.G] {
|
|
// CHECK:STDOUT: %self.param_patt: %pattern_type = value_param_pattern [concrete = constants.%self.param_patt]
|
|
// CHECK:STDOUT: %self.patt: %pattern_type = wrapper_binding_pattern self, %self.param_patt [concrete = constants.%self.patt]
|
|
// CHECK:STDOUT: } {
|
|
// CHECK:STDOUT: %self.param: %C = value_param call_param0
|
|
// CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
|
|
// CHECK:STDOUT: %self: %C = wrapper_binding self, %self.param
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
|
|
// CHECK:STDOUT: complete_type_witness = %complete_type
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: !members:
|
|
// CHECK:STDOUT: .Self = constants.%C
|
|
// CHECK:STDOUT: .F = %C.F.decl
|
|
// CHECK:STDOUT: .G = %C.G.decl
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @C.F();
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @C.G(%self.param: %C);
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: --- fail_extern_library_in_importer.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: %F: %F.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
|
|
// CHECK:STDOUT: .F = invalid
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %default.import = import <none>
|
|
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: extern fn @F [from "basic.carbon"];
|
|
// CHECK:STDOUT:
|