Commit Graph

2716 Commits

Author SHA1 Message Date
Chris Willmore
1c3b391fd3 Don't allow function types that differ in SIL representation to be matched by SameType constraint.
<rdar://problem/22181714> Crash when typing "signal"

Swift SVN r31594
2015-09-01 01:46:59 +00:00
Chris Lattner
1e5c13aa58 refactor the interface to performMemberLookup to pass in the components
of the constraint that it needs, not a constraint itself.  NFC.


Swift SVN r31585
2015-08-30 20:58:21 +00:00
Chris Willmore
32bcecb2a2 Use Defaultable constraint instead of disjunction for last-resort type.
NFC.

Swift SVN r31393
2015-08-21 21:27:46 +00:00
Chris Willmore
032f7532f7 Don't bind unlabeled type variable to single labeled argument.
<rdar://problem/22348394> Compiler crash on invoking function with labeled defaulted param with non-labeled argument

Swift SVN r31355
2015-08-19 22:57:29 +00:00
Chris Lattner
f6ce68a45a remove ConstraintKind::Conjunction, it is unused and unnecessary - after
all, the whole system is a conjunction!


Swift SVN r31284
2015-08-18 00:47:28 +00:00
Chris Willmore
86f64d3e26 Don't simplify argument tuple conversion constraint $T1 < (T2, ...) to
$T1 < T2 if there are other required params in the tuple.

<rdar://problem/22211854> wrong arg list crashing sourcekit

Swift SVN r31218
2015-08-13 18:28:25 +00:00
Joe Pamer
cd492d96b5 Fix a type checker crash when the object type of a ForceValueExpr is an 'Any'.
This allows us to remove the workaround generic overloads for "print" without sacrificing good diagnostics for its unavailable overloads. (rdar://problem/21499048, rdar://problem/21582758, rdar://problem/22126141)

Swift SVN r31169
2015-08-12 15:59:31 +00:00
Chris Lattner
a899872d91 Reapply r31105, with some fixes to invalid unconstrained generics. These fixes correct
the regressions that r31105 introduced in the validation tests, as well as fixing a number
of other validation tests as well.

Introduce a new UnresolvedType to the type system, and have CSDiags start to use it
as a way to get more type information out of incorrect subexpressions.  UnresolvedType
generally just propagates around the type system like a type variable:
 - it magically conforms to all protocols
 - it CSGens as an unconstrained type variable.
 - it ASTPrints as _, just like a type variable.

The major difference is that UnresolvedType can be used outside the context of a
ConstraintSystem, which is useful for CSGen since it sets up several of them to 
diagnose subexpressions w.r.t. their types.

For now, our use of this is extremely limited: when a closureexpr has no contextual
type available and its parameters are invalid, we wipe them out with UnresolvedType
(instead of the previous nulltype dance) to get ambiguities later on.

We also introduce a new FreeTypeVariableBinding::UnresolvedType approach for
constraint solving (and use this only in one place in CSDiags so far, to resolve
the callee of a CallExpr) which solves a system and rewrites any leftover type 
variables as UnresolvedTypes.  This allows us to get more precise information out,
for example, diagnosing:

 func r22162441(lines: [String]) {
   lines.map { line in line.fooBar() }
 }

with: value of type 'String' has no member 'fooBar'
instead of: type of expression is ambiguous without more context

This improves a number of other diagnostics as well, but is just the infrastructural
stepping stone for greater things.





Swift SVN r31130
2015-08-11 06:06:05 +00:00
Chris Lattner
2204dbcbfd revert r31105, it causes some regressions on validation tests.
Swift SVN r31107
2015-08-10 15:01:22 +00:00
Chris Lattner
de79b60c89 Introduce a new UnresolvedType to the type system, and have CSDiags start to use it
as a way to get more type information out of incorrect subexpressions.  UnresolvedType
generally just propagates around the type system like a type variable:
 - it magically conforms to all protocols
 - it CSGens as an unconstrained type variable.
 - it ASTPrints as _, just like a type variable.

The major difference is that UnresolvedType can be used outside the context of a
ConstraintSystem, which is useful for CSGen since it sets up several of them to 
diagnose subexpressions w.r.t. their types.

For now, our use of this is extremely limited: when a closureexpr has no contextual
type available and its parameters are invalid, we wipe them out with UnresolvedType
(instead of the previous nulltype dance) to get ambiguities later on.

We also introduce a new FreeTypeVariableBinding::UnresolvedType approach for
constraint solving (and use this only in one place in CSDiags so far, to resolve
the callee of a CallExpr) which solves a system and rewrites any leftover type 
variables as UnresolvedTypes.  This allows us to get more precise information out,
for example, diagnosing:

 func r22162441(lines: [String]) {
   lines.map { line in line.fooBar() }
 }

with: value of type 'String' has no member 'fooBar'
instead of: type of expression is ambiguous without more context

This improves a number of other diagnostics as well, but is just the infrastructural
stepping stone for greater things.



Swift SVN r31105
2015-08-10 06:18:27 +00:00
Joe Pamer
828eb68e72 Commit DaveA's API changes to 'print', along with the compiler changes necessary to support them.
There's still work left to do. In terms of next steps, there's still rdar://problem/22126141, which covers removing the 'workaround' overloads for print (that prevent bogus overload resolution failures), as well as providing a decent diagnostic when users invoke print with 'appendNewline'.

Swift SVN r30976
2015-08-04 01:57:11 +00:00
Chris Willmore
3485a97cd4 Diagnose ambiguous initializer resolution.
Fix construction and simplication of constructor member locators, so
that locators involving constructor members can be simplified during
diagnosis.

<rdar://problem/21427130> Swift can't discriminate on arity of function parameters

Swift SVN r30932
2015-08-02 12:12:19 +00:00
Chris Lattner
bb6dfa41cb remove extraneous ;, NFC
Swift SVN r30926
2015-08-02 05:08:02 +00:00
Joe Pamer
d4a9aba927 Expand upon r30921 to allow for remapping against parameter tuples consisting of a single vararg entry. (rdar://problem/18083599)
Swift SVN r30922
2015-08-01 20:18:32 +00:00
Joe Pamer
3fab0eb2db Consider varargs in the first function parameter position when remapping argument constraints to accommodate default values. (rdar://problem/22056861)
Swift SVN r30921
2015-08-01 18:45:31 +00:00
Slava Pestov
20ff9df27f Sema: More robust in the face of missing stdlib protocols
This came up with SourceKit fuzz testing on the stdlib. I don't have a
reduced testcase right now.

Progress on <rdar://problem/19554069>.

Swift SVN r30882
2015-07-31 21:22:51 +00:00
Chris Lattner
7851d348a8 merge the ErrorDoesNotHaveInitOnInstance complexity into the normal
"member not found" diagnostic machinery.


Swift SVN r30842
2015-07-31 04:23:07 +00:00
Chris Lattner
50be7e4ecf reapply r30789, r30795, r30796, r30797, without r30787 which causes a compile time hit:
- Produce more specific diagnostics relating to different kinds of invalid
 - add a testcase, nfc
 - Reimplement FailureDiagnosis::diagnoseGeneralMemberFailure in terms of

Not including r30787 means that we still generate bogus diagnostics like:
[1, 2, 3].doesntExist(0)  // expected-error {{type 'Int2048' does not conform to protocol 'IntegerLiteralConvertible'}}

But it is an existing and separable problem from the issues addressed here.



Swift SVN r30819
2015-07-30 23:31:56 +00:00
Ben Langmuir
c1a2955ef6 Revert r30787, r30789, r30795, r30796, r30797
r30787 causes our tests to time out; the other commits depend on r30787.

Revert "revert part of my previous patch."
Revert "Produce more specific diagnostics relating to different kinds of invalid"
Revert "add a testcase, nfc"
Revert "- Reimplement FailureDiagnosis::diagnoseGeneralMemberFailure in terms of"
Revert "Fix places in the constraint solver where it would give up once a single "

Swift SVN r30805
2015-07-30 17:44:22 +00:00
Chris Lattner
fe04ebfd2f - Reimplement FailureDiagnosis::diagnoseGeneralMemberFailure in terms of
performMemberLookup, eliminating a ton of duplicated logic, but keeping the
  same general behavior.

- Now that r30787 landed, we can have diagnoseGeneralMemberFailure inform
  clients when a member lookup fails due to referencing a candidate decl of
  ErrorType (i.e, it is already invalid somehow).  When this happens, there is
  no reason to diagnose a problem, because the original issue has been diagnosed
  and anything we produce now is just garbage.

The second point cleans up a bunch of bogus diagnostics in the testsuite, which are
*actually* due to upstream error that are already diagnosed.



Swift SVN r30789
2015-07-30 05:28:16 +00:00
Chris Lattner
4fb19ada2b Fix places in the constraint solver where it would give up once a single
constraint failed, leaving a bunch of other solvable constraints laying 
around in the system as inactive.

This is a problem for diagnostics emission, because it turns around and
reaches into the constraint system for some inactive constraint, assuming
that anything left could not be solved.  The constraint system attempted to
solve this by taking the first failure and putting it into the failedConstraint
with the intention of driving diagnostics, but just because it happened to fail
first in constraint-solver-worklist-order doesn't mean it is the most pertinent
one to diagnose.



Swift SVN r30787
2015-07-30 05:08:50 +00:00
Chris Lattner
9df95e8377 Simplify logic in simplifyMemberConstraint, NFC.
Swift SVN r30786
2015-07-30 04:09:08 +00:00
Chris Lattner
17ca02f407 split the meat of simplifyMemberConstraint out into a new
performMemberLookup helper function, this makes it simpler
and enables reuse by CSDiags.


Swift SVN r30775
2015-07-29 19:01:27 +00:00
Chris Lattner
1a0a0315fe wordsmith a diagnostic, NFC otherwise.
Swift SVN r30731
2015-07-28 23:35:25 +00:00
Chris Lattner
ede0c50856 Revamp how value & type member constraint failures are diagnosed, eliminating the
"unavoidable failure" path, along with Failure::DoesNotHaveNonMutatingMember and
just doing some basic disambiguation in CSDiags.

This provides some benefits:
 - Allows us to plug in much more specific diagnostics for the existing "only has 
   mutating members" diagnostic, including producing notes for why the base expr
   isn't mutable (see e.g. test/Sema/immutability.swift diffs).
 - Corrects issues where we'd drop full decl name info for selector references.
 - Wordsmiths diagnostics to not complain about "values of type Foo.Type" instead 
   complaining about "type Foo"
 - Where before we would diagnose all failures with "has no member named", we now
   distinguish between when there is no member, and when you can't use it.  When you
   can't use it, you get a vauge "cannot use it" diagnostic, but...
 - This provides an infrastructure for diagnosing other kinds of problems (e.g. 
   trying to use a private member or a static member from an instance).
 - Improves a number of cases where failed type member constraints would produce uglier
   diagnostics than a different constraint failure would.
 - Resolves a number of rdars, e.g. (and probably others):
   <rdar://problem/20294245> QoI: Error message mentions value rather than key for subscript



Swift SVN r30715
2015-07-28 07:04:22 +00:00
Joe Groff
58ccc8e160 Sema: Check mutating-ness of methods irrespective of reference semantics.
Some types are born to mutating methods, and some types have mutating methods thrust upon them. Protocol extensions put classes into the latter category. A 'mutating' protocol extension method requires a mutable 'self' base even when applied to a class. Fix the synthesized getter for lazy properties so that it's only marked 'mutating' on value types, since otherwise it'd now be impossible to access lazy properties on immutable class references.

Fixes part of rdar://problem/21578832, allowing 'mutating' methods from protocol extensions to apply to mutable class references. Properties with mutating setters are still improperly allowed on immutable class reference bases though.

Swift SVN r30557
2015-07-24 00:10:16 +00:00
Doug Gregor
f00e5bc6ab Allow a variadic parameter anywhere in the parameter list.
Requiring a variadic parameter to come at the end of the parameter
list is an old restriction that makes no sense nowadays, and which we
had all thought we had already lifted. It made variadic parameters
unusable with trailing closures or defaulted arguments, and made our
new print() design unimplementable.

Remove this restriction, replacing it with a less onerous and slightly
less silly restriction that we not have more than one variadic
parameter in a given parameter clause. Fixes rdar://problem/20127197.

Swift SVN r30542
2015-07-23 18:45:29 +00:00
Joe Pamer
a900fd7bc2 Fix a couple of common crashers related to functions with default arguments. (rdar://problem/21799331 and rdar://problem/21643052, plus dupes.)
If a function declaration possessed default parameters, and was invoked with a single argument expression that was modeled as a type variable, the compiler would often crash during type application.  This was due to the fact that during simplification, we would bind the type variable to the full tuple type of the parameter list. Later on, during constraint application, we would then look to whatever expression created the type variable for information on its default arguments - even if no such thing was possible. (E.g., we would examine, say, an IfExpr expecting to find information on its default arguments.) In these cases, we should have instead been binding the argument type variable to the first element of the parameter tuple.

Swift SVN r30486
2015-07-22 01:19:08 +00:00
John McCall
50a667b295 Instead of assuming that the return type of a closure
with no returns *must* be (), add a defaulting constraint
so that it will be inferred as () in the absence of
other possibilities.

The chief benefit here is that it allows better QoI when
the user simply hasn't yet written the return statement.

Doing this does regress a corner case where an attempt
to recover from an uncalled function leads to the
type-checker inferring a result for a closure that
doesn't make any sense at all.

Swift SVN r30476
2015-07-22 00:13:02 +00:00
Devin Coughlin
6ecaca24a3 Sema: Remove leftover availability lookup from treating unavailable symbols as optional. NFC.
This should have been removed in r30355 when I removed the rest of that functionality.

Swift SVN r30422
2015-07-20 22:24:14 +00:00
Chris Lattner
ab487a17b5 Introduce a new TypeCheckExprFlags::AllowUnresolvedTypeVariables option,
which allows solving of a constraint system to succeed without emitting
errors in the face of ambiguous solutions.  This is important for CSDiag
because it is in the business of trying to solve subexpressions of a global
expression - and it wants to know the difference between a subexpression
that is inherently impossible to solve, vs one that is simply ambiguous
because its context has been removed.

Use this in CSDiag's typeCheckChildIndependently() to provide it an
extra flag that enables this behavior.  This is currently unused, so NFC 
with this patch.


Swift SVN r30402
2015-07-20 16:16:54 +00:00
Chris Lattner
a6a172511b Remove duplicated comment.
Swift SVN r30399
2015-07-20 15:55:17 +00:00
Chris Lattner
33c6de5001 disable array/string -> pointer conversions in operator contexts that lack an &. They
are still allowed in non-operator calls.



Swift SVN r30398
2015-07-20 15:54:13 +00:00
Devin Coughlin
c1caddae62 Remove experimental support for treating unavailable symbols as optional.
This has always been off by default and is a language direction we have decided not to
pursue.

Swift SVN r30355
2015-07-18 01:56:25 +00:00
Doug Gregor
29764dc34d Remove the ability to access the "0" member of a scalar.
This is a travesty that makes it hard to reason about tuple vs. scalar
types. It's only use in our own code involved dealing with the
difference in element types between Set and Dictionary in
gyb-generated code. Ewww. Fixes rdar://problem/17963034.

Swift SVN r30074
2015-07-10 17:28:53 +00:00
Slava Pestov
fd141bead9 Sema: Remove unused inExpression parameter from ASTContext::getBridgedToObjC(), NFC
Progress on <rdar://problem/21215099>.

Swift SVN r29968
2015-07-08 06:49:27 +00:00
Doug Gregor
c410bce986 We cannot validate a declaration while its context is being type-checked.
Fixes rdar://problem/21538521 and a few crashers.

Swift SVN r29957
2015-07-08 04:21:51 +00:00
Doug Gregor
3023a710fc Split TypeBase::isDependentType() into isTypeParameter() and hasTypeParameter().
The isDependentType() query is woefully misunderstood. Some places
seem to want it to mean "a generic type parameter of dependent member
type", which corresponds to what is effectively a type parameter in
the language, while others want it to mean "contains a type parameter
anywhere in the type". Tease out these two meanings in
isTypeParameter() and hasTypeParameter(), respectively, and sort out
the callers.

Swift SVN r29945
2015-07-07 21:20:54 +00:00
Chris Lattner
ca8cb3d19d fix <rdar://problem/21244068> QoI: IUO prevents specific diagnostic + fixit about non-implicitly converted bridge types
Swift SVN r29910
2015-07-03 22:31:29 +00:00
Chris Lattner
c619fd0fda fix <rdar://problem/21087089> Fixit for <RawOptionSetType>.allZeros
to improve swift 1 -> 2 migration.



Swift SVN r29903
2015-07-02 23:45:12 +00:00
Chris Lattner
612a41278a Teach TypeMember constraints to suggest force unwrapping optionals, the same way
value member constraints do.

This fixes:
<rdar://problem/21662365> QoI: diagnostic for for-each over an optional sequence isn't great

before we'd produce:
t.swift:3:10: error: '[Int]?' does not have a member named 'Generator'
for x in array {
         ^

now we produce:

t.swift:3:10: error: value of optional type '[Int]?' not unwrapped; did you mean to use '!' or '?'?
for x in array {
         ^



Swift SVN r29902
2015-07-02 23:25:08 +00:00
Slava Pestov
9a65927f40 Sema: Another fix for protocol self-conformance
It looks like we were checking in the wrong place, as a result we didn't
catch stuff like

class G<T : AnyObject> {}
_ = G<P>()

This would crash later in IRGen.

Make the conformsToProtocol() check do the right thing, and remove some
other miscellaneous diagnostics in the process. Also, make the
"type 'T' does not conform to protocol 'P'" diagnostic a bit more
detailed.

Unfortunately in a few instances we lose a more descriptive diagnostic to
a general 'cannot invoke 'foo' with argument list of type 'T'' error. The
argument matching diagnostics need to be addressed anyway though.

Fixes <rdar://problem/20311619>.

Swift SVN r29737
2015-06-26 07:29:56 +00:00
Slava Pestov
20f2488417 Sema: Don't allow access to instance members of an existential metatype
We don't know what's inside the existential type container, so we cannot
refer to uncurried instance members of the type without running into
theoretical difficulties.

Swift SVN r29378
2015-06-15 02:00:48 +00:00
Slava Pestov
1052d095c4 Sema: Fixes for conformance between higher-kind metatypes
For a concrete type A and protocol type P, A <c P now implies
A.Type <c P.Type, not just A.Type < P.Type. This in turn means
that A.Type.Type <c P.Type.Type. To make the coercion work,
recursively peel off metatype layers when collecting conformances
and in a similar situation in IRGen.

Swift SVN r29377
2015-06-15 02:00:47 +00:00
Slava Pestov
6a9790ed30 Sema: Fix compiler_crasher with FixKind::ForceOptional
We were using getRValueType() and getRValueObjectType() when
setting up the constraint, but getRValueType() when simplifying
it. This led to a crash if the type was a single-argument tuple
with a named argument.

Swift SVN r29376
2015-06-15 02:00:46 +00:00
Slava Pestov
6550c7a858 Sema: Fix subtyping between metatypes and existential metatypes
X.Protocol is an instance of Y.Type only if X conforms to Y. Since X
is a protocol, this is only true if X contains Y and Y is
self-conforming.

Note that this updates some tests that actually contained invalid code.

Fixes <rdar://problem/20915927>.

Swift SVN r29349
2015-06-08 07:59:16 +00:00
Joe Groff
bebfa969bd Sema: Allow 'x.init' references on metatype expressions.
If 'x.init' appears as a member reference other than 'self.init' or 'super.init' within an initializer, treat it as a regular static member lookup for 'init' members. This allows a more explicit syntax for dynamic initializations; 'self.someMetatype()' looks too much like it's invoking a method. It also allows for partial applications of initializers using 'someMetatype.init' (though this needs some SILGen fixes, coming up next). While we're in the neighborhood, do some other correctness and QoI fixes:

- Only lookup initializers as members of metatypes, not instances, and add a fixit (instead of crashing) to insert '.dynamicType' if the initializer is found on an instance.
- Make it so that constructing a class-constrained archetype type correctly requires a 'required' or protocol initializer.
- Warn on unused initializer results. This seems to me like just the right thing to do, but is also a small guard against the fact that 'self.init' is now valid in a static method, but produces a newly-constructed value instead of delegating initialization (and evaluating to void).

Swift SVN r29344
2015-06-08 04:11:28 +00:00
Slava Pestov
3277bb21c0 Sema: On Linux, AnyObject is not @objc
This fixes a build regression from r29337.

Swift SVN r29340
2015-06-07 21:49:10 +00:00
Slava Pestov
086b9961ba Sema: Don't allow static member access on protocol metatypes
If P is a protocol, calling static methods or constructors
via values of type P.Protocol makes no sense, so let's prohibit
this.

Fixes <rdar://problem/21176676>.

Swift SVN r29338
2015-06-07 10:16:23 +00:00
Slava Pestov
322f58d8b1 Sema: Tighten up existential vs generic type parameter distinction
Rename existentialConformsToSelf() to existentialTypeSupported(). This
predicate is the "protocol has no Self or associated type requirements"
check, which is a looser condition than self-conformance. This was being
tested to see if the user could refer to the protocol via an existential
type.

The new existentialConformsToSelf() now checks for protocol being @objc,
and for the absence of static methods. This is used as part of the
argument type matching logic in matchType() to determine if the
existential can be bound to a generic type parameter.

The latter condition is stricter, for two reasons:

1) We allow binding existentials to multiple type parameters all sharing
   the same generic type parameter T, so we don't want the user to be
   able to see any static methods on T.
2) There is an IRGen limitation whereby only existentials without witness
   tables can be passed in this manner.

Using the above, the representsNonTrivialGenericParameter() function
has been renamed to canBindGenericParamToExistential(). It now allows
an existential type to be bound to a generic type parameter only under
the following circumstances:

A) If the generic type parameter has no conformances, the match is allowed.

B) If the generic type parameter has at least one conformance, then all
   of the conformances on the generic type parameter must be
   existentialConformsToSelf() (condition 1 above), and all conformances
   on the existential must be @objc (condition 2 above).

Fixes <rdar://problem/18378390> and <rdar://problem/18683843>, and lays
the groundwork for fixing a few other related issues.

Swift SVN r29337
2015-06-07 10:16:21 +00:00