Commit Graph

908 Commits

Author SHA1 Message Date
Slava Pestov
cbf08e4359 AST: Make ProtocolDecl::requiresClass() const 2016-04-25 15:03:16 -07:00
Doug Gregor
8802d6d52a Improve diagnostics for selector collisions with @objc optional requirements.
When an optional requirement of an @objc protocol has a selector that
collides with an entity that has a different *Swift* name but produces
an Objective-C method with the same selector, we have an existing
diagnostic complaining about the conflict. In such cases, make a few
suggestions (with Fix-Its) to improve the experience:

* Change Swift name to match the requirement, adding or modifying the
  @objc as appropriate.
* Add "@nonobjc" to silence the diagnostic, explicitly opting out of
  matching an @objc requirement.

This is intended to help with migration of Swift 2 code into Swift
3. The Swift 2 code will produce selectors that match Objective-C
methods in the protocol from Swift names that don't match; this helps
fix up those Swift names so that we now match.

Fixes the rest of rdar://problem/25159872. In some sense, it's a
stop-gap for more detailed checking of near-misses for optional
requirements, but it's not clear how wide-reaching such changes would
be.
2016-04-19 10:22:23 -07:00
Greg Parker
125a146365 Revert "[Sema] Improve diagnostics for witness mismatches against @objc protocols." and "Improve diagnostics for selector collisions with @objc optional requirements."
This reverts commits 46269299cd
and 27279866ad
and c826a408dd.

The changes broke test bots, including
https://ci.swift.org/job/oss-swift-package-osx/1348/
2016-04-19 05:52:33 -07:00
Doug Gregor
27279866ad Improve diagnostics for selector collisions with @objc optional requirements.
When an optional requirement of an @objc protocol has a selector that
collides with an entity that has a different *Swift* name but produces
an Objective-C method with the same selector, we have an existing
diagnostic complaining about the conflict. In such cases, make a few
suggestions (with Fix-Its) to improve the experience:

* Change Swift name to match the requirement, adding or modifying the
  @objc as appropriate.
* Add "@nonobjc" to silence the diagnostic, explicitly opting out of
  matching an @objc requirement.

This is intended to help with migration of Swift 2 code into Swift
3. The Swift 2 code will produce selectors that match Objective-C
methods in the protocol from Swift names that don't match; this helps
fix up those Swift names so that we now match.

Fixes the rest of rdar://problem/25159872. In some sense, it's a
stop-gap for more detailed checking of near-misses for optional
requirements, but it's not clear how wide-reaching such changes would
be.
2016-04-18 17:08:06 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
practicalswift
abfecfde17 [gardening] if ([space]…[space]) → if (…), for(…) → for (…), while(…) → while (…), [[space]x, y[space]] → [x, y] 2016-04-04 16:22:11 +02:00
Doug Gregor
7d1a2e8339 Reinstate "Implement support for Clang's objc_runtime_visible attribute."
When a Clang-defined Objective-C class has the objc_runtime_visible
attribute, use objc_lookUpClass to get the Objective-C class object
rather than referencing the symbol directly. Also, ban subclassing of
Objective-C-runtime-visible classes as well as @objc on members of
extensions of such classes.

As a drive-by needed for this test, make
ClassDecl::getObjCRuntimeName() respect the Clang objc_runtime_name
attribute.

Fixes rdar://problem/25494454.

Fix an i32 vs. 64 issue in the IR matching for the IR generation test.

This reverts commit 09973e6956.
2016-04-02 20:10:32 -07:00
Ted Kremenek
09973e6956 Revert "Implement support for Clang's objc_runtime_visible attribute."
This reverts commit 2c1f19a547.

This appears to be breaking all the iOS bots.
2016-04-02 07:33:14 -07:00
Doug Gregor
2c1f19a547 Implement support for Clang's objc_runtime_visible attribute.
When a Clang-defined Objective-C class has the objc_runtime_visible
attribute, use objc_lookUpClass to get the Objective-C class object
rather than referencing the symbol directly. Also, ban subclassing of
Objective-C-runtime-visible classes as well as @objc on members of
extensions of such classes.

As a drive-by needed for this test, make
ClassDecl::getObjCRuntimeName() respect the Clang objc_runtime_name
attribute.

Fixes rdar://problem/25494454.
2016-04-01 23:07:21 -07:00
Chris Lattner
514ec31114 Progress towards SR-1069:
- Fix ExprTypeSaverAndEraser to save & restore the invalid bit on closure parameter decls.
 - Teach CalleeCandidateInfo::evaluateCloseness to not try to find generic subs on types that
   contain a unresolved type within them.

With these changes, the compiler doesn't segfault on the testcase when assertions are
enabled.  It still doesn't produce a great diagnostic though.
2016-03-29 22:33:07 -07:00
Michael Ilseman
8759905351 Merge remote-tracking branch 'origin' into import-as-member 2016-03-24 11:37:36 -07:00
Jordan Rose
9e9c80e090 Add a @_versioned attribute for testing resilience.
This attribute is a stand-in for the versioning annotations
described in docs/LibraryEvolution.rst; right now it's just present
or absent, and its only effect is to make sure versioned internal
decls are treated as public at the SIL level. (This functionality
already existed for -enable-testing, so it can probably be trusted.)

Also, allow inlineable functions to reference transparent and
inline-always functions /if/ they're only called immediately (not used
as values or partial-applied), since they'll be inlined away before
emitting IR. (We should really only allow this /before/ mandatory
inlining, but we don't have a separate SIL stage for that.)
2016-03-24 00:53:35 -07:00
Michael Ilseman
47e73ad4f1 Merge branch 'master' of github.com:apple/swift into import-as-member 2016-03-23 17:01:47 -07:00
Michael Ilseman
7867533f2c Merge commit 'b894798e461077b30a3aa4afe89639f312f44858' into import-as-member 2016-03-23 15:40:04 -07:00
Michael Ilseman
d7581d1b36 Merge commit 'a31edf53d0580efe47f4e9ef89dccc4429c056e8' into import-as-member 2016-03-23 13:05:57 -07:00
Joe Groff
5db097d098 SILGen: Fixes for C functions imported as CF class members. 2016-03-21 19:33:22 -07:00
Joe Groff
aec7be3930 SILGen: Handle C functions imported as property accessors. 2016-03-18 13:23:40 -07:00
Jordan Rose
2ecb93b2f3 Merge pull request #1406 from drewcrawford/secret-squashed
[Serialization] Don't serialize non-public documentation
2016-03-18 09:26:31 -07:00
Joe Groff
f42e579040 Change SILGen to use ImportAsMemberStatus populated by ClangImporter. 2016-03-17 17:09:41 -07:00
Drew Crawford
aa5cf2b842 [Serialization] Don't serialize non-public documentation
Let's say I am a good citizen and document my private symbols:

    /** My TOP SECRET DOCUMENTATION */
    private class Foo {
    }

When I go to distribute the compiled binary, I find out my private
documentation is distributed as well:

    $ swiftc test.swift -emit-module -module-name "test"
    $ strings test.swiftdoc
    My TOP SECRET DOCUMENTATION
    /** My TOP SECRET DOCUMENTATION */

If a client can't use a symbol (e.g. it's private [or internal and not
-enable-testing]) don't emit the documentation for a symbol in the
swiftdoc.

Fixes: SR-762, rdar://21453624

The test coverage implements this truth table:

| visibility | -enable-testing | documentation? |
|------------|-----------------|----------------|
| private    | no              |              |
| internal   | no              |              |
| public     | no              |              |
| private    | yes             |              |
| internal   | yes             |              |
| public     | yes             |              |

Modified the existing comments test coverage to expect non-public
documentation not to be emitted.

Don't rely on existing comment structure

Refuse to emit comments if the decl cannot actually have one.  To
accomplish this, we move `canHaveComment` into the Decl instance.  It
must also be marked `const`, since one of its existing usages operates
on a const pointer.

Perform fewer checks when serializing the standard library.
2016-03-17 17:46:23 -05:00
Michael Ilseman
875920b540 [Import as member] Also encode when imported as a static member in the AST 2016-03-17 11:16:20 -07:00
Joe Groff
c4a69e9d5d SILGen: Basic code generation for C functions imported as methods.
Introduce abstraction patterns for curried C-functions-as-methods for type lowering, and plumb the "foreign self parameter index" through call emission so that we emit the "self" parameter in the right position. This gets us handling C functions imported as methods with explicit swift_name attributes in simple, fully-applied cases. There's still more work to be done for properties, partial applications, and initializers introduced by extensions.
2016-03-17 10:43:25 -07:00
Slava Pestov
a4c50e1c40 Serialization: Serialize default witnesses when serializing a protocol
This will allow SILGen to check if a requirement was witnessed by
its default implementation.
2016-03-17 10:39:34 -07:00
Slava Pestov
552a6ad1b7 Sema: Remove some dead code for "delayed members", NFC
This was added at some point to make 'import Foundation' faster in the REPL.
What we really care about though is not delaying synthesis of the rawValue
accessors (those are synthesized on demand anyway), but delaying the
conformance check to RawRepresentable.
2016-03-16 18:35:31 -07:00
Michael Ilseman
332959df8e [Import as member] Store selfIndex in the AST 2016-03-15 13:18:59 -07:00
Janek Spaderna
a341b0f4ca [AST] Return whether type computation of EnumElementDecl succeeded
This is no change in functionality, but allows for easier bailing out when
type-checking EnumElementDecls.
2016-03-12 11:50:40 +01:00
Ben Langmuir
bac7521af7 [SourceKit] Fix crash with cursor info on generic typealias
For now, just skip mangling the name since we know the decl context will
crash the mangler.
2016-03-08 18:03:42 -08:00
Xi Ge
c84f667676 [Serialization] Serialize the source order of Decls appearing in their original source file.
We used to think the source order is preserved after de-serialization; however, this seems to be
not the case.
2016-03-08 12:18:10 -08:00
Xi Ge
7a3276738b Re-apply "ModulePrinter: Print decls from the same source file near each other."
This reverts commit 1db83907e3.
2016-03-08 12:18:10 -08:00
Xi Ge
1db83907e3 Revert "ModulePrinter: Print decls from the same source file near each other."
This reverts commit a5c9072344 for mysterious bot test failure.
2016-03-07 23:33:23 -08:00
Xi Ge
a5c9072344 ModulePrinter: Print decls from the same source file near each other. 2016-03-07 22:51:50 -08:00
Chris Lattner
fe9fe47b7e Implement support for generic typealiases. 2016-03-07 22:20:16 -08:00
Chris Lattner
dd3e962698 sink some more logic down to GenericTypeDecl, add support for safe downcasting
from Decl to GenericTypeDecl.  NFC.
2016-03-06 21:44:40 -08:00
Chris Lattner
868a795566 Introduce a new class between TypeDecl and NominalTypeDecl named GenericTypeDecl.
This factors the DeclContext and generic signature behavior out of NTD, allowing
it to be reused in the future.  NFC.
2016-03-04 23:09:15 -08:00
Chris Lattner
9e62009bb6 update comment for "new" syntax, NFC. 2016-03-04 16:00:37 -08:00
Slava Pestov
36ef7eb0f0 Sema: Add ProtocolDecl::DefaultWitnesses
NFC for now, since the code that uses this is not checked in yet.
2016-03-02 22:12:21 -08:00
Joe Groff
e94fcace3f Sema: Infrastructure for DI-style initialization through behaviors.
Allow a behavior protocol to declare an `initStorage` implementation with a parameter. If we have an initializer expression, use `initStorage(initExpr)` to initialize the storage; otherwise, remember the storage declaration and its initializer. Definite
initialization will have to use these to insert the initialization operation for the behavior property at the right place.
2016-03-01 11:51:25 -08:00
Joe Groff
a4fa786a6f Handle a parameter to property behaviors.
For prototyping purposes, we only support a single trailing closure parameter to begin with.
2016-03-01 11:51:25 -08:00
Slava Pestov
6812b6926b Sema: Cleanups and minor fixes for protocol 'Self' types
We had four duplicated implementations of checking how a protocol
requirement uses 'Self', all slightly wrong or incomplete:

- When deciding if the protocol type can be used as an existential.
  This one would just ignore 'Self' in the return type of a method
  completely, which was incorrect for cases where 'Self' is
  contravariant but part of the return value, for example:

  func foo() -> (Self -> ())

- When deciding if a member access can be performed on an existential
  value. This is distinct from the former, because the member may
  have been defined in a protocol extension, in which case it cannot
  be used even if the protocol type can be used as an existential.
  Unfortunately, this implementation was overly conservative, and
  would reject uses of 'Self' where Sema could in fact erase the
  existential type, for example:

  func foo() -> Self??
  func foo() -> Self.Type
  func foo() -> (Self, Self)

  This function handled function return types correctly, effectively
  plugging the leak in the previous code. It did lead to inconsistent
  behavior with protocols that had contravariant Self in requirements
  though; sometimes we would diagnose uses of the existential type,
  other times we would only complain about specific members.

- When deciding if a method in a non-final class can model a protocol
  requirement. This one was the most elaborate one, but here
  contravariance and uses of associated types are actually okay, so
  it was written to pick up covariant 'Self' only. However, it also
  did not handle metatypes and tuples.

- When opening the type of member of an existential, we would check
  if the return value was 'Self' or an optional of 'Self', but again
  this check was too conservative, so after the previous three were
  fixed, we could reference members on existentials that did not
  have a correct opened type.

Now, these have been combined into one check. To fix some crashes,
Sema's implementation of erasing existentials now relies on
coerceToType() instead of hand-rolling a few coercions of its own,
and wrapping the rest in CovariantFunctionConversionExpr, which
didn't make much sense if the result was not a function type.

SILGen still does not support function type conversions where an
existential return value is being erased; these would silently
miscompile before, but crash with an assertion now, because they
are correctly modeled as a FunctionConversionExpr, and not
CovariantFunctionConversionExpr.
2016-02-28 23:52:35 -08:00
Dmitri Gribenko
a9f8d97d3e Replace 'unsigned int' with 'unsigned'
'unsigned' is more idiomatic in LLVM style.
2016-02-27 16:20:27 -08:00
Xi Ge
a91cc97058 [SourceKit] Indentation: aligning parameters when argument names are present. rdar://24862107
Fix is in AST by removing unnecessary override.
Originally reported by: https://twitter.com/JohnRHeaton/status/702526804138074112
2016-02-26 12:09:24 -08:00
Joe Groff
48b643c6af Property behaviors: Iterate prototype syntax to try a postfix form.
If behaviors are specified after the declaration, something like this:

  ```swift
  var x: Int __behavior foo // __behavior is a stand-in keyword
  ```

we're thinking this encourages a simpler design for smaller, more composable behaviors. If we think of behavior application as function-like, then parameters to the behavior could be passed with function-like syntax:

  ```swift
  __behavior lazy(@autoclosure initialValue: () -> Value) { ... }

  var x: Int __behavior lazy(1738)

  __behavior didSet(body: (oldValue: Value) -> Void) { ... }

  var x: Int __behavior didSet {
    trailingClosure()
  }
  ```

Since behaviors are implementation details, they arguably belong to the right of the declaration as well.
2016-02-25 15:49:27 -08:00
Doug Gregor
6fe6266c99 [ObjC Interop] Map Swift @objc properties named isFoo to ObjC Cocoa conventions
The Objective-C Cocoa convention eschew "is" on property names, but
use it on the getter, while the Swift API guidelines state that
Boolean properties should read as assertions (e.g., "isEmpty" rather
than "empty"). Map Swift properties named "isFoo" to Objective-C by
removing the "is" from the resulting Objective-C property name (so it
will be named "foo") and from the setter (which will have the
Objective-C selector "setFoo:") while retaining the "is" for the
getter selector ("isFoo").

Fixes rdar://problem/17090661.
2016-02-23 20:49:20 -08:00
Joe Groff
26e55ce465 Sema: Initial parsing and synthesis for properties with behaviors.
Parse 'var [behavior] x: T', and when we see it, try to instantiate the property's
implementation in terms of the given behavior. To start out, behaviors are modeled
as protocols. If the protocol follows this pattern:

  ```
  protocol behavior {
    associatedtype Value
  }
  extension behavior {
    var value: Value { ... }
  }
  ```

then the property is instantiated by forming a conformance to `behavior` where
`Self` is bound to the enclosing type and `Value` is bound to the property's
declared type, and invoking the accessors of the `value` implementation:

  ```
  struct Foo {
    var [behavior] foo: Int
  }

  /* behaves like */

  extension Foo: private behavior {
    @implements(behavior.Value)
    private typealias `[behavior].Value` = Int

    var foo: Int {
      get { return value }
      set { value = newValue }
    }
  }
  ```

If the protocol requires a `storage` member, and provides an `initStorage` method
to provide an initial value to the storage:

  ```
  protocol storageBehavior {
    associatedtype Value

    var storage: Something<Value> { ... }
  }
  extension storageBehavior {
    var value: Value { ... }

    static func initStorage() -> Something<Value> { ... }
  }
  ```

then a stored property of the appropriate type is instantiated to witness the
requirement, using `initStorage` to initialize:

  ```
  struct Foo {
    var [storageBehavior] foo: Int
  }

  /* behaves like */

  extension Foo: private storageBehavior {
    @implements(storageBehavior.Value)
    private typealias `[storageBehavior].Value` = Int
    @implements(storageBehavior.storage)
    private var `[storageBehavior].storage`: Something<Int> = initStorage()

    var foo: Int {
      get { return value }
      set { value = newValue }
    }
  }
  ```

In either case, the `value` and `storage` properties should support any combination
of get-only/settable and mutating/nonmutating modifiers. The instantiated property
follows the settability and mutating-ness of the `value` implementation. The
protocol can also impose requirements on the `Self` and `Value` types.

Bells and whistles such as initializer expressions, accessors,
out-of-line initialization, etc. are not implemented. Additionally, behaviors
that instantiate storage are currently only supported on instance properties.
This also hasn't been tested past sema yet; SIL and IRGen will likely expose
additional issues.
2016-02-20 15:01:05 -08:00
Xi Ge
9aa3d01101 [Serialization] Avoid serializing source orders, trust deserialized decls in preserving such order. rdar://24610992
Thank Jordan for suggesting this.
2016-02-11 12:24:38 -08:00
Xi Ge
98734f588a [Serialization] Serialize and deserialize source order in module doc files. 2016-02-10 17:46:49 -08:00
Jordan Rose
68299c7c43 Remove ABI-dependent hackery from AbstractStorageDecl.
Replace pointer arithmetic between multiple base classes with pointer arithmetic
between adjacent allocations. This is still pretty fragile, but at least not
ABI-dependent, and in practice we should generate exactly the same code.
2016-02-08 19:40:47 -08:00
Jordan Rose
d9d49f72a3 Adopt llvm::TrailingObjects as much as possible in AST.
This class formalizes the common case of the "trailing allocation" idiom we use
frequently. I didn't spot any true bugs while making this change, but I did see
places where we were using the wrong pointer type or casting through void* for
no good reason. This will keep us honest.

I'll get to the other libraries soon.
2016-02-08 19:40:47 -08:00
Jordan Rose
91b72d3802 Fold the rest of PointerLikeTypeTraitsFwdDecl.h into TypeAlignments.h.
TypeAlignments.h predates this whole mess; it was used for types with
stronger alignment in PointerLikeTypeTraits than the old default of
"2 by fiat and assumption". All remaining forward-declared types are
AST types, so fold them into TypeAlignments.h.

(The one exception is SILTypeList.h, but that's already gone on master.)

To avoid future ODR issues, explicitly include TypeAlignments.h into
every header that defines a type it forward-declares.

I wish we could use partial specialization to provide PointerLikeTypeTraits
for all derived classes of Decl, TypeBase, etc, but that's not something
you can do in C++ if you don't control the traits class.
2016-02-06 11:22:28 -08:00
Jordan Rose
88ae300e26 Don't forward-declare PointerLikeTypeTraits for Clang types.
It may be an ODR violation, i.e. Clang may already have an instantiation of
the template in its own code.

rdar://problem/24284074
2016-02-06 11:22:28 -08:00