Commit Graph

1005 Commits

Author SHA1 Message Date
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
David Rönnqvist
57731ebc09 [QoI] [Parse] Improve error message when parsing floating point exponent
Update error messages to mention the invalid character.
Improve the diagnostic of floating point exponents.

Add tests for error messages when parsing floating point exponents.
Update existing tests for new error messages.
2017-07-08 13:32:34 +02: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
David Rönnqvist
a615d9ede3 [QoI] Improve error message when parsing integer literal
Rephrased error message to indicate which character is unexpected.
Provide error message variations when parsing binary, octal, decimal (default), and hexadecimal integer literals.
Look for unexpected digits in binary and octal integer literals.
Look for unexpected letters in hex integer literals.

Resolves: SR-5236 rdar://problem/32858684
2017-07-06 19:59:09 +02: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
Pavel Yaskevich
c46e3e83e8 [QoI] Improve diagnostics for single argument calls with trailing closures
Calls involving single trailing closure arguments require special
handling because we don't have as much contextual information
about function/argument types as in with regular calls, which means
that diagnosing such situations only by `visitApplyExpr`
yields subpar results.

Resolves: SR-4836.
2017-06-21 11:26:38 -07:00
John McCall
c131ea4d18 Implement the restriction on calls to non-escaping function parameters from the SE-0176 revision. 2017-06-20 17:29:20 -04:00
Joe Groff
f262ced6e6 Sema: Track writes through WritableKeyPaths for mutation warnings.
Previously we would erroneously flag a `var` as not being mutated if the only mutations were through WritableKeyPaths. Fixes SR-5214 | rdar://problem/32599483.
2017-06-16 12:44:52 -07:00
Alex Hoppen
e7da52776c Merge pull request #10152 from ahoppen/fixit-optional-switch
[Diag] Correct fixit location of "?" when pattern matching optional with non-optional
2017-06-07 10:59:54 -07:00
Alex Hoppen
99acf816f4 [Diag] Correct fixit location of "?" when pattern matching optional with non-optional
Trying to pattern match an optional with a non-optional should provide
a fixit inserting a "?" after the last token and not before it.
2017-06-07 07:25:46 -07:00
Dmitri Gribenko
984210aa53 tests: replace '// RUN: rm -rf' '// RUN: mkdir' pairs with '%empty-directory(...)'
These changes were made using a script.
2017-06-04 11:08:39 -07:00
Slava Pestov
b639f7fd83 Sema: Fix conditional downcasts from Swift types to CF types
Conditional and forced downcasts enter a constraint that almost
always succeeds; only when applying the solution do we evaluate
the feasability of the cast and determine if it always succeeds,
always fails, or conditionally succeeds. This changes how the
resulting AST is represented and can also emit diagnostics.

If the conditional cast is at this stage determined to always
succeed, we treat it as an unconditional cast, going through
ExprRewriter::coerceToType() to build the AST for the coercion.

However conditional cast constraints don't enter the same
restrictions into the solution as unconditional casts do, so
coerceToType() would fall over if casting a Swift type to a CF
type by first bridging the Swift type to Objective-C.

Get around this by checking for this case explicitly when
lowering a CoerceExpr.

It feels like there's a more fundamental issue here with how
casts are modeled in the constraint solver, but I'm not going
to try understanding that now.

Fixes <rdar://problem/32227571>.
2017-06-01 21:44:07 -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
8022266404 Merge pull request #9933 from jckarter/partial-key-path-application
Support application of AnyKeyPath/PartialKeyPath.
2017-05-26 08:45:55 -07:00
Joe Groff
cdc7a5c945 Support application of AnyKeyPath/PartialKeyPath.
rdar://problem/32237567
2017-05-25 15:51:22 -07:00
Slava Pestov
fa155bf1d1 Sema: Rework typeCheckBinding() to use the new foundSolution() callback
Record the initializer type as soon as we have a solution, before
it is applied, and get the type from the constriant system instead
of from the final type checked expression.

Note that the coerceToMaterializable() was unnecessary, since we
always coerce the value to an rvalue type with coerceToType().

Eventually coerceToMaterializable() should go away.

This is mostly NFC, except using the result of simplifyType() rather
than the type of the final expression changes some diagnostics where it
appears we were previously losing sugar.

Also this accidentally fixes a crasher. Unfortunately the underlying
issue is still there (applying a solution has bugs with opened
existentials "leaking" out) -- this merely masks the problem by
getting the initializer type directly from the constriant system.
2017-05-24 17:21:28 -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
9157372431 Merge pull request #9840 from jckarter/already-checked-key-path
Sema: Don't crash when type-checking a KeyPathExpr that's already type-checked.
2017-05-23 15:11:51 -07:00
Joe Groff
2425258593 Sema: Don't crash when type-checking a KeyPathExpr that's already type-checked.
This can come up when re-checking a larger expression for diagnostics. Fixes SR-4965.
2017-05-23 14:00:32 -07:00
Pavel Yaskevich
6d160482e8 [QoI] Improve diagnostic when unsupported tuple element references are used in key path literals
Resolves: rdar://problem/32209039.
2017-05-22 14:53:02 -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
Alex Hoppen
18128308e4 Merge pull request #9294 from ahoppen/SR-4785-fixit-missing-type
[Parser] Add fix-it for missing type errors
2017-05-20 08:26:52 +01:00
Maxim Moiseev
130231456b Merge pull request #9714 from moiseev/int-perf
[stdlib] Speeding up typechecking for complex expressions with integers
2017-05-17 17:33:47 -07:00
Max Moiseev
fa0904f1e5 [stdlib] Adding operator overloads on concrete integer types
It looks like the compiler is having troubles with the pattern when the
non-mutaing operator is defined on the protocol, and delegates to the
mutating version, that is provided by the concrete type. Adding similar
definitions of non-mutating operators to concrete types significantly
speeds up the typechecking of complex expressions, like the one in
the ByteSwap benchmark.
2017-05-17 15:56:17 -07:00
Pavel Yaskevich
573f20e55c [QoI] Unify member diagnostics into one FailureDiagnosis method 2017-05-17 12:00:23 -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
David Farler
0c48f71384 Migrator/QoI: Function input type: Don't fix Void to (Void), but fix (Void) to ()
```swift
func foo(f: Void) -> ()) {}
```

This compiler currently suggests we change this to:

```swift
func foo(f: (Void) -> ()) {}
```

That's `(()) -> ()`, almost certainly not what someone wants in Swift
4. We should suggest changing that to:
```swift
func foo(f: () -> ()) {}
```

Additionally,

```swift
func foo(f: (Void) -> ()) {}
```

Should trigger a warning that the `(Void)` input type is `(())`, and you
can't supply `()` to it in Swift 4, and suggest a fix-it change it to:

```swift
func foo(f: () -> ()) {}
```

rdar://problem/32143617
2017-05-13 17:36:28 -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
Joe Groff
cf6cc484c0 Switch on key paths.
Enabling the global subscript degrades some diagnostics (rdar://problem/31724211).
2017-05-05 08:32:41 -07:00
Alex Hoppen
adbe86243e [Parser] Add fix-it for missing type errors
We can't reliably decide in the parser if a type was forgotten or a
wrong type was meant to be a type (e.g. `let x: class MyClass`).
This patch applies a heuristic that the parameter was most likely
forgotten if the next character is a closing bracket or a semantic
separator.
This catches the most common cases of function parameters and variable
declarations that are immediately initialised.

This fixes SR-4785.
2017-05-04 21:43:58 +01:00
Huon Wilson
4fc27a1f3e Merge pull request #9150 from huonw/keypath-syntax
Implement \ syntax for Swift key paths.
2017-05-03 06:22:47 +10: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
Doug Gregor
b6494f6daf Merge pull request #4785 from jrose-apple/trailing-closures-and-default-arguments
When ranking overloads, always match up final closure parameters.
2017-05-01 09:53:09 -07:00
Robert Widmann
4e2f36c763 Lift case redundancy checks into Sema 2017-05-01 01:32:02 -04:00
Robert Widmann
39494b2ba2 Rearrange test code for exhaustiveness 2017-04-28 02:06:39 -04:00
Jordan Rose
27b4070f5f When ranking overloads, always match up final closure parameters.
(in Swift 4 mode)

There are a lot of other things that overload ranking does *not* take
into account, and I intend to file a more general bug about that, but
this should resolve some of the most egregious ambiguities with Swift
3's import rules (SE-0005, particularly "omit needless words" and
adding default arguments).

Finishes rdar://problem/25607552.
2017-04-26 19:33:07 -07:00
Andrew Bennett
ca31338e49 Simplifying implementation of ExpressibleByStringLiteral (#7125)
* Simplify conforming to ExpressibleByStringLiteral with default implementations

* attributes on default implementations

* ExpressibleByUnicodeScalarLiteral validation test

* more generic default implementations

* clean up test

* remove unneeded implementations

* remove test verification

* indent

* revert @effects and affected methods

* fix test generics with _ protocols

* Add semantic tests

* clean up tests

* Fix redundant conformance requirements
2017-04-21 20:45:28 -07:00
Joe Groff
3d178be169 Merge pull request #8875 from jckarter/keypaths
Keypaths
2017-04-21 17:51:17 -07:00
Slava Pestov
eb8b1ab3e1 Sema: Casts between unrelated optional types may succeed
... if the value is 'nil'!

Fixes <https://bugs.swift.org/browse/SR-3505>.
2017-04-20 00:37:39 -07:00
Slava Pestov
b1177f0413 Sema: Fix bogus "always fails" warnings with casts between generic classes
We would misreport a cast from G<T> to G<Int> or vice versa
as always failing, because we were checking for an exact
subtype relationship instead of archetype binding.

Fixes <https://bugs.swift.org/browse/SR-3609>.
2017-04-20 00:37:39 -07:00
Joe Groff
cdb54ccadf Put keypaths behind a flag. 2017-04-19 20:39:11 -07:00
Joe Groff
595e0e4ede Merge branch 'master' into keypaths 2017-04-19 18:38:24 -07:00
Max Moiseev
fe4553eb80 Merge branch 'master' into new-integer-protocols 2017-04-11 17:07:11 -07:00
Slava Pestov
de323b5bef Sema: Update resolveType() for subclass existentials
If the -enable-experimental-subclass-existentials staging flag
is on, resolveType() now allows protocol compositions to contain
class types. It also diagnoses if a composition has more than one
superclass requirement.

Also, change diagnostics that talked about 'protocol composition'
to 'protocol-constrained type'.

Since such types can now contain a superclass constraint, it's not
correct to call them protocol composition.

"Protocol-constrained type" isn't quite accurate either because
'Any' has no protocols, and 'AnyObject' will have no protocols but
a general class constraint; but those are edge cases which won't
come up in these diagnostics.
2017-04-10 17:11:07 -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