@@ -75,22 +75,16 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
7575 }
7676 }
7777
78- ImportAccessLevel problematicImport = D->getImportAccessFrom (DC);
79- if (problematicImport.has_value ()) {
80- auto SF = DC->getParentSourceFile ();
81- if (SF)
82- SF->registerAccessLevelUsingImport (problematicImport.value (),
83- AccessLevel::Public);
84-
85- if (Context.LangOpts .EnableModuleApiImportRemarks ) {
86- ModuleDecl *importedVia = problematicImport->module .importedModule ,
87- *sourceModule = D->getModuleContext ();
88- Context.Diags .diagnose (loc, diag::module_api_import,
89- D, importedVia, sourceModule,
90- importedVia == sourceModule,
91- /* isImplicit*/ false );
92- }
93- }
78+ // Remember that the module defining the decl must be imported publicly.
79+ recordRequiredImportAccessLevelForDecl (
80+ D, DC, AccessLevel::Public,
81+ [&](AttributedImport<ImportedModule> attributedImport) {
82+ ModuleDecl *importedVia = attributedImport.module .importedModule ,
83+ *sourceModule = D->getModuleContext ();
84+ Context.Diags .diagnose (loc, diag::module_api_import, D, importedVia,
85+ sourceModule, importedVia == sourceModule,
86+ /* isImplicit*/ false );
87+ });
9488
9589 // General check on access-level of the decl.
9690 auto declAccessScope =
@@ -141,6 +135,7 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
141135
142136 Context.Diags .diagnose (D, diag::resilience_decl_declared_here, D);
143137
138+ ImportAccessLevel problematicImport = D->getImportAccessFrom (DC);
144139 if (problematicImport.has_value () &&
145140 problematicImport->accessLevel < D->getFormalAccess ()) {
146141 Context.Diags .diagnose (problematicImport->importLoc ,
@@ -161,25 +156,20 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
161156 if (!D)
162157 return false ;
163158
164- auto exportingModule = where.getDeclContext ()->getParentModule ();
159+ const DeclContext *DC = where.getDeclContext ();
160+ auto exportingModule = DC->getParentModule ();
165161 ASTContext &ctx = exportingModule->getASTContext ();
166162
167- ImportAccessLevel problematicImport = D->getImportAccessFrom (
168- where.getDeclContext ());
169- if (problematicImport.has_value ()) {
170- auto SF = where.getDeclContext ()->getParentSourceFile ();
171- if (SF)
172- SF->registerAccessLevelUsingImport (problematicImport.value (),
173- AccessLevel::Public);
174-
175- if (ctx.LangOpts .EnableModuleApiImportRemarks ) {
176- ModuleDecl *importedVia = problematicImport->module .importedModule ,
177- *sourceModule = D->getModuleContext ();
178- ctx.Diags .diagnose (loc, diag::module_api_import_aliases,
179- D, importedVia, sourceModule,
180- importedVia == sourceModule);
181- }
182- }
163+ // Remember that the module defining the underlying type must be imported
164+ // publicly.
165+ recordRequiredImportAccessLevelForDecl (
166+ D, DC, AccessLevel::Public,
167+ [&](AttributedImport<ImportedModule> attributedImport) {
168+ ModuleDecl *importedVia = attributedImport.module .importedModule ,
169+ *sourceModule = D->getModuleContext ();
170+ ctx.Diags .diagnose (loc, diag::module_api_import_aliases, D, importedVia,
171+ sourceModule, importedVia == sourceModule);
172+ });
183173
184174 auto ignoredDowngradeToWarning = DowngradeToWarning::No;
185175 auto originKind =
@@ -224,7 +214,6 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
224214
225215 // If limited by an import, note which one.
226216 if (originKind == DisallowedOriginKind::NonPublicImport) {
227- const DeclContext *DC = where.getDeclContext ();
228217 ImportAccessLevel limitImport = D->getImportAccessFrom (DC);
229218 assert (limitImport.has_value () &&
230219 limitImport->accessLevel < AccessLevel::Public &&
@@ -250,14 +239,20 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
250239 ASTContext &ctx = DC->getASTContext ();
251240 auto originKind = getDisallowedOriginKind (D, where, downgradeToWarning);
252241
253- // If we got here it was used in API, we can record the use of the import.
254- ImportAccessLevel import = D->getImportAccessFrom (DC);
255- if (import .has_value () && reason.has_value ()) {
256- auto SF = DC->getParentSourceFile ();
257- if (SF)
258- SF->registerAccessLevelUsingImport (import .value (),
259- AccessLevel::Public);
260- }
242+ // Remember that the module defining the decl must be imported publicly.
243+ recordRequiredImportAccessLevelForDecl (
244+ D, DC, AccessLevel::Public,
245+ [&](AttributedImport<ImportedModule> attributedImport) {
246+ if (where.isExported () && reason != ExportabilityReason::General &&
247+ originKind != DisallowedOriginKind::NonPublicImport) {
248+ // These may be reported twice, for the Type and for the TypeRepr.
249+ ModuleDecl *importedVia = attributedImport.module .importedModule ,
250+ *sourceModule = D->getModuleContext ();
251+ ctx.Diags .diagnose (loc, diag::module_api_import, D, importedVia,
252+ sourceModule, importedVia == sourceModule,
253+ /* isImplicit*/ false );
254+ }
255+ });
261256
262257 // Access levels from imports are reported with the others access levels.
263258 // Except for extensions and protocol conformances, we report them here.
@@ -277,19 +272,6 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
277272 return false ;
278273 }
279274
280- if (ctx.LangOpts .EnableModuleApiImportRemarks &&
281- import .has_value () && where.isExported () &&
282- reason != ExportabilityReason::General &&
283- originKind != DisallowedOriginKind::NonPublicImport) {
284- // These may be reported twice, for the Type and for the TypeRepr.
285- ModuleDecl *importedVia = import ->module .importedModule ,
286- *sourceModule = D->getModuleContext ();
287- ctx.Diags .diagnose (loc, diag::module_api_import,
288- D, importedVia, sourceModule,
289- importedVia == sourceModule,
290- /* isImplicit*/ false );
291- }
292-
293275 if (originKind == DisallowedOriginKind::None)
294276 return false ;
295277
@@ -336,6 +318,7 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
336318 }
337319
338320 // If limited by an import, note which one.
321+ ImportAccessLevel import = D->getImportAccessFrom (DC);
339322 if (originKind == DisallowedOriginKind::NonPublicImport) {
340323 assert (import .has_value () &&
341324 import ->accessLevel < AccessLevel::Public &&
@@ -379,25 +362,22 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
379362 if (ext->getParentModule ()->isBuiltinModule ())
380363 return false ;
381364
365+ const DeclContext *DC = where.getDeclContext ();
382366 ModuleDecl *M = ext->getParentModule ();
383367 ASTContext &ctx = M->getASTContext ();
384368
385- ImportAccessLevel problematicImport = ext->getImportAccessFrom (where.getDeclContext ());
386- if (problematicImport.has_value ()) {
387- auto SF = where.getDeclContext ()->getParentSourceFile ();
388- if (SF)
389- SF->registerAccessLevelUsingImport (problematicImport.value (),
390- AccessLevel::Public);
391-
392- if (ctx.LangOpts .EnableModuleApiImportRemarks ) {
393- ModuleDecl *importedVia = problematicImport->module .importedModule ,
394- *sourceModule = ext->getModuleContext ();
395- ctx.Diags .diagnose (loc, diag::module_api_import_conformance,
396- rootConf->getType (), rootConf->getProtocol (),
397- importedVia, sourceModule,
398- importedVia == sourceModule);
399- }
400- }
369+ // Remember that the module defining the conformance must be imported
370+ // publicly.
371+ recordRequiredImportAccessLevelForDecl (
372+ ext, DC, AccessLevel::Public,
373+ [&](AttributedImport<ImportedModule> attributedImport) {
374+ ModuleDecl *importedVia = attributedImport.module .importedModule ,
375+ *sourceModule = ext->getModuleContext ();
376+ ctx.Diags .diagnose (loc, diag::module_api_import_conformance,
377+ rootConf->getType (), rootConf->getProtocol (),
378+ importedVia, sourceModule,
379+ importedVia == sourceModule);
380+ });
401381
402382 auto originKind = getDisallowedOriginKind (ext, where);
403383 if (originKind == DisallowedOriginKind::None)
@@ -425,7 +405,6 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
425405
426406 // If limited by an import, note which one.
427407 if (originKind == DisallowedOriginKind::NonPublicImport) {
428- const DeclContext *DC = where.getDeclContext ();
429408 ImportAccessLevel limitImport = ext->getImportAccessFrom (DC);
430409 assert (limitImport.has_value () &&
431410 limitImport->accessLevel < AccessLevel::Public &&
0 commit comments