Skip to content

Commit 9da193e

Browse files
committed
[NFC] Unify helpers for checking for ObjC generics
There were two differently-named and slightly differently-implemented checks on nominal type decls. Clean this up a bit.
1 parent 25a1552 commit 9da193e

10 files changed

+18
-17
lines changed

include/swift/AST/Decl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3979,7 +3979,7 @@ class ClassDecl final : public NominalTypeDecl {
39793979
/// Returns true if the decl uses the Objective-C generics model.
39803980
///
39813981
/// This is true of imported Objective-C classes.
3982-
bool usesObjCGenericsModel() const {
3982+
bool isTypeErasedGenericClass() const {
39833983
return hasClangNode() && isGenericContext() && isObjC();
39843984
}
39853985

lib/AST/Decl.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -2919,7 +2919,8 @@ bool ValueDecl::isObjCDynamicInGenericClass() const {
29192919
if (!classDecl)
29202920
return false;
29212921

2922-
return classDecl->isGenericContext() && !classDecl->usesObjCGenericsModel();
2922+
return classDecl->isGenericContext()
2923+
&& !classDecl->isTypeErasedGenericClass();
29232924
}
29242925

29252926
bool ValueDecl::shouldUseObjCMethodReplacement() const {
@@ -4190,7 +4191,7 @@ bool NominalTypeDecl::isTypeErasedGenericClass() const {
41904191
// ObjC classes are type erased.
41914192
// TODO: Unless they have magic methods...
41924193
if (auto clas = dyn_cast<ClassDecl>(this))
4193-
return clas->hasClangNode() && clas->isGenericContext();
4194+
return clas->isTypeErasedGenericClass();
41944195
return false;
41954196
}
41964197

lib/IRGen/GenProto.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ void PolymorphicConvention::considerParameter(SILParameterInfo param,
418418
// sources of metadata.
419419
CanType objTy = metatypeTy.getInstanceType();
420420
if (auto classDecl = objTy->getClassOrBoundGenericClass())
421-
if (classDecl->usesObjCGenericsModel())
421+
if (classDecl->isTypeErasedGenericClass())
422422
return;
423423

424424
considerNewTypeSource(MetadataSource::Kind::Metadata,

lib/SIL/IR/SILFunctionType.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1766,7 +1766,7 @@ static bool isPseudogeneric(SILDeclRef c) {
17661766
if (!dc) return false;
17671767

17681768
auto classDecl = dc->getSelfClassDecl();
1769-
return (classDecl && classDecl->usesObjCGenericsModel());
1769+
return (classDecl && classDecl->isTypeErasedGenericClass());
17701770
}
17711771

17721772
/// Update the result type given the foreign error convention that we will be

lib/SIL/Utils/DynamicCasts.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -603,12 +603,12 @@ swift::classifyDynamicCast(ModuleDecl *M,
603603
if (targetClass) {
604604
// Imported Objective-C generics don't check the generic parameters, which
605605
// are lost at runtime.
606-
if (sourceClass->usesObjCGenericsModel()) {
606+
if (sourceClass->isTypeErasedGenericClass()) {
607607

608608
if (sourceClass == targetClass)
609609
return DynamicCastFeasibility::WillSucceed;
610610

611-
if (targetClass->usesObjCGenericsModel()) {
611+
if (targetClass->isTypeErasedGenericClass()) {
612612
// If both classes are ObjC generics, the cast may succeed if the
613613
// classes are related, irrespective of their generic parameters.
614614

lib/SIL/Verifier/SILVerifier.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4030,7 +4030,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
40304030
require(instClass,
40314031
"upcast must convert a class metatype to a class metatype");
40324032

4033-
if (instClass->usesObjCGenericsModel()) {
4033+
if (instClass->isTypeErasedGenericClass()) {
40344034
require(instClass->getDeclaredTypeInContext()
40354035
->isBindableToSuperclassOf(opInstTy),
40364036
"upcast must cast to a superclass or an existential metatype");
@@ -4063,7 +4063,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
40634063
auto ToClass = ToTy.getClassOrBoundGenericClass();
40644064
require(ToClass,
40654065
"upcast must convert a class instance to a class type");
4066-
if (ToClass->usesObjCGenericsModel()) {
4066+
if (ToClass->isTypeErasedGenericClass()) {
40674067
require(ToClass->getDeclaredTypeInContext()
40684068
->isBindableToSuperclassOf(FromTy.getASTType()),
40694069
"upcast must cast to a superclass or an existential metatype");

lib/Sema/TypeCheckCaptures.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class FindCapturedVars : public ASTWalker {
108108
// don't need to visit them.
109109
if (ObjC) {
110110
if (auto clas = dyn_cast_or_null<ClassDecl>(ty->getAnyNominal())) {
111-
if (clas->usesObjCGenericsModel()) {
111+
if (clas->isTypeErasedGenericClass()) {
112112
return Action::SkipChildren;
113113
}
114114
}
@@ -530,7 +530,7 @@ class FindCapturedVars : public ASTWalker {
530530
return false;
531531

532532
if (auto clas = dyn_cast_or_null<ClassDecl>(toTy->getAnyNominal())) {
533-
if (clas->usesObjCGenericsModel()) {
533+
if (clas->isTypeErasedGenericClass()) {
534534
return false;
535535
}
536536
}
@@ -658,7 +658,7 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
658658
// their context.
659659
if (AFD && finder.hasGenericParamCaptures()) {
660660
if (auto Clas = AFD->getParent()->getSelfClassDecl()) {
661-
if (Clas->usesObjCGenericsModel()) {
661+
if (Clas->isTypeErasedGenericClass()) {
662662
AFD->diagnose(diag::objc_generic_extension_using_type_parameter);
663663

664664
// If it's possible, suggest adding @objc.

lib/Sema/TypeCheckConstraints.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1857,7 +1857,7 @@ CheckedCastKind TypeChecker::typeCheckCheckedCast(Type fromType,
18571857
// classes. This may be necessary to force-fit ObjC APIs that depend on
18581858
// covariance, or for APIs where the generic parameter annotations in the
18591859
// ObjC headers are inaccurate.
1860-
if (clas && clas->usesObjCGenericsModel()) {
1860+
if (clas && clas->isTypeErasedGenericClass()) {
18611861
if (fromType->getClassOrBoundGenericClass() == clas)
18621862
return CheckedCastKind::ValueCast;
18631863
}

lib/Sema/TypeCheckDeclObjC.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ static bool checkObjCInExtensionContext(const ValueDecl *value,
553553
->getModuleContext()
554554
->isImplicitDynamicEnabled())
555555
return false;
556-
if (!classDecl->usesObjCGenericsModel()) {
556+
if (!classDecl->isTypeErasedGenericClass()) {
557557
softenIfAccessNote(value, reason.getAttr(),
558558
value->diagnose(diag::objc_in_generic_extension,
559559
classDecl->isGeneric())

lib/Sema/TypeCheckProtocol.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1993,7 +1993,7 @@ checkIndividualConformance(NormalProtocolConformance *conformance,
19931993
if (auto ext = dyn_cast<ExtensionDecl>(DC)) {
19941994
if (auto classDecl = ext->getSelfClassDecl()) {
19951995
if (classDecl->isGenericContext()) {
1996-
if (!classDecl->usesObjCGenericsModel()) {
1996+
if (!classDecl->isTypeErasedGenericClass()) {
19971997
C.Diags.diagnose(ComplainLoc,
19981998
diag::objc_protocol_in_generic_extension,
19991999
classDecl->isGeneric(), T, ProtoType);
@@ -2014,7 +2014,7 @@ checkIndividualConformance(NormalProtocolConformance *conformance,
20142014
// types for any obj-c ones.
20152015
while (nestedType) {
20162016
if (auto clas = nestedType->getClassOrBoundGenericClass()) {
2017-
if (clas->usesObjCGenericsModel()) {
2017+
if (clas->isTypeErasedGenericClass()) {
20182018
C.Diags.diagnose(ComplainLoc,
20192019
diag::objc_generics_cannot_conditionally_conform, T,
20202020
ProtoType);
@@ -3014,7 +3014,7 @@ bool ConformanceChecker::checkObjCTypeErasedGenerics(
30143014
auto classDecl = Adoptee->getClassOrBoundGenericClass();
30153015
if (!classDecl) return false;
30163016

3017-
if (!classDecl->usesObjCGenericsModel()) return false;
3017+
if (!classDecl->isTypeErasedGenericClass()) return false;
30183018

30193019
// Concrete types are okay.
30203020
if (!type->getCanonicalType()->hasTypeParameter()) return false;

0 commit comments

Comments
 (0)