Skip to content

Commit 5d46a2b

Browse files
committed
Don't claim to handle dependent init kinds for aggregates -- we can't.
Refactor.
1 parent 285f15a commit 5d46a2b

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

toolchain/check/convert.cpp

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@ static auto MakeElementAccessInst(Context& context, SemIR::LocId loc_id,
147147
}
148148
}
149149

150+
// Get the conversion target kind to use when initializing an element of an
151+
// aggregate.
152+
static auto GetAggregateElementConversionTargetKind(SemIR::File& sem_ir,
153+
ConversionTarget target)
154+
-> ConversionTarget::Kind {
155+
// If we're forming an initializer, then we want an initializer for each
156+
// element.
157+
if (target.is_initializer()) {
158+
// Perform a final destination store if we're performing an in-place
159+
// initialization.
160+
auto init_repr = SemIR::InitRepr::ForType(sem_ir, target.type_id);
161+
CARBON_CHECK(init_repr.kind != SemIR::InitRepr::Dependent,
162+
"Aggregate should not have dependent init kind");
163+
if (init_repr.kind == SemIR::InitRepr::InPlace) {
164+
return ConversionTarget::FullInitializer;
165+
}
166+
return ConversionTarget::Initializer;
167+
}
168+
169+
// Otherwise, we want a value representation for each element.
170+
return ConversionTarget::Value;
171+
}
172+
150173
// Converts an element of one aggregate so that it can be used as an element of
151174
// another aggregate.
152175
//
@@ -337,16 +360,8 @@ static auto ConvertTupleToTuple(Context& context, SemIR::TupleType src_type,
337360
return SemIR::ErrorInst::InstId;
338361
}
339362

340-
// If we're forming an initializer, then we want an initializer for each
341-
// element. Otherwise, we want a value representation for each element.
342-
// Perform a final destination store if we're performing an in-place
343-
// initialization.
344-
bool is_init = target.is_initializer();
345363
ConversionTarget::Kind inner_kind =
346-
!is_init ? ConversionTarget::Value
347-
: SemIR::InitRepr::ForType(sem_ir, target.type_id).MightBeInPlace()
348-
? ConversionTarget::FullInitializer
349-
: ConversionTarget::Initializer;
364+
GetAggregateElementConversionTargetKind(sem_ir, target);
350365

351366
// Initialize each element of the destination from the corresponding element
352367
// of the source.
@@ -373,7 +388,7 @@ static auto ConvertTupleToTuple(Context& context, SemIR::TupleType src_type,
373388
new_block.Set(i, init_id);
374389
}
375390

376-
if (is_init) {
391+
if (target.is_initializer()) {
377392
target.init_block->InsertHere();
378393
return AddInst<SemIR::TupleInit>(context, value_loc_id,
379394
{.type_id = target.type_id,
@@ -447,16 +462,8 @@ static auto ConvertStructToStructOrClass(
447462
}
448463
}
449464

450-
// If we're forming an initializer, then we want an initializer for each
451-
// element. Otherwise, we want a value representation for each element.
452-
// Perform a final destination store if we're performing an in-place
453-
// initialization.
454-
bool is_init = target.is_initializer();
455465
ConversionTarget::Kind inner_kind =
456-
!is_init ? ConversionTarget::Value
457-
: SemIR::InitRepr::ForType(sem_ir, target.type_id).MightBeInPlace()
458-
? ConversionTarget::FullInitializer
459-
: ConversionTarget::Initializer;
466+
GetAggregateElementConversionTargetKind(sem_ir, target);
460467

461468
// Initialize each element of the destination from the corresponding element
462469
// of the source.
@@ -544,6 +551,7 @@ static auto ConvertStructToStructOrClass(
544551
new_block.Set(i, init_id);
545552
}
546553

554+
bool is_init = target.is_initializer();
547555
if (ToClass) {
548556
target.init_block->InsertHere();
549557
CARBON_CHECK(is_init,

0 commit comments

Comments
 (0)