Commit Graph

830 Commits

Author SHA1 Message Date
Robert Widmann
9fa8341540 Only decompose function types for info about default arguments
AnyFunctionType::Param carries around information about decomposed
parameters now.  Information about default arguments must be computed
separately with swift::computeDefaultMap.
2017-06-19 23:05:44 -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
Pavel Yaskevich
3559047340 [ConstraintSolver] Unify value-to-pointer conversion scoring
Instead of having three different scores for - string, array, inout,
let's unify them under a single value-to-pointer score and use
it in appropriate places when simplifying restricted constraints.
2017-06-02 23:42:32 -07:00
Mark Lacey
567c9656f2 Merge pull request #10069 from rudkx/more-type-map-changes
[Constraint solver] More type map changes.
2017-06-02 10:42:46 -07:00
Mark Lacey
d1a0b35447 [Constraint solver] More type map changes.
A few more changes to use the side map for expression types in the
constraint solver. Still not enabled.
2017-06-02 08:13:57 -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
5998cd645f [ConstraintSolver] Penalize conversions from String to UnsafePointer
There are possible situations when we find solutions with String
and String -> UnsafePointer conversions at the same time for
expressions with default string literals. In order to disambiguite
such situations let's prefer solutions without String -> UnsafePointer
conversions if possible.
2017-06-01 14:43:43 -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
Slava Pestov
6526d513c0 Sema: Remove the 'extraFunctionAttrs' hack 2017-05-31 17:31:37 -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
Mark Lacey
fdc55395f2 Merge pull request #9928 from rudkx/type-map
[Constraint solver] More type map updates.
2017-05-31 09:07:53 -07:00
Mark Lacey
12926eb667 [Constraint solver] More type map updates.
More updates to read and write types from a side table in the constraint
solver and only write back into expressions when we've finished type
checking an expression.

We still write directly into expressions, and will continue to do so
until all of the code has been updated.
2017-05-31 00:36:09 -07:00
Mark Lacey
e539428ceb Update SK_LastScoreKind to SK_KeyPathSubscript.
When this score kind was added, the marker for the last score kind was
not updated. As a result, increasing the score for SK_KeyPathSubscript
was not actually having an effect.

Add tests that include a type with subscripts that also take the key
paths as their argument with an identical argument label ("keyPath").

These tests show that we're actually falling back on the keypath
application in many cases despite the score kind specific to keypath
application. I'll open a couple new JIRAs to investigate this
behavior.

The update to SK_LastScoreKind results in fixing a crash in these tests
that happens as a result of an assumption in CSRanking.cpp that if we're
comparing two solutions we end up with decls for overloads to compare,
which isn't the case here due to the keypath application.
2017-05-30 21:02:01 -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
1b254a9843 Sema: Kill off old modeling of generic arguments 2017-05-24 20:39:10 -07:00
Slava Pestov
d8234bace6 Sema: Fix double type checking of lazy initializer expressions
If a lazy var has no declared type, we have to type check the
initializer to get a type before we can build the getter.

Then, the initializer is type checked as part of the getter
again.

Use the new SkipApplyingSolution flag when type checking for
the first time. We still end up doing redundant work, but by
not applying the solution we avoid feeding invalid AST nodes
back into the constraint solver.

This fixes some bad diagnostics and crashes.

Fixes <https://bugs.swift.org/browse/SR-2616> and
<rdar://problem/28313602>.
2017-05-24 17:21:28 -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
Slava Pestov
a6dfcd52cf Sema: Remove OverloadChoiceKind::TypeDecl, which was never used 2017-05-23 02:10:02 -07:00
Slava Pestov
ca6e4e6138 Sema: Clean up openGeneric() for nested generic types
If we had an unbound generic type whose parent was a bound
generic type, and the outer type's generic signature
placed generic constraints on outer parameters, we would
create type variables that weren't ever bound to anything.

Previously, this would never come up, but it will once
preCheckExpression() is folding more MemberRefExprs down
to TypeExprs.
2017-05-21 18:11:52 -07:00
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