Commit Graph

77 Commits

Author SHA1 Message Date
Slava Pestov
23cac673ca IRGen: Enable dynamic replacement with library evolution
It looks like the only thing that fails is the linkage computation
for the dynamic replacement key of class methods. Even though
methods have hidden linkage to prevent them from being directly
referenced from outside a resilient module, we need to ensure
the dynamic replacement key is visible.

Fixes <rdar://problem/58457716>.
2020-04-10 22:53:36 -04:00
Doug Gregor
def86ce402 Revert "[irgen] Force emission of objc class refs for non-foreign clang objc classes whose metadata we use."
This reverts commit 8247525471. While
correct, it has uncovered several issues in existing code bases that
need to be sorted out before we can land it again.
Fixes rdar://problem/57846390.
2020-01-11 21:46:42 -08:00
Robert Widmann
b8779f7df7 Fix getDirectlyInheritedNominalTypeDecls
getDirectlyInheritedNominalTypeDecls looks for inherited protocols in two places: the actual inheritance clause of a declaration and the trailing where clause.  This works fine for normal declarations, but deserialized protocols that have Self constraints have no trailing where clause and appear to have no super-protocol bounds.  This means clients can potentially disagree about the structure of the protocol, and any symbols derived from it.

The test case demonstrates this problem directly: We build a hollowed-out SwiftUI preview provider then try to dynamically replace a requirement in a Self-constrained protocol extension.  The SwiftUI module sees the where clause, but when we go to deserialize the module in the "Preview" the protocol extension sees the wrong inheritance bounds and mis-mangles the call to Self.view(for:ofType).

The fix is to ask the requirement signature for Self requirements that would normally appear on a trailing where clause.

Resolves rdar://57150777
2019-12-03 17:01:57 -08:00
Michael Gottesman
8247525471 [irgen] Force emission of objc class refs for non-foreign clang objc classes whose metadata we use.
Today in far more cases we are using mangled strings to look up metadata at
runtime. If we do this for an objc class but for whatever reason we do not have
any other references to the class, the static linker will fail to link in the
relevant framework. The reason why this happens is that autolinking is treated
by the static linker as a hint that a framework may be needed rather than as a
"one must link against the framework". If there aren't any undefined symbols
needed by the app from that framework, the linker just will ignore the hint. Of
course this then causes the class lookup to fail at runtime when we use our
mangled name to try to lookup the class.

I included an Interpreter test as well as IRGen tests to make sure that we do
not regress here in the future.

NOTE: The test modifications here are due to my moving the ObjCClasses framework
out of ./test/Interpreters/Inputs => test/Inputs since I am using it in the
IRGen test along side the interpreter test.

rdar://56136123
2019-11-21 16:03:54 -08:00
Arnold Schwaighofer
7acabfe1cf IRGen: We need to emit metadata for types that are emitted with shared linkage
Otherwise one TU could only require the type descriptor without metadata
and another TU could require metadata and type descriptor. Whether the
metadata access function is available would then depend on the linking
order of the two TUs.

rdar://56929811
2019-11-11 10:00:17 -08:00
Mike Ash
93128d8fea [Runtime] Add missing FakeUnavailableSwiftDylib.swift file. 2019-08-14 15:01:58 -04:00
Joe Groff
f9e5de9371 IRGen: Reenable caching opaque type metadata accessors.
Dynamic replacement can only really get away with replacing opaque return types if a function's
opaque type has never been instantiated in the first place. Meanwhile, repeatedly instantiating
the metadata is too slow for many clients to tolerate. Fixes rdar://problem/53213600.
2019-07-17 20:15:09 -07:00
Arnold Schwaighofer
b233ca78c6 Int64 in more places 2019-07-12 18:05:53 -07:00
Arnold Schwaighofer
4300a589dc Fix test/Interpreter/dynamic_replacement_opaque_result.swift
rdar://53029978
2019-07-12 17:20:57 -07:00
Stephen Canon
ca1e808bde Replace all 9999 availability in non-stdlib tests. (#26109)
Replace all 9999 availability in non-stdlib tests with the appropriate platform availability.
2019-07-12 14:48:37 -04:00
Slava Pestov
cea8a6d6dc Split up test/IRGen/jit_metadata_strategy.swift and fix the REQUIRES: line 2019-07-08 15:08:47 -04:00
Slava Pestov
e2d660f148 SILGen: Fix generated vtable thunk when a final override is more visible than the base
Don't re-dispatch to the override's vtable slot if the override
is final; there's no vtable slot and this will result in an
infinite loop.

Fixes <rdar://problem/52006394>.
2019-06-21 22:56:48 -04:00
Slava Pestov
a31248997c SILGen: Correctly emit vtables when an override is more visible than the base
If an override B.f() is more visible than a base method A.f(), it is
possible that an override C.f() of B.f() cannot see the original method
A.f().

In this case, we would encounter linker errors if we referenced the
method descriptor or method dispatch thunk for A.f().

Make this work by treating B.f() as the least derived method in this
case, and ensuring that the vtable thunk for B.f() dispatches through
the vtable again.

Fixes <rdar://problem/48330571>, <https://bugs.swift.org/browse/SR-10648>.
2019-06-01 00:08:05 -04:00
Arnold Schwaighofer
7e646847c7 SILGen: Add an option to disable the previous implementation inside of dynamic replacement thunks
@_dynamicReplacement(for: selfRec(x: acc:))
  func selfRec_r(x: Int, acc: Int) -> Int {
    if x <= 0 {
      return acc
    }
    // Normally, this will call selfRec(x: acc:)'s implementation.
    // With the option, this will call to selfRec_r(x: acc:).
    return selfRec(x: x - 1, acc: acc + 1)
  }

rdar://51229650
2019-05-29 11:31:27 -07:00
Arnold Schwaighofer
b127aac1ce Merge pull request #24781 from aschwaighofer/fix_some_type_dynamic_replacement
Fix dynamic replacement of some type when used with associated types
2019-05-23 16:36:32 -07:00
Joe Groff
3c0732ab14 Add regression test for SR-10264 2019-05-22 15:14:03 -07:00
Arnold Schwaighofer
5c3a4d329b Fix dynamic replacement of some type when used with associated types
rdar://50638228
2019-05-18 10:34:52 -07:00
Joe Groff
cec9e9e33a Opaque types require a newer Swift runtime.
Check the availability of decls that declare an opaque return type to ensure they deploy to a
runtime that supports opaque types.

rdar://problem/50731151
2019-05-15 11:39:53 -07:00
Arnold Schwaighofer
a0387eea40 Opaque result types: Dynamic replacement fixes for computed properties 2019-04-22 08:32:43 -07:00
Arnold Schwaighofer
84c7b77d02 Make opaque type descriptors dynamically replaceable
This is to support dynamic function replacement of functions with opaque
result type.

This approach requires that all state is thrown away (that could contain the
old returned type for an opaque type) between replacements.

rdar://48887938
2019-04-22 07:31:07 -07:00
Arnold Schwaighofer
1b863d6787 Allow final on dynamic members
Dynamic is about dynamically replacing a method implementiation not
overriding.

rdar://49535048
2019-04-04 07:26:43 -07:00
Arnold Schwaighofer
35ca0e3423 Add support for dynamic replacement of didSet/willSet
The observer in a dynamic replacement of variables with a observer will
provide the dynamic replacement for the original.

var original : Int = 0 {
  didSet {
    print("original")
  }
}

@_dynamicReplacement(for: original)
var replacement : Int = 0 {
  didSet {
    print("replacement")
  }
}

rdar://48518788
2019-03-15 16:30:03 -07:00
Arnold Schwaighofer
8d9b9f328b SILGen: Fix the logic of dynamic replacements for class constructors
To correctly call designated super class initializers the designated
intializer (and not the allocator) is dynamically replaceable.
Convenience allocators are dynamically replaceable as before.
2019-02-22 10:15:06 -08:00
Arnold Schwaighofer
8394fc525e DynamicReplacement: Don't fail when a library is closed and reloaded
rdar://47560273
2019-01-25 13:50:27 -08:00
Arnold Schwaighofer
24b8766b31 Fix test 2018-11-15 10:56:08 -08:00
Arnold Schwaighofer
edd4f2ca6a Don't implicitly add dynamic to stored properties
This can introduce an exclusivity check failure.
2018-11-15 09:50:00 -08:00
Arnold Schwaighofer
b033d1b443 Calling the previous implementation does not work for global vars yet 2018-11-14 08:49:00 -08:00
Arnold Schwaighofer
081bb95bee Synthesize accessors for dynamic global variables 2018-11-14 07:57:45 -08:00
Arnold Schwaighofer
28da6b1075 Sema: Don't add dynamic to dynamic replacement decls
They can't be dynamic.

Add test for enable-implicit-dynamic flag.
2018-11-13 09:36:54 -08:00
Arnold Schwaighofer
fb7b223ba2 Add support to modify chaining behavior of dynamic replacements
Default to not chain dynamic replacements: Only one replacement and the
original implementation are active.
2018-11-09 13:17:09 -08:00
Arnold Schwaighofer
152e8db8bb IRGen and runtime implementation for dynamic replacements 2018-11-06 09:58:36 -08:00
Michael Gottesman
dd8edef2d5 [typelowering] Look through 1 level of optionality when determining if a value has a NewType representation.
This is necessary to ensure that we autorelease such values in objc thunks.
Previously, we were returning the value as unowned, leaking it. I added a test
to interpreter that will make sure in the future we do not leak like this
again.

rdar://45543138
2018-10-25 18:52:31 -07:00
Joe Groff
9f02ecd1a5 IRGen: Use any field of structs for extra inhabitants.
This allows us to layout-optimize Optional<T> when T is a struct with an
extra-inhabitant-bearing field anywhere in its definition, not only at
the beginning. rdar://problem/43019427
2018-08-14 12:53:06 -07:00
John McCall
c4abca3df4 Add a test case for an unresolvable dynamic metadata cycle. 2018-07-25 03:39:58 -04:00
Arnold Schwaighofer
b9728151df SILGen: Fix no escape verification if the closure throws an exception
It is safe to omit the retain/release dance in the reabstraction thunk
because we know we have an aditional reference outstanding for the
is_escaping verification.

The problem with throwing an objc exception inside a noescape closure is
that we verify the reference count of the closure sentinel. The
reabstraction thunk would increase the reference count call the
implementation function that then throws skipping the decrement.

rdar://40857699
2018-06-08 13:04:52 -07:00
Huon Wilson
250eb97af0 [test] Avoid defining symbols in headers.
This is incorrect, and also breaks TBD validation.
2018-05-23 15:39:21 +10:00
Jordan Rose
36eae9d4f6 [SILGen] Generate a trap for unexpected cases in all @objc enums
(both C enums and Swift enums declared @objc), because of the
"feature" in C of treating a value not declared as a case as a valid
value of an enum.  No more undefined behavior here!

This bit can go in separately from all the work on exhaustive/frozen
enums, which is still being discussed and will come later.

rdar://problem/20420436
2018-02-21 10:34:59 -08:00
Jordan Rose
c06cc016f9 [ClangImporter] Fix handling of bitfields in unions (#14412)
Previously this caused an assertion failure in +Asserts builds
and incorrect behavior in -Asserts builds.

rdar://problem/37242238
2018-02-05 18:12:58 -08:00
Arnold Schwaighofer
1832bcdfd5 Add test case that uses metadata of 'import as nested types' Objective-C types
This used to crash before we removed parent metadata references.

rdar://34846458
2017-10-10 09:32:14 -07:00
Arnold Schwaighofer
579360faf1 IRGen: Don't use global offset variables if we have a dependent layout and an Objective-C super class
We can't use global offset variables if we are generic and layout
dependent on a generic parameter because the objective-c layout might
depend on the alignment of the generic stored property ('t' in the
example below).

class Foo<T> : NSFoobar {
  var x : AKlass = AKlass()
  var y : AKlass = AKlass()
  var t : T?
}

SR-4687
rdar://31813495
2017-05-26 13:15:58 -07:00
Jordan Rose
23f25e1de7 In immediate mode, detect the host OS version on Apple platforms.
This allows script mode to pick up the current version of macOS
instead of defaulting to 10.9, making it unnecessary to write #available.
A -target flag can still override this if you're trying to write a
portable script.

The logic is a little tortured to avoid having to actually link to
Foundation.framework or libobjc.

Finishes rdar://problem/29433205.
2016-12-22 17:01:33 -08:00
Slava Pestov
0004bc480a IRGen: Differentiate between Swift.Bool and ObjCBool
SIL already does this where necessary, except with foreign throwing
functions; this patch changes Sema and the ClangImporter to give
them an ObjCBool foreign error result type explicitly.

This fixes a problem where calls to functions taking and returning
the C99 _Bool type were miscompiled on Mac OS X x86-64, because
IRGen was conflating the Objective-C BOOL type (which is a signed
char on some platforms) and C99 _Bool (which lowers as the LLVM
i1 type).

Fixes <rdar://problem/26506458> and <rdar://problem/27365520>.
2016-08-10 10:34:24 -07:00
Slava Pestov
992b68b116 Add tests for failable and throwing designated initializers in @objc classes
Recently this was made to work, but we only had tests for the
pure-Swift cases.

Finishes off <rdar://problem/23104261>.
2016-08-03 19:55:18 -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
Jordan Rose
e4118f19c8 Port tests to 'fileprivate'.
Similar to apple/swift#3753.

Groundwork for SE-0025 ('private' and 'fileprivate').
No intended functionality change.
2016-07-25 20:20:58 -07:00
Doug Gregor
823c24b355 [SE-0112] Rename ErrorProtocol to Error.
This is bullet (5) of the proposed solution in SE-0112, and the last
major piece to be implemented.
2016-07-12 10:53:52 -07:00
Jordan Rose
3b6e40c030 Use ClassDecl::ForeignKind to model Clang's objc_runtime_visible.
We're now correctly checking for inheritance, adding @objc methods,
and adding @objc protocols for both CF types and objc_runtime_visible
classes (those without visible symbols). The latter is used for some
of the types in Dispatch, which has exposed some of the classes that
were considered implementation details on past OSs.

We still don't properly implement using 'as?' to check conformance to
a Swift protocol for a CF or objc_runtime_visible type, but we can do
that later.

rdar://problem/26850367
2016-06-30 11:20:58 -07:00
David Farler
591761e22d Merge pull request #2820 from bitjammer/weak-extern-c-functions
Ask SILFunction link entities' clang nodes for weakness when applicable
2016-06-01 14:13:15 -07:00
David Farler
8ca30ff2d7 Ask SILFunction link entities' clang nodes for weakness when applicable
When importing C functions with availability attributes, we don't
properly use that information to decide whether a symbol should be
weak_extern, causing load failures in dylibs that reference these
symbols when deployed to an older OS.

This is a very targeted fix and we need a better architecture for
deciding this.

rdar://problem/26359452
2016-06-01 12:52:04 -07:00
John McCall
6328f3fe0b Explicitly represent "pseudo-generic" functions in SIL, which do
not have access to their type arguments at runtime.  Use this to
fix the emission of native thunks for imported ObjC-generic
initializers, since they may need to perform bridging.

For now, pseudo-genericity is all-or-nothing, but we may want to
make it apply only to certain type arguments.

Also, clean up some code that was using dead mangling nodes.
2016-06-01 11:41:27 -07:00