Commit Graph

370 Commits

Author SHA1 Message Date
Doug Gregor
4ea395f7ac [Constraint solver] Track whether type variables can bind to lvalues.
The type checker has a poor habit of inferring lvalue types where it
shouldn't, or (more rarely) dropping lvalue types when it should keep
them. Only allow a type variable to bind to an lvalue type in specific
situations where it makes sense (e.g., the type variable represents an
overload set that may contain a variable). Fixes <rdar://problem/13827562>.



Swift SVN r7179
2013-08-12 22:28:18 +00:00
Doug Gregor
f4e9c17032 [Constraint solver] Eliminate rvalue equality constraints.
We were only using these constraints in one place, where they had no
value.


Swift SVN r7166
2013-08-12 18:00:55 +00:00
Doug Gregor
a583ea585c const'ify Constraint dump and print methods.
Swift SVN r7164
2013-08-12 17:36:31 +00:00
Dmitri Hrybenko
de59d8dcd4 Remove unneeded llvm:: qualifier for llvm::StringRef and llvm::SmallVector
Swift SVN r7089
2013-08-09 18:41:46 +00:00
Doug Gregor
45a8e79021 [Constraint solver] Properly find and substitute deduced associated types.
Normal type lookup would find and substitute deduced associated types
appropriately, but the constraint solver's handling of member
references failed to account for them. Make sure we handle them now,
fixing <rdar://problem/14638725>.

The bulk of this change actually fixes the type vs. value member
confusion within the solver. Previously, our notion of an overload
choice lost track of whether we were looking for a type vs. a
value. This meant that we would end up getting metatypes when in fact
we expected types, and we had a few workarounds in the solver and in
the protocol conformance checker to work around that bug. Track that
we're looking for a type in OverloadChoice, so we can propagate that
information. Consequently, eliminate all of the hackarounds involving
metatypes that shouldn't be metatypes.



Swift SVN r7009
2013-08-07 22:36:49 +00:00
Doug Gregor
0dd4aedc14 [Constraint solver] Point at specific parameter or function in diagnostic.
When we're diagnosing an error during the initialization of a
parameter from an argument, point at the actual parameter (if we can
find it) or at least the function (if we can't) where the call
failed. Baby steps:

t2.swift:3:4: error: '(x : Int, y : Int)' is not a subtype of 'Int'
f3(f3b)
   ^
t2.swift:1:9: note: in initialization of parameter 'x'
func f3(x : (x : Int, y : Int) -> ()) {}
        ^



Swift SVN r6924
2013-08-05 23:39:50 +00:00
Doug Gregor
60caf622da [Constraint solver] Don't eagerly build constraint locators.
Swift SVN r6908
2013-08-05 18:19:57 +00:00
Doug Gregor
6400fdc814 [Constraint solver] Track resolved overload sets more efficiently.
Rather than maintain a big DenseMap that was only ever used for
iteration, use a constraint-system--allocated linked list with a
shared head. Good for 1.5% compile time on the standard library, and a
necessary refactor for tracking more information for diagnostics.


Swift SVN r6907
2013-08-05 18:16:54 +00:00
Doug Gregor
64afdba89f Simplfiy the check for free type variables.
Eliminates another unnecessary use of simplifyType(). No performance impact.



Swift SVN r6899
2013-08-05 15:49:23 +00:00
Dmitri Hrybenko
e1c4ae3174 Wrap llvm::SourceMgr in swift::SourceManager so that we can add new members
to the source manager.


Swift SVN r6815
2013-08-01 20:39:22 +00:00
Jordan Rose
674a03b085 Replace "oneof" with "union"...everywhere.
We haven't fully updated references to union cases, and enums still are not
their own thing yet, but "oneof" is gone. Long live "union"!

Swift SVN r6783
2013-07-31 21:33:33 +00:00
Joe Groff
b11169b5db Sema: Coerce SuperRefExprs by changing the type directly.
A SuperRefExpr semantically includes an upcast, and coercing it to a deeper subclass by wrapping it in a DerivedToBaseExpr is redundant and confuses SILGen. Instead, update the type of the SuperRefExpr directly before loading from it. Fixes <rdar://problem/14581294>.

Swift SVN r6720
2013-07-29 22:52:01 +00:00
Anna Zaks
bf6c0e2cb3 Better way of type checking ApplyExpr in presence of attributes.
Add the new ApplicableFunction constraint to represent loose function
type equality - one type is applicable to another, if their inputs
and outputs match. I've removed the hack on [auto_closure], so
type checking a call to a function with attributes
("auto_closure" or "noreturn") is handled in the same way now.

Disables a test case (added in r6598) that was relying on the previous
behavior. Doug has offered to fix it.

Swift SVN r6629
2013-07-26 00:32:23 +00:00
Doug Gregor
59535bc751 Use the correct locator when converting an expression to its contextual type.
We were inferring the locator from an already-rewritten expression,
which resulted in a completely useless locator. Use the correct
(pre-rewrite) locator. Also, eliminate the Solution::coerceToType()
overload that infers the locator, since it was inferred the wrong
thing in two out of the three callers. Fixes <rdar://problem/14429520>.


Swift SVN r6448
2013-07-22 14:59:13 +00:00
Anna Zaks
d84a075bc6 Type check noreturn attribute.
Slightly modified the constraint construction for ApplyExpr to not
include checking of the attributes.

Swift SVN r6392
2013-07-19 21:28:48 +00:00
Doug Gregor
d52750a730 Match default arguments to the function or constructor declaration they came from.
When we form a tuple that uses default arguments, find the source of
the default arguments (which will be a function or constructor
declaration). This information will eventually be introduced into the
AST.


Swift SVN r6277
2013-07-16 00:58:54 +00:00
Dmitri Hrybenko
aa46064432 Pass a const ASTContext and const DeclContext whenever possible. This makes it
possible to use lookupVisibleDecls() with a const DeclContext.


Swift SVN r6274
2013-07-15 23:39:00 +00:00
Doug Gregor
9ba865e852 Diagnose ambiguity type-checks by pointing to candidates.
Introduces very basic support for diagnosing ambiguous expressions,
where the source of the ambiguity is a reference to an overloaded
name. Simple example:

t.swift:4:1: error: ambiguous use of 'f0'
f0(1, 2)
^
t.swift:1:6: note: found this candidate
func f0(i : Int, d : Double) {}
     ^
t.swift:2:6: note: found this candidate
func f0(d : Double, i : Int) {}
     ^

There's a lot of work needed to make this cleaner, but perhaps it will
ease the pain of developing the library <rdar://problem/14277889>.


Swift SVN r6195
2013-07-12 00:45:17 +00:00
Doug Gregor
fc1c256ef5 Allow references to deduced associated types.
When checking a type's conformance against a protocol, we can deduce
the values of associated types. Make these associated types visible to
qualified name lookup so that (for example) VectorEnumeratorType does
not need to define the Element type. It is deduced from the signautre
of next(), and made available as, e.g.,
VectorEnumeratorType<Int>.Element through the Enumerator protocol
conformance. Fixes <rdar://problem/11510701>, but with some lingering
dependencies on lazy type resolution (<rdar://problem/12202655>).

Note that the infrastructure here is meant to be generalized to
support default implementations in protocols, but there are several
pieces still not in place.



Swift SVN r6073
2013-07-08 22:32:27 +00:00
Doug Gregor
09e64a0243 Resolve unbound generic type references within their definitions.
When the name of a generic type is referenced, without any generic
arguments, within the definition of a generic type (or extensions of
that generic type), use the generic arguments provided by that
context. Thus, within the definition of X<T> below, one can simply use
'X' as a shorthand for 'X<T>':

  class X<T> {
    func swap(other : X) { /* ... */ } // same as "func swap(other : X<T>)"
  }

This resolution provides essentially the same behavior as the
injected class name does in C++. Note that this rule overrides any
inference rules, such that (for example) the following is ill-formed
rather than inferring the generic arguments of 'X':

  class X<T> {
    func foo() {
      var xi : X<Int> = X()
    }
  }

Note that name binding has changed slightly: when unqualified lookup
finds a type declaration within a nominal type, it is now treated as
a DeclRefExpr (or overloaded variant thereof) rather than as a member
access with an implicit 'this'.

Fixes <rdar://problem/14078437>.



Swift SVN r6049
2013-07-08 16:17:12 +00:00
Doug Gregor
40fb0e7961 Introduce a simplified entry point for looking up a member type.
The new TypeChecker::lookupMemberType() handles substitution of the
base type into the type result, as well as eliminating results for
which the resulting types are equivalent.


Swift SVN r5927
2013-07-01 16:49:58 +00:00
Joe Groff
22c52c34d2 Sema: Type-check ExprPatterns using ~= overload resolution.
When we coerce an ExprPattern, set up a constraint system to check the leaf expression using overload resolution on the ~= operator. Save a specialized DeclRef to the overload we find in the ExprPattern for later codegen.

Swift SVN r5920
2013-07-01 01:38:48 +00:00
Doug Gregor
05452e0872 Replace MemberLookup with TypeChecker::lookupMember() throughout the type checker.
Swift SVN r5861
2013-06-28 00:25:48 +00:00
Dave Abrahams
3bd556a4f7 [constraint-checker] Fix an assertion
Swift SVN r5749
2013-06-21 18:21:19 +00:00
Doug Gregor
96f819ce97 [Constraint solver] Don't guess at type variable bindings that are guaranteed to be fully bound by other constraints.
A type variable can be fully bound by either an overload set (when
selecting an overload will provide a binding for the type variable) or
by a member constraint (when the base type is determined, the member
type will be known). This keeps the solver from making really, really
bad type variable bindings based on context information.


Swift SVN r5634
2013-06-17 23:38:32 +00:00
Doug Gregor
ec12ee1b2e [Constraint solver] Dump failures as part of --debug-constraints.
Swift SVN r5627
2013-06-17 20:50:49 +00:00
Doug Gregor
491de4fc55 [Overload resolution] +1 for binding a literal expression to its default literal type.
Move this computation into the "fixed" part of the scoring, because it
is not actually relative. Collapses the linear-time check down to a
constant-time check based on the locator.

Also, unify the constraint generation logic for all of the basic type
literals (integer, float, character, string) now that we have a common
mapping from expression to literal protocol.


Swift SVN r5590
2013-06-14 22:37:01 +00:00
Doug Gregor
5711d09989 Associate locators with each of the type variables.
Swift SVN r5589
2013-06-14 22:10:32 +00:00
Doug Gregor
731782db6d [Overload resolution] Penalize user-defined conversions at -2.
This also starts to separate out the fixed part of a solution's score
from the part that is relative to another solution's score.


Swift SVN r5584
2013-06-14 18:36:55 +00:00
Doug Gregor
01c80ebc9b Produce a "diff" of type bindings and overload choices in a set of viable solutions.
Use this N-way solution "diff" when comparing two solutions to
determine which is better. No actual functionality change here,
because we're still not comparing overload choices.


Swift SVN r5518
2013-06-07 20:26:58 +00:00
Joe Groff
f0f8be9a1a Sema: Typecheck AssignExprs within the constraint system.
Set up constraints for AssignExprs within the constraint system instead of using typeCheckAssignment to set up their own isolated system. Kill the goofy hack not to consider a single-AssignExpr closure to be single-expression-body, because single-assign-expr-body closures now type-check successfully.

typeCheckAssignment still lingers because of a couple uses in typeCheckConstructorBody, but those should be easy to kill off next.

Swift SVN r5504
2013-06-07 05:15:04 +00:00
Doug Gregor
bd8e8b6afd Remove the last remnants of LiteralKind; it's no longer useful.
Swift SVN r5491
2013-06-06 06:25:51 +00:00
Doug Gregor
ac5474095b Remove literal constraints entirely; we use formal protocols now.
Swift SVN r5490
2013-06-06 06:12:56 +00:00
Doug Gregor
b8a2426673 Introduce the formal protocol IntegerLiteralConvertible for integer literals.
This eliminates the last of the old "convert literal to specific type"
code, as all of the basic literal kinds now go directly through formal
protocols. One minor casualty of this change is that we've lost the
"integer constant too large" warning. Filed as
<rdar://problem/14070127>, which is likely to be addressed in SIL.


Swift SVN r5477
2013-06-05 16:42:06 +00:00
Joe Groff
0959d96a64 Sema: Pass DeclContexts down into expression type-checking.
Thread the current DeclContext down into expression type-checking so that we will be able to set up ImplicitClosureExprs with the right parent context. (Don't actually set the right decl context when creating an ImplicitClosureExpr yet, since IRGen isn't quite ready to handle specialized partial applications and crashes on the emitted SIL if we do.)

Swift SVN r5431
2013-06-01 21:48:48 +00:00
Doug Gregor
b99375fe9f Switch type checking of float literals over to the formal protocol.
Swift SVN r5422
2013-05-31 18:43:51 +00:00
Doug Gregor
7a6e1dfce2 Introduce conformance constraints into the constraint system for float literals.
Another baby step toward relying completely on formal protocols for
float literals.


Swift SVN r5421
2013-05-31 18:18:28 +00:00
Doug Gregor
0698ace066 Introduce "shallow" type checking for expressions.
Add a version of TypeChecker::typeCheckExpression() that performs a
"shallow" type-check of just the top-level expression, rather than a
"deep" type-check of the full expression. Use it to eliminate
TypeChecker::semaApplyExpr(). 


Swift SVN r5394
2013-05-29 23:52:23 +00:00
Doug Gregor
31e19f095f Constraint generation rewrites some already-type-checked expression nodes; make those rewrites stick.
We were generating constraints from a slightly different expression
than we were applying the constraints to, which could produce odd
crashes. Be more principled about using the modified expression.


Swift SVN r5384
2013-05-29 19:53:36 +00:00
Doug Gregor
0158f71192 When opening up the context type for a member, open up all levels of archetypes.
Swift SVN r5370
2013-05-29 14:12:49 +00:00
Doug Gregor
718eb46c66 Initially, only record unavoidable failures when solving constraint systems.
If we fail to find a solution, solve the system again and track the
remaining failures. Provides a 13% improvement in the time to parse
swift.swift.


Swift SVN r5344
2013-05-25 18:46:42 +00:00
Doug Gregor
f10be3c003 Separate unavoidable failures from failures that occur under some set of assumptions.
The former should always be recorded, because they are always
reported, and require far less effort to report. The latter can be
lazily computed (only when we found no solution and no unavoidable
failures) and will require far more work to diagnose. No functionality
change.


Swift SVN r5340
2013-05-25 18:37:58 +00:00
Doug Gregor
1e90f0fd08 Make the "should we record failures?" question a parameter of constraint solving.
This will let us turn it off for the initial solve, which saves ~10%
on a debug build of swift.swift.


Swift SVN r5339
2013-05-25 18:17:52 +00:00
Doug Gregor
2fb9d8310b Clean up the ConstraintSystem interface a bit.
Swift SVN r5338
2013-05-25 17:59:48 +00:00
Doug Gregor
32ffdf7d7a Migrate ConstraintSystem::SavedBindings into SolverState.
This eliminates tracking of type variable bindings we've made during
initial constraint generation, where we wouldn't ever undo those
changes anyway.


Swift SVN r5337
2013-05-25 17:52:18 +00:00
Doug Gregor
e320f4c04a Eliminate ConstraintSystem::State; it's no longer useful.
Swift SVN r5336
2013-05-25 17:43:05 +00:00
Doug Gregor
221180d13a Factor the solving loop that attempts type bindings out of the main solver.
No functionality change.


Swift SVN r5335
2013-05-25 17:37:30 +00:00
Doug Gregor
9d34624669 [Constraint solver] Don't attempt obviously-wrong tuple-to-tuple conversions when scalar-to-tuple might work.
When we're trying to convert one tuple to another, and we hit a
problem that doesn't require us to actually perform more type
comparisons (mismatched labels, not enough elements, etc.), fall back
to attempt a scalar-to-tuple conversion when the target tuple can be
initialized by a scalar.

This is *not* a complete solution to this problem. It handles the
obvious cases, but for the non-obvious cases we'll have to pursue both
paths separately to see which one pans out.

Nonetheless, this fixes most of <rdar://problem/11293232>, as well as
<rdar://problem/13820721> and <rdar://problem/13394261>.


Swift SVN r5295
2013-05-24 00:16:08 +00:00
Doug Gregor
f2b6008871 Collapse ConstraintSystem::SharedState; it's all shared now.
Swift SVN r5283
2013-05-23 20:45:42 +00:00
Doug Gregor
3c3c56a200 [Constraint solver] Remove code unused in the current solver.
No functionality change.


Swift SVN r5282
2013-05-23 20:37:55 +00:00