Commit Graph

844 Commits

Author SHA1 Message Date
Pavel Yaskevich
7a1bae85ae [ConstraintSolver] NFC: refactor candidate solver to use constraint system arena
While shrinking we have to allocate containers for the reduced domains
for some of the candidates, it's currently done using permanent arena
of the `ASTContext` allocator. This patch changes candidate solver to
use arena associated with the parent constraint system, which significantly
limits lifetime of domain containers.
2017-08-14 12:44:38 -07:00
Mark Lacey
9e7b5b22db Remove -swift-version 3 checks for "expression too complex".
This was initially added to avoid "expression was too complex" in a
case where we were not previously reporting it for -swift-version 3
but should have been. In retrospect this seems misguided since
although we would not like to regress on "too complex" expressions, we
really don't want to silently continue in the cases where we decide an
expression is "too complex", but where we have a solution that we
could use. It's better to fail.
2017-08-03 20:33:07 -07:00
Doug Gregor
1e3bd76b75 [Constraint solver] Teach checkTypeOfBinding() to avoid dependent member types.
We don't want to explore these bindings anyway, so reject them earlier.
2017-07-12 12:51:24 -07:00
Pavel Yaskevich
78aa1e0cd6 [ConstraintSolver] Fix shrink to use correct primary expression as a candidate
When trying to identify candidates for shrinking we are missing the case
when apply expression is a source of the assignment operator, which leads
to incorrect results in some situations, because shrink is going to miss
some required contextual information about assignment.

Resolves: rdar://problem/33190087
2017-07-10 12:28:54 -07:00
Mark Lacey
7cd4102d01 [Constraint system] Begin refactoring type variable binding selection.
Split out the code for selecting potential bindings into a separate file
as a first step before refactoring it for improved clarity and ease of
modification.
2017-07-09 10:29:42 -07:00
Robert Widmann
957d633185 Rename getInOutOrLValueObjectType to getWithoutSpecifierType
Prepares the AST for a future in which more than just inout and
@lvalue need to be stripped off of ephemeral types.
2017-07-06 09:35:04 -07:00
Pavel Yaskevich
71c2c1e44a Merge pull request #10484 from xedin/rdar-32726044
[ConstraintSolver] Use a flag to prevent solver from removing possible solutions
2017-06-22 14:41:55 -07:00
Pavel Yaskevich
6231f85be9 [ConstraintSolver] Use a flag to prevent solver from removing possible solutions
In some situations e.g. while trying to shrink domains of the type
variables before attempting search, use a flag to tell constraint
system to retain all of the viable solutions otherwise solver could
loose some of the information required to produce complete solution.

Resolves: rdar://problem/32726044
2017-06-22 00:11:32 -07:00
Mark Lacey
fb593f5404 Merge pull request #10475 from rudkx/time-out-solver
Update determination of "too complex" expression to take time into account.
2017-06-21 20:51:27 -07:00
Mark Lacey
e51e3e4bd4 Update determination of "too complex" expression to take time into account.
By default, end expression type checking after the elapsed process time
is more than 60 seconds for the current expression. This threshold can
be overridden by using -solver-expression-time-threshold=<seconds>.

Resolves rdar://problem/32859654
2017-06-21 16:26:34 -07:00
Mark Lacey
05e79e6b02 Refactor how the expression type checker timer is set up.
Preparation for adding the ability for the expression type checker to be
halted after a period of time.
2017-06-21 14:18:25 -07:00
Pavel Yaskevich
c46e3e83e8 [QoI] Improve diagnostics for single argument calls with trailing closures
Calls involving single trailing closure arguments require special
handling because we don't have as much contextual information
about function/argument types as in with regular calls, which means
that diagnosing such situations only by `visitApplyExpr`
yields subpar results.

Resolves: SR-4836.
2017-06-21 11:26:38 -07:00
Pavel Yaskevich
3781877b93 [ConstraintSystem] Track conformance checking done while solving to validate solution
Track outcomes of `conformsToProtocol` calls in `simplifyConformanceConstraints`
to be able to validate conformances when solution is formed to avoid returning
solutions with nominal types with invalid conformances to protocols.
2017-06-15 14:18:36 -07:00
Robert Widmann
abd5aa8e6d Rename some X-Value-related entities
* Rename coerceToMaterializableValue to coerceToRValue

* Rename isLValueType to hasLValueType to better match the
intended semantics of the member.
2017-06-14 13:18:45 -07:00
Mark Lacey
65e338ea4a [Constraint solver] Do not allow unavailable decls to be favored.
There is a short-circuiting hack in the constraint solver that speeds up
solving, but isn't generally sound. If we allow unavailable decls to be
considered "favored", this can fire and result in our choosing a
solution that involves the unavailable decl where other solutions exist.

Fixes rdar://problem/32570734.
2017-06-06 13:38:36 -07:00
Pavel Yaskevich
9a4255e4bf Merge pull request #9717 from xedin/rdar-32034560
[ConstraintSystem] Prevent `shrink` from solving "too complex" sub-expressions
2017-06-01 21:47:47 -07:00
Pavel Yaskevich
1bc7a1edb9 [ConstraintSolver] Skip generic overloads only if non-generic choices produce higher score solutions
Restrict skipping of the generic overloads only to the situations
when non-generic solution doesn't have any restrictions/fixes, because
there is a possibility that generic overload could produce a better
solution.

Resolves: rdar://problem/32204609.
2017-06-01 14:43:43 -07:00
Pavel Yaskevich
f09d2ad397 [ConstraintSolver] Forbid forming solutions with free generic type parameters
`FreeTypeVariableBinding::GenericParameters` mode allowed to bind
all free type variables with fresh generic parameter types, which
is incorrect (at least) if there are multiple generic solutions
present, because such parameters couldn't be compared.

This mode was used for code completion, which is now switched to use
`FreeTypeVariableBinding::UnresolvedType` instead.
2017-06-01 14:05:05 -07:00
Pavel Yaskevich
fea7bcf232 [ConstraintSystem] Prevent shrink from solving "too complex" sub-expressions
Add additional checking for complexity of the shrinking candidate
given the number of the expressions solved so far and total number
of disjunctions present. This allows us to bail quicker in complex
expression cases which, at the very least, produces an error instead
of being "stuck" in solver for a long time.

Resolves: rdar://problem/32034560
2017-05-31 13:54:43 -07:00
Pavel Yaskevich
c27e447b7c Merge pull request #9955 from xedin/rdar-32204609
[ConstraintSolver] Refactor `solveSimplified` to consolidate disjunction choice attributes/operations
2017-05-27 13:52:26 -07:00
Pavel Yaskevich
817b86f9bf [ConstraintSolver] Refactor solveSimplified to consolidate disjunction choice attributes/operations
Consolidate some of the attributes and operations common to disjunction choice
in new `DisjunctionChoice` class, which simplifies implementation of the
`ConstraintSystem::solveSimplified` method related to overload selection.
2017-05-27 12:21:00 -07:00
Slava Pestov
586dda2eb3 Sema: Replace TVO_MustBeMaterializable with TVO_CanBindToInOut
The old TVO_MustBeMaterializable is now equivalent to
!TVO_CanBindToLValue && !TVO_CanBindToInOut.

I tried to update all usages of createTypeVariable() to
pass TVO_CanBindToInOut unless they explicitly passed
TVO_MustBeMaterializable before.

However, in reality TVO_CanBindToInOut is the rare case;
we can remove this flag gradually over time to fix
crashes and diagnostics.
2017-05-26 21:08:08 -07:00
Slava Pestov
fe41900873 Sema: Split off ConstraintSystem::openUnboundGenericType() from openType()
The openType() function did two things:

- Replace unbound generic types like 'G' with fresh type variables
  applied to bound generic types, like 'G<$T0>'.

- Replace generic parameters with type variables from the replacement
  map.

The two behaviors were mutually exclusive and never used from the
same call site, so split them up into two independent functions.

Also, eliminate ConstraintSystem::openBindingType() since it was only
used in one place and could be expressed in terms of existing functions.
2017-05-23 02:10:03 -07:00
swift-ci
1a07230668 Merge pull request #8642 from brentdax/error-type-debugging-fix 2017-05-17 20:41:02 -07:00
Pavel Yaskevich
732f942742 [ConstraintSystem] Additional debug logging for constraint system shrinking
Add logging before solving each of the candidates considered for
shrinking with information about sub-expression and new constraint system.
At the end of the candidate solving print each of the produced solutions.
2017-05-16 23:43:02 -07:00
Mark Lacey
bc7dff5c9f Revert "Re-instate "Improve disjunction selection""
This reverts commit
ecfa406fc5, which was reinstating
3c5b393e0c.

It does not revert one test change from that commit, because
inexplicably one of the tests is still failing, probably due to some
other changes that have happened since. I'm leaving a ticket open to
investigate.

I've added one of the new cases that is failing as a result of this
change.

This is being reverted not because of bugs in this particular commit,
but because it seems to be exposing other bugs in the type checker that
are resulting in source compatibility problems. We need to shake these
other bugs out of the type checker before bringing this change back.
2017-05-15 11:27:11 -07:00
Doug Gregor
badffdc9c3 [Constraint solver] Tune the "skip generic operators" heuristic.
The "skip generic operators" heuristic within the constraint solver is
fairly awful, because it is known to miss reasonable solutions. Make
is slightly more narrow by only skipping generic operators when the
concrete operator we found was symmetric.
2017-05-11 08:57:29 -07:00
Slava Pestov
a08c1a0655 Sema: Don't construct naked ErrorTypes in valid code
It is useful to set a breakpoint on ErrorType::get(ASTContext)
to see what is going wrong immediately instead of after the
fact when an ErrorType pops up where you don't expect it.

Unfortunately associated type inference, domain shrinking and
the type cleanup RAII utility would all build ErrorTypes even
with valid code.

Refactor things a bit so that this is no longer the case; at
least now the standard library and overlays build with
'assert(false)' inserted into ErrorType::get(ASTContext).

ErrorType::get(Type) is still expected to come up in associated
type inference since it is used as a signal while sorting
through potential type witness candidates.
2017-05-09 20:14:50 -07:00
practicalswift
089ea58fcb [gardening] Fix dupe word typo. 2017-05-09 20:45:10 +02:00
Mark Lacey
44882d2d2a [Constraint solver] Fix Swift 3 compatibility issue with overloaded operators.
Recent changes to the stdlib resulted in some expressions involving
literals and operators that have both generic and non-generic overloads
to become ambiguous. The ambiguity is due to an old performance hack in
the solver which unfortunately needs to remain in place at the moment to
avoid regressing expression type checker performance.

This commit changes the performance hack a bit in that instead of
stopping visiting the options in a disjunction as soon as we have a
solution involving non-generic operators and come across a constraint
involving generic operators, we instead continue visiting the elements
in the disjunction, but just skip over the generic operators.

Fixes rdar://problem/31695865 and rdar://problem/31698831.
2017-05-02 18:27:15 -07:00
Mark Lacey
ecfa406fc5 Re-instate "Improve disjunction selection"
Thie reinstates 3c5b393e0c, which was
backed out due to it exposing an ambiguous expression in a recent stdlib
commit.
2017-04-29 17:53:13 -07:00
Mark Lacey
1de530e229 Revert "Improve disjunction selection" 2017-04-29 10:45:15 -07:00
Mark Lacey
3c5b393e0c [Constraint solver] Rework how we select the next disjunction to visit.
We previously used a simple heuristic of visiting the disjunction with
the fewest number of elements in it.

Instead, this commit introduces a new way to select the best disjunction
to explore based on attempting disjunctions based on either how much
information we have about the associated type variables (e.g. how many
of the arguments of a function call in the case of bind overload
disjunctions) or by other criteria that help constraint the search or
split the connected components (e.g. coercions and calls to
initializers).

A key part of the improvement here is allowing the type checker to
attempt bindings of types to type variables when there are still
argument conversion constraints between type variables in the
system. The insight here is if there are no other constraints blocking
an attempt to bind types and we have types to try, we will be able to
test those types against active conformance constraints and potentially
fail much earlier while solving.

Visiting disjunctions in a different order exposed some other problems
with the type checker including a couple cases where map is used that
are now considered ambiguous due to problems in how we are ranking
solutions.

I measured an 11-13% reduction in type checking time for the standard
library using a release build of the compiler.
2017-04-28 00:50:16 -07:00
Doug Gregor
a5868a6148 [Constraint solver] Look through lvalue types when establish nil-convertibility.
When determining whether our inference of an optional type should add
a layer of optionality, look through lvalue types.
Fixes rdar://problem/31779785.
2017-04-25 17:04:22 -07:00
Joe Groff
595e0e4ede Merge branch 'master' into keypaths 2017-04-19 18:38:24 -07:00
swift-ci
62d077d2d3 Merge pull request #8776 from DougGregor/operator-availability-31592529 2017-04-14 09:57:22 -07:00
Doug Gregor
c739ab127c [Constraint Solver] Respect availability in operator-performance hacks.
We have various hacks in the constraint solver to improve the
compile-time performance characteristics of operators. Some of those
didn't respect availability annotations, causing us to incorrectly
choose an unavailable operator when available options exist.

Fixes rdar://problem/31592529.
2017-04-14 09:24:13 -07:00
Slava Pestov
db58e02cb2 Sema: Hook up layout constraints to the solver
There were various problems with layout constraints either
being ignored or handled incorrectly. Now that I've exercised
this support with an upcoming patch, there are some fixes
here.

Also, introduce a new ExistentialLayout::getLayoutConstriant()
which returns a value for existentials which are class-constrained
but don't have a superclass or any class-constrained protocols;
an example would be AnyObject, or AnyObject & P for some
non-class protocol P.

NFC for now, since these layout-constrained existentials cannot
be constructed yet.
2017-04-13 21:17:05 -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
96b01c48b3 Merge pull request #8339 from tonisuter/SR-4347
[Sema] [SR-4347] Improve inference of optional supertypes
2017-04-06 09:02:22 -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
Toni Suter
09a56c16fd [SR-4347] default to Any? instead of Any if there is a nil literal 2017-04-02 13:26:13 +02:00
Toni Suter
d630c3fa87 [SR-4347] Improve inference of optional supertypes 2017-03-25 07:44:33 +01:00
Mark Lacey
aab3de7985 [Constraint solver] Exit immediately from solve() if we already have a failed constraint.
As solveRec() already does, exit immediately if we have a failed
constraint from constraint generation. Not doing so means that we can
create a SolverScope in constraint propagation, and on destruction of
that SolverScope we clear out the failedConstraint field, obscuring the
fact that we had a failure.
2017-03-22 14:49:06 -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
a697a2b64b [Constraint solver] Mark a function as static. 2017-03-21 19:32:32 -07:00
Mark Lacey
839e3e9132 [Constraint solver] Disable shrink() when constraint propagation is enabled. 2017-03-21 19:32:32 -07:00
Xi Ge
383b0abd62 AST: Deep sugar type reconstitution should be optional. (#8244)
Tentatively fixing a test failure in Sil parser.
2017-03-21 11:04:53 -07:00
Xi Ge
92975fb9c0 AST: Expose reconstituteSugar as a public member function of TypeBase. NFC 2017-03-20 16:14:51 -07:00