Skip to content

[4.2] [SILGen] Don't assert on subscripts with no index arguments. #16215

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
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
12 changes: 6 additions & 6 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1617,12 +1617,6 @@ class NameAliasType final
return getNumSubstitutions();
}

/// Retrieve the set of substitutions to be applied to the declaration to
/// produce the underlying type.
SubstitutionList getSubstitutionList() const {
return {getTrailingObjects<Substitution>(), getNumSubstitutions()};
}

/// Retrieve the generic signature used for substitutions.
GenericSignature *getGenericSignature() const {
return getNumSubstitutions() > 0
Expand All @@ -1648,6 +1642,12 @@ class NameAliasType final
: Type();
}

/// Retrieve the set of substitutions to be applied to the declaration to
/// produce the underlying type.
SubstitutionList getSubstitutionList() const {
return {getTrailingObjects<Substitution>(), getNumSubstitutions()};
}

/// Retrieve the substitution map applied to the declaration's underlying
/// to produce the described type.
SubstitutionMap getSubstitutionMap() const;
Expand Down
13 changes: 10 additions & 3 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5073,7 +5073,7 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
if (!subscripts.isNull()) {
// If we have a value and index list, create a new rvalue to represent the
// both of them together.
auto inputTupleType = cast<TupleType>(accessType.getInput());
auto inputTupleType = dyn_cast<TupleType>(accessType.getInput());

SmallVector<ArgumentSource, 4> eltSources;

Expand All @@ -5086,18 +5086,25 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
// TODO: we should really take an array of RValues.
if (accessType->getNumParams() != 2) {
auto subscriptsTupleType = cast<TupleType>(subscripts.getType());
assert(inputTupleType->getNumElements()
assert(accessType.getParams().size()
== 1 + subscriptsTupleType->getNumElements());
SmallVector<RValue, 8> eltRVs;
std::move(subscripts).extractElements(eltRVs);
for (auto &elt : eltRVs)
eltSources.emplace_back(loc, std::move(elt));
} else {
assert(inputTupleType && "Must have an input tuple here");
subscripts.rewriteType(inputTupleType.getElementType(1));
eltSources.emplace_back(loc, std::move(subscripts));
}

setValue = ArgumentSource(loc, inputTupleType, eltSources);
if (eltSources.size() == 1) {
setValue = std::move(eltSources.front());
} else {
assert(inputTupleType);
setValue = ArgumentSource(loc, inputTupleType, eltSources);
}

} else {
setValue.rewriteType(accessType.getInput());
}
Expand Down
5 changes: 1 addition & 4 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3631,10 +3631,7 @@ void Serializer::writeType(Type ty) {
addTypeRef(alias->getParent()),
addTypeRef(alias->getSinglyDesugaredType()));
// Write the set of substitutions.
SmallVector<Substitution, 4> flatSubs;
if (auto genericSig = typeAlias->getGenericSignature())
genericSig->getSubstitutions(alias->getSubstitutionMap(), flatSubs);
writeSubstitutions(flatSubs, DeclTypeAbbrCodes);
writeSubstitutions(alias->getSubstitutionList(), DeclTypeAbbrCodes);
break;
}

Expand Down
20 changes: 20 additions & 0 deletions test/SILGen/subscript_accessor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %target-swift-frontend -enable-sil-ownership -O -emit-sil -primary-file %s | %FileCheck %s

// CHECK-LABEL: sil private [transparent] @$S18subscript_accessor1XVxSgycimytfU_
// CHECK: [[SETTER:%.*]] = function_ref @$S18subscript_accessor1XVxSgycis
// CHECK-NEXT: apply [[SETTER]]<T>
struct X<T> {
subscript () -> T? {
get {
return nil
}
set { }
}
}

// CHECK: sil{{.*}}S18subscript_accessor9testXRead1xxAA1XVyxG_tlF
@_specialize(where T == (Int, Int))
func testXRead<T>(x: X<T>) -> T {
return x[]!
}
// CHECK: $S18subscript_accessor1XVxSgycisTf4dn_n