Commit Graph

178 Commits

Author SHA1 Message Date
Slava Pestov
cd1b0d100f ClangImporter: Support for subclass existentials
In Swift 3, an Objective-C type like SomeClass <SomeProtocol> is
imported as SomeClass. The protocol qualification is erased unless
the class bound is 'id' or 'Class'.

Importing such types as class-constrained existentials is a source
breaking change, so the new behavior is only enabled in Swift 4
mode.

Furthermore as a transitional step the staging flag
-enable-experimental-subclass-existentials has to be passed in
also. The flag will soon be removed.
2017-04-19 00:52:14 -07:00
Michael Gottesman
ba5be9d8e8 [silgen] Eliminate an unnecessary writeback scope.
This writeback scope's writeback is handled by the ArgumentScope for argument
emission. Since the scope will be destroyed after argument scope, we get
mismatched scope depths.

rdar://31313534
2017-03-30 14:51:53 -07:00
Jordan Rose
9fcfd0a45a Merge pull request #7201 from KingOfBrian/bugfix/SR-3665
Fix @discardableResult when invoking function with explicit namespace
2017-02-06 11:39:21 -08:00
Brian King
e4ca85f417 Fix test failures from a change in warning strings 2017-02-02 11:54:36 -05:00
Jordan Rose
3411fc380e [ClangImporter] Prefer available enum elements over unavailable ones.
...and avoid making aliases from one unavailable declaration to another.
If it's unavailable, we can just import it as a normal case and not
worry about it. This fixes an issue where Sema would try to diagnose
the body of an "alias" for referring to unavailable declarations.

(Background: enum cases in Swift have to have unique values, so we
import any duplicate values as static properties. Pattern matching
logic has a hack to recognize these particular static properties as
being "case-like".)

This commit also sinks enum element uniqueness checking into importing
the enum, instead of keeping a global map we never consult again. This
should save a small bit of memory.

rdar://problem/30025723
2017-01-23 15:34:20 -08:00
Michael Ilseman
d1efc80b1f [Import Decl] Don’t import as init using omit needless words
Previously, for an Objective-C class method declaration that could be
imported as init, we were making 4 decls:

1) The Swift 2 init
2) The Swift 2 class method decl (suppressing init formation)
3) The Swift 3 init (omitting needless words)
4) The Swift 3 class method decl (suppressing init formation and
   omitting needless words)

Decls 1), 2), and 4) exist for diagnostics and redirect the user at
3). But, 4) does not correspond to any actual Swift version name and
producing it correctly would require the user to understand how
omit-needless-words and other importer magic operates. It provides
very limited value and more importantly gets in the way of future
Clang importer refactoring. We’d like to turn Decl importing into
something that is simpler and language-version parameterized, but
there is no real Swift version to correspond to decl 4).

Therefore we will be making the following decls:

1) The "raw" decl, the name as it would appear to the user if they
   copy-pasted Objective-C code
2) The name as it appeared in Swift 2 (which could be an init)
3) The name as it appeared in Swift 3 (which could be an init and omit
   needless words)

This aligns with the language versions we want to import as in the
future: raw, swift2, swift3, …, and current.

Note that swift-ide-test prunes decls that are unavailable in the
current Swift version, so the Swift 2 non-init decls are not printed
out, though they are still present. Tests were updated and expanded to
ensure this was still the case.
2016-12-01 18:50:40 -08:00
Jordan Rose
6163679538 [APINotes] Replace old FactoryAsInit annotation with equivalent SwiftNames.
'processInfo' was removed entirely because it's now considered a class property,
and we don't ever import those as initializers.
2016-10-14 09:07:43 -07:00
Doug Gregor
31edf710bf [Sema] Improve diagnostics for attempt to throw an error *code*
Attempting to throw an error code value, e.g.,

  throw CocoaError.fileNoSuchFileError

is now ill-formed, although it was well-formed prior to the
introduction of NSError bridging (SE-0112). Provide a specialized
diagnostic with a Fix-It to add the appropriate parentheses:

  throw CocoaError(.fileNoSuchFileError)

Fixes rdar://problem/27543121.
2016-08-02 14:47:18 -07:00
Jordan Rose
b5aca663bc [ClangImporter] Remove importer-based NS stripping. (#3880)
* [ClangImporter] Remove importer-based NS stripping.

As Tony puts it, in the end we wound up with more Foundation
declarations imported as members or keeping "NS" than those that
dropped it, and any further decisions will be made on a case-by-case
basis. Move all of the existing cases of prefix-stripping into
Foundation's API notes and drop the logic from the compiler.

Tested by dumping the generated interface for Foundation and its
submodules for both macOS and the iOS simulator, and comparing the
results. A few cases did slip through here because of the interaction
between "SwiftName" and "Availability: nonswift".

The next commit will re-add "NS" to some stragglers that we missed.

rdar://problem/26880017

* APINotes: Add "NS" back to a few types.

NSKeyedUnarchiverDelegate
NSKeyedArchiverDelegate
NSTextCheckingTypes
NSBinarySearchingOptions
NSEnumerationOptions
NSSortOptions

More rdar://problem/26880017

* Remove now-redundant SwiftNames from API notes.

No change observed in the generated interface of Foundation and its
submodules.

Finishes rdar://problem/26880017.
2016-08-01 20:54:26 -07:00
Doug Gregor
ca4a32b5a9 [SE-0112] Bridge NSError to ErrorProtocol.
Introduce bridging of NSError to ErrorProtocol, so an Objective-C API
expressed via an "NSError *" will be imported using ErrorProtocol in
the Swift. For example, the Objective-C method:

  - (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted;

will now be imported as:

  func handleError(_ error: ErrorProtocol, userInteractionPermitted: Bool)

This is bullet (3) under the proposed solution of SE-0112. Note that
we made one semantic change here: instead of removing the conformance
of NSError to ErrorProtocol, which caused numerous problems both
theoretical and actual because the model expects that an NSError
conforms to ErrorProtocol without requiring wrapping, we instead limit
the ErrorProtocol -> NSError conversion that would be implied by
bridging. This is defensible in the short term because it also
eliminates the implicit conversion, and aligns with SE-0072, which
eliminates implicit bridging conversions altogether.
2016-07-12 10:53:52 -07:00
Joe Groff
e6ba22fc0e SILGen: Look through ErasureExprs when bridging to AnyObject.
When we have an argument expr of type Any that's being emitted as a bridged ObjC object parameter, look through any ErasureExprs and directly bridge the concrete value. This saves us emitting an intermediate 'Any' value in the common case where a value of known concrete type is passed in from Swift.
2016-07-06 21:25:13 -07:00
Joe Groff
231db9d108 Clang importer: Import ObjC 'id' as Swift 'Any' (behind a flag).
For experimentation purposes, add a flag to cause the Clang importer to map ObjC APIs using `id` to `Any` instead of `AnyObject`.
2016-07-05 12:29:45 -07:00
Mishal Shah
87b7bcfd3e Update master to build with Xcode 8 beta 1, OS X 10.12, iOS 10, tvOS 10, and watchOS 3 SDKs. 2016-06-14 14:53:55 -07:00
Slava Pestov
0d3ef548e7 ClangImporter: Add support for __attribute__((availability(..., replacement="")))
We map clang::AvailabilityAttr::getReplacement() to
swift::AvailableAttr::Rename, transforming the replacement
name using by looking up the named Clang replacement, and
importing its name.

Fixes <rdar://problem/26301742>.
2016-06-01 23:12:48 -07:00
Slava Pestov
19f72c8593 ClangImporter: Fix diagnostics for NS* prefix stripping from generic types
We were failing to create an unavailable TypeAlias for the old name
in the case the renamed type was generic, leading to poor diagnostics.

Also, Sema resolves generic TypeAliases very early, while building
a Type from a TypeRepr -- this means the unavailable/deprecated
check runs too late to catch generic TypeAlises.

Add a hack where we preserve a reference to the original TypeAliasDecl
by way of constructing a SubstitutedType which desugars to the
replacement type, rather than resolving the replacement type
directly, so that the availability check can pick it up.

A better fix for this would be to introduce a BoundGenericAliasType
sugared type, but that's a bigger change that can come later.

Fixes <rdar://problem/26206263>.
2016-05-25 00:32:11 -07:00
Michael Ilseman
a8cebdc5d3 [swift_newtype] Special handling for Notifications
We now specially import global decls who we identify as fitting the
notification pattern: extern NSStrings whose name ends in
"Notification". When we see them, we import them as a member of
NSNotificationName and, if NSNotificationName is swift_newtype-ed, we
use that new type.

Test cases included.
2016-05-08 19:17:32 -07:00
Chris Willmore
af0c7bd620 Initial implementation of SE-0054 "Abolish IUO Type" (#2322)
This is a squash of the following commits:

* [SE-0054] Import function pointer arg, return types, typedefs as optional

IUOs are only allowed on function decl arguments and return types, so
don't import typedefs or function pointer args or return types as IUO.

* [SE-0054] Only allow IUOs in function arg and result type.

When validating a TypeRepr, raise a diagnostic if an IUO is found
anywhere other thn the top level or as a function parameter or return
tpye.

* [SE-0054] Disable inference of IUOs by default

When considering a constraint of the form '$T1 is convertible to T!',
generate potential bindings 'T' and 'T?' for $T1, but not 'T!'. This
prevents variables without explicit type information from ending up with
IUO type. It also prevents implicit instantiation of functions and types
with IUO type arguments.

* [SE-0054] Remove the -disable-infer-iuos flag.

* Add nonnull annotations to ObjectiveCTests.h in benchmark suite.
2016-05-03 14:06:19 -07:00
practicalswift
ea0ac3ac67 [gardening] Remove all tabs in C++ code 2016-04-08 21:22:57 +02:00
Joe Groff
bd97722931 Separate test objc_generics module in clang-importer-sdk from Foundation. 2016-03-28 09:50:30 -07:00
Chris Willmore
3bcb169f0b Import lightweight Objective-C generics as Swift generic type
parameters.
2016-03-28 09:50:30 -07:00
Chris Lattner
668d224b62 Fix <rdar://problem/25168818> Don't import None members in NS_OPTIONS types
When importing members of an NS_OPTIONS (aka an option set), mark imported
members that have a value of 0 with an unavailable error.  This produces an
experience like this:

  x = NSRuncingOptions.none // expected-error {{'none' is unavailable: use [] to construct an empty option set}}

This is important to do, because option set members with a value of zero
do not act like members of the option set.  For example, they always fail a
"contains" check.
2016-03-25 13:57:18 -07:00
Doug Gregor
5b97f8eeb9 [Clang importer] Don't strip prefixes from swift_bridge'd classes or their subclasses.
Part of rdar://problem/24050011.
2016-03-23 11:32:36 -07:00
Dmitri Gribenko
0f36bec31f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-18 16:41:35 -08:00
Doug Gregor
ecf6df6e71 [Omit needless words] More broadly recognize collection types 2016-02-17 19:58:23 -08:00
Dmitri Gribenko
dd75aed67a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-17 14:40:05 -08:00
Max Moiseev
3a3984877a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-15 15:43:34 -08:00
practicalswift
9eb2ee0638 [gardening] Fix accidental double spaces 2016-02-14 17:30:44 +01:00
Doug Gregor
319e2a7462 [Clang importer] Strip "NS" prefix from macro names, too 2016-02-03 21:03:09 -08:00
Doug Gregor
1dec2f1f34 [Clang importer] After stripping a prefix, lowercase initialisms as well. 2016-02-03 16:11:59 -08:00
Doug Gregor
1a2c0b063e [Clang importer] Be more careful when stripping module prefixes.
Clean up our handling of the removal of the "NS" prefix from
Foundation-defined entities in a few ways:

* If the "NS" is followed by a "_", strip that too
* Make sure that the result is still an identifier ("NS123" shouldn't
  become "123"!)
* Don't lowercase ALL_CAPS_NAMES
2016-02-03 16:11:59 -08:00
Doug Gregor
9736d54a77 [Code completion] For a Selector argument, provide #selector(<#objc method#>). 2016-02-02 16:06:32 -08:00
Doug Gregor
b9955404c8 [Clang importer] Lowercase plural initialisms correctly.
URLs -> urls rather than urLs.
2016-01-27 16:38:11 -08:00
Doug Gregor
312a7f0aea [Clang importer] Strip the "NS" prefix from entities in Foundation.
As part of the improved import of Objective-C APIs into Swift, strip
the "NS" prefix from entities defined in the Foundation
framework. Addresses rdar://problem/24050011, which is part of
SE-0005. Naturally, this is hidden behind -enable-omit-needless-words.
2016-01-20 15:02:54 -08:00
Doug Gregor
fef5aa5dbc Revert "[Omit needless words] Test lowercasing of values after prefix stripping."
This reverts commit 2b339286f2. This is
a temporary revert to aid with merging to swift-3-api-guidelines.
2016-01-20 13:24:58 -08:00
Doug Gregor
2b339286f2 [Omit needless words] Test lowercasing of values after prefix stripping. 2016-01-18 22:29:22 -08:00
Slava Pestov
79b24fa7d1 Update availability tests to be independent of the actual deployment target
Use fictional version numbers in the 10.50..10.99 range.
2016-01-13 19:27:26 -08:00
Doug Gregor
bfba3a7968 [Omit needless words] Fix lowercasing of initialisms ending with a non-letter.
Lowercases "UTF8String" to "utf8String" rather than "utF8String".
2016-01-04 16:45:31 -08:00
Xi Ge
f1ae3bbb64 Revert "[test] Put variadic definitions in a platform-independent header and remove the platform requirement."
The linux bots are still in oblivion of the variadic c function error.

Swift SVN r32915
2015-10-27 19:41:45 +00:00
Xi Ge
e048947869 [test] Put variadic definitions in a platform-independent header and remove the platform requirement.
Swift SVN r32914
2015-10-27 19:11:57 +00:00
Xi Ge
1604c4ba9e [ClangImporter] Import variadic C functions as unavailable functions instead of ignoring them. rdar://16677468
Also, this helps us fix rdar://20092567.

Swift SVN r32903
2015-10-27 00:29:44 +00:00
Slava Pestov
35d4d98242 Add test for Objective-C class reference from static inline function
Corresponding radar is <rdar://problem/22001279>, but the actual fix
is a Clang patch.

Swift SVN r32790
2015-10-21 00:08:21 +00:00
Slava Pestov
4bedf4562b Use the 'self consumed' analysis to eliminate releases of consumed self
Previously, SILGen would store a null pointer into the self box upon
encountering a constructor delegation that consumes self. This was a
constant source of bugs. Now, use the new analysis to make this use
DI information instead, emitting an extra bit at runtime if necessary.

Also re-organize the DI tests for initializers, and add CHECK: lines
instead of just asserting we don't crash or diagnose.

Swift SVN r32604
2015-10-11 02:11:15 +00:00
Doug Gregor
7778790a68 Omit needless words: prepend "is" to Boolean property names.
Prepend       "is" to        Boolean property names (e.g., "empty" becomes
"isEmpty") unless the property name strongly indicates its Boolean
nature or we're   likely to ruin the name. Therefore, the  presence of
one of the following in  the property name will suppress   this
transformation:

* An auxiliary verb, such as "is", "has", "may", "should", or "will".

* A word ending in "s", indicating either a plural (for which
  prepending "is" would be incorrect) or a verb in the continuous
  tense (which indicates its Boolean nature, e.g., "translates" in
  "translatesCoordinates").

Swift SVN r32458
2015-10-06 07:03:17 +00:00
Doug Gregor
93a24c44b0 Omit needless words: be more consistent about AnyObject -> "Object".
Swift SVN r32450
2015-10-05 22:53:53 +00:00
Doug Gregor
ab2f20f649 Omit needless words: don't introduce default arguments for lone parameters to setters.
Swift SVN r32446
2015-10-05 21:41:51 +00:00
Doug Gregor
44e34850ae Omit needless words: give initial Boolean parameters argument labels.
When the first parameter of a function has Boolean type, try to create
an argument label for it. We start with the (normally non-API)
parameter name as the argument label, then try to match that against
the end of the base name of the method to eliminate redundancy. Add a
little magic, and here are some diffs:

    -  func openUntitledDocumentAndDisplay(_: Bool) throws -> NSDocument
    +  func openUntitledDocument(display _: Bool) throws -> NSDocument

    -  func fontMenu(_: Bool) -> NSMenu?
    -  func fontPanel(_: Bool) -> NSFontPanel?
    +  func fontMenu(create _: Bool) -> NSMenu?
    +  func fontPanel(create _: Bool) -> NSFontPanel?

    -  func lockFocusFlipped(_: Bool)
    +  func lockFocus(flipped _: Bool)

    -  func rectForSearchTextWhenCentered(_: Bool) -> NSRect
    +  func rectForSearchText(whenCentered _: Bool) -> NSRect

    -  func dismissPreviewAnimated(_: Bool)
    -  func dismissMenuAnimated(_: Bool)
    +  func dismissPreview(animated _: Bool)
    +  func dismissMenu(animated _: Bool)

Swift SVN r32392
2015-10-01 23:34:21 +00:00
Doug Gregor
229e4f4af5 Omit needless words: Objective-C BOOL -> "Bool".
Swift SVN r32385
2015-10-01 22:47:03 +00:00
Doug Gregor
cf7cc554cd Omit needless words: make _t a skippable type suffix.
Swift SVN r32384
2015-10-01 22:37:43 +00:00
Doug Gregor
2d5d3d240c Omit needless words: use the actual mapped names for builtin C types.
This provides better fidelity between omit-needless-words acting on
Clang types vs. acting on Swift types.

Swift SVN r32383
2015-10-01 22:37:40 +00:00
Doug Gregor
169581f73b Omit needless words: don't inject argument labels for trailing closures.
We were getting this wrong when there were no parentheses.

Swift SVN r32289
2015-09-28 23:44:01 +00:00