Files
carbon-lang/toolchain/check/testdata/eval/symbolic.carbon
Dana Jansens 7beb29f8b2 Use a fallback loc for diagnostics generated on canonical insts (#7967)
We allow using `InstId` as a `LocId` for diagnostics, which works great
in `handle_*` functions, where we are working with non-canonical insts.
And convert and pattern matching often are given non-canonical insts,
which they can use, or pull apart to find some other inst, and use those
as a location for diagnostics. However these operations can also be
called with canonical insts, which have no location. In that case if we
need to diagnose an error, it ends up not being attached to any code at
all. Since we always have a `LocId` somewhere up the call stack, pass it
along and in these places that handle canonical instructions too, use
`GetInstLocationOrFallback()` to get a valid location for diagnostics.

This is also similar to what eval does with its fallback `LocId`.

The result is that many diagnostics now have a location where they had
none before. It also means some insts don't get `<elided>` in formatted
semir for not having a location at all and thus not being in range.
2026-10-15 19:03:10 +00:00

203 lines
9.9 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(generic B: bool) -> type {
if (B) { return (); }
return ({}, {});
}
fn G(generic 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(generic B: bool) -> type {
if (B) { return (); }
return ({}, {});
}
fn G(generic 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+3]]:3: error: unable to monomorphize specific `G(true)` [ResolvingSpecificHere]
// CHECK:STDERR: fail_symbolic_call_to_eval_fn_invalid_instantiation.carbon:[[@LINE-6]]:24-31: 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(generic 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({});
}
// --- fail_print_call_in_diagnostic.carbon
library "[[@TEST_NAME]]";
eval fn F(generic B: bool) -> type {
if (B) { return (); }
return bool;
}
eval fn G(generic B: bool, (a: bool, b: bool)) -> type {
if (B and a and b) { return (); }
return bool;
}
// A redeclaration with a mismatched parameter type prints the symbolic call as
// the type of the parameter. The argument for the generic parameter is found
// in the specific for the callee, not in the arguments of the SemIR `call`.
fn UseF(generic B: bool, x: F(B));
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE+3]]:26-32: error: type `<pattern for bool>` of parameter 2 in redeclaration differs from previous parameter type `<pattern for F(B)>` [RedeclParamDiffersType]
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE-2]]:26-32: note: previous declaration's corresponding parameter here [RedeclParamPrevious]
// CHECK:STDERR:
fn UseF(generic B: bool, x: bool);
// The arguments of the SemIR `call` are flattened by the nested tuple pattern
// in the signature of `G`, and are regrouped here to match the source.
fn UseG(generic B: bool, x: G(B, (true, false)));
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE+3]]:26-32: error: type `<pattern for bool>` of parameter 2 in redeclaration differs from previous parameter type `<pattern for G(B, (true, false))>` [RedeclParamDiffersType]
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE-2]]:26-47: note: previous declaration's corresponding parameter here [RedeclParamPrevious]
// CHECK:STDERR:
fn UseG(generic B: bool, x: bool);
// A one-element tuple pattern needs a trailing comma.
eval fn H(generic B: bool, (a: bool,)) -> type {
if (B and a) { return (); }
return bool;
}
fn UseH(generic B: bool, x: H(B, (true,)));
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE+3]]:26-32: error: type `<pattern for bool>` of parameter 2 in redeclaration differs from previous parameter type `<pattern for H(B, (true,))>` [RedeclParamDiffersType]
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE-2]]:26-41: note: previous declaration's corresponding parameter here [RedeclParamPrevious]
// CHECK:STDERR:
fn UseH(generic B: bool, x: bool);
// The `self` argument is the first argument of the SemIR `call`, but is printed
// before the name of the method, as it was written.
class C {
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE+2]]:13-22: warning: binding `self` unused [UnusedBinding]
// CHECK:STDERR:
eval fn M(self: Self, b: bool) -> type {
if (b) { return (); }
return bool;
}
}
fn UseM(generic c: C, x: c.M(true));
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE+3]]:23-29: error: type `<pattern for bool>` of parameter 2 in redeclaration differs from previous parameter type `<pattern for (c).M(true)>` [RedeclParamDiffersType]
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE-2]]:23-34: note: previous declaration's corresponding parameter here [RedeclParamPrevious]
// CHECK:STDERR:
fn UseM(generic c: C, x: bool);
// Parentheses are added around the `self` argument if needed.
interface J {
eval fn N(self: Self) -> type;
}
impl type as J {
eval fn N(self: Self) -> type { return self; }
}
fn UseN(generic T: type, x: (const T).(J.N)());
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE+3]]:26-32: error: type `<pattern for bool>` of parameter 2 in redeclaration differs from previous parameter type `<pattern for (const T).N()>` [RedeclParamDiffersType]
// CHECK:STDERR: fail_print_call_in_diagnostic.carbon:[[@LINE-2]]:26-45: note: previous declaration's corresponding parameter here [RedeclParamPrevious]
// CHECK:STDERR:
fn UseN(generic T: type, x: bool);
// CHECK:STDOUT: --- dependent_symbolic_instruction.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// 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.f3e: <witness> = impl_witness imports.%Copy.impl_witness_table.aa5 [concrete]
// CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value type, (%Copy.impl_witness.f3e) [concrete]
// CHECK:STDOUT: %Copy.WithSelf.Op.type.ca9: type = fn_type @Copy.WithSelf.Op, @Copy.WithSelf(%Copy.facet) [concrete]
// CHECK:STDOUT: %.cd2: type = fn_type_with_self_type %Copy.WithSelf.Op.type.ca9, %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.aa5 = impl_witness_table (%Core.import_ref.9c3), @type.as.Copy.impl [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: generic fn @E(%U.loc6_20.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_20.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_20.2 [symbolic = %U.loc6_20.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: %.cd2 = impl_witness_access constants.%Copy.impl_witness.f3e, 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_20.2 => constants.%U.patt.4a7
// CHECK:STDOUT: %U.loc6_20.1 => constants.%U.013
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: specific @E(constants.%Z.facet) {
// CHECK:STDOUT: %U.patt.loc6_20.2 => constants.%U.patt.4a7
// CHECK:STDOUT: %U.loc6_20.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: