Skip to content

Commit f4b530f

Browse files
[NFC] Abstracting isStdlibType and isStdLibDecl logic into Type and Decl
1 parent 2dfd569 commit f4b530f

File tree

7 files changed

+25
-28
lines changed

7 files changed

+25
-28
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,9 @@ class alignas(1 << DeclAlignInBits) Decl {
882882
bool hasUnderscoredNaming() const;
883883

884884
bool isPrivateStdlibDecl(bool treatNonBuiltinProtocolsAsPublic = true) const;
885+
886+
/// Check if this is a declaration defined at the top level of the Swift module
887+
bool isStdlibDecl() const;
885888

886889
AvailabilityContext getAvailabilityForLinkage() const;
887890

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {
780780

781781
/// Check if this type is equal to Builtin.IntN.
782782
bool isBuiltinIntegerType(unsigned bitWidth);
783+
784+
/// Check if this is a nominal type defined at the top level of the Swift module
785+
bool isStdlibType();
783786

784787
/// If this is a class type or a bound generic class type, returns the
785788
/// (possibly generic) class.

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -753,11 +753,6 @@ static const char *getMetatypeRepresentationOp(MetatypeRepresentation Rep) {
753753
llvm_unreachable("Unhandled MetatypeRepresentation in switch.");
754754
}
755755

756-
static bool isStdlibType(const TypeDecl *decl) {
757-
DeclContext *dc = decl->getDeclContext();
758-
return dc->isModuleScopeContext() && dc->getParentModule()->isStdlibModule();
759-
}
760-
761756
/// Whether to mangle the given type as generic.
762757
static bool shouldMangleAsGeneric(Type type) {
763758
if (!type)
@@ -986,7 +981,7 @@ void ASTMangler::appendType(Type type, const ValueDecl *forDecl) {
986981
if (tryMangleTypeSubstitution(tybase))
987982
return;
988983

989-
if (isStdlibType(Decl) && Decl->getName().str() == "Optional") {
984+
if (Decl->isStdlibDecl() && Decl->getName().str() == "Optional") {
990985
auto GenArgs = type->castTo<BoundGenericType>()->getGenericArgs();
991986
assert(GenArgs.size() == 1);
992987
appendType(GenArgs[0], forDecl);
@@ -2483,7 +2478,7 @@ void ASTMangler::appendDeclType(const ValueDecl *decl, bool isFunctionMangling)
24832478

24842479
bool ASTMangler::tryAppendStandardSubstitution(const GenericTypeDecl *decl) {
24852480
// Bail out if our parent isn't the swift standard library.
2486-
if (!isStdlibType(decl))
2481+
if (!decl->isStdlibDecl())
24872482
return false;
24882483

24892484
if (isa<NominalTypeDecl>(decl)) {

lib/AST/Decl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,12 @@ bool Decl::isPrivateStdlibDecl(bool treatNonBuiltinProtocolsAsPublic) const {
788788
return hasUnderscoredNaming();
789789
}
790790

791+
bool Decl::isStdlibDecl() const {
792+
DeclContext *DC = getDeclContext();
793+
return DC->isModuleScopeContext() &&
794+
DC->getParentModule()->isStdlibModule();
795+
}
796+
791797
AvailabilityContext Decl::getAvailabilityForLinkage() const {
792798
auto containingContext =
793799
AvailabilityInference::annotatedAvailableRange(this, getASTContext());

lib/AST/Type.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,15 @@ bool TypeBase::isExistentialWithError() {
751751
return layout.isExistentialWithError(getASTContext());
752752
}
753753

754+
bool TypeBase::isStdlibType() {
755+
if (auto *NTD = getAnyNominal()) {
756+
auto *DC = NTD->getDeclContext();
757+
return DC->isModuleScopeContext() &&
758+
DC->getParentModule()->isStdlibModule();
759+
}
760+
return false;
761+
}
762+
754763
/// Remove argument labels from the function type.
755764
Type TypeBase::removeArgumentLabels(unsigned numArgumentLabels) {
756765
// If there is nothing to remove, don't.

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -515,18 +515,8 @@ bool MissingConformanceFailure::diagnoseAsAmbiguousOperatorRef() {
515515
if (!ODRE)
516516
return false;
517517

518-
auto isStdlibType = [](Type type) {
519-
if (auto *NTD = type->getAnyNominal()) {
520-
auto *DC = NTD->getDeclContext();
521-
return DC->isModuleScopeContext() &&
522-
DC->getParentModule()->isStdlibModule();
523-
}
524-
525-
return false;
526-
};
527-
528518
auto name = ODRE->getDecls().front()->getBaseName();
529-
if (!(name.isOperator() && isStdlibType(getLHS()) && isStdlibType(getRHS())))
519+
if (!(name.isOperator() && getLHS()->isStdlibType() && getRHS()->isStdlibType()))
530520
return false;
531521

532522
// If this is an operator reference and both types are from stdlib,

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,16 +1330,7 @@ assessRequirementFailureImpact(ConstraintSystem &cs, Type requirementType,
13301330
if (!anchor)
13311331
return 1;
13321332

1333-
auto isStdlibType = [&](Type type) {
1334-
if (auto *NTD = type->getAnyNominal()) {
1335-
auto *DC = NTD->getDeclContext();
1336-
return DC->isModuleScopeContext() &&
1337-
DC->getParentModule()->isStdlibModule();
1338-
}
1339-
return false;
1340-
};
1341-
1342-
if (requirementType && isStdlibType(cs.simplifyType(requirementType))) {
1333+
if (requirementType && cs.simplifyType(requirementType)->isStdlibType()) {
13431334
if (auto last = locator.last()) {
13441335
if (auto requirement = last->getAs<LocatorPathElt::AnyRequirement>()) {
13451336
auto kind = requirement->getRequirementKind();

0 commit comments

Comments
 (0)