-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathResilienceDiagnostics.cpp
421 lines (361 loc) · 17 KB
/
ResilienceDiagnostics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//===--- ResilienceDiagnostics.cpp - Resilience Inlineability Diagnostics -===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements diagnostics for fragile functions, like those with
// @inlinable, @_alwaysEmitIntoClient, or @backDeployed.
//
//===----------------------------------------------------------------------===//
#include "TypeChecker.h"
#include "TypeCheckAvailability.h"
#include "TypeCheckAccess.h"
#include "swift/AST/Attr.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DeclContext.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeDeclFinder.h"
#include "swift/Basic/Assertions.h"
using namespace swift;
static bool addMissingImport(SourceLoc loc, const Decl *D,
const ExportContext &where) {
ASTContext &ctx = where.getDeclContext()->getASTContext();
ModuleDecl *M = D->getModuleContext();
auto *SF = where.getDeclContext()->getParentSourceFile();
// Only add imports of API level modules if this is an API level module.
if (M->getLibraryLevel() != LibraryLevel::API &&
SF->getParentModule()->getLibraryLevel() == LibraryLevel::API)
return false;
// Hack to fix swiftinterfaces in case of missing imports. We can get rid of
// this logic when we don't leak the use of non-locally imported things in
// API.
auto missingImport = ImportedModule(ImportPath::Access(),
const_cast<ModuleDecl *>(M));
SF->addImplicitImportForModuleInterface(missingImport);
ctx.Diags.diagnose(loc, diag::missing_import_inserted, M->getName());
return true;
}
bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
const ValueDecl *D,
const ExportContext &where) {
auto fragileKind = where.getFragileFunctionKind();
if (fragileKind.kind == FragileFunctionKind::None)
return false;
// Local declarations are OK.
if (D->getDeclContext()->isLocalContext())
return false;
auto *DC = where.getDeclContext();
auto &Context = DC->getASTContext();
if (auto *init = dyn_cast<ConstructorDecl>(DC)) {
if (init->isDesignatedInit()) {
auto *storage = dyn_cast<AbstractStorageDecl>(D);
if (storage && storage->hasInitAccessor()) {
if (diagnoseInlinableDeclRefAccess(
loc, storage->getAccessor(AccessorKind::Init), where))
return true;
}
}
}
// Remember that the module defining the decl must be imported publicly.
recordRequiredImportAccessLevelForDecl(
D, DC, AccessLevel::Public,
[&](AttributedImport<ImportedModule> attributedImport) {
ModuleDecl *importedVia = attributedImport.module.importedModule,
*sourceModule = D->getModuleContext();
Context.Diags.diagnose(loc, diag::module_api_import, D, importedVia,
sourceModule, importedVia == sourceModule,
/*isImplicit*/ false);
});
// General check on access-level of the decl.
auto declAccessScope =
D->getFormalAccessScope(/*useDC=*/DC,
/*allowUsableFromInline=*/true);
// Public declarations are OK, even if they're SPI or came from an
// implementation-only import. We'll diagnose exportability violations
// from diagnoseDeclRefExportability().
if (declAccessScope.isPublic())
return false;
// Dynamic declarations were mistakenly not checked in Swift 4.2.
// Do enforce the restriction even in pre-Swift-5 modes if the module we're
// building is resilient, though.
if (D->shouldUseObjCDispatch() && !Context.isSwiftVersionAtLeast(5) &&
!DC->getParentModule()->isResilient()) {
return false;
}
DowngradeToWarning downgradeToWarning = DowngradeToWarning::No;
// Swift 4.2 did not perform any checks for type aliases.
if (isa<TypeAliasDecl>(D)) {
if (!Context.isSwiftVersionAtLeast(4, 2))
return false;
if (!Context.isSwiftVersionAtLeast(5))
downgradeToWarning = DowngradeToWarning::Yes;
}
// Swift 4.2 did not check accessor accessibility.
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
if (!accessor->isInitAccessor() && !Context.isSwiftVersionAtLeast(5))
downgradeToWarning = DowngradeToWarning::Yes;
}
// Swift 5.0 did not check the underlying types of local typealiases.
if (isa<TypeAliasDecl>(DC) && !Context.isSwiftVersionAtLeast(6))
downgradeToWarning = DowngradeToWarning::Yes;
auto diagID = diag::resilience_decl_unavailable;
if (downgradeToWarning == DowngradeToWarning::Yes)
diagID = diag::resilience_decl_unavailable_warn;
AccessLevel diagAccessLevel = declAccessScope.accessLevelForDiagnostics();
Context.Diags.diagnose(loc, diagID, D, diagAccessLevel,
fragileKind.getSelector());
Context.Diags.diagnose(D, diag::resilience_decl_declared_here, D);
ImportAccessLevel problematicImport = D->getImportAccessFrom(DC);
if (problematicImport.has_value() &&
problematicImport->accessLevel < D->getFormalAccess()) {
Context.Diags.diagnose(problematicImport->importLoc,
diag::decl_import_via_here, D,
problematicImport->accessLevel,
problematicImport->module.importedModule);
}
return (downgradeToWarning == DowngradeToWarning::No);
}
static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
const TypeAliasDecl *TAD,
const ExportContext &where) {
assert(where.mustOnlyReferenceExportedDecls());
auto *D = TAD->getUnderlyingType()->getAnyNominal();
if (!D)
return false;
const DeclContext *DC = where.getDeclContext();
auto exportingModule = DC->getParentModule();
ASTContext &ctx = exportingModule->getASTContext();
// Remember that the module defining the underlying type must be imported
// publicly.
recordRequiredImportAccessLevelForDecl(
D, DC, AccessLevel::Public,
[&](AttributedImport<ImportedModule> attributedImport) {
ModuleDecl *importedVia = attributedImport.module.importedModule,
*sourceModule = D->getModuleContext();
ctx.Diags.diagnose(loc, diag::module_api_import_aliases, D, importedVia,
sourceModule, importedVia == sourceModule);
});
auto ignoredDowngradeToWarning = DowngradeToWarning::No;
auto originKind =
getDisallowedOriginKind(D, where, ignoredDowngradeToWarning);
if (originKind == DisallowedOriginKind::None)
return false;
// As an exception, if the import of the module that defines the desugared
// decl is just missing (as opposed to imported explicitly with reduced
// visibility) then we should only diagnose if we're building a resilient
// module.
if (originKind == DisallowedOriginKind::MissingImport &&
!exportingModule->isResilient())
return false;
auto definingModule = D->getModuleContext();
auto fragileKind = where.getFragileFunctionKind();
bool warnPreSwift6 = originKind != DisallowedOriginKind::SPIOnly &&
originKind != DisallowedOriginKind::NonPublicImport;
if (fragileKind.kind == FragileFunctionKind::None) {
auto reason = where.getExportabilityReason();
ctx.Diags
.diagnose(loc, diag::typealias_desugars_to_type_from_hidden_module,
TAD, definingModule->getNameStr(), D->getNameStr(),
static_cast<unsigned>(*reason), definingModule->getName(),
static_cast<unsigned>(originKind))
.warnUntilSwiftVersionIf(warnPreSwift6, 6);
} else {
ctx.Diags
.diagnose(loc,
diag::inlinable_typealias_desugars_to_type_from_hidden_module,
TAD, definingModule->getNameStr(), D->getNameStr(),
fragileKind.getSelector(), definingModule->getName(),
static_cast<unsigned>(originKind))
.warnUntilSwiftVersionIf(warnPreSwift6, 6);
}
D->diagnose(diag::kind_declared_here, DescriptiveDeclKind::Type);
if (originKind == DisallowedOriginKind::MissingImport &&
!ctx.LangOpts.isSwiftVersionAtLeast(6))
addMissingImport(loc, D, where);
// If limited by an import, note which one.
if (originKind == DisallowedOriginKind::NonPublicImport) {
ImportAccessLevel limitImport = D->getImportAccessFrom(DC);
assert(limitImport.has_value() &&
limitImport->accessLevel < AccessLevel::Public &&
"The import should still be non-public");
ctx.Diags.diagnose(limitImport->importLoc,
diag::decl_import_via_here, D,
limitImport->accessLevel,
limitImport->module.importedModule);
}
return true;
}
static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
const ExportContext &where) {
assert(where.mustOnlyReferenceExportedDecls());
auto definingModule = D->getModuleContext();
auto downgradeToWarning = DowngradeToWarning::No;
auto reason = where.getExportabilityReason();
auto DC = where.getDeclContext();
auto SF = DC->getParentSourceFile();
ASTContext &ctx = DC->getASTContext();
auto originKind = getDisallowedOriginKind(D, where, downgradeToWarning);
// Remember that the module defining the decl must be imported publicly.
recordRequiredImportAccessLevelForDecl(
D, DC, AccessLevel::Public,
[&](AttributedImport<ImportedModule> attributedImport) {
if (where.isExported() && reason != ExportabilityReason::General &&
originKind != DisallowedOriginKind::NonPublicImport) {
// These may be reported twice, for the Type and for the TypeRepr.
ModuleDecl *importedVia = attributedImport.module.importedModule,
*sourceModule = D->getModuleContext();
ctx.Diags.diagnose(loc, diag::module_api_import, D, importedVia,
sourceModule, importedVia == sourceModule,
/*isImplicit*/ false);
}
});
// Access levels from imports are reported with the others access levels.
// Except for extensions and protocol conformances, we report them here.
if (originKind == DisallowedOriginKind::NonPublicImport) {
bool reportHere = [&] {
switch (*reason) {
case ExportabilityReason::ExtensionWithPublicMembers:
case ExportabilityReason::ExtensionWithConditionalConformances:
return true;
case ExportabilityReason::Inheritance:
return isa<ProtocolDecl>(D);
default:
return false;
}
}();
if (!reportHere)
return false;
}
if (originKind == DisallowedOriginKind::None)
return false;
// Some diagnostics emitted with the `MemberImportVisibility` feature enabled
// subsume these diagnostics.
if (originKind == DisallowedOriginKind::MissingImport &&
ctx.LangOpts.hasFeature(Feature::MemberImportVisibility) && SF)
return false;
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
// Only diagnose accessors if their disallowed origin kind differs from
// that of their storage.
if (getDisallowedOriginKind(accessor->getStorage(), where) == originKind)
return false;
}
auto fragileKind = where.getFragileFunctionKind();
if (fragileKind.kind == FragileFunctionKind::None) {
DiagnosticBehavior limit = downgradeToWarning == DowngradeToWarning::Yes
? DiagnosticBehavior::Warning
: DiagnosticBehavior::Unspecified;
ctx.Diags.diagnose(loc, diag::decl_from_hidden_module, D,
static_cast<unsigned>(*reason),
definingModule->getName(),
static_cast<unsigned>(originKind))
.limitBehavior(limit);
D->diagnose(diag::kind_declared_here, D->getDescriptiveKind());
} else {
// Only implicitly imported decls should be reported as a warning,
// and only for language versions below Swift 6.
assert(downgradeToWarning == DowngradeToWarning::No ||
originKind == DisallowedOriginKind::MissingImport &&
"Only implicitly imported decls should be reported as a warning.");
ctx.Diags.diagnose(loc, diag::inlinable_decl_ref_from_hidden_module, D,
fragileKind.getSelector(), definingModule->getName(),
static_cast<unsigned>(originKind))
.warnUntilSwiftVersionIf(downgradeToWarning == DowngradeToWarning::Yes,
6);
if (originKind == DisallowedOriginKind::MissingImport &&
downgradeToWarning == DowngradeToWarning::Yes)
addMissingImport(loc, D, where);
}
// If limited by an import, note which one.
ImportAccessLevel import = D->getImportAccessFrom(DC);
if (originKind == DisallowedOriginKind::NonPublicImport) {
assert(import.has_value() &&
import->accessLevel < AccessLevel::Public &&
"The import should still be non-public");
ctx.Diags.diagnose(import->importLoc,
diag::decl_import_via_here, D,
import->accessLevel,
import->module.importedModule);
}
return true;
}
bool TypeChecker::diagnoseDeclRefExportability(SourceLoc loc,
const ValueDecl *D,
const ExportContext &where) {
if (!where.mustOnlyReferenceExportedDecls())
return false;
if (diagnoseValueDeclRefExportability(loc, D, where))
return true;
if (auto *TAD = dyn_cast<TypeAliasDecl>(D))
if (diagnoseTypeAliasDeclRefExportability(loc, TAD, where))
return true;
return false;
}
bool
TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
const RootProtocolConformance *rootConf,
const ExtensionDecl *ext,
const ExportContext &where,
bool warnIfConformanceUnavailablePreSwift6) {
if (!where.mustOnlyReferenceExportedDecls())
return false;
// Skip the special Sendable and Copyable conformances we synthesized in
// ASTContext::getBuiltinTupleDecl().
if (ext->getParentModule()->isBuiltinModule())
return false;
const DeclContext *DC = where.getDeclContext();
ModuleDecl *M = ext->getParentModule();
ASTContext &ctx = M->getASTContext();
// Remember that the module defining the conformance must be imported
// publicly.
recordRequiredImportAccessLevelForDecl(
ext, DC, AccessLevel::Public,
[&](AttributedImport<ImportedModule> attributedImport) {
ModuleDecl *importedVia = attributedImport.module.importedModule,
*sourceModule = ext->getModuleContext();
ctx.Diags.diagnose(loc, diag::module_api_import_conformance,
rootConf->getType(), rootConf->getProtocol(),
importedVia, sourceModule,
importedVia == sourceModule);
});
auto originKind = getDisallowedOriginKind(ext, where);
if (originKind == DisallowedOriginKind::None)
return false;
auto reason = where.getExportabilityReason();
if (!reason.has_value())
reason = ExportabilityReason::General;
ctx.Diags.diagnose(loc, diag::conformance_from_implementation_only_module,
rootConf->getType(),
rootConf->getProtocol()->getName(),
static_cast<unsigned>(*reason),
M->getName(),
static_cast<unsigned>(originKind))
.warnUntilSwiftVersionIf((warnIfConformanceUnavailablePreSwift6 &&
originKind != DisallowedOriginKind::SPIOnly &&
originKind != DisallowedOriginKind::NonPublicImport) ||
originKind == DisallowedOriginKind::MissingImport,
6);
if (originKind == DisallowedOriginKind::MissingImport &&
!ctx.LangOpts.isSwiftVersionAtLeast(6))
addMissingImport(loc, ext, where);
// If limited by an import, note which one.
if (originKind == DisallowedOriginKind::NonPublicImport) {
ImportAccessLevel limitImport = ext->getImportAccessFrom(DC);
assert(limitImport.has_value() &&
limitImport->accessLevel < AccessLevel::Public &&
"The import should still be non-public");
ctx.Diags.diagnose(limitImport->importLoc,
diag::decl_import_via_here, ext,
limitImport->accessLevel,
limitImport->module.importedModule);
}
return true;
}