Files
carbon-lang/toolchain/check/testdata/builtins/int/left_shift.carbon
T
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

234 lines
10 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/uint.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/int/left_shift.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/left_shift.carbon
// --- i32.carbon
library "[[@TEST_NAME]]";
class Expect(N: i32) {}
fn Test(generic N: i32) -> Expect(N) { return {}; }
fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
fn F() {
Test(LeftShift(0, 0)) as Expect(0);
Test(LeftShift(0, 1)) as Expect(0);
Test(LeftShift(0, 30)) as Expect(0);
Test(LeftShift(1, 30)) as Expect(0x4000_0000);
Test(LeftShift(5, 2)) as Expect(20);
Test(LeftShift(-1, 1)) as Expect(-2);
Test(LeftShift(-2, 1)) as Expect(-4);
Test(LeftShift(-3, 1)) as Expect(-6);
}
fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
//@dump-sem-ir-begin
return LeftShift(a, b);
//@dump-sem-ir-end
}
// --- u32.carbon
library "[[@TEST_NAME]]";
class Expect(N: u32) {}
fn Test(generic N: u32) -> Expect(N) { return {}; }
fn LeftShift(a: u32, b: i32) -> u32 = "int.left_shift";
fn F() {
Test(LeftShift(0, 0)) as Expect(0);
Test(LeftShift(0, 1)) as Expect(0);
Test(LeftShift(0, 30)) as Expect(0);
Test(LeftShift(1, 30)) as Expect(0x4000_0000);
Test(LeftShift(5, 2)) as Expect(20);
Test(LeftShift(0xFFFF_FFFF, 1)) as Expect(0xFFFF_FFFE);
Test(LeftShift(0xFFFF_FFFE, 1)) as Expect(0xFFFF_FFFC);
Test(LeftShift(0xABCD_EF01, 8)) as Expect(0xCDEF_0100);
}
fn RuntimeCall(a: u32, b: i32) -> u32 {
//@dump-sem-ir-begin
return LeftShift(a, b);
//@dump-sem-ir-end
}
// --- literal.carbon
library "[[@TEST_NAME]]";
fn LeftShift(a: Core.IntLiteral, b: Core.IntLiteral) -> Core.IntLiteral = "int.left_shift";
class Expect(N: Core.IntLiteral) {}
fn Test(generic N: Core.IntLiteral) -> Expect(N) { return {}; }
fn F() {
// Zero can be shifted by any amount.
Test(LeftShift(0, 0)) as Expect(0);
Test(LeftShift(0, 0)) as Expect(0);
Test(LeftShift(0, 1)) as Expect(0);
Test(LeftShift(0, 30)) as Expect(0);
Test(LeftShift(0, 1_000_000_000)) as Expect(0);
// Positive numbers can be shifted.
Test(LeftShift(1, 0)) as Expect(1);
Test(LeftShift(1, 1)) as Expect(2);
Test(LeftShift(2, 1)) as Expect(4);
Test(LeftShift(1, 2)) as Expect(4);
Test(LeftShift(3, 2)) as Expect(12);
Test(LeftShift(1, 30)) as Expect(0x4000_0000);
Test(LeftShift(5, 2)) as Expect(20);
// Negative numbers can be shifted too.
Test(LeftShift(-1, 0)) as Expect(-1);
Test(LeftShift(-1, 1)) as Expect(-2);
Test(LeftShift(-2, 1)) as Expect(-4);
Test(LeftShift(-3, 1)) as Expect(-6);
// Large numbers can be shifted losslessly.
Test(LeftShift(0xFFFF_FFFF, 1)) as Expect(0x1_FFFF_FFFE);
Test(LeftShift(0xFFFF_FFFE, 1)) as Expect(0x1_FFFF_FFFC);
Test(LeftShift(0xABCD_EF01, 8)) as Expect(0xAB_CDEF_0100);
Test(LeftShift(0x7FFF_FFFF_FFFF_FFFF, 1)) as Expect(0xFFFF_FFFF_FFFF_FFFE);
Test(LeftShift(0xFFFF_FFFF_FFFF_FFFF, 1)) as Expect(0x1_FFFF_FFFF_FFFF_FFFE);
}
// --- fail_bad_shift.carbon
library "[[@TEST_NAME]]";
fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
fn LeftShiftLit(a: Core.IntLiteral, b: i32) -> Core.IntLiteral = "int.left_shift";
// Shift greater than size is disallowed for sized types.
let size_1: i32 = LeftShift(1, 31);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+2]]:19-34: error: shift distance >= type width of 32 in `1 << 32` [CompileTimeShiftOutOfRange]
// CHECK:STDERR:
let size_2: i32 = LeftShift(1, 32);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+2]]:19-34: error: shift distance >= type width of 32 in `1 << 33` [CompileTimeShiftOutOfRange]
// CHECK:STDERR:
let size_3: i32 = LeftShift(1, 33);
// Overflow is allowed if the shift distance is in bounds.
let overflow_1: i32 = LeftShift(1000, 31);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+2]]:23-41: error: shift distance >= type width of 32 in `1000 << 32` [CompileTimeShiftOutOfRange]
// CHECK:STDERR:
let overflow_2: i32 = LeftShift(1000, 32);
// Oversize shifts aren't allowed even if there's no overflow.
let no_overflow_1: i32 = LeftShift(0, 31);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+2]]:26-41: error: shift distance >= type width of 32 in `0 << 32` [CompileTimeShiftOutOfRange]
// CHECK:STDERR:
let no_overflow_2: i32 = LeftShift(0, 32);
// Negative shifts aren't allowed either, even for literals, even if the lhs is zero.
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+2]]:21-36: error: shift distance >= type width of 32 in `1 << -1` [CompileTimeShiftOutOfRange]
// CHECK:STDERR:
let negative: i32 = LeftShift(1, -1);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+2]]:26-41: error: shift distance >= type width of 32 in `0 << -1` [CompileTimeShiftOutOfRange]
// CHECK:STDERR:
let negative_zero: i32 = LeftShift(0, -1);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+2]]:37-55: error: shift distance negative in `1 << -1` [CompileTimeShiftNegative]
// CHECK:STDERR:
let negative_lit: Core.IntLiteral = LeftShiftLit(1, -1);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+2]]:42-60: error: shift distance negative in `0 << -1` [CompileTimeShiftNegative]
// CHECK:STDERR:
let negative_lit_zero: Core.IntLiteral = LeftShiftLit(0, -1);
// --- fail_literal_overflow.carbon
library "[[@TEST_NAME]]";
fn LeftShift(a: Core.IntLiteral, b: Core.IntLiteral) -> Core.IntLiteral = "int.left_shift";
// CHECK:STDERR: fail_literal_overflow.carbon:[[@LINE+2]]:16-42: error: shift distance of 1000000000 would result in an integer whose width is greater than the maximum supported width of 8388608 [CompileTimeUnsizedShiftOutOfRange]
// CHECK:STDERR:
let bad: i32 = LeftShift(1, 1_000_000_000);
// CHECK:STDERR: fail_literal_overflow.carbon:[[@LINE+2]]:25-52: error: shift distance of 1000000000 would result in an integer whose width is greater than the maximum supported width of 8388608 [CompileTimeUnsizedShiftOutOfRange]
// CHECK:STDERR:
let bad_negative: i32 = LeftShift(-1, 1_000_000_000);
// --- fail_comp_time_only_shift.carbon
library "[[@TEST_NAME]]";
fn LeftShiftByLit(a: i32, b: Core.IntLiteral) -> i32 = "int.left_shift";
fn LeftShiftOfLit(a: Core.IntLiteral, b: i32) -> Core.IntLiteral = "int.left_shift";
var a_lit: Core.IntLiteral = 12;
var an_i32: i32 = 34;
// This can't be valid: we don't have a compile-time or runtime integer value for `a_lit`.
// CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+3]]:17-45: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
// CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-8]]:1-54: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
// CHECK:STDERR:
let bad1: i32 = LeftShiftByLit(an_i32, a_lit);
// TODO: This could be valid because we don't actually need the return value at runtime.
// CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+3]]:29-57: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
// CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-13]]:1-66: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
// CHECK:STDERR:
let bad2: Core.IntLiteral = LeftShiftOfLit(a_lit, an_i32);
// TODO: This could be valid because the literal argument has a constant value.
// CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+3]]:17-42: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
// CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-20]]:1-54: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
// CHECK:STDERR:
let bad3: i32 = LeftShiftByLit(an_i32, 12);
// TODO: This could be valid because we don't actually need the return value at runtime.
// CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+3]]:29-54: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
// CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-25]]:1-66: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
// CHECK:STDERR:
let bad4: Core.IntLiteral = LeftShiftOfLit(12, an_i32);
// CHECK:STDOUT: --- i32.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %LeftShift.type: type = fn_type @LeftShift [concrete]
// CHECK:STDOUT: %LeftShift: %LeftShift.type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @RuntimeCallIsValid(%a.param: %i32, %b.param: %i32) -> out %return.param: %i32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %LeftShift.ref: %LeftShift.type = name_ref LeftShift, file.%LeftShift.decl [concrete = constants.%LeftShift]
// CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
// CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
// CHECK:STDOUT: %LeftShift.call: init %i32 = call %LeftShift.ref(%a.ref, %b.ref)
// CHECK:STDOUT: return %LeftShift.call
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- u32.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(%int_32) [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %LeftShift.type: type = fn_type @LeftShift [concrete]
// CHECK:STDOUT: %LeftShift: %LeftShift.type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @RuntimeCall(%a.param: %u32, %b.param: %i32) -> out %return.param: %u32 {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %LeftShift.ref: %LeftShift.type = name_ref LeftShift, file.%LeftShift.decl [concrete = constants.%LeftShift]
// CHECK:STDOUT: %a.ref: %u32 = name_ref a, %a
// CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
// CHECK:STDOUT: %LeftShift.call: init %u32 = call %LeftShift.ref(%a.ref, %b.ref)
// CHECK:STDOUT: return %LeftShift.call
// CHECK:STDOUT: }
// CHECK:STDOUT: