Commit Graph

692 Commits

Author SHA1 Message Date
Hugh Bellamy
48109f2a64 Fix errors and warnings building libSwiftSema on Windows using MSVC 2016-11-29 11:24:13 +00:00
Slava Pestov
835472b14f AST: Remove PolymorphicFunctionType 2016-11-29 03:05:35 -07:00
Slava Pestov
7cfe0e6401 Sema: getType() => getInterfaceType() 2016-11-29 03:05:27 -07:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Mark Lacey
a4a6c0d5e0 Clean-up solver state management for a constraint system.
Make SolverState manage whether the ConstraintSystem it belongs to has a
current SolverState.

Also a couple minor formatting fixes for ternary expressions involving
solverState.
2016-11-15 17:06:20 -08:00
Doug Gregor
e20464a02c [Constraint solver] Remove 'type member' constraint.
Only some fallback code in the for-each binding checking code was
using this constraint. Implement those checks more directly.
2016-11-06 23:14:15 -08:00
Doug Gregor
f168e7270c [Type checker] Use DependentMemberType instead of type variables for nested types.
In the constraint solver, we've traditionally modeled nested type via
a "type member" constraint of the form

  $T1 = $T0.NameOfTypeMember

and treated $T1 as a type variable. While the solver did generally try
to avoid attempting bindings for $T1 (it would wait until $T0 was
bound, which solves the constraint), on occasion we would get weird
behavior because the solver did try to bind the type
variable.

With this commit, model nested types via DependentMemberType, the same
way we handle (e.g.) the nested type of a generic type parameter. This
solution maintains more information (e.g., we know specifically which
associated type we're referring to), fits in better with the type
system (we know how to deal with dependent members throughout the type
checker, AST, and so on), and is easier to reason able.

This change is a performance optimization for the type checker for a
few reasons. First, it reduces the number of type variables we need to
deal with significantly (we create half as many type variables while
type checking the standard library), and the solver scales poorly with
the number of type variables because it visits all of the
as-yet-unbound type variables at each solving step. Second, it
eliminates a number of redundant by-name lookups in cases where we
already know which associated type we want.

Overall, this change provides a 25% speedup when type-checking the
standard library.
2016-11-05 23:20:28 -07:00
swift-ci
675f70e04f Merge pull request #5568 from DougGregor/allow-member-int-eq 2016-10-31 16:55:57 -07:00
Doug Gregor
a66b35d79b [Constraint solver] Allow constraint regeneration when inputs are simplified.
When we process a constraint, the first step is generally to call
getFixedTypeRecursive() to look through type variables. When this
operation actually does non-trivial work, we could save
that result by considering the current constraint "solved" and
generating a new constraint (if needed!) with the simplified types.

This commit adds the infrastructure to do that, because it's important
when getFixedTypeRecursive() starts performing more interesting
substitutions (e.g., handling member types of type
variables). However, enabling for the common case of looking through a
type variable isn't profitable (it's ~2% slower to type-check the
standard library). Stage in this infrastructure change now.
2016-10-31 09:27:22 -07:00
Alex Hoppen
b0e67da844 [TypeChecker] Add error message when accessing a type's destructor
Provide error messages when the destructor is accessed using foo.deinit
instead of crashing

This resolves SR-2652 and SR-3043
2016-10-30 19:43:57 +00:00
Doug Gregor
b295106830 [Constraint solver] Use the type representative in the "occurs" check.
When performing the occurs check, look for the *representative* of the
type variable we're about to bind, rather than the type variable
itself. Fixes rdar://problem/26845038, SR-1512, SR-1902, SR2635,
SR-2852, and SR-2766.
2016-10-25 22:37:21 -07:00
Doug Gregor
c58aafdce5 [Constraint solver] Unify the two typeVarOccursInType implementations.
NFC, thanks @CodaFi!
2016-10-25 14:13:31 -07:00
Doug Gregor
4cc41e5e64 [Constraint solver] Perform the "occurs" check properly.
We've been performing the "occurs" check when computing potential
bindings for type variables, but we weren't actually performing the
check for bindings that *must* occur. Perform the occurs check before
binding type variables, which fixes a few crashers and is far more principled.

Note that this obviates the need for tracking the type variables we've
substituted in simplifyType(), so simplify that as well.

Fixes rdar://problem/27879334 / SR-2351.
2016-10-25 13:32:06 -07:00
Doug Gregor
b484cbb6b9 [Constraint solver] Eliminate TypeMatchKind. NFC
It's no more than a subset of ConstraintKind with a bunch of
boilerplate to convert back and forth. Just use ConstraintKind.
2016-10-24 23:26:07 -07:00
Doug Gregor
a65812c558 [Constraint solver] Add ConstraintKind::BindToPointerType.
This matches up with TypeMatchKind::BindToPointerType.
2016-10-24 21:24:49 -07:00
Doug Gregor
d790291c48 [Constraint solver] Replace a use of simplifyType() with getFixedTypeRecursive()
The latter is cheaper to compute, and suitable for the
is-it-a-metatype check.
2016-10-24 13:19:58 -07:00
Doug Gregor
7f6654001b [Constraint solver] Handle 'bind' constraints lazily. 2016-10-23 22:27:33 -07:00
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