Skip to content

Commit 0f27312

Browse files
committed
Sema: Refactor TypeChecker::diagnoseInlinableDeclRef()
1 parent 051f4c4 commit 0f27312

File tree

4 files changed

+23
-42
lines changed

4 files changed

+23
-42
lines changed

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,19 @@
2727

2828
using namespace swift;
2929

30-
bool TypeChecker::diagnoseInlinableDeclRef(SourceLoc loc,
31-
const ValueDecl *D,
32-
ExportContext where) {
33-
auto fragileKind = where.getFragileFunctionKind();
34-
if (fragileKind.kind == FragileFunctionKind::None)
35-
return false;
36-
37-
// Do some important fast-path checks that apply to all cases.
38-
39-
// Type parameters are OK.
40-
if (isa<AbstractTypeParamDecl>(D))
41-
return false;
42-
43-
// Check whether the declaration is accessible.
44-
if (diagnoseInlinableDeclRefAccess(loc, D, where))
45-
return true;
46-
47-
// Check whether the declaration comes from a publically-imported module.
48-
// Skip this check for accessors because the associated property or subscript
49-
// will also be checked, and will provide a better error message.
50-
if (!isa<AccessorDecl>(D))
51-
if (diagnoseDeclRefExportability(loc, D, where))
52-
return true;
53-
54-
return false;
55-
}
56-
5730
bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
5831
const ValueDecl *D,
5932
ExportContext where) {
60-
auto *DC = where.getDeclContext();
6133
auto fragileKind = where.getFragileFunctionKind();
62-
assert(fragileKind.kind != FragileFunctionKind::None);
34+
if (fragileKind.kind == FragileFunctionKind::None)
35+
return false;
6336

6437
// Local declarations are OK.
6538
if (D->getDeclContext()->isLocalContext())
6639
return false;
6740

41+
auto *DC = where.getDeclContext();
42+
6843
// Public declarations or SPI used from SPI are OK.
6944
if (D->getFormalAccessScope(/*useDC=*/nullptr,
7045
fragileKind.allowUsableFromInline).isPublic() &&
@@ -145,6 +120,11 @@ bool
145120
TypeChecker::diagnoseDeclRefExportability(SourceLoc loc,
146121
const ValueDecl *D,
147122
ExportContext where) {
123+
// Accessors cannot have exportability that's different than the storage,
124+
// so skip them for now.
125+
if (isa<AccessorDecl>(D))
126+
return false;
127+
148128
if (!where.mustOnlyReferenceExportedDecls())
149129
return false;
150130

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,6 +2716,10 @@ AvailabilityWalker::diagAvailability(ConcreteDeclRef declRef, SourceRange R,
27162716
return false;
27172717
const ValueDecl *D = declRef.getDecl();
27182718

2719+
// Generic parameters are always available.
2720+
if (isa<GenericTypeParamDecl>(D))
2721+
return false;
2722+
27192723
if (auto *attr = AvailableAttr::isUnavailable(D)) {
27202724
if (diagnoseIncDecRemoval(D, R, attr))
27212725
return true;
@@ -2733,14 +2737,12 @@ AvailabilityWalker::diagAvailability(ConcreteDeclRef declRef, SourceRange R,
27332737
return false;
27342738
}
27352739

2736-
if (Where.getFragileFunctionKind().kind != FragileFunctionKind::None) {
2737-
if (R.isValid())
2738-
if (TypeChecker::diagnoseInlinableDeclRef(R.Start, D, Where))
2739-
return true;
2740-
} else if (Where.getExportabilityReason().hasValue()) {
2741-
if (R.isValid())
2742-
if (TypeChecker::diagnoseDeclRefExportability(R.Start, D, Where))
2743-
return true;
2740+
if (R.isValid()) {
2741+
if (TypeChecker::diagnoseInlinableDeclRefAccess(R.Start, D, Where))
2742+
return true;
2743+
2744+
if (TypeChecker::diagnoseDeclRefExportability(R.Start, D, Where))
2745+
return true;
27442746
}
27452747

27462748
if (R.isValid()) {

lib/Sema/TypeCheckStmt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,8 +1696,8 @@ static bool checkSuperInit(ConstructorDecl *fromCtor,
16961696
}
16971697

16981698
// Make sure we can reference the designated initializer correctly.
1699-
TypeChecker::diagnoseInlinableDeclRef(
1700-
fromCtor->getLoc(), ctor,
1699+
diagnoseDeclAvailability(
1700+
ctor, fromCtor->getLoc(),
17011701
ExportContext::forFunctionBody(fromCtor));
17021702
}
17031703

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,10 @@ DeclName getObjectLiteralConstructorName(ASTContext &ctx,
947947
/// we're parsing the standard library.
948948
ModuleDecl *getStdlibModule(const DeclContext *dc);
949949

950-
/// \name Resilience diagnostics
951-
bool diagnoseInlinableDeclRef(SourceLoc loc, const ValueDecl *D, ExportContext where);
952-
953950
Expr *buildDefaultInitializer(Type type);
954951

952+
/// \name Resilience diagnostics
953+
955954
bool diagnoseInlinableDeclRefAccess(SourceLoc loc, const ValueDecl *D,
956955
ExportContext where);
957956

0 commit comments

Comments
 (0)