Commit Graph

509 Commits

Author SHA1 Message Date
Joe Groff
595e0e4ede Merge branch 'master' into keypaths 2017-04-19 18:38:24 -07:00
Mark Lacey
5e6b4a97ee [Constraint solver] Fix memory corruption issue.
We use simplifyConstraint() to activate other constraints, and then
examine those constraints to find related disjunctions. In examining
those active constraints, we were simplifying them in case they
failed (which would allow us to bail out earlier). In doing so, we could
potentially generate new disjunctions when we simplify an unresolved
value member constraint. If we do that, we end up collecting these new
disjunctions as part of the set of related disjunctions, but that's
problematic because as part of exiting the solver scope to roll back
changes we delete these disjunctions from the system.

Instead of actually simplifying the active constraints, just collect the
disjunctions and move the active constraints back to the inactive list.

With this change we can build the stdlib.
2017-04-15 20:18:09 -07:00
Mark Lacey
337684cb4d [Constraint system] More or less rewrite the constraint propagation pass.
This is still disabled by default, and as with the previous
implementation does not pass all tests if enabled.

Rather than looking for applicable function constraints and then looking
at the bind overload disjunctions associated with those, instead
directly collect all bind overload disjunctions and revise the enabled
elements of those disjunctions by attempting to bind them simultaneously
with the elements of other disjunctions that are linked to this one
through applicable function constraints.
2017-04-12 11:22:52 -07:00
Slava Pestov
819dfd5211 Sema: Better type safety for opened types map
Now that we no longer map dependent member types to fresh type
variables, the keys in the replacement map can just be
GenericTypeParamTypes.
2017-04-10 17:04:36 -07:00
Slava Pestov
ed4c681aae Sema: Simplify Solution::computeSubstitutions()
Don't pass in the opened type; instead have the caller call
simplifyType() if needed. Also, make computeSubstitutions()
bail out if there's no generic signature, which allowed
unifying several generic vs non-generic code paths.

Hopefully there is enough short circuiting in there now that
we're not doing any extra work in the non-generic case.
2017-04-10 17:04:36 -07:00
Joe Groff
7eebb27153 Sema: Infer the specific type of a key path literal from the mutability of the resolved components. 2017-04-10 16:06:40 -07:00
Joe Groff
964dc0e174 Sema: (wip) Overload resolution for keypath subscripts.
TODO: Some diagnostic regressions:
test-macosx-x86_64/Compatibility/tuple_arguments.swift
test-macosx-x86_64/Constraints/diagnostics.swift
test-macosx-x86_64/Constraints/tuple_arguments.swift
test-macosx-x86_64/expr/unary/keypath/keypath.swift
test-macosx-x86_64/expr/unary/selector/selector.swift
2017-04-09 16:38:02 -07:00
Mark Lacey
88123195c9 [Constraint system] Fix some issues with constraint propagation.
There are a handful of things, but the primary ones are that we really
don't want to track depth of recursion so much as whether we are
processing at the "top level" of constraints we're looking at or not so
that we can determine whether we can disable elements from a
disjunction.

Related, there are places where we can bail out earlier if we know
that *any* neighboring constraint passes.

Also updating the way we gather neighboring constraints to be
independent of the function that simplifies them. The original code was
wrong in that in bailed as soon as one of the simplifications
failed (which meant we might not gather all of them).
2017-04-06 15:38:47 -07:00
Mark Lacey
0cd57bd481 [Constraint solver] Constraint propagation, disabled by default.
This is an initial implementation of the constraint propagation pass,
disabled by default at the moment as it does not currently pass all
tests.

I have other changes that I'll hold off on for now because they are
incomplete. Without those changes, this pass doesn't really achieve
much, so this is really just a checkpoint on some progress and not
something really worth trying out at this point.
2017-04-03 19:05:27 -07:00
Mark Lacey
4a4e0f4ab8 [Constraint solver] Minor tweak to where constraint propagation runs.
No real functionality at this point - move the point of invocation for
the moment.
2017-03-21 19:32:32 -07:00
Mark Lacey
041a25ecf1 [Constraint solver] More progress on the constraint solver type map. 2017-03-14 21:56:13 -07:00
Joe Groff
eb40d4303b Sema: Add a stdlib-internal _openExistential helper.
Leverage the "special type-checking semantics" hack to pass an opened existential down to an arbitrary subexpression. Please don't use this.
2017-03-10 13:41:00 -08:00
Doug Gregor
ce718eeb54 Merge branch 'master' into normal-conformance-requirement-signature 2017-03-01 20:38:20 -08:00
swift-ci
2561eddab0 Merge pull request #7861 from rudkx/simplify-remove-generated-constraints 2017-03-01 20:09:00 -08:00
Mark Lacey
2ee4add74e [Constraint solver] Simplify removeGeneratedConstraint.
NFC - just a small simplification.
2017-03-01 22:36:05 -05:00
Mark Lacey
779177d39f Use a SmallVector rather than SmallDenseMap to hold solver scopes.
The vector makes more sense given these follow a stack discipline.
2017-03-01 16:00:32 -05:00
Doug Gregor
ec4bf005af [AST] Eliminate unnecessary #includes of AST/ProtocolConformance.h. 2017-03-01 08:22:39 -08:00
Mark Lacey
f32824296a Add an empty constraint propagation pass.
This is disabled by default but enabled under the frontend option
-propagate-constraints.

The idea here is to have a pass that enforces local consistency in our
constraint system, in order to reduce the domains of constraint
variables, speeding up the solving of the constraint system.

The initial focus will be on reducing the size of the disjunctions for
function overloads with the hope that it substantially improves the
performance of type checking many expressions.
2017-02-23 15:04:07 -08:00
Mark Lacey
b026a07fd8 Add a scale-test counter for expression type checking.
Also add the first example of using the counter to test for
known-exponential typechecking behavior for nil-coalescing.
2017-02-23 12:08:30 -08:00
Slava Pestov
46ea929072 Sema: Remove TMF_UnwrappingOptional
We should remove the only remaining TMF flag one day too, since it looks
completely bogus.
2017-02-13 16:59:13 -08:00
Graydon Hoare
9cb1c52fd8 Count constraint-solving memory more precisely, rdar://29684330 2017-02-06 13:04:19 -08:00
Mark Lacey
00cc26004b [Sema] Penalize conversions to Any.
We previously penalized bindings to Any, and that resulted in
inappropriately rejecting solutions where we bind a decl that is
explicitly typed Any. That penalty was removed in
170dc8acd7, but that has led to a variety
of source compatibility issues.

So now, we'll reintroduce a penalty for Any-typed things, but instead of
penalizing bindings (which happen both because of explicitly-typed
values in the source, and implicitly due to attempting various types for
a particular type variable), penalize casts, which are always present in
some form in the source.

Thanks go to John for the suggestion.

Fixes
  SR-3817 (aka rdar://problem/30311052)
  SR-3786 (aka rdar://problem/30268529)
  rdar://problem/29907555
2017-02-02 17:37:02 -08:00
John McCall
39b65f49a0 Track the actual DC of a member access in the constraint system.
Without this, CSGen/CSSimplify and CSApply may have differing
opinions about whether e.g. a let property is settable, which
can lead to invalid ASTs.

Arguably, a better fix would be to remove the dependency on the
exact nested DC.  For example, we could treat lets as settable
in all contexts and then just complain later about invalid
attempts to set them.  Or we could change CSApply to directly
use the information it already has about how an l-value is used,
rather than trying to figure out whether it *might* be getting set.
But somehow, tracking a new piece of information through the
entire constraint system seems to be the more minimal change.

Fixes rdar://29810997.
2017-01-26 00:14:21 -05:00
practicalswift
a9d6d8938c [gardening] Fix recently introduced typos 2017-01-22 20:40:45 +01:00
Slava Pestov
c86b5ae427 AST: Header file gardening - include what you use 2017-01-19 20:07:06 -08:00
Slava Pestov
8de0ca54e5 Sema: Merge the two implementations of simplifyType()
ConstraintSystem::simplifyType() replaced types with their fixed types
from the global map.

Solution::simplifyType() replaced types with their fixed types from the
current bindings.

There were some minor differences between the two, and some dead code.
2017-01-19 20:07:06 -08:00
Slava Pestov
599021bf7b Sema: Simplify interface of Solution::computeSubstitutions()
Instead of passing the original type and DeclContext, we just
need a GenericSignature.
2017-01-19 20:07:05 -08:00
Hugh Bellamy
b3ade87eee Fix signed warnings compiling lib/Sema/ConstraintSystem.h 2017-01-15 10:36:11 +00:00
Mark Lacey
5f2aeec434 Implement a new "expression too complex" metric for Swift 4 mode.
Rather than waiting until we've used a huge amount of memory, attempt to
make the choice to bail out based on the number of type bindings /
disjunction choices we visit.

I expect this will generally fail faster than the Swift 3 metric, but
will still only fail when we've got clearly exponential type checking
behvior.

Since we have multiple sources of exponential behavior today, I don't
want to make the bounds too tight. Once we fix some/most of that
behavior we can look at further tightening up the metric.
2017-01-10 22:09:11 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Mark Lacey
7f64b6853f Improve handling of types in constraint system.
Several fixes in order to make handling of types more consistent.

In particular:

- Expression types used within the constraint system itself always use
  the type map.

- Prior to calling out to any TypeChecker functions, the types are
  written back into the expression tree. After the call, the types are
  read back into the constraint system type map.

- Some calls to directly set types on the expressions are reintroduced
  in places they make sense (e.g. when building up new expressions that
  are then passed to typeCheckExpressionShallow).

ConstraintSystem::setType() is still setting the type on the expression
nodes at the moment, because there is still some incorrect handling of
types that I need to track down (in particular some issues related to
closures do not appear to be handled correctly). Similarly, when we
cache the types in the constraint system map, we are not clearing them
from the expression tree yet.

The diagnostics have not been updated at all to use the types from
the constraint system where appropriate, and that will need to happen as
well before we can remove the write from ConstraintSystem::setType()
into the expression and enable the clearing of the expression tree type
when we cache it in the constraint system.
2017-01-05 11:54:34 -08:00
Mark Lacey
36b07395bc Add type accessor arguments to expression APIs that access types.
These are used from within constraint system code, and for those uses we
need to be reading from the constraint system type map.

Add the parallel constraint system interfaces that call into the
Expr interfaces with the appropriate accessors.
2017-01-04 14:25:38 -08:00
Mark Lacey
7396a369d9 Make ConstraintSystem's hasType/getType take const Exprs. 2017-01-04 13:56:17 -08:00
Slava Pestov
8f96606cb2 Sema: Remove TupleToScalar conversion, which did nothing useful except crash
As far as I can tell there's no way to trigger this from valid code.
2017-01-03 23:00:07 -08:00
Joe Groff
0c9297862f Sema: Handle type-checking for withoutActuallyEscaping.
withoutActuallyEscaping has a signature like `<T..., U, V, W> (@nonescaping (T...) throws<U> -> V, (@escaping (T...) throws<U> -> V) -> W) -> W, but our type system for functions unfortunately isn't quite that expressive yet, so we need to special-case it. Set up the necessary type system when resolving an overload set to reference withoutActuallyEscaping, and if a type check succeeds, build a MakeTemporarilyEscapableExpr to represent it in the type-checked AST.
2016-12-22 17:51:26 -08:00
Doug Gregor
f7b5d9d69e [Type checker] Allow bridging conversions to more-optional types.
The prior formulation of bridging conversions allowed conversion to
more-optional types, e.g., converting an "NSDate" to "Date?", which
was broken by my recent refactoring in this area. Allow bridging
conversions to more-optional types by introducing extra optional
injections at the end.

Fixes rdar://problem/29780527.
2016-12-22 10:02:41 -08:00
Doug Gregor
e97ab635ea [Constraint solver] Separate bridging conversions from other conversions.
Previously, bridging conversions were handled as a form of "explicit
conversion" that was treated along the same path as normal
conversions in matchTypes(). Historically, this made some
sense---bridging was just another form of conversion---however, Swift
now separates out bridging into a different kind of conversion that is
available only via an explicit "as". This change accomplishes a few
things:

* Improves type inference around "as" coercions. We were incorrectly
  inferring type variables of the "x" in "x as T" in cases where a
  bridging conversion was expected, which cause some type inference
  failures (e.g., the SR-3319 regression).

* Detangles checking for bridging conversions from other conversions,
  so it's easier to isolate when we're applying a bridging
  conversion.

* Explicitly handle optionals when dealing with bridging conversions,
  addressing a number of problems with incorrect diagnostics, e.g.,
  complains about "unrelated type" cast failures that would succeed at
  runtime.

Addresses rdar://problem/29496775 / SR-3319 / SR-2365.
2016-12-21 13:46:14 -08:00
Doug Gregor
642c8ed3eb [Constraint solver] Collapse getBaseTypeFor(Array|Set)Type into is(Array|Set)Type.
Follow the pattern set by isDictionaryType() of performing the query
and extracting the underlying key/element types directly. We often
need both regardless. NFC
2016-12-21 13:46:13 -08:00
Slava Pestov
dc3af8fc99 Sema: Don't re-typecheck multi-statement closures in lazy var initializer
Normally you have to declare a type if the lazy property
initializer is a multi-statement closure, but if the user
forgets we don't want to crash.
2016-12-21 14:20:28 -05:00
Mark Lacey
170dc8acd7 Do not penalize binding or equality constraints involving Any.
We currently have an element in the solution score related to whether we
had a binding or equality constraint involving Any.

Doing this yields some strange results, e.g. if overload resolution
results in a property declared as Any we end up discarding that solution
in favor of solutions that involve other overloads that are not declared
as Any but are also not actually better solutions (e.g. overloads that
are declared as function types).

We really want to retain both solutions in this case and allow the
ranking step of the solver to decide on the better choice.

Fixes rdar://problem/29374163, rdar://problem/29691909.
2016-12-20 13:38:14 -08:00
practicalswift
16d6dce62e [gardening] Fix recently introduced typos. 2016-12-16 21:42:09 +01:00
Mark Lacey
59ce1ff73c Add a function parameter to Expr::propagateLValueAccessKind.
This parameter implements getType() for the given expression, making
it possible to use this from within the constraint system, which now
has it's own side map for types of expressions.
2016-12-12 23:55:25 -08:00
swift-ci
f20537387c Merge pull request #6220 from rudkx/cs-typemap 2016-12-12 09:34:39 -08:00
Mark Lacey
1139f8728e Return expression types from the constraint system type map.
Switch ConstraintSystem::getType() to returning the type from the
constraint system type map.

For now, assert that these types equal the types in the expression
nodes since we are still setting the expression node types in
ConstraintSystem::setType().
2016-12-11 23:08:36 -07:00
Pavel Yaskevich
8208c94dcc [TypeChecker] Add getter/setter methods for generatedConstraints of SolverState 2016-12-11 21:46:03 -08:00
Pavel Yaskevich
ff33665ff8 [TypeChecker] Add getter/setter methods for retiredConstraints of SolverState 2016-12-11 21:45:53 -08:00
Pavel Yaskevich
e546414b1d [TypeChecker] Refactor SolverState to track life time of the retired/generated constraints
Instead of relying on the SolverScope to rollback all of the changes
done to constraints in the system at the end of its life time, move
all of the logic handling that into SolverState where retired/generated
constraints live.
2016-12-11 21:45:30 -08:00
Pavel Yaskevich
646638d214 [TypeChecker] Add retired constraints to the front of the list in SolverScope
Since retired constraints are re-added back to the circulation in LIFO
order, make sure that all of the constraints are added to the front of
SolverScope::retiredConstraints list.
2016-12-11 21:44:19 -08:00
Mark Lacey
0a8678cf19 Replace Expr::getType() with ConstraintSystem::getType().
Update CSGen/CSApply/CSSolver to primarily use getType() from
ConstraintSystem.

Currently getType() just returns the type on the expression. As with
setType(), which continues to set the type on the expression, this
will be updated once all the other changes are in place.

This change also moves coerceToRValue from TypeChecker to
CosntraintSystem so that it can access the expression type map in the
constraint system.
2016-12-11 17:46:34 -07:00
Mark Lacey
294f90a68b Re-apply: Cache types in the constraint system expression type map.
I've been unable to reproduce the issue that hit on the builder, and
still expect this change to be NFC, so trying it out again. If it
fails, I'll revert again and try another shot at reproducing.

Original commit message:

We create new expressions that have the type on the expression
set. Make sure we capture these types in the constraint system type
map so that we can refer to types uniformally by consulting the map.

NFC.

(cherry picked from commit 57d5d974ff)
2016-12-11 14:07:46 -07:00