Commit Graph

2448 Commits

Author SHA1 Message Date
Doug Gregor
4ec1cb2537 Collapse creation of DynamicMemberRefExprs into the normal member-reference path. NFC
Swift SVN r27229
2015-04-11 03:20:15 +00:00
Joe Groff
7612df1918 Runtime: Rename 'becomeNSError' to 'bridgeErrorTypeToNSError'.
We may want to use optimized ErrorType representations that don't naturally "become" NSErrors, such as tagged-pointer representations of small error enums, or a tagged function pointer to a deferred error type constructor. Rename the runtime function to something a bit more descriptive of its real purpose, not its implementation.

Swift SVN r27209
2015-04-10 17:37:40 +00:00
Doug Gregor
5f4bea74e1 Open existential uses of dynamic-Self methods and initializers in the constraint solver.
Previously, we were only opening the existentials as part of
constraint application, which involved some ugly, redundant code. This
should be NFC because it's just moving the existential opening
operation earlier, but it's a step toward opening up all existential
references.

Swift SVN r27190
2015-04-09 23:58:55 +00:00
Doug Gregor
85b89ba112 Tighten up our handling of erasure of opened existentials. NFC
Swift SVN r27189
2015-04-09 23:58:54 +00:00
Roman Levenstein
d1698ba1cb Use a _bridgeable suffix for newly introduced fast bridging functions. NFC.
Dave explained that stdlib usually uses the suffix notation in such cases. This change follows his advice.

Swift SVN r27177
2015-04-09 20:59:42 +00:00
Doug Gregor
7fc1ac61ab Revert "Kill the sequence number in OpenedExistential."
This reverts r27106; we're going to need this for recovery.

Swift SVN r27170
2015-04-09 15:51:03 +00:00
Doug Gregor
6755aaf6b9 Revert "Simplify constraint application now that open-existentials are more local."
This reverts r27107; we're going to need this code for recovery.

Swift SVN r27169
2015-04-09 15:51:01 +00:00
Doug Gregor
d6dad48548 Simplify constraint application now that open-existentials are more local.
Remove some unnecessary boilerplate when working with ExprRewriter and
eliminate some closeExistential() call sites that are no longer
useful. NFC.

Swift SVN r27107
2015-04-07 23:22:06 +00:00
Doug Gregor
26ffdc7ace Kill the sequence number in OpenedExistential.
Because we're binding opened existentials early now, we no longer need
to be able to sort them to bind them late. NFC

Swift SVN r27106
2015-04-07 23:22:05 +00:00
Doug Gregor
595fb8c94f Introduce lvalue open-existential expressions and start using them.
Place OpenExistentialExprs for references to lvalue subscripts or properties
(in protocol extensions) via existentials just outside of the member
or subscript reference, rather than far outside the expression. This
gives us a tighter bound on the open-existential expressions without
introducing the post-pass I was threatening.

OpenExistentialExprs just outside of lvalue member/subscript are
themselves lvalues. Resurrect John's OpenOpaqueExistentialComponent to
handle the opening of a (materialized) existential lvalues as an
lvalue path component. This has the nice effect of codifying the
formal access rules for opened existentials as well as handling inout
on opened existentials appropriately.

Big thanks to John for talking through the model with me and leaving
dead code around for me to use.

Swift SVN r27105
2015-04-07 23:22:05 +00:00
Roman Levenstein
a762fb2b94 Teach bridgeFromObjectiveC to make use of the knownConditionallyBridgeFromObjectiveC/knownForceBridgeFromObjectiveC
If a conformance to _BridgedToObjectiveC is statically known, generate a more efficient code by using the newly introduced library functions for bridging casts.
This covers the cases arising from the source code, but does not cover any casts resulting from SIL optimizations. Those will be covered by the subsequent commit.

This change is to stay in CSApply for now. But the plan is to make the bridged casts optimization a guaranteed optimization. Once it is done, there is no need to lower the bridged casts in a special way inside Sema, because they all can be handled by the optimizer in a uniform way. This would apply to bridging of Error types too.

Swift SVN r27101
2015-04-07 22:53:56 +00:00
Joe Groff
ad0d20c07a Fold "AbstractCC" into SILFunctionType::Representation.
These aren't really orthogonal concerns--you'll never have a @thick @cc(objc_method), or an @objc_block @cc(witness_method)--and we have gross decision trees all over the codebase that try to hopscotch between the subset of combinations that make sense. Stop the madness by eliminating AbstractCC and folding its states into SILFunctionTypeRepresentation. This cleans up a ton of code across the compiler.

I couldn't quite eliminate AbstractCC's information from AST function types, since SIL type lowering transiently created AnyFunctionTypes with AbstractCCs set, even though these never occur at the source level. To accommodate type lowering, allow AnyFunctionType::ExtInfo to carry a SILFunctionTypeRepresentation, and arrange for the overlapping representations to share raw values.

In order to avoid disturbing test output, AST and SILFunctionTypes are still printed and parsed using the existing @thin/@thick/@objc_block and @cc() attributes, which is kind of gross, but lets me stage in the real source-breaking change separately.

Swift SVN r27095
2015-04-07 21:59:39 +00:00
Doug Gregor
82f634624c Handle uses of mutating methods of protocol extensions on existentials.
To properly deal with mutating methods on protocol extensions accessed through an existential (such as a property or subscript setter), open the existential as an lvalue so that its value can be mutated. 

As part of this, make sure we form any OpenExistentialExprs at the top level of the expression if we didn't manage to build them sooner. This is a fairly ad hoc approach that I am not happy with (these could probably be placed better with a post-pass), but the cost of opening too early is fairly minimal.

Swift SVN r27068
2015-04-07 06:01:19 +00:00
Doug Gregor
cdd5850707 Simplify the tracking of opened existentials in CSApply. NFC
Swift SVN r27065
2015-04-07 06:01:15 +00:00
Doug Gregor
7d6f2accd9 Improve use of properties/subscripts of protocol extensions via existentials.
Improves the placement of open-existential expressions when accessing
a property or subscript declared in a protocol extension. We need to
delay until the load occurs when the property/subscript has a usable
setter.

Swift SVN r27064
2015-04-07 06:01:14 +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
Doug Gregor
f4d98da668 Support the use of members of protocol extensions on existential types.
To use members of protocol extensions on existential types, we
introduce an OpenExistentialExpr expression to open up the existential
type (into a local archetype) and perform the operations on that local
archetype.

Unlike with uses of initializers or dynamic-Self-producing
methods of protocols, which produce similar ASTs, we have the type
checker perform the "open" operation and then track it through
constraint application. This scheme is better (because it's more
direct), but it's still using a simplistic approach to deciding where
the actual OpenExistentialExpr goes that needs improvement.

Swift SVN r26964
2015-04-04 00:00:14 +00:00
Chris Lattner
8a07a85a91 Progress towards let/else:
- Implement SILGen for conditional multi-pattern PBD's.
 - Have the type checker check where clauses on PBDs.
 - Change the AST to represent complex if/let PBD's with
   composed PBDs instead of breaking them down. For example, 
   represent:
     if let x? = foo(), y? = bar() where x == y {
   as a single PBD in a StmtCondition instead of representing
   it as three entries in the condition.

The later change is good for AST/source fidelity as well as providing
a cheap way to exercise all the logic I'm building.



Swift SVN r26959
2015-04-03 22:46:34 +00:00
Chris Lattner
2e188bda20 Fix <rdar://problem/20365753> Bogus diagnostic "refutable pattern match can fail"
We do this by banning single-element tuple patterns with a label (in most cases).
We now produce:
x.swift:2:8: error: label is not allowed on single element tuple pattern
  let (responseObject: Int?) = f()
       ^
x.swift:2:7: note: remove the parentheses to make this a type annotation
  let (responseObject: Int?) = f()
      ^                    ~
                           
x.swift:2:8: note: remove the label to make this a tuple pattern
  let (responseObject: Int?) = f() 
       ^~~~~~~~~~~~~~~




Swift SVN r26898
2015-04-02 20:57:50 +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
Jordan Rose
b6c5872889 Account for Optional<Void> in comparing function types for ABI compatibility.
Fixes a crash when the user uses such a type. (Which is probably a
mistake--they probably meant to make the whole closure optional--but
that's another issue.)

rdar://problem/20163908

Swift SVN r26274
2015-03-18 18:55:54 +00:00
Doug Gregor
1511d7a15a Start building conformances when requested in the AST.
Previously, we would require the type checker to be able to build a
conformance, which meant we would actually have to lie in the AST
about having a conformance (or crash; we did the form). Now, we can
form the conformance in the AST and it will be checked in the type
checker when needed. The intent here is to push conformance creation
into the conformance lookup table.

To get here, we had to stop relying on the broken, awful,
ASTContext-wide conformance "cache". A proper cache can come back once
the model is sorted out.

Swift SVN r26250
2015-03-18 04:31:22 +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 Willmore
bad04371f0 Insert parentheses around expression before appending 'as T', if
necessary. Wrap forced optional fixit in parens if necessary.

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

Swift SVN r26189
2015-03-16 20:52:18 +00:00
Chris Lattner
3e3f568179 By far, the most common use of Lexer::getLocForEndOfToken is in
conjunction with .fixItInsert().  As such, introduce a helper named
.fixItInsertAfter() that does what we all want.  Adopt this in various
places around the compiler.  NFC.



Swift SVN r26147
2015-03-15 05:30:04 +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
Devin Coughlin
749f8500d6 Sema: Emit Fix-Its for potential unavailability
We now suggest up to three Fix-Its for each reference to a potentially
unavailable symbol: one to wrap the reference in an if #os(...) { ... }
guard (if possible), one to add an @availability attribute to an enclosing
property or method (if possible), and one to add an @availability attribute
to an enclosing class/struct/extension, etc. or global function.

The goal here is not to infer the "best" Fix-It but rather to ensure
discoverability of #os() and @availability attributes. We want the user, when
faced with an availability diagnostic, to be aware of the tools in her toolbox
to deal with it.

This is still missing QoI improvements, including Fix-Its to update
existing @availability attributes and more precise wording in diagnostics
(e.g, "initializer" instead of function, "class" instead of "type"). These
improvements will come in later commits.

Swift SVN r26073
2015-03-12 23:51:11 +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
Jordan Rose
772a124b63 Compute captures after all bodies have been type-checked, as a separate pass.
We already have the restriction that captures can't be computed until
everything is type-checked, but previously we tried to compute captures
/immediately/ after a closure was type-checked. Unfortunately, we either
type-checked too early (before @noescape was propagated onto multi-statement
closures) or too late (trying to compute autoclosure captures at the point
the autoclosure was introduced).

Now, all closure captures are computed after type-checking, and local
function captures as well. They also more consistently reuse the capture
list of nested closures/functions. Because captures can be computed on
demand, there is now a flag on CaptureInfo for whether we've computed
captures yet. Note that some functions will never have captures computed,
namely those that are not in a local context.

rdar://problem/19956242

Swift SVN r25776
2015-03-05 02:33:37 +00:00
Joe Pamer
23458ce268 Fix constraint application on single expression closure bodies
So, it turns out that if the type checker were to re-write the Expr node of a single-expression closure body, we were never writing the modified body Expr back into the closure Expr.
This meant that if the closure expression's body were something like 'self.foo', we'd leave an UnresolvedDotExpr in the AST and most likely end up crashing down the line.

This has been responsible for about 4700 crash reports over the past several months. (Though, oddly enough, we didn't seem to hit it in the crash suite.) Thanks to Argyrios for pushing on this one!

rdar://problem/19840785

Swift SVN r25774
2015-03-05 01:57:25 +00:00
Joe Groff
5fa20867e9 SILGen: Implement thunking for C function pointer conversions.
If we have a C function pointer conversion, generate a thunk using the same logic we use for ObjC method thunks, and emit a pointer to that thunk as the C function pointer value. (This works for nongeneric, nonmember functions; generics will additionally need to apply generic parameters within the thunks. Static functions would need to gather the metatype as well.)

Swift SVN r25653
2015-03-01 06:18:58 +00:00
Devin Coughlin
b906a54fbd [Sema] Remove unnecessary check for potentially unavailable inferred type parameter actuals
This commit removes an unnecessary check that ensured that inferred type
parameter actuals are available. This check is unnecessary because the
declaration for any inferred actual type parameter (i.e., a generic type
argument at the callsite) must be referred to in an enclosing type refinement
context. If the inferred type is not available, this reference will cause a
diagnostic to be emitted, so we don't need to emit another diagnostic for the
inferred actual. Further, this diagnostic was potentially confusing because the
point at which we emitted the diagnostic did not directly mention the type
name (because it was inferred).

This commit also beefs up testing of diagnostics for potentially unavailable
type parameters.

Swift SVN r25540
2015-02-26 02:39:16 +00:00
Chris Willmore
fc561cd7ea Fix errant fixit "as!" -> "as?!"
"let x: Int? = 5 as Any as! Int" now suggests changing "as!" to "as?"
instead of "as?!".

rdar://problem/19883819

Swift SVN r25490
2015-02-23 22:59:46 +00:00
Chris Lattner
b0db122925 fix rdar://19763676 - False positive in @noescape analysis triggered by parameter label
This changes CSApply to propagate the noescape (and noreturn) bits into a closureexpr even
when the closure type is undergoing some sort of conversion, since the conversion doesn't
affect the semantics of these bits.



Swift SVN r25454
2015-02-21 01:36:19 +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
Nadav Rotem
f5910bbe62 Fix a warning.
Swift SVN r25354
2015-02-17 18:43:17 +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
Joe Pamer
a3122ecf92 Provide a better, tailored diagnostic for result-type mismatches. (rdar://problem/19800727)
These changes include ChrisW's code review feedback.

Swift SVN r25340
2015-02-17 01:57:20 +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
Chris Willmore
5b51620d1d Be more tolerant of invalid protocol conformances when applying constraint solution.
* Lift NormalProtocolConformance::hasTypeWitness() to
  ProtocolConformance. This method can be used to determine whether a
  potentially invalid conformance has a particular type witness.

* ProtocolConformance::getWitness() may return nullptr if the witness
  does not exist.

* In TypeChecker::getWitnessType(), don't complain that a builtin
  protocol is broken if it's the conformance that's broken.

* ConformanceChecker should only emit diagnostics from within calls to
  checkConformance().

* TypeChecker::conformsToProtocol() now returns true if the type
  has a declared conformance to the protocol, regardless of whether the
  conformance is valid. Clients must check the validity of the
  ProtocolConformance themselves if necessary.

<rdar://problem/19495341> Can't upcast to parent types of type constraints without forcing

Swift SVN r25326
2015-02-16 22:41:52 +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 Groff
94be8424ff Sema: Type checking for C function pointers.
Since a function pointer doesn't carry any context, we can only form a C function pointer from a static reference to a global function or a context-free local function or closure. (Or maybe a static function applied to its metatype, but that's not handled here yet.) As a placeholder for a to-be-bikeshedded surface syntax, let the existing @cc(cdecl) attribute appear in source text when the -enable-c-function-pointers frontend flag is passed.

Swift SVN r25308
2015-02-16 00:41:53 +00:00
Joe Pamer
d6b362ee81 Don't re-typecheck closures that have been transformed to match a void-returning contextual type. Due to their transformation, these single-expression closures will now appear as multi-expression, so a re-typecheck could potentially result in bogus type errors. (Fixes rdar://problem/19806558, which is a regression from Xcode 6.4 seed 1.)
Swift SVN r25278
2015-02-13 19:23:30 +00:00
Jordan Rose
6f87cff84e Be more consistent about requiring "self." in closures.
"self." is required whenever it causes a potentially escaping closure to
capture 'self'. This happens not just in plain ClosureExprs, but in local
functions as well. In addition, even no-escape ClosureExprs can require
self to be captured in a /parent/ closure, which may be potentially-escaping.

Swift SVN r25173
2015-02-11 07:20:29 +00:00
Chris Willmore
ee342c15c4 <rdar://problem/19770981> Swift doesn't recognize that NSString has been as! converted to String and continually suggests conversion
If appending 'as T' to an expression in a fixit, also suggest
parentheses around the resulting expression if it would otherwise be
parsed incorrectly.

Swift SVN r25168
2015-02-11 04:49:34 +00:00
Denis Vnukov
f585966027 Fix for rdar://19634354, SourceKit crashes while defining a function inside a while loop
Avoid re-typechecking of boolean literals in code completion



Swift SVN r25136
2015-02-10 21:47: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