Commit Graph

509 Commits

Author SHA1 Message Date
Mark Lacey
6abcc32103 Revert "Cache types in the constraint system expression type map." 2016-12-11 00:38:10 -07:00
Mark Lacey
57d5d974ff Cache types in the constraint system expression type map.
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.
2016-12-10 23:12:36 -07:00
Mark Lacey
e00756cda4 Set types in the constraint system type map in CSApply.
The setType() function in ConstraintSystem still sets the types on the
expressions themselves and that will continue until everything is
moved over to using the map.

Unfortunately this exposed cases where we are currently setting the
type multiple times to different values, so I've commented out the
assert that I previously added. I will circle back and start looking
into those issues once everything is moved over.

NFC for now.
2016-12-10 19:25:43 -07:00
Mark Lacey
3d9e4d7f86 Add a map from expressions to types in the constraint system.
This map will be used instead of directly accessing types on
expressions. This will allow us to avoid mutating the types on
expression trees directly, making it possible to remove code that
currently attempts to save & restore types, and reducing the number of
bugs that exist as a result of not always perfectly saving & restoring
types (e.g. dangling references to released type variables).

The setType() function introduced here currently still sets the type
on the expression, so this change is NFC. This is just step one of
staging in this transition to using the types from the maps.

A map for TypeLocs and possibly other maps for other types we mutate
will be added in future commits.
2016-12-09 12:07:09 -07:00
Pavel Yaskevich
712052bc9a [QoI] Mark inactive constraints as retired after removing
This makes sure that removed constraints are returned back to the
system after current run, otherwise only constraint graph would
get them back since it has its own scope.
2016-12-05 20:50:32 -08:00
Hugh Bellamy
48109f2a64 Fix errors and warnings building libSwiftSema on Windows using MSVC 2016-11-29 11:24:13 +00:00
practicalswift
7d412572e6 [gardening] Use proper doxygen comments (///) 2016-11-28 21:25:30 +01: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
practicalswift
6fa577dfbd [gardening] Fix recently introduced typos. Fix inconsistent headers. 2016-11-17 13:09:02 +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
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
Doug Gregor
3205b8a6c4 Fix ad hoc substitution lists to not include derived type substitutions. 2016-11-08 16:11:29 -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
f19c4af050 Revert "[Type checker] Recurse into statements when cleaning type variables, harder."
This reverts commit 01913beba0.
2016-11-06 22:30:54 -08:00
Doug Gregor
01913beba0 [Type checker] Recurse into statements when cleaning type variables, harder.
Resolved crasher #28474, which is failing to crash reliably on CI. Just fix it.
2016-11-06 22:08:49 -08:00
Doug Gregor
4a5402849b Remove the now-unnecessary "get member type" callback.
The ASTContext had a wacky "get member type" callback that actually
called back into the constraint system (!) to build member types. This
callback was obsoleted by the change that started representing nested
types as DependentMemberTypes.
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
Doug Gregor
245f995b5d [Constraint solver] Track the total number of type variables created.
The constraint solver scales poorly with the number of type variables,
so track it.
2016-11-05 23:20:27 -07:00
swift-ci
675f70e04f Merge pull request #5568 from DougGregor/allow-member-int-eq 2016-10-31 16:55:57 -07:00
Mark Lacey
12cf13de8e Remove dead code related to tracking favored constraints.
At one point this was added in order to inhibit some bridging
conversions while we are handling favored constraints, but that code has
been removed now, making this dead.

Noticed by inspection.
2016-10-31 11:44:25 -07:00
Doug Gregor
c587c69f38 Merge pull request #5536 from ahoppen/SR-3043-deinit-reference
[TypeChecker] Add error message when accessing a type's destructor
2016-10-31 10:03:43 -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
Doug Gregor
99daad0f30 Rework witness matching for generic requirements.
Reimplement the witness matching logic used for generic requirements
so that it properly models the expectations required of the witness,
then captures the results in the AST. The new approach has a number of
advantages over the existing hacks:

* The constraint solver no longer requires hacks to try to tangle
  together the innermost archetypes from the requirement with the
  outer archetypes of the context of the protocol
  conformance. Instead, we create a synthetic set of archetypes that
  describes the requirement as it should be matched against
  witnesses. This eliminates the infamous 'SelfTypeVar' hack.
* The type checker no longer records substitutions involving a weird
  mix of archetypes from different contexts (see above), so it's
  actually plausible to reason about the substitutions of a witness. A
  new `Witness` class contains the declaration, substitutions, and all
  other information required to interpret the witness.
* SILGen now uses the substitution information for witnesses when
  building witness thunks, rather than computing all of it from
  scratch. ``substSelfTypeIntoProtocolRequirementType()` is now gone
  (absorbed into the type checker, and improved from there), and the
  witness-thunk emission code is simpler. A few other bits of SILGen
  got simpler because the substitutions can now be trusted.
* Witness matching and thunk generation involving generic requirements
  and nested generics now works, based on some work @slavapestov was
  already doing in this area.
* The AST verifier can now verify the archetypes that occur in witness substitutions.
* Although it's not in this commit, the `Witness` structure is
  suitable for complete (de-)serialization, unlike the weird mix of
  archetypes previously present.

Fixes rdar://problem/24079818 and cleans up an area that's been messy
and poorly understood for a very, very long time.
2016-10-30 23:15:43 -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
33ad7a8473 [Constraint solver] Remove erroneous constraints from the constraint graph.
When a constraint fails, we retire it... but we also need to remove it
from the constraint graph. Otherwise, we break invariants when
diagnostic generation attempts to continue simplification.

Fixes rdar://rdar28145033.
2016-10-27 21:35:14 -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
cf19b8a76b [Constraint solver] Eliminate addConstraint(Constraint*).
We no longer build constraints just to immediately simplify them.
2016-10-23 22:27:33 -07:00
Doug Gregor
d4f5c72c1e [Constraint solver] Only build disjunctions when they're unsolved. 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
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
a5ec531cd0 [Constraint solver] Teach addBindOverloadConstraint to simplify directly.
We never have to allocate BindOverload constraints, so don't.
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
beadfc774b [Constraint solver] Eliminate unused parameters to addConstraint().
ConstraintSystem::addConstraint() is no longer used to simplify
existing constraints, nor does it have to deal with "externally
solved" constraints. Remove those parameters and simplify the code. 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
Doug Gregor
11b30a22a6 [Constraint solver] Remove some dead instance variables. 2016-10-14 09:31:23 -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