Skip to content

Commit ee49d65

Browse files
authored
Remove a use of zip/to_array in eval (#6393)
The to_array was mainly needed for zip_equal, and the GetBlockAsTypeInstIds is forming a vector that should also be size two. But just writing this out should avoid memory allocations. Of course, then I'm like "but maybe a lambda or function would be clearer than a for loop"... So the second commit.
1 parent 7c1077c commit ee49d65

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

toolchain/check/eval.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,26 @@ static auto PerformBuiltinBoolComparison(
16491649
: lhs != rhs);
16501650
}
16511651

1652+
// Converts a call argument to a FacetTypeId.
1653+
static auto ArgToFacetTypeId(Context& context, SemIR::LocId loc_id,
1654+
SemIR::InstId arg_id) -> SemIR::FacetTypeId {
1655+
auto type_arg_id = context.types().GetAsTypeInstId(arg_id);
1656+
if (auto facet_type =
1657+
context.insts().TryGetAs<SemIR::FacetType>(type_arg_id)) {
1658+
return facet_type->facet_type_id;
1659+
}
1660+
CARBON_DIAGNOSTIC(FacetTypeRequiredForTypeAndOperator, Error,
1661+
"non-facet type {0} combined with `&` operator",
1662+
SemIR::TypeId);
1663+
// TODO: Find a location for the lhs or rhs specifically, instead of
1664+
// the whole thing. If that's not possible we can change the text to
1665+
// say if it's referring to the left or the right side for the error.
1666+
// The `arg_id` instruction has no location in it for some reason.
1667+
context.emitter().Emit(loc_id, FacetTypeRequiredForTypeAndOperator,
1668+
context.types().GetTypeIdForTypeInstId(type_arg_id));
1669+
return SemIR::FacetTypeId::None;
1670+
}
1671+
16521672
// Returns a constant for a call to a builtin function.
16531673
static auto MakeConstantForBuiltinCall(EvalContext& eval_context,
16541674
SemIR::LocId loc_id, SemIR::Call call,
@@ -1718,27 +1738,9 @@ static auto MakeConstantForBuiltinCall(EvalContext& eval_context,
17181738

17191739
case SemIR::BuiltinFunctionKind::TypeAnd: {
17201740
CARBON_CHECK(arg_ids.size() == 2);
1721-
auto lhs_facet_type_id = SemIR::FacetTypeId::None;
1722-
auto rhs_facet_type_id = SemIR::FacetTypeId::None;
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))) {
1726-
if (auto facet_type =
1727-
context.insts().TryGetAs<SemIR::FacetType>(type_arg_id)) {
1728-
*facet_type_id = facet_type->facet_type_id;
1729-
} else {
1730-
CARBON_DIAGNOSTIC(FacetTypeRequiredForTypeAndOperator, Error,
1731-
"non-facet type {0} combined with `&` operator",
1732-
SemIR::TypeId);
1733-
// TODO: Find a location for the lhs or rhs specifically, instead of
1734-
// the whole thing. If that's not possible we can change the text to
1735-
// say if it's referring to the left or the right side for the error.
1736-
// The `arg_id` instruction has no location in it for some reason.
1737-
context.emitter().Emit(
1738-
loc_id, FacetTypeRequiredForTypeAndOperator,
1739-
context.types().GetTypeIdForTypeInstId(type_arg_id));
1740-
}
1741-
}
1741+
auto lhs_facet_type_id = ArgToFacetTypeId(context, loc_id, arg_ids[0]);
1742+
auto rhs_facet_type_id = ArgToFacetTypeId(context, loc_id, arg_ids[1]);
1743+
17421744
// Allow errors to be diagnosed for both sides of the operator before
17431745
// returning here if any error occurred on either side.
17441746
if (!lhs_facet_type_id.has_value() || !rhs_facet_type_id.has_value()) {

0 commit comments

Comments
 (0)