diff --git a/toolchain/check/action.h b/toolchain/check/action.h index 13048d138147..a8ef790fb2da 100644 --- a/toolchain/check/action.h +++ b/toolchain/check/action.h @@ -83,17 +83,26 @@ auto AddDependentActionSplice(Context& context, LocT loc, InstT inst, // to act as the type of the splice. template auto HandleAction(Context& context, LocIdT loc_id, - SemIR::TypeInstId result_type_inst_id, ActionT action_inst) - -> SemIR::InstId { + SemIR::TypeInstId expected_result_type_inst_id, + ActionT action_inst) -> SemIR::InstId { CARBON_CHECK(action_inst.type_id == SemIR::InstType::TypeId); if (!ActionIsPerformable(context, action_inst)) { return AddDependentActionSplice(context, SemIR::LocIdAndInst::RuntimeVerified( context.sem_ir(), loc_id, action_inst), - result_type_inst_id); + expected_result_type_inst_id); } - return PerformAction(context, loc_id, action_inst); + auto expected_result_type_id = + expected_result_type_inst_id.has_value() + ? context.types().GetTypeIdForTypeInstId(expected_result_type_inst_id) + : SemIR::TypeId::None; + auto result_id = PerformAction(context, loc_id, action_inst); + auto result_type_id = context.insts().Get(result_id).type_id(); + CARBON_CHECK(expected_result_type_id == SemIR::TypeId::None || + result_type_id == SemIR::ErrorInst::TypeId || + result_type_id == expected_result_type_id); + return result_id; } namespace Internal { diff --git a/toolchain/check/function.cpp b/toolchain/check/function.cpp index c601f5b73734..9cc135755200 100644 --- a/toolchain/check/function.cpp +++ b/toolchain/check/function.cpp @@ -45,8 +45,9 @@ auto FindSelfPattern(Context& context, auto AddReturnPattern(Context& context, SemIR::LocId loc_id, Context::FormExpr form_expr) -> SemIR::InstId { auto result_type_id = GetPatternType(context, form_expr.type_component_id); + auto result_type_inst_id = context.types().GetTypeInstId(result_type_id); auto result_id = HandleAction( - context, loc_id, form_expr.type_component_inst_id, + context, loc_id, result_type_inst_id, {.type_id = SemIR::InstType::TypeId, .form_id = form_expr.form_inst_id}); return AddInst( context, loc_id, diff --git a/toolchain/check/testdata/basics/raw_sem_ir/bundle.carbon b/toolchain/check/testdata/basics/raw_sem_ir/bundle.carbon index ac071035b358..3241dd05fac6 100644 --- a/toolchain/check/testdata/basics/raw_sem_ir/bundle.carbon +++ b/toolchain/check/testdata/basics/raw_sem_ir/bundle.carbon @@ -38,7 +38,7 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: entity_name70000001: {name: name1, parent_scope: name_scope70000001, index: -1, is_template: 0, is_unused: 0, form: inst} // CHECK:STDOUT: entity_name70000002: {name: name1, parent_scope: name_scope, index: 0, is_template: 0, is_unused: 0, form: inst} // CHECK:STDOUT: functions: -// CHECK:STDOUT: function70000000: {name: name0, parent_scope: name_scope0, call_param_patterns_id: inst_block70000007, call_params_id: inst_block70000008, return_type_inst_id: inst70000024, return_form_inst_id: inst70000021, return_pattern_id: inst7000002B} +// CHECK:STDOUT: function70000000: {name: name0, parent_scope: name_scope0, call_param_patterns_id: inst_block70000007, call_params_id: inst_block70000008, return_type_inst_id: inst70000024, return_form_inst_id: inst70000021, return_pattern_id: inst7000002C} // CHECK:STDOUT: classes: {} // CHECK:STDOUT: interfaces: {} // CHECK:STDOUT: associated_constants: {} @@ -113,33 +113,33 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: inst70000024: {kind: TypeComponentOf, arg0: inst70000022, type: type(TypeType)} // CHECK:STDOUT: inst70000025: {kind: PatternType, arg0: inst70000024, type: type(TypeType)} // CHECK:STDOUT: inst70000026: {kind: OutFormParamPatternAction, arg0: inst70000021, type: type(inst(InstType))} -// CHECK:STDOUT: inst70000027: {kind: SpliceInst, arg0: inst70000026, type: type(symbolic_constant7000000C)} -// CHECK:STDOUT: inst70000028: {kind: SpliceInst, arg0: inst70000026, type: type(symbolic_constant70000008)} +// CHECK:STDOUT: inst70000027: {kind: SpliceInst, arg0: inst70000026, type: type(symbolic_constant7000000D)} +// CHECK:STDOUT: inst70000028: {kind: SpliceInst, arg0: inst70000026, type: type(symbolic_constant70000009)} // CHECK:STDOUT: inst70000029: {kind: TypeComponentOf, arg0: inst70000023, type: type(TypeType)} -// CHECK:STDOUT: inst7000002A: {kind: SpliceInst, arg0: inst70000026, type: type(symbolic_constant7000000C)} -// CHECK:STDOUT: inst7000002B: {kind: ReturnSlotPattern, arg0: inst70000027, arg1: inst70000024, type: type(symbolic_constant7000000F)} -// CHECK:STDOUT: inst7000002C: {kind: ReturnSlotPattern, arg0: inst70000028, arg1: inst70000024, type: type(symbolic_constant70000009)} -// CHECK:STDOUT: inst7000002D: {kind: PatternType, arg0: inst70000029, type: type(TypeType)} -// CHECK:STDOUT: inst7000002E: {kind: ReturnSlotPattern, arg0: inst7000002A, arg1: inst70000029, type: type(symbolic_constant7000000F)} +// CHECK:STDOUT: inst7000002A: {kind: PatternType, arg0: inst70000029, type: type(TypeType)} +// CHECK:STDOUT: inst7000002B: {kind: SpliceInst, arg0: inst70000026, type: type(symbolic_constant7000000D)} +// CHECK:STDOUT: inst7000002C: {kind: ReturnSlotPattern, arg0: inst70000027, arg1: inst70000024, type: type(symbolic_constant7000000D)} +// CHECK:STDOUT: inst7000002D: {kind: ReturnSlotPattern, arg0: inst70000028, arg1: inst70000024, type: type(symbolic_constant70000009)} +// CHECK:STDOUT: inst7000002E: {kind: ReturnSlotPattern, arg0: inst7000002B, arg1: inst70000029, type: type(symbolic_constant7000000D)} // CHECK:STDOUT: inst7000002F: {kind: SpliceBlock, arg0: inst_block70000005, arg1: inst70000017, type: type(TypeType)} // CHECK:STDOUT: inst70000030: {kind: CalleePatternMatchAction, arg0: bundle70000000, type: type(inst(InstType))} -// CHECK:STDOUT: inst70000031: {kind: SpliceInst, arg0: inst70000030, type: type(symbolic_constant7000000C)} +// CHECK:STDOUT: inst70000031: {kind: SpliceInst, arg0: inst70000030, type: type(symbolic_constant7000000D)} // CHECK:STDOUT: inst70000032: {kind: ReturnSlot, arg0: inst70000024, arg1: inst70000031, type: type(symbolic_constant7000000C)} // CHECK:STDOUT: inst70000033: {kind: FunctionDecl, arg0: function70000000, arg1: inst_block7000000A, type: type(inst7000003C)} // CHECK:STDOUT: inst70000034: {kind: RefineFormAction, arg0: inst7000001D, type: type(inst(InstType))} // CHECK:STDOUT: inst70000035: {kind: SpliceInst, arg0: inst70000034, type: type(inst(FormType))} // CHECK:STDOUT: inst70000036: {kind: OutFormParamPatternAction, arg0: inst70000035, type: type(inst(InstType))} // CHECK:STDOUT: inst70000037: {kind: TypeComponentOf, arg0: inst70000035, type: type(TypeType)} -// CHECK:STDOUT: inst70000038: {kind: SpliceInst, arg0: inst70000036, type: type(symbolic_constant70000015)} -// CHECK:STDOUT: inst70000039: {kind: PatternType, arg0: inst70000037, type: type(TypeType)} -// CHECK:STDOUT: inst7000003A: {kind: ReturnSlotPattern, arg0: inst70000038, arg1: inst70000037, type: type(symbolic_constant70000017)} +// CHECK:STDOUT: inst70000038: {kind: PatternType, arg0: inst70000037, type: type(TypeType)} +// CHECK:STDOUT: inst70000039: {kind: SpliceInst, arg0: inst70000036, type: type(symbolic_constant70000016)} +// CHECK:STDOUT: inst7000003A: {kind: ReturnSlotPattern, arg0: inst70000039, arg1: inst70000037, type: type(symbolic_constant70000016)} // CHECK:STDOUT: inst7000003B: {kind: CalleePatternMatchAction, arg0: bundle70000001, type: type(inst(InstType))} // CHECK:STDOUT: inst7000003C: {kind: FunctionType, arg0: function70000000, arg1: specific, type: type(TypeType)} // CHECK:STDOUT: inst7000003D: {kind: TupleType, arg0: inst_block_empty, type: type(TypeType)} // CHECK:STDOUT: inst7000003E: {kind: StructValue, arg0: inst_block_empty, type: type(inst7000003C)} // CHECK:STDOUT: bundles: // CHECK:STDOUT: bundle70000000: {arg0: inst70000027, arg1: call_param0} -// CHECK:STDOUT: bundle70000001: {arg0: inst70000038, arg1: call_param0} +// CHECK:STDOUT: bundle70000001: {arg0: inst70000039, arg1: call_param0} // CHECK:STDOUT: constant_values: // CHECK:STDOUT: values: // CHECK:STDOUT: instF: concrete_constant(instF) @@ -165,12 +165,12 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: inst70000024: symbolic_constant70000008 // CHECK:STDOUT: inst70000025: symbolic_constant70000009 // CHECK:STDOUT: inst70000026: symbolic_constant7000000A -// CHECK:STDOUT: inst70000027: symbolic_constant7000000D +// CHECK:STDOUT: inst70000027: symbolic_constant7000000E // CHECK:STDOUT: inst70000028: symbolic_constant7000000B // CHECK:STDOUT: inst70000029: symbolic_constant7000000C // CHECK:STDOUT: inst7000002A: symbolic_constant7000000D -// CHECK:STDOUT: inst7000002B: symbolic_constant70000010 -// CHECK:STDOUT: inst7000002C: symbolic_constant7000000E +// CHECK:STDOUT: inst7000002B: symbolic_constant7000000E +// CHECK:STDOUT: inst7000002C: symbolic_constant70000010 // CHECK:STDOUT: inst7000002D: symbolic_constant7000000F // CHECK:STDOUT: inst7000002E: symbolic_constant70000010 // CHECK:STDOUT: inst7000002F: concrete_constant(inst(FormType)) @@ -201,10 +201,10 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: symbolic_constant7000000A: {inst: inst70000026, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl4}} // CHECK:STDOUT: symbolic_constant7000000B: {inst: inst70000028, kind: template, attached: null} // CHECK:STDOUT: symbolic_constant7000000C: {inst: inst70000024, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl5}} -// CHECK:STDOUT: symbolic_constant7000000D: {inst: inst70000028, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl6}} -// CHECK:STDOUT: symbolic_constant7000000E: {inst: inst7000002C, kind: template, attached: null} -// CHECK:STDOUT: symbolic_constant7000000F: {inst: inst70000025, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl7}} -// CHECK:STDOUT: symbolic_constant70000010: {inst: inst7000002C, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl8}} +// CHECK:STDOUT: symbolic_constant7000000D: {inst: inst70000025, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl6}} +// CHECK:STDOUT: symbolic_constant7000000E: {inst: inst70000028, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl7}} +// CHECK:STDOUT: symbolic_constant7000000F: {inst: inst7000002D, kind: template, attached: null} +// CHECK:STDOUT: symbolic_constant70000010: {inst: inst7000002D, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl8}} // CHECK:STDOUT: symbolic_constant70000011: {inst: inst70000030, kind: template, attached: {generic: generic70000000, index: generic_inst_in_decl9}} // CHECK:STDOUT: symbolic_constant70000012: {inst: inst70000034, kind: template, attached: null} // CHECK:STDOUT: symbolic_constant70000013: {inst: inst70000035, kind: template, attached: null} @@ -235,7 +235,7 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: 0: inst70000031 // CHECK:STDOUT: inst_block70000009: // CHECK:STDOUT: 0: inst70000019 -// CHECK:STDOUT: 1: inst7000002B +// CHECK:STDOUT: 1: inst7000002C // CHECK:STDOUT: inst_block7000000A: // CHECK:STDOUT: 0: inst7000001F // CHECK:STDOUT: 1: inst70000021 @@ -256,7 +256,7 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: 4: inst70000026 // CHECK:STDOUT: 5: inst70000029 // CHECK:STDOUT: 6: inst7000002A -// CHECK:STDOUT: 7: inst7000002D +// CHECK:STDOUT: 7: inst7000002B // CHECK:STDOUT: 8: inst7000002E // CHECK:STDOUT: 9: inst70000030 // CHECK:STDOUT: inst_block7000000E: diff --git a/toolchain/check/testdata/function/call/form.carbon b/toolchain/check/testdata/function/call/form.carbon index f72f60401310..37c75d3ba4c4 100644 --- a/toolchain/check/testdata/function/call/form.carbon +++ b/toolchain/check/testdata/function/call/form.carbon @@ -569,16 +569,16 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: %.344: Core.Form = splice_inst @F.%.loc4_28.1 [template] // CHECK:STDOUT: %.82f: type = type_component_of %.344 [template] // CHECK:STDOUT: %pattern_type.ec7: type = pattern_type %.82f [template] -// CHECK:STDOUT: %.f74: %.82f = splice_inst @F.%.loc4_24.2 [template] -// CHECK:STDOUT: %return.patt.08d: %pattern_type.ec7 = return_slot_pattern %.f74, %.82f [template] +// CHECK:STDOUT: %.b9b: %pattern_type.ec7 = splice_inst @F.%.loc4_24.2 [template] +// CHECK:STDOUT: %return.patt.96f: %pattern_type.ec7 = return_slot_pattern %.b9b, %.82f [template] // CHECK:STDOUT: %.33d: = refine_form_action %Form [template] // CHECK:STDOUT: %.80a: Core.Form = splice_inst %.33d [template] // CHECK:STDOUT: %.af3: = out_form_param_pattern_action %.80a [template] // CHECK:STDOUT: %.465: type = type_component_of %.80a [template] -// CHECK:STDOUT: %.e7f: %.465 = splice_inst %.af3 [template] // CHECK:STDOUT: %pattern_type.a51: type = pattern_type %.465 [template] -// CHECK:STDOUT: %return.patt.de8: %pattern_type.a51 = return_slot_pattern %.e7f, %.465 [template] -// CHECK:STDOUT: %.cab: = callee_pattern_match_action %.e7f, call_param0 [template] +// CHECK:STDOUT: %.3c2: %pattern_type.a51 = splice_inst %.af3 [template] +// CHECK:STDOUT: %return.patt.de0: %pattern_type.a51 = return_slot_pattern %.3c2, %.465 [template] +// CHECK:STDOUT: %.849: = callee_pattern_match_action %.3c2, call_param0 [template] // CHECK:STDOUT: %F.type: type = fn_type @F [concrete] // CHECK:STDOUT: %F: %F.type = struct_value () [concrete] // CHECK:STDOUT: } @@ -595,18 +595,18 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: file { // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] { // CHECK:STDOUT: %Form.patt.loc4_10.1: %pattern_type.13f = symbolic_binding_pattern Form, 0 [symbolic = %Form.patt.loc4_10.2 (constants.%Form.patt)] -// CHECK:STDOUT: %return.patt.loc4_24.1: @F.%pattern_type (%pattern_type.ec7) = return_slot_pattern %.loc4_24.6, constants.%.82f [template = %return.patt.loc4_24.2 (constants.%return.patt.08d)] +// CHECK:STDOUT: %return.patt.loc4_24.1: @F.%pattern_type (%pattern_type.ec7) = return_slot_pattern %.loc4_24.6, constants.%.82f [template = %return.patt.loc4_24.2 (constants.%return.patt.96f)] // CHECK:STDOUT: } { // CHECK:STDOUT: %Form.ref.loc4_28: Core.Form = name_ref Form, %Form.loc4_10.2 [symbolic = %Form.loc4_10.1 (constants.%Form)] // CHECK:STDOUT: %.loc4_28.3: Core.Form = splice_inst %.loc4_28.1 [template = %.loc4_28.2 (constants.%.344)] -// CHECK:STDOUT: %.loc4_24.6: @F.%.loc4_24.3 (%.82f) = splice_inst %.loc4_24.2 [template = %.loc4_24.4 (constants.%.f74)] +// CHECK:STDOUT: %.loc4_24.6: @F.%pattern_type (%pattern_type.ec7) = splice_inst %.loc4_24.2 [template = %.loc4_24.4 (constants.%.b9b)] // CHECK:STDOUT: %.loc4_17: type = splice_block %Form.ref.loc4_17 [concrete = Core.Form] { // CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self] // CHECK:STDOUT: %Core.ref: = name_ref Core, imports.%Core [concrete = imports.%Core] // CHECK:STDOUT: %Form.ref.loc4_17: type = name_ref Form, imports.%Core.Form [concrete = Core.Form] // CHECK:STDOUT: } // CHECK:STDOUT: %Form.loc4_10.2: Core.Form = symbolic_binding Form, 0 [symbolic = %Form.loc4_10.1 (constants.%Form)] -// CHECK:STDOUT: %.loc4_24.1: @F.%.loc4_24.3 (%.82f) = splice_inst %.loc4_24.5 +// CHECK:STDOUT: %.loc4_24.1: @F.%pattern_type (%pattern_type.ec7) = splice_inst %.loc4_24.5 // CHECK:STDOUT: %return: @F.%.loc4_24.3 (%.82f) = return_slot %.loc4_24.1 // CHECK:STDOUT: } // CHECK:STDOUT: } @@ -618,9 +618,9 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: %.loc4_28.2: Core.Form = splice_inst %.loc4_28.1 [template = %.loc4_28.2 (constants.%.344)] // CHECK:STDOUT: %.loc4_24.2: = out_form_param_pattern_action %.loc4_28.3 [template] // CHECK:STDOUT: %.loc4_24.3: type = type_component_of %.loc4_28.2 [template = %.loc4_24.3 (constants.%.82f)] -// CHECK:STDOUT: %.loc4_24.4: @F.%.loc4_24.3 (%.82f) = splice_inst %.loc4_24.2 [template = %.loc4_24.4 (constants.%.f74)] // CHECK:STDOUT: %pattern_type: type = pattern_type %.loc4_24.3 [template = %pattern_type (constants.%pattern_type.ec7)] -// CHECK:STDOUT: %return.patt.loc4_24.2: @F.%pattern_type (%pattern_type.ec7) = return_slot_pattern %.loc4_24.4, %.loc4_24.3 [template = %return.patt.loc4_24.2 (constants.%return.patt.08d)] +// CHECK:STDOUT: %.loc4_24.4: @F.%pattern_type (%pattern_type.ec7) = splice_inst %.loc4_24.2 [template = %.loc4_24.4 (constants.%.b9b)] +// CHECK:STDOUT: %return.patt.loc4_24.2: @F.%pattern_type (%pattern_type.ec7) = return_slot_pattern %.loc4_24.4, %.loc4_24.3 [template = %return.patt.loc4_24.2 (constants.%return.patt.96f)] // CHECK:STDOUT: %.loc4_24.5: = callee_pattern_match_action %.loc4_24.6, call_param0 [template] // CHECK:STDOUT: // CHECK:STDOUT: fn() -> out %.loc4_24.1:? @F.%.loc4_28.1 (@F.%.loc4_28.1); @@ -633,9 +633,9 @@ fn F(Form:! Core.Form) ->? Form; // CHECK:STDOUT: %.loc4_28.2 => constants.%.80a // CHECK:STDOUT: %.loc4_24.2 => constants.%.af3 // CHECK:STDOUT: %.loc4_24.3 => constants.%.465 -// CHECK:STDOUT: %.loc4_24.4 => constants.%.e7f // CHECK:STDOUT: %pattern_type => constants.%pattern_type.a51 -// CHECK:STDOUT: %return.patt.loc4_24.2 => constants.%return.patt.de8 -// CHECK:STDOUT: %.loc4_24.5 => constants.%.cab +// CHECK:STDOUT: %.loc4_24.4 => constants.%.3c2 +// CHECK:STDOUT: %return.patt.loc4_24.2 => constants.%return.patt.de0 +// CHECK:STDOUT: %.loc4_24.5 => constants.%.849 // CHECK:STDOUT: } // CHECK:STDOUT: