| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- // 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/if/basics.carbon
- // TIP: To dump output, run:
- // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/if/basics.carbon
- // --- else.carbon
- library "[[@TEST_NAME]]";
- fn F() {}
- fn G() {}
- fn H() {}
- //@dump-sem-ir-begin
- fn If(b: bool) {
- if (b) {
- F();
- } else {
- G();
- }
- H();
- }
- //@dump-sem-ir-end
- // --- no_else.carbon
- library "[[@TEST_NAME]]";
- fn F() {}
- fn G() {}
- //@dump-sem-ir-begin
- fn If(b: bool) {
- if (b) {
- F();
- }
- G();
- }
- //@dump-sem-ir-end
- // --- unreachable_fallthrough.carbon
- library "[[@TEST_NAME]]";
- //@dump-sem-ir-begin
- fn If(b: bool) -> bool {
- if (b) {
- return false;
- } else {
- return true;
- }
- // Missing return here is OK.
- }
- //@dump-sem-ir-end
- // --- fail_reachable_fallthrough.carbon
- library "[[@TEST_NAME]]";
- fn If1(b: bool) -> bool {
- if (b) {
- return false;
- } else {
- }
- // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+4]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
- // CHECK:STDERR: }
- // CHECK:STDERR: ^
- // CHECK:STDERR:
- }
- fn If2(b: bool) -> bool {
- if (b) {
- } else {
- return true;
- }
- // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+4]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
- // CHECK:STDERR: }
- // CHECK:STDERR: ^
- // CHECK:STDERR:
- }
- fn If3(b: bool) -> bool {
- if (b) {
- return false;
- }
- // CHECK:STDERR: fail_reachable_fallthrough.carbon:[[@LINE+4]]:1: error: missing `return` at end of function with declared return type [MissingReturnStatement]
- // CHECK:STDERR: }
- // CHECK:STDERR: ^
- // CHECK:STDERR:
- }
- // --- fail_scope.carbon
- library "[[@TEST_NAME]]";
- fn VarScope(b: bool) -> bool {
- if (b) {
- var n: bool = true;
- return n;
- }
- // CHECK:STDERR: fail_scope.carbon:[[@LINE+4]]:10: error: name `n` not found [NameNotFound]
- // CHECK:STDERR: return n;
- // CHECK:STDERR: ^
- // CHECK:STDERR:
- return n;
- }
- // CHECK:STDOUT: --- else.carbon
- // CHECK:STDOUT:
- // CHECK:STDOUT: constants {
- // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
- // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
- // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
- // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
- // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
- // CHECK:STDOUT: %H.type: type = fn_type @H [concrete]
- // CHECK:STDOUT: %H: %H.type = struct_value () [concrete]
- // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
- // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
- // CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
- // CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
- // CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: imports {
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: file {
- // CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
- // CHECK:STDOUT: %b.patt: %pattern_type.831 = value_binding_pattern b [concrete]
- // CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern %b.patt, call_param0 [concrete]
- // CHECK:STDOUT: } {
- // CHECK:STDOUT: %b.param: bool = value_param call_param0
- // CHECK:STDOUT: %.loc8_10.1: type = splice_block %.loc8_10.3 [concrete = bool] {
- // CHECK:STDOUT: %Bool.call: init type = call constants.%Bool() [concrete = bool]
- // CHECK:STDOUT: %.loc8_10.2: type = value_of_initializer %Bool.call [concrete = bool]
- // CHECK:STDOUT: %.loc8_10.3: type = converted %Bool.call, %.loc8_10.2 [concrete = bool]
- // CHECK:STDOUT: }
- // CHECK:STDOUT: %b: bool = value_binding b, %b.param
- // CHECK:STDOUT: }
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: fn @If(%b.param: bool) {
- // CHECK:STDOUT: !entry:
- // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
- // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
- // CHECK:STDOUT:
- // CHECK:STDOUT: !if.then:
- // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
- // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
- // CHECK:STDOUT: br !if.done
- // CHECK:STDOUT:
- // CHECK:STDOUT: !if.else:
- // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
- // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref()
- // CHECK:STDOUT: br !if.done
- // CHECK:STDOUT:
- // CHECK:STDOUT: !if.done:
- // CHECK:STDOUT: %H.ref: %H.type = name_ref H, file.%H.decl [concrete = constants.%H]
- // CHECK:STDOUT: %H.call: init %empty_tuple.type = call %H.ref()
- // CHECK:STDOUT: return
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: --- no_else.carbon
- // CHECK:STDOUT:
- // CHECK:STDOUT: constants {
- // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
- // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
- // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
- // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
- // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
- // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
- // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
- // CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
- // CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
- // CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: imports {
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: file {
- // CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
- // CHECK:STDOUT: %b.patt: %pattern_type.831 = value_binding_pattern b [concrete]
- // CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern %b.patt, call_param0 [concrete]
- // CHECK:STDOUT: } {
- // CHECK:STDOUT: %b.param: bool = value_param call_param0
- // CHECK:STDOUT: %.loc7_10.1: type = splice_block %.loc7_10.3 [concrete = bool] {
- // CHECK:STDOUT: %Bool.call: init type = call constants.%Bool() [concrete = bool]
- // CHECK:STDOUT: %.loc7_10.2: type = value_of_initializer %Bool.call [concrete = bool]
- // CHECK:STDOUT: %.loc7_10.3: type = converted %Bool.call, %.loc7_10.2 [concrete = bool]
- // CHECK:STDOUT: }
- // CHECK:STDOUT: %b: bool = value_binding b, %b.param
- // CHECK:STDOUT: }
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: fn @If(%b.param: bool) {
- // CHECK:STDOUT: !entry:
- // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
- // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
- // CHECK:STDOUT:
- // CHECK:STDOUT: !if.then:
- // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
- // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.ref()
- // CHECK:STDOUT: br !if.else
- // CHECK:STDOUT:
- // CHECK:STDOUT: !if.else:
- // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [concrete = constants.%G]
- // CHECK:STDOUT: %G.call: init %empty_tuple.type = call %G.ref()
- // CHECK:STDOUT: return
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: --- unreachable_fallthrough.carbon
- // CHECK:STDOUT:
- // CHECK:STDOUT: constants {
- // CHECK:STDOUT: %Bool.type: type = fn_type @Bool [concrete]
- // CHECK:STDOUT: %Bool: %Bool.type = struct_value () [concrete]
- // CHECK:STDOUT: %pattern_type.831: type = pattern_type bool [concrete]
- // CHECK:STDOUT: %If.type: type = fn_type @If [concrete]
- // CHECK:STDOUT: %If: %If.type = struct_value () [concrete]
- // CHECK:STDOUT: %false: bool = bool_literal false [concrete]
- // CHECK:STDOUT: %Copy.type: type = facet_type <@Copy> [concrete]
- // CHECK:STDOUT: %Copy.Op.type: type = fn_type @Copy.Op [concrete]
- // CHECK:STDOUT: %Copy.impl_witness.a56: <witness> = impl_witness imports.%Copy.impl_witness_table.189 [concrete]
- // CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value bool, (%Copy.impl_witness.a56) [concrete]
- // CHECK:STDOUT: %.05c: type = fn_type_with_self_type %Copy.Op.type, %Copy.facet [concrete]
- // CHECK:STDOUT: %bool.as.Copy.impl.Op.type: type = fn_type @bool.as.Copy.impl.Op [concrete]
- // CHECK:STDOUT: %bool.as.Copy.impl.Op: %bool.as.Copy.impl.Op.type = struct_value () [concrete]
- // CHECK:STDOUT: %bool.as.Copy.impl.Op.bound.4eb: <bound method> = bound_method %false, %bool.as.Copy.impl.Op [concrete]
- // CHECK:STDOUT: %true: bool = bool_literal true [concrete]
- // CHECK:STDOUT: %bool.as.Copy.impl.Op.bound.5ee: <bound method> = bound_method %true, %bool.as.Copy.impl.Op [concrete]
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: imports {
- // CHECK:STDOUT: %Core.import_ref.cc5: %bool.as.Copy.impl.Op.type = import_ref Core//prelude/parts/copy, loc{{\d+_\d+}}, loaded [concrete = constants.%bool.as.Copy.impl.Op]
- // CHECK:STDOUT: %Copy.impl_witness_table.189 = impl_witness_table (%Core.import_ref.cc5), @bool.as.Copy.impl [concrete]
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: file {
- // CHECK:STDOUT: %If.decl: %If.type = fn_decl @If [concrete = constants.%If] {
- // CHECK:STDOUT: %b.patt: %pattern_type.831 = value_binding_pattern b [concrete]
- // CHECK:STDOUT: %b.param_patt: %pattern_type.831 = value_param_pattern %b.patt, call_param0 [concrete]
- // CHECK:STDOUT: %return.patt: %pattern_type.831 = return_slot_pattern [concrete]
- // CHECK:STDOUT: %return.param_patt: %pattern_type.831 = out_param_pattern %return.patt, call_param1 [concrete]
- // CHECK:STDOUT: } {
- // CHECK:STDOUT: %Bool.call.loc4_19: init type = call constants.%Bool() [concrete = bool]
- // CHECK:STDOUT: %.loc4_19.1: type = value_of_initializer %Bool.call.loc4_19 [concrete = bool]
- // CHECK:STDOUT: %.loc4_19.2: type = converted %Bool.call.loc4_19, %.loc4_19.1 [concrete = bool]
- // CHECK:STDOUT: %b.param: bool = value_param call_param0
- // CHECK:STDOUT: %.loc4_10.1: type = splice_block %.loc4_10.3 [concrete = bool] {
- // CHECK:STDOUT: %Bool.call.loc4_10: init type = call constants.%Bool() [concrete = bool]
- // CHECK:STDOUT: %.loc4_10.2: type = value_of_initializer %Bool.call.loc4_10 [concrete = bool]
- // CHECK:STDOUT: %.loc4_10.3: type = converted %Bool.call.loc4_10, %.loc4_10.2 [concrete = bool]
- // CHECK:STDOUT: }
- // CHECK:STDOUT: %b: bool = value_binding b, %b.param
- // CHECK:STDOUT: %return.param: ref bool = out_param call_param1
- // CHECK:STDOUT: %return: ref bool = return_slot %return.param
- // CHECK:STDOUT: }
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
- // CHECK:STDOUT: fn @If(%b.param: bool) -> bool {
- // CHECK:STDOUT: !entry:
- // CHECK:STDOUT: %b.ref: bool = name_ref b, %b
- // CHECK:STDOUT: if %b.ref br !if.then else br !if.else
- // CHECK:STDOUT:
- // CHECK:STDOUT: !if.then:
- // CHECK:STDOUT: %false: bool = bool_literal false [concrete = constants.%false]
- // CHECK:STDOUT: %impl.elem0.loc6: %.05c = impl_witness_access constants.%Copy.impl_witness.a56, element0 [concrete = constants.%bool.as.Copy.impl.Op]
- // CHECK:STDOUT: %bound_method.loc6: <bound method> = bound_method %false, %impl.elem0.loc6 [concrete = constants.%bool.as.Copy.impl.Op.bound.4eb]
- // CHECK:STDOUT: %bool.as.Copy.impl.Op.call.loc6: init bool = call %bound_method.loc6(%false) [concrete = constants.%false]
- // CHECK:STDOUT: return %bool.as.Copy.impl.Op.call.loc6 to %return
- // CHECK:STDOUT:
- // CHECK:STDOUT: !if.else:
- // CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true]
- // CHECK:STDOUT: %impl.elem0.loc8: %.05c = impl_witness_access constants.%Copy.impl_witness.a56, element0 [concrete = constants.%bool.as.Copy.impl.Op]
- // CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %true, %impl.elem0.loc8 [concrete = constants.%bool.as.Copy.impl.Op.bound.5ee]
- // CHECK:STDOUT: %bool.as.Copy.impl.Op.call.loc8: init bool = call %bound_method.loc8(%true) [concrete = constants.%true]
- // CHECK:STDOUT: return %bool.as.Copy.impl.Op.call.loc8 to %return
- // CHECK:STDOUT: }
- // CHECK:STDOUT:
|