Skip to content

SILGen: Fix RValue::elementsToBeAdded bookkeeping #79612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions lib/SILGen/RValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ using namespace Lowering;
// Helper Routines
//===----------------------------------------------------------------------===//

static unsigned getTupleSize(CanType t) {
if (auto tt = dyn_cast<TupleType>(t))
return tt->getNumElements();
return 1;
}

unsigned RValue::getRValueSize(AbstractionPattern pattern, CanType formalType) {
if (pattern.isTuple()) {
if (pattern.doesTupleContainPackExpansionType())
Expand Down Expand Up @@ -483,7 +477,7 @@ RValue::RValue(SILGenFunction &SGF, Expr *expr, ManagedValue v)
}

RValue::RValue(CanType type)
: type(type), elementsToBeAdded(getTupleSize(type)) {
: type(type), elementsToBeAdded(getRValueSize(type)) {
}

RValue::RValue(AbstractionPattern pattern, CanType type)
Expand All @@ -493,12 +487,14 @@ RValue::RValue(AbstractionPattern pattern, CanType type)
void RValue::addElement(RValue &&element) & {
assert(!element.isUsed() && "adding consumed value to r-value");
assert(!element.isInSpecialState() && "adding special value to r-value");
assert(!isComplete() && "rvalue already complete");
assert(!isInSpecialState() && "cannot add elements to a special r-value");
--elementsToBeAdded;
values.insert(values.end(),
element.values.begin(), element.values.end());
element.makeUsed();
assert(elementsToBeAdded >= element.values.size() && "rvalue too full");
if (!element.values.empty()) {
assert(!isInSpecialState() && "cannot add elements to a special r-value");
elementsToBeAdded -= element.values.size();
values.insert(values.end(),
element.values.begin(), element.values.end());
element.makeUsed();
}

assert(!isComplete() || values.size() == getRValueSize(type));
// Call into the verifier helper directly without an SGF since we know that
Expand Down
10 changes: 10 additions & 0 deletions test/SILGen/variadic-generic-tuples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,13 @@ func testTupleExpansionInEnumConstructor<each T>(
// CHECK-NEXT: [[PAYLOAD_ADDR:%.*]] = init_enum_data_addr [[RESULT_TEMP]] : $*Result<(repeat each T), any Error>, #Result.success
// CHECK-NEXT: copy_addr [[VAR]] to [init] [[PAYLOAD_ADDR]] : $*(repeat each T)
// CHECK-NEXT: inject_enum_addr [[RESULT_TEMP]] : $*Result<(repeat each T), any Error>, #Result.success

// rdar://145478336

func convertVoidPayloads() {
convertPayloads(as: Void.self)
}

func convertPayloads<each Value>(as valueTypes: repeat (each Value).Type) -> (repeat each Value) {
fatalError()
}