Commit Graph

1015 Commits

Author SHA1 Message Date
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
Xi Ge
b37330d3b7 [Fixit] Add a fixit for converting non-trailing closures to trailing closures.
Reminded by Ben's last commit of code completion, I noticed that some users
do not use trailing closures when helping migrating their code. To enhance the discoverability
of this feature, this fix helps users to convert non-trailing closures to trailing
closures. To ensure disambiguity after conversion, we check name conflict before adding
the note.
2016-07-01 19:44:56 -07:00
Rintaro Ishizaki
ba2e4e144f [Fix-it] In fixItAvailableAttrRename, handle the case args is TupleShuffleExpr (#3040)
Also, handle the ParenExpr is trailing closure case.

Fix-it used to add `fn: ` label before trailing closure:

   // (tuple_shuffle_expr (paren_expr trailing-closure)) case:
   @available(*, unavailable, renamed: "bar(fn:)")
   func foo(closure: () -> Int) {}
   foo { 0 }

   // (paren_expr trailing-closure) case:
   @available(*, unavailable, renamed: "bar(fn:)")
   func foo(_: () -> Int) {}
   foo { 0 }

   // (tuple_shuffle_expr (tuple_expr trailing-closure)) case:
   @available(*, unavailable, renamed: "bar(x:fn:)")
   func oldFunc6(_ x: Int, y: @noescape () -> Int) { }
   foo(0) { 1 }
2016-07-01 12:58:34 +09:00
Ben Langmuir
ea848aeaae Rename C++ macro 'defer' -> 'SWIFT_DEFER'
In C++ we can't have nice things. The macro name 'defer' collided with
use of 'defer' in the Tokens.def file and we were already doing horrible
workarounds in a couple of places to allow them to be included into the
same file. So use a less awesome but more robust name (thanks to Joe for
suggesting SWIFT_DEFER).

Incidentally, sort a bunch of #inlcudes.
2016-06-29 14:57:58 -07:00
Xi Ge
c994a3b36c Specify the underlying type of enum class. 2016-06-21 10:48:04 -07:00
Xi Ge
286b18fb07 Address Jordan's code review comments. 2016-06-21 10:43:35 -07:00
Xi Ge
7beb451993 [fixit] Provide a fixit when c-style loop is counting down. 2016-06-20 17:38:05 -07:00
Doug Gregor
2807a17eb8 [Type checker SR-899] Warning on missing ".self" for single-parameter functions.
Due to a modeling error in the type checker's folding of type
references into type expressions, code such as "strideof(Int)" would
be accepted without the required ".self". Commit
4a60b6cbf4 fixes the modeling issue but
left the historical accepts-invalid; now, diagnose these cases with a
warning + Fix-It to ease the transition.

Fixes SR-899.
2016-06-12 22:31:59 -07:00
Doug Gregor
01682af23a Revert "[Type checker] Be more rigorous about extracting argument labels from calls."
This reverts commit 93dac8f759.
2016-06-03 16:29:31 -07:00
Doug Gregor
93dac8f759 [Type checker] Be more rigorous about extracting argument labels from calls.
Whenever we have a call, retrieve the argument labels from the
argument structurally and associate them with the callee. We were
previously doing this as a separate AST walk (which was unnecessary),
so fold that into constraint generation for a CallExpr.

This is a slightly-pared-back version of
3753d779bc that isn't so rigid in its
interpretation of ASTs. I'll tighten up the semantics over time.
2016-06-03 14:45:21 -07:00