Skip to content

Commit 0889565

Browse files
committed
[Parser] Intro parser support and flag for access level on imports
1 parent 3d8e60d commit 0889565

File tree

8 files changed

+88
-3
lines changed

8 files changed

+88
-3
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,16 @@ ERROR(spi_only_imports_not_enabled, none,
20442044
"'@_spiOnly' requires setting the frontend flag '-experimental-spi-only-imports'",
20452045
())
20462046

2047+
// Access level on imports
2048+
ERROR(access_level_on_import_not_enabled, none,
2049+
"Access level on imports require '-enable-experimental-feature AccessLevelOnImport'",
2050+
())
2051+
ERROR(access_level_on_import_unsupported, none,
2052+
"The access level %0 is unsupported on imports: "
2053+
"only 'public', 'package', 'internal', 'fileprivate' and 'private' "
2054+
"are unsupported",
2055+
(DeclAttribute))
2056+
20472057
// Opaque return types
20482058
ERROR(opaque_type_invalid_constraint,none,
20492059
"an 'opaque' type must specify only 'Any', 'AnyObject', protocols, "

include/swift/Basic/Features.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ EXPERIMENTAL_FEATURE(MoveOnlyClasses, true)
115115
EXPERIMENTAL_FEATURE(OneWayClosureParameters, false)
116116
EXPERIMENTAL_FEATURE(TypeWitnessSystemInference, false)
117117
EXPERIMENTAL_FEATURE(LayoutPrespecialization, true)
118+
118119
EXPERIMENTAL_FEATURE(ModuleInterfaceExportAs, true)
120+
EXPERIMENTAL_FEATURE(AccessLevelOnImport, false)
119121

120122
/// Whether to enable experimental differentiable programming features:
121123
/// `@differentiable` declaration attribute, etc.

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,10 @@ static bool usesFeatureModuleInterfaceExportAs(Decl *decl) {
31193119
return false;
31203120
}
31213121

3122+
static bool usesFeatureAccessLevelOnImport(Decl *decl) {
3123+
return false;
3124+
}
3125+
31223126
static bool usesFeatureNamedOpaqueTypes(Decl *decl) {
31233127
return false;
31243128
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ void AttributeChecker::visitLazyAttr(LazyAttr *attr) {
934934
bool AttributeChecker::visitAbstractAccessControlAttr(
935935
AbstractAccessControlAttr *attr) {
936936
// Access control attr may only be used on value decls and extensions.
937-
if (!isa<ValueDecl>(D) && !isa<ExtensionDecl>(D)) {
937+
if (!isa<ValueDecl>(D) && !isa<ExtensionDecl>(D) && !isa<ImportDecl>(D)) {
938938
diagnoseAndRemoveAttr(attr, diag::invalid_decl_modifier, attr);
939939
return true;
940940
}
@@ -960,6 +960,19 @@ bool AttributeChecker::visitAbstractAccessControlAttr(
960960
return true;
961961
}
962962

963+
if (auto importDecl = dyn_cast<ImportDecl>(D)) {
964+
if (!D->getASTContext().LangOpts.hasFeature(Feature::AccessLevelOnImport)) {
965+
diagnoseAndRemoveAttr(attr, diag::access_level_on_import_not_enabled);
966+
return true;
967+
}
968+
969+
if (attr->getAccess() == AccessLevel::Open) {
970+
diagnoseAndRemoveAttr(attr, diag::access_level_on_import_unsupported,
971+
attr);
972+
return true;
973+
}
974+
}
975+
963976
return false;
964977
}
965978

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
/// Build the libraries.
5+
// RUN: %target-swift-frontend -emit-module %t/PublicLib.swift -o %t
6+
// RUN: %target-swift-frontend -emit-module %t/PackageLib.swift -o %t
7+
// RUN: %target-swift-frontend -emit-module %t/InternalLib.swift -o %t
8+
// RUN: %target-swift-frontend -emit-module %t/FileprivateLib.swift -o %t
9+
// RUN: %target-swift-frontend -emit-module %t/PrivateLib.swift -o %t
10+
11+
/// Check flag requirement, without and with the flag.
12+
// RUN: %target-swift-frontend -typecheck %t/ClientWithoutTheFlag.swift -I %t -verify
13+
// RUN: %target-swift-frontend -typecheck %t/ClientWithoutTheFlag.swift -I %t \
14+
// RUN: -enable-experimental-feature AccessLevelOnImport
15+
16+
//--- PublicLib.swift
17+
//--- PackageLib.swift
18+
//--- InternalLib.swift
19+
//--- FileprivateLib.swift
20+
//--- PrivateLib.swift
21+
22+
//--- ClientWithoutTheFlag.swift
23+
public import PublicLib // expected-error@:1 {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport'}}
24+
package import PackageLib // expected-error@:1 {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport'}}
25+
internal import InternalLib // expected-error@:1 {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport'}}
26+
fileprivate import FileprivateLib // expected-error@:1 {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport'}}
27+
private import PrivateLib // expected-error@:1 {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport'}}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
/// Build the libraries.
5+
// RUN: %target-swift-frontend -emit-module %t/PublicLib.swift -o %t
6+
// RUN: %target-swift-frontend -emit-module %t/PackageLib.swift -o %t
7+
// RUN: %target-swift-frontend -emit-module %t/InternalLib.swift -o %t
8+
// RUN: %target-swift-frontend -emit-module %t/FileprivateLib.swift -o %t
9+
// RUN: %target-swift-frontend -emit-module %t/PrivateLib.swift -o %t
10+
// RUN: %target-swift-frontend -emit-module %t/OpenLib.swift -o %t
11+
12+
/// Check that all access levels are accepted, except for 'open'.
13+
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
14+
// RUN: -enable-experimental-feature AccessLevelOnImport -verify
15+
16+
//--- PublicLib.swift
17+
//--- PackageLib.swift
18+
//--- InternalLib.swift
19+
//--- FileprivateLib.swift
20+
//--- PrivateLib.swift
21+
//--- OpenLib.swift
22+
23+
//--- Client.swift
24+
public import PublicLib
25+
package import PackageLib
26+
internal import InternalLib
27+
fileprivate import FileprivateLib
28+
private import PrivateLib
29+
open import OpenLib // expected-error {{The access level 'open' is unsupported on imports: only 'public', 'package', 'internal', 'fileprivate' and 'private' are unsupported}}

test/attr/accessibility.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var unterminatedEmptySubject = 0
6363
duplicateAttr(1) // expected-error{{argument passed to call that takes no arguments}}
6464

6565
// CHECK ALLOWED DECLS
66-
private import Swift // expected-error {{'private' modifier cannot be applied to this declaration}} {{1-9=}}
66+
private import Swift // expected-error {{Access level on imports require '-enable-experimental-feature AccessLevelOnImport}}
6767
private(set) infix operator ~~~ // expected-error {{'private' modifier cannot be applied to this declaration}} {{1-14=}}
6868

6969
private typealias MyInt = Int

utils/gyb_syntax_support/AttributeKinds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ def __init__(self, name, swift_name=None):
825825
code=44),
826826
DeclAttribute('private', 'AccessControl',
827827
OnFunc, OnAccessor, OnExtension, OnGenericType, OnVar, OnSubscript,
828-
OnConstructor, OnMacro,
828+
OnConstructor, OnMacro, OnImport,
829829
DeclModifier,
830830
NotSerialized,
831831
ABIStableToAdd, ABIStableToRemove, APIStableToAdd, APIStableToRemove,

0 commit comments

Comments
 (0)