Skip to content

Commit 192b523

Browse files
authored
Revert "Fix issue with 'Self' metadata when class conforms to protocol with default implementations" (#12344)
It broke the 32-bit iOS simulator, and possibly the 64-bit simulator as well. Reverts 5618553.
1 parent ed184e9 commit 192b523

File tree

9 files changed

+294
-723
lines changed

9 files changed

+294
-723
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,11 +1452,6 @@ ERROR(witness_requires_dynamic_self,none,
14521452
"method %0 in non-final class %1 must return `Self` to conform to "
14531453
"protocol %2",
14541454
(DeclName, Type, Type))
1455-
ERROR(witness_requires_class_implementation,none,
1456-
"method %0 in non-final class %1 cannot be implemented in a "
1457-
"protocol extension because it returns `Self` and has associated type "
1458-
"requirements",
1459-
(DeclName, Type))
14601455
ERROR(witness_not_accessible_proto,none,
14611456
"%select{initializer %1|method %1|%select{|setter for }2property %1"
14621457
"|subscript%select{| setter}2}0 must be declared "

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 13 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,7 @@ DevirtualizationResult swift::tryDevirtualizeClassMethod(FullApplySite AI,
712712
//===----------------------------------------------------------------------===//
713713

714714
static SubstitutionMap
715-
getSubstitutionsForProtocolConformance(ModuleDecl *M,
716-
ProtocolConformanceRef CRef) {
715+
getSubstitutionsForProtocolConformance(ProtocolConformanceRef CRef) {
717716
auto C = CRef.getConcrete();
718717

719718
// Walk down to the base NormalProtocolConformance.
@@ -747,7 +746,8 @@ getSubstitutionsForProtocolConformance(ModuleDecl *M,
747746

748747
if (Subs.empty()) {
749748
auto *DC = NormalC->getDeclContext();
750-
return NormalC->getType()->getContextSubstitutionMap(M, DC);
749+
return NormalC->getType()
750+
->getContextSubstitutionMap(DC->getParentModule(), DC);
751751
}
752752

753753
return NormalC->getGenericSignature()->getSubstitutionMap(Subs);
@@ -764,50 +764,17 @@ getSubstitutionsForProtocolConformance(ModuleDecl *M,
764764
/// are written in terms of the requirement's generic signature need
765765
/// to be remapped to substitutions suitable for the witness signature.
766766
///
767-
/// Supported remappings are:
768-
///
769-
/// - (Concrete witness thunk) Original substitutions:
770-
/// [Self := ConcreteType, R0 := X0, R1 := X1, ...]
771-
/// - Requirement generic signature:
772-
/// <Self : P, R0, R1, ...>
773-
/// - Witness thunk generic signature:
774-
/// <W0, W1, ...>
775-
/// - Remapped substitutions:
776-
/// [W0 := X0, W1 := X1, ...]
777-
///
778-
/// - (Class witness thunk) Original substitutions:
779-
/// [Self := C<A0, A1>, T0 := X0, T1 := X1, ...]
780-
/// - Requirement generic signature:
781-
/// <Self : P, R0, R1, ...>
782-
/// - Witness thunk generic signature:
783-
/// <Self : C<B0, B1>, B0, B1, W0, W1, ...>
784-
/// - Remapped substitutions:
785-
/// [Self := C<B0, B1>, B0 := A0, B1 := A1, W0 := X0, W1 := X1]
786-
///
787-
/// - (Default witness thunk) Original substitutions:
788-
/// [Self := ConcreteType, R0 := X0, R1 := X1, ...]
789-
/// - Requirement generic signature:
790-
/// <Self : P, R0, R1, ...>
791-
/// - Witness thunk generic signature:
792-
/// <Self : P, W0, W1, ...>
793-
/// - Remapped substitutions:
794-
/// [Self := ConcreteType, W0 := X0, W1 := X1, ...]
795-
///
796767
/// \param conformanceRef The (possibly-specialized) conformance
797768
/// \param requirementSig The generic signature of the requirement
798769
/// \param witnessThunkSig The generic signature of the witness method
799770
/// \param origSubs The substitutions from the call instruction
800-
/// \param isDefaultWitness True if this is a default witness method
801-
/// \param classWitness The ClassDecl if this is a class witness method
802771
static SubstitutionMap
803772
getWitnessMethodSubstitutions(
804-
ModuleDecl *mod,
805773
ProtocolConformanceRef conformanceRef,
806774
GenericSignature *requirementSig,
807775
GenericSignature *witnessThunkSig,
808776
SubstitutionList origSubs,
809-
bool isDefaultWitness,
810-
ClassDecl *classWitness) {
777+
bool isDefaultWitness) {
811778

812779
if (witnessThunkSig == nullptr)
813780
return SubstitutionMap();
@@ -822,7 +789,7 @@ getWitnessMethodSubstitutions(
822789

823790
// If `Self` maps to a bound generic type, this gives us the
824791
// substitutions for the concrete type's generic parameters.
825-
auto baseSubMap = getSubstitutionsForProtocolConformance(mod, conformanceRef);
792+
auto baseSubMap = getSubstitutionsForProtocolConformance(conformanceRef);
826793

827794
unsigned baseDepth = 0;
828795
auto *rootConformance = conformance->getRootNormalConformance();
@@ -831,39 +798,6 @@ getWitnessMethodSubstitutions(
831798

832799
auto origDepth = 1;
833800

834-
// If the witness has a class-constrained 'Self' generic parameter,
835-
// we have to build a new substitution map that shifts all generic
836-
// parameters down by one.
837-
if (classWitness != nullptr) {
838-
baseDepth += 1;
839-
840-
auto &ctx = mod->getASTContext();
841-
auto *proto = conformance->getProtocol();
842-
auto selfType = proto->getSelfInterfaceType();
843-
844-
auto witnessThunkToWitnessMap = witnessThunkSig->getSubstitutionMap(
845-
[&](SubstitutableType *type) -> Type {
846-
if (type->isEqual(selfType))
847-
return classWitness->getSelfInterfaceType();
848-
849-
auto *origParamTy = cast<GenericTypeParamType>(type);
850-
auto *substParamTy = GenericTypeParamType::get(
851-
origParamTy->getDepth() - 1,
852-
origParamTy->getIndex(),
853-
ctx);
854-
855-
return substParamTy;
856-
},
857-
[&](CanType origType, Type replacementType, ProtocolType *protoType)
858-
-> Optional<ProtocolConformanceRef> {
859-
assert(!origType->isEqual(selfType));
860-
861-
return ProtocolConformanceRef(protoType->getDecl());
862-
});
863-
864-
baseSubMap = witnessThunkToWitnessMap.subst(baseSubMap);
865-
}
866-
867801
return SubstitutionMap::combineSubstitutionMaps(
868802
baseSubMap,
869803
origSubMap,
@@ -873,40 +807,24 @@ getWitnessMethodSubstitutions(
873807
witnessThunkSig);
874808
}
875809

876-
static ClassDecl *
877-
getWitnessMethodClass(SILFunctionType *witnessFnTy, ModuleDecl &M) {
878-
auto selfTy = witnessFnTy->getSelfInstanceType();
879-
auto genericSig = witnessFnTy->getGenericSignature();
880-
if (auto paramTy = dyn_cast<GenericTypeParamType>(selfTy)) {
881-
auto superclass = genericSig->getSuperclassBound(paramTy, M);
882-
if (superclass)
883-
return superclass->getClassOrBoundGenericClass();
884-
}
885-
886-
return nullptr;
887-
}
888-
889810
static SubstitutionMap
890811
getWitnessMethodSubstitutions(SILModule &Module, ApplySite AI, SILFunction *F,
891812
ProtocolConformanceRef CRef) {
892-
auto witnessFnTy = F->getLoweredFunctionType();
893-
assert(witnessFnTy->getRepresentation() ==
894-
SILFunctionTypeRepresentation::WitnessMethod);
895-
896813
auto requirementSig = AI.getOrigCalleeType()->getGenericSignature();
897-
auto witnessThunkSig = witnessFnTy->getGenericSignature();
814+
auto witnessThunkSig = F->getLoweredFunctionType()->getGenericSignature();
898815

899816
SubstitutionList origSubs = AI.getSubstitutions();
900817

901-
auto *mod = Module.getSwiftModule();
902818
bool isDefaultWitness =
903-
(witnessFnTy->getDefaultWitnessMethodProtocol(*mod)
904-
== CRef.getRequirement());
905-
auto *classWitness = getWitnessMethodClass(witnessFnTy, *mod);
819+
F->getLoweredFunctionType()->getRepresentation()
820+
== SILFunctionTypeRepresentation::WitnessMethod &&
821+
F->getLoweredFunctionType()->getDefaultWitnessMethodProtocol(
822+
*Module.getSwiftModule())
823+
== CRef.getRequirement();
906824

907825
return getWitnessMethodSubstitutions(
908-
mod, CRef, requirementSig, witnessThunkSig,
909-
origSubs, isDefaultWitness, classWitness);
826+
CRef, requirementSig, witnessThunkSig,
827+
origSubs, isDefaultWitness);
910828
}
911829

912830
/// Generate a new apply of a function_ref to replace an apply of a

0 commit comments

Comments
 (0)