Commit Graph

510 Commits

Author SHA1 Message Date
Jordan Rose
042569a3be Optional: Replace uses of Nothing with None.
llvm::Optional (like Swift.Optional!) uses None as its placeholder value,
not Nothing.

Swift SVN r22476
2014-10-02 18:51:42 +00:00
Doug Gregor
8cf718f9f6 Don't allow a generic parameter with a non-@objc protocol requirement to bind to an existential.
We don't properly open up the existential to make this work, which
leads to an IRGen crash. Reject the uses of generics that would cause
such a crash rdar://problem/17491663.


Swift SVN r21946
2014-09-15 17:36:26 +00:00
Doug Gregor
e002261865 Diagnose free type variables that correspond to generic parameters.
t2.swift:3:1: error: argument for generic parameter 'U' could not be
inferred
f(i)
^
t2.swift:2:6: note: in call to function 'f'
func f<T, U>(t: T) -> U? { return nil }
     ^

Our lack of decent locator information means that we don't get notes
in all of the cases we want them. I'll look at that separately.

Swift SVN r21921
2014-09-12 20:27:21 +00:00
Doug Gregor
dd6c32cf3e Use ConstraintLocator::Member for members rather than ConstraintLocator::MemberRefBase.
Swift SVN r21920
2014-09-12 20:27:20 +00:00
Joe Pamer
fa692976f5 If an expression fails to type check, and there were no failures posted or constraints to mine for a reason, we can be fairly certain that the reason for the failure was a lack of contextual type data. In this case, we should post a diagnostic rather than let the failure slip through. This addresses crash suite scenarios 001, 005, 020 and 045. (rdar://problem/16712071, rdar://problem/17317691, rdar://problem/17317691)
You'll notice that emitting this diagnostic will make some already noisy closure-related errors slightly more so. This is unfortunate, but for the time-being it's better than crashing.

Swift SVN r21817
2014-09-09 23:58:59 +00:00
Joe Pamer
a7a541fb1a Turn the "expression too complex" note into a proper error, and stop printing any auxiliary diagnostics that are produced as a result of the incompletely solved system. The "too complex" note had discoverability issues in Xcode, and the auxiliary errors were often misleading. (rdar://problem/18151024)
Swift SVN r21790
2014-09-08 22:43:29 +00:00
Joe Pamer
71cf758055 Mitigate exponential solver behavior (rdar://problem/17162690)
While we work out the remaining performance improvements in the type checker, we can improve the user experience for some "runaway solver" bugs by setting a limit on the amount of temporary memory allocated for type variables when solving over a single expression.

Exponential behavior usually manifests itself while recursively attempting bindings over opened type variables in an expression. Each one of these bindings may result in one or more fresh type variables being created. On average, memory consumption by type variables is fairly light, but in some exponential cases it can quickly grow to many hundreds of megabytes or even gigabytes. (This memory is managed by a distinct arena in the AST context, so it's easy to track.) This problem is the source of many of the "freezing" compiler and SourceKit bugs we've been seeing.

These changes set a limit on the amount of memory that can be allocated for type variables while solving for a single expression. If the memory threshold is exceeded, we can surface a type error and suggest that the user decompose the expression into distinct, less-complex sub-expressions.

I've set the current threshold to 15MB which, experimentally, avoids false positives but doesn't let things carry on so long that the user feels compelled to kill the process before they can see an error message. (As a point of comparison, the largest allocation of type variable data while solving for a single expression in the standard library is 592,472 bytes.) I've also added a new hidden front-end flag, "solver-memory-threshold", that will allow users to set their own limit, in bytes.

Swift SVN r20986
2014-08-03 23:10:42 +00:00
Joe Pamer
0896a12197 Further improve type checker diagnostics through a variety of means.
Start capitalizing on some of the new diagnostic machinery in a few different ways:
- When mining constraints for type information, utilize constraints "favored" by the overload resolution process.
- When printing type variables, if the variable was created by opening a literal expression, utilize the literal
  default type or conformance if possible.
- Utilize syntactic information when crafting diagnostics:
	- If the constraint miner can produce a better diagnostic than the recorded failure, diagnose via constraints.
	- Factor in the expression kind when choosing which types to include in a diagnostic message.
- Start customizing diagnostics based on the amount of type data available.

What does all this mean?
- Fewer type variables leaking into diagnostic messages.
- Far better diagnostics for overload resolution failures. Specifically, we now print proper argument type data
  for failed function calls.
- No more "'Foo' is not convertible to 'Foo'" error messages
- A greater emphasis on type data means less dependence on the ordering of failed constraints. This means fewer
  inscrutable diagnostics complaining about 'UInt8' when all the constituent expressions are of type Float.

So we still have a ways to go, but these changes should greatly improve the number of head-scratchers served up
by the type checker.

These changes address the following radars:
rdar://problem/17618403
rdar://problem/17559042
rdar://problem/17007456
rdar://problem/17559042
rdar://problem/17590992
rdar://problem/17646988
rdar://problem/16979859
rdar://problem/16922560
rdar://problem/17144902
rdar://problem/16616948
rdar://problem/16756363
rdar://problem/16338509

Swift SVN r20927
2014-08-01 23:32:19 +00:00
Joe Groff
13fd70edbb Sema: Diagnose OptionalObject failures arising from BindOptional expressions.
Produce an appropriate error message when a BindOptional-originated OptionalObject constraint fails to bind to an actual optional type. Will be tested when the switch for lvalue '?' is flipped on.

Swift SVN r20648
2014-07-28 22:24:14 +00:00
Joe Groff
25fa3f399a Sema: Diagnose ambiguous member references devoid of context.
Handle an UnresolvedDot in the fallback case of the constraint solver where there are no active constraints but the system is ambiguous. Fixes the common case of 'Array.map' taking down SourceKit halfway through typing an expression. <rdar://problem/17125912>

Swift SVN r20450
2014-07-24 00:15:01 +00:00
Chris Lattner
6867ce2e2c fix <rdar://problem/17763388> ambiguous operator emits same candidate multiple times
It seems not-great that the same candidate can get into the overload set for a DeclRefExpr
multiple times, but if it does, don't expose this to the user.



Swift SVN r20315
2014-07-22 16:44:25 +00:00
Jordan Rose
14196453cc Add a special diagnostic when a type has no accessible initializers.
Rather than just saying "'Foo' is not constructible with '()'", say
"'Foo' cannot be constructed because it has no accessible initializers",
which would help framework authors realize what they did wrong.

<rdar://problem/17717714>

Swift SVN r20232
2014-07-20 19:52:24 +00:00
Joe Groff
8e757bd21f Sema: Diagnose OptionalObject constraint failures.
Don't regress error reporting when optional lvalues are enabled and OptionalObject constraints fail.

Swift SVN r20229
2014-07-20 18:16:38 +00:00
Joe Groff
ed88875794 Sema: Propagate lvalue-ness through the postfix ! operator.
Create a new "OptionalObject" constraint kind in the solver that relates an optional type to its payload type, preserving lvalue-ness, and use it to model ForceValueExpr under the optional-lvalues regime.

Swift SVN r20140
2014-07-18 04:37:00 +00:00
Doug Gregor
bd9ead4371 Remove -[no-]strict-keyword-arguments.
Swift SVN r19850
2014-07-11 16:58:35 +00:00
Doug Gregor
0c31ff5681 Move bridging conversions into the type checker.
Previously, bridged value types and their corresponding Objective-C
classes allow inter-conversion via a number of user-defined conversion
functions in the Foundation module. Instead, make this a general
feature of the type checker so we can reason about it more
directly. Fixes <rdar://problem/16956098> and
<rdar://problem/17134986>, and eliminates 11 (half) of the
__conversion functions from the standard library and overlays.

A few notes:
  - The XCTest changes are because a String can no longer directly
  conform to CVarArg: this is a Good Thing (TM), because it should be
  ambiguous: did you mean to pass it as an NSString or a C string?
  - The Objective-C representations for the bridged collections are
  hard-coded in the type checker. This is unfortunate and can be
  remedied by adding another associated type to the
  _BridgedToObjectiveC protocol.

Swift SVN r19618
2014-07-07 18:04:18 +00:00
Joe Groff
3c539b7f24 Sema: Look through optional types for .member lookup.
When we see a '.member' expression in optional context, look for the member in the optional's object type if it isn't found in Optional itself. <rdar://problem/16125392>

Swift SVN r19469
2014-07-02 16:33:45 +00:00
Doug Gregor
5a08a69ebc Remove NewArrayExpr entirely; reject "new" expressions in the parser.
Swift SVN r19293
2014-06-27 15:57:48 +00:00
Joe Pamer
ba15b81fe3 When using constraint data to generate diagnostics for conversion failures, we usually use the type of the expression in question as the basis type. (E.g., "Cannot convert type 'ExprType' to type 'Foo'.) However, if the expression type is already the same as the type being compared against, use the "first type" of the constraint. This will help us generate a more sensible error message. (rdar://problem/17075954, rdar://problem/17007456)
Swift SVN r19259
2014-06-26 22:11:40 +00:00
Joe Pamer
a314db950e Remove another point of non-determinism while tracking failed constraints, and use this as an opportunity to improve certain diagnostics. (rdar://problem/16808495)
Swift SVN r19244
2014-06-26 19:48:48 +00:00
Joe Pamer
b38cd540ad Address rdar://problem/17125445 ("dead _ in top level code crashes silgen")
DiscardAssignment expressions are special in that during constraint generation they'll introduce a new type variable, but not place any constraints upon it.  (They are the only expression kind that behaves in this way.) If no subsequent expressions constrain the type variable, we may end up with a failed constraint system that's devoid of constraints, and hence no information to synthesize a diagnostic from. With no diagnostic associated with the DiscardAssignmentExpr's source location, we'll attempt to generate SIL and raise an assertion failure. Fortunately, we can detect these cases during the constraint salvage phase, and raise an appropriate error.

Swift SVN r19020
2014-06-20 00:06:15 +00:00
Doug Gregor
1c06c309bc Automatically reflect non-Foundation categories of bridged Objective-C classes onto their bridged value types.
This makes categories of NSString, NSArray, and NSDictionary available
on String, Array, and Dictionary. Note that we only consider
categories not present in the Objective-C Foundation module, because
we want to manually map those APIs ourselves. Hence, no changes to the
NSStringAPI. Implements <rdar://problem/13653329>.


Swift SVN r18920
2014-06-16 09:07:00 +00:00
Joe Groff
18b4650f50 Sema/SILGen: Make default argument generators work for generic functions.
Tweak the AST representation and type-checking of default arguments to preserve a full ConcreteDeclRef with substitutions to the owner of the default arguments. In SILGen, emit default argument generators with the same genericity as the original function.

Swift SVN r18760
2014-06-09 22:16:57 +00:00
Joe Pamer
e4e472bf09 When emitting diagnostics for failed constraints, favor conformances with fewer unsubstituted type variables. (rdar://problem/16892723)
Swift SVN r18388
2014-05-19 04:29:15 +00:00
Joe Pamer
a74c44a6e4 Fix two crashing bugs related to checked downcasts - rdar://problem/16093456 and rdar://problem/16892211.
Swift SVN r18118
2014-05-15 18:36:44 +00:00
Joe Pamer
a17cec6cb2 Some test and diagnostic cleanup:
- Mine conjunction constraints for constraint failure data. (rdar://problem/16833763)
- Rather than crash, add a diagnostic to signify a missing user constraint. (rdar://problem/16747055) I don't have a deterministic repro for this to include as a test, but users hit it from time to time, I'd like to address this issue holistically, and we're hoping that the new diagnostic will help us collect isolated repros.
- As promised, remove the temporary "compiler_submit_version" build configuration predicate in time for WWDC. (rdar://problem/16380797)

Swift SVN r17705
2014-05-08 18:46:08 +00:00
Doug Gregor
36cbcccbe9 Generalize constraint application under -strict-keyword-arguments.
Introduce a new locator kind for argument/parameter comparisons that
tracks both the argument and the parameter, which we will eventually
use in diagnostics more regularly. For now, this helps us smooth over
scalar-to-tuple/tuple-to-tuple/tuple-to-scalar nonsense when dealing
with calls.

Fix a pile of fallout from this change.

Swift SVN r17648
2014-05-07 22:36:49 +00:00
Joe Pamer
11441abc4b Rather than special-case NSString conversions in the type checker, utilize the _BridgedToObjectiveC protocol conformance on the type being converted if available. This will allow us to more broadly accommodate bridged conversions between types.
Swift SVN r17482
2014-05-05 19:40:03 +00:00
Doug Gregor
eb7a9144a8 Bring keyword arguments to subscripts.
Subscript declarations were still encoding the names of index
variables in the subscript type, which unintentionally made them
keyword arguments. Bring subscript declarations into the modern day,
using compound names to encode the subscript argument names, which
provides consistency for the keyword-argument world
<rdar://problem/14462349>. Note that arguments in subscripts default
to not being keyword arguments, which seems like the right default.

We now get keyword arguments for subscripts, so one can overload
subscripts on the names of the indices, and distinguish at the call
site. Under -strict-keyword-arguments, we require strictness here as well.

The IRGen/IDE/SILGen test updates are because the mangling of common
subscripts changed from accidentally having keyword arguments to not
having keyword arguments.

Swift SVN r17393
2014-05-04 19:31:09 +00:00
Doug Gregor
98a359bd38 Keyword argument QoI: diagnostic missing/extra arguments directly.
Swift SVN r17368
2014-05-04 06:21:49 +00:00
Joe Pamer
62f9932795 Accept an Identifier instead of a StringRef in the new "could not find member" diagnostic.
Swift SVN r17342
2014-05-04 00:22:11 +00:00
Joe Pamer
ab576bb84b The constraint solver should try to diagnose all failures, rather than use a catch-all error message.
Swift SVN r17335
2014-05-03 23:03:50 +00:00
Joe Pamer
006158ff56 Always emit detailed type check diagnostics, update unit tests to reflect this.
Swift SVN r17334
2014-05-03 23:03:47 +00:00
Joe Pamer
0a81cdd730 Factor constraint diagnostic search functionality out of the salvage method.
Swift SVN r17257
2014-05-02 17:32:20 +00:00
Joe Pamer
66391bd9e9 More improvements to constraint solver diagnostics.
Swift SVN r17256
2014-05-02 17:32:19 +00:00
Joe Pamer
47628dbfb6 A couple more simple tweaks to improve type-check error reporting.
Swift SVN r17099
2014-04-30 17:54:56 +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
Joe Pamer
519e0ec82d Tweak the heuristics for mining inactive constraints for possible reasons why a constraint system failed to converge on a solution. (For the time-being, favor conversion and overload constraints over others.)
Swift SVN r16994
2014-04-28 21:05:40 +00:00
Joe Pamer
1beb2db860 Begin QOI work for rdar://problem/16007230 by making it possible to get slightly more precise constraint solver errors when using the detailed-diagnostics flag.
The work has obviously barely begun, but this change may be immediately useful to some people.

Swift SVN r16989
2014-04-28 19:13:08 +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
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
Doug Gregor
887fada26f Make the locator associated with an assignment source based on the assignment.
For an assignment "x = y", the locator for the conversion constraint
is an "assign source" locator anchored on the AssignExpr (not the
source expression), so we can properly relate source to destination.

Swift SVN r16931
2014-04-27 16:40:42 +00:00
Chris Lattner
371a9316f4 - Introduce a new 'nonmutating' context sensitive keyword, and use it instead of @!mutating.
- Change the parser to unconditionally reject @mutating and @!mutating with a fixit and 
  specific diagnostic to rewrite them into the [non]mutating keyword.
- Update tests.

This resolves <rdar://problem/16735619> introduce nonmutating CS keyword and remove the attribute form of mutating all together



Swift SVN r16892
2014-04-26 21:00:06 +00:00
Nadav Rotem
2dd564b102 Change the diagnostics message to: "immutable value of type ‘X’ only has mutating members named ‘Y'"
Swift SVN r16458
2014-04-17 16:53:08 +00:00
Nadav Rotem
960a6f2892 Improve the diagnostics of access to non-mutating members.
Swift SVN r16446
2014-04-17 06:34:15 +00:00
Doug Gregor
b0149f3f1b Switch a use of ArgParamPatterns -> BodyParamPatterns.
Swift SVN r16390
2014-04-16 03:14:43 +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
Joe Groff
bd8b2d33e7 Fix typos in diagnostics.
Swift SVN r14802
2014-03-07 23:24:37 +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
3e08673772 Introduce patterns for self arguments to ctors and dtors, making them uniform
with FuncDecls.  This allows us to eliminate special case code for handling
self in various parts of the compiler.

This also improves loc info (debug info and AST info) because 'self' now
has a location instead of being invalid.

I also took the opportunity to factor a bunch of places creating self decls
to use similar patterns and less copy and paste code.



Swift SVN r13196
2014-01-31 02:17:22 +00:00