Files
carbon-lang/toolchain/check/testdata/if_expr/fail_partial_constant.carbon
T
Geoff RomerandDana Jansens e67b6db910 Give files shorthand names in textual SemIR (#8104)
Shorthand names are intended to be used in the textual format of
references to instructions in another IR (which can't happen now, but
will be possible in a forthcoming PR). The shorthand name uses the file
name (minus the `.carbon` suffix) as a base, and if that base name is
ambiguous within the compilation, it is given a numerical suffix `.N`,
just like other textual IR names. Shorthand names are prefixed with `$`
in the textual format, to distinguish them from inst and scope names.

For example, a file `foo/bar.carbon` has the shorthand name `$bar`, and
an instruction `@baz.%qux` in that file can be referenced from another
file as `$bar.@baz.%qux`.

This PR adds `toolchain/check/testdata/basics/file_name.carbon` to test
this behavior. All other test changes are just applying this change to
the existing textual SemIR dumps.

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
2026-11-12 23:34:32 +00:00

241 lines
13 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/primitives.carbon
// TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
// EXTRA-ARGS: --dump-sem-ir-ranges=if-present
//
// AUTOUPDATE
// TIP: To test this file alone, run:
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/if_expr/fail_partial_constant.carbon
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if_expr/fail_partial_constant.carbon
// --- fail_non_constant_condition.carbon
package NonConstantCondition;
fn ConditionIsNonConstant(b: bool) {
// We choose to not accept this even if both arms evaluate to the same
// constant value, because it notionally involves evaluating a non-constant
// condition.
// CHECK:STDERR: ╭─┤ fail_non_constant_condition.carbon:[[@LINE+7]]:17
// CHECK:STDERR: -> │ var unused v: if b then i32 else i32 = 1;
// CHECK:STDERR: · ━━━━━━━━━━━┳━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: cannot evaluate type expression
// CHECK:STDERR: · ┃ [TypeExprEvaluationFailure]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
var unused v: if b then i32 else i32 = 1;
}
// --- fail_non_constant_result.carbon
package NonConstantResult;
fn ChosenBranchIsNonConstant(t: type) {
// CHECK:STDERR: ╭─┤ fail_non_constant_result.carbon:[[@LINE+7]]:17
// CHECK:STDERR: -> │ var unused v: if true then t else i32 = 1;
// CHECK:STDERR: · ━━━━━━━━━━━┳━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: cannot evaluate type expression
// CHECK:STDERR: · ┃ [TypeExprEvaluationFailure]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
var unused v: if true then t else i32 = 1;
// CHECK:STDERR: ╭─┤ fail_non_constant_result.carbon:[[@LINE+7]]:17
// CHECK:STDERR: -> │ var unused w: if false then i32 else t = 1;
// CHECK:STDERR: · ━━━━━━━━━━━━┳━━━━━━━━━━━
// CHECK:STDERR: · ┗━━┫ error: cannot evaluate type expression
// CHECK:STDERR: · ┃ [TypeExprEvaluationFailure]
// CHECK:STDERR: ╰────
// CHECK:STDERR:
var unused w: if false then i32 else t = 1;
}
// CHECK:STDOUT: --- fail_non_constant_condition.carbon = $fail_non_constant_condition
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
// CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern [concrete]
// CHECK:STDOUT: %b.patt: %pattern_type.831 = wrapper_binding_pattern b, %b.param_patt [concrete]
// CHECK:STDOUT: %ConditionIsNonConstant.type: type = fn_type @ConditionIsNonConstant [concrete]
// CHECK:STDOUT: %ConditionIsNonConstant: %ConditionIsNonConstant.type = struct_value () [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %Int.type: type = parameterized_class_name_type @Int [concrete]
// CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
// CHECK:STDOUT: %SelfDestruct.type: type = fn_type @SelfDestruct [concrete]
// CHECK:STDOUT: %SelfDestruct: %SelfDestruct.type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: .Bool = %Core.Bool
// CHECK:STDOUT: .Int = %Core.Int
// CHECK:STDOUT: .SelfDestruct = %Core.SelfDestruct
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Bool: type = import_ref Core//prelude/parts/bool, Bool, loaded [concrete = bool]
// CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
// CHECK:STDOUT: %Core.SelfDestruct: %SelfDestruct.type = import_ref Core//prelude/parts/destroy, SelfDestruct, loaded [concrete = constants.%SelfDestruct]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .ConditionIsNonConstant = %ConditionIsNonConstant.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %ConditionIsNonConstant.decl: %ConditionIsNonConstant.type = fn_decl @ConditionIsNonConstant [concrete = constants.%ConditionIsNonConstant] {
// CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern [concrete = constants.%b.param_patt]
// CHECK:STDOUT: %b.patt: %pattern_type.831 = wrapper_binding_pattern b, %b.param_patt [concrete = constants.%b.patt]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %b.param: bool = value_param call_param0
// CHECK:STDOUT: %.loc4: type = type_literal bool [concrete = bool]
// CHECK:STDOUT: %b: bool = wrapper_binding b, %b.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ConditionIsNonConstant(%b.param: bool) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %v.var: ref <error> = var_storage %v.var_patt [concrete = <error>]
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
// CHECK:STDOUT: assign %v.var, <error>
// CHECK:STDOUT: br !.loc15_20
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc15_20:
// CHECK:STDOUT: %b.ref: bool = name_ref b, %b
// CHECK:STDOUT: if %b.ref br !if.expr.then else br !if.expr.else
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then:
// CHECK:STDOUT: %i32.loc15_27: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: br !if.expr.result(%i32.loc15_27)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else:
// CHECK:STDOUT: %i32.loc15_36: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: br !if.expr.result(%i32.loc15_36)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result:
// CHECK:STDOUT: %.loc15: type = block_arg !if.expr.result
// CHECK:STDOUT: br !.loc15_15
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc15_15:
// CHECK:STDOUT: %v: <error> = wrapper_binding v, <error> [concrete = <error>]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %v.patt: <error> = ref_binding_pattern v [concrete = <error>]
// CHECK:STDOUT: %v.var_patt: <error> = var_pattern %v.patt [concrete = <error>]
// CHECK:STDOUT: }
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_non_constant_result.carbon = $fail_non_constant_result
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: type: type = facet_type <type> [concrete]
// CHECK:STDOUT: %pattern_type.9a587c.1: type = pattern_type type [concrete]
// CHECK:STDOUT: %t.param_patt.277: %pattern_type.9a587c.1 = value_param_pattern [concrete]
// CHECK:STDOUT: %t.patt.3a0: %pattern_type.9a587c.1 = wrapper_binding_pattern t, %t.param_patt.277 [concrete]
// CHECK:STDOUT: %ChosenBranchIsNonConstant.type: type = fn_type @ChosenBranchIsNonConstant [concrete]
// CHECK:STDOUT: %ChosenBranchIsNonConstant: %ChosenBranchIsNonConstant.type = struct_value () [concrete]
// CHECK:STDOUT: %true: bool = bool_literal true [concrete]
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %Int.type: type = parameterized_class_name_type @Int [concrete]
// CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
// CHECK:STDOUT: %false: bool = bool_literal false [concrete]
// CHECK:STDOUT: %SelfDestruct.type: type = fn_type @SelfDestruct [concrete]
// CHECK:STDOUT: %SelfDestruct: %SelfDestruct.type = struct_value () [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
// CHECK:STDOUT: .Int = %Core.Int
// CHECK:STDOUT: .SelfDestruct = %Core.SelfDestruct
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/parts/int, Int, loaded [concrete = constants.%Int.generic]
// CHECK:STDOUT: %Core.SelfDestruct: %SelfDestruct.type = import_ref Core//prelude/parts/destroy, SelfDestruct, loaded [concrete = constants.%SelfDestruct]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
// CHECK:STDOUT: package: <namespace> = namespace [concrete] {
// CHECK:STDOUT: .Core = imports.%Core
// CHECK:STDOUT: .ChosenBranchIsNonConstant = %ChosenBranchIsNonConstant.decl
// CHECK:STDOUT: }
// CHECK:STDOUT: %Core.import = import Core
// CHECK:STDOUT: %ChosenBranchIsNonConstant.decl: %ChosenBranchIsNonConstant.type = fn_decl @ChosenBranchIsNonConstant [concrete = constants.%ChosenBranchIsNonConstant] {
// CHECK:STDOUT: %t.param_patt: %pattern_type.9a587c.1 = value_param_pattern [concrete = constants.%t.param_patt.277]
// CHECK:STDOUT: %t.patt: %pattern_type.9a587c.1 = wrapper_binding_pattern t, %t.param_patt [concrete = constants.%t.patt.3a0]
// CHECK:STDOUT: } {
// CHECK:STDOUT: %t.param: type = value_param call_param0
// CHECK:STDOUT: %.loc4: type = type_literal type [concrete = type]
// CHECK:STDOUT: %t: type = wrapper_binding t, %t.param
// CHECK:STDOUT: }
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @ChosenBranchIsNonConstant(%t.param: type) {
// CHECK:STDOUT: !entry:
// CHECK:STDOUT: %v.var: ref <error> = var_storage %v.var_patt [concrete = <error>]
// CHECK:STDOUT: %int_1.loc12: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
// CHECK:STDOUT: assign %v.var, <error>
// CHECK:STDOUT: br !.loc12_20
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc12_20:
// CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
// CHECK:STDOUT: if %true br !if.expr.then.loc12 else br !if.expr.else.loc12
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then.loc12:
// CHECK:STDOUT: %t.ref.loc12: type = name_ref t, %t
// CHECK:STDOUT: br !if.expr.result.loc12(%t.ref.loc12)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else.loc12:
// CHECK:STDOUT: %i32.loc12: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: br !if.expr.result.loc12(%i32.loc12)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result.loc12:
// CHECK:STDOUT: %.loc12: type = block_arg !if.expr.result.loc12
// CHECK:STDOUT: br !.loc12_15
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc12_15:
// CHECK:STDOUT: %v: <error> = wrapper_binding v, <error> [concrete = <error>]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %v.patt: <error> = ref_binding_pattern v [concrete = <error>]
// CHECK:STDOUT: %v.var_patt: <error> = var_pattern %v.patt [concrete = <error>]
// CHECK:STDOUT: }
// CHECK:STDOUT: %w.var: ref <error> = var_storage %w.var_patt [concrete = <error>]
// CHECK:STDOUT: %int_1.loc20: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
// CHECK:STDOUT: assign %w.var, <error>
// CHECK:STDOUT: br !.loc20_20
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc20_20:
// CHECK:STDOUT: %false: bool = bool_literal false [concrete = constants.%false]
// CHECK:STDOUT: if %false br !if.expr.then.loc20 else br !if.expr.else.loc20
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.then.loc20:
// CHECK:STDOUT: %i32.loc20: type = type_literal constants.%i32 [concrete = constants.%i32]
// CHECK:STDOUT: br !if.expr.result.loc20(%i32.loc20)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.else.loc20:
// CHECK:STDOUT: %t.ref.loc20: type = name_ref t, %t
// CHECK:STDOUT: br !if.expr.result.loc20(%t.ref.loc20)
// CHECK:STDOUT:
// CHECK:STDOUT: !if.expr.result.loc20:
// CHECK:STDOUT: %.loc20: type = block_arg !if.expr.result.loc20
// CHECK:STDOUT: br !.loc20_15
// CHECK:STDOUT:
// CHECK:STDOUT: !.loc20_15:
// CHECK:STDOUT: %w: <error> = wrapper_binding w, <error> [concrete = <error>]
// CHECK:STDOUT: name_binding_decl {
// CHECK:STDOUT: %w.patt: <error> = ref_binding_pattern w [concrete = <error>]
// CHECK:STDOUT: %w.var_patt: <error> = var_pattern %w.patt [concrete = <error>]
// CHECK:STDOUT: }
// CHECK:STDOUT: return
// CHECK:STDOUT: }
// CHECK:STDOUT: