// 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: %.df2: form = init_form bool, call_param1 [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.348: = impl_witness imports.%Copy.impl_witness_table.3cc [concrete] // CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value bool, (%Copy.impl_witness.348) [concrete] // CHECK:STDOUT: %.56b: 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.082: = 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.12b: = bound_method %true, %bool.as.Copy.impl.Op [concrete] // CHECK:STDOUT: } // CHECK:STDOUT: // CHECK:STDOUT: imports { // CHECK:STDOUT: %Core.import_ref.595: %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.3cc = impl_witness_table (%Core.import_ref.595), @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: %.loc4_19.3: form = init_form %.loc4_19.2, call_param1 [concrete = constants.%.df2] // 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: %.56b = impl_witness_access constants.%Copy.impl_witness.348, element0 [concrete = constants.%bool.as.Copy.impl.Op] // CHECK:STDOUT: %bound_method.loc6: = bound_method %false, %impl.elem0.loc6 [concrete = constants.%bool.as.Copy.impl.Op.bound.082] // 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.param // CHECK:STDOUT: // CHECK:STDOUT: !if.else: // CHECK:STDOUT: %true: bool = bool_literal true [concrete = constants.%true] // CHECK:STDOUT: %impl.elem0.loc8: %.56b = impl_witness_access constants.%Copy.impl_witness.348, element0 [concrete = constants.%bool.as.Copy.impl.Op] // CHECK:STDOUT: %bound_method.loc8: = bound_method %true, %impl.elem0.loc8 [concrete = constants.%bool.as.Copy.impl.Op.bound.12b] // 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.param // CHECK:STDOUT: } // CHECK:STDOUT: