Commit Graph

844 Commits

Author SHA1 Message Date
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
Robert Widmann
bde054aa75 [DiagnosticsQoI] Look through argument tuples when diagnosing conversion failures
This gives us much better diagnostics around things like
escaping-mismatched function parameters which would previously skip
this and produce bogus invalid conversion errors between “identical”
types.
2016-12-31 20:32:41 -07: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
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
practicalswift
38be6125e5 [gardening] C++ gardening: Terminate namespaces, fix argument names, ...
Changes:
* Terminate all namespaces with the correct closing comment.
* Make sure argument names in comments match the corresponding parameter name.
* Remove redundant get() calls on smart pointers.
* Prefer using "override" or "final" instead of "virtual". Remove "virtual" where appropriate.
2016-12-17 00:32:42 +01:00
Doug Gregor
f68f87a56a [Constraint solver] After binding a type variable, activate affected constraints
Once we've bound a type variable, we find those inactive constraints
that mention the type variable and make them active, so they'll be
simplified again. However, we weren't finding *all* constraints that
could be affected---in particular, we weren't searching everything
related to the type variables in the equivalence class, which meant
that some constraints would not get visited... and we would to
type-check simply because we didn't look at a constraint again when we
should have.

Fixes rdar://problem/29633747.
2016-12-14 20:18:04 -08: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
Hugh Bellamy
48109f2a64 Fix errors and warnings building libSwiftSema on Windows using MSVC 2016-11-29 11:24:13 +00: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
Mark Lacey
fbbd8346d0 Small control-flow simplifications, NFC.
Simplify some control-flow to reduce indentation and eliminate an
unneeded flag variable.

Also update a comment to match the code that went in with
b5500b8600.
2016-11-15 09:20:40 -08:00
swift-ci
0029ba22f3 Merge pull request #5785 from rudkx/minor-cleanup 2016-11-14 22:57:19 -08:00
Mark Lacey
8b2bfcb442 Refactor a couple functions out of solveSimplified().
These are both logical bits of code that deserve first-class function status.
2016-11-14 22:14:10 -08:00
Mark Lacey
3ff07ab031 Remove extra calls to getRepresentative().
The calls to simplifyType() already return the representative if it exists.
2016-11-14 21:56:12 -08:00
practicalswift
38351faefc [gardening] Fix recently introduced typos 2016-11-11 18:35:01 +01: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
ba06709eca [Constraint solver] Dependent members don't "involve type variables".
The constraint solver tries not to solve for type variables that
"involve other type variables", which handles the case where we have
seen a constraint that mentions the type variable under consideration
as well as a different type variable, but in a constraint that we
cannot capture in a binding. Solving for such type variables too early
can lead to missed solutions, so we avoid it.

Tweak the logic for this computation to not consider type variables
mentioned within dependent member types (e.g., $T0.Iterator.Element),
because such types do not affect type inference at all, and therefore
shouldn't prevent solving for the type variable in question.
2016-11-05 23:20:28 -07:00
Doug Gregor
522cbf26fa [Constraint solver] Show potential bindings in debug output. 2016-11-05 23:20:28 -07: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