Commit Graph

237 Commits

Author SHA1 Message Date
Chris Lattner
ea52d4fbc7 fix <rdar://problem/20749592> Conditional Optional binding hides compiler error
Swift SVN r28220
2015-05-06 22:12:34 +00:00
Dmitri Hrybenko
6a15456cec Eliminate uses of println() where it is irrelevant
Swift SVN r28172
2015-05-05 18:21:39 +00:00
Chris Willmore
331b570329 Remove reverse subtype constraint between Objective-C classes when type
checking checked cast via bridging. It prevented bridging upcasts using
'as!' from typechecking; we should emit an 'as!'->'as' warning instead.

Also, use ExplicitConversion constraint instead of Conversion when
determining whether a checked cast can be carried out unconditionally.
This matches the constraint used after applying the 'as!'->'as' fixit.

(Also, fix the error that was responsible for breaking
the expr/cast/bridged.swift test.)

<rdar://problem/19813772>

Swift SVN r28034
2015-05-01 08:27:19 +00:00
Ted Kremenek
acf0c496e2 Revert "Remove reverse subtype constraint between Objective-C classes when type"
This is breaking TEST 'Swift :: expr/cast/bridged.swift'.

Swift SVN r28030
2015-05-01 06:37:59 +00:00
Chris Willmore
7d413057aa Remove reverse subtype constraint between Objective-C classes when type
checking checked cast via bridging. It prevented bridging upcasts using
'as!' from typechecking; we should emit an 'as!'->'as' warning instead.

Also, use ExplicitConversion constraint instead of Conversion when
determining whether a checked cast can be carried out unconditionally.
This matches the constraint used after applying the 'as!'->'as' fixit.

<rdar://problem/19813772>

Swift SVN r28028
2015-05-01 06:21:50 +00:00
Chris Lattner
31c01eab73 Change the meaning of "if let x = foo()" back to Xcode 6.4 semantics. The compiler
includes a number of QoI things to help people write the correct code.  I will commit
the testcase for it as the next patch.

The bulk of this patch is moving the stdlib, testsuite and validation testsuite to
the new syntax.  I moved a few uses of "as" patterns back to as? expressions in the 
stdlib as well.



Swift SVN r27959
2015-04-30 04:38:13 +00:00
Dave Abrahams
27448f174f [stdlib] == and != for metatypes and optional metatypes
Swift SVN r27933
2015-04-29 22:19:01 +00:00
Chris Lattner
43c7334abc fix two QoI issues:
- <rdar://problem/16306600> QoI: passing a 'let' value as an inout results in an unfriendly diagnostic
 - <rdar://problem/16927246> provide a fixit to change "let" to "var" if needing to mutate a variable

We now refer to an inout argument as such, e.g.:

t.swift:7:9: error: cannot pass 'let' value 'a' as inout argument
  swap(&a, &b)
        ^

we also produce a note with a fixit to rewrite let->var in trivial cases where mutation is
being assed for, e.g.:

t.swift:3:3: note: change 'let' to 'var' to make it mutable
  let a = 42
  ^~~
  var

The note is produced by both Sema and DI.



Swift SVN r27774
2015-04-26 21:51:50 +00:00
Chris Lattner
bebbdfe001 fix <rdar://problem/17691565> attempt to modify a 'let' variable with ++ results in typecheck error not being able to apply ++ to Float
We now produce diagnostics like:
 - cannot pass 'let' value 'a' to mutating unary operator '++'
 - cannot pass get-only property 'b' to mutating unary operator '++'
 - cannot pass immutable value of type 'Int64' to mutating unary operator '++'



Swift SVN r27772
2015-04-26 20:42:19 +00:00
Doug Gregor
b2cc34c241 Remove '#' for making parameter names into argument labels.
If you want to make the parameter and argument label the same in
places where you don't get the argument label for free (i.e., the
first parameter of a function or a parameter of a subscript),
double-up the identifier:

  func translate(dx dx: Int, dy: Int) { }

Make this a warning with Fix-Its to ease migration. Part of
rdar://problem/17218256.

Swift SVN r27715
2015-04-24 23:58:57 +00:00
Doug Gregor
793b3326af Implement the new rules for argument label defaults.
The rule changes are as follows:
  * All functions (introduced with the 'func' keyword) have argument
  labels for arguments beyond the first, by default. Methods are no
  longer special in this regard.
  * The presence of a default argument no longer implies an argument
  label.

The actual changes to the parser and printer are fairly simple; the
rest of the noise is updating the standard library, overlays, tests,
etc.

With the standard library, this change is intended to be API neutral:
I've added/removed #'s and _'s as appropriate to keep the user
interface the same. If we want to separately consider using argument
labels for more free functions now that the defaults in the language
have shifted, we can tackle that separately.

Fixes rdar://problem/17218256.

Swift SVN r27704
2015-04-24 19:03:30 +00:00
Doug Gregor
750566b249 Implicitly create type declarations for inferred associated type witnesses.
The type checker (and various other parts of the front end) jump
through many hoops to try to cope with the lack of a proper
declaration for an inferred type witness, causing various annoying
bugs. Additionally, we were creating implicit declarations for
derived/synthesized witnesses, leading to inconsistent AST
representations. This ch

Note that we'll now end up printing the inferred type aliases for type
witnesses, which represents a reversal of the decision that closed
rdar://problem/15168378. This result is more consistent.

Now with a simpler accessibility computation.

Swift SVN r27512
2015-04-21 00:21:55 +00:00
Doug Gregor
9e68a5761a Revert "Implicitly create type declarations for inferred associated type witnesses."
This reverts r27487; it's breaking one of the bots.

Swift SVN r27505
2015-04-20 22:56:50 +00:00
Doug Gregor
e907845d3e Implicitly create type declarations for inferred associated type witnesses.
The type checker (and various other parts of the front end) jump
through many hoops to try to cope with the lack of a proper
declaration for an inferred type witness, causing various annoying
bugs. Additionally, we were creating implicit declarations for
derived/synthesized witnesses, leading to inconsistent AST
representations. This ch

Note that we'll now end up printing the inferred type aliases for type
witnesses, which represents a reversal of the decision that closed
rdar://problem/15168378. This result is more consistent.

Swift SVN r27487
2015-04-20 18:10:57 +00:00
Ben Langmuir
082c5f8257 Fix expr/expressions test after r27479
Swift SVN r27481
2015-04-20 16:20:42 +00:00
Jordan Rose
700ca49f62 [test] Make sure tests that use a mock SDK don't pick up external overlays.
...take two.

rdar://problem/20474694

Swift SVN r27152
2015-04-09 01:02:30 +00:00
Doug Gregor
c5b9f265d5 -enable-source-import hacks for various tests that are sensitive to them
Swift SVN r26440
2015-03-23 19:00:53 +00:00
Chris Lattner
d46a289470 allow tok::period (not just tok::period_prefix) when starting a prefix unary operator
for a member element reference.  This improves error recovery and fixes cases where we'd
reject invalid code in unspaced situations (like "(.x)") this fixes rdar://20251513.


Swift SVN r26406
2015-03-22 05:24:00 +00:00
Chris Lattner
82956ede54 fix a QoI regression I introduced in r26224. If an initializer expr in a var/let decl
is invalid and produces a ParseError, recovery by producing an AST with an ErrorExpr in it
instead of dropping the initializer on the floor.  This silences downstream errors about
"must have an initializer" sorts of stuff.



Swift SVN r26405
2015-03-22 05:08:26 +00:00
Chris Lattner
59c22383fb Rework PatternBindingDecl to maintain a list of pattern/initexpr pairs inside of it.
Previously, a multi-pattern var/let decl like:
  var x = 4, y = 17

would produce two pattern binding decls (one for x=4 one for y=17).  This is convenient
in some ways, but is bad for source reproducibility from the ASTs (see, e.g. the improvements
in test/IDE/structure.swift and test/decl/inherit/initializer.swift).

The hardest part of this change was to get parseDeclVar to set up the AST in a way
compatible with our existing assumptions. I ended up with an approach that forms PBDs in 
more erroneous cases than before.  One downside of this is that we now produce a spurious
  "type annotation missing in pattern"
diagnostic in some cases.  I'll take care of that in a follow-on patch.





Swift SVN r26224
2015-03-17 16:14:18 +00:00
Chris Lattner
20f8f09ea8 Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.

Where before you might have written:
  if let x = foo() {

you now need to write:
  if let x? = foo() {
    
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.

To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?.  It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.

For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error.  This means that you'll get:

t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
       ^
        ?

I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time.  I filed 20166013 to remember to upgrade this to an error.

In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).




Swift SVN r26150
2015-03-15 07:06:22 +00:00
Jordan Rose
f54e3570a3 Preserve the location of the 'in' when doing void-conversion on a closure.
This location is used when deciding whether a capture has already been
initialized, and without it the compiler decides that the reference to
a name from the capture list should be rejected.

rdar://problem/19776255&20153574

Swift SVN r26103
2015-03-13 17:35:14 +00:00
Chris Willmore
b3e0264e08 "@lvalue" shouldn't appear in general conversion failure diagnostic.
rdar://problem/19836341

Swift SVN r25826
2015-03-07 01:07:42 +00:00
Dmitri Hrybenko
3890d64e49 Fix a crash in the type checker when trying to find an ObjC bridge
intrinsic when ObjC interop is absent

Investigated and fixed together with Graham.

Swift SVN r25621
2015-02-27 22:09:15 +00:00
Dave Abrahams
1badcbc14b [stdlib] Rename Slice => ArraySlice
Leave room for a generic Slice type in a later release.

Swift SVN r25493
2015-02-24 00:18:58 +00:00
Chris Willmore
4cf775f37a For class C and protocol P, C.Type is not convertible to P.Protocol.
Swift SVN r25376
2015-02-18 19:58:28 +00:00
Chris Willmore
fdf4c035e9 Only suggest appending 'as! T' if there's a chance that the conversion would succeed.
<rdar://problem/19831919> Fixit offers as! conversions that are known to always fail

Swift SVN r25346
2015-02-17 03:42:52 +00:00
Doug Gregor
1dbbf92660 Add a warning when a closure parameter is followed by defaulted parameters.
This finds cases where something that used to be a trailing closure is
no longer a trailing closure due to the recent rule change.

Swift SVN r25289
2015-02-14 00:23:38 +00:00
Chris Lattner
635d46d909 Fix <rdar://problem/19821633> only allow autoclosure on parameters, not on properties and globals
Disallow autoclosure on anything but paramdecls, and provide better QoI when you use them in enums.



Swift SVN r25262
2015-02-13 01:55:56 +00:00
Chris Lattner
db421806e2 fix <rdar://problem/19772570> Parsing of as and ?? regressed
In r22669, we expanded our parsing of T?? by using operator splitting.  This works
great when the ?? is lexed as a postfix operator, but does the wrong thing when the ??
is lexed as an infix  and there is whitespace around it.  For that, we don't want to 
process it as a type.  This was reported in a number of places including on stack
overflow:
http://stackoverflow.com/questions/28430496/nil-coalescing-to-provide-default-values-in-swift-1-2



Swift SVN r25228
2015-02-12 05:54:05 +00:00
Chris Lattner
a31d33fa69 Fix: <rdar://problem/19657458> Nil Coalescing operator (??) should have a higher precedence
increasing the precedence of ?? to be higher than ||/&& and comparisons.




Swift SVN r25218
2015-02-12 01:39:44 +00:00
Dmitri Hrybenko
f43843f25c tests: use the new substitution for the mock SDK
This is required to correctly use the mock SDK when the SDK overlay is
built and tested separately.  (Otherwise, the mock SDK might not get
used, because the overlay SDK options would expand from the
%-substitution, appear first on the command line, and shadow the mock
SDK in the search path).

Swift SVN r25185
2015-02-11 18:57:29 +00:00
Jordan Rose
ecf260e1cd [test] Fix expectations I changed without re-testing.
- Remove incorrectly copy/pasted expected-error in default_args.swift.
- Move expected-error to the correct line in protocols.swift

Swift SVN r25125
2015-02-10 17:37:47 +00:00
Jordan Rose
6fe976d3b0 Diagnose function conversions that change the function's ABI.
This post-hoc diagnostic replaces r24915, r25045, and r25054 by doing a
very basic check for representation incompatibility between two types.
More cases can be added as necessary.

rdar://problem/19600325, again.

Swift SVN r25117
2015-02-10 03:46:47 +00:00
Jordan Rose
07041fc7d3 Revert all the function type ABI restriction changes.
John pointed out that messing with the type checker's notion of "subtype"
is a bad idea. Instead, we should just have a separate check for ABI
compatibility...and eventually (rdar://problem/19517003) just insert the
appropriate thunks rather than forcing the user to perform the conversion.

I'm leaving all the tests as they are because I'm adding a post-type-checking
diagnostic in the next commit, and that should pass all the same tests.

Part of rdar://problem/19600325

Swift SVN r25116
2015-02-10 03:46:46 +00:00
Chris Willmore
e2ac9f65ac Add FixKind for 'as' -> 'as!' conversion
Penalize solutions that involve 'as' -> 'as!' changes by recording a Fix
when simplifying the corresponding checked-cast constraint.

<rdar://problem/19724719> Type checker thinks "(optionalNSString ?? nonoptionalNSString) as String" is a forced cast

Swift SVN r25061
2015-02-07 00:33:37 +00:00
Jordan Rose
19af8a124c Re-apply "If a function conversion fails, suggest wrapping in a closure."
This re-applies r24987, reverted in r24990, with a fix for a spuriously-
introduced error: don't use a favored constraint in a disjunction to avoid
applying a fix. (Why not? Because favoring bubbles up, i.e. the
/disjunction/ becomes favored even if the particular branch is eventually
rejected.) This doesn't seem to affect the outcome, though: the other
branch of the disjunction doesn't seem to be tried anyway.

Finishes rdar://problem/19600325

Swift SVN r25054
2015-02-06 23:12:54 +00:00
Jordan Rose
18355ca44a Revert "If a function conversion fails, suggest wrapping in a closure."
This reverts commit r24987. The constraint system is choosing the fix
case over the normal case in Dollar.swift.

Swift SVN r24990
2015-02-05 03:56:20 +00:00
Jordan Rose
ad7440989b If a function conversion fails, suggest wrapping in a closure.
And even if we don't suggest wrapping in a closure (say, because there's
already a closure involved), emit a more relevant diagnostic anyway.
(Wordsmithing welcome.)

Wrapping a function value in a closure essentially explicitly inserts a
conversion thunk that we should eventually be able to implicitly insert;
that's rdar://problem/19517003.

Part of rdar://problem/19600325

Swift SVN r24987
2015-02-05 01:56:47 +00:00
Joe Pamer
e452c87edb If an expression fails to type check because one of its sub-expressions is a failed call expression, make sure that the inner call expression's diagnostic is surfaced. Doing so address rdar://problem/19419422, rdar://problem/19437880 and rdar://problem/19559649.
Swift SVN r24935
2015-02-04 02:03:41 +00:00
Jordan Rose
3e1e2c4852 Lock down on function conversions that change the ABI of the function.
These haven't ever been safe in Swift's development because they require
generating thunks, and we currently don't do that. However, we were letting
existential conversions slip through the cracks because we consider them
subtypes, so that /metatype/ conversions work correctly. To be concrete:

  "let _: Any.Type = Int.self" is okay.
  "let _: (Int) -> Void = { (_: Any) -> Void in return }" is not.

We should implement this some day; that's rdar://problem/19517003.

This produces some lousy error messages, which I intend to fix soon.

Part of rdar://problem/19600325

Swift SVN r24915
2015-02-03 03:44:05 +00:00
Doug Gregor
6a1b7348e0 Make trailing closure syntax match the last parameter, always.
Previously, trailing closures would try to match the first parameter
of (possibly optional) function type that didn't seem to have an
argument already, but in practice this broke when there were
parameters with default arguments before the function parameter.

The new rule is far simpler: a trailing closure matches the last
parameter. Fixes rdar://problem/17965209.

Swift SVN r24898
2015-02-02 19:47:31 +00:00
Chris Willmore
ab86515fb2 <rdar://problem/19671476> Offer as -> as! changes in all nested contexts
When generating constraints for an 'as' expression, consider the
possibility that the code is supposed to be 'as!' instead of 'as'. Emit
the appropriate fixit if that branch of the disjunction is chosen by the
constraint solver.

This is a more comprehensive fix for <rdar://problem/19499340> than the
one in r24815.

Swift SVN r24872
2015-01-31 00:55:53 +00:00
Graham Batty
83b4384fac Update test flags for linux failures and support.
Also removed the sdk 'feature' in favour of the more specific
objc_interop.

Swift SVN r24856
2015-01-30 21:31:48 +00:00
Joe Pamer
0562411bb2 Improve support for diagnosing errors that result from contextual or conversion type mismatches. Doing so allows us to improve our diagnostics for a few important cases:
- Situations where the type of a return statement's result expression doesn't line up with the function's type annotation.
- Situations where the type of an initializer expression doesn't line up with its declaration's type pattern.
- Situations where we assume a conversion to a built-in protocol must take place, such as in if-statement conditionals.

(Addresses rdar://problem/19224776, rdar://problem/19422107, rdar://problem/19422156, rdar://problem/19547806 and lots of other dupes.)

Swift SVN r24853
2015-01-30 19:32:20 +00:00
Chris Willmore
b8e893c606 <rdar://problem/19650402> Swift compiler segfaults while running the annotation tests
r24834 fixes this bug, not 19563805. Fix comment on test.

Swift SVN r24835
2015-01-29 23:44:08 +00:00
Chris Willmore
aa3de78f2b <rdar://problem/19563805> Fuzzing Swift: performTypeChecking(...) crashes in ConformanceChecker::recordTypeWitness(...): Assertion failed: "Conformance should already have been verified"
Don't attempt to re-typecheck coerce expr when we're already pretty sure
it has failed.

Swift SVN r24834
2015-01-29 23:28:12 +00:00
Doug Gregor
2bf69a0ea0 Require witnesses for @objc requirements to be @objc.
Previously, we attempted to infer @objc-ness based on conformance, but
doing so is fraught with ordering dependencies, and just doesn't work
in the general case. Among other crimes, this allowed us to
retroactively mark a non-@objc method from an imported module as
@objc... even though nobody would ever then emit the @objc entry
points for it.

Fixes the rest of rdar://problem/18383574.

Swift SVN r24831
2015-01-29 22:53:53 +00:00
Joe Pamer
6a70bd085e These changes implement some oft-requested tweaks and fixes to our closure implementation:
- Closures that are comprised of only a single return statement are now considered to be "single expression" closures. (rdar://problem/17550847)
- Unannotated single expression closures with non-void return types can now be used in void contexts. (rdar://problem/17228969)
- Situations where a multi-statement closure's type could not be inferred because of the lack of a return-type annotation are now properly diagnosed. (rdar://problem/17212107)

I also encountered a number of crashers along the way, which should now be fixed.

Swift SVN r24817
2015-01-29 18:48:39 +00:00
Chris Willmore
fd272aefb9 <rdar://problem/19499340> QoI: Nimble as -> as! changes not covered by Fix-Its
If the typechecking failure involves a conversion constraint anchored at
as 'as' or 'as!' expression, try diagnosing it directly.

Swift SVN r24815
2015-01-29 08:21:32 +00:00