mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 17:28:10 +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
153 lines
5.5 KiB
Plaintext
153 lines
5.5 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/destroy.carbon
|
|
// EXTRA-ARGS: --clang-arg=-Wno-abstract-final-class
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/class/import/abstract.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/class/import/abstract.carbon
|
|
|
|
// --- abstract.h
|
|
|
|
struct A {
|
|
virtual void f() = 0;
|
|
};
|
|
|
|
// --- derive_base_from_abstract.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import Cpp library "abstract.h";
|
|
|
|
base class B {
|
|
extend base: Cpp.A;
|
|
}
|
|
|
|
// --- todo_fail_derive_final_from_abstract.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import Cpp library "abstract.h";
|
|
|
|
class C {
|
|
// TODO: We should reject this because `f` is not implemented.
|
|
extend base: Cpp.A;
|
|
}
|
|
|
|
// --- impl_abstract_member.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import Cpp library "abstract.h";
|
|
|
|
class C {
|
|
extend base: Cpp.A;
|
|
override fn f(unused ref self) {}
|
|
}
|
|
|
|
// --- fail_impl_mismatch_member.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import Cpp library "abstract.h";
|
|
|
|
class C {
|
|
extend base: Cpp.A;
|
|
// CHECK:STDERR: fail_impl_mismatch_member.carbon:[[@LINE+2]]:3-40: error: override without compatible virtual in base class [OverrideWithoutVirtualInBase]
|
|
// CHECK:STDERR:
|
|
override fn invalid(unused ref self) {}
|
|
}
|
|
|
|
// --- abstract_final.h
|
|
|
|
// C++ allows a class to be both abstract and final.
|
|
struct AbstractFinal final {
|
|
virtual void f() = 0;
|
|
};
|
|
|
|
// --- fail_derive_from_abstract_final.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import Cpp library "abstract_final.h";
|
|
|
|
class C {
|
|
// CHECK:STDERR: fail_derive_from_abstract_final.carbon:[[@LINE+2]]:16-32: error: deriving from final type `AbstractFinal`; base type must be an `abstract` or `base` class [BaseIsFinal]
|
|
// CHECK:STDERR:
|
|
extend base: Cpp.AbstractFinal;
|
|
}
|
|
|
|
// --- use_abstract_final.carbon
|
|
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import Cpp library "abstract_final.h";
|
|
|
|
// We model an abstract final class as being final, rather than abstract. This
|
|
// prevents inheritance from it, and there's no actual way to initialize an
|
|
// instance of the type, but that does not prevent defining a function that
|
|
// returns the type by value. Make sure that doesn't crash.
|
|
//@dump-sem-ir-begin
|
|
fn F() -> Cpp.AbstractFinal {
|
|
return F();
|
|
}
|
|
//@dump-sem-ir-end
|
|
|
|
// CHECK:STDOUT: --- use_abstract_final.carbon
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: constants {
|
|
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
|
|
// CHECK:STDOUT: %AbstractFinal: type = class_type @AbstractFinal [concrete]
|
|
// CHECK:STDOUT: %.dbd: Core.Form = init_form %AbstractFinal [concrete]
|
|
// CHECK:STDOUT: %pattern_type: type = pattern_type %AbstractFinal [concrete]
|
|
// CHECK:STDOUT: %return.param_patt: %pattern_type = out_param_pattern [concrete]
|
|
// CHECK:STDOUT: %return.patt: %pattern_type = return_slot_pattern %return.param_patt, %AbstractFinal [concrete]
|
|
// CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
|
|
// CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: %AbstractFinal.f.type: type = fn_type @AbstractFinal.f [concrete]
|
|
// CHECK:STDOUT: %AbstractFinal.f: %AbstractFinal.f.type = struct_value () [concrete]
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: imports {
|
|
// CHECK:STDOUT: %Cpp: <namespace> = namespace file.%Cpp.import_cpp, [concrete] {
|
|
// CHECK:STDOUT: .AbstractFinal = %AbstractFinal.decl
|
|
// CHECK:STDOUT: import Cpp//...
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: %AbstractFinal.decl: type = class_decl @AbstractFinal [concrete = constants.%AbstractFinal] {} {}
|
|
// CHECK:STDOUT: %AbstractFinal.f.decl: %AbstractFinal.f.type = fn_decl @AbstractFinal.f [concrete = constants.%AbstractFinal.f] {
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: } {
|
|
// CHECK:STDOUT: <elided>
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: file {
|
|
// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
|
|
// CHECK:STDOUT: %return.param_patt: %pattern_type = out_param_pattern [concrete = constants.%return.param_patt]
|
|
// CHECK:STDOUT: %return.patt: %pattern_type = return_slot_pattern %return.param_patt, %AbstractFinal.ref [concrete = constants.%return.patt]
|
|
// CHECK:STDOUT: } {
|
|
// CHECK:STDOUT: %Cpp.ref: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
|
|
// CHECK:STDOUT: %AbstractFinal.ref: type = name_ref AbstractFinal, imports.%AbstractFinal.decl [concrete = constants.%AbstractFinal]
|
|
// CHECK:STDOUT: %.loc11_14.2: Core.Form = init_form %AbstractFinal.ref [concrete = constants.%.dbd]
|
|
// CHECK:STDOUT: %return.param: ref %AbstractFinal = out_param call_param0
|
|
// CHECK:STDOUT: %return: ref %AbstractFinal = return_slot %return.param
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: vtable @AbstractFinal.vtable {
|
|
// CHECK:STDOUT: imports.%AbstractFinal.f.decl
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: fn @F() -> out %return.param: %AbstractFinal {
|
|
// CHECK:STDOUT: !entry:
|
|
// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
|
|
// CHECK:STDOUT: %.loc11_14.1: ref %AbstractFinal = splice_block %return.param {}
|
|
// CHECK:STDOUT: %F.call: init %AbstractFinal to %.loc11_14.1 = call %F.ref()
|
|
// CHECK:STDOUT: return %F.call to %return.param
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|