Commit Graph

24 Commits

Author SHA1 Message Date
Crazy凡
b2c054a59c Fixed issue #72085 where BinaryExpr fix-it suggested missing operator. 2024-03-12 11:58:25 +08:00
Luciano Almeida
c09af513a0 [tests] Adjusting regression tests argument mismatch diagnostics on suit 2020-07-23 20:05:27 -03:00
Pavel Yaskevich
b905113f5c [ConstraintSystem] Remove now completely obsolete CSDiag 2020-02-18 09:12:56 -08:00
Pavel Yaskevich
ca91a1bee0 [ConstraintSystem] Detect and diagnose OoO arguments in call to binary functions/operators
Identify problems like:

```swift
func foo(_ x: Int, _ y: String) {}

func bar(a: Int, b: String) {
  foo(b, a) // Instead of `foo(a, b)`
}
```

Where arguments are out-of-order and repair it by using OoO fix on the
parent locator.
2020-02-17 16:09:11 -08:00
Pavel Yaskevich
202234f325 [CSDiagnostics] Diagnose invalid optional unwrap via fixes
Detect and fix situations when (force) unwrap is used on
a non-optional type, this helps to diagnose invalid unwraps
precisely and provide fix-its.

Resolves: [SR-8977](https://bugs.swift.org/browse/SR-8977)
Resolves: rdar://problem/45218255
2018-12-05 08:10:07 -08:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
John McCall
c8c41b385c Implement SE-0077: precedence group declarations.
What I've implemented here deviates from the current proposal text
in the following ways:

- I had to introduce a FunctionArrowPrecedence to capture the parsing
  of -> in expression contexts.

- I found it convenient to continue to model the assignment property
  explicitly.

- The comparison and casting operators have historically been
  non-associative; I have chosen to preserve that, since I don't
  think this proposal intended to change it.

- This uses the precedence group names and higherThan/lowerThan
  as agreed in discussion.
2016-07-26 14:04:57 -07:00
Chris Lattner
3549ec5404 [QoI] make several improvements to the unused expression diagnostics, to go
along with recent policy changes:

- For expression types that are not specifically handled, make sure to
  produce a general "unused value" warning, catching a bunch of unused
  values in the testsuite.

- For unused operator results, diagnose them as uses of the operator
  instead of "calls".

- For calls, mutter the type of the result for greater specificity.

- For initializers, mutter the type of the initialized value.

- Look through OpenExistentialExpr's so we can handle protocol member
  references propertly.

- Look through several other expressions so we handle @discardableResult
  better.
2016-05-16 23:26:07 -07:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00
Chris Lattner
bd03e48090 teach typeCheckArgumentChildIndependently how to propagate type information
down to call argument lists that have more than one operand (heavily leveraging
"computeTupleShuffle").  This resolves a great number of QoI radars, including
things like:
<rdar://problem/19981782> QoI: poor diagnostic for call to memcmp with UInt length parameter

where we used to produce:

error: cannot invoke 'memcmp' with an argument list of type '([UInt8], [UInt8], UInt)'
    return memcmp(left, right, UInt(left.count)) == 0
           ^
note: expected an argument list of type '(UnsafePointer<Void>, UnsafePointer<Void>, Int)'

but now we produce:
error: cannot convert value of type 'UInt' to expected argument type 'Int'
    return memcmp(left, right, UInt(left.count)) == 0
                               ^~~~~~~~~~~~~~~~

which is more "to the point"



Swift SVN r30930
2015-08-02 05:51:26 +00:00
Chris Lattner
487e0c058b Fix the rest of <rdar://problem/20409366> Diagnostics for init calls should print the class name
Now for:
let req = NSURLRequest(URL: NSURL(string: "<some url>")!)?

instead of producing:
test.swift:2:58: error: could not find an overload for 'init' that accepts the supplied 

we produce the correct diagnostic, with a fixit:

error: cannot use optional chaining on non-optional value of type 'NSURLRequest'
let req = NSURLRequest(URL: NSURL(string: "<some url>")!)?
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

This also consolidates some existing diagnostics to improve their wording.



Swift SVN r30049
2015-07-10 01:11:15 +00:00
Chris Lattner
762cb3fa62 start loosening up getTypeOfIndependentSubExpression() to walk into more kinds of
expressions.  Broadening from callexpr to apply expr (picking up operators) improves
several diagnostics in the testsuite, and is important to avoid regressions from an 
upcoming patch.


Swift SVN r29821
2015-06-30 23:25:47 +00:00
Chris Lattner
e4b6afb9ae Start moving the testsuite to the "_ = foo()" idiom for evaluating an
expression but ignoring its value.  This is the right canonical way to do
this.  NFC, just testsuite changes.



Swift SVN r28638
2015-05-15 20:15:54 +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
Joe Pamer
f935a15460 Fix a typo in our bin application diagnostic. (rdar://problem/19422548)
Swift SVN r24613
2015-01-21 22:11:50 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +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
Joe Groff
c2dee83422 Add expected failures for <rdar://problem/17821399>.
We can't parse x!!y or x??y correctly as a binary expression without regressing type parsing, until we get operator splitting.

Swift SVN r20594
2014-07-26 20:16:47 +00:00
Joe Groff
0ea3dbc652 Lex '??' as an operator.
Change the lexing of '?' to be similar to '!', where we special-case the postfix case for the intrinsic postfix optional operator, but fall back to lexing as an operator when it isn't left-bound. For now, only accept '??' as an operator name--we could easily generalize this, but that warrants discussion first.

Swift SVN r20591
2014-07-26 19:48:42 +00:00
Chris Lattner
57cd2506ff Change "operator infix" to "infix operator" for consistency with the rest of the declaration
modifiers and with the func implementations of the operators.  This resolves the rest of:
<rdar://problem/17527000> change operator declarations from "operator prefix" to "prefix operator" & make operator a keyword




Swift SVN r19931
2014-07-14 16:39:10 +00:00
Chris Lattner
8991456ff2 Switch infix/postfix/prefix to be declaration modifiers instead of attributes,
eliminating the @'s from them when used on func's.  This is progress towards
<rdar://problem/17527000> change operator declarations from "operator prefix" to "prefix operator" & make operator a keyword

This also consolidates rejection of custom operator definitions into one
place and makes it consistent, and adds postfix "?" to the list of rejected
operators.

This also changes the demangler to demangle weak/inout/postfix and related things
without the @.



Swift SVN r19929
2014-07-14 15:51:49 +00:00
Ted Kremenek
fad874708e Adjust test cases.
Swift SVN r17964
2014-05-12 22:01:52 +00:00
Joe Groff
ac23437886 Replace infix attributes with operator decl lookup
During name binding, associate func decls with operator decls. When parsing SequenceExprs, look up operator decls to determine associativity and precedence of infix operators. Remove the infix_left and infix_left attributes, and make the infix attribute a simple declared attribute [infix] with no precedence.

Operator decls are resolved as follows:

- If an operator is declared in the same module as the use, resolve to the declaration in the current module.
- Otherwise, import operator declarations from all imported modules. If more than one declaration is imported for the operator and they conflict, raise an ambiguity error. If they are equivalent, pick one arbitrarily.

This allows operator declarations within the current module to override imported declarations if desired or to disambiguate conflicting operator declarations.

I've updated the standard library and the tests. stdlib2 and some of the examples still need to be updated.

Swift SVN r4629
2013-04-07 02:43:03 +00:00