Commit Graph

2695 Commits

Author SHA1 Message Date
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
Chris Willmore
df9136e0e0 When unifying two type variables, make sure the new representative type
variable has the must-be-materializable bit set if the old one does.
When assigning a fixed type to a type variable that must be
materializable, transfer the bit to any type variables within the fixed
type, as appropriate.

Add Options field to SavedTypeVariableBinding to save/restore type
variable options during solution.

<rdar://problem/21026806> Propagate MustBeMaterializable bit among type variables appropriately

Swift SVN r28883
2015-05-21 19:53:48 +00:00
Chris Lattner
d6ba8666ca various parts of the compiler are doggedly trying to figure out
whether a getter is mutating or a setter is nonmutating, centralize
it into methods on AbstractStorageDecl, NFC.


Swift SVN r28823
2015-05-20 05:00:13 +00:00
Chris Willmore
5905d07a08 Don't bind generic type parameters to non-materializable types.
Add a new option, TVO_MustBeMaterializable, to
TypeVariableType::Implementation, and set it for type variables
resulting from opening a generic type. This solution isn't complete (we
don't yet copy the non-materializable bit on unification of type
variables, and it's possible to bind a must-be-materializable type
variable to a type with type variables that later get bound to
non-materializable types) but it addresses all reported crashes for this
issue.

<rdar://problem/20807269> Crash in non-materializable type

Swift SVN r28792
2015-05-19 21:50:26 +00:00
Chris Lattner
580315d9ee fix <rdar://problem/17652759> Default arguments cause crash with tuple permutation
This just deletes some code out of matchCallArguments to make sure that CSApply
and CSSimplify do the same argument matching.  This code presumably
dated to a period of time when the stuff after it was just for error recovery,
but now we do argument reordering matching stuff after this point.


Swift SVN r28670
2015-05-17 01:12:47 +00:00
Ted Kremenek
9f9bb725cf Rename '_ErrorType' to 'ErrorType'.
Swift SVN r28293
2015-05-07 21:59:29 +00:00
Doug Gregor
b8995b0aa3 Transform the Module class into ModuleDecl.
Modules occupy a weird space in the AST now: they can be treated like
types (Swift.Int), which is captured by ModuleType. They can be
treated like values for disambiguation (Swift.print), which is
captured by ModuleExpr. And we jump through hoops in various places to
store "either a module or a decl".

Start cleaning this up by transforming Module into ModuleDecl, a
TypeDecl that's implicitly created to describe a module. Subsequent
changes will start folding away the special cases (ModuleExpr ->
DeclRefExpr, name lookup results stop having a separate Module case,
etc.).

Note that the Module -> ModuleDecl typedef is there to limit the
changes needed. Much of this patch is actually dealing with the fact
that Module used to have Ctx and Name public members that now need to
be accessed via getASTContext() and getName(), respectively.

Swift SVN r28284
2015-05-07 21:10:50 +00:00
Doug Gregor
a55050376f Switch TypeChecker::lookupMemberType() over to NameLookupOptions. NFC
Swift SVN r28283
2015-05-07 21:10:44 +00:00
Doug Gregor
da47d233da Teach TypeChecker::lookupConstructors() to use NameLookupOptions. NFC
Swift SVN r28282
2015-05-07 21:10:43 +00:00
Doug Gregor
83d336e714 Stop using TupleTypeElts in the type checking for arg/param matching.
Instead, use a dedicated struct. NFC

Swift SVN r28207
2015-05-06 17:05:27 +00:00
Chris Willmore
331b570329 Remove reverse subtype constraint between Objective-C classes when type
checking checked cast via bridging. It prevented bridging upcasts using
'as!' from typechecking; we should emit an 'as!'->'as' warning instead.

Also, use ExplicitConversion constraint instead of Conversion when
determining whether a checked cast can be carried out unconditionally.
This matches the constraint used after applying the 'as!'->'as' fixit.

(Also, fix the error that was responsible for breaking
the expr/cast/bridged.swift test.)

<rdar://problem/19813772>

Swift SVN r28034
2015-05-01 08:27:19 +00:00
Ted Kremenek
acf0c496e2 Revert "Remove reverse subtype constraint between Objective-C classes when type"
This is breaking TEST 'Swift :: expr/cast/bridged.swift'.

Swift SVN r28030
2015-05-01 06:37:59 +00:00
Chris Willmore
7d413057aa Remove reverse subtype constraint between Objective-C classes when type
checking checked cast via bridging. It prevented bridging upcasts using
'as!' from typechecking; we should emit an 'as!'->'as' warning instead.

Also, use ExplicitConversion constraint instead of Conversion when
determining whether a checked cast can be carried out unconditionally.
This matches the constraint used after applying the 'as!'->'as' fixit.

<rdar://problem/19813772>

Swift SVN r28028
2015-05-01 06:21:50 +00:00
Doug Gregor
a30ca2a60d Replace bool parameter to TypeChecker::conformsToProtocol() with an option set.
NFC; we can extend this option set more readily later.

Swift SVN r27894
2015-04-29 00:08:22 +00:00
Chris Lattner
976620b513 random tidying up, rename the raw_ostream form of
ConstraintSystem::dump to ConstraintSystem::print for
consistency with other parts of the compiler.  Enhance
CS::print to print the ID # of a Type Variable, so you
don't have to count them to realize that you're looking
at typevar #13


Swift SVN r27874
2015-04-28 17:50:24 +00:00
Chris Lattner
d18740d603 Fix <rdar://problem/19773562> Closures executed immediately { like this }() are not automatically @noescape
In addition to being better for performance in these cases, this disables the "self." 
requirement in these blocks.  {}() constructs are often used to work around statements
that are not exprs in Swift, so they are reasonably important.

Fixing this takes a couple of pieces working together:
 - Add a new 'extraFunctionAttrs' map to the ConstraintSystem for solution
   invariant function attributes that are inferred (like @noescape).
 - Teach constraint simplification of function applications to propagate 
   @noescape between unified function types.
 - Teach CSGen of ApplyExprs to mark the callee functiontype as noescape
   when it is obviously a ClosureExpr.

This is a very limited fix in some ways: you could argue that ApplyExpr should
*always* mark its callee as noescape.  However, doing so would just introduce a
ton of function conversions to remove it again, so we don't do that.




Swift SVN r27723
2015-04-25 23:34:18 +00:00
Joe Pamer
3f572cab62 Remove hack to bind throwing initializer overloads based on the presence of a throwing initializer in the overload group.
Swift SVN r27694
2015-04-24 17:48:29 +00:00
Joe Pamer
220c92a09b Revert "Revert "A couple of 'throws' inference fix-ups:""
This reverts commit r27576.

(In some cases of catastrophic error recovery, ctor types may still be null during constraint solving, so it was wrong of me to assume otherwise.)

Swift SVN r27599
2015-04-22 20:34:05 +00:00
Dmitri Hrybenko
4229e8a14f Revert "A couple of 'throws' inference fix-ups:"
This reverts commit r27568 to unblock the buildbot.  It regressed three
compiler crashers:

    Swift :: compiler_crashers_fixed/0367-llvm-errs.swift
    Swift :: compiler_crashers_fixed/1769-getselftypeforcontainer.swift
    Swift :: compiler_crashers_fixed/1916-swift-nominaltypedecl-getdeclaredtypeincontext.swift

Swift SVN r27576
2015-04-22 08:42:21 +00:00
Joe Pamer
70fd1d6360 A couple of 'throws' inference fix-ups:
- When inferring 'throws' for a closure function type, look inside of catchless do blocks for 'try' expressions.
- When simplifying overload constriants for applications of throwing initializers, the bound member type of the initializer should also be marked as throwing.
  (Not doing so would cause us to incorrectly reject the overload.)

Swift SVN r27568
2015-04-22 06:39:39 +00:00
Joe Groff
3f241bb80a Sema: Handle NSError not being found during type checking better.
Fix crashes when type-checking ErrorType dynamic casts when NSError isn't available. Split the ErrorType execution tests into separate units for bridging and non-bridging, so that we get better coverage of this situation in both ObjC and non-ObjC builds. Fixes rdar://problem/20585210.

Swift SVN r27556
2015-04-22 03:49:01 +00:00
Joe Groff
be7c339af8 Type checker: Increase the score of metatype-to-object conversions.
Metatype-to-metatype conversions should be favored.

Swift SVN r27415
2015-04-17 05:30:38 +00:00