Skip to content

Commit 8bc1887

Browse files
committed
[NFC] Override-check decls by property, not kind
Rather than figuring out which kind of decl we’re using and then testing the traits it has, check each trait once on the decls where the AST can express it. This change is NFC currently, but will support static subscripts.
1 parent 022e2d3 commit 8bc1887

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -234,30 +234,21 @@ static bool areOverrideCompatibleSimple(ValueDecl *decl,
234234
if (parentDecl->isInvalid())
235235
return false;
236236

237-
if (auto func = dyn_cast<FuncDecl>(decl)) {
238-
// Specific checking for methods.
239-
auto parentFunc = cast<FuncDecl>(parentDecl);
240-
if (func->isStatic() != parentFunc->isStatic())
241-
return false;
242-
if (func->isGeneric() != parentFunc->isGeneric())
243-
return false;
244-
} else if (auto ctor = dyn_cast<ConstructorDecl>(decl)) {
245-
auto parentCtor = cast<ConstructorDecl>(parentDecl);
246-
if (ctor->isGeneric() != parentCtor->isGeneric())
237+
// If their staticness is different, they aren't compatible.
238+
if (decl->isStatic() != parentDecl->isStatic())
239+
return false;
240+
241+
// If their genericity is different, they aren't compatible.
242+
if (auto genDecl = decl->getAsGenericContext()) {
243+
auto genParentDecl = parentDecl->getAsGenericContext();
244+
if (genDecl->isGeneric() != genParentDecl->isGeneric())
247245
return false;
246+
}
248247

249-
// Factory initializers cannot be overridden.
248+
// Factory initializers cannot be overridden.
249+
if (auto parentCtor = dyn_cast<ConstructorDecl>(parentDecl))
250250
if (parentCtor->isFactoryInit())
251251
return false;
252-
} else if (auto var = dyn_cast<VarDecl>(decl)) {
253-
auto parentVar = cast<VarDecl>(parentDecl);
254-
if (var->isStatic() != parentVar->isStatic())
255-
return false;
256-
} else if (auto subscript = dyn_cast<SubscriptDecl>(decl)) {
257-
auto parentSubscript = cast<SubscriptDecl>(parentDecl);
258-
if (subscript->isGeneric() != parentSubscript->isGeneric())
259-
return false;
260-
}
261252

262253
return true;
263254
}

0 commit comments

Comments
 (0)