Guard feature behind experimental flag.

This commit is contained in:
Amritpan Kaur
2024-09-20 13:28:47 -07:00
parent c69e5cb49c
commit 8ebc928649
14 changed files with 67 additions and 35 deletions

View File

@@ -663,6 +663,9 @@ ERROR(expr_keypath_mutating_getter,none,
"%select{key path|dynamic key path member lookup}1 cannot refer to %0, "
"which has a mutating getter",
(const ValueDecl *, bool))
ERROR(expr_keypath_static_member,none,
"%select{key path|dynamic key path member lookup}1 cannot refer to static member %0",
(const ValueDecl *, bool))
ERROR(expr_keypath_enum_case,none,
"%select{key path|dynamic key path member lookup}1 cannot refer to enum case %0",
(const ValueDecl *, bool))

View File

@@ -233,6 +233,7 @@ EXPERIMENTAL_FEATURE(TupleConformances, false)
EXPERIMENTAL_FEATURE(FullTypedThrows, false)
EXPERIMENTAL_FEATURE(SameElementRequirements, false)
EXPERIMENTAL_FEATURE(GlobalActorInferenceCutoff, false)
EXPERIMENTAL_FEATURE(KeyPathWithStaticMembers, false)
// Whether to enable @_used and @_section attributes
EXPERIMENTAL_FEATURE(SymbolLinkageMarkers, true)

View File

@@ -2026,6 +2026,9 @@ public:
class AllowInvalidRefInKeyPath final : public ConstraintFix {
enum RefKind {
// Allow a reference to a static member as a key path component if it is
// declared in a module with built with Swift 6.0 compiler version or older.
StaticMember,
// Allow a reference to a declaration with mutating getter as
// a key path component.
MutatingGetter,
@@ -2050,6 +2053,8 @@ class AllowInvalidRefInKeyPath final : public ConstraintFix {
public:
std::string getName() const override {
switch (Kind) {
case RefKind::StaticMember:
return "allow reference to a static member as a key path component";
case RefKind::MutatingGetter:
return "allow reference to a member with mutating getter as a key "
"path component";

View File

@@ -196,6 +196,7 @@ UNINTERESTING_FEATURE(GroupActorErrors)
UNINTERESTING_FEATURE(SameElementRequirements)
UNINTERESTING_FEATURE(UnspecifiedMeansMainActorIsolated)
UNINTERESTING_FEATURE(GlobalActorInferenceCutoff)
UNINTERESTING_FEATURE(KeyPathWithStaticMembers)
static bool usesFeatureSendingArgsAndResults(Decl *decl) {
auto isFunctionTypeWithSending = [](Type type) {

View File

@@ -6339,13 +6339,19 @@ bool InvalidStaticMemberRefInKeyPath::diagnoseAsError() {
auto rootTyRepr = KPE->getExplicitRootType();
auto isProtocol = getBaseType()->isExistentialType();
if (rootTyRepr && !isProtocol) {
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
DeclNameRef(getMember()->getName()))
.fixItInsert(rootTyRepr->getEndLoc(), ".Type");
if (!getConstraintSystem().getASTContext().LangOpts.hasFeature(
Feature::KeyPathWithStaticMembers)) {
emitDiagnostic(diag::expr_keypath_static_member, getMember(),
isForKeyPathDynamicMemberLookup());
} else {
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
DeclNameRef(getMember()->getName()));
if (rootTyRepr && !isProtocol) {
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
DeclNameRef(getMember()->getName()))
.fixItInsert(rootTyRepr->getEndLoc(), ".Type");
} else {
emitDiagnostic(diag::could_not_use_type_member_on_instance, getBaseType(),
DeclNameRef(getMember()->getName()));
}
}
return true;

View File

@@ -1230,6 +1230,10 @@ TreatKeyPathSubscriptIndexAsHashable::create(ConstraintSystem &cs, Type type,
bool AllowInvalidRefInKeyPath::diagnose(const Solution &solution,
bool asNote) const {
switch (Kind) {
case RefKind::StaticMember: {
return false;
}
case RefKind::EnumCase: {
InvalidEnumCaseRefInKeyPath failure(solution, Member, getLocator());
return failure.diagnose(asNote);
@@ -1277,6 +1281,13 @@ AllowInvalidRefInKeyPath *
AllowInvalidRefInKeyPath::forRef(ConstraintSystem &cs, Type baseType,
ValueDecl *member,
ConstraintLocator *locator) {
if (!cs.getASTContext().LangOpts.hasFeature(
Feature::KeyPathWithStaticMembers) &&
member->isStatic())
return AllowInvalidRefInKeyPath::create(cs, baseType, RefKind::StaticMember,
member, locator);
// Referencing (instance or static) methods in key path is
// not currently allowed.
if (isa<FuncDecl>(member))

View File

@@ -1,5 +1,6 @@
// RUN: %target-run-simple-swift | %FileCheck %s
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-feature -Xfrontend KeyPathWithStaticMembers) | %FileCheck %s
// REQUIRES: asserts
// REQUIRES: executable_test
// UNSUPPORTED: use_os_stdlib

View File

@@ -2,13 +2,13 @@
// RUN: split-file %s %t/src
/// Build LibA
// 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
// RUN: %host-build-swift %t/src/LibA.swift -swift-version 5 -enable-experimental-feature KeyPathWithStaticMembers -emit-module -emit-library -enable-library-evolution -module-name LibA -o %t/%target-library-name(LibA) -emit-module-interface-path %t/LibA.swiftinterface
// Build LibB
// 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)
// RUN: %target-build-swift %t/src/LibB.swift -I %t -L %t -l LibA -swift-version 5 -enable-experimental-feature KeyPathWithStaticMembers -emit-module -emit-library -module-name LibB -o %t/%target-library-name(LibB)
// Build LibC
// 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)
// RUN: %target-build-swift %t/src/LibC.swift -I %t -L %t -l LibA -swift-version 5 -enable-experimental-feature KeyPathWithStaticMembers -emit-module -emit-library -module-name LibC -o %t/%target-library-name(LibC)
// Build & run main.swift
// RUN: %target-build-swift -I %t -L %t -l LibA -l LibB -l LibC %t/src/main.swift -o %t/a.out

View File

@@ -1,4 +1,6 @@
// RUN: %target-swift-emit-silgen -disable-availability-checking -parse-stdlib -module-name keypaths %s | %FileCheck %s
// RUN: %target-swift-emit-silgen -enable-experimental-feature KeyPathWithStaticMembers -disable-availability-checking -parse-stdlib -module-name keypaths %s | %FileCheck %s
// REQUIRES: asserts
import Swift
@@ -719,4 +721,4 @@ protocol HasAlias {
func testHasAlias() {
_ = \HasAlias.id
}
}

View File

@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -parse-as-library -emit-sil -enforce-exclusivity=checked -primary-file %s -o /dev/null -verify
// RUN: %target-swift-frontend -enable-experimental-feature KeyPathWithStaticMembers -parse-as-library -emit-sil -enforce-exclusivity=checked -primary-file %s -o /dev/null -verify
// AccessEnforcementWMO assumes that the only way to address a global or static
// property is via a formal begin_access. If we ever allow keypaths for static

View File

@@ -358,4 +358,4 @@ Added: _$ss15ContinuousClockV3nowAB7InstantVvpZMV
Added: _$ss15ContinuousClockV7InstantV3nowADvpZMV
Added: _$ss15SuspendingClockV3nowAB7InstantVvpZMV
Added: _$ss15SuspendingClockV7InstantV3nowADvpZMV
Added: _$ss9TaskLocalC18_enclosingInstance7wrapped7storagexs5NeverO_s24ReferenceAdded: WritableKeyPathCyAGxGAIyAgByxGGtcipZMV
Added: _$ss9TaskLocalC18_enclosingInstance7wrapped7storagexs5NeverO_s24ReferenceWritableKeyPathCyAGxGAIyAgByxGGtcipZMV

View File

@@ -689,21 +689,6 @@ Added: _$ss6Int128V8bitWidthSivpZMV
Added: _$ss6UInt16V8bitWidthSivpZMV
Added: _$ss6UInt32V8bitWidthSivpZMV
Added: _$ss6UInt64V8bitWidthSivpZMV
Added: _$ss7Float16V12signalingNaNABvpZMV
Added: _$ss7Float16V13_exponentBiasSuvpZMV
Added: _$ss7Float16V13_quietNaNMasks6UInt16VvpZMV
Added: _$ss7Float16V16_significandMasks6UInt16VvpZMV
Added: _$ss7Float16V16exponentBitCountSivpZMV
Added: _$ss7Float16V17_infinityExponentSuvpZMV
Added: _$ss7Float16V19significandBitCountSivpZMV
Added: _$ss7Float16V20leastNormalMagnitudeABvpZMV
Added: _$ss7Float16V21leastNonzeroMagnitudeABvpZMV
Added: _$ss7Float16V23greatestFiniteMagnitudeABvpZMV
Added: _$ss7Float16V2piABvpZMV
Added: _$ss7Float16V3nanABvpZMV
Added: _$ss7Float16V8infinityABvpZMV
Added: _$ss7Float16V8quietNaNABvpZMV
Added: _$ss7Float16V8ulpOfOneABvpZMV
Added: _$ss7UInt128V3maxABvpZMV
Added: _$ss7UInt128V3minABvpZMV
Added: _$ss7UInt128V4zeroABvpZMV
@@ -729,11 +714,7 @@ Added: _$ss7UnicodeO23CanonicalCombiningClassV7overlayADvpZMV
Added: _$ss7UnicodeO23CanonicalCombiningClassV9aboveLeftADvpZMV
Added: _$ss7UnicodeO23CanonicalCombiningClassV9belowLeftADvpZMV
Added: _$ss7UnicodeO4UTF8O27encodedReplacementCharacters06_ValidB6BufferVvpZMV
Added: _$ss7UnicodeO5ASCIIO27encodedReplacementCharacters15CollectionOfOneVys5UInt8VGAdded: vpZMV
Added: _$ss7UnicodeO5UTF16O20_replacementCodeUnits6UInt16VvpZMV
Added: _$ss7UnicodeO5UTF16O27encodedReplacementCharacters11_UIntBufferVys6UInt16VGvpZAdded: MV
Added: _$ss7UnicodeO5UTF32O20_replacementCodeUnits6UInt32VvpZMV
Added: _$ss7UnicodeO5UTF32O27encodedReplacementCharacters15CollectionOfOneVys6UInt32VAdded: GvpZMV
Added: _$ss8DurationV4zeroABvpZMV
Added: _$ss8SIMDMaskVss5SIMD2VySiGRszrlE7allTrueAByAEGvpZMV
Added: _$ss8SIMDMaskVss5SIMD2Vys4Int8VGRszrlE7allTrueAByAGGvpZMV
@@ -770,3 +751,24 @@ Added: _$ss8SIMDMaskVss6SIMD64Vys4Int8VGRszrlE7allTrueAByAGGvpZMV
Added: _$ss8SIMDMaskVss6SIMD64Vys5Int16VGRszrlE7allTrueAByAGGvpZMV
Added: _$ss8SIMDMaskVss6SIMD64Vys5Int32VGRszrlE7allTrueAByAGGvpZMV
Added: _$ss8SIMDMaskVss6SIMD64Vys5Int64VGRszrlE7allTrueAByAGGvpZMV
Added: _$sSo19_SwiftStdlibVersionasE6v6_1_0ABvpZMV
Added: _$ss7Float80V12signalingNaNABvpZMV
Added: _$ss7Float80V13_exponentBiasSuvpZMV
Added: _$ss7Float80V13_quietNaNMasks6UInt64VvpZMV
Added: _$ss7Float80V16_explicitBitMasks6UInt64VvpZMV
Added: _$ss7Float80V16_significandMasks6UInt64VvpZMV
Added: _$ss7Float80V16exponentBitCountSivpZMV
Added: _$ss7Float80V17_infinityExponentSuvpZMV
Added: _$ss7Float80V19significandBitCountSivpZMV
Added: _$ss7Float80V20leastNormalMagnitudeABvpZMV
Added: _$ss7Float80V21leastNonzeroMagnitudeABvpZMV
Added: _$ss7Float80V23greatestFiniteMagnitudeABvpZMV
Added: _$ss7Float80V2piABvpZMV
Added: _$ss7Float80V3nanABvpZMV
Added: _$ss7Float80V8infinityABvpZMV
Added: _$ss7Float80V8quietNaNABvpZMV
Added: _$ss7Float80V8ulpOfOneABvpZMV
Added: _$ss7UnicodeO5ASCIIO27encodedReplacementCharacters15CollectionOfOneVys5UInt8VGvpZMV
Added: _$ss7UnicodeO5UTF16O20_replacementCodeUnits6UInt16VvpZMV
Added: _$ss7UnicodeO5UTF16O27encodedReplacementCharacters11_UIntBufferVys6UInt16VGvpZMV
Added: _$ss7UnicodeO5UTF32O27encodedReplacementCharacters15CollectionOfOneVys6UInt32VGvpZMV

View File

@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -enable-experimental-feature KeyPathWithStaticMembers
var global = 42

View File

@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -typecheck -parse-as-library %s -verify
// RUN: %target-swift-frontend -typecheck -enable-experimental-feature KeyPathWithStaticMembers -parse-as-library %s -verify
struct Sub: Hashable {
static func ==(_: Sub, _: Sub) -> Bool { return true }