Commit Graph

90 Commits

Author SHA1 Message Date
Joe Pamer
2eedc06d66 Begin adding plumbing for the type checker to accept "forward" bridged array conversions. (rdar://problem/16540403)
Swift SVN r17640
2014-05-07 19:45:37 +00:00
Doug Gregor
4f7a3d40cf Start enforcing strict keyword argument usage and ordering under -strict-keyword-arguments.
Implement a completely new path for matching up an argument tuple to a
parameter tuple, which handles the specific rules we want for
calls. The rules are:

  - The keyword arguments at the call site must match those of the
    declaration; one cannot omit a keyword argument if the declaration
    requires it, nor can one provide a keyword argument if the
    declaration doesn't have one.
  - Arguments must be passed in order, except that arguments for
    parameters with defaults can be re-ordered among themselves (we
    can't test all of this because neither constraint application nor
    the AST can express these).

QoI is extremely important in this area, and this change improves the
situation considerably. We now provide good diagnostics for several
important cases, with Fix-Its to clean up the code:

  - Missing keyword arguments:

    t.swift:8:13: error: missing argument labels 'x:y:' in call
    allkeywords1(1, 2)
                ^
                 x: y:

  - Extraneous keyword arguments:

    t.swift:17:12: error: extraneous argument labels 'x:y:' in call
    nokeywords1(x: 1, y: 1)
               ^~~~   ~~~

  - General confusion over keyword arguments (some missing, some
    wrong, etc.):

    t.swift:26:14: error: incorrect argument labels in call (have
        'x:_:z:', expected '_:y:z:')
    somekeywords1(x: 1, 2, z: 3)
                 ^~~~
                        y:

There are still a few areas where the keyword-argument-related
diagnostics are awful, which correspond to FIXMEs in this
implementation:

  - Duplicated arguments: f(x: 1, x: 2)
  - Extraneous arguments: f(x: 1, y: 2, z: 3) where f takes only 2
    parameters
  - Missing arguments
  - Arguments that are out-of-order
  - Proper matching of arguments to parameters for diagnostics that
    complain about type errors.

And, of course, since this has only been lightly tested, there are
undoubtedly other issues lurking.

This new checking is somewhat disjoint from what constraint
application can handle, so we can type-check some things that will
then fail catastrophically at constraint application time. That work
is still to come, as is the AST work to actually represent everything
we intend to allow.

This is part of <rdar://problem/14462349>.

Swift SVN r17341
2014-05-03 23:58:09 +00:00
Ted Kremenek
050fd53af7 Rename UncheckedOptional to ImplicitlyUnwrappedOptional.
Swift SVN r17232
2014-05-02 06:13:57 +00:00
Doug Gregor
667d90c1f4 Start diagnosing scalar-to-tuple conversions that are missing a keyword argument.
Another baby step toward <rdar://problem/14462349>, made even more
tepid by the fact that I've quarantined this behind a new flag,
-strict-keyword-arguments. Enforcing this breaks a lot of code, so I'd
like to bring up the new model on the side (with good diagnostics that
include Fix-Its) before trolling through the entire standard library
and testsuite to fix violations of these new rules.


Swift SVN r17143
2014-05-01 06:15:30 +00:00
Doug Gregor
2b1cb6c101 My general "relabel tuple" fix kind isn't going to fly; use TupleToScalar for the existing case.
Swift SVN r17139
2014-05-01 04:45:59 +00:00
Doug Gregor
c9fd60155e Diagnose an extraneous keyword when calling a single-parameter function with no keyword argument.
This is the simplest case to test the infrastructure for
adding/removing/fixing keyword arguments at the call site that don't
line up with the keyword arguments in a declaration. Baby steps toward
<rdar://problem/14462349>.



Swift SVN r17136
2014-05-01 03:47:52 +00:00
Doug Gregor
97a2df25c5 Add a new conversion and type-matching kind for argument tuples.
We're going to give argument-tuple handling special behavior; separate
it out first.

Swift SVN r17095
2014-04-30 15:32:50 +00:00
Doug Gregor
d8a71c4866 Type checker: encapsulate fix kind into a Fix class, which will grow. NFC
Swift SVN r17094
2014-04-30 14:24:08 +00:00
Doug Gregor
3dfaddb674 Drop extraneous '()' applied to a non-function.
Fixes <rdar://problem/15042686>, i.e.,

t.swift:3:7: error: call to non-function of type 'Int'; did you mean
    to leave off the '()'?
  i = i()
      ^~~

Swift SVN r16950
2014-04-27 22:36:39 +00:00
Doug Gregor
beb43749f1 Suggest '&' when it is needed to pass an lvalue by reference.
Fixes <rdar://problem/11592572>.

Swift SVN r16944
2014-04-27 21:42:06 +00:00
Doug Gregor
eaf2bf632f Speculatively fix optionals by unwrapping them with '!'.
Example:

test/Constraints/fixes.swift:54:9: error: value of optional type 'B?'
    not unwrapped; did you mean to use '!' or '?'?
  b = a as B
        ^
            !

Swift SVN r16938
2014-04-27 20:41:02 +00:00
Doug Gregor
321911a059 Add infrastructure for applying fixes during constraint solving.
Introduce some infrastructure that allows us to speculatively apply
localized fixes to expressions during constraint solving to fix minor
typos and omissions. At present, we're able to introduce the fixes
during constraint simplification, prefer systems with fewer fixes when
there are multiple fixes, and diagnose the fixes with Fix-Its.

Actually rewriting the AST to reflect what the Fix-Its are doing is
still not handled.

As a start, introduce a fix that adds '()' if it appears to have been
forgotton, producing a diagnostic like this if it works out:

t.swift:8:3: error: function produces expected type 'B'; did you mean
to call it with '()'?
f(g)
  ^
   ()

Note that we did regress in one test case
(test/NameBinding/multi-file.swift), because that diagnostic was
getting lucky with the previous formulation.

Swift SVN r16937
2014-04-27 19:04:04 +00:00
Joe Pamer
86b79d6bd3 Some code cleanup for array upcast conversions. (Part 2 of the fix for rdar://problem/16540403)
Swift SVN r16837
2014-04-25 19:52:06 +00:00
Joe Pamer
066f5e6da5 Allow for simple upcast conversions between array types. (Part 1 of the fix for rdar://problem/16540403)
Swift SVN r16836
2014-04-25 19:52:05 +00:00
John McCall
f1180f5e6d in order to work correctly for non-@objc protocols.
Language features like erasing concrete metatype
values are also left for the future.  Still, baby steps.

The singleton ordinary metatype for existential types
is still potentially useful; we allow it to be written
as P.Protocol.

I've been somewhat cavalier in making code accept
AnyMetatypeType instead of a more specific type, and
it's likely that a number of these places can and
should be more restrictive.
When T is an existential type, parse T.Type as an
ExistentialMetatypeType instead of a MetatypeType.

An existential metatype is the formal type
 \exists t:P . (t.Type)
whereas the ordinary metatype is the formal type
 (\exists t:P . t).Type
which is singleton.  Our inability to express that
difference was leading to an ever-increasing cascade
of hacks where information is shadily passed behind
the scenes in order to make various operations with
static members of protocols work correctly.

This patch takes the first step towards fixing that
by splitting out existential metatypes and giving
them a pointer representation.  Eventually, we will
need them to be able to carry protocol witness tables

Swift SVN r15716
2014-04-01 00:38:28 +00:00
John McCall
fe9793da94 Allow disjunction constraints to request that their
chosen alternative be recorded in the solution.  NFC.

Swift SVN r15253
2014-03-20 00:24:58 +00:00
John McCall
5531ffde25 Add a conversion from T? to @unchecked T?. This is not
a subtype conversion.

Swift SVN r15026
2014-03-14 04:43:46 +00:00
John McCall
e3f6be8631 Permit @unchecked T? to be coerced to T as a conversion.
Originally, I didn't want this because I felt it made
unchecked-optional too non-local --- it wasn't always
obvious that an assignment might crash because it was
implicitly dropping optionality.  And that's still a
concern!  But I think that overall, if we're prepared
to accept that that danger is inherent in @unchecked T?,
this is a more consistent model: @unchecked T? means
that we don't know enough about the value to say for
certain that nil is a real possibility, so we'll let
you coerce it to the underlying type, and that coercion
just might not be dynamically safe.  No more special
cases for calls and member access (to the user; of
course, to the implementation these are still special cases
because of lookup and overload resolution).

Swift SVN r14796
2014-03-07 21:57:36 +00:00
Joe Groff
1585b625f9 Sema: Wire up compound name lookup to UnresolvedSelectorExpr.
Resolve selector references using compound name lookup, pushing DeclNames a bit deeper through the type-checker and diagnostics as necessary.

Swift SVN r14791
2014-03-07 20:52:30 +00:00
Chris Lattner
d758e0dfe3 Eliminate more "DynamicLookup" in favor of "AnyObject", this is the
bulk of finishing rdar://13327098.


Swift SVN r14653
2014-03-04 22:15:46 +00:00
Dmitri Hrybenko
e4dad5cbe4 Type checker: don't create constraints with null types
Fix an issue in the type checker where it could create a constraint with a null
SecondType, and then fail elsewhere later.


Swift SVN r14638
2014-03-04 09:39:58 +00:00
John McCall
954936f279 Dump BindOverload constaints more verbosely.
Swift SVN r14628
2014-03-04 01:58:47 +00:00
John McCall
6cb7ccd18f Various enhancements to constraint solution dumping.
Swift SVN r14627
2014-03-04 01:58:47 +00:00
John McCall
9c415a3bed Remove the TrivialSubtype conversion kind and implement
its basic logic in libAST, which both makes it easier to
implement and makes it possible to use in the places that
should care about it, i.e. in IR-gen and SIL-gen.

Per Doug, none of the places that were introducing
trivial-subtype constraints really needed to do so rather
than just using subtype constraints.

Swift SVN r12679
2014-01-22 00:19:56 +00:00
John McCall
817e80bde5 Implicit conversions for UncheckedOptional.
rdar://15189000
rdar://15189009

Swift SVN r12260
2014-01-13 23:15:03 +00:00
Chris Lattner
18a9193452 Redesign how @inout propagation works in the typechecker:
- Switch all the 'self' mutable arguments to take self as @inout, since
   binding methods to uncurried functions expose them as such.
 - Eliminate the subtype relationship between @inout and @inout(implicit),
   which means that we eliminate all sorts of weird cases where they get
   dropped (see the updated testcases).
 - Eliminate the logic in adjustLValueForReference that walks through functions
   converting @inout to @inout(implicit) in strange cases.
 - Introduce a new set of type checker constraints and conversion kinds to properly
   handle assignment operators: when rebound or curried, their input/result argument
   is exposed as @inout and requires an explicit &.  When applied directly (e.g. 
   as ++i), they get an implicit AddressOfExpr to bind the mutated lvalue as an
   @inout argument.

Overall, the short term effect of this is to fix a few old bugs handling lvalues.
The long term effect is to drive a larger wedge between implicit and explicit 
lvalues.


Swift SVN r11708
2013-12-29 04:38:26 +00:00
Doug Gregor
08893199b5 Pre-calculate and store the type variables referenced by a constraint.
This is a micro-optimization that doesn't seem to matter much at the
moment.


Swift SVN r11012
2013-12-09 13:39:16 +00:00
Doug Gregor
52d611a683 Add, use factory methods to create constraints.
Swift SVN r11011
2013-12-09 13:09:45 +00:00
Doug Gregor
dfe6646d5a Micro-optimization: replace ActiveConstraints set with a bit on Constraint.
No functionality change.


Swift SVN r10940
2013-12-06 21:41:51 +00:00
Doug Gregor
3d4da69b08 In "x as T", type-check the subexpression "x" using "T" as the context type.
Using "T" as the contextual type, either for an implicit conversion
(in the coercion case) or as a downcast (for the checked-cast case),
opens up more type-inference opportunities. Most importantly, it
allows coercions such as "1 as UInt32" (important for
<rdar://problem/15283100>). Additionally, it allows one to omit
generic arguments within the type we're casting to.

Some additional cleanup to follow.


Swift SVN r10799
2013-12-04 22:32:28 +00:00
Joe Groff
d873764add Sema: Allow an implicit conversion from T? to U? if T can be converted to U.
Similar to the T -> U? conversion, allow T? to be converted to U? through a bind-convert-inject chain. Naively adding this conversion creates ambiguities in the type checker, opening up multiple paths to the same solution in certain cases, so add a a special case to the solver that avoids introducing the conversion if a solution is already available by other means. (Doug figured out that part; thanks Doug!)

This still introduces a regression in test/Constraints/optional.swift, rendering a call ambiguous when overloaded on argument types. <rdar://problem/15367036>

Swift SVN r9857
2013-10-31 22:51:36 +00:00
Doug Gregor
e45254857a Tighten the constraint on the base of a '.' expression for protocol members.
The "conversion" constraint was far too loose, because we don't want
to permit arbitrary conversions on the left-hand side of the
'.'. Conformance constraints aren't correct either, because an
existential that does not conform to itself can still be used on the
left-hand side of a '.', so we introduce a new kind of constraint for
this.


Swift SVN r9630
2013-10-23 21:25:00 +00:00
Dmitri Hrybenko
acdd5b120a Make type checker debug output redirectable
Introduce a replaceable TypeCheckerDebugConsumer for this.


Swift SVN r9555
2013-10-21 20:26:32 +00:00
Doug Gregor
1e975393de Model lvalue-to-rvalue conversion as a conversion restriction.
Swift SVN r9414
2013-10-16 18:02:45 +00:00
Doug Gregor
e980d87f1a Explicitly model the single-element-tuple-to-scalar conversion.
Cleaner modeling of this conversion from (a : T) to T helps simplify
some of the type matching.


Swift SVN r9410
2013-10-16 15:56:45 +00:00
Doug Gregor
cf146ecaac Use disjunctions for "deep" equality match vs. user-defined conversions.
This should fix <rdar://problem/13140447> completely.


Swift SVN r9388
2013-10-15 23:36:40 +00:00
Doug Gregor
f57e20edd1 Use disjunctions for subclass-to-superclass vs. user-defined conversions.
This fixes the superclass conversion issue in <rdar://problem/13140447>.


Swift SVN r9337
2013-10-14 23:42:42 +00:00
Doug Gregor
8a896ef59a Use disjunction conversions to handle T -> U? conversion ambiguity.
The constraint solver was eagerly applying the T -> U? conversion
rule, which only succeeds when T is convertible to U. Thus, we would
reject valid code where T has a user-defined conversion to
U?. Instead, introduce a disjunction conversion to try either T -> U?
by converting T to U or via a user-defined conversion. 

Most of this is infrastructure for the introduction of constraint
restrictions, which specify that a particular constraint (say, a
conversion constraint) should only try a single direct path:
scalar-to-tuple, value-to-optional, user-defined, etc. Each term in the
disjunction created for the T -> U? case is restricted to a particular
conversion rule, so that checking it does not create another
disjunction.

Keep track of the constraint restrictions we used within a given
solution, so that we can replay those steps during the application
phase. There is a bunch of inactive code here at the moment, which
will become useful as we start creating disjunctions for other
ambiguous conversions.



Swift SVN r9318
2013-10-14 18:08:12 +00:00
Doug Gregor
16901477c2 Introduce an overload-binding constraint.
Overload bindings constraints bind a given type to a specific overload
choice. This is a step toward moving overload sets into normal
disjunction constraints.

Part of the work here is to eliminate the constraint system's
dependence on OverloadSet, which will be going away in favor of a
disjunction constraint.


Swift SVN r9209
2013-10-11 18:27:51 +00:00
Doug Gregor
2d215d68d3 Split Constraint and ConstraintLocator into their own headers.
No functionality change.


Swift SVN r9204
2013-10-11 17:24:46 +00:00