@@ -66,17 +66,22 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
6666 ImportAccessLevel problematicImport = D->getImportAccessFrom (DC);
6767 if (problematicImport.has_value ()) {
6868 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+ }
8085 }
8186 }
8287
@@ -100,6 +105,16 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
100105 }
101106
102107 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+ }
103118
104119 // Swift 4.2 did not perform any checks for type aliases.
105120 if (isa<TypeAliasDecl>(D)) {
@@ -119,15 +134,17 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
119134 if (isa<TypeAliasDecl>(DC) && !Context.isSwiftVersionAtLeast (6 ))
120135 downgradeToWarning = DowngradeToWarning::Yes;
121136
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;
125141
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 ());
129145
130- Context.Diags .diagnose (D, diag::resilience_decl_declared_here, D);
146+ Context.Diags .diagnose (D, diag::resilience_decl_declared_here, D, allowedForPkgCtx);
147+ }
131148
132149 if (problematicImport.has_value () &&
133150 problematicImport->accessLevel < D->getFormalAccess ()) {
@@ -156,10 +173,14 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
156173 where.getDeclContext ());
157174 if (problematicImport.has_value ()) {
158175 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+ }
163184 if (ctx.LangOpts .EnableModuleApiImportRemarks ) {
164185 ModuleDecl *importedVia = problematicImport->module .importedModule ,
165186 *sourceModule = D->getModuleContext ();
@@ -186,7 +207,8 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
186207 auto definingModule = D->getModuleContext ();
187208 auto fragileKind = where.getFragileFunctionKind ();
188209 bool warnPreSwift6 = originKind != DisallowedOriginKind::SPIOnly &&
189- originKind != DisallowedOriginKind::NonPublicImport;
210+ originKind != DisallowedOriginKind::PackageImport &&
211+ originKind != DisallowedOriginKind::InternalOrLessImport;
190212 if (fragileKind.kind == FragileFunctionKind::None) {
191213 auto reason = where.getExportabilityReason ();
192214 ctx.Diags
@@ -211,7 +233,8 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
211233 addMissingImport (loc, D, where);
212234
213235 // If limited by an import, note which one.
214- if (originKind == DisallowedOriginKind::NonPublicImport) {
236+ if (originKind == DisallowedOriginKind::InternalOrLessImport ||
237+ originKind == DisallowedOriginKind::PackageImport) {
215238 const DeclContext *DC = where.getDeclContext ();
216239 ImportAccessLevel limitImport = D->getImportAccessFrom (DC);
217240 assert (limitImport.has_value () &&
@@ -242,22 +265,31 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
242265 ImportAccessLevel import = D->getImportAccessFrom (DC);
243266 if (import .has_value () && reason.has_value ()) {
244267 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+ }
248276 }
249277
250278 // Access levels from imports are reported with the others access levels.
251279 // 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+ }
256288
257289 if (ctx.LangOpts .EnableModuleApiImportRemarks &&
258290 import .has_value () && where.isExported () &&
259291 reason != ExportabilityReason::General &&
260- originKind != DisallowedOriginKind::NonPublicImport ) {
292+ originKind != DisallowedOriginKind::InternalOrLessImport ) {
261293 // These may be reported twice, for the Type and for the TypeRepr.
262294 ModuleDecl *importedVia = import ->module .importedModule ,
263295 *sourceModule = D->getModuleContext ();
@@ -270,6 +302,14 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
270302 if (originKind == DisallowedOriginKind::None)
271303 return false ;
272304
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+
273313 auto diagName = D->getName ();
274314 if (auto accessor = dyn_cast<AccessorDecl>(D)) {
275315 // Only diagnose accessors if their disallowed origin kind differs from
@@ -313,7 +353,8 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
313353 }
314354
315355 // If limited by an import, note which one.
316- if (originKind == DisallowedOriginKind::NonPublicImport) {
356+ if (originKind == DisallowedOriginKind::InternalOrLessImport ||
357+ originKind == DisallowedOriginKind::PackageImport) {
317358 assert (import .has_value () &&
318359 import ->accessLevel < AccessLevel::Public &&
319360 " The import should still be non-public" );
@@ -362,10 +403,14 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
362403 ImportAccessLevel problematicImport = ext->getImportAccessFrom (where.getDeclContext ());
363404 if (problematicImport.has_value ()) {
364405 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+ }
369414 if (ctx.LangOpts .EnableModuleApiImportRemarks ) {
370415 ModuleDecl *importedVia = problematicImport->module .importedModule ,
371416 *sourceModule = ext->getModuleContext ();
@@ -392,7 +437,8 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
392437 static_cast <unsigned >(originKind))
393438 .warnUntilSwiftVersionIf ((warnIfConformanceUnavailablePreSwift6 &&
394439 originKind != DisallowedOriginKind::SPIOnly &&
395- originKind != DisallowedOriginKind::NonPublicImport) ||
440+ originKind != DisallowedOriginKind::PackageImport &&
441+ originKind != DisallowedOriginKind::InternalOrLessImport) ||
396442 originKind == DisallowedOriginKind::MissingImport,
397443 6 );
398444
@@ -401,7 +447,8 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
401447 addMissingImport (loc, ext, where);
402448
403449 // If limited by an import, note which one.
404- if (originKind == DisallowedOriginKind::NonPublicImport) {
450+ if (originKind == DisallowedOriginKind::InternalOrLessImport ||
451+ originKind == DisallowedOriginKind::PackageImport) {
405452 const DeclContext *DC = where.getDeclContext ();
406453 ImportAccessLevel limitImport = ext->getImportAccessFrom (DC);
407454 assert (limitImport.has_value () &&
0 commit comments