Skip to content

Commit 8607ebb

Browse files
authored
Merge pull request #24154 from jrose-apple/5.1-implementation-only-implementation---only
[5.1] Miscellaneous cleanup and consolidation for implementation-only checking
2 parents 1053c0c + 8066cbf commit 8607ebb

16 files changed

+382
-454
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2395,16 +2395,15 @@ NOTE(construct_raw_representable_from_unwrapped_value,none,
23952395
"construct %0 from unwrapped %1 value", (Type, Type))
23962396

23972397
ERROR(decl_from_implementation_only_module,none,
2398-
"cannot use %0 here; %1 has been imported as "
2399-
"'@_implementationOnly'",
2400-
(DeclName, Identifier))
2398+
"cannot use %0 %1 here; %2 has been imported as implementation-only",
2399+
(DescriptiveDeclKind, DeclName, Identifier))
24012400
ERROR(conformance_from_implementation_only_module,none,
24022401
"cannot use conformance of %0 to %1 here; %2 has been imported as "
2403-
"'@_implementationOnly'",
2402+
"implementation-only",
24042403
(Type, DeclName, Identifier))
24052404
ERROR(assoc_conformance_from_implementation_only_module,none,
24062405
"cannot use conformance of %0 to %1 in associated type %3 (inferred as "
2407-
"%4); %2 has been imported as '@_implementationOnly'",
2406+
"%4); %2 has been imported as implementation-only",
24082407
(Type, DeclName, Identifier, Type, Type))
24092408

24102409
// Derived conformances
@@ -4091,9 +4090,9 @@ WARNING(resilience_decl_unavailable_warn,
40914090
(DescriptiveDeclKind, DeclName, AccessLevel, unsigned, bool))
40924091

40934092
ERROR(inlinable_decl_ref_implementation_only,
4094-
none, "%0 %1 cannot be used in an inlinable "
4095-
"function because its module was imported implementation-only",
4096-
(DescriptiveDeclKind, DeclName))
4093+
none, "%0 %1 cannot be used in " FRAGILE_FUNC_KIND "2 "
4094+
"because %3 was imported implementation-only",
4095+
(DescriptiveDeclKind, DeclName, unsigned, Identifier))
40974096

40984097
#undef FRAGILE_FUNC_KIND
40994098

include/swift/AST/AccessScopeChecker.h renamed to include/swift/AST/TypeDeclFinder.h

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,28 @@
1-
//===--- AccessScopeChecker.h - Access calculation helpers -----*- C++ -*-===//
1+
//===--- TypeDeclFinder.h - Finds TypeDecls in Types/TypeReprs --*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12-
//
13-
// This file defines helpers for access-control calculation.
14-
//
15-
//===----------------------------------------------------------------------===//
16-
#ifndef SWIFT_ACCESS_SCOPE_CHECKER_H
17-
#define SWIFT_ACCESS_SCOPE_CHECKER_H
12+
#ifndef SWIFT_AST_TYPEDECLFINDER_H
13+
#define SWIFT_AST_TYPEDECLFINDER_H
1814

19-
#include "swift/AST/AccessScope.h"
20-
#include "swift/AST/AttrKind.h"
2115
#include "swift/AST/ASTWalker.h"
2216
#include "swift/AST/TypeWalker.h"
17+
#include "llvm/ADT/STLExtras.h"
2318

2419
namespace swift {
2520

2621
class BoundGenericType;
2722
class ComponentIdentTypeRepr;
2823
class NominalType;
29-
class SourceFile;
3024
class TypeAliasType;
3125

32-
class AccessScopeChecker {
33-
const SourceFile *File;
34-
bool TreatUsableFromInlineAsPublic;
35-
36-
Optional<AccessScope> Scope = AccessScope::getPublic();
37-
38-
AccessScopeChecker(const DeclContext *useDC,
39-
bool treatUsableFromInlineAsPublic);
40-
bool visitDecl(const ValueDecl *VD);
41-
42-
public:
43-
static Optional<AccessScope>
44-
getAccessScope(TypeRepr *TR, const DeclContext *useDC,
45-
bool treatUsableFromInlineAsPublic = false);
46-
static Optional<AccessScope>
47-
getAccessScope(Type T, const DeclContext *useDC,
48-
bool treatUsableFromInlineAsPublic = false);
49-
};
50-
5126
/// Walks a Type to find all NominalTypes, BoundGenericTypes, and
5227
/// TypeAliasTypes.
5328
class TypeDeclFinder : public TypeWalker {

lib/AST/AccessScopeChecker.cpp

Lines changed: 0 additions & 99 deletions
This file was deleted.

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ else()
3737
endif()
3838

3939
add_swift_host_library(swiftAST STATIC
40-
AccessScopeChecker.cpp
4140
AccessRequests.cpp
4241
ASTContext.cpp
4342
ASTDemangler.cpp
@@ -91,6 +90,7 @@ add_swift_host_library(swiftAST STATIC
9190
SwiftNameTranslation.cpp
9291
Type.cpp
9392
TypeCheckRequests.cpp
93+
TypeDeclFinder.cpp
9494
TypeJoinMeet.cpp
9595
TypeRefinementContext.cpp
9696
TypeRepr.cpp

lib/AST/TypeDeclFinder.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//===--- TypeDeclFinder.cpp - Finds TypeDecls inside Types/TypeReprs ------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/TypeDeclFinder.h"
14+
#include "swift/AST/Decl.h"
15+
#include "swift/AST/TypeRepr.h"
16+
#include "swift/AST/Types.h"
17+
18+
using namespace swift;
19+
20+
TypeWalker::Action TypeDeclFinder::walkToTypePre(Type T) {
21+
if (auto *TAT = dyn_cast<TypeAliasType>(T.getPointer()))
22+
return visitTypeAliasType(TAT);
23+
24+
// FIXME: We're looking through sugar here so that we visit, e.g.,
25+
// Swift.Array when we see `[Int]`. But that means we do redundant work when
26+
// we see sugar that's purely structural, like `(Int)`. Fortunately, paren
27+
// types are the only such purely structural sugar at the time this comment
28+
// was written, and they're not so common in the first place.
29+
if (auto *BGT = T->getAs<BoundGenericType>())
30+
return visitBoundGenericType(BGT);
31+
if (auto *NT = T->getAs<NominalType>())
32+
return visitNominalType(NT);
33+
34+
return Action::Continue;
35+
}
36+
37+
TypeWalker::Action
38+
SimpleTypeDeclFinder::visitNominalType(NominalType *ty) {
39+
return Callback(ty->getDecl());
40+
}
41+
42+
TypeWalker::Action
43+
SimpleTypeDeclFinder::visitBoundGenericType(BoundGenericType *ty) {
44+
return Callback(ty->getDecl());
45+
}
46+
47+
TypeWalker::Action
48+
SimpleTypeDeclFinder::visitTypeAliasType(TypeAliasType *ty) {
49+
return Callback(ty->getDecl());
50+
}
51+
52+
bool TypeReprIdentFinder::walkToTypeReprPost(TypeRepr *TR) {
53+
auto CITR = dyn_cast<ComponentIdentTypeRepr>(TR);
54+
if (!CITR || !CITR->getBoundDecl())
55+
return true;
56+
return Callback(CITR);
57+
}

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
#include "TypeChecker.h"
1818
#include "TypeCheckAvailability.h"
19-
#include "swift/AST/AccessScopeChecker.h"
2019
#include "swift/AST/Attr.h"
2120
#include "swift/AST/Decl.h"
2221
#include "swift/AST/DeclContext.h"
2322
#include "swift/AST/Initializer.h"
2423
#include "swift/AST/ProtocolConformance.h"
24+
#include "swift/AST/TypeDeclFinder.h"
2525

2626
using namespace swift;
2727
using FragileFunctionKind = TypeChecker::FragileFunctionKind;
@@ -119,8 +119,11 @@ bool TypeChecker::diagnoseInlinableDeclRef(SourceLoc loc,
119119
return true;
120120

121121
// Check whether the declaration comes from a publically-imported module.
122-
if (diagnoseDeclRefExportability(loc, declRef, DC))
123-
return true;
122+
// Skip this check for accessors because the associated property or subscript
123+
// will also be checked, and will provide a better error message.
124+
if (!isa<AccessorDecl>(D))
125+
if (diagnoseDeclRefExportability(loc, declRef, DC, Kind))
126+
return true;
124127

125128
return false;
126129
}
@@ -208,15 +211,18 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
208211
}
209212

210213
static bool diagnoseDeclExportability(SourceLoc loc, const ValueDecl *D,
211-
const SourceFile &userSF) {
214+
const SourceFile &userSF,
215+
FragileFunctionKind fragileKind) {
212216
auto definingModule = D->getModuleContext();
213217
if (!userSF.isImportedImplementationOnly(definingModule))
214218
return false;
215219

216220
// TODO: different diagnostics
217221
ASTContext &ctx = definingModule->getASTContext();
218222
ctx.Diags.diagnose(loc, diag::inlinable_decl_ref_implementation_only,
219-
D->getDescriptiveKind(), D->getFullName());
223+
D->getDescriptiveKind(), D->getFullName(),
224+
static_cast<unsigned>(fragileKind),
225+
definingModule->getName());
220226
return true;
221227
}
222228

@@ -288,9 +294,11 @@ void TypeChecker::diagnoseGenericTypeExportability(const TypeLoc &TL,
288294
}));
289295
}
290296

291-
bool TypeChecker::diagnoseDeclRefExportability(SourceLoc loc,
292-
ConcreteDeclRef declRef,
293-
const DeclContext *DC) {
297+
bool
298+
TypeChecker::diagnoseDeclRefExportability(SourceLoc loc,
299+
ConcreteDeclRef declRef,
300+
const DeclContext *DC,
301+
FragileFunctionKind fragileKind) {
294302
// We're only interested in diagnosing uses from source files.
295303
auto userSF = DC->getParentSourceFile();
296304
if (!userSF)
@@ -305,7 +313,7 @@ bool TypeChecker::diagnoseDeclRefExportability(SourceLoc loc,
305313
return false;
306314

307315
const ValueDecl *D = declRef.getDecl();
308-
if (diagnoseDeclExportability(loc, D, *userSF))
316+
if (diagnoseDeclExportability(loc, D, *userSF, fragileKind))
309317
return true;
310318
if (diagnoseGenericArgumentsExportability(loc, declRef.getSubstitutions(),
311319
*userSF)) {

0 commit comments

Comments
 (0)