Commit Graph

2525 Commits

Author SHA1 Message Date
Doug Gregor
50cb31c5c4 [Constraint solver] Make 'defaultable' constraint creation lazy. 2016-10-23 00:15:50 -07:00
Doug Gregor
1b8d4399f3 [Constraint solver] Make optional object constraint construction lazy. 2016-10-23 00:15:50 -07:00
Doug Gregor
12b6894849 [Constraint solver] Address some of Mark's comments. NFC 2016-10-22 23:27:41 -07:00
Doug Gregor
d2cb83b4c7 [Constraint solver] Make checked cast constraint generation lazy.
Fixes rdar://problem/27148148.
2016-10-22 22:57:25 -07:00
Doug Gregor
af4d44e013 [Constraint solver] Lazily handle conformance-related constraints.
The code was already there for them; make sure we use it
consistently.
2016-10-22 22:20:03 -07:00
Doug Gregor
8451536e89 [Constraint solver] Lazily allocate DynamicTypeOf constraints. 2016-10-22 22:20:03 -07:00
Doug Gregor
ed71fd08c3 [Constraint solver] Lazily construct ApplicableFunction constraints. 2016-10-22 22:20:03 -07:00
Doug Gregor
41cd760682 [Constraint solver] Clean up existential metatype handling.
Our existential metatype conversion/conformance handling in the type
checker was tripped up the refactoring to lazily introduce
constraints, because it dependended on the otherwise-unused
TypeMatchKind::ConformsTo. Eliminate TypeMatchKind::ConformsTo and
simplify the code here.
2016-10-21 14:31:11 -07:00
Doug Gregor
7519d83007 [Constraint solver] Start lazily building relational constraints.
Start migrating the main ConstraintSolver::addConstraint() entrypoint
for relational constraints over to the model where it simplifies
first, then only creates a new constraint if the constraint it built
cannot be solved. This covers only the main relational
constraints--there are a number of other relational constraint kinds
to handle this way.
2016-10-21 14:03:56 -07:00
Doug Gregor
bdd27e9982 [Constraint solver] Lazily generate member constraints.
When adding a new member constraint, try to immediately simplify it,
generating a constraint when it cannot be resolved. Note that we also
generate constraints in top-level failure cases, so that the
diagnostics machinery can find it after-the-fact.
2016-10-21 14:03:56 -07:00
Doug Gregor
337558dcb9 [Constraint solver] Allocate 'restricted' constraints lazily.
When adding a 'restricted' constraint, go straight into the 'simplify'
operation. A new constraint will only be allocated and recorded if it
cannot be immediately simplified. To get here, be more rigorous about passing TMF_GenerateConstraints through simplifyRestrictedConstraint().
2016-10-21 14:03:56 -07:00
Doug Gregor
a4fc0aaad2 [Constraint solver] Don't reprocess constraints known to be unsolvable.
When we're creating a new constraint because we couldn't solve it (and
need to record the result), do so without trying to simplify it yet
again. It's just wasted work.
2016-10-21 14:03:56 -07:00
Doug Gregor
b69d01eef2 [Constraint solver] Remove unused 'class' constraints. NFC
We're not using these now. Someday, maybe, but it will be easy to
bring back this code if we find that we need it later.
2016-10-21 14:03:56 -07:00
Doug Gregor
483dfdcaa1 [Constraint solver] Remove 'archetype' constraint kind. NFC
Archetype constraints haven't been needed since we implemented SE-0091.
2016-10-21 14:03:56 -07:00
Doug Gregor
14a4fdbc50 [Constraint solver] Use OptionSet rather an 'unsigned'. NFC 2016-10-21 14:03:56 -07:00
Doug Gregor
e094adb50a [Constraint solver] Use custom addConstraint entrypoints consistently.
When adding constraints into the constraint system, don't immediately
allocate a Constraint and add it via the most-general
addConstraint(). Instead, go through a more specific entrypoint (e.g.,
addValueMemberConstraint, addRestrictedConstraint, etc.), so we can
start phasing out the general "add an already-formed constraint"
function. NFC
2016-10-21 14:03:56 -07:00
swift-ci
a4104d87d5 Merge pull request #5300 from DougGregor/constraint-solver-cleanups 2016-10-14 16:59:09 -07:00
Pavel Yaskevich
578e36a7e1 [Type Checker] SR-2505: Fix "Call arguments did not match up" assertion
Always check arguments of the tuple type against corresponding parameters,
otherwise for a single argument functions e.g. foo(_ a: Any) after SE-0046
type checker is going to produce incorrect solution.
2016-10-14 13:27:33 -07:00
Doug Gregor
2e800f4acc [Constraint system] Centralize the handling of "unsolved" returns in matchTypes. 2016-10-14 10:28:20 -07:00
Doug Gregor
49b833b51a [Type checker] Eliminate the 'literalConformanceProto' state on type variables.
The 'literalConformanceProto' field of
TypeVariableType::Implementation didn't take into account equivalence
classes of type variables. Eliminate it, and either look at the actual
expressions (for optimizing constraints during constraint generation)
or the actual constraints on a given type variable (for determining
whether to include optionals in the set of potential type variable
bindings).

(cherry picked from commit 6bdd9cfae5)
2016-10-13 16:22:01 -07:00
Doug Gregor
01fa24cc9b Speculatively revert "[Type checker] Eliminate the 'literalConformanceProto' state on type variables."
This reverts commit 6bdd9cfae5. This
commit *appears* to be breaking something in Dollar involving
inference with array literals and 'nil'; pull it back for more
investigation.
2016-10-12 09:20:16 -07:00
Robert Widmann
f59d5cb577 Normalize the way paren types are stripped in Sema. 2016-10-12 00:00:39 -04:00
Doug Gregor
6bdd9cfae5 [Type checker] Eliminate the 'literalConformanceProto' state on type variables.
The 'literalConformanceProto' field of
TypeVariableType::Implementation didn't take into account equivalence
classes of type variables. Eliminate it, and either look at the actual
expressions (for optimizing constraints during constraint generation)
or the actual constraints on a given type variable (for determining
whether to include optionals in the set of potential type variable
bindings).
2016-10-11 17:09:13 -07:00
Doug Gregor
c2b9759cd3 Simplify ConstraintSystem::getFixedTypeRecursive and use it consistently.
We had a few places that were performing ad hoc variants of
ConstraintSystem::getFixedTypeRecursive(); simplify it's interface so
we can use it everywhere consistently. Fixes rdar://problem/27261929.
2016-10-11 17:08:52 -07:00
Pavel Yaskevich
054e3e4eb4 [Diagnostics] SR-2242: Fix diagnostic when argument label is omitted 2016-09-23 17:17:59 -07:00
Slava Pestov
e97df4a285 Sema: Implicit conversion for single-expression closures of Never type
This fixes a usability regression with the removal of @noreturn
in Swift 3. Previously, it was legal to write this:

let callback: () -> Int = { fatalError() }

Now that the special @noreturn attribute has been replaced with
a Never type, the above fails to typecheck, because the expression
now has type 'Never', and we expect a value of type 'Int'.

Getting around this behavior requires ugly workarounds to force the
parser to treat the body as a statement rather than an expression;
for example,

let callback: () -> Int = { _ = (); fatalError() }

This patch generalized single-expression closures to allow
the 'Never to T' conversion. Note that this is rather narrow
in scope -- it only applies to closure *literals*, single-expression
ones at that, not arbitrary function *values*.

In fact, it is not really a conversion at all, but more of a
desugaring rule for single-expression closures. They can now be
summarized as follows:

- If the closure literal has contextual return type T and
  the expression has Never type, the closure desugars as
  { _ = <expr> }, with no ReturnStmt.

- If the closure literal has contextual return type T for some
  non-void type T, the closure desugars as { return <expr> };
  the expression type must be convertible to T.

- If the closure literal has contextual return type Void, and
  the expression has some non-Void type T, the closure
  desugars as { _ = <expr>; return () }.

Fixes <rdar://problem/28269358> and <https://bugs.swift.org/browse/SR-2661>.
2016-09-22 23:40:25 -07:00
Michael Ilseman
ed2522b384 [AST] Create ParameterTypeFlags and put them on function params
Long term, we want to refactor the AST to reflect the current
programming model in Swift. This would include refactoring
FunctionType to take a list of ParameterTypeElt, or something with a
better name, that can contain both the type and flags/bits that are
only specific to types in parameter position, such as @autoclosure and
@escaping. At the same time, noescape-by-default has severely hurt our
ability to print types without significant context, as we either have
to choose to too aggressively print @escaping or not print it in every
situation it occurs, or both.

As a gentle step towards the final solution, without uprooting our
overall AST structure, and as a way towards fixing the @escaping
printing ails, put these bits on the TupleTypeElt and ParenType, which
will serve as a model for what ParameterTypeElt will be like in the
future. Re-use these flags on CallArgParam, to leverage shared
knowledge in the type system. It is a little painful to tack onto
these types, but it's minor and will be overhauled soon, which will
eventually result in size savings and less complexity overall.

This includes all the constraint system adjustments to make these
types work and influence type equality and overload resolution as
desired. They are encoded in the module format. Additional tests
added.
2016-09-22 12:24:02 -07:00
Doug Gregor
6b91c5bb34 [Type checker] Handle explicit conversion of bridged generic types.
The type checker had some logic for performing specific checking for
explicit bridging casts of generic types based on knowledge of
Array/Dictionary/Set, but pretended no other bridged generic types
existed. That's incorrect now; simply require them to match exactly.

Fixes rdar://problem/27539951.
2016-09-14 11:00:30 -07:00
Doug Gregor
f99904ac66 Eliminate the useless flag -enable-experimental-collection-casts.
This eliminates a pile of now-dead code in:
  * The type checker, where we no longer have special cases for bridging conversions
  * The expression ASTs, where we no longer need to distinguish bridging collection up/down casts
  * SILGen, which no longer uses

Still to come is the removal of the
_(set|dictionary)Bridge(From|To)ObjectiveC(Conditional)? entrypoints
from the standard library. They're still used by some tests.
2016-08-19 21:17:10 -07:00
Doug Gregor
51529ae888 Eliminate the -enable-id-as-any flag; it's always on now anyway.
Simplify e.g., ASTContext::getBridgedToObjC(), which no longer needs
the optional return.

Eliminate the now-unused constraint kind for checking bridging to
Objective-C.
2016-08-19 21:17:09 -07:00
Joe Groff
b9bc62dc93 Sema: Make Hashable-to-AnyHashable a Subtype rather than Conversion.
This admits AnyHashable in collection downcasts, though runtime support for the dynamic cast still appears to be incomplete. rdar://problem/27869032
2016-08-16 17:14:34 -07:00
Slava Pestov
a485e525c5 Sema: Allow protocol typealiases to be accessed from expression context, as long as they don't depend on 'Self'
Suppose you have this protocol:

protocol P {
  typealias A = Int
  typealias B = Self
}

Clearly, 'P.B' does not make sense, because then the type parameter
would "leak out" of the existential container. However, 'P.A' is totally
fine, it just means 'Int'.

Previously, we would allow 'P.A' in type context, and diagnose on 'P.B'.

However, due to an oversight, neither one was allowed in expression
context, so for example you could not write 'P.A.self', even though
that should just mean 'Int.self'.

Fix this by generalizing performMemberLookup(), and fix up some
diagnostics to be more specific when something is wrong -- we want
to avoid talking about typealiases as 'static members', since that
doesn't really make much sense.

Fixes <https://bugs.swift.org/browse/SR-2314>.
2016-08-10 23:23:24 -07:00
Slava Pestov
d5c2f44c4e Sema: Clean up name lookup and fix a couple of bugs
Previously, qualified lookup would use a metatype to signal
to the LookupResultBuilder that conformance checks should not
be used to resolve protocol members found with a concrete
type base. This is too subtle for my taste. Add an explicit
flag and clean up some usages, fixing <rdar://problem/16123805>.

Also, clean up the 'CheckProtocolMembers' flag. We now set
when the base type is an archetype, and correctly handle
the concept of an abstract conformance. This finally
fixes <rdar://problem/22776964>.
2016-08-09 00:53:30 -07:00
John McCall
a6e1e87585 Add implicit conversions and casts from T:Hashable <-> AnyHashable.
rdar://27615802
2016-08-04 23:13:27 -07:00
Mark Lacey
d470fc292e Do not allow Any to satisfy more specific same-type constraints. (#3856)
For associated types inferred to be Any, we were allowing the type to
satisfy more specific same-type constraints, e.g. Element ==
Character (where Element is the associated type). This is clearly wrong.

The fix here is very specific to empty protocol compositions, and
removes some code in matchTypes() that doesn't make a lot of
sense. Looking back at the history, this was added in a commit that made
a handful of other changes, and it's not clear this particular change
was important for the issues that commit claimed to fix (and in fact
removing this regresses no tests).

Fixes rdar://problem/27515965.
2016-07-29 12:38:07 -07:00
Doug Gregor
202cf2e754 [SE-0111] Track function reference kinds in member references.
Extend the handling of function reference kinds to member references
(e.g., x.f), and therefore the logic for stripping argument labels. We
appear to be stripping argument labels from all of the places where it
is required.
2016-07-28 15:41:59 -07:00
Doug Gregor
a9536906ff [SE-0111] Drop argument labels on references to function values.
When referencing a function in the type checker, drop argument labels
when we don't need them to type-check an immediate call to that
function. This provides the semantic behavior of SE-0111, e.g.,
references to functions as values produce unlabeled function types,
without the representational change of actually dropping argument
labels from the type system.

At the moment, this only works for bare references to functions. It
still needs to be pushed through more of the type checker and more AST
nodes to work in the general case.

Keep this work behind the frontend flag
-suppress-argument-labels-in-types for now.
2016-07-28 15:40:11 -07:00
Doug Gregor
dad155705d [SE-0111] Store argument labels directly on object literal expressions. 2016-07-25 23:14:41 -07:00
Doug Gregor
5ca9f8852e [SE-0111] Store argument labels directly on unresolved member expressions. 2016-07-25 23:14:41 -07:00
Doug Gregor
493c0f2a0e [SE-0111] Store argument labels directly on dynamic subscript expressions. 2016-07-25 23:14:41 -07:00
Doug Gregor
33d8b469d4 [SE-0111] Store argument labels directly on subscript expressions.
Factor out the trailing storage of call arguments, since we'll need it
for a few different kinds of expression nodes. Use it for both
CallExpr (which already had this storage, albeit with a specialized
implementation) and now SubscriptExpr.
2016-07-25 23:14:41 -07:00
Doug Gregor
604adff1bd [SE-0111] Capture argument labels directly in CallExpr.
Yet another step on the way to SE-0111, capture the argument labels
(and their locations) directly in CallExpr, rather than depending on
them being part of the tuple argument.
2016-07-25 23:14:41 -07:00
Joe Groff
10a0eb1c13 Merge pull request #3710 from jckarter/turn-on-id-as-any
Turn on id as any
2016-07-25 18:58:27 -07:00
Joe Groff
3e4f7c2f29 Sema: Increase score of collection conversions.
Collection conversion should be avoided in favor of non-collection-converting solutions.
2016-07-25 15:10:52 -07:00
Doug Gregor
847b78245a [Type checker] Use argument labels from the expression for type-checking calls.
When we are type-checking calls, subscripts, or other call-like
expressions, use the argument labels provided by the various
expression nodes rather than those encoded in the tuple type. This
means that argument label matching now matches the callee
declaration's argument labels against the argument labels, without
relying on encoding the argument labels within types in the AST.

This refactor is a stepping stone torward SE-0111.
2016-07-25 13:27:35 -07:00
Joe Groff
82ca1cabd4 Sema: Score function conversions.
Break an ambiguity when overloads for e.g. [() -> ()] and [() throws -> ()] exist.
2016-07-25 06:40:41 -07:00
Joe Groff
b9d21b4540 Reinstate SE-0072. Revert "Pending QOI improvements, reinstate implicit bridging conversions."
This reverts commit 6542100d62. SE-0116
expands Objective-C `id` to Swift `Any`, moving the conversion into the
bridging mechanism rather than the type system.
2016-07-25 06:01:21 -07:00
Joe Groff
bbdca55dd0 Sema: Containers of any type can bridge when id-as-Any is enabled.
We no longer need to constrain the generic parameters to be ObjC-bridgeable.
2016-07-25 06:01:21 -07:00
Joe Groff
d03cdc7301 Sema: 'as AnyObject' is only an unconditional conversion under ObjC interop. 2016-07-25 06:01:21 -07:00
Slava Pestov
57c58176bc AST: Remove noreturn bit from function types 2016-07-24 00:15:34 -07:00