Skip to content

Commit f6a0c7d

Browse files
committed
Use separate ConversionKind instead of bool flag
1 parent 84b206b commit f6a0c7d

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

toolchain/check/convert.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ static auto IsValidExprCategoryForConversionTarget(
730730
case ConversionTarget::Value:
731731
return category == SemIR::ExprCategory::Value;
732732
case ConversionTarget::ValueOrRef:
733+
case ConversionTarget::RefParam:
733734
case ConversionTarget::Discarded:
734735
return category == SemIR::ExprCategory::Value ||
735736
category == SemIR::ExprCategory::DurableRef ||
@@ -1396,13 +1397,13 @@ auto Convert(Context& context, SemIR::LocId loc_id, SemIR::InstId expr_id,
13961397
if (lookup_result) {
13971398
has_ref_tag = true;
13981399
if (lookup_result.value() == Context::RefTag::Present &&
1399-
!target.require_ref_tag) {
1400+
target.kind != ConversionTarget::RefParam) {
14001401
CARBON_DIAGNOSTIC(RefTagNoRefParam, Error,
14011402
"`ref` tag is not an argument to a `ref` parameter");
14021403
context.emitter().Emit(expr_id, RefTagNoRefParam);
14031404
}
14041405
} else {
1405-
if (target.require_ref_tag) {
1406+
if (target.kind == ConversionTarget::RefParam) {
14061407
CARBON_DIAGNOSTIC(RefParamNoRefTag, Error,
14071408
"argument to `ref` parameter not marked with `ref`");
14081409
context.emitter().Emit(expr_id, RefParamNoRefTag);
@@ -1597,7 +1598,8 @@ auto Convert(Context& context, SemIR::LocId loc_id, SemIR::InstId expr_id,
15971598
// If a reference expression is an acceptable result, we're done.
15981599
if (target.kind == ConversionTarget::ValueOrRef ||
15991600
target.kind == ConversionTarget::Discarded ||
1600-
target.kind == ConversionTarget::CppThunkRef) {
1601+
target.kind == ConversionTarget::CppThunkRef ||
1602+
target.kind == ConversionTarget::RefParam) {
16011603
break;
16021604
}
16031605

toolchain/check/convert.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ struct ConversionTarget {
2121
ValueOrRef,
2222
// Convert to a durable reference of type `type_id`.
2323
DurableRef,
24+
// Convert to a reference, suitable for binding to a reference parameter.
25+
RefParam,
2426
// Convert to a reference of type `type_id`, for use as the argument to a
2527
// C++ thunk.
2628
CppThunkRef,
@@ -57,8 +59,6 @@ struct ConversionTarget {
5759
// should be false.
5860
bool diagnose = true;
5961

60-
bool require_ref_tag = false;
61-
6262
// Are we converting this value into an initializer for an object?
6363
auto is_initializer() const -> bool {
6464
return kind == Initializer || kind == FullInitializer;

toolchain/check/pattern_match.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,13 @@ auto MatchContext::DoEmitPatternMatch(Context& context,
374374
// We should already have a durable reference (in the case of a
375375
// VarParamPattern, that's handled by the enclosing VarPattern), but we
376376
// may need to adjust its type.
377-
auto scrutinee_ref_id = Convert(
378-
context, SemIR::LocId(entry.scrutinee_id), entry.scrutinee_id,
379-
{.kind = ConversionTarget::ValueOrRef,
380-
.type_id = scrutinee_type_id,
381-
.require_ref_tag =
382-
std::is_same_v<RefParamPatternT, SemIR::RefParamPattern>});
377+
auto conversion_kind =
378+
std::is_same_v<RefParamPatternT, SemIR::RefParamPattern>
379+
? ConversionTarget::RefParam
380+
: ConversionTarget::ValueOrRef;
381+
auto scrutinee_ref_id =
382+
Convert(context, SemIR::LocId(entry.scrutinee_id), entry.scrutinee_id,
383+
{.kind = conversion_kind, .type_id = scrutinee_type_id});
383384

384385
switch (SemIR::GetExprCategory(context.sem_ir(), scrutinee_ref_id)) {
385386
case SemIR::ExprCategory::Error:

0 commit comments

Comments
 (0)