Commit Graph

2609 Commits

Author SHA1 Message Date
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
112511cd1c Remember to set AbstractFunctionDecl's generic signature in a few places
I originally added this so that we would keep the signature around
even if type checking failed, and the function was given an
ErrorType.

Add a formal check to the AST verifier for this, and set the signature
in a few places where it wasn't being set.

Note that since we only serialize valid declarations, we don't have
to serialize a reference to the generic signature separately, but we
do have to remember to set it when deserializing, which wasn't being
done for destructors.
2016-05-28 22:30:40 -07:00
Michael Ilseman
d79546a3d7 Merge pull request #2751 from milseman/newtype_fixit
[Import as Member] Print full context in fixit
2016-05-27 13:38:12 -07:00
Michael Ilseman
22ec60007a [Import as Member] Print full context in fixit
Due to swift_name and swift_newtype, we are frequently importing onto
different contexts. This was confusing the fixit logic for unavailable
swift2 names, as we were trying to use Clang names when the Swift name
might be totally different (and even a nested type). This change has a
two-fold effect:

1) Globals who are imported onto swift_newtype-ed typedefs should be
   considered ImportAsMember.
2) When printing out the name of an ImportAsMember Swift 3 decl, we
   need to print out a fully qualified context, which also uses the
   Swift names, not the Clang names.
2016-05-27 11:39:19 -07:00
Joe Groff
b47b16850b Merge pull request #2731 from jckarter/objc-generic-protocol-conformance
Clang importer: Fix the 'self' type of imported ObjC generic members.
2016-05-26 19:11:05 -07:00
Joe Groff
615cfd4a10 Clang importer: Fix the 'self' type of imported ObjC generic members.
A variable was called `selfInterfaceType` even though it was inconsistently set to a contextual Self archetype for protocols, and an interface type for nominal types, and the one place we used the type expected to work with contextual types. Fixes rdar://problem/26396895.
2016-05-26 16:34:56 -07:00
Jordan Rose
239b5e02a6 [ClangImporter] Import NS_OPTIONS 0 values with explicit Swift-names.
This shows that the owner of the option set has thought about the
zero case, and wants it to be available in Swift, rather than disabling
it with the default "use [] to construct an empty option set" message.

rdar://problem/26290766
2016-05-26 11:08:24 -07:00
Joe Groff
9d86ca26df Merge pull request #2686 from jckarter/generic-anyobject-chaining-properties-subscripts
SILGen: Fix crashes when conditionally looking up generic subscripts and properties via AnyObject.
2016-05-25 09:46:12 -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
Doug Gregor
abc42b459e [Clang importer] Allow a CF type spelled with "void *".
There wasn't any justification for the "const" restriction. Fixes
rdar://problem/24821660.
2016-05-24 22:39:20 -07:00
Joe Groff
5e2b20d05f SILGen: Fix crashes when conditionally looking up generic subscripts and properties via AnyObject.
The fix for methods to lower the dynamic method type from the substituted AST type of the expression also needed to be applied to the optional chaining, subscript, and property paths.

This also exposed a problem in the Clang importer, where imported subscript accessors would get the unbound generic context type as their Self parameter type instead of the type with the correct generic parameters. Fix this by renaming the all-too-convenient ParamDecl::createSelf factory to `createUnboundSelf`, and introduce a new `createSelf` that uses the bound generic type.

Fixes rdar://problem/26447758.
2016-05-24 19:18:37 -07:00
Michael Ilseman
bd3e590925 [Clang importer] Comments and cleanup. NFC 2016-05-24 10:12:08 -07:00
Michael Ilseman
134f41ccae [Clang importer] Use OptionSets for makeStructRawValued
NFC
2016-05-23 16:21:17 -07:00
Michael Ilseman
e0414a457f [swift_newtype] Structs have label-less init
Adds an unlabeled rawValue init for swift_newtype(struct), which
expresses the extensibility theme better.  This makes the use and
creation of new instances of that type more succinct.
swift_newtype(enum) still requires the explicit label, as it is
non-extensible.
2016-05-23 14:40:41 -07:00
Slava Pestov
170992c39f AST: Add Throws flag and ThrowsLoc to AbstractFunctionDecl
The verifier now asserts that Throws, ThrowsLoc and isBodyThrowing()
match up.

Also, add /*Label=*/ comments where necessary to make the long argument
lists easier to read, and cleaned up some inconsistent naming conventions.

I caught a case where ClangImporter where we were passing in a loc as
StaticLoc instead of FuncLoc, but probably this didn't affect anything.
2016-05-21 12:51:50 -07:00
Doug Gregor
1255a5cdb9 [Clang importer] Make sure an unavailable superclass init doesn't override an available import-as-member-init.
More generally, an unavailable initializer shouldn't stomp on an
available initializer, because it's possible that (for example) a
designated initializer will be unavailable but a factory initializer
will be available, so one still construct objects of that type.
Fixes rdar://problem/26238032.
2016-05-20 09:39:57 -07:00
David Farler
ee4d9d36aa [QoI] Don't skip over protocols when importing Swift 2 type aliases
Protocols have the implicit Self generic parameter, which was
preventing this type alias from getting created, so some renamed
imported protocols for Swift 3 weren't emitting the expected
diagnostics and fix-its.

rdar://problem/26304496
2016-05-20 09:18:09 -07:00
Jordan Rose
2fa3da4089 Drop the notion of "alias" names for CF types.
Previously we imported a Core Foundation type "CCFooRef" as "CCFoo",
but also provided a typealias "CCFooRef". In Swift 3, we decided to
mark "CCFooRef" unavailable to force developers to consistently use
"CCFoo". Now that we have infrastructure to mark /all/ renamed
declarations as unavailable, just use that to track the renaming,
i.e. pretend that "CCFooRef" was the "Swift 2" name for the type.

This doesn't change the conflict resolution behavior: if there's
another name "CCFoo" in the same module, the CF type will be
imported as just "CCFooRef".

Groundwork cleanup for rdar://problem/26347297, which notes that our
import-as-member fix-its use the "Ref" names rather than the short
names.
2016-05-19 13:21:47 -07:00
Joe Groff
7154c6167b Stray punctuation, NFC 2016-05-16 13:11:49 -07:00
Jordan Rose
26e1f5be27 [ClangImporter] Don't include Swift members in AnyObject lookup. (#2503)
More specifically, don't include declarations of methods and properties
in the list of "all imported Objective-C members" if said method or
property is in a generated header. (We actually key off of whether the
enclosing class, protocol, or category is marked as coming from Swift,
but since users aren't supposed to modify generated headers themselves
it's much the same thing.)

This previously caused a crash because we tried to import a Clang member
onto a Swift decl in order to provide the particular member on AnyObject.

rdar://problem/25955831 and probably also rdar://problem/25828987, which
deals with the fix-it to migrate to #selector. (We do an AnyObject-like
lookup to find out which class likely implements the specified selector.)
2016-05-12 16:58:31 -07:00
Doug Gregor
dfc8d5a5f8 [Sema/SILGen] Track per-source-file "used" conformances to reference in SILGen
Sema was dutifully tracking conformances that were "used" as part of
type checking, so it could make sure that those conformances got
completed for SILGen to use. However, this information never actually
made it to SILGen, which included its own (more conservative, not
broad enough) heuristics for finding "used" conformances. Teach Sema
to record conformances within the appropriate source file, and have
SILGen reference the conformances when it emits SIL for the source
file.
2016-05-09 20:43:12 -07:00
Michael Ilseman
c62eb2e5aa [swift_newtype] Handle un-audited CF types
Special case logic for CF types, which may be coming in as
unmanaged. In this case, we will use audit information if present to
import with the new type, otherwise we have to fall back to
Unmanaged<CF...>.

We still import global variables that must be unmanaged onto the new
type, though they keep their unmanaged types. This helps to
consolidate the definitions, as well as make future migration easier
if they get audited.

Test cases included.
2016-05-09 14:26:26 -07:00
Michael Ilseman
b5d29b54d2 [swift_newtype] Also newtype up CF Ref types.
Code cleaup and generalization to also catch typedefs of CF reference
types. Tests will be included in a subsequent commit, as this now
exhibits a latent problem when interacting with Unmanaged.
2016-05-09 11:48:18 -07:00
Doug Gregor
b4a58650dd [SE-0033] Add _SwiftNewtypeWrapper protocol for swift_newtype'd types.
This protocol lets us identify swift_newtype'd types. More
importantly, use protocol extensions to make it easy to transfer
specific conformances from the underlying type to the wrapper
type. So, for example, if the underlying type is Hashable, make the
wrapper type Hashable as well. Do the same for Equatable, Comparable,
and _ObjectiveCBridgeable. Fixes rdar://problem/26010804.
2016-05-08 22:34:39 -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
Doug Gregor
26fcf1ab4a [Clang importer] Properly create getter for bridged swift_newtype's rawValue.
When swift_newtype is introducing a new Swift type that wraps a
bridged type, the actual storage (called _rawValue) matches the
Objective-C type and "rawValue" itself is a computed
properly. Properly create the getter for that computed property, which
performs a bridging conversion.
2016-05-08 00:51:02 -07:00
Doug Gregor
3739470553 [Clang importer] Fix rawValue initializer for swift_newtype'd types.
The code was creating a new "self" rather than using the existing
"self", which was caught by DI.
2016-05-08 00:00:50 -07:00
Ted Kremenek
d3c41c4668 Revert "Revert "SE-0072: Fully eliminate implicit bridging conversions from Swift"" (#2441) 2016-05-06 23:26:45 -07:00
Ted Kremenek
4df003c291 Revert "SE-0072: Fully eliminate implicit bridging conversions from Swift" (#2440) 2016-05-06 23:22:56 -07:00
Doug Gregor
2fd0a9f79e [Clang importer] Don't import swift_newtype'd underlying types as optional. 2016-05-06 22:32:38 -07:00
Joe Pamer
6af546e89e Merge pull request #2419 from apple/se-0072
SE-0072: Fully eliminate implicit bridging conversions from Swift
2016-05-06 22:14:13 -07:00
Doug Gregor
55a3f5398c [Clang importer] Import Swift 2 "stubs" to improve errors in "Swift 2" code.
When attempting to compile Swift 2 code (or any Swift code using the
Swift 2 names) in Swift 3, the compiler diagnostics are often entirely
useless because the names have changed radically enough that one
generally gets "no member named 'foo'" errors rather than a helpful
"'foo' was renamed to 'bar'" error. This makes for a very poor user
experience when (e.g.) trying to move Swift 2 code forward to Swift 3.

To improve the experience, when the Swift 2 and Swift 3 names of an
API differ, the Clang importer will produce a "stub" declaration that
matches the Swift 2 API. That stub will be marked with a synthesized
attribute

  @available(unavailable, renamed: "the-swift-3-name")

that enables better diagnostics (e.g., "'foo' is unavailable: renamed
to 'bar') along with Fix-Its (courtesy of @jrose-apple's recent work)
that fix the Swift 2 code to compile in Swift 3.

This change addresses much of rdar://problem/25309323 (concerning QoI
of Swift 2 code compiled with a Swift 3 compiler), but some cleanup
remains.
2016-05-06 21:12:20 -07:00
Doug Gregor
9f6734312d [Clang importer] Factor the checking for -enable-swift-newtype more sensibly. NFC 2016-05-06 16:45:13 -07:00
Jordan Rose
e0e5563a96 [ClangImporter] Allow NS_SWIFT_NAME to just specify a base name.
...when renaming C functions. This is currently rejected by Clang
itself, but showed up when testing apinotes. Rather than complicate
the apinotes by including parameter placeholders, just do the
obvious thing (and future-proof against us potentially allowing this
in the future).

rdar://problem/26133744
2016-05-06 11:25:30 -07:00
Joe Pamer
3b4d98445e SE-0072: Fully eliminate implicit bridging conversions from Swift
Per Swift Evolution proposal SE-0072, these changes prevent the compiler from introducing implicit bridging conversions during type checking.
2016-05-05 19:36:33 -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
Michael Ilseman
0c011c328c Merge pull request #2170 from milseman/coregraphics
[Import as member] Don't pair up get/set pairs from other modules
2016-04-27 07:00:00 -07:00
Michael Ilseman
aa1a68552d Merge pull request #2319 from milseman/newtype
[Clang importer] swift_newtype with bridged computed rawValue
2016-04-26 21:57:42 -07:00
Michael Ilseman
15e7d1f6d7 [Clang importer] Code re-use and testing.
Incorporates review feedback to re-use code more. Improves
swift_newtype testing by also testing non-bridging path.
2016-04-26 16:44:31 -07:00
Michael Ilseman
0c67ddd8b6 [Clang importer] swift_newtype with bridged computed rawValue
For swift_newtype structs that we create, we sometimes need to provide
a bridged type interface. In these cases, we use the original
non-bridged type as an underlying stored value, and create a computed
rawValue of bridged type. We similarly create an init() taking the
bridged type, and cast it appropriately to/from storage.

Tests updated.
2016-04-26 15:12:50 -07:00
Doug Gregor
2477206ed4 [Clang importer] Mark discardableResult attribute "implicit".
The Clang importer implicitly synthesizes @discardableResult for
nearly all imported functions. Printing this attribute in the
generated interface leads to a lot of noise. Mark it as implicit so we
don't print it.
2016-04-26 14:48:39 -07:00
Doug Gregor
a2e03d556b Stage implementation of SE-0033 behind a flag, -enable-swift-newtype.
We're going to quarantine this feature behind a frontend flag for a
bit.
2016-04-25 18:03:04 -07:00
Joe Groff
825e02e2f1 Remove -enable-import-objc-generics staging flag. 2016-04-25 14:25:11 -07:00
practicalswift
9a078b54ef [gardening] Fix recently introduced typo: "a executable" → "an executable"
[gardening] Fix recently introduced typo: "a offset" → "an offset"
[gardening] Fix recently introduced typo: "accessiblity" → "accessibility"
[gardening] Fix recently introduced typo: "cant" → "can't"
[gardening] Fix recently introduced typo: "inteference" → "interference"
[gardening] Fix recently introduced typo: "unsatified" → "unsatisfied"
[gardening] Remove accidental space.
2016-04-24 22:11:59 +02:00
Michael Ilseman
9c31aa45c5 [Clang Importer] implement swift_newtype(enum) as struct
This is a temporary solution that implements swift_newtype(enum) as
though it were written swift_newtype(struct). This is to work around
to the fact that a String-backed enum does not actually have a String
stored, and a struct is closer to reflecting that storage
properly. Struct provides most of the functionality and appearance for
now, though it does not allow for switching over the values.

Full support for swift_newtype(enum) as a Swift enum is forthcoming.
2016-04-21 16:43:52 -07:00
Michael Ilseman
faceb558d7 [Clang Importer] Initial support for swift_newtype(struct)
This introduces support for swift_newtype(struct) attribute (also
known as swift_wrapper). The Clang importer will create a brand new
struct corresponding to the annotated typedefs, which has a backing
raw value. Globals of that type are imported as static members on the
struct.

Additionally, this interacts seamlessly with prior import-as-member
work, meaning that the newly created type can be imported onto. Tests
included.
2016-04-21 16:40:10 -07:00
Michael Ilseman
65288eec66 [Clang Importer] Cleanup and sharing of code
Adds makeStructRawValued utility function, which shares common code
for raw-value-backed structs from both OptionSets and many imported
enums. NFC.
2016-04-21 15:59:28 -07:00
Michael Ilseman
08ac432d3c [Import as member] Don't pair up get/set pairs from other modules
While inferring get/set, we paired them up even when one of them was
available through a custom objc header (e.g. a private
header). Instead, fail to pair them up. Test case added.
2016-04-13 13:08:16 -07:00
Trent Nadeau
8a55e428f4 Imported C decls w/o warn_unused_result get @discardableResult 2016-04-13 00:39:07 +00:00
Joe Groff
fe7c1ca670 Clang importer: Global vars imported as static member properties are final. 2016-04-12 10:03:48 -07:00