Commit Graph

7976 Commits

Author SHA1 Message Date
Doug Gregor
d9dd8f735c Merge pull request #4683 from harlanhaskins/var-you-sure-about-that
Fix var-parameter detection for inferred params
2016-09-23 14:40:10 -07:00
Jordan Rose
3170c65c3d Merge pull request #4925 from jrose-apple/ignore-access-when-you-find-nothing
Add "IgnoreAccessControl" to UnqualifiedLookup.

rdar://problem/27663403
2016-09-23 11:04:56 -07:00
Harlan Haskins
751f0e8773 Fix var-parameter detection for inferred params
Previously, we would skip validating the type (and checking var/inout)
on parameters that didn't have an explicit type. Instead, teach
validateParameterType how to handle an elided type.
2016-09-23 13:08:57 -04:00
Slava Pestov
e97df4a285 Sema: Implicit conversion for single-expression closures of Never type
This fixes a usability regression with the removal of @noreturn
in Swift 3. Previously, it was legal to write this:

let callback: () -> Int = { fatalError() }

Now that the special @noreturn attribute has been replaced with
a Never type, the above fails to typecheck, because the expression
now has type 'Never', and we expect a value of type 'Int'.

Getting around this behavior requires ugly workarounds to force the
parser to treat the body as a statement rather than an expression;
for example,

let callback: () -> Int = { _ = (); fatalError() }

This patch generalized single-expression closures to allow
the 'Never to T' conversion. Note that this is rather narrow
in scope -- it only applies to closure *literals*, single-expression
ones at that, not arbitrary function *values*.

In fact, it is not really a conversion at all, but more of a
desugaring rule for single-expression closures. They can now be
summarized as follows:

- If the closure literal has contextual return type T and
  the expression has Never type, the closure desugars as
  { _ = <expr> }, with no ReturnStmt.

- If the closure literal has contextual return type T for some
  non-void type T, the closure desugars as { return <expr> };
  the expression type must be convertible to T.

- If the closure literal has contextual return type Void, and
  the expression has some non-Void type T, the closure
  desugars as { _ = <expr>; return () }.

Fixes <rdar://problem/28269358> and <https://bugs.swift.org/browse/SR-2661>.
2016-09-22 23:40:25 -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
swift-ci
675e542f3b Merge pull request #4939 from rudkx/fix-27830834 2016-09-22 18:02:09 -07:00
Jordan Rose
b9e1d4c21c Don't use construction to convert literals in rawValue fix-it. (#4934)
Input:  panel.styleMask = 8345
Old output:  panel.styleMask = NSWindowStyleMask(rawValue: UInt(8345))
New output:  panel.styleMask = NSWindowStyleMask(rawValue: 8345)

rdar://problem/26681232
2016-09-22 17:36:41 -07:00
Michael Ilseman
3619738965 Merge pull request #4905 from milseman/escaping_printing_fix_clean
Fix @escaping printing
2016-09-22 17:24:44 -07:00
Mark Lacey
b44a93ea84 Fix crash on typecheck failure in interpolated strings.
We were attempting to clean up stray type variables before creating a
new constraint system and moving forward with narrowing down the
typecheck failure, but we were failing to remove type variables for
interpolated strings.

Fix that, as well as removing them from TypeExpr's, and add an assert
that on exit from the pre-order visitor we don't have any type
variables (except for literals that aren't interpolated strings, which
I'm going to dig into further).

Fixes rdar://problem/27830834 and SR-2716.
2016-09-22 17:09:31 -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
b76dea8cbb Recover from unqualified value lookup the same way as type lookup.
Use the flag added in the previous commit to look for inaccessible
values when an (unqualified) DeclRefExpr can't be resolved.

Finishes rdar://problem/27663403.
2016-09-22 15:09:28 -07:00
Jordan Rose
9eaaf5d3b8 Add "IgnoreAccessControl" to UnqualifiedLookup.
...and use it when top-level type lookup fails, to produce better
diagnostics.

Really this should be using an option set, or setting flags on the
UnqualifiedLookup instance or something, but I want to cherry-pick
this to the swift-3.0-branch without major disturbances.

More rdar://problem/27663403
2016-09-22 15:09:27 -07:00
Michael Ilseman
4324d8f4b0 [CSDiag] Switch to using the ASPrinter rather than repeating logic 2016-09-22 13:19:22 -07:00
Michael Ilseman
0bb868a539 [ParameterTypeFlags] Incorporate review feedback 2016-09-22 12:24:37 -07:00
Michael Ilseman
ed2522b384 [AST] Create ParameterTypeFlags and put them on function params
Long term, we want to refactor the AST to reflect the current
programming model in Swift. This would include refactoring
FunctionType to take a list of ParameterTypeElt, or something with a
better name, that can contain both the type and flags/bits that are
only specific to types in parameter position, such as @autoclosure and
@escaping. At the same time, noescape-by-default has severely hurt our
ability to print types without significant context, as we either have
to choose to too aggressively print @escaping or not print it in every
situation it occurs, or both.

As a gentle step towards the final solution, without uprooting our
overall AST structure, and as a way towards fixing the @escaping
printing ails, put these bits on the TupleTypeElt and ParenType, which
will serve as a model for what ParameterTypeElt will be like in the
future. Re-use these flags on CallArgParam, to leverage shared
knowledge in the type system. It is a little painful to tack onto
these types, but it's minor and will be overhauled soon, which will
eventually result in size savings and less complexity overall.

This includes all the constraint system adjustments to make these
types work and influence type equality and overload resolution as
desired. They are encoded in the module format. Additional tests
added.
2016-09-22 12:24:02 -07:00
Michael Ilseman
81b0aa7339 [Cleanup] Drop needless TupleTypeElt constructor calls
Now that TupleTypeElts are simpler in Swift 3 (though they're about to
become more complicated for other reasons), most of the cases where we
are explicitly constructing ones are really just plain copies or can
otherwise use existing helper functions.

NFC
2016-09-22 12:24:02 -07:00
Mark Lacey
71dfd99eaf Remove an assert and instead return nullptr.
We were asserting in ExprRewriter::visitCoerceExpr() that the conversion
we attempt is successfully. However, we have known cases where we will
emit a diagnostic and fail attempting to convert, so we should really
just bail out at this point rather than asserting.

Resovles the last part of rdar://problem/28207648.
2016-09-22 11:38:13 -07:00
Joe Groff
b0e3c0be59 Merge pull request #4865 from jckarter/nsvalue-bridging
SE-0139: NSValue bridging
2016-09-22 09:13:47 -07:00
swift-ci
bb660c4967 Merge pull request #4915 from DougGregor/file-literal-crash-26586984 2016-09-21 23:43:14 -07:00
Doug Gregor
0283be9492 Merge pull request #4661 from jtbandes/diag-fixity
[QoI] diagnose operator fixity attrs together; improve messages
2016-09-21 23:23:21 -07:00
swift-ci
c1c9f4ddbe Merge pull request #4914 from DougGregor/keypath-crash-28061409 2016-09-21 23:10:59 -07:00
Doug Gregor
e5b9ad7f64 [Type checker] Don't overflow array bounds with #fileLiteral *ahem*.
Fixes rdar://problem/26586984.
2016-09-21 23:08:58 -07:00
Doug Gregor
d225a2177d [Type checker] Use proper lookup type in substitutions for #keyPath.
Fixes crasher in rdar://problem/28061409.
2016-09-21 22:40:58 -07:00
Joe Groff
86fbeee285 SE-0139: Bridge Cocoa framework structs to NSValue.
For every struct type for which the frameworks provides an NSValue category for boxing and unboxing values of that type, provide an _ObjectiveCBridgeable conformance in the Swift overlay that bridges that struct to NSValue, allowing the structs to be used naturally with id-as-Any APIs and Cocoa container classes. This is mostly a matter of gyb-ing out boilerplate using `NSValue.init(bytes:objCType:)` to construct the instance, `NSValue.objCType` to check its type when casting, and `NSValue.getValue(_:)` to extract the unboxed value, though there are a number of special snowflake cases that need special accommodation:

- To maintain proper layering, CoreGraphics structs need to be bridged in the Foundation overlay.
- AVFoundation provides the NSValue boxing categories for structs owned by CoreMedia, but it does so using its own internal subclasses of NSValue, and these subclasses do not interop properly with the standard `NSValue` subclasses instantiated by Foundation. To do the right thing, we therefore have to let AVFoundation provide the bridging implementation for the CoreMedia types, and we have to use its category methods to do so.
- SceneKit provides NSValue categories to box and unbox SCNVector3, SCNVector4, and SCNMatrix4; however, the methods it provides do so in an unusual way. SCNVector3 and SCNVector4 are packaged into `CGRect`s and then the CGRect is boxed using `valueWithCGRect:`. SCNMatrix4 is copied into a CATransform3D, which is then boxed using `valueWithCATransform3D:` from CoreAnimation. To be consistent with what SceneKit does, use its category methods for these types as well, and when casting, check the type against the type encoding SceneKit uses rather than the type encoding of the expected type.
2016-09-21 19:26:10 -07:00
Jordan Rose
821cdf368d Merge pull request #4910 from jrose-apple/fix-it-swift_newtype-rawValue
Use the existing '.rawValue' fix-it to handle unwrapping objects too.

rdar://problem/26678862
2016-09-21 18:28:40 -07:00
Jordan Rose
693fd9d455 Use the existing '.rawValue' fix-it to handle unwrapping objects too.
We could probably make this even more general, but this specifically
avoids trying to insert an "as AnyObject" fix-it, which will now work
but probably produce an incorrect result. This comes up when working
with swift_newtype wrappers of CF types being passed to CFTypeRef
parameters.

rdar://problem/26678862
2016-09-21 17:16:26 -07:00
Jordan Rose
1d7ad9e38a Better generic fix-its for types as well as construction and casts.
Slava pointed out that we have an existing version of the code from
the previous commit that's only used when checking types. Replace it
with the new code, which handles more cases.
2016-09-21 18:04:14 -06: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
Jordan Rose
6bc61fe8ce Fix a fix-it to cast to the destination type, not the source type.
Copy/paste issue, most likely.
2016-09-21 16:40:40 -07:00
Jordan Rose
f21cf8b014 Merge pull request #4904 from jrose-apple/fix-private-access-checking
Remove incorrect early exit from type access checking.

https://bugs.swift.org/browse/SR-2579
2016-09-21 15:50:03 -07: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
Jordan Rose
b7e4746f85 Downgrade type access checks to warnings in certain cases.
...to be compatible with Swift 3. Fortunately these cases are all safe;
they're the cases that would all be 'fileprivate' in Swift 2.

Finishes https://bugs.swift.org/browse/SR-2579, although we'll need a
follow-up bug to turn this /back/ into an error in Swift 4.
2016-09-21 11:34:45 -07:00
Jordan Rose
eb1d127a9e Remove incorrect early exit from type access checking.
This was a correct compile-time optimization for Swift 2, but Swift 3
has multiple levels of "private" that need to be taken into account.

This is a purely /subtractive/ change, so the next commit will
downgrade this to a warning in cases where it would have been accepted
in 3.0GM.

https://bugs.swift.org/browse/SR-2579
2016-09-21 11:33:50 -07:00
swift-ci
c3b7709a7c Merge pull request #4897 from rudkx/rdar28196843 2016-09-20 23:10:11 -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
Jacob Bandes-Storch
682ab47c2d [QoI] diagnose operator fixity attrs together; improve messages
Previously, `infix` was not recognized as conflicting with `prefix` and `postfix`. We now offer to remove all but the first fixity attribute.
2016-09-20 20:54:07 -07:00
swift-ci
ca59a400ff Merge pull request #4874 from DougGregor/sr-993 2016-09-19 20:37:49 -07:00
Doug Gregor
81661262c8 [Type checker] Don't infer 'dynamic' for 'let' variables or within final classes.
The 'dynamic' inference would infer 'dynamic' in places where it would
also infer 'final', leading to a diagnostic

  error: a declaration cannot be both 'final' and 'dynamic'

with no source location information. Don't infer 'dynamic' in such
places. Fixes SR-993 / rdar://problem/20449627.
2016-09-19 20:07:24 -07:00
Slava Pestov
ced715cb46 Sema: Fix crash when attempting to diagnose a contextual conversion of an ambiguous expression
The fixits call back into the type checker via typeCheckCheckedCast(),
which sets up a new constraint system. As a result we would hit
assertions by introducing type variables from a previous "generation".

It seems that if we bail out of this code path altogether, we get a
better diagnostic -- in the provided test, it complains about an
ambiguous member to '.value', rather than not being able to convert
_? to V?.

Fixes <https://bugs.swift.org/browse/SR-2592>.
2016-09-19 17:53:05 -07:00
Mark Lacey
126b9fcb2a Do not recurse infinitely when type checking constructor parameters.
Fully-qualified references to associated types in parameter lists of
constructors could result in infinite recursion and crash the compiler
when the typealias for the associated type is not defined.

Use the same approach used in normal function parameter lists of setting
IsBeingTypeChecked on the enclosing type to avoid going into an infinite
recursion here.

Resolves rdar://problem/27680407.
2016-09-19 14:55:25 -07:00
practicalswift
8d6251de66 [gardening] Fix accidental uses of \t 2016-09-17 13:15:26 +02:00
practicalswift
7c63623ffc [gardening] Fix 13 recently introduced typos. 2016-09-16 20:30:57 +02:00
practicalswift
f250a2349b [gardening] Remove duplicate words. 2016-09-16 20:09:34 +02:00
practicalswift
b19481f887 [gardening] Fix 67 recently introduced typos 2016-09-16 11:16:07 +02:00
Slava Pestov
a1eef126ba AST: Don't print "aka <<desugared type>>" for generic function types
This was causing us to emit diagnostics talking about τ_m_n, which is
not helpful.

Now that generic function types print sanely, print them in a few
places where we were previously printing PolymorphicFunctionTypes.
2016-09-15 21:47:57 -07:00
Slava Pestov
90d6e8dce8 Sema: Variadic parameters are always @escaping and cannot be @autoclosure
A variadic parameter of function type must be @escaping -- we cannot
reason about an array of non-escaping closures, so this was a safety
hole.

Also, attempting to define an @autoclosure variadic did not produce a
diagnostic, but would fail later on if you actually tried to do
anything with it. Let's ban this completely.

Both changes are source breaking, but impact is limited to code that
was already only marginally valid.
2016-09-15 21:46:02 -07:00
Doug Gregor
dd7ae44f58 Merge pull request #4770 from DougGregor/scope-map-fixes
[Scope map] Miscellaneous improvements (and a hack) for unqualified name lookup
2016-09-15 14:30:15 -07:00
Jordan Rose
81284f5756 Merge pull request #4777 from jrose-apple/fix-it-trailing-closure-ambiguity
Offer fix-its to disambiguate based on a trailing closure's label.
2016-09-15 13:03:47 -07:00
swift-ci
40483d470a Merge pull request #4767 from practicalswift/inconsistent-headers 2016-09-15 12:14:44 -07:00
Jordan Rose
e6d6e0e92f Offer fix-its to disambiguate based on a trailing closure's label.
(by making it a normal argument with a label and not a trailing
closure)

Diagnostic part of rdar://problem/25607552. A later commit will keep
us from getting in this situation quite so much when default arguments
are involved.
2016-09-15 11:05:02 -07:00