Commit Graph

194 Commits

Author SHA1 Message Date
Joe Pamer
3edab3db8d When walking overload groups during constraint analysis, defend against function/decl type mismatches. (rdar://problem/20162476)
Swift SVN r30202
2015-07-14 22:58:54 +00:00
Chris Lattner
8637bb4c01 fix <rdar://problem/21679169> compiler crashes on "{(=_=_)in"
Swift SVN r29914
2015-07-06 04:46:56 +00:00
Slava Pestov
e7d8c3c4bc Sema: Fix synthesis of materializeForSet for subscripts in protocol extensions
Since each override of a subscript protocol requirement provides
its own materializeForSet, there is no need to do dynamic dispatch,
a peer call to the setter suffices. However, since CodeSynthesis
runs at the AST level, it would create a SubscriptExpr which
overload resolution would later bind to the protocol requirement
subscript rather than the static witness in the extension.

This triggered an assertion. Solve the problem by binding the
actual ConcreteDeclRef of the SubscriptExpr at synthesis time,
and modifying CSGen to special-case SubscriptExprs that already
have a ConcreteDeclRef set.

Fixes <rdar://problem/21370629>.

Swift SVN r29906
2015-07-03 02:25:26 +00:00
Chris Willmore
99058b45a5 Only strip implicit self parameter from func decl type if the func decl
has an implicit self decl. Follow-up to r29732.

Swift SVN r29742
2015-06-26 17:15:20 +00:00
Chris Willmore
fd5812be50 Don't assume that a ValueDecl in a type DeclContext with function type
is an instance method. Make sure it's a non-static function before
attempting to strip the implicit self parameter from the type.

<rdar://problem/20886179> compiler error

Swift SVN r29732
2015-06-26 05:39:38 +00:00
Ben Langmuir
1be2a778c1 [CodeCompletion] Avoid choking on a doubly-typechecked expr
First, fix a case of type checking an expression twice, which is against
the design of the type checker. We hit this because we can type check
the "context" in
  let x = foo.#^COMPLETE^#
and then type check the "parsed expression", which is contained in the
context here.

Second, make the hack from rdar://20738314 more robust so that if we
*do* double typecheck for some reason we won't just choke on an
AutoClosureExpr.  I filed rdar://21466394 to audit for other cases of
double typechecking and remove this hack.

Fixes rdar://21346928

Swift SVN r29527
2015-06-19 22:16:07 +00:00
Joe Groff
bebfa969bd Sema: Allow 'x.init' references on metatype expressions.
If 'x.init' appears as a member reference other than 'self.init' or 'super.init' within an initializer, treat it as a regular static member lookup for 'init' members. This allows a more explicit syntax for dynamic initializations; 'self.someMetatype()' looks too much like it's invoking a method. It also allows for partial applications of initializers using 'someMetatype.init' (though this needs some SILGen fixes, coming up next). While we're in the neighborhood, do some other correctness and QoI fixes:

- Only lookup initializers as members of metatypes, not instances, and add a fixit (instead of crashing) to insert '.dynamicType' if the initializer is found on an instance.
- Make it so that constructing a class-constrained archetype type correctly requires a 'required' or protocol initializer.
- Warn on unused initializer results. This seems to me like just the right thing to do, but is also a small guard against the fact that 'self.init' is now valid in a static method, but produces a newly-constructed value instead of delegating initialization (and evaluating to void).

Swift SVN r29344
2015-06-08 04:11:28 +00:00
Joe Groff
1479a56ab3 Sema: Move semantic constraints on super/self.init out of the parser.
Instead of forcing full application of '{super,self}.init' in the parser, and installing the RebindSelf semantic expr node early, make these constraints to Sema-time checks, and parse '<expr>.init' as a regular postfix production. This is a better separation of concerns, and also opens the door to supporting 'metatype.init()' in more general expression contexts (though that part still needs some follow-up sema work).

Swift SVN r29343
2015-06-08 04:11:16 +00:00
Jordan Rose
2d66428272 Clean up KnownIdentifiers.def.
- Remove unused names.
- Define IDENTIFIER in terms of IDENTIFIER_WITH_NAME.
- Adjust each name to always match the corresponding value in case.
- Add an IDENTIFIER_ macro for the common case of defining an underscored name.
- Avoid creating names with double underscores, which are technically reserved
  by the C++ standard.

There are two special cases I left in here for the identifiers '_code' and
'_domain'. I didn't want to call these simply 'Id_code' and 'Id_domain' for
fear someone would try to use them as 'code' and 'domain', so I made them into
'Id_code_' and 'Id_domain_' for now.

No intended functionality change.

Swift SVN r29291
2015-06-04 04:01:06 +00:00
Ben Langmuir
477ea489c7 [CodeCompletion] Avoid assert when top-level if contains an autoclosure
We have an unreachable in the visitor for auto closures, so avoid
visiting it.  It would be great to have a clearer picture of what needs
to happen for AutoClosure vs ClosureExpr. It's hard to tell exactly
where AutoClosure is supposed to be handled if at all.

Swift SVN r29037
2015-05-26 20:09:42 +00:00
Chris Lattner
e517ad9182 Fix unreachable code handling to properly diagnose things like:
throw x 
whatever()  

as being unreachable after the throw.



Swift SVN r28680
2015-05-17 15:13:35 +00:00
Dmitri Hrybenko
0a1f7c09df Revert "Fix unreachable code handling to properly diagnose things like:"
This reverts commit 28678.  It broke the IDE/complete_exception.swift
test.

Swift SVN r28679
2015-05-17 12:27:57 +00:00
Chris Lattner
5ead9764bd Fix unreachable code handling to properly diagnose things like:
throw x
  whatever()

as being unreachable after the throw.



Swift SVN r28678
2015-05-17 05:56:02 +00:00
John McCall
5d4fa0b7b6 A better fix: stop storing the address of a type in the constraint
system in the first place, because it's totally immutable on all
access paths.

Swift SVN r28595
2015-05-15 01:05:56 +00:00
Joe Pamer
9bf1f91893 Prevent the type checker from going exponential on straightforward bindings of named patterns to nested array literals. I have a more principled fix for these kinds of issues in the works, but this will unblock a lot of users until those changes land. (rdar://problem/19810828)
Swift SVN r28544
2015-05-14 00:15:57 +00:00
Chris Lattner
8a7b3f414e Revise the parser and AST representation of #available to be part of StmtCondition
instead of being an expression.

To the user, this has a couple of behavior changes, stemming from its non-expression-likeness.
 - #available cannot be parenthesized anymore
 - #available is in its own clause, not used in a 'where' clause of if/let.

Also, the implementation in the compiler is simpler and fits the model better.  This
fixes:
<rdar://problem/20904820> Following a "let" condition with #available is incorrectly rejected



Swift SVN r28521
2015-05-13 19:00:40 +00:00
Doug Gregor
2653a6569b Eliminate ModuleExpr; DeclRefExpr is good enough for anyone.
Swift SVN r28285
2015-05-07 21:10:53 +00:00
Joe Pamer
3b57cabae7 Consider default argument patterns, as they relate to actual arguments, when deciding on a specific overload to favor for a function application. Doing so addresses another class of exponential behavior bugs in the type checker (rdar://problem/19779591 and rdar://problem/20772053). There's still some work left to do, though - hence the change to Concatenate.swift, to work around rdar://problem/20789500.
Swift SVN r28221
2015-05-06 22:32:07 +00:00
John McCall
204bb04f04 In closure 'throws' inference, infer 'throw' as throwing.
Also, walk into 'do' bodies when the catches aren't
exhaustive, but don't walk into try! operands.

Swift SVN r28092
2015-05-02 19:50:21 +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
6c8e9ac276 change some calls to dyn_cast in boolean contexts to isa.
Swift SVN r27724
2015-04-25 23:34:38 +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
d4e744b2ed Use "isa" instead of "dyn_cast".
Swift SVN r27606
2015-04-22 21:35:57 +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
Chris Willmore
d4db635e3d Add object literal syntax and _{Color,Image}LiteralConvertible protocols
Add syntax "[#Color(...)#]" for object literals, to be used by
Playgrounds for inline color wells etc. The arguments are forwarded to
the relevant constructor (although we will probably change this soon,
since (colorLiteralRed:... blue:... green:... alpha) is kind of
verbose). Add _ColorLiteralConvertible and _ImageLiteralConvertible
protocols, and link them to the new expressions in the type checker.
CSApply replaces the object literal expressions with a call to the
appropriate protocol witness.

Swift SVN r27479
2015-04-20 12:55:56 +00:00
Joe Pamer
006c182c13 Begin inferring throwing function types for closures. (There's more work to do here - hence the thin tests - but I need to investigate a couple of sema bugs before moving forward.)
Swift SVN r27438
2015-04-17 19:06:52 +00:00
Joe Pamer
e4f7fee4f4 When gathering "linked" expressions to determine a favored constraint, consider top-level literals to be fair game.
This unblocks standard library work by preventing us from going exponential when extending existing struct types to have failable initializers. (rdar://problem/20336356)
On my laptop, it also results in a 7% end-to-end improvement in the time it takes to run our unit tests under DebugAssert (previously: 591.63s, now: 552.64s). Though, as usual, YMMV.

Swift SVN r27156
2015-04-09 01:54:34 +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
John McCall
08d3460a19 Implement throw expressions. Untested.
Tests tomorrow for this and 'catch', I promise. :)

Swift SVN r26432
2015-03-23 08:10:15 +00:00
Argyrios Kyrtzidis
68bd67fc12 [Parse/AST] Introduce an AST node for editor placeholders in expression contexts.
If the placeholder is a typed one, parse its type string into a TypeRepr,
resolve it during typechecking and set it as the type for the associated EditorPlaceholderExpr.

Swift SVN r26215
2015-03-17 01:53:02 +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
Chris Lattner
90920ec02f rename the IsaExpr AST node to IsExpr to follow the syntax of the language, NFC.
Swift SVN r25933
2015-03-10 17:11:37 +00:00
Doug Gregor
3805e18090 Explicitly track the mapping from dependent types to their opened type variables.
Previously, we were reconstructing this mapping from the "full" opened
type produced by declaration references. However, when dealing with
same-type constraints between associated types and type parameters, we
could end up with an incomplete mapping, which let archetypes slip
through. Most of the churn here is sorting out the locators we need to
use to find the opened-type information. Fixes rdar://problem/18208283
and at least 3 dupes of it that I've found so far.

Swift SVN r25375
2015-02-18 19:41:40 +00:00
Joe Pamer
ec6810827a Replace a dyn_cast to "DictionaryType" with an invocation of ConstraintSystem::isDictionaryType. (I'm pretty sure that just relying on the cast can cause problems when generating constraints.)
Swift SVN r25157
2015-02-11 01:25:33 +00:00
Joe Pamer
c5c521392e Don't over-eagerly specialize the return type of an enum initializer application. Doing so doesn't really improve compiler throughput, but can cause problems if the enum has overloaded initializers. (rdar://problem/19697319)
Swift SVN r25147
2015-02-11 00:13:16 +00:00
Joe Pamer
9923e3ff89 When optimizing constraints for an unresolved dot expression, don't walk into the base expression - it will most likely result in an incorrect favored type, which can lead to "expression too complex" errors. (rdar://problem/19773442, rdar://problem/19770646)
Swift SVN r25129
2015-02-10 19:14:29 +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
Joe Pamer
45e7fd53dd Remove a misguided peephole optimization from the constraint generator. This fix partially unblocks the build of the Cartography external project.
Swift SVN r24927
2015-02-03 21:15:00 +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
Joe Pamer
a18bedf079 Factor the constraint-favoring machinery out of the constraint generation process, and re-work it into a series of passes over an expression sub-tree.
Aside from tidying things up, doing this results in some significant benefits:
- Allows for global constraint ordering optimizations over a given expression, not just on a peephole basis.
- Eliminates a set of order-dependent bugs in the solver that have been dogging us for a while. (rdar://problem/19459079)
- Brings another set of tyvar-to-tyvar solving problems out of the realm of the exponential. (rdar://problem/19005271)
- Opens up the possibility of optimizing constraints during later solving phases - not just while generating them.

Swift SVN r24693
2015-01-23 23:10:50 +00:00
Joe Pamer
ea7bd2de53 Per Doug's comment, check for failable initializers on enum types when optimizing the return type of an initializer application. With this change, Adventure builds cleanly once again.
Swift SVN r24595
2015-01-21 06:10:41 +00:00
Joe Pamer
d9325547e5 Favor dictionary subscript return types (in addition to array subscript return types).
Swift SVN r24523
2015-01-19 20:59:15 +00:00
Joe Pamer
f453bceeb1 Fix utilization of expected types for subscript and apply expressions when favoring constraints. (rdar://problem/19334553)
Swift SVN r24522
2015-01-19 20:59:15 +00:00
Joe Pamer
35184ff7b5 Utilize argument type information to favor overload binding constraints on initializers. Doing so addresses a number of cases where the type checker was going exponential on seemingly simple user code.
Also, these changes fix the performance regressions that were introduced as a result of September's convertible/init requirement modifications, and allow us to roll back the associated workarounds that were added to the Adventure sample (rdar://problem/18942100).

Swift SVN r24520
2015-01-19 20:59:14 +00:00
Joe Pamer
f56949846f Propagate constraint favoring information across nodes of chained operations. This allows us to properly favor constraints for expressions the form "(float0 + float1) * float2".
Swift SVN r24519
2015-01-19 20:59:14 +00:00
Joe Pamer
87be8b13b7 Only favor overload constraints on OverloadedDeclRefs with distinct numbers of parameters. This allows us to keep the previous optimization, while still preserving ambiguity diagnostics.
Swift SVN r24518
2015-01-19 20:59:13 +00:00
Joe Pamer
da7e63cc24 For closure and subscript expressions, if valid contextual information is available use it instead of allocating type variables for their result types.
Swift SVN r24516
2015-01-19 20:59:12 +00:00