Commit Graph

216 Commits

Author SHA1 Message Date
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
Joe Groff
a16daf1312 Merge pull request #8020 from jckarter/open-existential-helper
Sema: Add a stdlib-internal _openExistential helper.
2017-03-10 14:06:02 -08: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
Alex Hoppen
1b7055db5a [Gardening] Change calls of getName().isOperator() to isOperator() 2017-03-10 09:06:20 +00:00
Slava Pestov
721e6178f5 AST: Remove TypeBase::isUnspecializedGeneric() 2017-02-27 20:00:35 -08:00
Doug Gregor
99a139695a [Constraint solver] Dont assume orphans are along in the inactive list.
The inactive list may contain other disjunctions associated with bound
type variables. For now, make sure we recover the orphan directly to
fix the crash in SR-4056 / rdar://problem/30686926. Later, we can
treat these as orphans, too.
2017-02-23 23:38:23 -08:00
Doug Gregor
40b6764e80 [Constraint solver] Handle disjunctions as separate connected components.
The constraint graph models type variables (as the nodes) and
constraints (as the multi-edges connecting nodes). The connected
components within this (multi-)graph are independent subproblems that
are solved separately; the results from each subproblem are then
combined. The approach helps curtail exponential behavior, because
(e.g.) the disjunctions/type variables in one component won't ever be
explored while solving for another component

This approach assumes that all of the constraints that cannot be
immediately solved are associated with one or more type
variables. This is almost entirely true---constraints that don't
involve type variables are immediately simplified.

Except for disjunctions. A disjunction involving no type variables
would not appear *at all* in the constraint graph. Worse, it's
independence from other constraints could not be established, so the
constraint solver would go exponential for every one of these
constraints. This has always been an issue, but it got worse with the
separation of type checking of "as" into the "coercion" case and the
"bridging" case, which introduced more of these disjunctions. This led
to counterintuitive behavior where adding "as Foo" would cause the
type checking to take *more* time than leaving it off, if both sides
of the "as" were known to be concrete. rdar://problem/30545483
captures a case (now in the new test case) where we saw such
exponential blow-ups.

Teach the constraint graph to keep track of "orphaned" constraints
that don't reference any type variables, and treat each "orphaned"
constraint as a separate connected component. That way, they're solved
independently.

Fixes rdar://problem/30545483 and will likely curtain other
exponential behavior we're seeing in the solver.
2017-02-20 17:18:18 -08:00
Mark Lacey
d4a5fe58be Attempt to bind the result of unwrapping optionals if we're asked to bind subtypes.
When trying to solve for the test case we attempt to simplify a value
member constraint and it fails because we've bound the LHS type
variable to an optional as a result of other constraints involving
other type variables in the equivalence class of this type
variable.

We don't have enough information to directly deduce the non-optional
type directly from other constraints involving this type variable.

This change results in one interesting type checking anomoly. In Swift
3 mode, we now successfully typecheck an expression that we previously
did not. Although the type checking technically makes sense given the
type checking rules we have in place, it can be a bit surprising to
users. Fortunately we emit a warning that calls out the surprising
behavior of considering an expression unused.

Fixes rdar://problem/30271695.
2017-02-19 14:53:01 -07:00
Mark Lacey
58b14ffc2d Update -debug-constraints to dump the source range immediately.
We end up dumping information during constraint generation, and it's
nice to know which range we're dealing with at that point.
2017-02-15 23:21:52 -08:00
Hugh Bellamy
f001b7562b Use relatively new LLVM_FALLLTHROUGH instead of our own SWIFT_FALLTHROUGH 2017-02-12 10:47:03 +07:00
Graydon Hoare
9cb1c52fd8 Count constraint-solving memory more precisely, rdar://29684330 2017-02-06 13:04:19 -08:00
Doug Gregor
4127f700f1 [Type checker] Remove the now-unused InterpolationArgument locator element.
NFC; the prior commit eliminated all uses of this locator path element.
2017-01-30 10:04:55 -08:00
Doug Gregor
545c43f500 [Type checker] Don't shrink() arithmetic expressions of literals.
Shrinking such expressions will immediately restrict the solution set
to the default literal types (Int or Double), causing later solutions
to fail. Fixes rdar://problem/30220565.
2017-01-27 14:48:12 -08:00
practicalswift
a9d6d8938c [gardening] Fix recently introduced typos 2017-01-22 20:40:45 +01:00
Pavel Yaskevich
6f217dbe2a [ConstraintSystem] Don't attempt to solve candidates which have closures.
While shrinking constraint system, avoid candidates which contain
closures inside, because closure expressions require special handling
and allow no rollback.
2017-01-15 14:15:04 -08:00
Pavel Yaskevich
6f74251803 [QoI] Move from RAII to direct sub-expression cleanup. NFC 2017-01-15 04:36:10 -08:00
Pavel Yaskevich
488fc0fad5 [QoI] Cleanup AST after trying to shrink constraint system of invalid expression
Since `ConstraintSystem::shrink` is going to attempt to type-check
sub-expressions separately it's essential to clean-up AST if constraint
generation or solving of the such expressions fails, otherwise if
such solving resulted in creation of implicit expression type variables
might leak to the outside.
2017-01-13 17:20:56 -08:00
Arnold Schwaighofer
e46e63ebc6 Revert "[QoI] Cleanup AST after trying to shrink constraint system of invalid expression" 2017-01-13 13:35:19 -08:00
Mark Lacey
a94b2dd7b5 Merge pull request #6780 from xedin/keypath-crashers
[QoI] Cleanup AST after trying to shrink constraint system of invalid expression
2017-01-13 11:43:26 -08:00
Pavel Yaskevich
9b7d632160 [QoI] Cleanup AST after trying to shrink constraint system of invalid expression
Since `ConstraintSystem::shrink` is going to attempt to type-check
sub-expressions separately it's essential to clean-up AST if constraint
generation or solving of the such expressions fails, otherwise if
such solving resulted in creation of implicit expression type variables
might leak to the outside.
2017-01-13 00:39:37 -08:00
Doug Gregor
d79ba781c6 [Constraint solver] Collapse the three optional-to-optional restriction kinds.
All three of these conversion restriction kinds were handled in
exactly the same way, so just treat them as one.
2017-01-12 17:11:13 -08:00
Roman Levenstein
29180ca1a0 Add support for layout requirements with layout constraints.
This commit introduces new kind of requirements: layout requirements.

This kind of requirements allows to expose that a type should satisfy certain layout properties, e.g. it should be a trivial type, have a given size and alignment, etc.
2017-01-11 19:21:45 -08:00
swift-ci
aa2402c200 Merge pull request #6725 from rudkx/expression-too-complex 2017-01-10 23:15:25 -08: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
Mark Lacey
1a4795e2a1 Report "expression was too complex" more consistently.
We were only reporting it in cases where we found no solutions before
deciding the expression was too complex. Had we found some solution, we
would go ahead and use that solution before bailing out due to
complexity, which means e.g. we might have been allowing otherwise
ambiguous expression to type check.
2017-01-10 20:12:11 -08:00
Doug Gregor
ffa901cb9f [Type checker] Don't infer type variable bindings from checked casts (as?/as!).
Checked casts are dependent on run-time queries; we should not attempt
to infer type variable bindings from them, because doing so produces
unreasonable bindings. Fixes rdar://problem/29894174.
2017-01-10 11:03:54 -08:00
Slava Pestov
95ddef57df Fix JSON serialization of ConstraintSolver statistics 2017-01-08 18:12:47 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Slava Pestov
f56f403f9a Merge pull request #6528 from CodaFi/minimum-escape-velocity
[DiagnosticsQoI][SR-2657] Look through argument tuples when diagnosing conversion failures
2017-01-03 18:48:46 -08:00