Skip to content

ModuleInterface: prepare to accept access-level on imports in swiftinterfaces #68523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/Frontend/ModuleInterfaceSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ static void printImports(raw_ostream &out,
out << "@_spi(" << spiName << ") ";
}

if (M->getASTContext().LangOpts.isSwiftVersionAtLeast(6)) {
out << "public ";
}

out << "import ";
if (Opts.AliasModuleNames &&
AliasModuleNamesTargets.contains(importedModule->getName().str()))
Expand Down
7 changes: 5 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,8 @@ void AttributeChecker::visitLazyAttr(LazyAttr *attr) {

bool AttributeChecker::visitAbstractAccessControlAttr(
AbstractAccessControlAttr *attr) {
// Access control attr may only be used on value decls and extensions.
// Access control attr may only be used on value decls, extensions and
// imports.
if (!isa<ValueDecl>(D) && !isa<ExtensionDecl>(D) && !isa<ImportDecl>(D)) {
diagnoseAndRemoveAttr(attr, diag::invalid_decl_modifier, attr);
return true;
Expand Down Expand Up @@ -996,7 +997,9 @@ bool AttributeChecker::visitAbstractAccessControlAttr(
}

if (auto importDecl = dyn_cast<ImportDecl>(D)) {
if (!D->getASTContext().LangOpts.hasFeature(Feature::AccessLevelOnImport)) {
SourceFile *File = D->getDeclContext()->getParentSourceFile();
if (!D->getASTContext().LangOpts.hasFeature(Feature::AccessLevelOnImport) &&
File && File->Kind != SourceFileKind::Interface) {
diagnoseAndRemoveAttr(attr, diag::access_level_on_import_not_enabled);
return true;
}
Expand Down
18 changes: 18 additions & 0 deletions test/ModuleInterface/imports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -I %S/Inputs/imports-clang-modules/ -I %t
// RUN: %FileCheck -implicit-check-not BAD %s < %t.swiftinterface

/// Swift 6 variant.
// RUN: %target-swift-emit-module-interface(%t.swiftinterface) %s %S/Inputs/imports-other.swift -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -I %S/Inputs/imports-clang-modules/ -I %t -verify -swift-version 6
// RUN: %target-swift-typecheck-module-from-interface(%t.swiftinterface) -I %S/Inputs/imports-clang-modules/ -I %t
// RUN: %FileCheck -implicit-check-not BAD -check-prefix CHECK-6 %s < %t.swiftinterface

@_exported import empty // expected-warning {{module 'empty' was not compiled with library evolution support; using it means binary compatibility for 'imports' can't be guaranteed}}
@_exported import emptyButWithLibraryEvolution
Expand All @@ -29,3 +33,17 @@ import NotSoSecret2 // expected-warning {{'NotSoSecret2' inconsistently imported
// CHECK-NEXT: {{^}}@_exported import empty{{$}}
// CHECK-NEXT: {{^}}@_exported import emptyButWithLibraryEvolution{{$}}
// CHECK-NOT: import

// CHECK-6-NOT: import
// CHECK-6: {{^}}public import A{{$}}
// CHECK-6-NEXT: {{^}}public import B{{$}}
// CHECK-6-NEXT: {{^}}public import B.B2{{$}}
// CHECK-6-NEXT: {{^}}public import B.B3{{$}}
// CHECK-6-NEXT: {{^}}public import C/*.c*/{{$}}
// CHECK-6-NEXT: {{^}}public import D{{$}}
// CHECK-6-NEXT: {{^}}public import NotSoSecret{{$}}
// CHECK-6-NEXT: {{^}}public import NotSoSecret2{{$}}
// CHECK-6-NEXT: {{^}}public import Swift{{$}}
// CHECK-6-NEXT: {{^}}@_exported public import empty{{$}}
// CHECK-6-NEXT: {{^}}@_exported public import emptyButWithLibraryEvolution{{$}}
// CHECK-6-NOT: import
27 changes: 22 additions & 5 deletions test/Sema/access-level-import-flag-check.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
// RUN: split-file %s %t

/// Build the libraries.
// RUN: %target-swift-frontend -emit-module %t/PublicLib.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/PackageLib.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/InternalLib.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/FileprivateLib.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/PrivateLib.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/PublicLib.swift -o %t \
// RUN: -enable-library-evolution
// RUN: %target-swift-frontend -emit-module %t/PackageLib.swift -o %t \
// RUN: -enable-library-evolution
// RUN: %target-swift-frontend -emit-module %t/InternalLib.swift -o %t \
// RUN: -enable-library-evolution
// RUN: %target-swift-frontend -emit-module %t/FileprivateLib.swift -o %t \
// RUN: -enable-library-evolution
// RUN: %target-swift-frontend -emit-module %t/PrivateLib.swift -o %t \
// RUN: -enable-library-evolution

/// Check flag requirement, without and with the flag.
// RUN: %target-swift-frontend -typecheck %t/ClientWithoutTheFlag.swift -I %t -verify \
Expand All @@ -16,6 +21,9 @@
// RUN: -package-name package
// REQUIRES: asserts

/// swiftinterfaces don't need the flag.
// RUN: %target-swift-typecheck-module-from-interface(%t/Client.swiftinterface) -I %t

//--- PublicLib.swift
//--- PackageLib.swift
//--- InternalLib.swift
Expand All @@ -28,3 +36,12 @@ package import PackageLib // expected-error@:1 {{Access level on imports require
internal import InternalLib // expected-error@:1 {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport'}}
fileprivate import FileprivateLib // expected-error@:1 {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport'}}
private import PrivateLib // expected-error@:1 {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport'}}

//--- Client.swiftinterface
// swift-interface-format-version: 1.0
// swift-module-flags: -enable-library-evolution -package-name MyPackage
public import PublicLib
package import PackageLib
internal import InternalLib
fileprivate import FileprivateLib
private import PrivateLib