Skip to content

Commit 16b033c

Browse files
authored
Merge pull request #8876 from slavapestov/subclass-existentials-sema-casts
Update Sema cast checks for subclass existentials... and more!
2 parents 0f4ebfd + 191afb0 commit 16b033c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+528
-198
lines changed

include/swift/AST/ConcreteDeclRef.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class ConcreteDeclRef {
113113

114114
/// Retrieve a reference to the declaration this one overrides.
115115
ConcreteDeclRef
116-
getOverriddenDecl(ASTContext &ctx, LazyResolver *resolver) const;
116+
getOverriddenDecl(ASTContext &ctx) const;
117117

118118
/// Determine whether this reference specializes the declaration to which
119119
/// it refers.

include/swift/AST/SubstitutionMap.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ class SubstitutionMap {
111111
static SubstitutionMap
112112
getOverrideSubstitutions(const ValueDecl *baseDecl,
113113
const ValueDecl *derivedDecl,
114-
Optional<SubstitutionMap> derivedSubs,
115-
LazyResolver *resolver);
114+
Optional<SubstitutionMap> derivedSubs);
116115

117116
/// Variant of the above for when we have the generic signatures but not
118117
/// the decls for 'derived' and 'base'.
@@ -121,8 +120,7 @@ class SubstitutionMap {
121120
const ClassDecl *derivedClass,
122121
GenericSignature *baseSig,
123122
GenericSignature *derivedSig,
124-
Optional<SubstitutionMap> derivedSubs,
125-
LazyResolver *resolver);
123+
Optional<SubstitutionMap> derivedSubs);
126124

127125
/// Combine two substitution maps as follows.
128126
///

include/swift/AST/Types.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -656,18 +656,13 @@ class alignas(1 << TypeAlignInBits) TypeBase {
656656

657657
/// \brief Retrieve the superclass of this type.
658658
///
659-
/// \param resolver The resolver for lazy type checking, or null if the
660-
/// AST is already type-checked.
661-
///
662659
/// \returns The superclass of this type, or a null type if it has no
663660
/// superclass.
664-
Type getSuperclass(LazyResolver *resolver);
661+
Type getSuperclass();
665662

666663
/// \brief True if this type is the exact superclass of another type.
667664
///
668665
/// \param ty The potential subclass.
669-
/// \param resolver The resolver for lazy type checking, or null if the
670-
/// AST is already type-checked.
671666
///
672667
/// \returns True if this type is \c ty or a superclass of \c ty.
673668
///
@@ -678,7 +673,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
678673
/// will return false. `isBindableToSuperclassOf` should be used
679674
/// for queries that care whether a generic class type can be substituted into
680675
/// a type's subclass.
681-
bool isExactSuperclassOf(Type ty, LazyResolver *resolver);
676+
bool isExactSuperclassOf(Type ty);
682677

683678
/// \brief Get the substituted base class type, starting from a base class
684679
/// declaration and a substituted derived class type.
@@ -691,23 +686,21 @@ class alignas(1 << TypeAlignInBits) TypeBase {
691686
///
692687
/// Calling `C<String, NSObject>`->getSuperclassForDecl(`A`) will return
693688
/// `A<Int, NSObject>`.
694-
Type getSuperclassForDecl(const ClassDecl *classDecl, LazyResolver *resolver);
689+
Type getSuperclassForDecl(const ClassDecl *classDecl);
695690

696691
/// \brief True if this type is the superclass of another type, or a generic
697692
/// type that could be bound to the superclass.
698693
///
699694
/// \param ty The potential subclass.
700-
/// \param resolver The resolver for lazy type checking, or null if the
701-
/// AST is already type-checked.
702695
///
703696
/// \returns True if this type is \c ty, a superclass of \c ty, or an
704697
/// archetype-parameterized type that can be bound to a superclass
705698
/// of \c ty.
706-
bool isBindableToSuperclassOf(Type ty, LazyResolver *resolver);
699+
bool isBindableToSuperclassOf(Type ty);
707700

708701
/// True if this type contains archetypes that could be substituted with
709702
/// concrete types to form the argument type.
710-
bool isBindableTo(Type ty, LazyResolver *resolver);
703+
bool isBindableTo(Type ty);
711704

712705
/// \brief Determines whether this type is permitted as a method override
713706
/// of the \p other.
@@ -916,8 +909,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
916909
/// 'self' argument type as appropriate.
917910
Type adjustSuperclassMemberDeclType(const ValueDecl *baseDecl,
918911
const ValueDecl *derivedDecl,
919-
Type memberType,
920-
LazyResolver *resolver);
912+
Type memberType);
921913

922914
/// Return T if this type is Optional<T>; otherwise, return the null type.
923915
Type getOptionalObjectType();

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ SILCloner<ImplClass>::visitWitnessMethodInst(WitnessMethodInst *Inst) {
14941494
CanType Ty = conformance.getConcrete()->getType()->getCanonicalType();
14951495

14961496
if (Ty != newLookupType) {
1497-
assert(Ty->isExactSuperclassOf(newLookupType, nullptr) &&
1497+
assert(Ty->isExactSuperclassOf(newLookupType) &&
14981498
"Should only create upcasts for sub class.");
14991499

15001500
// We use the super class as the new look up type.

include/swift/SIL/SILType.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,25 +407,23 @@ class SILType {
407407

408408
/// Return the immediate superclass type of this type, or null if
409409
/// it's the most-derived type.
410-
SILType getSuperclass(LazyResolver *resolver) const {
411-
auto superclass = getSwiftRValueType()->getSuperclass(resolver);
410+
SILType getSuperclass() const {
411+
auto superclass = getSwiftRValueType()->getSuperclass();
412412
if (!superclass) return SILType();
413413
return SILType::getPrimitiveObjectType(superclass->getCanonicalType());
414414
}
415415

416416
/// Return true if Ty is a subtype of this exact SILType, or false otherwise.
417417
bool isExactSuperclassOf(SILType Ty) const {
418-
return getSwiftRValueType()->isExactSuperclassOf(Ty.getSwiftRValueType(),
419-
nullptr);
418+
return getSwiftRValueType()->isExactSuperclassOf(Ty.getSwiftRValueType());
420419
}
421420

422421
/// Return true if Ty is a subtype of this SILType, or if this SILType
423422
/// contains archetypes that can be found to form a supertype of Ty, or false
424423
/// otherwise.
425424
bool isBindableToSuperclassOf(SILType Ty) const {
426425
return getSwiftRValueType()->isBindableToSuperclassOf(
427-
Ty.getSwiftRValueType(),
428-
nullptr);
426+
Ty.getSwiftRValueType());
429427
}
430428

431429
/// Transform the function type SILType by replacing all of its interface

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2760,7 +2760,7 @@ class Verifier : public ASTWalker {
27602760

27612761
// If the destination is a class, walk the supertypes of the source.
27622762
if (destTy->getClassOrBoundGenericClass()) {
2763-
if (!destTy->isBindableToSuperclassOf(srcTy, nullptr)) {
2763+
if (!destTy->isBindableToSuperclassOf(srcTy)) {
27642764
srcTy.print(Out);
27652765
Out << " is not a superclass of ";
27662766
destTy.print(Out);

lib/AST/ConcreteDeclRef.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ ConcreteDeclRef::SpecializedDeclRef::create(
3434
}
3535

3636
ConcreteDeclRef
37-
ConcreteDeclRef::getOverriddenDecl(ASTContext &ctx,
38-
LazyResolver *resolver) const {
37+
ConcreteDeclRef::getOverriddenDecl(ASTContext &ctx) const {
3938
auto *derivedDecl = getDecl();
4039
auto *baseDecl = derivedDecl->getOverriddenDecl();
4140

@@ -50,7 +49,7 @@ ConcreteDeclRef::getOverriddenDecl(ASTContext &ctx,
5049
if (derivedSig)
5150
derivedSubMap = derivedSig->getSubstitutionMap(getSubstitutions());
5251
auto subMap = SubstitutionMap::getOverrideSubstitutions(
53-
baseDecl, derivedDecl, derivedSubMap, resolver);
52+
baseDecl, derivedDecl, derivedSubMap);
5453
baseSig->getSubstitutions(subMap, subs);
5554
}
5655
return ConcreteDeclRef(ctx, baseDecl, subs);

lib/AST/ConformanceLookupTable.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -801,9 +801,9 @@ ProtocolConformance *ConformanceLookupTable::getConformance(
801801

802802
// Find the superclass type that matches where the conformance was
803803
// declared.
804-
Type superclassTy = type->getSuperclass(resolver);
804+
Type superclassTy = type->getSuperclass();
805805
while (superclassTy->getAnyNominal() != conformingNominal)
806-
superclassTy = superclassTy->getSuperclass(resolver);
806+
superclassTy = superclassTy->getSuperclass();
807807

808808
// Look up the inherited conformance.
809809
ModuleDecl *module = entry->getDeclContext()->getParentModule();

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,7 +2603,7 @@ void GenericSignatureBuilder::updateSuperclass(
26032603
//
26042604
// then the second constraint should be allowed, constraining U to Bar
26052605
// and secondarily imposing a T == Int constraint.
2606-
if (existingSuperclass->isExactSuperclassOf(superclass, nullptr)) {
2606+
if (existingSuperclass->isExactSuperclassOf(superclass)) {
26072607
equivClass->superclass = superclass;
26082608

26092609
// We've strengthened the bound, so update superclass conformances.
@@ -4525,7 +4525,7 @@ void GenericSignatureBuilder::checkSuperclassConstraints(
45254525
},
45264526
[&](Type superclass) {
45274527
// If this class is a superclass of the "best"
4528-
if (superclass->isExactSuperclassOf(equivClass->superclass, nullptr))
4528+
if (superclass->isExactSuperclassOf(equivClass->superclass))
45294529
return ConstraintRelation::Redundant;
45304530

45314531
// Otherwise, it conflicts.
@@ -4539,8 +4539,7 @@ void GenericSignatureBuilder::checkSuperclassConstraints(
45394539
// FIXME: Substitute into the concrete type.
45404540
if (equivClass->concreteType) {
45414541
// Make sure the concrete type fulfills the superclass requirement.
4542-
if (!equivClass->superclass->isExactSuperclassOf(equivClass->concreteType,
4543-
nullptr)) {
4542+
if (!equivClass->superclass->isExactSuperclassOf(equivClass->concreteType)) {
45444543
if (auto existing = equivClass->findAnyConcreteConstraintAsWritten(
45454544
representativeConstraint.archetype)) {
45464545
Diags.diagnose(existing->source->getLoc(), diag::type_does_not_inherit,

lib/AST/Module.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,9 @@ ModuleDecl::lookupConformance(Type type, ProtocolDecl *protocol,
681681
= rootConformance->getType()->getClassOrBoundGenericClass();
682682

683683
// Map up to our superclass's type.
684-
Type superclassTy = type->getSuperclass(resolver);
684+
Type superclassTy = type->getSuperclass();
685685
while (superclassTy->getAnyNominal() != conformingNominal)
686-
superclassTy = superclassTy->getSuperclass(resolver);
686+
superclassTy = superclassTy->getSuperclass();
687687

688688
// Compute the conformance for the inherited type.
689689
auto inheritedConformance = lookupConformance(superclassTy, protocol,

lib/AST/SubstitutionMap.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,7 @@ SubstitutionMap::getProtocolSubstitutions(ProtocolDecl *protocol,
237237
SubstitutionMap
238238
SubstitutionMap::getOverrideSubstitutions(const ValueDecl *baseDecl,
239239
const ValueDecl *derivedDecl,
240-
Optional<SubstitutionMap> derivedSubs,
241-
LazyResolver *resolver) {
240+
Optional<SubstitutionMap> derivedSubs) {
242241
auto *baseClass = baseDecl->getDeclContext()
243242
->getAsClassOrClassExtensionContext();
244243
auto *derivedClass = derivedDecl->getDeclContext()
@@ -251,17 +250,15 @@ SubstitutionMap::getOverrideSubstitutions(const ValueDecl *baseDecl,
251250

252251
return getOverrideSubstitutions(baseClass, derivedClass,
253252
baseSig, derivedSig,
254-
derivedSubs,
255-
resolver);
253+
derivedSubs);
256254
}
257255

258256
SubstitutionMap
259257
SubstitutionMap::getOverrideSubstitutions(const ClassDecl *baseClass,
260258
const ClassDecl *derivedClass,
261259
GenericSignature *baseSig,
262260
GenericSignature *derivedSig,
263-
Optional<SubstitutionMap> derivedSubs,
264-
LazyResolver *resolver) {
261+
Optional<SubstitutionMap> derivedSubs) {
265262
if (baseSig == nullptr)
266263
return SubstitutionMap();
267264

@@ -275,7 +272,7 @@ SubstitutionMap::getOverrideSubstitutions(const ClassDecl *baseClass,
275272
auto derivedClassTy = derivedClass->getDeclaredInterfaceType();
276273
if (derivedSubs)
277274
derivedClassTy = derivedClassTy.subst(*derivedSubs);
278-
auto baseClassTy = derivedClassTy->getSuperclassForDecl(baseClass, resolver);
275+
auto baseClassTy = derivedClassTy->getSuperclassForDecl(baseClass);
279276

280277
baseSubMap = baseClassTy->getContextSubstitutionMap(M, baseClass);
281278
}

lib/AST/Type.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@ bool TypeBase::mayHaveSuperclass() {
15651565
return is<DynamicSelfType>();
15661566
}
15671567

1568-
Type TypeBase::getSuperclass(LazyResolver *resolver) {
1568+
Type TypeBase::getSuperclass() {
15691569
auto *nominalDecl = getAnyNominal();
15701570
auto *classDecl = dyn_cast_or_null<ClassDecl>(nominalDecl);
15711571

@@ -1604,7 +1604,7 @@ Type TypeBase::getSuperclass(LazyResolver *resolver) {
16041604
return superclassTy.subst(subMap);
16051605
}
16061606

1607-
bool TypeBase::isExactSuperclassOf(Type ty, LazyResolver *resolver) {
1607+
bool TypeBase::isExactSuperclassOf(Type ty) {
16081608
// For there to be a superclass relationship, we must be a superclass, and
16091609
// the potential subtype must be a class or superclass-bounded archetype.
16101610
if (!getClassOrBoundGenericClass() || !ty->mayHaveSuperclass())
@@ -1615,12 +1615,12 @@ bool TypeBase::isExactSuperclassOf(Type ty, LazyResolver *resolver) {
16151615
return true;
16161616
if (ty->getAnyNominal() && ty->getAnyNominal()->isInvalid())
16171617
return false;
1618-
} while ((ty = ty->getSuperclass(resolver)));
1618+
} while ((ty = ty->getSuperclass()));
16191619
return false;
16201620
}
16211621

16221622
/// Returns true if type `a` has archetypes that can be bound to form `b`.
1623-
bool TypeBase::isBindableTo(Type b, LazyResolver *resolver) {
1623+
bool TypeBase::isBindableTo(Type b) {
16241624
class IsBindableVisitor : public TypeVisitor<IsBindableVisitor, bool, CanType>
16251625
{
16261626
llvm::DenseMap<ArchetypeType *, CanType> Bindings;
@@ -1845,10 +1845,10 @@ bool TypeBase::isBindableTo(Type b, LazyResolver *resolver) {
18451845
b->getCanonicalType());
18461846
}
18471847

1848-
bool TypeBase::isBindableToSuperclassOf(Type ty, LazyResolver *resolver) {
1848+
bool TypeBase::isBindableToSuperclassOf(Type ty) {
18491849
// Do an exact match if no archetypes are involved.
18501850
if (!hasArchetype())
1851-
return isExactSuperclassOf(ty, resolver);
1851+
return isExactSuperclassOf(ty);
18521852

18531853
// For there to be a superclass relationship,
18541854
// the potential subtype must be a class or superclass-bounded archetype.
@@ -1865,11 +1865,11 @@ bool TypeBase::isBindableToSuperclassOf(Type ty, LazyResolver *resolver) {
18651865
return true;
18661866

18671867
do {
1868-
if (isBindableTo(ty, resolver))
1868+
if (isBindableTo(ty))
18691869
return true;
18701870
if (ty->getAnyNominal() && ty->getAnyNominal()->isInvalid())
18711871
return false;
1872-
} while ((ty = ty->getSuperclass(resolver)));
1872+
} while ((ty = ty->getSuperclass()));
18731873
return false;
18741874
}
18751875

@@ -2418,7 +2418,7 @@ static bool canOverride(CanType t1, CanType t2,
24182418
}
24192419

24202420
// Class-to-class.
2421-
return t2->isExactSuperclassOf(t1, resolver);
2421+
return t2->isExactSuperclassOf(t1);
24222422
}
24232423

24242424
bool TypeBase::canOverride(Type other, OverrideMatchMode matchMode,
@@ -3122,8 +3122,7 @@ Type Type::substDependentTypesWithErrorTypes() const {
31223122
SubstFlags::UseErrorType));
31233123
}
31243124

3125-
Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
3126-
LazyResolver *resolver) {
3125+
Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass) {
31273126
Type t(this);
31283127
while (t) {
31293128
// If we have a class-constrained archetype or class-constrained
@@ -3132,7 +3131,7 @@ Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
31323131
if (!nominalDecl) {
31333132
assert(t->is<ArchetypeType>() || t->isExistentialType() &&
31343133
"expected a class, archetype or existential");
3135-
t = t->getSuperclass(resolver);
3134+
t = t->getSuperclass();
31363135
assert(t && "archetype or existential is not class constrained");
31373136
continue;
31383137
}
@@ -3141,7 +3140,7 @@ Type TypeBase::getSuperclassForDecl(const ClassDecl *baseClass,
31413140
if (nominalDecl == baseClass)
31423141
return t;
31433142

3144-
t = t->getSuperclass(resolver);
3143+
t = t->getSuperclass();
31453144
}
31463145
llvm_unreachable("no inheritance relationship between given classes");
31473146
}
@@ -3169,13 +3168,10 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
31693168
return substitutions;
31703169
}
31713170

3172-
// Extract the lazy resolver.
3173-
LazyResolver *resolver = dc->getASTContext().getLazyResolver();
3174-
31753171
// Find the superclass type with the context matching that of the member.
31763172
auto *ownerNominal = dc->getAsNominalTypeOrNominalTypeExtensionContext();
31773173
if (auto *ownerClass = dyn_cast<ClassDecl>(ownerNominal))
3178-
baseTy = baseTy->getSuperclassForDecl(ownerClass, resolver);
3174+
baseTy = baseTy->getSuperclassForDecl(ownerClass);
31793175

31803176
assert(ownerNominal == baseTy->getAnyNominal());
31813177

@@ -3307,10 +3303,9 @@ Type TypeBase::getTypeOfMember(ModuleDecl *module, const ValueDecl *member,
33073303

33083304
Type TypeBase::adjustSuperclassMemberDeclType(const ValueDecl *baseDecl,
33093305
const ValueDecl *derivedDecl,
3310-
Type memberType,
3311-
LazyResolver *resolver) {
3306+
Type memberType) {
33123307
auto subs = SubstitutionMap::getOverrideSubstitutions(
3313-
baseDecl, derivedDecl, /*derivedSubs=*/None, resolver);
3308+
baseDecl, derivedDecl, /*derivedSubs=*/None);
33143309

33153310
if (auto *genericMemberType = memberType->getAs<GenericFunctionType>()) {
33163311
memberType = FunctionType::get(genericMemberType->getInput(),

0 commit comments

Comments
 (0)