Files
carbon-lang/toolchain/check/testdata/builtins/int/right_shift.carbon
T
Chandler Carruth 8be274cf60 Replace :! binding syntax with phase keywords and contextual defaults (#7479)
Implement the toolchain side of proposal #7254, removing the `:!`
binding
syntax for generic and template parameters in favor of the keywords
`generic`,
`template`, and `runtime` plus contextual defaults for phase.

For valid programs this is semantics-preserving: each binding resolves
to the
same phase, and produces the same SemIR, as it did under `:!`/`:`. The
parser
derives a binding's phase from its syntactic context plus any explicit
phase
keyword; new diagnostics and error recovery for misused keywords are
described
below.

Implementation details for each component:

- Lexer: remove the `:!` (`ColonExclaim`) token, move its virtual
parse-node
  budget onto `:`, and add the `generic` and `runtime` keywords.
- Parser: thread a `BindingContext` (`ExplicitParam`, `DeducedParam`, or
`CompileTimeEntityParam`) from declaration introducers down through
parameter
lists to each binding pattern, using a one-token lookahead to
distinguish a
name-qualifier parameter list from a declaration's own final list.
Parameters
of a compile-time entity (`class`, `interface`, `constraint`, `choice`,
`alias`, `export`, `namespace`) and deduced `[]` parameters default to
checked
generic; explicit function parameters and local bindings default to
runtime.
`HandleBindingPattern` resolves the phase from that context plus the
keyword: a
`generic` keyword needs no node of its own (the phase is carried by the
  binding's node kind), while a `runtime` keyword is preserved as a
`RuntimeBindingName` node so `check` can name it in a diagnostic. A
phase
keyword that is merely redundant with the contextual default is
diagnosed
  here, without invalidating the parse tree.
- Check: a phase keyword that is invalid for its context (for example
`runtime`
on a checked-generic parameter) is diagnosed here, and recovers by
building an
error binding that still introduces the name so that later uses of it do
not
  produce cascading errors.

The removed `:!` syntax is now rejected as an ordinary parse error.

The `form`/`:?`/`->?` ("extended types") portion of proposal #7254 is
left for a
separate change.

Assisted-by: Claude Code
2026-07-11 01:22:44 +00:00

176 lines
7.2 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/right_shift.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/right_shift.carbon
// --- i32.carbon
library "[[@TEST_NAME]]";
class Expect(N: i32) {}
fn Test(generic N: i32) -> Expect(N) { return {}; }
fn RightShift(a: i32, b: i32) -> i32 = "int.right_shift";
fn F() {
Test(RightShift(0, 31)) as Expect(0);
Test(RightShift(1, 31)) as Expect(0);
Test(RightShift(1, 0)) as Expect(1);
Test(RightShift(1, 2)) as Expect(0);
Test(RightShift(22, 2)) as Expect(5);
Test(RightShift(-1, 1)) as Expect(-1);
Test(RightShift(-2, 1)) as Expect(-1);
Test(RightShift(-10, 2)) as Expect(-3);
}
fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
//@dump-sem-ir-begin
return RightShift(a, b);
//@dump-sem-ir-end
}
// --- u32.carbon
library "[[@TEST_NAME]]";
class Expect(N: u32) {}
fn Test(generic N: u32) -> Expect(N) { return {}; }
fn RightShift(a: u32, b: i32) -> u32 = "int.right_shift";
fn F() {
Test(RightShift(0, 31)) as Expect(0);
Test(RightShift(1, 31)) as Expect(0);
Test(RightShift(1, 0)) as Expect(1);
Test(RightShift(1, 2)) as Expect(0);
Test(RightShift(22, 2)) as Expect(5);
Test(RightShift(0xFFFF_FFFF, 1)) as Expect(0x7FFF_FFFF);
Test(RightShift(0xABCD_EF01, 8)) as Expect(0xAB_CDEF);
}
fn RuntimeCall(a: u32, b: i32) -> u32 {
//@dump-sem-ir-begin
return RightShift(a, b);
//@dump-sem-ir-end
}
// --- literal.carbon
library "[[@TEST_NAME]]";
fn RightShift(a: Core.IntLiteral, b: Core.IntLiteral) -> Core.IntLiteral = "int.right_shift";
class Expect(N: Core.IntLiteral) {}
fn Test(generic N: Core.IntLiteral) -> Expect(N) { return {}; }
fn F() {
Test(RightShift(0, 31)) as Expect(0);
Test(RightShift(1, 31)) as Expect(0);
Test(RightShift(1, 0)) as Expect(1);
Test(RightShift(1, 2)) as Expect(0);
Test(RightShift(22, 2)) as Expect(5);
Test(RightShift(-1, 1)) as Expect(-1);
Test(RightShift(-2, 1)) as Expect(-1);
Test(RightShift(-10, 2)) as Expect(-3);
Test(RightShift(0xFFFF_FFFF, 1)) as Expect(0x7FFF_FFFF);
Test(RightShift(0xABCD_EF01, 8)) as Expect(0xAB_CDEF);
Test(RightShift(0x1234_5678, 1_000_000_000)) as Expect(0);
Test(RightShift(-0x1234_5678, 1_000_000_000)) as Expect(-1);
Test(RightShift(0xFFFF_FFFF_FFFF_FFFF, 1_000_000_000)) as Expect(0);
Test(RightShift(0x7FFF_FFFF_FFFF_FFFF, 1_000_000_000)) as Expect(0);
Test(RightShift(-0x7FFF_FFFF_FFFF_FFFF, 1_000_000_000)) as Expect(-1);
Test(RightShift(-0x8000_0000_0000_0000, 1_000_000_000)) as Expect(-1);
}
// --- fail_bad_shift.carbon
library "[[@TEST_NAME]]";
fn RightShift(a: i32, b: i32) -> i32 = "int.right_shift";
fn RightShiftLit(a: Core.IntLiteral, b: i32) -> Core.IntLiteral = "int.right_shift";
// Shift greater than size is disallowed for sized types.
let size_1: i32 = RightShift(1, 31);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance >= type width of 32 in `1 >> 32` [CompileTimeShiftOutOfRange]
// CHECK:STDERR: let size_2: i32 = RightShift(1, 32);
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let size_2: i32 = RightShift(1, 32);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance >= type width of 32 in `1 >> 33` [CompileTimeShiftOutOfRange]
// CHECK:STDERR: let size_3: i32 = RightShift(1, 33);
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let size_3: i32 = RightShift(1, 33);
// Negative shifts aren't allowed either, even for literals, even if the lhs is zero.
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:21: error: shift distance >= type width of 32 in `1 >> -1` [CompileTimeShiftOutOfRange]
// CHECK:STDERR: let negative: i32 = RightShift(1, -1);
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let negative: i32 = RightShift(1, -1);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:26: error: shift distance >= type width of 32 in `0 >> -1` [CompileTimeShiftOutOfRange]
// CHECK:STDERR: let negative_zero: i32 = RightShift(0, -1);
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let negative_zero: i32 = RightShift(0, -1);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:37: error: shift distance negative in `1 >> -1` [CompileTimeShiftNegative]
// CHECK:STDERR: let negative_lit: Core.IntLiteral = RightShiftLit(1, -1);
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let negative_lit: Core.IntLiteral = RightShiftLit(1, -1);
// CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:42: error: shift distance negative in `0 >> -1` [CompileTimeShiftNegative]
// CHECK:STDERR: let negative_lit_zero: Core.IntLiteral = RightShiftLit(0, -1);
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
// CHECK:STDERR:
let negative_lit_zero: Core.IntLiteral = RightShiftLit(0, -1);
// CHECK:STDOUT: --- i32.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %RightShift.type: type = fn_type @RightShift [concrete]
// CHECK:STDOUT: %RightShift: %RightShift.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: %RightShift.ref: %RightShift.type = name_ref RightShift, file.%RightShift.decl [concrete = constants.%RightShift]
// CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
// CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
// CHECK:STDOUT: %RightShift.call: init %i32 = call %RightShift.ref(%a.ref, %b.ref)
// CHECK:STDOUT: return %RightShift.call
// CHECK:STDOUT:
// CHECK:STDOUT: !observes:
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- u32.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// 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: %RightShift.type: type = fn_type @RightShift [concrete]
// CHECK:STDOUT: %RightShift: %RightShift.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: %RightShift.ref: %RightShift.type = name_ref RightShift, file.%RightShift.decl [concrete = constants.%RightShift]
// CHECK:STDOUT: %a.ref: %u32 = name_ref a, %a
// CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
// CHECK:STDOUT: %RightShift.call: init %u32 = call %RightShift.ref(%a.ref, %b.ref)
// CHECK:STDOUT: return %RightShift.call
// CHECK:STDOUT:
// CHECK:STDOUT: !observes:
// CHECK:STDOUT: }
// CHECK:STDOUT: