@@ -66,17 +66,22 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
66
66
ImportAccessLevel problematicImport = D->getImportAccessFrom (DC);
67
67
if (problematicImport.has_value ()) {
68
68
auto SF = DC->getParentSourceFile ();
69
- if (SF)
70
- SF->registerAccessLevelUsingImport (problematicImport.value (),
71
- AccessLevel::Public);
72
-
73
- if (Context.LangOpts .EnableModuleApiImportRemarks ) {
74
- ModuleDecl *importedVia = problematicImport->module .importedModule ,
75
- *sourceModule = D->getModuleContext ();
76
- Context.Diags .diagnose (loc, diag::module_api_import,
77
- D, importedVia, sourceModule,
78
- importedVia == sourceModule,
79
- /* isImplicit*/ false );
69
+ if (SF) {
70
+ // The max used access level previously registered might be Package,
71
+ // in which case, don't reset it to Public here; this ensures proper
72
+ // diags between public and package.
73
+ if (SF->isMaxAccessLevelUsingImportInternal (problematicImport.value ()))
74
+ SF->registerAccessLevelUsingImport (problematicImport.value (),
75
+ AccessLevel::Public);
76
+
77
+ if (Context.LangOpts .EnableModuleApiImportRemarks ) {
78
+ ModuleDecl *importedVia = problematicImport->module .importedModule ,
79
+ *sourceModule = D->getModuleContext ();
80
+ Context.Diags .diagnose (loc, diag::module_api_import,
81
+ D, importedVia, sourceModule,
82
+ importedVia == sourceModule,
83
+ /* isImplicit*/ false );
84
+ }
80
85
}
81
86
}
82
87
@@ -100,6 +105,16 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
100
105
}
101
106
102
107
DowngradeToWarning downgradeToWarning = DowngradeToWarning::No;
108
+ // Don't change the order of the getDisallowedOriginKind call;
109
+ // it can reset downgradeToWarning to NO so needs to be called here.
110
+ auto originKind = getDisallowedOriginKind (D, where, downgradeToWarning);
111
+ // For a default argument or property initializer, package type is
112
+ // allowed at the use site with package access scope.
113
+ auto allowedForPkgCtx = false ;
114
+ if (originKind == DisallowedOriginKind::None ||
115
+ originKind == DisallowedOriginKind::PackageImport) {
116
+ allowedForPkgCtx = where.isPackage () && declAccessScope.isPackage ();
117
+ }
103
118
104
119
// Swift 4.2 did not perform any checks for type aliases.
105
120
if (isa<TypeAliasDecl>(D)) {
@@ -119,15 +134,17 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
119
134
if (isa<TypeAliasDecl>(DC) && !Context.isSwiftVersionAtLeast (6 ))
120
135
downgradeToWarning = DowngradeToWarning::Yes;
121
136
122
- auto diagID = diag::resilience_decl_unavailable;
123
- if (downgradeToWarning == DowngradeToWarning::Yes)
124
- diagID = diag::resilience_decl_unavailable_warn;
137
+ if (!allowedForPkgCtx) {
138
+ auto diagID = diag::resilience_decl_unavailable;
139
+ if (downgradeToWarning == DowngradeToWarning::Yes)
140
+ diagID = diag::resilience_decl_unavailable_warn;
125
141
126
- AccessLevel diagAccessLevel = declAccessScope.accessLevelForDiagnostics ();
127
- Context.Diags .diagnose (loc, diagID, D, diagAccessLevel,
128
- fragileKind.getSelector ());
142
+ AccessLevel diagAccessLevel = declAccessScope.accessLevelForDiagnostics ();
143
+ Context.Diags .diagnose (loc, diagID, D, diagAccessLevel,
144
+ fragileKind.getSelector ());
129
145
130
- Context.Diags .diagnose (D, diag::resilience_decl_declared_here, D);
146
+ Context.Diags .diagnose (D, diag::resilience_decl_declared_here, D, allowedForPkgCtx);
147
+ }
131
148
132
149
if (problematicImport.has_value () &&
133
150
problematicImport->accessLevel < D->getFormalAccess ()) {
@@ -156,10 +173,14 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
156
173
where.getDeclContext ());
157
174
if (problematicImport.has_value ()) {
158
175
auto SF = where.getDeclContext ()->getParentSourceFile ();
159
- if (SF)
160
- SF->registerAccessLevelUsingImport (problematicImport.value (),
161
- AccessLevel::Public);
162
-
176
+ if (SF) {
177
+ // The max used access level previously registered might be Package,
178
+ // in which case, don't reset it to Public here; this ensures proper
179
+ // diags between public and package.
180
+ if (SF->isMaxAccessLevelUsingImportInternal (problematicImport.value ()))
181
+ SF->registerAccessLevelUsingImport (problematicImport.value (),
182
+ AccessLevel::Public);
183
+ }
163
184
if (ctx.LangOpts .EnableModuleApiImportRemarks ) {
164
185
ModuleDecl *importedVia = problematicImport->module .importedModule ,
165
186
*sourceModule = D->getModuleContext ();
@@ -186,7 +207,8 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
186
207
auto definingModule = D->getModuleContext ();
187
208
auto fragileKind = where.getFragileFunctionKind ();
188
209
bool warnPreSwift6 = originKind != DisallowedOriginKind::SPIOnly &&
189
- originKind != DisallowedOriginKind::NonPublicImport;
210
+ originKind != DisallowedOriginKind::PackageImport &&
211
+ originKind != DisallowedOriginKind::InternalOrLessImport;
190
212
if (fragileKind.kind == FragileFunctionKind::None) {
191
213
auto reason = where.getExportabilityReason ();
192
214
ctx.Diags
@@ -211,7 +233,8 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
211
233
addMissingImport (loc, D, where);
212
234
213
235
// If limited by an import, note which one.
214
- if (originKind == DisallowedOriginKind::NonPublicImport) {
236
+ if (originKind == DisallowedOriginKind::InternalOrLessImport ||
237
+ originKind == DisallowedOriginKind::PackageImport) {
215
238
const DeclContext *DC = where.getDeclContext ();
216
239
ImportAccessLevel limitImport = D->getImportAccessFrom (DC);
217
240
assert (limitImport.has_value () &&
@@ -242,22 +265,31 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
242
265
ImportAccessLevel import = D->getImportAccessFrom (DC);
243
266
if (import .has_value () && reason.has_value ()) {
244
267
auto SF = DC->getParentSourceFile ();
245
- if (SF)
246
- SF->registerAccessLevelUsingImport (import .value (),
247
- AccessLevel::Public);
268
+ if (SF) {
269
+ // The max used access level previously registered might be Package,
270
+ // in which case, don't reset it to Public here; this ensures proper
271
+ // diags between public and package.
272
+ if (SF->isMaxAccessLevelUsingImportInternal (import .value ()))
273
+ SF->registerAccessLevelUsingImport (import .value (),
274
+ AccessLevel::Public);
275
+ }
248
276
}
249
277
250
278
// Access levels from imports are reported with the others access levels.
251
279
// Except for extensions, we report them here.
252
- if (originKind == DisallowedOriginKind::NonPublicImport &&
253
- reason != ExportabilityReason::ExtensionWithPublicMembers &&
254
- reason != ExportabilityReason::ExtensionWithConditionalConformances)
255
- return false ;
280
+ if (originKind == DisallowedOriginKind::InternalOrLessImport ||
281
+ originKind == DisallowedOriginKind::PackageImport) {
282
+ if (reason != ExportabilityReason::ExtensionWithPublicMembers &&
283
+ reason != ExportabilityReason::ExtensionWithPackageMembers &&
284
+ reason != ExportabilityReason::ExtensionWithConditionalConformances &&
285
+ reason != ExportabilityReason::ExtensionWithPackageConditionalConformances)
286
+ return false ;
287
+ }
256
288
257
289
if (ctx.LangOpts .EnableModuleApiImportRemarks &&
258
290
import .has_value () && where.isExported () &&
259
291
reason != ExportabilityReason::General &&
260
- originKind != DisallowedOriginKind::NonPublicImport ) {
292
+ originKind != DisallowedOriginKind::InternalOrLessImport ) {
261
293
// These may be reported twice, for the Type and for the TypeRepr.
262
294
ModuleDecl *importedVia = import ->module .importedModule ,
263
295
*sourceModule = D->getModuleContext ();
@@ -270,6 +302,14 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
270
302
if (originKind == DisallowedOriginKind::None)
271
303
return false ;
272
304
305
+ // No diags needed for extensions with package members or
306
+ // conformance to types with package access scope.
307
+ if (originKind == DisallowedOriginKind::PackageImport) {
308
+ if (reason == ExportabilityReason::ExtensionWithPackageMembers ||
309
+ reason == ExportabilityReason::ExtensionWithPackageConditionalConformances)
310
+ return false ;
311
+ }
312
+
273
313
auto diagName = D->getName ();
274
314
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
275
315
// Only diagnose accessors if their disallowed origin kind differs from
@@ -313,7 +353,8 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
313
353
}
314
354
315
355
// If limited by an import, note which one.
316
- if (originKind == DisallowedOriginKind::NonPublicImport) {
356
+ if (originKind == DisallowedOriginKind::InternalOrLessImport ||
357
+ originKind == DisallowedOriginKind::PackageImport) {
317
358
assert (import .has_value () &&
318
359
import ->accessLevel < AccessLevel::Public &&
319
360
" The import should still be non-public" );
@@ -362,10 +403,14 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
362
403
ImportAccessLevel problematicImport = ext->getImportAccessFrom (where.getDeclContext ());
363
404
if (problematicImport.has_value ()) {
364
405
auto SF = where.getDeclContext ()->getParentSourceFile ();
365
- if (SF)
366
- SF->registerAccessLevelUsingImport (problematicImport.value (),
367
- AccessLevel::Public);
368
-
406
+ if (SF) {
407
+ // The max used access level previously registered might be Package,
408
+ // in which case, don't reset it to Public here; this ensures proper
409
+ // diags between public and package.
410
+ if (SF->isMaxAccessLevelUsingImportInternal (problematicImport.value ()))
411
+ SF->registerAccessLevelUsingImport (problematicImport.value (),
412
+ AccessLevel::Public);
413
+ }
369
414
if (ctx.LangOpts .EnableModuleApiImportRemarks ) {
370
415
ModuleDecl *importedVia = problematicImport->module .importedModule ,
371
416
*sourceModule = ext->getModuleContext ();
@@ -392,7 +437,8 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
392
437
static_cast <unsigned >(originKind))
393
438
.warnUntilSwiftVersionIf ((warnIfConformanceUnavailablePreSwift6 &&
394
439
originKind != DisallowedOriginKind::SPIOnly &&
395
- originKind != DisallowedOriginKind::NonPublicImport) ||
440
+ originKind != DisallowedOriginKind::PackageImport &&
441
+ originKind != DisallowedOriginKind::InternalOrLessImport) ||
396
442
originKind == DisallowedOriginKind::MissingImport,
397
443
6 );
398
444
@@ -401,7 +447,8 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
401
447
addMissingImport (loc, ext, where);
402
448
403
449
// If limited by an import, note which one.
404
- if (originKind == DisallowedOriginKind::NonPublicImport) {
450
+ if (originKind == DisallowedOriginKind::InternalOrLessImport ||
451
+ originKind == DisallowedOriginKind::PackageImport) {
405
452
const DeclContext *DC = where.getDeclContext ();
406
453
ImportAccessLevel limitImport = ext->getImportAccessFrom (DC);
407
454
assert (limitImport.has_value () &&
0 commit comments