Skip to content

Sema: Check availability of key path components [4.2] #17780

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
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
7 changes: 6 additions & 1 deletion lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6949,13 +6949,18 @@ static bool diagnoseKeyPathComponents(ConstraintSystem &CS, KeyPathExpr *KPE,

// Drop non-property, non-type candidates.
if (!isa<VarDecl>(result.getValueDecl()) &&
!isa<TypeDecl>(result.getValueDecl()))
!isa<TypeDecl>(result.getValueDecl()) &&
!isa<SubscriptDecl>(result.getValueDecl()))
return false;

return true;
});
}

// If all results were unavailable, fail.
if (!lookup)
break;

// If we *still* have more than one result, fail.
if (lookup.size() > 1) {
// Don't diagnose ambiguities if the results are from typo correction.
Expand Down
30 changes: 29 additions & 1 deletion lib/Sema/TypeCheckAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2306,6 +2306,9 @@ class AvailabilityWalker : public ASTWalker {
maybeDiagStorageAccess(S->getDecl().getDecl(), S->getSourceRange(), DC);
}
}
if (auto KP = dyn_cast<KeyPathExpr>(E)) {
maybeDiagKeyPath(KP);
}
if (auto A = dyn_cast<AssignExpr>(E)) {
walkAssignExpr(A);
return skipChildren();
Expand Down Expand Up @@ -2399,7 +2402,32 @@ class AvailabilityWalker : public ASTWalker {
// Diagnose for appropriate accessors, given the access context.
maybeDiagStorageAccess(D, E->getSourceRange(), DC);
}


/// Walk a keypath expression, checking all of its components for
/// availability.
void maybeDiagKeyPath(KeyPathExpr *KP) {
for (auto &component : KP->getComponents()) {
switch (component.getKind()) {
case KeyPathExpr::Component::Kind::Property:
case KeyPathExpr::Component::Kind::Subscript: {
auto *decl = component.getDeclRef().getDecl();
auto loc = component.getLoc();
SourceRange range(loc, loc);
diagAvailability(decl, range, nullptr);
break;
}

case KeyPathExpr::Component::Kind::Invalid:
case KeyPathExpr::Component::Kind::UnresolvedProperty:
case KeyPathExpr::Component::Kind::UnresolvedSubscript:
case KeyPathExpr::Component::Kind::OptionalChain:
case KeyPathExpr::Component::Kind::OptionalWrap:
case KeyPathExpr::Component::Kind::OptionalForce:
break;
}
}
}

/// Walk an inout expression, checking for availability.
void walkInOutExpr(InOutExpr *E) {
walkInContext(E, E->getSubExpr(), MemberAccessContext::InOut);
Expand Down
10 changes: 10 additions & 0 deletions test/attr/attr_inlinable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,13 @@ public struct PublicFixedStructWithInit {
var x = internalGlobal // expected-error {{let 'internalGlobal' is internal and cannot be referenced from a property initializer in a '@_fixed_layout' type}}
var y = publicGlobal // OK
}

public struct KeypathStruct {
var x: Int
// expected-note@-1 {{var 'x' is not '@usableFromInline' or public}}

@inlinable public func usesKeypath() {
_ = \KeypathStruct.x
// expected-error@-1 {{var 'x' is internal and cannot be referenced from an '@inlinable' function}}
}
}
58 changes: 58 additions & 0 deletions test/expr/unary/keypath/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ struct C<T> {
subscript<X>(noHashableConstraint sub: X) -> X { get { return sub } set { } }
}

struct Unavailable {
@available(*, unavailable)
var unavailableProperty: Int
// expected-note@-1 {{'unavailableProperty' has been explicitly marked unavailable here}}

@available(*, unavailable)
subscript(x: Sub) -> Int { get { } set { } }
// expected-note@-1 {{'subscript' has been explicitly marked unavailable here}}
}

struct Deprecated {
@available(*, deprecated)
var deprecatedProperty: Int

@available(*, deprecated)
subscript(x: Sub) -> Int { get { } set { } }
}

@available(*, deprecated)
func getDeprecatedSub() -> Sub {
return Sub()
}

extension Array where Element == A {
var property: Prop { fatalError() }
}
Expand Down Expand Up @@ -178,6 +201,14 @@ func testKeyPath(sub: Sub, optSub: OptSub,
let _ = \C<Int>.[sub]
let _ = \C<Int>.[noHashableConstraint: sub]
let _ = \C<Int>.[noHashableConstraint: nonHashableSub] // expected-error{{subscript index of type 'NonHashableSub' in a key path must be Hashable}}

let _ = \Unavailable.unavailableProperty // expected-error {{'unavailableProperty' is unavailable}}
let _ = \Unavailable.[sub] // expected-error {{'subscript' is unavailable}}

let _ = \Deprecated.deprecatedProperty // expected-warning {{'deprecatedProperty' is deprecated}}
let _ = \Deprecated.[sub] // expected-warning {{'subscript' is deprecated}}

let _ = \A.[getDeprecatedSub()] // expected-warning {{'getDeprecatedSub()' is deprecated}}
}

func testKeyPathInGenericContext<H: Hashable, X>(hashable: H, anything: X) {
Expand Down Expand Up @@ -471,6 +502,33 @@ func testImplicitConversionInSubscriptIndex() {
_ = \BassSubscript.["hello"] // expected-error{{must be Hashable}}
}

// Crash in diagnostics
struct AmbiguousSubscript {
subscript(sub: Sub) -> Int { get { } set { } }
// expected-note@-1 {{'subscript' declared here}}

subscript(y y: Sub) -> Int { get { } set { } }
// expected-note@-1 {{'subscript(y:)' declared here}}
}

func useAmbiguousSubscript(_ sub: Sub) {
let _: PartialKeyPath<AmbiguousSubscript> = \.[sub]
// expected-error@-1 {{ambiguous reference to member 'subscript'}}
}

struct BothUnavailableSubscript {
@available(*, unavailable)
subscript(sub: Sub) -> Int { get { } set { } }

@available(*, unavailable)
subscript(y y: Sub) -> Int { get { } set { } }
}

func useBothUnavailableSubscript(_ sub: Sub) {
let _: PartialKeyPath<BothUnavailableSubscript> = \.[sub]
// expected-error@-1 {{type of expression is ambiguous without more context}}
}

// SR-6106
func sr6106() {
class B {}
Expand Down