Commit Graph

510 Commits

Author SHA1 Message Date
Chris Lattner
585cf17b67 don't use the term "r-value" in a diagnostic, use immutable value. Thanks to Jordan for pointing this out.
Swift SVN r28953
2015-05-23 04:10:25 +00:00
Chris Lattner
d29d030ea3 this is the code that went with r28942
Swift SVN r28943
2015-05-23 01:27:06 +00:00
Chris Lattner
7d881f8440 - Switch "&x", "++x" and "x+=1" onto the new style mutability diagnostics,
which tell you what the problem is, not just that you have one.
- Enhance diagnostics to be more specific about function calls producing 
  rvalues.



Swift SVN r28939
2015-05-23 00:17:12 +00:00
Chris Lattner
ef9d112ffc consolidate some diagnostics in a helper in prep for making them more
rich, consistent, and pervasive.


Swift SVN r28938
2015-05-22 23:03:26 +00:00
Chris Lattner
3f0ca3e2c8 - Change the general assignment failure diagnostic to include the type of the
result and make it more specific.
- Move the 'assignment failure' diagnostic logic to CSDiags.cpp where it belongs.



Swift SVN r28937
2015-05-22 22:07:26 +00:00
Chris Lattner
8853d19c2b Dramatically improve the diagnostics when a store is invalid, by taking
into account accesibility, assignments to self in a non-mutating
method (consistently), recursive components of an lvalue that makes it 
non-settable, etc.  Now we tell you what the *problem* was, instead of
just whining.

This fixes:
<rdar://problem/19370429> QoI: fixit to add "mutating" when assigning to a member of self in a struct
<rdar://problem/17632908> QoI: Modifying struct member in non-mutating function produces difficult to understand error message

in their full generality.



Swift SVN r28867
2015-05-21 05:53:36 +00:00
Chris Willmore
278d342c22 Remove "materializable" language from diagnostic for 20807269.
Swift SVN r28798
2015-05-19 23:02:22 +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
John McCall
5d4fa0b7b6 A better fix: stop storing the address of a type in the constraint
system in the first place, because it's totally immutable on all
access paths.

Swift SVN r28595
2015-05-15 01:05:56 +00:00
Joe Pamer
e12cc2dcc0 Account for typeExprs as arguments when diagnosing CallExpr failures. This is surprisingly common, and accounts for (rdar://problem/19750901&19471376&19615313&19686369&19823567 and likely many more dupes I've yet to dig out.)
Swift SVN r28390
2015-05-10 00:31:09 +00:00
Joe Pamer
c6b30d9d8c Prioritize generalized conversion diagnostics over generalized overload diagnostics when diagnosing an error in a return expression. (rdar://problem/19735220)
Swift SVN r28389
2015-05-10 00:31:00 +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
da47d233da Teach TypeChecker::lookupConstructors() to use NameLookupOptions. NFC
Swift SVN r28282
2015-05-07 21:10:43 +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
9e6faca3bb Enhance constraints::resolveLocatorToDecl to look through IdentityExpr's like parens,
fixing:
<rdar://problem/18400194> Parenthesized function expression crashes the compiler

it still grosses me out that we're using diagnostics machinery for correctness of
type checking default arguments :-(



Swift SVN r27855
2015-04-28 04:03:46 +00:00
Chris Lattner
0b5c1125e4 improve and consistify diagnostics for non-lvalue inout arguments.
Swift SVN r27795
2015-04-27 01:05:29 +00:00
Chris Lattner
227e676b4b clean up diagnoseFailureForAssignExpr, no significant win though.
Swift SVN r27793
2015-04-27 00:45:10 +00:00
Chris Lattner
9f36074698 fix <rdar://problem/19711233> QoI: poor diagnostic for operator-assignment involving immutable operand
We now produce tailored diagnostics for assignment operators that are passed a non-mutable LHS,
e.g.:

t.swift:14:3: error: cannot pass 'let' value 'x' to mutating binary operator '/='
x /= 19
~ ^
t.swift:13:1: note: change 'let' to 'var' to make it mutable
let x = 42
^~~
var





Swift SVN r27780
2015-04-27 00:20:57 +00:00
Chris Lattner
43c7334abc fix two QoI issues:
- <rdar://problem/16306600> QoI: passing a 'let' value as an inout results in an unfriendly diagnostic
 - <rdar://problem/16927246> provide a fixit to change "let" to "var" if needing to mutate a variable

We now refer to an inout argument as such, e.g.:

t.swift:7:9: error: cannot pass 'let' value 'a' as inout argument
  swap(&a, &b)
        ^

we also produce a note with a fixit to rewrite let->var in trivial cases where mutation is
being assed for, e.g.:

t.swift:3:3: note: change 'let' to 'var' to make it mutable
  let a = 42
  ^~~
  var

The note is produced by both Sema and DI.



Swift SVN r27774
2015-04-26 21:51:50 +00:00
Chris Lattner
bebbdfe001 fix <rdar://problem/17691565> attempt to modify a 'let' variable with ++ results in typecheck error not being able to apply ++ to Float
We now produce diagnostics like:
 - cannot pass 'let' value 'a' to mutating unary operator '++'
 - cannot pass get-only property 'b' to mutating unary operator '++'
 - cannot pass immutable value of type 'Int64' to mutating unary operator '++'



Swift SVN r27772
2015-04-26 20:42:19 +00:00
Chris Willmore
d4db635e3d Add object literal syntax and _{Color,Image}LiteralConvertible protocols
Add syntax "[#Color(...)#]" for object literals, to be used by
Playgrounds for inline color wells etc. The arguments are forwarded to
the relevant constructor (although we will probably change this soon,
since (colorLiteralRed:... blue:... green:... alpha) is kind of
verbose). Add _ColorLiteralConvertible and _ImageLiteralConvertible
protocols, and link them to the new expressions in the type checker.
CSApply replaces the object literal expressions with a call to the
appropriate protocol witness.

Swift SVN r27479
2015-04-20 12:55:56 +00:00
Doug Gregor
9256250273 Fix type checking for a non-throwing function as a subtype of a throwing function.
A non-throwing function can be a trivial subtype of a throwing
function. Encode this rule more directly, introduce some additional
tests to ensure that we get the behavior right where we need exact
matches, and add a failure kind with custom diagnostic for cases where
function types mismatch due to 'throws'.

Swift SVN r27255
2015-04-13 18:39:39 +00:00
Joe Pamer
e4f7fee4f4 When gathering "linked" expressions to determine a favored constraint, consider top-level literals to be fair game.
This unblocks standard library work by preventing us from going exponential when extending existing struct types to have failable initializers. (rdar://problem/20336356)
On my laptop, it also results in a 7% end-to-end improvement in the time it takes to run our unit tests under DebugAssert (previously: 591.63s, now: 552.64s). Though, as usual, YMMV.

Swift SVN r27156
2015-04-09 01:54:34 +00:00
Chris Lattner
79ed57f9f2 standardize naming of tuples and tuple patterns on "elements".
Previously some parts of the compiler referred to them as "fields",
and most referred to them as "elements".  Use the more generic 'elements'
nomenclature because that's what we refer to other things in the compiler
(e.g. the elements of a bracestmt).

At the same time, make the API better by providing "getElement" consistently
and using it, instead of getElements()[i].

NFC.



Swift SVN r26894
2015-04-02 20:23:49 +00:00
Chris Willmore
433de9f2f1 Deal with constraint system failure without associated locator in FailureDiagnosis::diagnoseFailureForCallExpr().
<rdar://problem/20142523>

Swift SVN r26185
2015-03-16 18:41:59 +00:00
Chris Willmore
b3e0264e08 "@lvalue" shouldn't appear in general conversion failure diagnostic.
rdar://problem/19836341

Swift SVN r25826
2015-03-07 01:07:42 +00:00
Chris Willmore
fc561cd7ea Fix errant fixit "as!" -> "as?!"
"let x: Int? = 5 as Any as! Int" now suggests changing "as!" to "as?"
instead of "as?!".

rdar://problem/19883819

Swift SVN r25490
2015-02-23 22:59:46 +00:00
Joe Pamer
a3122ecf92 Provide a better, tailored diagnostic for result-type mismatches. (rdar://problem/19800727)
These changes include ChrisW's code review feedback.

Swift SVN r25340
2015-02-17 01:57:20 +00:00
Joe Pamer
81df1eeee0 Revert "Provide a better, tailored diagnostic for result-type mismatches. (rdar://problem/19800727)"
This reverts commit r25319.

Swift SVN r25333
2015-02-16 23:46:59 +00:00
Joe Pamer
6e9ddbafd0 Provide a better, tailored diagnostic for result-type mismatches. (rdar://problem/19800727)
Swift SVN r25319
2015-02-16 20:00:04 +00:00
Joe Pamer
9923e3ff89 When optimizing constraints for an unresolved dot expression, don't walk into the base expression - it will most likely result in an incorrect favored type, which can lead to "expression too complex" errors. (rdar://problem/19773442, rdar://problem/19770646)
Swift SVN r25129
2015-02-10 19:14:29 +00:00
Chris Willmore
e2ac9f65ac Add FixKind for 'as' -> 'as!' conversion
Penalize solutions that involve 'as' -> 'as!' changes by recording a Fix
when simplifying the corresponding checked-cast constraint.

<rdar://problem/19724719> Type checker thinks "(optionalNSString ?? nonoptionalNSString) as String" is a forced cast

Swift SVN r25061
2015-02-07 00:33:37 +00:00
David Farler
4ff6e69bf7 Add tests for noescape diagnostics with multiple overload matches
Don't emit multiple diagnostics for the same destination type -
this makes diagnostics less noisy where there are multiple overloads
with the same type.

Also, move the check for multiple overloads into
diagnoseFailureForCallExpr.

Swift SVN r24993
2015-02-05 05:35:30 +00:00
David Farler
8c4d1b2064 Emit diagnostics when multiple overload matches have the wrong function attribute
When threading a @noescape, @noreturn, or @autoclosure parameter through
to a function that doesn't expect one of those attributes, multiple
overload matches may cause the diagnostic to be dropped on the floor,
causing a crash in AST verification. If there is more than one failure
recorded for these attribute mismatches, don't fall back to the
contextual type diagnostics.

Fixes crash: rdar://problem/19704523

Swift SVN r24974
2015-02-04 21:27:13 +00:00
Joe Pamer
e452c87edb If an expression fails to type check because one of its sub-expressions is a failed call expression, make sure that the inner call expression's diagnostic is surfaced. Doing so address rdar://problem/19419422, rdar://problem/19437880 and rdar://problem/19559649.
Swift SVN r24935
2015-02-04 02:03:41 +00:00
Chris Willmore
ab86515fb2 <rdar://problem/19671476> Offer as -> as! changes in all nested contexts
When generating constraints for an 'as' expression, consider the
possibility that the code is supposed to be 'as!' instead of 'as'. Emit
the appropriate fixit if that branch of the disjunction is chosen by the
constraint solver.

This is a more comprehensive fix for <rdar://problem/19499340> than the
one in r24815.

Swift SVN r24872
2015-01-31 00:55:53 +00:00
Joe Pamer
0562411bb2 Improve support for diagnosing errors that result from contextual or conversion type mismatches. Doing so allows us to improve our diagnostics for a few important cases:
- Situations where the type of a return statement's result expression doesn't line up with the function's type annotation.
- Situations where the type of an initializer expression doesn't line up with its declaration's type pattern.
- Situations where we assume a conversion to a built-in protocol must take place, such as in if-statement conditionals.

(Addresses rdar://problem/19224776, rdar://problem/19422107, rdar://problem/19422156, rdar://problem/19547806 and lots of other dupes.)

Swift SVN r24853
2015-01-30 19:32:20 +00:00
Chris Willmore
aa3de78f2b <rdar://problem/19563805> Fuzzing Swift: performTypeChecking(...) crashes in ConformanceChecker::recordTypeWitness(...): Assertion failed: "Conformance should already have been verified"
Don't attempt to re-typecheck coerce expr when we're already pretty sure
it has failed.

Swift SVN r24834
2015-01-29 23:28:12 +00:00
Joe Pamer
6a70bd085e These changes implement some oft-requested tweaks and fixes to our closure implementation:
- Closures that are comprised of only a single return statement are now considered to be "single expression" closures. (rdar://problem/17550847)
- Unannotated single expression closures with non-void return types can now be used in void contexts. (rdar://problem/17228969)
- Situations where a multi-statement closure's type could not be inferred because of the lack of a return-type annotation are now properly diagnosed. (rdar://problem/17212107)

I also encountered a number of crashers along the way, which should now be fixed.

Swift SVN r24817
2015-01-29 18:48:39 +00:00
Chris Willmore
fd272aefb9 <rdar://problem/19499340> QoI: Nimble as -> as! changes not covered by Fix-Its
If the typechecking failure involves a conversion constraint anchored at
as 'as' or 'as!' expression, try diagnosing it directly.

Swift SVN r24815
2015-01-29 08:21:32 +00:00
Chris Willmore
6c21a6414a <rdar://problem/19421148> Calling init with a missing label doesn't provide a descriptive error when overloaded inits differ only by label
Swift SVN r24624
2015-01-22 01:12:45 +00:00
Chris Willmore
3dea623b71 <rdar://problem/19495142> Various incorrect diagnostics for explicit type conversions
Fix diagnostics for 'as' and 'as!' expressions by ensuring that the
conversion constraint used to generate them actually corresponds to the
expression in question. Add tests from 19495142.

Swift SVN r24547
2015-01-20 04:16:37 +00:00
Chris Willmore
03a6190a1f <rdar://problem/19031957> Change failable casts from "as" to "as!"
Previously the "as" keyword could either represent coercion or or forced
downcasting. This change separates the two notions. "as" now only means
type conversion, while the new "as!" operator is used to perform forced
downcasting. If a program uses "as" where "as!" is called for, we emit a
diagnostic and fixit.

Internally, this change removes the UnresolvedCheckedCastExpr class, in
favor of directly instantiating CoerceExpr when parsing the "as"
operator, and ForcedCheckedCastExpr when parsing the "as!" operator.

Swift SVN r24253
2015-01-08 00:33:59 +00:00
Joe Pamer
8d971d659c When checking a sub-expression independently of its parent to produce a diagnostic, a new expression node may be produced. Should that occur, reset the original node's type to that of the new node. This ensures that type variable data created for the temporary system does not leak outwards. (Addresses rdar://problem/19235186 and rdar://problem/19349724 to bring the ASAN Jenkins build online.)
Swift SVN r24241
2015-01-07 20:31:22 +00:00
Chris Lattner
3578c7fde0 Teach the type checker about subtype relationships of noescape, and use this
to allow passing off a no-escape formal parameter to the argument of a 
noescape parameter call.



Swift SVN r24126
2014-12-23 22:18:09 +00:00
Joe Pamer
f0fecf3ae5 Clean up some orphaned code from a previous refactoring.
Swift SVN r23785
2014-12-08 21:56:54 +00:00
Joe Pamer
dc338c2a71 Update wording of some new diagnostics.
Swift SVN r23783
2014-12-08 21:56:52 +00:00
Joe Pamer
2912159776 Improve diagnostics for expression typecheck errors
These changes make the following improvements to how we generate diagnostics for expression typecheck failure:
- Customizing a diagnostic for a specific expression kind is as easy as adding a new method to the FailureDiagnosis class,
  and does not require intimate knowledge of the constraint solver’s inner workings.
    - As part of this patch, I’ve introduced specialized diagnostics for call, binop, unop, subscript, assignment and inout
      expressions, but we can go pretty far with this.
    - This also opens up the possibility to customize diagnostics not just for the expression kind, but for the specific types
      involved as well.
- For the purpose of presenting accurate type info, partially-specialized subexpressions are individually re-typechecked
  free of any contextual types. This allows us to:
    - Properly surface subexpression errors.
    - Almost completely avoid any type variables in our diagnostics. In cases where they could not be eliminated, we now
      substitute in "_".
    - More accurately indicate the sources of errors.
- We do a much better job of diagnosing disjunction failures. (So no more nonsensical ‘UInt8’ error messages.)
- We now present reasonable error messages for overload resolution failures, informing the user of partially-matching
  parameter lists when possible.

At the very least, these changes address the following bugs:

<rdar://problem/15863738> More information needed in type-checking error messages
<rdar://problem/16306600> QoI: passing a 'let' value as an inout results in an unfriendly diagnostic
<rdar://problem/16449805> Wrong error for struct-to-protocol downcast
<rdar://problem/16699932> improve type checker diagnostic when passing Double to function taking a Float
<rdar://problem/16707914> fatal error: Can't unwrap Optional.None…Optional.swift, line 75 running Master-Detail Swift app built from template
<rdar://problem/16785829> Inout parameter fixit
<rdar://problem/16900438> We shouldn't leak the internal type placeholder
<rdar://problem/16909379> confusing type check diagnostics
<rdar://problem/16951521> Extra arguments to functions result in an unhelpful error
<rdar://problem/16971025> Two Terrible Diagnostics
<rdar://problem/17007804> $T2 in compiler error string
<rdar://problem/17027483> Terrible diagnostic
<rdar://problem/17083239> Mysterious error using find() with Foundation types
<rdar://problem/17149771> Diagnostic for closure with no inferred return value leaks type variables
<rdar://problem/17212371> Swift poorly-worded error message when overload resolution fails on return type
<rdar://problem/17236976> QoI: Swift error for incorrectly typed parameter is confusing/misleading
<rdar://problem/17304200> Wrong error for non-self-conforming protocols
<rdar://problem/17321369> better error message for inout protocols
<rdar://problem/17539380> Swift error seems wrong
<rdar://problem/17559593> Bogus locationless "treating a forced downcast to 'NSData' as optional will never produce 'nil'" warning
<rdar://problem/17567973> 32-bit error message is really far from the mark: error: missing argument for parameter 'withFont' in call
<rdar://problem/17671058> Wrong error message: "Missing argument for parameter 'completion' in call"
<rdar://problem/17704609> Float is not convertible to UInt8
<rdar://problem/17705424> Poor error reporting for passing Doubles to NSColor: extra argument 'red' in call
<rdar://problem/17743603> Swift compiler gives misleading error message in "NSLayoutConstraint.constraintsWithVisualFormat("x", options: 123, metrics: nil, views: views)"
<rdar://problem/17784167> application of operator to generic type results in odd diagnostic
<rdar://problem/17801696> Awful diagnostic trying to construct an Int when .Int is around
<rdar://problem/17863882> cannot convert the expression's type '()' to type 'Seq'
<rdar://problem/17865869> "has different argument names" diagnostic when parameter defaulted-ness differs
<rdar://problem/17937593> Unclear error message for empty array literal without type context
<rdar://problem/17943023> QoI: compiler displays wrong error when a float is provided to a Int16 parameter in init method
<rdar://problem/17951148> Improve error messages for expressions inside if statements by pre-evaluating outside the 'if'
<rdar://problem/18057815> Unhelpful Swift error message
<rdar://problem/18077468> Incorrect argument label for insertSubview(...)
<rdar://problem/18079213> 'T1' is not identical to 'T2' lacks directionality
<rdar://problem/18086470> Confusing Swift error message: error: 'T' is not convertible to 'MirrorDisposition'
<rdar://problem/18098995> QoI: Unhelpful compiler error when leaving off an & on an inout parameter
<rdar://problem/18104379> Terrible error message
<rdar://problem/18121897> unexpected low-level error on assignment to immutable value through array writeback
<rdar://problem/18123596> unexpected error on self. capture inside class method
<rdar://problem/18152074> QoI: Improve diagnostic for type mismatch in dictionary subscripting
<rdar://problem/18242160> There could be a better error message when using [] instead of [:]
<rdar://problem/18242812> 6A1021a : Type variable leaked
<rdar://problem/18331819> Unclear error message when trying to set an element of an array constant (Swift)
<rdar://problem/18414834> Bad diagnostics example
<rdar://problem/18422468> Calculation of constant value yields unexplainable error
<rdar://problem/18427217> Misleading error message makes debugging difficult
<rdar://problem/18439742> Misleading error: "cannot invoke" mentions completely unrelated types as arguments
<rdar://problem/18535804> Wrong compiler error from swift compiler
<rdar://problem/18567914> Xcode 6.1. GM, Swift, assignment from Int64 to NSNumber. Warning shown as problem with UInt8
<rdar://problem/18784027> Negating Int? Yields Float
<rdar://problem/17691565> attempt to modify a 'let' variable with ++ results in typecheck error about @lvalue Float
<rdar://problem/17164001> "++" on let value could give a better error message

Swift SVN r23782
2014-12-08 21:56:47 +00:00
Jordan Rose
2b0fbcbe80 Looking up conformances for a type isn't always a public use of the type.
Specifically, it's not when
- the conformance is being used within a function body (test included)
- the conformance is being used for or within a private type (test included)
- the conformance is being used to generate a diagnostic string

We're still a bit imprecise in some places (checking ObjC bridging), but
in general this means less of an issue for checking literals.

Swift SVN r23700
2014-12-05 00:23:24 +00:00
Ben Langmuir
e9e1666ab0 Update for upstream LLVM changes
* removal of StringMap's GetOrCreateValue
* SmallSet::insert now returns a pair like std::set

Swift SVN r23435
2014-11-19 16:49:30 +00:00