Commit Graph

673 Commits

Author SHA1 Message Date
Doug Gregor
6ae3d5db47 Only force immediate closure of an open existential when we opened one.
Fixes rdar://problem/21597711.

Swift SVN r30190
2015-07-14 18:11:13 +00:00
Chris Lattner
9ae1ff707c Add a new DisableStructuralChecks option to typeCheckExpr which allows CSDiag to
avoid emitting this diagnostics.  They are always irritating to it because they
are duplicative with whatever the actual type constraint problem is.  It also
interferes with there recursive structure of the problem.

Disabling them also allows us to shorten the black list of expr nodes that cannot
be handled in typeCheckIndependentSubExpression.



Swift SVN r30122
2015-07-11 22:28:40 +00:00
Chris Lattner
3edd82837f remove the isDiscarded bool from TypeChecker::solveForExpression
and getTypeOfExpressionWithoutApplying and change typeCheckExpression
to take it as a single-entry OptionSet, in prep for getting other 
options.  NFC.


Swift SVN r30121
2015-07-11 22:06:45 +00:00
Slava Pestov
fd141bead9 Sema: Remove unused inExpression parameter from ASTContext::getBridgedToObjC(), NFC
Progress on <rdar://problem/21215099>.

Swift SVN r29968
2015-07-08 06:49:27 +00:00
Chris Lattner
5d3d653784 remove unused field.
Swift SVN r29938
2015-07-07 05:58:11 +00:00
Chris Lattner
acecd187a3 fix <rdar://problem/21553065> Spurious diagnostic: '_' can only appear in a pattern or on the left side of an assignment
... by reimplementing the DiscardAssignmentExpr checker in MiscDiagnostics 
instead of being in CSApply (which gets run on partial expression ASTs).

Also, when type checking and salvaging an expression fails, do not generate 
structural diagnostics in addition to the type checker diagnostics, they are 
just noise.



Swift SVN r29937
2015-07-07 05:52:17 +00:00
Slava Pestov
e598bfb21b Sema: Fix typos in comments, NFC
Swift SVN r29933
2015-07-07 04:41:49 +00:00
Chris Lattner
c619fd0fda fix <rdar://problem/21087089> Fixit for <RawOptionSetType>.allZeros
to improve swift 1 -> 2 migration.



Swift SVN r29903
2015-07-02 23:45:12 +00:00
Doug Gregor
d44651f366 Allow type witness resolution for unqualified lookups.
Fixes rdar://problem/21512599.

Swift SVN r29894
2015-07-02 18:29:13 +00:00
Chris Lattner
f04d2bb828 Implement <rdar://problem/20336036> QoI: Add cast-removing fixit for "Forced cast from 'T' to 'T' always succeeds"
Swift SVN r29876
2015-07-02 00:28:44 +00:00
Chris Lattner
2afc0dbaa9 Minor improvements:
- Remove a weird special case for literals from TypeChecker::typeCheckCondition.
 - Enhance FailureDiagnosis::getTypeOfIndependentSubExpression to know about situations
   where recursive type checks fail (in some nested situation) but still produce a type
   for the top level of the expr tree.
 - Remove dead code from CSApply now that you can't branch on Builtin.Int1.

The first & second combine to slightly improve one case I've been looking at in 
test/expr/expressions.swift.




Swift SVN r29860
2015-07-01 22:53:37 +00:00
Doug Gregor
b646af0ec2 Eliminate some unnecessary uses of getAllArchetypes(). NFC
Swift SVN r29843
2015-07-01 18:14:52 +00:00
Slava Pestov
ee07ddc2bf Sema: Fix crash calling protocol extension methods returning Self on existential
In r26737, Sema was changed to not wrap Self occurring in a protocol
extension in a DynamicSelf. The commit message was rather terse but
I believe this is because the metadata for Self is bound to the static
base type, not the runtime base type.

However, we still need to substitute Self in the return type for the
static base type in the case where the base is an existential,
otherwise we get an open existential type leaking out.

Also remove the default argument for replaceCovariantResultType(),
every call site passed in a value and it seems bad to omit it on
accident.

Fixes <rdar://problem/21433694>.

Swift SVN r29802
2015-06-30 05:36:00 +00:00
Chris Willmore
3bf7708c1f Put backquotes around reserved argument labels in fixits.
<rdar://problem/21392294> Swift 2 Migration failures

Swift SVN r29754
2015-06-27 03:03:22 +00:00
Chris Lattner
e41c23801f A bunch of conflated changes:
- Fix TypeCheckExpr.cpp to be more careful when propagating sugar from an 
   argument to the result of the function.  We don't want to propagate parens,
   because they show up in diagnostics later.

 - Restructure FailureDiagnosis::diagnoseFailure() to strictly process the tree
   in depth first order.  Before it would only do this if contextual typing was
   unavailable, leading to unpredictable inconsistencies between diagnostics.

 - Always perform diagnoseContextualConversionError early, as part of the thing
   that calls the visitor, instead of in each visit method.  This may change in
   the future, but is a simplification for now.

 - Make the operator processing code handle the "candidate is an exact match"
   case by emitting a diagnostic indicating that the result type of the operator
   must not match expectations, instead of emitting the silly things like
   "binary operator '&' cannot be applied to two Int operands" which is obviously
   false.

These changes lead to minor improvements across the testsuite, and should make the
diagnostics more predictable for more complex real-world ones, but I haven't gone
through the radars yet.  

Major missing pieces:
 - CallExpr isn't using the same logic that the operators are.
 - When you have a near match (only one argument mismatches) we should specifically
   complain about that argument, instead of spewing an entire argument list.
 - The noescape function attr diagnostic is being emitted twice now.
 



Swift SVN r29733
2015-06-26 07:29:05 +00:00
Chris Lattner
da103337b9 fix <rdar://problem/20912019> passing unmaterialized r-value as inout argument
Swift SVN r29573
2015-06-23 17:44:47 +00:00
Jordan Rose
38c3bce48f Tweak diagnostics for invalid instantiation expressions.
rdar://problem/21334185

Swift SVN r29570
2015-06-23 16:15:42 +00:00
Slava Pestov
f0d98f3af3 Sema: Fix problems with coerceClosureExprToVoid()
- If the closure being rewritten was nested inside of another
  closure, we would rewrite the nested closure into a new closure
  having cs.DC as its parent, rather than the DeclContext of the
  outer closure as required.

- If the closure being rewritten had nested closures, the parent
  DeclContext of the nested closures was set to the old closure,
  not the new one.

Fix both problems by having coerceClosureExprToVoid() modify the
closure in place instead of creating a new closure.

Fixes <rdar://problem/20931915>.

Swift SVN r29563
2015-06-23 04:49:41 +00:00
Slava Pestov
9deaf6c851 Sema: Fixes for partial application of protocol methods
Previously the placement of the OpenExistentialExpr was determined
entirely from the natural argument count of the function.

There was a hack to add any missing OpenExistentialExprs at the top
level, but this didn't work if the method had a Self return value
and there were intermediate expressions, eg, if someMethod has a
Self return, foo(anExistential.someMethod) would generate a
diagnostic about open existentials.

Change ExprRewriter to use a new existential placement algorithm
that instead walks up the expression stack to determine the outermost
function application of an existential base, and insert the
OpenExistentialExpr there.

Progress on <rdar://problem/21289579>.

Swift SVN r29448
2015-06-17 07:42:47 +00:00
Slava Pestov
8504517e15 Sema: Fix opened existentials leaking out in diagnostics
getTypeOfIndependentSubExpression() might replace some values in the
given expression with OpaqueValueExprs, but if the type checker
decided to insert the corresponding OpenExistentialExpr at the top
level, the AST would now be in an inconsistent state, since
getTypeOfIndependentSubExpression() does not return a new expression
to the caller.

Ideally we would separate out type checking from expression rewriting
so that the latter is only performed when we know the expression
type checks, but that is a bigger project. For now, erase open
existentials after re-typechecking a sub-expression.

Fixes <rdar://problem/20598568>.

Swift SVN r29400
2015-06-16 17:25:46 +00:00
Slava Pestov
6c1ee5537a Sema: Moved ExprWalker class out of applySolution()
Perhaps it's a matter of taste but that function is already
quite long. NFC

Swift SVN r29380
2015-06-15 02:00:50 +00:00
Slava Pestov
1052d095c4 Sema: Fixes for conformance between higher-kind metatypes
For a concrete type A and protocol type P, A <c P now implies
A.Type <c P.Type, not just A.Type < P.Type. This in turn means
that A.Type.Type <c P.Type.Type. To make the coercion work,
recursively peel off metatype layers when collecting conformances
and in a similar situation in IRGen.

Swift SVN r29377
2015-06-15 02:00:47 +00:00
Joe Groff
d7b9ae72aa Sema: Require '.init' when constructing from a dynamic metatype.
This makes it clearer that expressions like "foo.myType.init()" are creating new objects, instead of invoking a weird-looking method. The last part of rdar://problem/21375845.

Swift SVN r29375
2015-06-14 19:50:06 +00:00
Slava Pestov
6550c7a858 Sema: Fix subtyping between metatypes and existential metatypes
X.Protocol is an instance of Y.Type only if X conforms to Y. Since X
is a protocol, this is only true if X contains Y and Y is
self-conforming.

Note that this updates some tests that actually contained invalid code.

Fixes <rdar://problem/20915927>.

Swift SVN r29349
2015-06-08 07:59:16 +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
Slava Pestov
086b9961ba Sema: Don't allow static member access on protocol metatypes
If P is a protocol, calling static methods or constructors
via values of type P.Protocol makes no sense, so let's prohibit
this.

Fixes <rdar://problem/21176676>.

Swift SVN r29338
2015-06-07 10:16:23 +00:00
Slava Pestov
322f58d8b1 Sema: Tighten up existential vs generic type parameter distinction
Rename existentialConformsToSelf() to existentialTypeSupported(). This
predicate is the "protocol has no Self or associated type requirements"
check, which is a looser condition than self-conformance. This was being
tested to see if the user could refer to the protocol via an existential
type.

The new existentialConformsToSelf() now checks for protocol being @objc,
and for the absence of static methods. This is used as part of the
argument type matching logic in matchType() to determine if the
existential can be bound to a generic type parameter.

The latter condition is stricter, for two reasons:

1) We allow binding existentials to multiple type parameters all sharing
   the same generic type parameter T, so we don't want the user to be
   able to see any static methods on T.
2) There is an IRGen limitation whereby only existentials without witness
   tables can be passed in this manner.

Using the above, the representsNonTrivialGenericParameter() function
has been renamed to canBindGenericParamToExistential(). It now allows
an existential type to be bound to a generic type parameter only under
the following circumstances:

A) If the generic type parameter has no conformances, the match is allowed.

B) If the generic type parameter has at least one conformance, then all
   of the conformances on the generic type parameter must be
   existentialConformsToSelf() (condition 1 above), and all conformances
   on the existential must be @objc (condition 2 above).

Fixes <rdar://problem/18378390> and <rdar://problem/18683843>, and lays
the groundwork for fixing a few other related issues.

Swift SVN r29337
2015-06-07 10:16:21 +00:00
Slava Pestov
7319a97ab4 Sema: Rewrite witness method calls as ApplyExpr + DeclRefExpr
Special-casing these as MemberRefExprs created an asymmetry
where unbound archetype instance methods (<T : P> T.f) could
not be represented. Treating class and protocol methods
uniformly also eliminates a handful of special cases around
MemberRefExpr.

SILGen's RValue and call emission peepholes now have to know
about DeclRefExprs that point to protocol methods.

Finally, generalize the diagnostic for partially applied
mutating methods to any partially applied function with an
inout parameter, since this is not supported.

Fixes <rdar://problem/20564672>.

Swift SVN r29298
2015-06-04 15:57:58 +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
Slava Pestov
d43cef47a4 Sema: coerceImplicitlyUnwrappedOptionalToValue() can't return nullptr
NFC

Swift SVN r29264
2015-06-03 00:01:34 +00:00
Doug Gregor
e2bec4377b Don't allow existentials to be used where we need a witness table.
If we end up trying to form a substitution where the replacement type
is an existential and there is a conformance to a non-@objc protocol
(i.e., a conformance where a witness table is required), complain in
Sema rather than crashing in IRGen. Fixes rdar://problem/21087341, but
the existential/generic interaction is still quite broken.

Swift SVN r29133
2015-05-29 05:02:18 +00:00
Jordan Rose
d3eaad8334 Look through CaptureListExpr when checking C function pointer conversions.
This results in a more correct diagnostic being printed for closures with
capture lists, rather than claiming that they aren't literal closures.

Swift SVN r29058
2015-05-27 04:43:35 +00:00
Joe Groff
5e0361647d Sema: Admit partial applications of non-'mutating' methods.
Our implementation of partial_apply and currying is robust enough to handle these cases now. Mutating methods are still problematic since capturing would violate 'inout' semantics. (Maybe we could support 'mutating' partial applications as @noescape closures, some day.)

Swift SVN r28992
2015-05-24 19:39:07 +00:00
Joe Groff
a14b83ba2c SILGen: Allow partial applications of enum cases.
This isn't as straightforward as it should be, since EnumElementDecls aren't AbstractFunctionDecls, but luckily there's only one trivial curry level with a thin metatype parameter.

Swift SVN r28991
2015-05-24 19:39:02 +00:00
Chris Lattner
258f91f60b revert: r28801 - improve the source location information for { and } in a ClosureExpr.
Instead, provide the location of the { in a closure expr to the argument formation as 
part of the datastructure already used to manage implicit closure arguments in the parser.


Swift SVN r28818
2015-05-20 03:19:56 +00:00
Chris Lattner
66916683a2 improve the source location information for { and } in a ClosureExpr, by
actually tracking it. NFC.


Swift SVN r28801
2015-05-20 00:10:04 +00:00
Chris Lattner
52bf6623e8 improve typing, NFC.
Swift SVN r28800
2015-05-19 23:42:43 +00:00
Chris Lattner
4b4f909d1a Several tangled up changes:
- Enable 'catch is NSError' and 'catch let e as NSError {' patterns to
   a) work, and b) be considered to be exhaustive catches.  This enables
   people to catch an error and *use* it as an NSError directly, instead
   of having to do boiler-platey cases.  This is particularly important
   for the migrator.

 - Do not warn about non-noop coersion casts (like "_ as NSError" when
   matching an ErrorType), since they provide useful type adjustment to
   the subpattern.  Still warn on noop ones.

 - Simplify CatchStmt::isSyntacticallyExhaustive to use 
   Pattern::isRefutablePattern.  Add a FIXME, because the parser is guiding
   closure "throws" inference before the pattern is type checked, which means
   that it is incorrect (but only in subtle cases).

 - When diagnosing pointless 'as' patterns like:

  switch 4 {
  case _ as Int: break 

say "'as' test is always true" instead of "'is' test is always true".




Swift SVN r28774
2015-05-19 17:32:32 +00:00
Doug Gregor
4fa6201283 Make sure to close existentials that come from constructions.
Fixes rdar://problem/20780797.

Swift SVN r28697
2015-05-18 05:11:48 +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
David Farler
dbd78643ce Build fix: Adventure triggers nullptr dereference
Fix a nullptr dereference when looking for a base expression
in a member access. Remove use of Optional<T*>, it wasn't providing
any value versus nullptr checking.

Swift SVN r28648
2015-05-15 23:36:33 +00:00
Doug Gregor
64d837d605 Don't emit a Fix-It adding an argument label for a trailing closure.
Fixes rdar://problem/20926939.

Swift SVN r28642
2015-05-15 21:27:29 +00:00
David Farler
80571cf916 Check base expr when determining mutability of a member
When in an initializer, we allow setting into immutable properties
provided that the type of base in `base.member` matches that of that
initializer's containing type. This was an approximation for allowing
full access into `self` during initialization but this doesn't work when
passing in a different struct of the same type because that struct
should be still be immutable.

Check whether the base of the member access is the implicit self
parameter of the initializer before allowing mutation.

rdar://problem/19814302

Swift SVN r28634
2015-05-15 18:48:54 +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
922db0185d Initializers in protocol extensions are convenience initializers.
... and since they can only use required initializers (or those found
in a protocol), one can use them when constructing metatypes. Fixes
rdar://problem/20739719.

Swift SVN r28364
2015-05-09 04:35:53 +00:00
Chris Willmore
7b5767261b Extend the fix from r28200 to all UnresolvedMemberExpr instances in CSApply.
<rdar://problem/20596042> SIL gen crasher in rvalue emission: unexpected storage type that differs from type-of-rvalue

Swift SVN r28337
2015-05-08 23:52:33 +00:00
Doug Gregor
b5bc4c2b47 Start encoding a base declaration in name lookup results.
Swift SVN r28301
2015-05-08 00:26:18 +00:00