Skip to content
Closed
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
51 changes: 50 additions & 1 deletion lib/SIL/Utils/GenericSpecializationMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "swift/AST/ASTContext.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SubstitutionMap.h"
#include "swift/Basic/Assertions.h"
#include "swift/Demangling/ManglingMacros.h"
Expand Down Expand Up @@ -98,7 +99,55 @@ appendSubstitutions(GenericSignature sig, SubstitutionMap subs) {
auto ty = Type(ParamType);
auto substTy = ty.subst(subs);
auto canTy = substTy->getCanonicalType();
appendType(canTy, nullptr);

GenericSignature signature;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slavapestov Is there a better way to obtain this† signature?

† Or the one this ought to be.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what this function is doing, but calling GenericSignature::get() directly with a bunch of random requirements is unlikely to be meaningful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the given sig is the generic signature of the callee. We also need to pass down the generic signature of the caller.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't figure out a nice way to plumb this down. GenericSpecializationMangler is a tangled mess. This should work though: #84765

if (canTy->hasTypeParameter()) {
if (isa<GenericTypeParamType>(canTy) ||
isa<DependentMemberType>(canTy)) {
signature = sig;
} else {
SmallVector<Requirement, 2> requirements;
llvm::SmallSetVector<GenericTypeParamType *, 2> params;
auto mergeComponents = [&](GenericSignature signature) {
for (auto *param : signature.getGenericParams()) {
params.insert(param);
}
for (auto req : signature.getRequirements()) {
requirements.push_back(req);
}
};
for (auto conformance : subs.getConformances()) {
if (conformance.getType()->getCanonicalType() == canTy) {
if (!conformance.isConcrete()) {
continue;
}
auto concrete = conformance.getConcrete();
if (auto inherited =
dyn_cast<InheritedProtocolConformance>(concrete)) {
mergeComponents(
inherited->getSubstitutionMap().getGenericSignature());
} else if (auto specialized =
dyn_cast<SpecializedProtocolConformance>(
concrete)) {
mergeComponents(
specialized->getSubstitutionMap().getGenericSignature());
} else if (auto builtin =
dyn_cast<BuiltinProtocolConformance>(concrete)) {
mergeComponents(builtin->getGenericSignature());
} else {
mergeComponents(concrete->getGenericSignature());
}
}
}
if (!params.empty()) {
signature =
GenericSignature::get(params.getArrayRef(), requirements);
}
}
}
// TODO: Enable this assertion.
// assert(!canTy->hasTypeParameter() || signature);
appendType(canTy, signature);
appendListSeparator(First);
}
});
Expand Down
34 changes: 34 additions & 0 deletions validation-test/SILOptimizer/rdar161968922.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %target-swift-frontend -emit-sil -debug-diagnostic-names -verify %s

public protocol Fooable {
func foo() -> Int
}

extension Int : Fooable {
public func foo() -> Int {
self
}
}

extension Array: Comparable where Element: Comparable { // expected-warning{{extension_retroactive_conformance}}
// expected-note@-1{{extension_retroactive_conformance_silence}}
public static func < (lhs: Self, rhs: Self) -> Bool {
true
}
}

extension Array : Fooable where Element : Fooable {
public func foo() -> Int {
0
}
}

struct Wrapper<Symbol: Hashable & Comparable & Fooable>: Hashable {
var partitions: PartitionSet<Array<Symbol>>
}

struct PartitionSet<Symbol: Hashable & Comparable & Fooable>: Equatable, Hashable {
var partitions: Set<Set<Symbol>>
}