Commit Graph

974 Commits

Author SHA1 Message Date
Doug Gregor
50341da32b Use "TypeBase::hasError()" rather than "is<ErrorType>()" where needed.
In most places where we were checking "is<ErrorType>()", we now mean
"any error occurred". The few exceptions are in associated type
inference, code completion, and expression diagnostics, where we might
still work with partial errors.
2016-10-07 10:58:23 -07:00
Robert Widmann
024349788e General bike shedding 2016-10-04 10:09:56 -04:00
Robert Widmann
43a582ef0f Initial implementation of optionals-in-string-interpolation warnings
Basic extension of the optional-to-any AST walker to incorporate
warnings for the as-of-now up and coming Swift evolution proposal.
2016-10-03 17:08:11 -04:00
Rintaro Ishizaki
65f3ba5dbb [Sema] Unify fix-it logic for enclosing trailing closure in argument parens (#4961)
`tryDiagnoseTrailingClosureAmbiguity` and `checkStmtConditionTrailingClosure`
had similar logic.
Added swift::fixItEncloseTrailingClosure function.
2016-09-24 18:33:40 +09:00
Jordan Rose
d89c232f59 Add empty parens to var-to-function renames, take two. (#4938)
This time, propagate the decl marked deprecated or unavailable through
to the fix-it, so we can be sure it's a var.

https://bugs.swift.org/browse/SR-1649
2016-09-23 14:56:08 -07:00
Slava Pestov
a9c68c0736 AST: Remove archetype from AbstractTypeParamDecl
There's a bit of a hack to deal with generic typealiases, but
overall this makes things more logical.

This is the last big refactoring before we can allow constrained
extensions to make generic parameters concrete. All that remains
is a small set of changes to SIL type lowering, and retooling
some diagnostics in Sema.
2016-09-22 19:48:30 -07:00
Jordan Rose
ff4f2ce47f Revert "Add empty parens to var-to-function renames" (#4930)
"!call" doesn't imply "variable", so the fix-it isn't always correct.

This reverts commit a99ca851df (#3947).
2016-09-22 16:12:23 -07:00
Jordan Rose
caeed32302 Add a fix-it for missing generic parameters on construction.
For example, if someone tries to use the newly-generic type Cache,
from Foundation:

  var cache = Cache()

they'll now get a fix-it to substitute the default generic parameters:

  var cache = Cache<AnyObject, AnyObject>()

The rules for choosing this placeholder type are based on constraints
and won't be right 100% of the time, but they should be reasonable.
(In particular, constraints on associated types are ignored.)
In cases where there's no one concrete type that will work, an Xcode-
style placeholder is inserted instead.

- An unconstrained generic parameter defaults to 'Any'.
- A superclass-constrained parameter defaults to that class,
  e.g. 'UIView'.
- A parameter constrained to a single @objc protocol (or to AnyObject)
  defaults to that protocol, e.g. 'NSCoding'.
- Anything else gets a placeholder using the generic parameter's name
  and protocol composition syntax.

rdar://problem/27087345
2016-09-21 18:04:14 -06:00
Mark Lacey
d8135798eb Improvements to optional-to-Any coercion warning.
Don't explicitly desguar types when it is not needed and/or results in
worse types displayed in diagnostics.

Tweak the warning messages to use "this warning" rather than "the
warning".

Addresses feedback from Jordan on commit 401ca24532.
2016-09-21 13:27:52 -07:00
Mark Lacey
401ca24532 Emit a warning when optionals are coerced to Any.
Emit a warning for optionals that are implicitly converted to Any, and
add fixits giving options to:
- Add '??' with a default value after
- Force-unwrap the optional with '!'
- Explicitly cast to 'as Any' to silence the warning

This covers diagnostics aspect of SE-0140.

rdar://problem/28196843
2016-09-20 22:33:45 -07:00
practicalswift
b19481f887 [gardening] Fix 67 recently introduced typos 2016-09-16 11:16:07 +02:00
Jordan Rose
9e79e0b362 Provide a fix-it when overriding 'Any' with 'AnyObject'. (#4729)
This helps when manually migrating Swift 2 code to Swift 3, which
includes SE-0116 (id-as-Any). We already did this for specific bridged
types (like URL and NSURL); this just adds a special case for
Any/AnyObject.

rdar://problem/27865590
2016-09-12 17:19:53 -07:00
Doug Gregor
68c3f3b1b3 Remove EnableSwift3Private staging option. 2016-08-19 21:53:32 -07:00
Doug Gregor
51529ae888 Eliminate the -enable-id-as-any flag; it's always on now anyway.
Simplify e.g., ASTContext::getBridgedToObjC(), which no longer needs
the optional return.

Eliminate the now-unused constraint kind for checking bridging to
Objective-C.
2016-08-19 21:17:09 -07:00
Doug Gregor
fe7e780559 Remove -Womit-needless-words; it was a hack to bring up the Grand Renaming.
I'm slightly sad about losing the lovely code to detect
redundantly-specified defaulted arguments, but we could bring it back
later as a separate warning.
2016-08-19 14:19:51 -07:00
Harlan Haskins
96ff4528c4 Special-case constructor calls in MiscDiagnostics. 2016-08-13 09:37:26 -07:00
Xi Ge
a2b837300a Address @jrose-apple's code review comments. (#4278)
* Address Jordan's code review comments

* [test] Add test case for generic function types.
2016-08-12 19:45:33 -07:00
Xi Ge
b58c3a2fb7 [FixCode] Add @escaping when overriding mismatch is because of it. (#4273)
* [FixCode] Add @escaping when overriding mismatch is because of it. rdar://27814862

With the change of default escaping behavior, users' existing code overriding
objc functions may need to add @escaping to make the overriding match as before. This
patch checks if an overriding mismatch is due to the lacking of @escaping and add
it as a fixit.

* [test] Update existing test. NFC
2016-08-12 15:43:48 -07:00
Dave Abrahams
e6dec58cc5 Merge pull request #4041 from xwu/memory-layout-of-value
[stdlib] Implement SE-0136 (Restore MemoryLayout.*(ofValue:))
2016-08-12 11:09:17 -07:00
Jordan Rose
f65ad810df Check protocol witnesses using access scopes. (#4176)
...rather than relying on the access-as-spelled, which may be greater
than the effective access due to parent scopes.

(Some of this will get cleaned up with SR-2209.)

rdar://problem/27663492
2016-08-11 14:30:09 -07:00
Jordan Rose
38e6b2808e Do even less availability checking under -disable-availability-checking. (#4214)
...and make sure we're in that mode for SIL inputs and for sil-opt and
sil-extract, even when working with AST types and declarations rather
than SIL types.

Without this, we get zillions of deprecation warnings coming out of
the validation tests SIL/parse_stdlib_*.sil, which dump the standard
library and then attempt to re-parse it. This has been causing the
"long" tests to take, well, too long.
2016-08-11 10:19:46 -07:00
Robert Widmann
4b41a7fdee Merge pull request #3947 from harlanhaskins/you-know-claremont-street
Add empty parens to var-to-function renames
2016-08-09 13:56:04 -07:00
Xiaodi Wu
f9435b9ce8 [stdlib] Restore MemoryLayout.*(ofValue:) 2016-08-08 10:46:37 -05:00
Doug Gregor
22287ddb58 [Type system] Infer 'Any' for array elements and dictionary values and 'AnyHashable' for dictionary keys.
The id-as-Any work regressed cases where Swift code could specify
heterogeneous collection literals, e.g.,

    var states: [String: Any] = [
      "California": [
        "population": 37_000_000,
        "cities": ["Los Angeles", "San Diego", "San Jose"],
      ],
      "Oregon": [
        "population": 4_000_000,
        "cities": ["Portland", "Salem", "Eugene"],
      ]
    ]

Prior to this, the code worked (when Foundation was imported) because
we'd end up with literals of type [NSObject : AnyObject].

The new defaulting rule says that the element type of an array literal
and the key/value types of a dictionary literal can be defaulted if no
stronger type can be inferred. The default type is:

  Any, for the element type of an array literal or the value type of a
  dictionary literal, or

  AnyHashable, for the key type of a dictionary literal.

The latter is intended to compose with implicit conversions to
AnyHashable, so the most-general inferred dictionary type is
[AnyHashable : Any] and will work for any plausible dictionary
literal.

To prevent this inference from diluting types too greatly, we don't
allow this inference in "top-level" expressions, e.g.,

  let d = ["a" : 1, "b" : "two"]

will produce an error because it's a heterogeneous dictionary literal
at the top level. One should annotate this with, e.g.,

  let d = ["a" : 1, "b" : "two"] as [String : Any]

However, we do permit heterogeneous collections in nested positions,
to support cases like the original motivating example.

Fixes rdar://problem/27661580.
2016-08-04 20:58:13 -07:00
Harlan Haskins
a99ca851df Add empty parens to var-to-function renames 2016-08-04 11:03:36 -07:00
Rintaro Ishizaki
2f6b6d2c67 [Fix-it] Support defaulted and variadic arguments in renamed fix-it 2016-08-05 02:05:42 +09:00
Chris Lattner
1d21b4e4e1 Add a bit and some diagnostics logic to help with the ongoing id-as-any project.
This is NFC because nothing sets the bit yet, it is just to aid an ongoing
investigation.
2016-08-03 16:45:01 -07:00
John McCall
29ccaafc81 Merge pull request #3882 from apple/WIP-SE-0117
Implement SE-0117 (DON'T MERGE)
2016-08-02 08:56:02 -07:00
Chris Lattner
300462ea45 fix <rdar://problem/27650521> Fixits for escaping closure parameters are inserted into wrong location 2016-08-02 08:48:57 -07:00
John McCall
afdda3d107 Implement SE-0117.
One minor revision: this lifts the proposed restriction against
overriding a non-open method with an open one.  On reflection,
that was inconsistent with the existing rule permitting non-public
methods to be overridden with public ones.  The restriction on
subclassing a non-open class with an open class remains, and is
in fact consistent with the existing access rule.
2016-08-02 07:46:38 -07:00
Rintaro Ishizaki
fd4ddd6322 [SE-0101] Migration support for sizeof(T) family to MemoryLayout<T> 2016-07-30 18:24:13 +09:00
Robert Widmann
80fb5c1c5a Remove usage of getMetatypeLoc() 2016-07-29 16:59:14 -07:00
Michael Ilseman
2bb3347b65 [noescape by default] Improve and introduce diagnostics
Changes diangostic messages from referring specifically to @noescape,
which is going away, to 'non-escaping'. Introduces better diagnostics
and fixits for adding @escaping to closures that escape.
2016-07-29 13:48:07 -07:00
Doug Gregor
e4d8f486a8 Simplify AST for string literals to not depend on implicit tuple splat.
String literal expressions, as well as the magic literals #file and
tuple value that is then fed into one or two call expressions. For
string literals, that tuple value was implicitly splatted, breaking
AST invariants.

Instead, keep string literals and these magic literals that produce a
string as a single expression node, but store the declarations that
will be used to transform the raw literal into the complete
literal. SILGen will form the appropriate calls. This representation
is far simpler---the AST no longer has a bunch of implicit nodes---and
doesn't break AST invariants.
2016-07-27 12:30:22 -07:00
Jordan Rose
508e825ff2 Split 'fileprivate' and 'private', but give them the same behavior.
'fileprivate' is considered a broader level of access than 'private',
but for now both of them are still available to the entire file. This
is intended as a migration aid.

One interesting fallout of the "access scope" model described in
758cf64 is that something declared 'private' at file scope is actually
treated as 'fileprivate' for diagnostic purposes. This is something
we can fix later, once the full model is in place. (It's not really
/wrong/ in that they have identical behavior, but diagnostics still
shouldn't refer to a type explicitly declared 'private' as
'fileprivate'.)

As a note, ValueDecl::getEffectiveAccess will always return 'FilePrivate'
rather than 'Private'; for purposes of optimization and code generation,
we should never try to distinguish these two cases.

This should have essentially no effect on code that's /not/ using
'fileprivate' other than altered diagnostics.

Progress on SE-0025 ('fileprivate' and 'private')
2016-07-25 13:13:35 -07:00
Slava Pestov
e5f1d73f97 AST: Remove FuncDecl::getNaturalArgumentCount(), NFC 2016-07-24 00:15:34 -07:00
Chris Lattner
0ad1ff7126 Generalize a condition to allow _OptionalNilComparisonType. We now diagnose all of
the cases the hack Mark reverted used to (and more).
2016-07-23 17:44:19 -07:00
Chris Lattner
842d6777d9 enhance a few diagnostics to include the non-optional type name. 2016-07-23 17:30:03 -07:00
Chris Lattner
669f40aa00 Produce warnings for implicit optional promotions involving !== and ===, and for
pattern matches.  In the case of an 'if let' with an explicit type, produce a
Taylor'd diagnostic that rewrites the condition to the right type.

This wraps up:
<rdar://problem/27457457> [Type checker] Diagnose unsavory optional injections
2016-07-23 17:06:54 -07:00
Chris Lattner
d138290448 Produce warnings when implicit optional promotions are introduced in some
common standard library operators.  This is progress towards:
<rdar://problem/27457457> [Type checker] Diagnose unsavory optional injections

but there is more work to be done here.
2016-07-23 16:03:41 -07:00
Chris Lattner
8e2597b48c Introduce a helper function, NFC. 2016-07-23 15:14:30 -07:00
Robert Widmann
f97e5dcb0e [SE-0115][1/2] Rename *LiteralConvertible protocols to ExpressibleBy*Literal. This
change includes both the necessary protocol updates and the deprecation
warnings
suitable for migration.  A future patch will remove the renamings and
make this
a hard error.
2016-07-12 15:25:24 -07:00
Robert Widmann
2a5c7ddf90 Quick fix for off-by-count errors in pattern binding diagnostics
Rather than return the last var pattern, only match on the ones that
match the vardecl the diagnostic is warning about.
2016-07-10 22:32:08 -07:00
Dmitri Gribenko
1f63d98e1d Merge pull request #3431 from practicalswift/minor-syntax-fixes
[gardening] Minor syntax cleanups.
2016-07-09 20:47:57 -07:00
practicalswift
808b539f10 [gardening] Minor syntax cleanups. 2016-07-09 13:27:57 +02:00
practicalswift
fcd470c111 [gardening] Fix recently introduced typos. 2016-07-09 11:45:37 +02:00
Rintaro Ishizaki
4bf1c34f80 [Parse/Sema][SR-1672] Improve diagnostics for trailing closures in stmt-condition (#3184)
Fix-it suggests normal argument expression, instead of of enclosing whole
expression with parens.

* Moved diagnostic logic to Sema, because we have to add correct argument
  label for the closure.

    if arr.starts(with: IDs) { $0.id == $2 } { ... }
                           ~~^
                           , isEquivalent:  )

* We now accept trailing closures for each expressions and right before `where`
  clause, as well as closures right before the body.

    if let _ = maybeInt { 1 }, someOtherCondition { ... }
                       ~^
                       (     )

    for let x in arr.map { $0 * 4 } where x != 0 { ... }
                        ~^
                        (          )
2016-07-09 12:51:51 +09:00
Xi Ge
1e85e1bcd2 Revert "[Fixit] Add a fixit for converting non-trailing closures to trailing closures (#3317)"
This patch needs some polish to fix more false positives found by @rintaro and @lattner
2016-07-02 09:39:07 -07:00
Xi Ge
1886b4ab56 [Fixit] Add a fixit for converting non-trailing closures to trailing closures (#3317)
* [Fixit] Add a fixit for converting non-trailing closures to trailing closures.

* [test] Update test to reflect the added note about converting to trailing closures.
2016-07-02 08:11:58 -07:00
Michael Gottesman
95d4cbf962 Revert "[Fixit] Add a fixit for converting non-trailing closures to trailing closures."
This reverts commit b37330d3b7.

It broke the build.
2016-07-01 20:19:10 -07:00