Commit Graph

39091 Commits

Author SHA1 Message Date
Alexis Laferrière
685d35b7d1 Merge pull request #30306 from xymus/print-objc-ext
[PrintAsObjC] Don't print imports for empty extensions
2020-03-09 16:52:18 -07:00
Andrew Trick
c34625779b Disable LetPropertiesOpt on struct 'let's.
This pass makes assumptions about the visibility of a type's memory
based on the visibility of its properties. This is the wrong way to
think about memory visibility.

Fixes <rdar://57564377> Struct component with 'let', unexpected
behavior

This pass wants assume that the contents of a property is known based
on whether the property is declared as a 'let' and the visibility of
the initializers that access the property. For example:

```
public struct X<T> {
  public let hidden: T

  init(t: T) { self.hidden = t }
}
```

The pass currently assumes that `X` only takes on values that are
assigned by the invocations of `X.init`, which is only visible in `X`s
module. This is wrong if the layout of `Impl` is exposed to other
modules. A struct's memory may be initialized by any module with
access to the struct's layout.

In fact, this assumption is wrong even if the struct, and it's let
property cannot be accessed externally by name. In this next example,
external modules cannot access `Impl` or `Impl.hidden` by name, but
can still access the memory.

```
internal struct Impl<T> {
  let hidden: T

  init(t: T) { self.hidden = t }
}

public struct Wrapper<T> {
  var impl: Impl<T>

  public var property: T {
    get {
      return impl.hidden
    }
  }
}
```

As long as `Wrapper`s layout is exposed to other modules, the contents
of `Wrapper`, `Impl`, and `hidden' can all be initialized in another
module

```
// Legal as long Wrapper's home module is *not* built with library evolution
// (or if Wrapper is declared `@frozen`).
func inExternalModule(buffer: UnsafeRawPointer) -> Wrapper<Int64> {
  return buffer.load(as: Wrapper<Int64>.self)
}
```

If library evolution is enabled and a `public` struct is not declared
`@frozen` then external modules cannot assume its layout, and therefore
cannot initialize the struct memory. In that case, it is possible to optimize
`X.hidden` and `Impl.hidden` as if the properties are only initialized inside
their home module.

The right way to view a type's memory visibility is to consider whether
external modules have access to the layout of the type. If not, then the
property can still be optimized As long as a struct is never enclosed in a
public effectively-`@frozen` type. However, finding all places where a struct
is explicitly created is still insufficient. Instead, the optimization needs
to find all uses of enclosing types and determine if every use has a known
constant initialization, or is simply copied from another value. If an
escaping unsafe pointer to any enclosing type is created, then the
optimization is not valid.

When viewed this way, the fact that a property is declared 'let' is mostly
irrelevant to this optimization--it can be expanded to handle non-'let'
properties. The more salient feature is whether the propery has a public
setter.

For now, this optimization only recognizes class properties because class
properties are only accessibly via a ref_element_addr instruction. This is a
side effect of the fact that accessing a class property requires a "formal
access". This means that begin_access marker must be emitted directly on the
address produced by a ref_element_addr. Struct properties are not handled, as
explained above, because they can be indirectly accessed via addresses of
outer types.
2020-03-09 16:20:01 -07:00
Michael Gottesman
6fee59bd6a [semantic-arc-opts] Implement @owned phi web elimination for phi webs with a single phi node that only have copy_value introducers.
This is the most simple initial version that I can commit. The hope is that this will help to bring this up in a nice way.

I am going to handle the multiple phi node and load [copy] case later to reduce
code churn.

<rdar://problem/56720436>
2020-03-09 16:04:48 -07:00
Rintaro Ishizaki
8f7340cd6e [CodeCompletion] Re-typecheck TypeExpr without call arguments
Call arguments sometimes affect the inference for the generic parameters of the
type expression. When we want to show all initializers from all
extensions, we do not want to infer any generic arguments.

rdar://problem/53516588
2020-03-09 15:48:54 -07:00
Alexis Laferrière
4e8867e021 [PrintAsObjC] Test that we don't leak extended implementationOnly types 2020-03-09 14:11:06 -07:00
Michael Gottesman
7c403ed4df [temp-rvalueopt] Teach how to promote fix_lifetime.
In the case of copy_addr, we move it onto the source address and in the case of
a store, put it on the source object.

I just noted this pattern happening a bunch in the stdlib when I was looking
through it for ownership patterns.
2020-03-09 12:29:56 -07:00
Harlan Haskins
2e4d315cad Merge pull request #30287 from HassanElDesouky/SR-12311
[SR-12311] Improve diagnostic for read-only properties
2020-03-09 07:36:56 -07:00
swift-ci
883d546dda Merge pull request #30291 from gottesmm/pr-20d3a11a596c39f79b461a829ff03722b9755ec9 2020-03-09 01:02:03 -07:00
Doug Gregor
ce97d22f21 [Function builders] Add support for buildFinalResult().
When present in a function builder, buildFinalResult() will be called on
the value of the outermost block to form the final result of the closure.
This allows one to collapse the full function builder computation into
a single result without having to do it in each buildBlock() call.
2020-03-09 00:10:07 -07:00
Michael Gottesman
e9896fb3da [ownership] Change init_existential_ref to be forwarding for both guaranteed and owned.
Otherwise in call frames like the one in the test in this commit get unneeded
ARC traffic. We should never pessimize read only code that doesnt need
side-effects with side-effects if we can avoid it.

I am seeing this a bunch when I look at SIL from projects that use a lot of
protocols. Specifically, one has a sort of trampoline code that wraps a ref
counted object in an existential ref container (which from an ARC perspective
doesn't imply ownership) and then calls a method on it or passes it off to some
other code.

Because of this requirement, there is a copy/destroy that can not be eliminated
unless we can devirt/inline/eliminate the init_existential_ref box, inline
enough that the low level ARC optimizer can hit it. We shouldn't rely on such
properties if we do not need to.
2020-03-08 23:21:57 -07:00
Michael Gottesman
dae4c0b015 [temp-rvalue] Teach how to optimize unchecked_take_enum_data_addr of an Optional type.
unchecked_take_enum_data_addr only writes to memory in certain cases. Optional
is not one of those cases luckily.

Given that I am adding support here just for optionals so that we can get rid of
temporaries that are used with a switch_enum_addr. This is an example of a case
where we need to eliminate temporary allocations to allow for Semantic ARC Opts
to eliminate further traffic.
2020-03-08 19:54:19 -07:00
Hassan El Desouky
d0b6d9f2c1 Improve diagnostic for read-only properties
Make the message within 80 columns width
Improve diagnostic for read-only properties
Improve diagnostic for read-only properties
Improve diagnostic for read-only properties
2020-03-09 04:48:54 +02:00
Luciano Almeida
5aaf2ce7b6 [tests] Fix broken Misc/stats_dir_tracer.swift tests 2020-03-08 23:33:05 -03:00
Luciano Almeida
6ca7e383a9 [tests] Add more test cases for SR-12019 dynamic callable 2020-03-08 22:56:26 -03:00
Pavel Yaskevich
a1987ec274 Merge pull request #30283 from xedin/rdar-60185506
Revert "[ConstraintSystem] Make it possible to infer subtype bindings…
2020-03-07 22:34:10 -08:00
Kuba (Brecka) Mracek
b2edf20a81 Merge pull request #30112 from apple/mracek/arm64e
Add arm64e and pointer authentication support for Swift
2020-03-07 20:38:36 -08:00
Pavel Yaskevich
135287871b [TypeChecker] NFC: Add a test-case for rdar://problem/60185506 2020-03-07 20:37:34 -08:00
Pavel Yaskevich
0ecedfa5ea Revert "[ConstraintSystem] Make it possible to infer subtype bindings through argument conversions"
Reverts apple/swift#30006. It caused a regression that we'd like to address before re-landing:

```swift
struct X {
  var cgf: CGFloat
}

func test(x: X?) {
  let _ = (x?.cgf ?? 0) <= 0.5
}
```

This reverts commit 0a6b444b49.
This reverts commit ed255596a6.
This reverts commit 3e01160a2f.
This reverts commit 96297b7e39.

Resolves: rdar://problem/60185506
2020-03-07 20:16:56 -08:00
John McCall
1f62fe591f Merge pull request #30272 from rjmccall/invocation-sil-function-subs
Distinguish invocation and pattern substitutions on SILFunctionType
2020-03-07 18:45:29 -05:00
John McCall
ceff414820 Distinguish invocation and pattern substitutions on SILFunctionType.
In order to allow this, I've had to rework the syntax of substituted function types; what was previously spelled `<T> in () -> T for <X>` is now spelled `@substituted <T> () -> T for <X>`.  I think this is a nice improvement for readability, but it did require me to churn a lot of test cases.

Distinguishing the substitutions has two chief advantages over the existing representation.  First, the semantics seem quite a bit clearer at use points; the `implicit` bit was very subtle and not always obvious how to use.  More importantly, it allows the expression of generic function types that must satisfy a particular generic abstraction pattern, which was otherwise impossible to express.

As an example of the latter, consider the following protocol conformance:

```
protocol P { func foo() }
struct A<T> : P { func foo() {} }
```

The lowered signature of `P.foo` is `<Self: P> (@in_guaranteed Self) -> ()`.  Without this change, the lowered signature of `A.foo`'s witness would be `<T> (@in_guaranteed A<T>) -> ()`, which does not preserve information about the conformance substitution in any useful way.  With this change, the lowered signature of this witness could be `<T> @substituted <Self: P> (@in_guaranteed Self) -> () for <A<T>>`, which nicely preserves the exact substitutions which relate the witness to the requirement.

When we adopt this, it will both obviate the need for the special witness-table conformance field in SILFunctionType and make it far simpler for the SILOptimizer to devirtualize witness methods.  This patch does not actually take that step, however; it merely makes it possible to do so.

As another piece of unfinished business, while `SILFunctionType::substGenericArgs()` conceptually ought to simply set the given substitutions as the invocation substitutions, that would disturb a number of places that expect that method to produce an unsubstituted type.  This patch only set invocation arguments when the generic type is a substituted type, which we currently never produce in type-lowering.

My plan is to start by producing substituted function types for accessors.  Accessors are an important case because the coroutine continuation function is essentially an implicit component of the function type which the current substitution rules simply erase the intended abstraction of.  They're also used in narrower ways that should exercise less of the optimizer.
2020-03-07 16:25:59 -05:00
Luciano Almeida
c1e517a798 [tests] Adding tests for SR-12019 2020-03-07 16:52:47 -03:00
Argyrios Kyrtzidis
85a5e5a493 [CMake] Move the _add_swift_target_library_single() call for BlocksRuntimeStub under the SWIFT_BUILD_STDLIB check
Since the stub for BlocksRuntime library is to "be used for the reflection tests", don't set it up unless the reflecting tests are also setup.
2020-03-06 23:22:22 -08:00
Xi Ge
a09b6418a0 Merge pull request #30267 from nkcsgexi/tbd-category
TBDGen: avoid emitting symbols with OBJC_CLASS and OBJC_METACLASS prefixes
2020-03-06 18:48:52 -08:00
Xi Ge
699e05712f TBDGen: avoid emitting symbols with OBJC_CLASS and OBJC_METACLASS prefixes
rdar://60057489
2020-03-06 16:07:57 -08:00
Kuba (Brecka) Mracek
ab6533a40f Merge branch 'master' into mracek/arm64e 2020-03-06 15:07:01 -08:00
Slava Pestov
adbf8da7c2 Merge pull request #23489 from AnthonyLatsis/where-clause-nongeneric-decl
[SE] Allow where clauses on non-generic declarations in generic contexts
2020-03-06 17:53:14 -05:00
tbkka
0d361bd3ea Teach RemoteMirror how to project enum values (#30161)
Teach RemoteMirror how to project enum values

This adds two new functions to the SwiftRemoteMirror
facility that support inspecting enum values.

Currently, these support non-payload enums and
single-payload enums, including nested enums and
payloads with struct, tuple, and reference payloads.
In particular, it handles nested `Optional` types.

TODO: Multi-payload enums use different strategies for
encoding the cases that aren't yet supported by this
code.

Note: This relies on information from dataLayoutQuery
to correctly decode invalid pointer values that are used
to encode enums.  Existing clients will need to augment
their DLQ functions before using these new APIs.

Resolves rdar://59961527

```
/// Projects the value of an enum.
///
/// Takes the address and typeref for an enum and determines the
/// index of the currently-selected case within the enum.
///
/// Returns true iff the enum case could be successfully determined.
/// In particular, note that this code may fail for valid in-memory data
/// if the compiler is using a strategy we do not yet understand.
SWIFT_REMOTE_MIRROR_LINKAGE
int swift_reflection_projectEnumValue(SwiftReflectionContextRef ContextRef,
                                      swift_addr_t EnumAddress,
                                      swift_typeref_t EnumTypeRef,
                                      uint64_t *CaseIndex);

/// Finds information about a particular enum case.
///
/// Given an enum typeref and index of a case, returns:
/// * Typeref of the associated payload or zero if there is no payload
/// * Name of the case if known.
///
/// The Name points to a freshly-allocated C string on the heap.  You
/// are responsible for freeing the string (via `free()`) when you are finished.
SWIFT_REMOTE_MIRROR_LINKAGE
int swift_reflection_getEnumCaseTypeRef(SwiftReflectionContextRef ContextRef,
                                        swift_typeref_t EnumTypeRef,
                                        unsigned CaseIndex,
                                        char **CaseName,
                                        swift_typeref_t *PayloadTypeRef);
```


Co-authored-by: Mike Ash <mikeash@apple.com>
2020-03-06 13:17:40 -08:00
Michael Ilseman
9cabe077f4 Merge pull request #30263 from milseman/malloc_san
[test] Disable allocator-specific test portion
2020-03-06 11:47:27 -08:00
Michael Ilseman
5236224eb1 [test] Disable allocator-specific test portion
Disagble a portion of a test with hard-coded assumptions about the
result of malloc_size.
2020-03-06 09:37:55 -08:00
Michael Ilseman
c4f1b83a5a Merge pull request #30259 from milseman/backspective
[test] Disable introspective test in back deployment
2020-03-06 09:16:56 -08:00
lhames
1d5c008f97 Merge pull request #29863 from lhames/switch-imm-to-lljit
[Immediate] Switch immediate mode from MCJIT to LLJIT.
2020-03-06 09:12:27 -08:00
Doug Gregor
b2facd0331 Merge pull request #30258 from DougGregor/constraint-pattern-locators
[Constraint system] Clean up constraints associated with patterns.
2020-03-06 09:11:15 -08:00
Michael Ilseman
8d5d3815a1 Merge pull request #30180 from benrimmington/se-0263-test
[SE-0263] Add test, rename API, update docs
2020-03-06 08:54:24 -08:00
Joe Groff
58950a576a Merge pull request #30250 from jckarter/mangle-objc-runtime-names
IRGen: Generate runtime type manglings using ObjC runtime names.
2020-03-06 07:33:19 -08:00
Michael Ilseman
633d46ff26 [test] Disable introspective test in back deployment 2020-03-06 07:19:08 -08:00
Brent Royal-Gordon
2cc9446482 Merge pull request #29412 from brentdax/two-revisions-for-slashing
[SE-0274] Update concise #file implementation in response to first round of review
2020-03-06 02:56:16 -08:00
Michael Gottesman
c4ed7bd0be Merge pull request #30249 from gottesmm/pr-1c59567660f53bc35248d864c54d25e2760a74d0
[ownership] Implement InteriorPointer abstraction/validate current recognized ones addresses are not used outside of parent object's borrowed lifetime.
2020-03-06 02:54:29 -08:00
Brent Royal-Gordon
62ab3dfd64 Temporarily disable new #file test on Windows 2020-03-05 23:30:37 -08:00
Brent Royal-Gordon
85599f7582 Keep gyb from messing up line endings on Windows
GYB opens its inputs and outputs in text mode. This is a problem because the recommendation in Windows is to check everything out in LF mode to avoid breaking tests, so gybbing a file ends up converting LFs to CRLFs. That broke test/SILGen/magic_identifier_file_conflicting.swift.gyb.

Modify GYB to open its files in binary mode instead of text mode. This is a no-op everywhere but Windows.

Note that this change is incomplete: it’s somewhat difficult to switch stdin and stdout to binary mode in Python 2. I’ve added FIXME comments about this.
2020-03-05 23:30:37 -08:00
Brent Royal-Gordon
d590823f3c Warn about conflicts between #file strings 2020-03-05 23:30:37 -08:00
Doug Gregor
1a981f917c [Constraint system] Clean up constraints associated with patterns.
Wherever we have constraints that involve pattern matching, use the
PatternMatch locator element. Additionally, don't use the TupleElement
locator element for tuple patterns, because it violates assumptions used
for diagnostics.

The new test was crashing; now it has a terrible diagnostic for which I
need to think harder about a fix.
2020-03-05 23:06:21 -08:00
Doug Gregor
60cf68fcfe Merge pull request #30254 from DougGregor/cursed-dual-identities
[Clang importer] Suppress method imports found via selector lookup.
2020-03-05 22:59:21 -08:00
Doug Gregor
6a5069d017 Merge pull request #30245 from xedin/rdar-60061646
[ConstraintSystem] Make sure pattern matching tuple destructuring works both ways
2020-03-05 21:48:30 -08:00
swift-ci
4dad6c5954 Merge pull request #30255 from ravikandhadai/skip-tsan-instrumentation-in-consteval 2020-03-05 21:25:52 -08:00
Michael Gottesman
0aec5a7f43 [ownership] Implement InteriorPointer abstraction/validate current recognized ones addresses are not used outside of parent object's borrowed lifetime.
Currently, we only classify ref_element_addr and ref_tail_addr. But we should
expand this to project_box, project_existential_box and open_existential_box as
well.
2020-03-05 20:28:59 -08:00
Michael Ilseman
79bac4e6a3 Merge pull request #30244 from milseman/string_shrink
[string] Shrink storage class sizes
2020-03-05 19:57:40 -08:00
Ravi Kandhadai
fd77ae9a9c [Constant Evaluator] Make the constant evaluator skip tsan
instrumentation builtin: tsanInoutAccess
2020-03-05 18:52:30 -08:00
Doug Gregor
c84bec752f [Clang importer] Suppress method imports found via selector lookup.
When we found Objective-C methods via selector lookup, we would skip the
“should we even try to import this?” check. If the Swift-name-as-derived-
from-Objective-C is different from the actual Swift name, we might try to
(redundantly) import something from the generated Objective-C header,
which can lead to crashes. The specific method causing problems was defined
like this in Swift:

    @objc public class func using(index: AnyObject) -> AnyObject? { return nil }

which produced an Objective-C method like this:

    + (id _Nullable)usingIndex:(id _Nonnull)index;

The Swift name derived from the Objective-C method is `usingIndex(_:)`, which
of course does not match `using(index:)`, meaning that we think these two
methods are different (they aren’t).

The safe fix is to check whether we should import a given Objective-C method
declaration along the found-via-Objective-C-selector path, like we do with
normal name lookup. A longer term fix is to emit swift_name attributes for
such methods.

Fixes the fiendish Clang importer crash in rdar://problem/60046206.
2020-03-05 18:34:29 -08:00
Xi Ge
93db1619c6 Merge pull request #30252 from nkcsgexi/add-enum-case-break
APIChecker: adding enum cases are source breaking changes
2020-03-05 18:29:02 -08:00
Owen Voorhees
d68089dcf9 [Diagnostics] Add a basic terminal markdown printer for educational notes
Currently the printer only supports the subset of markdown used by
educational notes. It bolds headers, improves the appearance of lists
and horizontal rules, and does basic indenting of blockquotes and
code blocks.
2020-03-05 18:04:32 -08:00