Skip to content

Commit 4e63e02

Browse files
committed
Guard feature behind experimental flag.
1 parent 9c8fa4f commit 4e63e02

File tree

14 files changed

+66
-34
lines changed

14 files changed

+66
-34
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,9 @@ ERROR(expr_keypath_mutating_getter,none,
663663
"%select{key path|dynamic key path member lookup}1 cannot refer to %0, "
664664
"which has a mutating getter",
665665
(const ValueDecl *, bool))
666+
ERROR(expr_keypath_static_member,none,
667+
"%select{key path|dynamic key path member lookup}1 cannot refer to static member %0",
668+
(const ValueDecl *, bool))
666669
ERROR(expr_keypath_enum_case,none,
667670
"%select{key path|dynamic key path member lookup}1 cannot refer to enum case %0",
668671
(const ValueDecl *, bool))

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ EXPERIMENTAL_FEATURE(TupleConformances, false)
233233
EXPERIMENTAL_FEATURE(FullTypedThrows, false)
234234
EXPERIMENTAL_FEATURE(SameElementRequirements, false)
235235
EXPERIMENTAL_FEATURE(GlobalActorInferenceCutoff, false)
236+
EXPERIMENTAL_FEATURE(KeyPathWithStaticMember, false)
236237

237238
// Whether to enable @_used and @_section attributes
238239
EXPERIMENTAL_FEATURE(SymbolLinkageMarkers, true)

include/swift/Sema/CSFix.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,6 +2026,9 @@ class TreatKeyPathSubscriptIndexAsHashable final : public ConstraintFix {
20262026

20272027
class AllowInvalidRefInKeyPath final : public ConstraintFix {
20282028
enum RefKind {
2029+
// Allow a reference to a static member as a key path component if it is
2030+
// declared in a module with built with Swift 6.0 compiler version or older.
2031+
StaticMember,
20292032
// Allow a reference to a declaration with mutating getter as
20302033
// a key path component.
20312034
MutatingGetter,
@@ -2050,6 +2053,8 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
20502053
public:
20512054
std::string getName() const override {
20522055
switch (Kind) {
2056+
case RefKind::StaticMember:
2057+
return "allow reference to a static member as a key path component";
20532058
case RefKind::MutatingGetter:
20542059
return "allow reference to a member with mutating getter as a key "
20552060
"path component";

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ UNINTERESTING_FEATURE(GroupActorErrors)
196196
UNINTERESTING_FEATURE(SameElementRequirements)
197197
UNINTERESTING_FEATURE(UnspecifiedMeansMainActorIsolated)
198198
UNINTERESTING_FEATURE(GlobalActorInferenceCutoff)
199+
UNINTERESTING_FEATURE(KeyPathWithStaticMember)
199200

200201
static bool usesFeatureSendingArgsAndResults(Decl *decl) {
201202
auto isFunctionTypeWithSending = [](Type type) {

lib/Sema/CSDiagnostics.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6339,13 +6339,19 @@ bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
63396339
auto rootTyRepr = KPE->getExplicitRootType();
63406340
auto isProtocol = getBaseType()->isExistentialType();
63416341

6342-
if (rootTyRepr && !isProtocol) {
6343-
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
6344-
DeclNameRef(getMember()->getName()))
6345-
.fixItInsert(rootTyRepr->getEndLoc(), ".Type");
6342+
if (!getConstraintSystem().getASTContext().LangOpts.hasFeature(
6343+
Feature::KeyPathWithStaticMember)) {
6344+
emitDiagnostic(diag::expr_keypath_static_member, getMember(),
6345+
isForKeyPathDynamicMemberLookup());
63466346
} else {
6347-
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
6348-
DeclNameRef(getMember()->getName()));
6347+
if (rootTyRepr && !isProtocol) {
6348+
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
6349+
DeclNameRef(getMember()->getName()))
6350+
.fixItInsert(rootTyRepr->getEndLoc(), ".Type");
6351+
} else {
6352+
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
6353+
DeclNameRef(getMember()->getName()));
6354+
}
63496355
}
63506356

63516357
return true;

lib/Sema/CSFix.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,10 @@ TreatKeyPathSubscriptIndexAsHashable::create(ConstraintSystem &cs, Type type,
12301230
bool AllowInvalidRefInKeyPath::diagnose(const Solution &solution,
12311231
bool asNote) const {
12321232
switch (Kind) {
1233+
case RefKind::StaticMember: {
1234+
return false;
1235+
}
1236+
12331237
case RefKind::EnumCase: {
12341238
InvalidEnumCaseRefInKeyPath failure(solution, Member, getLocator());
12351239
return failure.diagnose(asNote);
@@ -1277,6 +1281,13 @@ AllowInvalidRefInKeyPath *
12771281
AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, Type baseType,
12781282
ValueDecl *member,
12791283
ConstraintLocator *locator) {
1284+
1285+
if (!cs.getASTContext().LangOpts.hasFeature(
1286+
Feature::KeyPathWithStaticMember) &&
1287+
member->isStatic())
1288+
return AllowInvalidRefInKeyPath::create(cs, baseType, RefKind::StaticMember,
1289+
member, locator);
1290+
12801291
// Referencing (instance or static) methods in key path is
12811292
// not currently allowed.
12821293
if (isa<FuncDecl>(member))

test/Interpreter/keypath.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// RUN: %target-run-simple-swift | %FileCheck %s
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-feature -Xfrontend KeyPathWithStaticMember) | %FileCheck %s
22

3+
// REQUIRES: asserts
34
// REQUIRES: executable_test
45

56
// UNSUPPORTED: use_os_stdlib

test/ModuleInterface/static_keypaths.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// RUN: split-file %s %t/src
33

44
/// Build LibA
5-
// RUN: %host-build-swift %t/src/LibA.swift -swift-version 5 -emit-module -emit-library -enable-library-evolution -module-name LibA -o %t/%target-library-name(LibA) -emit-module-interface-path %t/LibA.swiftinterface
5+
// RUN: %host-build-swift %t/src/LibA.swift -swift-version 5 -enable-experimental-feature KeyPathWithStaticMember -emit-module -emit-library -enable-library-evolution -module-name LibA -o %t/%target-library-name(LibA) -emit-module-interface-path %t/LibA.swiftinterface
66

77
// Build LibB
8-
// RUN: %target-build-swift %t/src/LibB.swift -I %t -L %t -l LibA -swift-version 5 -emit-module -emit-library -module-name LibB -o %t/%target-library-name(LibB)
8+
// RUN: %target-build-swift %t/src/LibB.swift -I %t -L %t -l LibA -swift-version 5 -enable-experimental-feature KeyPathWithStaticMember -emit-module -emit-library -module-name LibB -o %t/%target-library-name(LibB)
99

1010
// Build LibC
11-
// RUN: %target-build-swift %t/src/LibC.swift -I %t -L %t -l LibA -swift-version 5 -emit-module -emit-library -module-name LibC -o %t/%target-library-name(LibC)
11+
// RUN: %target-build-swift %t/src/LibC.swift -I %t -L %t -l LibA -swift-version 5 -enable-experimental-feature KeyPathWithStaticMember -emit-module -emit-library -module-name LibC -o %t/%target-library-name(LibC)
1212

1313
// Build & run main.swift
1414
// RUN: %target-build-swift -I %t -L %t -l LibA -l LibB -l LibC %t/src/main.swift -o %t/a.out

test/SILGen/keypaths.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %target-swift-emit-silgen -disable-availability-checking -parse-stdlib -module-name keypaths %s | %FileCheck %s
1+
// RUN: %target-swift-emit-silgen -enable-experimental-feature KeyPathWithStaticMember -disable-availability-checking -parse-stdlib -module-name keypaths %s | %FileCheck %s
2+
3+
// REQUIRES: asserts
24

35
import Swift
46

test/SILOptimizer/access_wmo_diagnose.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -parse-as-library -emit-sil -enforce-exclusivity=checked -primary-file %s -o /dev/null -verify
1+
// RUN: %target-swift-frontend -enable-experimental-feature KeyPathWithStaticMember -parse-as-library -emit-sil -enforce-exclusivity=checked -primary-file %s -o /dev/null -verify
22

33
// AccessEnforcementWMO assumes that the only way to address a global or static
44
// property is via a formal begin_access. If we ever allow keypaths for static

test/abi/macOS/x86_64/concurrency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,4 @@ Added: _$ss15ContinuousClockV3nowAB7InstantVvpZMV
358358
Added: _$ss15ContinuousClockV7InstantV3nowADvpZMV
359359
Added: _$ss15SuspendingClockV3nowAB7InstantVvpZMV
360360
Added: _$ss15SuspendingClockV7InstantV3nowADvpZMV
361-
Added: _$ss9TaskLocalC18_enclosingInstance7wrapped7storagexs5NeverO_s24ReferenceAdded: WritableKeyPathCyAGxGAIyAgByxGGtcipZMV
361+
Added: _$ss9TaskLocalC18_enclosingInstance7wrapped7storagexs5NeverO_s24ReferenceWritableKeyPathCyAGxGAIyAgByxGGtcipZMV

test/abi/macOS/x86_64/stdlib.swift

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -689,21 +689,6 @@ Added: _$ss6Int128V8bitWidthSivpZMV
689689
Added: _$ss6UInt16V8bitWidthSivpZMV
690690
Added: _$ss6UInt32V8bitWidthSivpZMV
691691
Added: _$ss6UInt64V8bitWidthSivpZMV
692-
Added: _$ss7Float16V12signalingNaNABvpZMV
693-
Added: _$ss7Float16V13_exponentBiasSuvpZMV
694-
Added: _$ss7Float16V13_quietNaNMasks6UInt16VvpZMV
695-
Added: _$ss7Float16V16_significandMasks6UInt16VvpZMV
696-
Added: _$ss7Float16V16exponentBitCountSivpZMV
697-
Added: _$ss7Float16V17_infinityExponentSuvpZMV
698-
Added: _$ss7Float16V19significandBitCountSivpZMV
699-
Added: _$ss7Float16V20leastNormalMagnitudeABvpZMV
700-
Added: _$ss7Float16V21leastNonzeroMagnitudeABvpZMV
701-
Added: _$ss7Float16V23greatestFiniteMagnitudeABvpZMV
702-
Added: _$ss7Float16V2piABvpZMV
703-
Added: _$ss7Float16V3nanABvpZMV
704-
Added: _$ss7Float16V8infinityABvpZMV
705-
Added: _$ss7Float16V8quietNaNABvpZMV
706-
Added: _$ss7Float16V8ulpOfOneABvpZMV
707692
Added: _$ss7UInt128V3maxABvpZMV
708693
Added: _$ss7UInt128V3minABvpZMV
709694
Added: _$ss7UInt128V4zeroABvpZMV
@@ -729,11 +714,7 @@ Added: _$ss7UnicodeO23CanonicalCombiningClassV7overlayADvpZMV
729714
Added: _$ss7UnicodeO23CanonicalCombiningClassV9aboveLeftADvpZMV
730715
Added: _$ss7UnicodeO23CanonicalCombiningClassV9belowLeftADvpZMV
731716
Added: _$ss7UnicodeO4UTF8O27encodedReplacementCharacters06_ValidB6BufferVvpZMV
732-
Added: _$ss7UnicodeO5ASCIIO27encodedReplacementCharacters15CollectionOfOneVys5UInt8VGAdded: vpZMV
733-
Added: _$ss7UnicodeO5UTF16O20_replacementCodeUnits6UInt16VvpZMV
734-
Added: _$ss7UnicodeO5UTF16O27encodedReplacementCharacters11_UIntBufferVys6UInt16VGvpZAdded: MV
735717
Added: _$ss7UnicodeO5UTF32O20_replacementCodeUnits6UInt32VvpZMV
736-
Added: _$ss7UnicodeO5UTF32O27encodedReplacementCharacters15CollectionOfOneVys6UInt32VAdded: GvpZMV
737718
Added: _$ss8DurationV4zeroABvpZMV
738719
Added: _$ss8SIMDMaskVss5SIMD2VySiGRszrlE7allTrueAByAEGvpZMV
739720
Added: _$ss8SIMDMaskVss5SIMD2Vys4Int8VGRszrlE7allTrueAByAGGvpZMV
@@ -770,3 +751,24 @@ Added: _$ss8SIMDMaskVss6SIMD64Vys4Int8VGRszrlE7allTrueAByAGGvpZMV
770751
Added: _$ss8SIMDMaskVss6SIMD64Vys5Int16VGRszrlE7allTrueAByAGGvpZMV
771752
Added: _$ss8SIMDMaskVss6SIMD64Vys5Int32VGRszrlE7allTrueAByAGGvpZMV
772753
Added: _$ss8SIMDMaskVss6SIMD64Vys5Int64VGRszrlE7allTrueAByAGGvpZMV
754+
Added: _$sSo19_SwiftStdlibVersionasE6v6_1_0ABvpZMV
755+
Added: _$ss7Float80V12signalingNaNABvpZMV
756+
Added: _$ss7Float80V13_exponentBiasSuvpZMV
757+
Added: _$ss7Float80V13_quietNaNMasks6UInt64VvpZMV
758+
Added: _$ss7Float80V16_explicitBitMasks6UInt64VvpZMV
759+
Added: _$ss7Float80V16_significandMasks6UInt64VvpZMV
760+
Added: _$ss7Float80V16exponentBitCountSivpZMV
761+
Added: _$ss7Float80V17_infinityExponentSuvpZMV
762+
Added: _$ss7Float80V19significandBitCountSivpZMV
763+
Added: _$ss7Float80V20leastNormalMagnitudeABvpZMV
764+
Added: _$ss7Float80V21leastNonzeroMagnitudeABvpZMV
765+
Added: _$ss7Float80V23greatestFiniteMagnitudeABvpZMV
766+
Added: _$ss7Float80V2piABvpZMV
767+
Added: _$ss7Float80V3nanABvpZMV
768+
Added: _$ss7Float80V8infinityABvpZMV
769+
Added: _$ss7Float80V8quietNaNABvpZMV
770+
Added: _$ss7Float80V8ulpOfOneABvpZMV
771+
Added: _$ss7UnicodeO5ASCIIO27encodedReplacementCharacters15CollectionOfOneVys5UInt8VGvpZMV
772+
Added: _$ss7UnicodeO5UTF16O20_replacementCodeUnits6UInt16VvpZMV
773+
Added: _$ss7UnicodeO5UTF16O27encodedReplacementCharacters11_UIntBufferVys6UInt16VGvpZMV
774+
Added: _$ss7UnicodeO5UTF32O27encodedReplacementCharacters15CollectionOfOneVys6UInt32VGvpZMV

test/attr/attr_dynamic_member_lookup.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature KeyPathWithStaticMember
22

33
var global = 42
44

test/expr/unary/keypath/keypath.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -typecheck -parse-as-library %s -verify
1+
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature KeyPathWithStaticMember -parse-as-library %s -verify
22

33
struct Sub: Hashable {
44
static func ==(_: Sub, _: Sub) -> Bool { return true }

0 commit comments

Comments
 (0)