Skip to content

Commit fefde3b

Browse files
committed
Sema: Fix crash with property override and almost-valid superclass
It's possible that getSuperclassDecl() returns something but getSuperclass() does not, for example if you reference a generic superclass with missing or invalid generic arguments. We would crash in that case when going down this particular code path. However, the call to getSuperclass() and the entire function it appeared in was actually unnecessary. Fixes #74651 Fixes rdar://problem/130394943
1 parent ca0afe2 commit fefde3b

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -857,19 +857,6 @@ namespace {
857857

858858
return cachedDeclType;
859859
}
860-
861-
/// Adjust the interface of the given declaration, which is found in
862-
/// a supertype of the given type.
863-
Type getSuperMemberDeclType(ValueDecl *baseDecl) const {
864-
auto selfType = decl->getDeclContext()->getSelfInterfaceType();
865-
if (selfType->getClassOrBoundGenericClass()) {
866-
selfType = selfType->getSuperclass();
867-
assert(selfType && "No superclass type?");
868-
}
869-
870-
return selfType->adjustSuperclassMemberDeclType(
871-
baseDecl, decl, baseDecl->getInterfaceType());
872-
}
873860
};
874861
}
875862

@@ -1283,7 +1270,9 @@ bool OverrideMatcher::checkOverride(ValueDecl *baseDecl,
12831270
}
12841271
} else if (auto property = dyn_cast<VarDecl>(decl)) {
12851272
auto propertyTy = property->getInterfaceType();
1286-
auto parentPropertyTy = getSuperMemberDeclType(baseDecl);
1273+
auto selfType = decl->getDeclContext()->getSelfInterfaceType();
1274+
auto parentPropertyTy = selfType->adjustSuperclassMemberDeclType(
1275+
baseDecl, decl, baseDecl->getInterfaceType());
12871276

12881277
CanType parentPropertyCanTy =
12891278
parentPropertyTy->getReducedType(

test/decl/class/override.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ class IUOTestSubclassOkay : IUOTestBaseClass {
236236
override func oneC(_ x: AnyObject) {}
237237
}
238238

239-
class GenericBase<T> {}
239+
class GenericBase<T> { // expected-note{{generic type 'GenericBase' declared here}}
240+
var values: Int { 0 } // expected-note{{attempt to override property here}}
241+
}
242+
240243
class ConcreteDerived: GenericBase<Int> {}
241244

242245
class OverriddenWithConcreteDerived<T> {
@@ -424,3 +427,7 @@ class OverrideTypoSubclass: OverrideTypoBaseClass {
424427
override func foo(_ x: Itn) {} // expected-error {{cannot find type 'Itn' in scope}}
425428
}
426429

430+
// https://github.com/swiftlang/swift/issues/74651
431+
class InvalidSubclass: GenericBase { // expected-error {{reference to generic type 'GenericBase' requires arguments in <...>}}
432+
var values: Float { 0 } // expected-error {{property 'values' with type 'Float' cannot override a property with type 'Int'}}
433+
}

0 commit comments

Comments
 (0)