Commit Graph

2716 Commits

Author SHA1 Message Date
Chris Willmore
df9136e0e0 When unifying two type variables, make sure the new representative type
variable has the must-be-materializable bit set if the old one does.
When assigning a fixed type to a type variable that must be
materializable, transfer the bit to any type variables within the fixed
type, as appropriate.

Add Options field to SavedTypeVariableBinding to save/restore type
variable options during solution.

<rdar://problem/21026806> Propagate MustBeMaterializable bit among type variables appropriately

Swift SVN r28883
2015-05-21 19:53:48 +00:00
Chris Lattner
d6ba8666ca various parts of the compiler are doggedly trying to figure out
whether a getter is mutating or a setter is nonmutating, centralize
it into methods on AbstractStorageDecl, NFC.


Swift SVN r28823
2015-05-20 05:00:13 +00:00
Chris Willmore
5905d07a08 Don't bind generic type parameters to non-materializable types.
Add a new option, TVO_MustBeMaterializable, to
TypeVariableType::Implementation, and set it for type variables
resulting from opening a generic type. This solution isn't complete (we
don't yet copy the non-materializable bit on unification of type
variables, and it's possible to bind a must-be-materializable type
variable to a type with type variables that later get bound to
non-materializable types) but it addresses all reported crashes for this
issue.

<rdar://problem/20807269> Crash in non-materializable type

Swift SVN r28792
2015-05-19 21:50:26 +00:00
Chris Lattner
580315d9ee fix <rdar://problem/17652759> Default arguments cause crash with tuple permutation
This just deletes some code out of matchCallArguments to make sure that CSApply
and CSSimplify do the same argument matching.  This code presumably
dated to a period of time when the stuff after it was just for error recovery,
but now we do argument reordering matching stuff after this point.


Swift SVN r28670
2015-05-17 01:12:47 +00:00
Ted Kremenek
9f9bb725cf Rename '_ErrorType' to 'ErrorType'.
Swift SVN r28293
2015-05-07 21:59:29 +00:00
Doug Gregor
b8995b0aa3 Transform the Module class into ModuleDecl.
Modules occupy a weird space in the AST now: they can be treated like
types (Swift.Int), which is captured by ModuleType. They can be
treated like values for disambiguation (Swift.print), which is
captured by ModuleExpr. And we jump through hoops in various places to
store "either a module or a decl".

Start cleaning this up by transforming Module into ModuleDecl, a
TypeDecl that's implicitly created to describe a module. Subsequent
changes will start folding away the special cases (ModuleExpr ->
DeclRefExpr, name lookup results stop having a separate Module case,
etc.).

Note that the Module -> ModuleDecl typedef is there to limit the
changes needed. Much of this patch is actually dealing with the fact
that Module used to have Ctx and Name public members that now need to
be accessed via getASTContext() and getName(), respectively.

Swift SVN r28284
2015-05-07 21:10:50 +00:00
Doug Gregor
a55050376f Switch TypeChecker::lookupMemberType() over to NameLookupOptions. NFC
Swift SVN r28283
2015-05-07 21:10:44 +00:00
Doug Gregor
da47d233da Teach TypeChecker::lookupConstructors() to use NameLookupOptions. NFC
Swift SVN r28282
2015-05-07 21:10:43 +00:00
Doug Gregor
83d336e714 Stop using TupleTypeElts in the type checking for arg/param matching.
Instead, use a dedicated struct. NFC

Swift SVN r28207
2015-05-06 17:05:27 +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
Doug Gregor
a30ca2a60d Replace bool parameter to TypeChecker::conformsToProtocol() with an option set.
NFC; we can extend this option set more readily later.

Swift SVN r27894
2015-04-29 00:08:22 +00:00
Chris Lattner
976620b513 random tidying up, rename the raw_ostream form of
ConstraintSystem::dump to ConstraintSystem::print for
consistency with other parts of the compiler.  Enhance
CS::print to print the ID # of a Type Variable, so you
don't have to count them to realize that you're looking
at typevar #13


Swift SVN r27874
2015-04-28 17:50:24 +00:00
Chris Lattner
d18740d603 Fix <rdar://problem/19773562> Closures executed immediately { like this }() are not automatically @noescape
In addition to being better for performance in these cases, this disables the "self." 
requirement in these blocks.  {}() constructs are often used to work around statements
that are not exprs in Swift, so they are reasonably important.

Fixing this takes a couple of pieces working together:
 - Add a new 'extraFunctionAttrs' map to the ConstraintSystem for solution
   invariant function attributes that are inferred (like @noescape).
 - Teach constraint simplification of function applications to propagate 
   @noescape between unified function types.
 - Teach CSGen of ApplyExprs to mark the callee functiontype as noescape
   when it is obviously a ClosureExpr.

This is a very limited fix in some ways: you could argue that ApplyExpr should
*always* mark its callee as noescape.  However, doing so would just introduce a
ton of function conversions to remove it again, so we don't do that.




Swift SVN r27723
2015-04-25 23:34:18 +00:00
Joe Pamer
3f572cab62 Remove hack to bind throwing initializer overloads based on the presence of a throwing initializer in the overload group.
Swift SVN r27694
2015-04-24 17:48:29 +00:00
Joe Pamer
220c92a09b Revert "Revert "A couple of 'throws' inference fix-ups:""
This reverts commit r27576.

(In some cases of catastrophic error recovery, ctor types may still be null during constraint solving, so it was wrong of me to assume otherwise.)

Swift SVN r27599
2015-04-22 20:34:05 +00:00
Dmitri Hrybenko
4229e8a14f Revert "A couple of 'throws' inference fix-ups:"
This reverts commit r27568 to unblock the buildbot.  It regressed three
compiler crashers:

    Swift :: compiler_crashers_fixed/0367-llvm-errs.swift
    Swift :: compiler_crashers_fixed/1769-getselftypeforcontainer.swift
    Swift :: compiler_crashers_fixed/1916-swift-nominaltypedecl-getdeclaredtypeincontext.swift

Swift SVN r27576
2015-04-22 08:42:21 +00:00
Joe Pamer
70fd1d6360 A couple of 'throws' inference fix-ups:
- When inferring 'throws' for a closure function type, look inside of catchless do blocks for 'try' expressions.
- When simplifying overload constriants for applications of throwing initializers, the bound member type of the initializer should also be marked as throwing.
  (Not doing so would cause us to incorrectly reject the overload.)

Swift SVN r27568
2015-04-22 06:39:39 +00:00
Joe Groff
3f241bb80a Sema: Handle NSError not being found during type checking better.
Fix crashes when type-checking ErrorType dynamic casts when NSError isn't available. Split the ErrorType execution tests into separate units for bridging and non-bridging, so that we get better coverage of this situation in both ObjC and non-ObjC builds. Fixes rdar://problem/20585210.

Swift SVN r27556
2015-04-22 03:49:01 +00:00
Joe Groff
be7c339af8 Type checker: Increase the score of metatype-to-object conversions.
Metatype-to-metatype conversions should be favored.

Swift SVN r27415
2015-04-17 05:30:38 +00:00
Joe Groff
d46afe513e AnyObject is a BridgeableObjectType even if it's not @objc.
Swift SVN r27412
2015-04-17 05:30:29 +00:00
Joe Groff
852a701051 Type checker: Allow implicit conversions from existential metatypes to more general ones.
An existential composition protocol<P,Q>.Type is convertible to P.Type is convertible to Any.Type.

Swift SVN r27410
2015-04-17 05:30:24 +00:00
Joe Groff
92636a4a64 Type checker: Allow 'AnyObject: AnyObject' self-conformance even when AnyObject isn't @objc.
It can still trivially witness itself, since being a class is evidence enough.

Swift SVN r27408
2015-04-17 05:30:18 +00:00
Joe Groff
dfc57e6f01 Type checker: Don't attempt metatype-to-object conversions if ObjC interop is disabled.
The post-type-checking error that was here is arguably better QoI, but it is causing ambiguities that break the non-ObjC-compatible build in the stdlib. We shouldn't even attempt these conversions if there's no runtime support to back them up.

Swift SVN r27407
2015-04-17 05:30:16 +00:00
Doug Gregor
9256250273 Fix type checking for a non-throwing function as a subtype of a throwing function.
A non-throwing function can be a trivial subtype of a throwing
function. Encode this rule more directly, introduce some additional
tests to ensure that we get the behavior right where we need exact
matches, and add a failure kind with custom diagnostic for cases where
function types mismatch due to 'throws'.

Swift SVN r27255
2015-04-13 18:39:39 +00:00
Joe Groff
66ae68bcc3 Sema: Allow ErrorType-conforming types to be explicitly coerced 'as NSError'.
Swift SVN r26993
2015-04-04 22:38:04 +00:00
Chris Lattner
79ed57f9f2 standardize naming of tuples and tuple patterns on "elements".
Previously some parts of the compiler referred to them as "fields",
and most referred to them as "elements".  Use the more generic 'elements'
nomenclature because that's what we refer to other things in the compiler
(e.g. the elements of a bracestmt).

At the same time, make the API better by providing "getElement" consistently
and using it, instead of getElements()[i].

NFC.



Swift SVN r26894
2015-04-02 20:23:49 +00:00
Chris Lattner
f7fe8a5f4a Fix <rdar://problem/20392122> Destructuring tuple with labels doesn't work
This pushes tuple pattern labels forward:
  - Actually record them in TuplePatternElt.
  - Remove the tuple shuffle ban that prevents some cases
    (e.g. the one in the radar) of a tuple with labels being shuffled
    onto a tuple without labels.
  - Remove dead code enabled by removing the restriction.



Swift SVN r26852
2015-04-02 04:23:54 +00:00
Joe Pamer
eee40fc53f Add basic parsing, sema and mangling support for throwing function types. Next up, metadata and serialization support, as well as more tests.
Swift SVN r26767
2015-03-31 18:55:19 +00:00
Chris Willmore
9e8d4b0dbf Use isVoid() instead of isEqual(TheEmptyTupleType). Add tests for underspecification issue fixed by r25939.
<rdar://problem/19835413> Reference to value from array changed

Swift SVN r25962
2015-03-10 22:35:08 +00:00
Chris Willmore
44da3ceb8c When typechecking the constraint inout $T1 < UnsafeMutablePointer<Void>, bind $T1 to Void as a last resort.
Revert stdlib changes in r25921.

<rdar://problem/19835413> Reference to value from array changed

Swift SVN r25939
2015-03-10 18:54:36 +00:00
Chris Willmore
7cbadb1d2c When UnsafeMutablePointer<$T1> has a Conversion constraint to
UnsafeMutablePointer<Void>, don't bind $T1 to Void. This fixes an
unintentional dependency on the order in which constraints are visited
by the solver.

Fix some resulting underconstrained expressions in the stdlib.

<rdar://problem/19835413> Reference to value from array changed

Swift SVN r25921
2015-03-10 10:08:38 +00:00
Chris Willmore
4b939eef89 Allow implicit conversion constraint to bridge String to NSString even
when it is the favored constraint of a disjunction.

This allows 'ns ?? "str" as String as String' to typecheck where ns is
an NSString. This does not regress the relevant test for 17962491.

<rdar://problem/20029786> Swift compiler sometimes suggests changing "as!" to "as?!"

Swift SVN r25910
2015-03-10 03:45:53 +00:00
Chris Willmore
9910c75c67 Don't offer incorrect fixit for NSString? -> String? conversion.
Update the locator when matching optional types in parallel fashion.
This causes the simplifyLocator() call to fail to simplify the locator
completely, and avoids the bogus diagnostic.

<rdar://problem/19836341> Incorrect fixit for NSString? to String? conversions

Swift SVN r25596
2015-02-27 05:30:11 +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
Joe Pamer
81df1eeee0 Revert "Provide a better, tailored diagnostic for result-type mismatches. (rdar://problem/19800727)"
This reverts commit r25319.

Swift SVN r25333
2015-02-16 23:46:59 +00:00
Joe Pamer
6e9ddbafd0 Provide a better, tailored diagnostic for result-type mismatches. (rdar://problem/19800727)
Swift SVN r25319
2015-02-16 20:00:04 +00:00
Joe Pamer
989aa3d58a When converting a non-void-returning closure to a contextual function type that returns void, increase the function conversion score count. This ensures that, in the case of overload resolution, we don't accidentally favor a void-returning function over a better (or exact) fit. (rdar://problem/19776288)
Swift SVN r25140
2015-02-10 22:02:20 +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
003821a956 Value-to-optional conversions are only subtypes for class references.
let x: Int? = 4 as Int // okay
  let f: (Int) -> Void = { (x: Int?) -> Void in println(x) } // error!

As part of this, remove an invalid "protection" of value-to-optional
conversions that involve T? -> T??. Unfortunately, this results in an
ambiguity in the following code:

  var z: Int?
  z = z ?? 42

Both of our overloads for ?? here require one value-to-optional conversion,
and the overload preference isn't conclusive. (Turns out (T?, T) and
(U?, U?) can be unified as T=U or as T=U?.) The best thing to do would be
to take our solved T=Int binding into account, but the type checker isn't
set up to do that at the moment.

After talking to JoeP, I ended up just hardcoding a preference against
the (T?, T?) -> T? overload of ??. This is only acceptable because both
overloads have the same result, and I'll be filing another Radar to
remove this hack in the future.

Part of rdar://problem/19600325

Swift SVN r25045
2015-02-06 20:41:51 +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
Doug Gregor
9637137d45 Provide forced-downcasting Fix-Its where we used to have implicit bridging conversions.
For example, we used to have an implicit conversion from NSString to
String, but it was removed. Provide a Fix-It that suggests "as!
String". Fixes <rdar://problem/19551164>.

Swift SVN r24948
2015-02-04 07:08:27 +00:00
Doug Gregor
92ff07a556 If member lookup into an optional fails, suggest a '!'.
This Fix-It handles simple cases such as "foo.bar" where "foo" is of
optional type. Addresses rdar://problem/19707036.

Swift SVN r24944
2015-02-04 06:15:36 +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