@@ -75,22 +75,16 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
75
75
}
76
76
}
77
77
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
+ });
94
88
95
89
// General check on access-level of the decl.
96
90
auto declAccessScope =
@@ -141,6 +135,7 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
141
135
142
136
Context.Diags .diagnose (D, diag::resilience_decl_declared_here, D);
143
137
138
+ ImportAccessLevel problematicImport = D->getImportAccessFrom (DC);
144
139
if (problematicImport.has_value () &&
145
140
problematicImport->accessLevel < D->getFormalAccess ()) {
146
141
Context.Diags .diagnose (problematicImport->importLoc ,
@@ -161,25 +156,20 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
161
156
if (!D)
162
157
return false ;
163
158
164
- auto exportingModule = where.getDeclContext ()->getParentModule ();
159
+ const DeclContext *DC = where.getDeclContext ();
160
+ auto exportingModule = DC->getParentModule ();
165
161
ASTContext &ctx = exportingModule->getASTContext ();
166
162
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
+ });
183
173
184
174
auto ignoredDowngradeToWarning = DowngradeToWarning::No;
185
175
auto originKind =
@@ -224,7 +214,6 @@ static bool diagnoseTypeAliasDeclRefExportability(SourceLoc loc,
224
214
225
215
// If limited by an import, note which one.
226
216
if (originKind == DisallowedOriginKind::NonPublicImport) {
227
- const DeclContext *DC = where.getDeclContext ();
228
217
ImportAccessLevel limitImport = D->getImportAccessFrom (DC);
229
218
assert (limitImport.has_value () &&
230
219
limitImport->accessLevel < AccessLevel::Public &&
@@ -250,14 +239,20 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
250
239
ASTContext &ctx = DC->getASTContext ();
251
240
auto originKind = getDisallowedOriginKind (D, where, downgradeToWarning);
252
241
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
+ });
261
256
262
257
// Access levels from imports are reported with the others access levels.
263
258
// Except for extensions and protocol conformances, we report them here.
@@ -277,19 +272,6 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
277
272
return false ;
278
273
}
279
274
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
-
293
275
if (originKind == DisallowedOriginKind::None)
294
276
return false ;
295
277
@@ -336,6 +318,7 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
336
318
}
337
319
338
320
// If limited by an import, note which one.
321
+ ImportAccessLevel import = D->getImportAccessFrom (DC);
339
322
if (originKind == DisallowedOriginKind::NonPublicImport) {
340
323
assert (import .has_value () &&
341
324
import ->accessLevel < AccessLevel::Public &&
@@ -379,25 +362,22 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
379
362
if (ext->getParentModule ()->isBuiltinModule ())
380
363
return false ;
381
364
365
+ const DeclContext *DC = where.getDeclContext ();
382
366
ModuleDecl *M = ext->getParentModule ();
383
367
ASTContext &ctx = M->getASTContext ();
384
368
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
+ });
401
381
402
382
auto originKind = getDisallowedOriginKind (ext, where);
403
383
if (originKind == DisallowedOriginKind::None)
@@ -425,7 +405,6 @@ TypeChecker::diagnoseConformanceExportability(SourceLoc loc,
425
405
426
406
// If limited by an import, note which one.
427
407
if (originKind == DisallowedOriginKind::NonPublicImport) {
428
- const DeclContext *DC = where.getDeclContext ();
429
408
ImportAccessLevel limitImport = ext->getImportAccessFrom (DC);
430
409
assert (limitImport.has_value () &&
431
410
limitImport->accessLevel < AccessLevel::Public &&
0 commit comments