Files
carbon-lang/toolchain/check/testdata/eval/symbolic.carbon
T
Geoff RomerandRichard Smith e023f75254 Implement thunking in terms of constant evaluation (#7332)
The bulk of this change is changing most pattern insts to be `Always`
rather than `AlwaysUnique` constants, so that they can be wrapped in
`SpecificConstant`s to perform substitution. That then lets thunking
rely much more on `SpecificConstant` wrappers instead of deep-copying
the inst tree with modified types.

This approach to thunking should scale better, particularly as things
like form generics make function signatures more complex, because we can
leverage the existing support for constant evaluation and substitution.
Unfortunately, applying this approach to binding patterns will require
more work; see the TODO near the top of `thunk.cpp` for details.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2026-06-13 00:22:00 +00:00

131 lines
6.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/bool.carbon
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/eval/symbolic.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/eval/symbolic.carbon
// --- symbolic_call_to_eval_fn.carbon
library "[[@TEST_NAME]]";
eval fn F(B:! bool) -> type {
if (B) { return (); }
return ({}, {});
}
fn G(B:! bool) {
let unused n: F(B) = ({}, {});
}
fn H() {
G(false);
}
// --- fail_symbolic_call_to_eval_fn_invalid_instantiation.carbon
library "[[@TEST_NAME]]";
eval fn F(B:! bool) -> type {
if (B) { return (); }
return ({}, {});
}
fn G(B:! bool) {
let unused n: F(B) = ({}, {});
}
// TODO: Provide a location for the "cannot initialize" error message.
fn H() {
// CHECK:STDERR: fail_symbolic_call_to_eval_fn_invalid_instantiation.carbon:[[@LINE+5]]:3: error: unable to monomorphize specific `G(true)` [ResolvingSpecificHere]
// CHECK:STDERR: G(true);
// CHECK:STDERR: ^
// CHECK:STDERR: fail_symbolic_call_to_eval_fn_invalid_instantiation.carbon: note: cannot initialize tuple of 0 elements from tuple with 2 elements [TupleInitElementCountMismatch]
// CHECK:STDERR:
G(true);
}
// --- dependent_symbolic_instruction.carbon
library "[[@TEST_NAME]]";
interface Z {}
impl {} as Z {}
eval fn E(U:! Z) -> type {
// This makes a FacetAccessType(U) instruction, for which we have to get a
// constant value from the definition in the called function's specific. This
// requires that the called function has its specific definition resolved.
//@dump-sem-ir-begin
return U;
//@dump-sem-ir-end
}
fn F() {
E({});
}
// CHECK:STDOUT: --- dependent_symbolic_instruction.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
// CHECK:STDOUT: %Z.impl_witness: <witness> = impl_witness @empty_struct_type.as.Z.impl.%Z.impl_witness_table [concrete]
// CHECK:STDOUT: %Z.facet: %Z.type = facet_value %empty_struct_type, (%Z.impl_witness) [concrete]
// CHECK:STDOUT: %pattern_type.5d7: type = pattern_type %Z.type [concrete]
// CHECK:STDOUT: %U.patt.4a7: %pattern_type.5d7 = symbolic_binding_pattern U, 0 [symbolic]
// CHECK:STDOUT: %U.013: %Z.type = symbolic_binding U, 0 [symbolic]
// CHECK:STDOUT: %U.as_type.ff1: type = facet_access_type %U.013 [symbolic]
// CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
// CHECK:STDOUT: %Copy.impl_witness.638: <witness> = impl_witness imports.%Copy.impl_witness_table.4d6 [concrete]
// CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value type, (%Copy.impl_witness.638) [concrete]
// CHECK:STDOUT: %Copy.WithSelf.Op.type.8b3: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet) [concrete]
// CHECK:STDOUT: %.524: type = fn_type_with_self_type %Copy.WithSelf.Op.type.8b3, %Copy.facet [concrete]
// CHECK:STDOUT: %type.as.Copy.impl.Op.type: type = fn_type @type.as.Copy.impl.Op [concrete]
// CHECK:STDOUT: %type.as.Copy.impl.Op: %type.as.Copy.impl.Op.type = struct_value () [concrete]
// CHECK:STDOUT: %type.as.Copy.impl.Op.bound.05d: <bound method> = bound_method %U.as_type.ff1, %type.as.Copy.impl.Op [symbolic]
// CHECK:STDOUT: %type.as.Copy.impl.Op.bound.57f: <bound method> = bound_method %empty_struct_type, %type.as.Copy.impl.Op [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core.import_ref.9c3: %type.as.Copy.impl.Op.type = import_ref Core//prelude/parts/copy, loc{{\d+_\d+}}, loaded [concrete = constants.%type.as.Copy.impl.Op]
// CHECK:STDOUT: %Copy.impl_witness_table.4d6 = impl_witness_table (%Core.import_ref.9c3), @type.as.Copy.impl [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @E(%U.loc6_12.2: %Z.type) {
// CHECK:STDOUT: <elided>
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %U.as_type.loc11_11.2: type = facet_access_type %U.loc6_12.1 [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.ff1)]
// CHECK:STDOUT: %type.as.Copy.impl.Op.bound: <bound method> = bound_method %U.as_type.loc11_11.2, constants.%type.as.Copy.impl.Op [symbolic = %type.as.Copy.impl.Op.bound (constants.%type.as.Copy.impl.Op.bound.05d)]
// CHECK:STDOUT:
// CHECK:STDOUT: fn() -> out %return.param: type {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %U.ref: %Z.type = name_ref U, %U.loc6_12.2 [symbolic = %U.loc6_12.1 (constants.%U.013)]
// CHECK:STDOUT: %U.as_type.loc11_11.1: type = facet_access_type %U.ref [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.ff1)]
// CHECK:STDOUT: %.loc11: type = converted %U.ref, %U.as_type.loc11_11.1 [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.ff1)]
// CHECK:STDOUT: %impl.elem0: %.524 = impl_witness_access constants.%Copy.impl_witness.638, element0 [concrete = constants.%type.as.Copy.impl.Op]
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %.loc11, %impl.elem0 [symbolic = %type.as.Copy.impl.Op.bound (constants.%type.as.Copy.impl.Op.bound.05d)]
// CHECK:STDOUT: %type.as.Copy.impl.Op.call: init type = call %bound_method(%.loc11) [symbolic = %U.as_type.loc11_11.2 (constants.%U.as_type.ff1)]
// CHECK:STDOUT: return %type.as.Copy.impl.Op.call
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @E(constants.%U.013) {
// CHECK:STDOUT: %U.patt.loc6_12.2 => constants.%U.patt.4a7
// CHECK:STDOUT: %U.loc6_12.1 => constants.%U.013
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @E(constants.%Z.facet) {
// CHECK:STDOUT: %U.patt.loc6_12.2 => constants.%U.patt.4a7
// CHECK:STDOUT: %U.loc6_12.1 => constants.%Z.facet
// CHECK:STDOUT:
// CHECK:STDOUT: !definition:
// CHECK:STDOUT: %U.as_type.loc11_11.2 => constants.%empty_struct_type
// CHECK:STDOUT: %type.as.Copy.impl.Op.bound => constants.%type.as.Copy.impl.Op.bound.57f
// CHECK:STDOUT: }
// CHECK:STDOUT: