From b21d0c421030d2295b2bc1782d43876dd321ce28 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Mon, 23 Jun 2025 15:09:29 -0700 Subject: [PATCH] Fix tuple patterns matching expressions with atomic tuple form. (#5697) Fixes #5696. --- toolchain/check/pattern_match.cpp | 6 +- .../check/testdata/patterns/tuple.carbon | 169 ++++++++++++++++++ 2 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 toolchain/check/testdata/patterns/tuple.carbon diff --git a/toolchain/check/pattern_match.cpp b/toolchain/check/pattern_match.cpp index 136e98e8462a..eb51239c7032 100644 --- a/toolchain/check/pattern_match.cpp +++ b/toolchain/check/pattern_match.cpp @@ -553,11 +553,11 @@ auto MatchContext::DoEmitPatternMatch(Context& context, auto tuple_type_id = ExtractScrutineeType(context.sem_ir(), tuple_pattern.type_id); - auto converted_scrutinee = + auto converted_scrutinee_id = ConvertToValueOrRefOfType(context, SemIR::LocId(pattern_inst_id), entry.scrutinee_id, tuple_type_id); if (auto scrutinee_value = - context.insts().TryGetAs(converted_scrutinee)) { + context.insts().TryGetAs(converted_scrutinee_id)) { add_all_subscrutinees( context.inst_blocks().Get(scrutinee_value->elements_id)); return; @@ -573,7 +573,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context, subscrutinee_ids.push_back( AddInst(context, scrutinee.loc_id, {.type_id = element_type_id, - .tuple_id = entry.scrutinee_id, + .tuple_id = converted_scrutinee_id, .index = SemIR::ElementIndex(i)})); } add_all_subscrutinees(subscrutinee_ids); diff --git a/toolchain/check/testdata/patterns/tuple.carbon b/toolchain/check/testdata/patterns/tuple.carbon new file mode 100644 index 000000000000..5dc41888c1da --- /dev/null +++ b/toolchain/check/testdata/patterns/tuple.carbon @@ -0,0 +1,169 @@ +// 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 +// +// This is mostly checking against crashes for compile time bindings in +// difficult contexts. +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/patterns/tuple.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/patterns/tuple.carbon + +// --- literal.carbon + +library "[[@TEST_NAME]]"; + +//@dump-sem-ir-begin +let (a: (), b: ()) = ((), ()); +//@dump-sem-ir-end + +// --- init_expr.carbon + +library "[[@TEST_NAME]]"; + +fn F() -> ((), ()); + +//@dump-sem-ir-begin +let (a: (), b: ()) = F(); +//@dump-sem-ir-end + +// --- ref_expr.carbon + +library "[[@TEST_NAME]]"; + +var t: ((), ()); +//@dump-sem-ir-begin +let (a: (), b: ()) = t; +//@dump-sem-ir-end + +// CHECK:STDOUT: --- literal.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %pattern_type.cb1: type = pattern_type %empty_tuple.type [concrete] +// CHECK:STDOUT: %tuple.type: type = tuple_type (%empty_tuple.type, %empty_tuple.type) [concrete] +// CHECK:STDOUT: %pattern_type.5b8: type = pattern_type %tuple.type [concrete] +// CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: name_binding_decl { +// CHECK:STDOUT: %a.patt: %pattern_type.cb1 = binding_pattern a [concrete] +// CHECK:STDOUT: %b.patt: %pattern_type.cb1 = binding_pattern b [concrete] +// CHECK:STDOUT: %.loc5_18: %pattern_type.5b8 = tuple_pattern (%a.patt, %b.patt) [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: %.loc5_10.1: type = splice_block %.loc5_10.3 [concrete = constants.%empty_tuple.type] { +// CHECK:STDOUT: %.loc5_10.2: %empty_tuple.type = tuple_literal () +// CHECK:STDOUT: %.loc5_10.3: type = converted %.loc5_10.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type] +// CHECK:STDOUT: } +// CHECK:STDOUT: %empty_tuple.loc5_24: %empty_tuple.type = tuple_value () [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %.loc5_24: %empty_tuple.type = converted @__global_init.%.loc5_24, %empty_tuple.loc5_24 [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %a: %empty_tuple.type = bind_name a, %.loc5_24 +// CHECK:STDOUT: %.loc5_17.1: type = splice_block %.loc5_17.3 [concrete = constants.%empty_tuple.type] { +// CHECK:STDOUT: %.loc5_17.2: %empty_tuple.type = tuple_literal () +// CHECK:STDOUT: %.loc5_17.3: type = converted %.loc5_17.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type] +// CHECK:STDOUT: } +// CHECK:STDOUT: %empty_tuple.loc5_28: %empty_tuple.type = tuple_value () [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %.loc5_28: %empty_tuple.type = converted @__global_init.%.loc5_28, %empty_tuple.loc5_28 [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %b: %empty_tuple.type = bind_name b, %.loc5_28 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @__global_init() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %.loc5_24: %empty_tuple.type = tuple_literal () +// CHECK:STDOUT: %.loc5_28: %empty_tuple.type = tuple_literal () +// CHECK:STDOUT: %.loc5_29: %tuple.type = tuple_literal (%.loc5_24, %.loc5_28) +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- init_expr.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %tuple.type: type = tuple_type (%empty_tuple.type, %empty_tuple.type) [concrete] +// CHECK:STDOUT: %pattern_type.5b8: type = pattern_type %tuple.type [concrete] +// CHECK:STDOUT: %F.type: type = fn_type @F [concrete] +// CHECK:STDOUT: %F: %F.type = struct_value () [concrete] +// CHECK:STDOUT: %pattern_type.cb1: type = pattern_type %empty_tuple.type [concrete] +// CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: name_binding_decl { +// CHECK:STDOUT: %a.patt: %pattern_type.cb1 = binding_pattern a [concrete] +// CHECK:STDOUT: %b.patt: %pattern_type.cb1 = binding_pattern b [concrete] +// CHECK:STDOUT: %.loc7_18: %pattern_type.5b8 = tuple_pattern (%a.patt, %b.patt) [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: %.loc7_24.1: ref %tuple.type = temporary @__global_init.%.loc7, @__global_init.%F.call +// CHECK:STDOUT: %tuple.elem0: ref %empty_tuple.type = tuple_access %.loc7_24.1, element0 +// CHECK:STDOUT: %tuple.elem1: ref %empty_tuple.type = tuple_access %.loc7_24.1, element1 +// CHECK:STDOUT: %.loc7_10.1: type = splice_block %.loc7_10.3 [concrete = constants.%empty_tuple.type] { +// CHECK:STDOUT: %.loc7_10.2: %empty_tuple.type = tuple_literal () +// CHECK:STDOUT: %.loc7_10.3: type = converted %.loc7_10.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type] +// CHECK:STDOUT: } +// CHECK:STDOUT: %tuple.loc7_24.1: %empty_tuple.type = tuple_value () [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %.loc7_24.2: %empty_tuple.type = converted %tuple.elem0, %tuple.loc7_24.1 [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %a: %empty_tuple.type = bind_name a, %.loc7_24.2 +// CHECK:STDOUT: %.loc7_17.1: type = splice_block %.loc7_17.3 [concrete = constants.%empty_tuple.type] { +// CHECK:STDOUT: %.loc7_17.2: %empty_tuple.type = tuple_literal () +// CHECK:STDOUT: %.loc7_17.3: type = converted %.loc7_17.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type] +// CHECK:STDOUT: } +// CHECK:STDOUT: %tuple.loc7_24.2: %empty_tuple.type = tuple_value () [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %.loc7_24.3: %empty_tuple.type = converted %tuple.elem1, %tuple.loc7_24.2 [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %b: %empty_tuple.type = bind_name b, %.loc7_24.3 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @__global_init() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F] +// CHECK:STDOUT: %.loc7: ref %tuple.type = temporary_storage +// CHECK:STDOUT: %F.call: init %tuple.type = call %F.ref() to %.loc7 +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- ref_expr.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %tuple.type: type = tuple_type (%empty_tuple.type, %empty_tuple.type) [concrete] +// CHECK:STDOUT: %pattern_type.5b8: type = pattern_type %tuple.type [concrete] +// CHECK:STDOUT: %pattern_type.cb1: type = pattern_type %empty_tuple.type [concrete] +// CHECK:STDOUT: %tuple.elem0: ref %empty_tuple.type = tuple_access file.%t.var, element0 [concrete] +// CHECK:STDOUT: %tuple.elem1: ref %empty_tuple.type = tuple_access file.%t.var, element1 [concrete] +// CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: name_binding_decl { +// CHECK:STDOUT: %a.patt: %pattern_type.cb1 = binding_pattern a [concrete] +// CHECK:STDOUT: %b.patt: %pattern_type.cb1 = binding_pattern b [concrete] +// CHECK:STDOUT: %.loc6_18: %pattern_type.5b8 = tuple_pattern (%a.patt, %b.patt) [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: %tuple.elem0: ref %empty_tuple.type = tuple_access @__global_init.%t.ref, element0 [concrete = constants.%tuple.elem0] +// CHECK:STDOUT: %tuple.elem1: ref %empty_tuple.type = tuple_access @__global_init.%t.ref, element1 [concrete = constants.%tuple.elem1] +// CHECK:STDOUT: %.loc6_10.1: type = splice_block %.loc6_10.3 [concrete = constants.%empty_tuple.type] { +// CHECK:STDOUT: %.loc6_10.2: %empty_tuple.type = tuple_literal () +// CHECK:STDOUT: %.loc6_10.3: type = converted %.loc6_10.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type] +// CHECK:STDOUT: } +// CHECK:STDOUT: %tuple.loc6_22.1: %empty_tuple.type = tuple_value () [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %.loc6_22.1: %empty_tuple.type = converted %tuple.elem0, %tuple.loc6_22.1 [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %a: %empty_tuple.type = bind_name a, %.loc6_22.1 +// CHECK:STDOUT: %.loc6_17.1: type = splice_block %.loc6_17.3 [concrete = constants.%empty_tuple.type] { +// CHECK:STDOUT: %.loc6_17.2: %empty_tuple.type = tuple_literal () +// CHECK:STDOUT: %.loc6_17.3: type = converted %.loc6_17.2, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type] +// CHECK:STDOUT: } +// CHECK:STDOUT: %tuple.loc6_22.2: %empty_tuple.type = tuple_value () [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %.loc6_22.2: %empty_tuple.type = converted %tuple.elem1, %tuple.loc6_22.2 [concrete = constants.%empty_tuple] +// CHECK:STDOUT: %b: %empty_tuple.type = bind_name b, %.loc6_22.2 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @__global_init() { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %t.ref: ref %tuple.type = name_ref t, file.%t [concrete = file.%t.var] +// CHECK:STDOUT: +// CHECK:STDOUT: } +// CHECK:STDOUT: