Commit Graph

140 Commits

Author SHA1 Message Date
Mark Lacey
83ebd71644 Allowing forming WritableKeyPath to read-only value in Swift 3/4.
We inadvertantly allowed this in the past, so continue to do so when
compiling in Swift 3/4 mode to avoid suddenly breaking existing code.

The diagnostic here is pretty bad, and I've opened issues for that as
well as providing some kind of deprecation warning for this so that
even under Swift 3/4 mode we alert users that this is unsupported.

rdar://problem/39802797
2018-05-08 14:26:02 -07:00
Mark Lacey
9a7497aa17 Opportunistic fix to allow \Derived.property to be inferred as \Base.property.
Noticed this opportunity while reading through the keypath type checking code.
2018-04-12 09:55:40 -07:00
Joe Groff
6d053f5a9c Sema: Fix setter visibility computation when checking key path literals.
`AbstractStorageDecl::isSettable` by itself doesn't account for access control, so we need to check `isSetterAccessibleFrom` as well. Furthermore, key paths should not get the special context-sensitive privileges direct accesses do, such as initializers being able to assign into `let` properties. Fix the type checking logic here to only create a statically writable KeyPath value when the referenced declaration has a setter visible independent of current context. Fixes SR-6667 | rdar://problem/36257266.
2018-04-09 11:06:29 -07:00
gregomni
90f0edc009 Need to check that the keypath decl is an AbstractStorageDecl instead of assuming it. 2018-01-14 15:05:44 -08:00
Joe Groff
73d2e9a47f Sema: Fixes for KeyPath type checking.
When testing KeyPathApplication constraints, we would keep going after rejecting a concrete KeyPath application by trying PartialKeyPath and AnyKeyPath, even though that's not what we want, since any key path application expression can type check with an AnyKeyPath. We would then miscompile by building the AST such that we applied the mismatched key path expression directly to the base. We also didn't handle expressions where the base was a subtype of the key path's base type correctly—the conversion means the base can't be written through in this situation, and we hardcoded the concrete-to-existential case instead of handling general conversions. Fix these problems, and add an AST verifier for KeyPathApplicationExprs to help catch problems in the future. Fixes SR-6300 | rdar://problem/35368903.
2017-11-09 11:57:23 -08:00
gregomni
a76d74b6d4 When subscripting with a keypath, check for the path being an IUO. 2017-10-21 16:12:28 -07:00
gregomni
0188e1cb65 Incorrect condition here resulted in incorrectly trying to optional-wrap for an optional chain to optional lvalue. 2017-10-15 15:31:06 -07:00
Joe Groff
d3a2a0ff82 Sema: Coerce the type of the index expression in a key path component to match the subscript decl's index type.
Fixes SR-6062 | rdar://problem/34835322.
2017-10-05 14:33:21 -07:00
Robert Widmann
e310ab3e3c Coerce the base of existential keypath applies
We can wind up in a situation where the base of a keypath
application is concrete but is being applied to a keypath
with an existential source type.  Coerce the base to the
intended type if necessary when building the final subscript
expression.

Resolves SR-5878.
2017-09-17 16:52:49 -04:00
Joe Groff
2ba8f57514 Remove staging flag for unimplemented key path components.
All the originally-planned component kinds are now implemented.
2017-09-15 11:55:35 -07:00
Joe Groff
a67f9ca3e5 Sema: Require indexes in key path subscript components to be Hashable.
And save the conformance to be captured when we form the KeyPath object.
2017-09-15 09:59:51 -07:00
Joe Groff
605804cd17 Sema: Don't allow key path literals to refer to static members.
This is theoretically supportable some day, but isn't implemented today. Fixes rdar://problem/33318422 | SR-5453.
2017-07-19 15:27:43 -07:00
Joe Groff
6ad01d63f6 Sema: Feed argument label and constraint locator info from key path subscript components into getCalleeDeclAndArgs.
Fixes SR-5189 | rdar://problem/32713662.
2017-07-10 14:55:08 -07:00
Joe Groff
04453d1ddc Merge pull request #10810 from jckarter/keypath-apply-lvalue
Sema: Coerce [keyPath:] index to rvalue.
2017-07-07 13:50:08 -07:00
Joe Groff
a3c6dbc469 Sema: Coerce [keyPath:] index to rvalue.
Fixes SR-5384 | rdar://problem/33160409.
2017-07-07 10:31:32 -07:00
Joe Groff
2bb827340b Sema: Simplifying a KeyPathExpr's type should *bind* to the specific type, not accept a subtype constraint.
We want the type of a KeyPathExpr to be the specific *KeyPath<T, U> subclass appropriate for the literal, with upcasts to a more general contextual type, since we rely on that invariant elsewhere to extract the base and projected value types. Fixes SR-5008 | rdar://problem/32395076.
2017-07-05 19:15:45 -07:00
Joe Groff
3c82e981f9 KeyPaths: Add support for optional chaining/forcing components.
rdar://problem/31768715
2017-06-26 09:40:31 -07:00
Mark Lacey
03962716ca Update tests of preferring subscript over keypath application.
Add an explicit argument label to the subscript declarations used in the
tests so that x[keyPath:] refers to the subscript rather than the
keypath.

Update test output accordingly.
2017-05-31 09:05:20 -07:00
Mark Lacey
e539428ceb Update SK_LastScoreKind to SK_KeyPathSubscript.
When this score kind was added, the marker for the last score kind was
not updated. As a result, increasing the score for SK_KeyPathSubscript
was not actually having an effect.

Add tests that include a type with subscripts that also take the key
paths as their argument with an identical argument label ("keyPath").

These tests show that we're actually falling back on the keypath
application in many cases despite the score kind specific to keypath
application. I'll open a couple new JIRAs to investigate this
behavior.

The update to SK_LastScoreKind results in fixing a crash in these tests
that happens as a result of an assumption in CSRanking.cpp that if we're
comparing two solutions we end up with decls for overloads to compare,
which isn't the case here due to the keypath application.
2017-05-30 21:02:01 -07:00
Joe Groff
cdc7a5c945 Support application of AnyKeyPath/PartialKeyPath.
rdar://problem/32237567
2017-05-25 15:51:22 -07:00
Joe Groff
a6fb6d6f15 Sema: Don't crash when key path literals appear in PartialKeyPath context.
And make an attempt to use the constraint system's contextualType to bind type information; this makes things better but doesn't seem to be a complete solution for contextually typing key paths.
2017-05-24 11:31:01 -07:00
Joe Groff
879397008c Sema: Don't crash when recovering type errors from malformed keypath expressions.
It's particularly likely someone will try to type `\(foo)`, which looks like a string interpolation segment, outside of a string literal, so give that case a special diagnostic. Fixes rdar://problem/32315365.
2017-05-22 10:42:40 -07:00
Joe Groff
faa6bc72f0 Sema: Reject unimplemented key path components during resolveKeyPathExpr.
This is a bit more robust and user-friendly than hoping more brittle recovery in SILGen or IRGen for unsupported components kicks in. rdar://problem/32200714
2017-05-15 16:10:59 -07:00
Joe Groff
ba7a5a80dd Sema: Don't crash when a tuple element is referenced in a key path.
Fixes rdar://problem/32200643. It'd be nice to have a targeted diagnostic too…
2017-05-15 14:12:41 -07:00
Mark Lacey
15fb957f09 Fix some LValue type checking issues exposed by key paths.
We had an inconsistency in the handling of ConstraintKind::Equal in that
we would take
  $T1 Equal $T2
where $T2 was previously bound to a type, and bind the RValue type of
$T2's type to $T1.  That does not allow for us to later attempt to bind
the LValue type of that type to $T1 (as might happen in simplifying an
OptionalObject constraint).

Instead, if $T1 can be bound to an LValue and $T2 is not an LValue,
we'll defer simplifying the Equal constraint until after $T1 is bound
through some other type variable binding or constraint simplification.

Fixes rdar://problem/31724272.
2017-05-08 18:07:49 -07:00
Mark Lacey
f4debf8a35 Fix keypath test.
The delcared type was incorrect here as opposed to the type checker
incorrectly diagnosing a problem. Fix the test and add another test
without a subscript operation to ensure that it works.
2017-05-08 12:38:44 -07:00
Huon Wilson
07c5ab8fb2 Implement \ syntax for Swift key paths.
This introduces a few unfortunate things because the syntax is awkward.
In particular, the period and following token in \.[a], \.? and \.! are
token sequences that don't appear anywhere else in Swift, and so need
special handling. This is somewhat compounded by \foo.bar.baz possibly
being \(foo).bar.baz or \(foo.bar).baz (parens around the type), and,
furthermore, needing to distinguish \Foo?.bar from \Foo.?bar.

rdar://problem/31724243
2017-05-01 16:06:15 -07:00
Joe Groff
cdb54ccadf Put keypaths behind a flag. 2017-04-19 20:39:11 -07:00
Joe Groff
7eebb27153 Sema: Infer the specific type of a key path literal from the mutability of the resolved components. 2017-04-10 16:06:40 -07:00
Joe Groff
964dc0e174 Sema: (wip) Overload resolution for keypath subscripts.
TODO: Some diagnostic regressions:
test-macosx-x86_64/Compatibility/tuple_arguments.swift
test-macosx-x86_64/Constraints/diagnostics.swift
test-macosx-x86_64/Constraints/tuple_arguments.swift
test-macosx-x86_64/expr/unary/keypath/keypath.swift
test-macosx-x86_64/expr/unary/selector/selector.swift
2017-04-09 16:38:02 -07:00
Joe Groff
fc23781906 Sema: First pass at type-checking Swift key paths.
TODO:

- Select the KeyPath subclass corresponding to the write capability of the key path components
- Figure out an issue with unresolved solutions being chosen with contextually-typed keypaths
- Diagnostic QoI
2017-04-04 11:31:15 -07:00
Joe Groff
e3046d6f75 Parsing for native keypaths.
Use `#keyPath2` as a stand-in for the final syntax.
2017-04-04 11:31:15 -07:00
Joe Groff
a5ca6ccd61 Restructure KeyPathExpr to allow more kinds of components.
Expand the representation in anticipation of more kinds of components. NFC intended yet.
2017-04-04 11:31:15 -07:00
Rintaro Ishizaki
590ac72316 [Parse] Parse subscript with '#' expressions
Fixes: https://bugs.swift.org/browse/SR-3439
2017-01-27 19:59:55 +09:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Doug Gregor
d225a2177d [Type checker] Use proper lookup type in substitutions for #keyPath.
Fixes crasher in rdar://problem/28061409.
2016-09-21 22:40:58 -07:00
Doug Gregor
823c24b355 [SE-0112] Rename ErrorProtocol to Error.
This is bullet (5) of the proposed solution in SE-0112, and the last
major piece to be implemented.
2016-07-12 10:53:52 -07:00
John McCall
3a23c08549 Implement typo correction for #keypath expressions. 2016-07-01 15:09:49 -07:00
practicalswift
ef4f925977 [gardening] Fix recently introduced typos. 2016-05-21 13:02:00 +02:00
Doug Gregor
9f0cec4984 SE-0062: Implement #keyPath expression.
Implement the Objective-C #keyPath expression, which maps a sequence
of @objc property accesses to a key-path suitable for use with
Cocoa[Touch]. The implementation handles @objc properties of types
that are either @objc or can be bridged to Objective-C, including the
collections that work with key-value coding (Array/NSArray,
Dictionary/NSDictionary, Set/NSSet).

Still to come: code completion support and Fix-Its to migrate string
literal keypaths to #keyPath.

Implements the bulk of SR-1237 / rdar://problem/25710611.
2016-05-18 23:30:15 -07:00