mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 11:28:11 +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
289 lines
8.7 KiB
Plaintext
289 lines
8.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/primitives.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/patterns/unused.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/patterns/unused.carbon
|
|
|
|
// --- todo_ensure_no_warning.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn ReturnVar() -> i32 {
|
|
var x: i32 = 0;
|
|
x = 1;
|
|
return x;
|
|
}
|
|
|
|
fn ReturnLet() -> i32 {
|
|
let y: i32 = 1;
|
|
return y;
|
|
}
|
|
|
|
fn RefParam(ref x: i32) {
|
|
x = 1;
|
|
}
|
|
|
|
fn ReturnedVar() -> i32 {
|
|
returned var x: i32 = 0;
|
|
return var;
|
|
}
|
|
|
|
fn ReturnVarExplicit() -> i32 {
|
|
var x: i32 = 0;
|
|
return x;
|
|
}
|
|
|
|
fn FieldAccess() {
|
|
var x: {.a: i32} = {.a = 0};
|
|
// Accessing a field should count as a use of x.
|
|
var _: i32 = x.a;
|
|
}
|
|
|
|
fn ParametersUsed(x: i32) -> i32 {
|
|
return x;
|
|
}
|
|
|
|
// --- unused_ref_param.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn RefParamUnused(unused ref y: i32) {
|
|
}
|
|
|
|
// --- local_let_named_not_used.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: local_let_named_not_used.carbon:[[@LINE+2]]:7-12: warning: binding `x` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
let x: i32 = 0;
|
|
}
|
|
|
|
// --- local_var_named_not_used.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: local_var_named_not_used.carbon:[[@LINE+2]]:7-12: warning: binding `a` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
var a: i32 = 0;
|
|
}
|
|
|
|
// --- local_var_shadowed_not_used.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() {
|
|
var x: i32 = 0;
|
|
if (true) {
|
|
// CHECK:STDERR: local_var_shadowed_not_used.carbon:[[@LINE+2]]:9-14: warning: binding `x` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
var x: i32 = 1;
|
|
}
|
|
x = 2;
|
|
}
|
|
|
|
// --- param_let_named_not_used.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// CHECK:STDERR: param_let_named_not_used.carbon:[[@LINE+2]]:6-11: warning: binding `p` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
fn F(p: i32) {
|
|
}
|
|
|
|
// --- param_ref_named_not_used.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// CHECK:STDERR: param_ref_named_not_used.carbon:[[@LINE+2]]:6-15: warning: binding `y` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
fn F(ref y: i32) {
|
|
}
|
|
|
|
// --- fail_read_from_param_unused_ref.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// CHECK:STDERR: fail_read_from_param_unused_ref.carbon:[[@LINE+1]]:26-35: error: variable `y` marked `unused` but used [UnusedButUsed]
|
|
fn RefParamUnused(unused ref y: i32) {
|
|
// CHECK:STDERR: fail_read_from_param_unused_ref.carbon:[[@LINE+2]]:16: note: usage here [UnusedButUsedHere]
|
|
// CHECK:STDERR:
|
|
var _: i32 = y;
|
|
}
|
|
|
|
// --- fail_unused_returned_var.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() -> i32 {
|
|
// CHECK:STDERR: fail_unused_returned_var.carbon:[[@LINE+3]]:23-28: error: variable `x` marked `unused` but used [UnusedButUsed]
|
|
// CHECK:STDERR: fail_unused_returned_var.carbon:[[@LINE+2]]:23-28: note: usage here [UnusedButUsedHere]
|
|
// CHECK:STDERR:
|
|
returned var unused x: i32 = 0;
|
|
return var;
|
|
}
|
|
|
|
// --- fail_read_from_unused_local_var.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F(x: i32) {
|
|
var _: i32 = x;
|
|
// CHECK:STDERR: fail_read_from_unused_local_var.carbon:[[@LINE+1]]:14-19: error: variable `y` marked `unused` but used [UnusedButUsed]
|
|
var unused y: i32 = x;
|
|
// CHECK:STDERR: fail_read_from_unused_local_var.carbon:[[@LINE+2]]:23: note: usage here [UnusedButUsedHere]
|
|
// CHECK:STDERR:
|
|
var unused z: i32 = y;
|
|
}
|
|
|
|
// --- fail_todo_match.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() -> i32 {
|
|
var x: i32 = 3;
|
|
// CHECK:STDERR: fail_todo_match.carbon:[[@LINE+2]]:3-7: error: semantics TODO: `HandleMatchIntroducer` [SemanticsTodo]
|
|
// CHECK:STDERR:
|
|
match (f(x)) {
|
|
case unused var (a: i32, b: i32) => { return 0; }
|
|
case (a: i32) if (a < 0) => { return 2; }
|
|
case var unused a: i32 if (a != x) => { return 3; }
|
|
default => { return 4; }
|
|
}
|
|
return 0;
|
|
// TODO: ensure no warning (after match is implemented).
|
|
}
|
|
|
|
// --- fail_unused_decl.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// CHECK:STDERR: fail_unused_decl.carbon:[[@LINE+2]]:13-18: error: `unused` modifier without a definition [UnusedModifierWithoutDefinition]
|
|
// CHECK:STDERR:
|
|
fn F(unused x: i32);
|
|
|
|
// --- fail_unused_self_decl.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class C {
|
|
// CHECK:STDERR: fail_unused_self_decl.carbon:[[@LINE+2]]:15-18: error: `unused` modifier without a definition [UnusedModifierWithoutDefinition]
|
|
// CHECK:STDERR:
|
|
fn F(unused self);
|
|
}
|
|
|
|
// --- fail_unused_implicit_param_decl.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// The `unused` modifier is rejected without a definition for any implicit
|
|
// parameter, both a compile-time binding and `self`.
|
|
|
|
// CHECK:STDERR: fail_unused_implicit_param_decl.carbon:[[@LINE+2]]:13-19: error: `unused` modifier without a definition [UnusedModifierWithoutDefinition]
|
|
// CHECK:STDERR:
|
|
fn F[unused T: type]();
|
|
|
|
class C {
|
|
// CHECK:STDERR: fail_unused_implicit_param_decl.carbon:[[@LINE+4]]:15-24: error: `self` must be declared in the explicit parameter list [SelfInImplicitParamList]
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: fail_unused_implicit_param_decl.carbon:[[@LINE+2]]:15-24: error: `unused` modifier without a definition [UnusedModifierWithoutDefinition]
|
|
// CHECK:STDERR:
|
|
fn G[unused self: Self]();
|
|
}
|
|
|
|
// --- fail_multiple_uses_of_unused.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: fail_multiple_uses_of_unused.carbon:[[@LINE+1]]:14-19: error: variable `x` marked `unused` but used [UnusedButUsed]
|
|
var unused x: i32 = 0;
|
|
// CHECK:STDERR: fail_multiple_uses_of_unused.carbon:[[@LINE+2]]:3: note: usage here [UnusedButUsedHere]
|
|
// CHECK:STDERR:
|
|
x;
|
|
x;
|
|
}
|
|
|
|
// --- fail_unused_does_not_affect_redeclaration.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F(x: i32);
|
|
// CHECK:STDERR: fail_unused_does_not_affect_redeclaration.carbon:[[@LINE+3]]:13-18: error: redeclaration differs at parameter 1 [RedeclParamDiffers]
|
|
// CHECK:STDERR: fail_unused_does_not_affect_redeclaration.carbon:[[@LINE-2]]:6-11: note: previous declaration's corresponding parameter here [RedeclParamPrevious]
|
|
// CHECK:STDERR:
|
|
fn F(unused y: i32) {}
|
|
|
|
// --- unreachable.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F(x: i32) {
|
|
return;
|
|
// Unreachable use suppresses the UnusedBinding warning.
|
|
var y: i32 = x;
|
|
}
|
|
|
|
fn G(unused x: i32) {
|
|
return;
|
|
// Unreachable use is ignored, so no UnusedButUsed error.
|
|
var y: i32 = x;
|
|
}
|
|
|
|
fn H(ref x: i32) {
|
|
return;
|
|
// Unreachable assignment suppresses the UnusedBinding warning.
|
|
x = 0;
|
|
}
|
|
|
|
// --- fail_todo_unused_generic.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F(generic T: type);
|
|
|
|
// CHECK:STDERR: fail_todo_unused_generic.carbon:[[@LINE+2]]:21-27: error: semantics TODO: `generic redeclaration differs from previous declaration` [SemanticsTodo]
|
|
// CHECK:STDERR:
|
|
fn F(unused generic T: type) {}
|
|
|
|
class C(T: type);
|
|
|
|
// CHECK:STDERR: fail_todo_unused_generic.carbon:[[@LINE+2]]:16-22: error: semantics TODO: `generic redeclaration differs from previous declaration` [SemanticsTodo]
|
|
// CHECK:STDERR:
|
|
class C(unused T: type) {}
|
|
|
|
// --- fail_unused_let_multiple_uses.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: fail_unused_let_multiple_uses.carbon:[[@LINE+1]]:14-19: error: variable `x` marked `unused` but used [UnusedButUsed]
|
|
let unused x: i32 = 0;
|
|
// CHECK:STDERR: fail_unused_let_multiple_uses.carbon:[[@LINE+4]]:16: note: usage here [UnusedButUsedHere]
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: fail_unused_let_multiple_uses.carbon:[[@LINE+2]]:7-12: warning: binding `y` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
var y: i32 = x;
|
|
// CHECK:STDERR: fail_unused_let_multiple_uses.carbon:[[@LINE+2]]:7-12: warning: binding `z` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
var z: i32 = x;
|
|
}
|
|
|
|
// --- multiple_unused_bindings.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() {
|
|
// CHECK:STDERR: multiple_unused_bindings.carbon:[[@LINE+2]]:7-12: warning: binding `x` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
var x: i32 = 0;
|
|
// CHECK:STDERR: multiple_unused_bindings.carbon:[[@LINE+2]]:7-12: warning: binding `y` unused [UnusedBinding]
|
|
// CHECK:STDERR:
|
|
var y: i32 = 0;
|
|
}
|
|
|
|
// --- fail_unused_tuple.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() -> i32 {
|
|
// CHECK:STDERR: fail_unused_tuple.carbon:[[@LINE+1]]:14-19: error: variable `a` marked `unused` but used [UnusedButUsed]
|
|
let unused (a: i32,
|
|
b: i32) = (1, 2);
|
|
// CHECK:STDERR: fail_unused_tuple.carbon:[[@LINE+3]]:15: note: usage here [UnusedButUsedHere]
|
|
// CHECK:STDERR:
|
|
// CHECK:STDERR: fail_unused_tuple.carbon:[[@LINE-3]]:14-19: error: variable `b` marked `unused` but used [UnusedButUsed]
|
|
let i: i32 = a;
|
|
// CHECK:STDERR: fail_unused_tuple.carbon:[[@LINE+2]]:22: note: usage here [UnusedButUsedHere]
|
|
// CHECK:STDERR:
|
|
let unused j: i32 = b;
|
|
return i;
|
|
}
|