mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 16:58:09 +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
127 lines
4.5 KiB
Plaintext
127 lines
4.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/full.carbon
|
|
//
|
|
// AUTOUPDATE
|
|
// TIP: To test this file alone, run:
|
|
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/primitives/float_conversions.carbon
|
|
// TIP: To dump output, run:
|
|
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/primitives/float_conversions.carbon
|
|
|
|
// --- test_helpers.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
class Expect[T: type](N: T) {}
|
|
fn Test[T: type](generic N: T) -> Expect(N) { return {}; }
|
|
|
|
// --- literal_conversions.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
let _: f32 = 1.25;
|
|
|
|
let _: Core.FloatLiteral = 42;
|
|
let _: Core.IntLiteral = 42.0 unsafe as Core.IntLiteral;
|
|
|
|
// --- int_to_float.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import library "test_helpers";
|
|
|
|
fn F(x: i32, y: u32) {
|
|
let _: f64 = x;
|
|
let _: f64 = y;
|
|
|
|
Test(12345 as f32) as Expect(12345.0 as f32);
|
|
Test(-42 as f64) as Expect(-(42.0 as f64));
|
|
}
|
|
|
|
// --- int_literal_to_float.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F() {
|
|
let _: f32 = 42;
|
|
let _: f64 = 1234567890123456;
|
|
}
|
|
|
|
// --- fail_int_literal_to_float_lossy.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
// CHECK:STDERR: fail_int_literal_to_float_lossy.carbon:[[@LINE+2]]:14-22: error: integer value 123456789 cannot be represented exactly in floating-point type `f32` [IntLossyConversionToFloat]
|
|
// CHECK:STDERR:
|
|
let a: f32 = 123456789;
|
|
|
|
// CHECK:STDERR: fail_int_literal_to_float_lossy.carbon:[[@LINE+2]]:14-22: error: integer value 100000001 cannot be represented exactly in floating-point type `f32` [IntLossyConversionToFloat]
|
|
// CHECK:STDERR:
|
|
let b: f32 = 100000001;
|
|
|
|
// CHECK:STDERR: fail_int_literal_to_float_lossy.carbon:[[@LINE+2]]:14-114: error: integer value 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 too large for floating-point type `f32` [IntTooLargeForFloatType]
|
|
// CHECK:STDERR:
|
|
let c: f32 = 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
|
|
|
|
// --- float_unsafe_as_int.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import library "test_helpers";
|
|
|
|
fn F() {
|
|
// Float literal to Sized Integer explicit conversion via `unsafe as`.
|
|
Test(12.34 unsafe as i32) as Expect(12 as i32);
|
|
|
|
Test(42.0 unsafe as u32) as Expect(42 as u32);
|
|
|
|
// Negative float to signed integer.
|
|
// TODO: Also test converting a negative float literal once they are
|
|
// supported.
|
|
Test(-(1.99 as f64) unsafe as i8) as Expect(-1 as i8);
|
|
|
|
// Float literal to int literal.
|
|
Test(123.45 unsafe as Core.IntLiteral) as Expect(123);
|
|
}
|
|
|
|
// --- fail_float_as_int.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import library "test_helpers";
|
|
|
|
fn F(f: f32) {
|
|
// Lossy explicit conversion using `as` not permitted.
|
|
// CHECK:STDERR: fail_float_as_int.carbon:[[@LINE+3]]:16-27: error: cannot convert expression of type `Core.FloatLiteral` to `i32` with `as` [ConversionFailure]
|
|
// CHECK:STDERR: fail_float_as_int.carbon:[[@LINE+2]]:16-27: note: type `Core.FloatLiteral` does not implement interface `Core.As(i32)` [MissingImplInMemberAccessInContext]
|
|
// CHECK:STDERR:
|
|
let _: i32 = 12.34 as i32;
|
|
|
|
// CHECK:STDERR: fail_float_as_int.carbon:[[@LINE+3]]:16-23: error: cannot convert expression of type `f32` to `i32` with `as` [ConversionFailure]
|
|
// CHECK:STDERR: fail_float_as_int.carbon:[[@LINE+2]]:16-23: note: type `f32` does not implement interface `Core.As(i32)` [MissingImplInMemberAccessInContext]
|
|
// CHECK:STDERR:
|
|
let _: i32 = f as i32;
|
|
}
|
|
|
|
// --- float_to_float.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
import library "test_helpers";
|
|
|
|
fn F(x: f32) {
|
|
// Implicit conversion (widening f32 -> f64)
|
|
let y: f64 = x;
|
|
|
|
// Explicit conversion (widening f32 -> f64)
|
|
let _: f64 = x as f64;
|
|
|
|
// Explicit conversion (narrowing f64 -> f32)
|
|
let _: f32 = y as f32;
|
|
}
|
|
|
|
// --- fail_float_to_float_narrowing.carbon
|
|
library "[[@TEST_NAME]]";
|
|
|
|
fn F(x: f64) {
|
|
// Implicit narrowing conversion (f64 -> f32) is not permitted.
|
|
// CHECK:STDERR: fail_float_to_float_narrowing.carbon:[[@LINE+3]]:16: error: cannot implicitly convert expression of type `f64` to `f32` [ConversionFailure]
|
|
// CHECK:STDERR: fail_float_to_float_narrowing.carbon:[[@LINE+2]]:16: note: type `f64` does not implement interface `Core.ImplicitAs(f32)` [MissingImplInMemberAccessInContext]
|
|
// CHECK:STDERR:
|
|
let _: f32 = x;
|
|
}
|