Files
carbon-lang/toolchain/check/testdata/primitives/float_conversions.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

145 lines
5.3 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+4]]:14: error: integer value 123456789 cannot be represented exactly in floating-point type `f32` [IntLossyConversionToFloat]
// CHECK:STDERR: let a: f32 = 123456789;
// CHECK:STDERR: ^~~~~~~~~
// CHECK:STDERR:
let a: f32 = 123456789;
// CHECK:STDERR: fail_int_literal_to_float_lossy.carbon:[[@LINE+4]]:14: error: integer value 100000001 cannot be represented exactly in floating-point type `f32` [IntLossyConversionToFloat]
// CHECK:STDERR: let b: f32 = 100000001;
// CHECK:STDERR: ^~~~~~~~~
// CHECK:STDERR:
let b: f32 = 100000001;
// CHECK:STDERR: fail_int_literal_to_float_lossy.carbon:[[@LINE+4]]:14: error: integer value 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 too large for floating-point type `f32` [IntTooLargeForFloatType]
// CHECK:STDERR: let c: f32 = 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;
// CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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+7]]:16: error: cannot convert expression of type `Core.FloatLiteral` to `i32` with `as` [ConversionFailure]
// CHECK:STDERR: let _: i32 = 12.34 as i32;
// CHECK:STDERR: ^~~~~~~~~~~~
// CHECK:STDERR: fail_float_as_int.carbon:[[@LINE+4]]:16: note: type `Core.FloatLiteral` does not implement interface `Core.As(i32)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: let _: i32 = 12.34 as i32;
// CHECK:STDERR: ^~~~~~~~~~~~
// CHECK:STDERR:
let _: i32 = 12.34 as i32;
// CHECK:STDERR: fail_float_as_int.carbon:[[@LINE+7]]:16: error: cannot convert expression of type `f32` to `i32` with `as` [ConversionFailure]
// CHECK:STDERR: let _: i32 = f as i32;
// CHECK:STDERR: ^~~~~~~~
// CHECK:STDERR: fail_float_as_int.carbon:[[@LINE+4]]:16: note: type `f32` does not implement interface `Core.As(i32)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: let _: i32 = f as i32;
// CHECK:STDERR: ^~~~~~~~
// 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+7]]:16: error: cannot implicitly convert expression of type `f64` to `f32` [ConversionFailure]
// CHECK:STDERR: let _: f32 = x;
// CHECK:STDERR: ^
// CHECK:STDERR: fail_float_to_float_narrowing.carbon:[[@LINE+4]]:16: note: type `f64` does not implement interface `Core.ImplicitAs(f32)` [MissingImplInMemberAccessInContext]
// CHECK:STDERR: let _: f32 = x;
// CHECK:STDERR: ^
// CHECK:STDERR:
let _: f32 = x;
}