Commit Graph

75 Commits

Author SHA1 Message Date
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
Chris Lattner
e2fe7704ad Reimplement emission of StmtCondition (e.g. if/let & while/let) to be
in terms of the pattern binding/emission facilities that are currently
used for switches.  They are more general (handling all patterns,
not hacked up just for optionals).

This leads to us producing better code for if/let bindings, because we 
don't alloc_stack a temporary and deal with memory for non-address-only
types (e.g. the common case of an optional pointer).  Instead, the code 
emits a select_enum{_addr} on the value.

While this changes the generated code in the compiler, there is no exposed
behavioral change to the developer.




Swift SVN r26142
2015-03-14 20:31:23 +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
Chris Lattner
3e723974cc Implement support for a leading boolean condition in an if-let condition,
resolving <rdar://problem/19797158> Swift 1.2's "if" has 2 behaviours. They could be unified.



Swift SVN r25369
2015-02-18 04:54:27 +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
David Farler
182792cada Reinstate multi-pattern conditions in if/while
rdar://problem/19450969

Undo reverts r24381..24384 with one fix: pull cleanup blocks from the
back of the list. When breaking out of a while loop, an extra release
could over-release a reference.

Swift SVN r24553
2015-01-20 09:59:44 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +00:00
Nadav Rotem
fbf9ff6a2d Revert "Implement SILGen for multi-pattern conditions in if/while, finishing the "
This reverts commit 24348 and 24355 that that broke the build.
See rdar://19445271.

Conflicts:
	test/SILGen/if_while_binding.swift

Swift SVN r24384
2015-01-13 00:24:49 +00:00
Nadav Rotem
bec9d8704a Revert "substantially rewrite StmtCondition emission (again). In the previous attempt, we"
This reverts commit 24360 that depends on 24348 that broke the build.

Swift SVN r24383
2015-01-13 00:24:48 +00:00
Nadav Rotem
39237eeb0f Revert "destroy_addr on a known-nil optional is always a noop, *never* emit it."
This reverts commit 24380 that depends on 24348 that broke the build.

Swift SVN r24381
2015-01-13 00:24:47 +00:00
Chris Lattner
06ff115af9 destroy_addr on a known-nil optional is always a noop, *never* emit it.
This cleans up codegen for if-let a LOT and should make it more efficient
even in release builds.


Swift SVN r24380
2015-01-13 00:05:39 +00:00
Chris Lattner
c2793c249e substantially rewrite StmtCondition emission (again). In the previous attempt, we
emitted just the optional buffers, and tested them when evaluated the condition, then
consumed them all (producing the contained value) when the entire condition is true.
This doesn't work with where conditions, because the where condition can use the bound
variables.

The new approach emits the variable bindings as the conditions is emitted, allowing the
bindings to be in scope for where clauses.  In addition to this redesign, the implementation
is also somewhat simpler by being a recursive implementation which is (to me at least)
easier to reason about.

multi-pattern where bindings with where clauses should now be fully operational.


Swift SVN r24360
2015-01-11 03:56:19 +00:00
Chris Lattner
cd16c318d4 add tests for if and if/else with multiple patterns.
Swift SVN r24355
2015-01-10 05:17:31 +00:00
Chris Lattner
b2e11e4da5 Implement SILGen for multi-pattern conditions in if/while, finishing the
known implementation work required for:
<rdar://problem/19382942> Improve 'if let' to avoid optional pyramid of doom

I want to write some more tests and improve error recovery QoI in the parser,
but I believe it fully works now.  Please kick the tires and let me know if
you observe any problems (other than 19432424 which was pre-existing).



Swift SVN r24348
2015-01-10 01:59:47 +00:00
Graham Batty
83f27a8af7 Revert "Mark tests that don't pass on linux as XFAIL."
This reverts commit 2711ca86de7bf6a7885ccea24219a48a590b1e95.

Swift SVN r23577
2014-11-24 17:42:13 +00:00
Graham Batty
198402dcfe Mark tests that don't pass on linux as XFAIL.
Swift SVN r23573
2014-11-24 17:40:37 +00:00
Joe Groff
f2658bf152 SILGen: Use select_enum_addr instead of a library function for "does optional have value" queries.
Thanks Arnold for fixing the crashes this exposed in the perf suite.

Swift SVN r22954
2014-10-26 22:34:23 +00:00
Dave Abrahams
69735ae0d0 Revert "SILGen: Use select_enum_addr instead of a library function for "does optional have value" queries."
This reverts r22828 because it was apparently causing an assertion on
the bot:

Swift SVN r22831
2014-10-19 19:54:37 +00:00
Joe Groff
05bacc48e0 SILGen: Use select_enum_addr instead of a library function for "does optional have value" queries.
This causes a regression in specialize_checked_cast_branch.swift that ought to be recovered by rdar://problem/18603827.

Swift SVN r22828
2014-10-18 22:20:53 +00:00
Joe Groff
a6a68d49cc SILGen: Avoid using a stdlib function to get optional values.
When we've already established that the optional has a value, using unchecked_take_enum_data_addr to directly extract the enum payload is sufficient and avoids a redundant call and check at -Onone. Keep using the _getOptionalValue stdlib function for checked optional wrapping operations such as "x!", so that the stdlib can remain in control of trap handling policy.

The test/SIL/Serialization failures on the bot seem to be happening sporadically independent of this patch, and I can't reproduce failures in any configuration I've tried.

Swift SVN r22537
2014-10-06 15:46:21 +00:00
Joe Groff
069ad18620 Revert "SILGen: Avoid using a stdlib function to get optional values."
This reverts commit r22533. The optimizing bots seem to have problems with it.

Swift SVN r22534
2014-10-06 04:40:58 +00:00
Joe Groff
28805f0d2a SILGen: Avoid using a stdlib function to get optional values.
When we've already established that the optional has a value, using unchecked_take_enum_data_addr to directly extract the enum payload is sufficient and avoids a redundant call and check at -Onone. Keep using the _getOptionalValue stdlib function for checked optional wrapping operations such as "x!", so that the stdlib can remain in control of trap handling policy.

Swift SVN r22533
2014-10-06 04:31:52 +00:00
Erik Eckstein
c16c510167 Set SILLinkage according to visibility.
Now the SILLinkage for functions and global variables is according to the swift visibility (private, internal or public).

In addition, the fact whether a function or global variable is considered as fragile, is kept in a separate flag at SIL level.
Previously the linkage was used for this (e.g. no inlining of less visible functions to more visible functions). But it had no effect,
because everything was public anyway.

For now this isFragile-flag is set for public transparent functions and for everything if a module is compiled with -sil-serialize-all,
i.e. for the stdlib.

For details see <rdar://problem/18201785> Set SILLinkage correctly and better handling of fragile functions.

The benefits of this change are:
*) Enable to eliminate unused private and internal functions
*) It should be possible now to use private in the stdlib
*) The symbol linkage is as one would expect (previously almost all symbols were public).

More details:

Specializations from fragile functions (e.g. from the stdlib) now get linkonce_odr,default
linkage instead of linkonce_odr,hidden, i.e. they have public visibility.
The reason is: if such a function is called from another fragile function (in the same module),
then it has to be visible from a third module, in case the fragile caller is inlined but not
the specialized function.

I had to update lots of test files, because many CHECK-LABEL lines include the linkage, which has changed.

The -sil-serialize-all option is now handled at SILGen and not at the Serializer.
This means that test files in sil format which are compiled with -sil-serialize-all
must have the [fragile] attribute set for all functions and globals.

The -disable-access-control option doesn't help anymore if the accessed module is not compiled
with -sil-serialize-all, because the linker will complain about unresolved symbols.

A final note: I tried to consider all the implications of this change, but it's not a low-risk change.
If you have any comments, please let me know.



Swift SVN r22215
2014-09-23 12:33:18 +00:00
Ted Kremenek
fad874708e Adjust test cases.
Swift SVN r17964
2014-05-12 22:01:52 +00:00
Joe Groff
9fe1ab427a Implement 'if let' and 'while let' statements.
Allow IfStmts and WhileStmts to have as their condition either an expression, as usual, or a pattern binding introduced by 'var' or 'let', which will conditionally bind to the value inside an optional. Unlike normal pattern bindings, these bindings require an in-line initializer, which will be required to be Optional type. Parse variable bindings in this position, and type-check them by requiring an Optional on the right-hand side and unwrapping it to form the pattern type. Extend SILGen's lowering of if and while statements to handle conditionally binding variables.

Swift SVN r13146
2014-01-30 10:37:39 +00:00