Skip to content

Commit fbc7690

Browse files
authored
Switch zip to zip_equal where possible (#6389)
There are two uses I'm not converting here, that seem to want the "shortest" behavior. For everything else, I'm going to `zip_equal` since it's more restrictive. I wish `zip` were named `zip_shortest`.
1 parent 205aea9 commit fbc7690

File tree

13 files changed

+34
-31
lines changed

13 files changed

+34
-31
lines changed

common/command_line.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ auto OneOfArgBuilder::OneOfImpl(const OneOfValueT<U> (&input_values)[N],
813813
new (&arg()->value_action) Arg::ValueActionT(
814814
[values, match](const Arg& arg, llvm::StringRef value_string) -> bool {
815815
for (auto [value, arg_value_string] :
816-
llvm::zip(values, arg.value_strings)) {
816+
llvm::zip_equal(values, arg.value_strings)) {
817817
if (value_string == arg_value_string) {
818818
match(value);
819819
return true;

toolchain/check/eval.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1720,9 +1720,9 @@ static auto MakeConstantForBuiltinCall(EvalContext& eval_context,
17201720
CARBON_CHECK(arg_ids.size() == 2);
17211721
auto lhs_facet_type_id = SemIR::FacetTypeId::None;
17221722
auto rhs_facet_type_id = SemIR::FacetTypeId::None;
1723-
for (auto [facet_type_id, type_arg_id] :
1724-
llvm::zip(std::to_array({&lhs_facet_type_id, &rhs_facet_type_id}),
1725-
context.types().GetBlockAsTypeInstIds(arg_ids))) {
1723+
for (auto [facet_type_id, type_arg_id] : llvm::zip_equal(
1724+
std::to_array({&lhs_facet_type_id, &rhs_facet_type_id}),
1725+
context.types().GetBlockAsTypeInstIds(arg_ids))) {
17261726
if (auto facet_type =
17271727
context.insts().TryGetAs<SemIR::FacetType>(type_arg_id)) {
17281728
*facet_type_id = facet_type->facet_type_id;

toolchain/check/handle_struct.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ static auto DiagnoseDuplicateNames(
8282
llvm::ArrayRef<SemIR::StructTypeField> fields, bool is_struct_type_literal)
8383
-> bool {
8484
Map<SemIR::NameId, Parse::NodeId> names;
85-
for (auto [field_name_node, field] : llvm::zip(field_name_nodes, fields)) {
85+
for (auto [field_name_node, field] :
86+
llvm::zip_equal(field_name_nodes, fields)) {
8687
auto result = names.Insert(field.name_id, field_name_node);
8788
if (!result.is_inserted()) {
8889
CARBON_DIAGNOSTIC(StructNameDuplicate, Error,

toolchain/check/impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
137137
// Check we have a value for all non-function associated constants in the
138138
// witness.
139139
for (auto [assoc_entity, witness_value] :
140-
llvm::zip(assoc_entities, witness_block)) {
140+
llvm::zip_equal(assoc_entities, witness_block)) {
141141
auto decl_id = context.constant_values().GetConstantInstId(assoc_entity);
142142
CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
143143
if (auto decl =
@@ -187,7 +187,7 @@ auto FinishImplWitness(Context& context, SemIR::ImplId impl_id) -> void {
187187
llvm::SmallVector<SemIR::InstId> used_decl_ids;
188188

189189
for (auto [assoc_entity, witness_value] :
190-
llvm::zip(assoc_entities, witness_block)) {
190+
llvm::zip_equal(assoc_entities, witness_block)) {
191191
auto decl_id =
192192
context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
193193
context.sem_ir(), impl.interface.specific_id, assoc_entity));

toolchain/check/impl_lookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ class SubstWitnessesCallbacks : public SubstInstCallbacks {
459459
auto lookup_query_interface =
460460
context().specific_interfaces().Get(specific_interface_id);
461461
for (auto [interface, witness_inst_id] :
462-
llvm::zip(interfaces_, witness_inst_ids_)) {
462+
llvm::zip_equal(interfaces_, witness_inst_ids_)) {
463463
// If the `LookupImplWitness` for `.Self` is not looking for the same
464464
// interface as we have a witness for, this is not the right witness to
465465
// use to replace the lookup for `.Self`.

toolchain/check/import_ref.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,7 +2157,7 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
21572157
}
21582158

21592159
for (auto [import_vtable_entry_inst_id, local_vtable_entry_inst_id] :
2160-
llvm::zip(virtual_functions, lazy_virtual_functions)) {
2160+
llvm::zip_equal(virtual_functions, lazy_virtual_functions)) {
21612161
// Use LoadedImportRef for imported symbolic constant vtable entries so they
21622162
// can carry attached constants necessary for applying specifics to these
21632163
// constants when they are used.
@@ -3211,7 +3211,7 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
32113211
llvm::SmallVector<SemIR::StructTypeField> new_fields;
32123212
new_fields.reserve(orig_fields.size());
32133213
for (auto [orig_field, field_type_inst_id] :
3214-
llvm::zip(orig_fields, field_type_inst_ids)) {
3214+
llvm::zip_equal(orig_fields, field_type_inst_ids)) {
32153215
auto name_id = GetLocalNameId(resolver, orig_field.name_id);
32163216
new_fields.push_back(
32173217
{.name_id = name_id, .type_inst_id = field_type_inst_id});
@@ -3909,8 +3909,8 @@ static auto ResolveLocalEvalBlock(ImportRefResolver& resolver,
39093909
// Set the locations of the instructions in the inst block to match those of
39103910
// the imported instructions.
39113911
for (auto [import_inst_id, local_inst_id] :
3912-
llvm::zip(resolver.import_inst_blocks().Get(import_block_id),
3913-
resolver.local_inst_blocks().Get(eval_block_id))) {
3912+
llvm::zip_equal(resolver.import_inst_blocks().Get(import_block_id),
3913+
resolver.local_inst_blocks().Get(eval_block_id))) {
39143914
auto import_ir_inst_id = AddImportIRInst(resolver, import_inst_id);
39153915
resolver.local_insts().SetLocId(local_inst_id, import_ir_inst_id);
39163916
}

toolchain/check/pattern_match.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
527527
auto add_all_subscrutinees =
528528
[&](llvm::ArrayRef<SemIR::InstId> subscrutinee_ids) {
529529
for (auto [subpattern_id, subscrutinee_id] :
530-
llvm::reverse(llvm::zip(subpattern_ids, subscrutinee_ids))) {
530+
llvm::reverse(llvm::zip_equal(subpattern_ids, subscrutinee_ids))) {
531531
AddWork(
532532
{.pattern_id = subpattern_id, .scrutinee_id = subscrutinee_id});
533533
}

toolchain/check/subst.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,10 @@ static auto PopOperand(Context& context, Worklist& worklist,
252252
new_facet_type_info.self_impls_named_constraints.resize(
253253
old_facet_type_info.self_impls_named_constraints.size(),
254254
SemIR::SpecificNamedConstraint::None);
255-
for (auto [old_constraint, new_constraint] : llvm::reverse(
256-
llvm::zip(old_facet_type_info.self_impls_named_constraints,
257-
new_facet_type_info.self_impls_named_constraints))) {
255+
for (auto [old_constraint, new_constraint] :
256+
llvm::reverse(llvm::zip_equal(
257+
old_facet_type_info.self_impls_named_constraints,
258+
new_facet_type_info.self_impls_named_constraints))) {
258259
new_constraint = {
259260
.named_constraint_id = old_constraint.named_constraint_id,
260261
.specific_id = pop_specific(old_constraint.specific_id)};
@@ -263,8 +264,8 @@ static auto PopOperand(Context& context, Worklist& worklist,
263264
old_facet_type_info.extend_named_constraints.size(),
264265
SemIR::SpecificNamedConstraint::None);
265266
for (auto [old_constraint, new_constraint] : llvm::reverse(
266-
llvm::zip(old_facet_type_info.extend_named_constraints,
267-
new_facet_type_info.extend_named_constraints))) {
267+
llvm::zip_equal(old_facet_type_info.extend_named_constraints,
268+
new_facet_type_info.extend_named_constraints))) {
268269
new_constraint = {
269270
.named_constraint_id = old_constraint.named_constraint_id,
270271
.specific_id = pop_specific(old_constraint.specific_id)};
@@ -273,18 +274,18 @@ static auto PopOperand(Context& context, Worklist& worklist,
273274
old_facet_type_info.self_impls_constraints.size(),
274275
SemIR::SpecificInterface::None);
275276
for (auto [old_constraint, new_constraint] : llvm::reverse(
276-
llvm::zip(old_facet_type_info.self_impls_constraints,
277-
new_facet_type_info.self_impls_constraints))) {
277+
llvm::zip_equal(old_facet_type_info.self_impls_constraints,
278+
new_facet_type_info.self_impls_constraints))) {
278279
new_constraint = {
279280
.interface_id = old_constraint.interface_id,
280281
.specific_id = pop_specific(old_constraint.specific_id)};
281282
}
282283
new_facet_type_info.extend_constraints.resize(
283284
old_facet_type_info.extend_constraints.size(),
284285
SemIR::SpecificInterface::None);
285-
for (auto [old_constraint, new_constraint] :
286-
llvm::reverse(llvm::zip(old_facet_type_info.extend_constraints,
287-
new_facet_type_info.extend_constraints))) {
286+
for (auto [old_constraint, new_constraint] : llvm::reverse(
287+
llvm::zip_equal(old_facet_type_info.extend_constraints,
288+
new_facet_type_info.extend_constraints))) {
288289
new_constraint = {
289290
.interface_id = old_constraint.interface_id,
290291
.specific_id = pop_specific(old_constraint.specific_id)};

toolchain/driver/runtimes_cache_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ TEST_F(RuntimesCacheTest, BasicBuild) {
251251
}
252252

253253
for (const auto& [target, built_runtimes_path] :
254-
llvm::zip(targets, built_runtimes_paths)) {
254+
llvm::zip_equal(targets, built_runtimes_paths)) {
255255
SCOPED_TRACE(target);
256256
auto lookup_result = cache_.Lookup({.target = target});
257257
ASSERT_THAT(lookup_result, IsSuccess(_));

toolchain/lower/specific_coalescer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ auto SpecificCoalescer::AreFunctionBodiesEquivalent(
228228
"Number of specific calls expected to be the same.");
229229

230230
for (auto [state1_call, state2_call] :
231-
llvm::zip(state1.calls, state2.calls)) {
231+
llvm::zip_equal(state1.calls, state2.calls)) {
232232
if (state1_call != state2_call) {
233233
if (ContainsPair(state1_call, state2_call, non_equivalent_specifics_)) {
234234
return false;

0 commit comments

Comments
 (0)