Commit Graph

49 Commits

Author SHA1 Message Date
Becca Royal-Gordon
f26a6fb985 [PrintAsClang] Tweak fallback member sort rules
Compare the names of all extension members first, before attempting weirder and more expensive comparisons like stringified type and mangled name. This gives us a sort order that’s a little more comprehensible to humans.
2025-02-18 17:46:20 -08:00
Becca Royal-Gordon
da07ff577c [PrintAsClang] Warn about unstable decl order
PrintAsClang is supposed to emit declarations in the same order regardless of the compiler’s internal state, but we have repeatedly found that our current criteria are inadequate, resulting in non-functionality-affecting changes to generated header content. Add a diagnostic that’s emitted when this happens soliciting a bug report.

Since there *should* be no cases where the compiler fails to order declarations, this diagnostic is never actually emitted. Instead, we test this change by enabling `-verify` on nearly all PrintAsClang tests to make sure they are unaffected.

This did demonstrate a missing criterion that only mattered in C++ mode: extensions that varied only in their generic signature were not sorted stably. Add a sort criterion for this.
2025-02-14 21:41:36 -08:00
Becca Royal-Gordon
d2100b7a67 [PrintAsClang] Beef up extension sorting rule
A couple of the rules that `ModuleContentsWriter::write()` uses to sort declarations didn’t actually work because of an incorrect predicate. In addition, there were a number of situations that could come up in C++ interop (where overloading is permitted) where extensions could not be sorted. Rework extension sorting to look for more kinds of differences between extension members.
2025-02-14 21:40:48 -08:00
Becca Royal-Gordon
60f6afb76c Correct newline emission in generated headers
Eliminates extraneous newlines between top-level Objective-C declarations in `-emit-objc-header` headers. Specifically, there should now always be exactly one—no more, no less—empty line between `@end` and whatever follows it.

Besides being more aesthetically pleasing, this eliminates ordering-dependent behavior where PrintAsClang would print an extra newline when visiting an empty extension, which meant that the order in which empty and non-empty extensions were visited during printing could result in whitespace differences in the compiler output. Printing the blank line is now conditional on whether `tell()` indicates that characters were actually written to the output.

Fixes rdar://143533893.
2025-01-24 16:26:33 -08:00
Becca Royal-Gordon
5d6ca1c10a Partially revert “Support @objc(CustomName) on extensions”
Revert portions of 07b9fe9ce6 to temporarily avoid applying `@objc(CustomName) extension`s to generated header category names.

Fixes rdar://135924149.
2024-09-12 23:15:26 -07:00
Becca Royal-Gordon
c3b57f24eb Merge pull request #74597 from beccadax/order-to-chaos
[NFC] [PrintAsClang] Add tiebreaking rule to sort
2024-06-21 21:56:54 -07:00
Becca Royal-Gordon
bf92156364 [NFC] [PrintAsClang] Add tiebreaking rule to sort
Because the underlying API for fetching top-level decls returns them in an unspecified order, PrintAsClang sorts the decls before printing them to make the output order more stable. However, the rules currently implemented have at least one known defect (they compare only the unqualified name of a nested class, so two nested classes with the same Swift name sort in an arbitrary order), and there are likely many more.

Add a fallback rule which sorts declarations by their mangled name; this should at least distinguish all non-colliding ValueDecls from each other, albeit according to fairly opaque criteria. Additionally add a rule to help distinguish extensions with very similar content, and tweak other logic so that the comparison function is less likely to give up early rather than continuing to look for a usable difference.

Fixes rdar://129485103.
2024-06-20 16:17:07 -07:00
Nate Chandler
2d03664e45 [TypeLowering] Return pseudogeneric @autoreleasing
Previously, when a pseudogeneric function returned a type parameter, it
was unowned.  That resulted in invalid OSSA out of SILGen.  Here, this
is fixed to use the @autoreleasing convention.

rdar://64375208
2024-05-31 18:42:25 -07:00
Becca Royal-Gordon
07b9fe9ce6 Support @objc(CustomName) on extensions
This now specifies a category name that’s used in TBDGen, IRGen, and PrintAsClang. There are also now category name conflict diagnostics; these subsume some @implementation diagnostics.

(It turns out there was already a check for @objc(CustomName) to make sure it wasn’t a selector!)
2024-05-16 13:40:13 -07:00
Andrew Trick
0a020815ad PrintAsObjC test is broken. Add comments. 2021-03-02 22:20:13 -08:00
Andrew Trick
b689b1dabe Rename GuaranteedARCOpts to MandatoryARCOpts.
This bleeds into the implementation where "guaranteed" is used
everywhere to talk about optimization of guaranteed values. We need to
use mandatory to indicate we're talking about the pass pipeline.
2021-03-02 22:20:13 -08:00
Michael Gottesman
702c1bc5e8 [arc] Change guaranteed arc opts to be based on SemanticARCOpts and move from Diagnostic pipeline -> Onone pipeline.
The pass is already not being run during normal compilation scenarios today
since it bails on OSSA except in certain bit-rot situations where a test wasn't
updated and so was inadvertently invoking the pass. I discovered these while
originally just trying to eliminate the pass from the diagnostic pipeline. The
reason why I am doing this in one larger change is that I found there were a
bunch of sil tests inadvertently relying on guaranteed arc opts to eliminate
copy traffic. So, if I just removed this and did this in two steps, I would
basically be unoptimizing then re-optimizing the tests.

Some notes:

1. The new guaranteed arc opts is based off of SemanticARCOpts and runs only on
   ossa. Specifically, in this new pass, we just perform simple
   canonicalizations that do not involve any significant analysis. Some
   examples: a copy_value all of whose uses are destroys. This will do what the
   original pass did and more without more compile time. I did a conservative
   first approximation, but we can probably tune this a bit.

2. the reason why I am doing this now is that I was trying to eliminate the
   enable-ownership-stripping-after-serialization flag and discovered that the
   test opaque_value_mandatory implicitly depends on this since sil-opt by
   default was the only place left in the compiler with that option set to false
   by default. So I am eliminating that dependency before I land the larger
   change.
2020-06-15 17:00:18 -07:00
Brent Royal-Gordon
f1e8ad9348 Reenable PrintAsObjC/extensions.swift
This test was disabled to work around a bug in the ObjC generics implementation in clang. That bug was corrected by https://reviews.llvm.org/D66696, so we should now be able to turn the Swift test back on.
2019-10-16 10:10:01 -07:00
Brent Royal-Gordon
f696ebdae5 XFAIL PrintAsObjC/extensions.swift
rdar://problem/54414986
2019-08-16 17:41:00 -07:00
Michael Gottesman
f854547c55 [ownership] Enable ownership verification by default.
I also removed the -verify-sil-ownership flag in favor of a disable flag
-disable-sil-ownership-verifier. I used this on only two tests that still need
work to get them to pass with ownership, but whose problems are well understood,
small corner cases. I am going to fix them in follow on commits. I detail them
below:

1. SILOptimizer/definite_init_inout_super_init.swift. This is a test case where
DI is supposed to error. The only problem is that we crash before we error since
the code emitting by SILGen to trigger this error does not pass ownership
invariants. I have spoken with JoeG about this and he suggested that I fix this
earlier in the compiler. Since we do not run the ownership verifier without
asserts enabled, this should not affect compiler users. Given that it has
triggered DI errors previously I think it is safe to disable ownership here.

2. PrintAsObjC/extensions.swift. In this case, the signature generated by type
lowering for one of the thunks here uses an unsafe +0 return value instead of
doing an autorelease return. The ownership checker rightly flags this leak. This
is going to require either an AST level change or a change to TypeLowering. I
think it is safe to turn this off since it is such a corner case that it was
found by a test that has nothing to do with it.

rdar://43398898
2019-03-25 00:11:52 -07:00
Slava Pestov
5964c00091 Migrate PrintAsObjC tests to Swift 4
I didn't migrate test/PrintAsObjC/versioned.swift, because it
explicitly checks the Swift 3 projection of some APIs.
2018-06-26 16:57:04 -07:00
Slava Pestov
5d2752f7d2 Run tests with -swift-version 4 by default
Some test now fail, so add an explicit -swift-version 3.
2018-06-19 23:24:19 -07:00
Dmitri Gribenko
984210aa53 tests: replace '// RUN: rm -rf' '// RUN: mkdir' pairs with '%empty-directory(...)'
These changes were made using a script.
2017-06-04 11:08:39 -07:00
Doug Gregor
80e9491fe3 Make Swift-3-inferred @objc explicit in test cases 2017-03-31 21:22:15 -07:00
Caleb Davenport
5e5b1540eb Restore test to ensure an extended imported class is not redefined. 2017-03-06 16:46:31 -05:00
Caleb Davenport
617167ad96 Remove redundant protocol conformance test.
This test was designed to ensure that extensions that only add a
non-objc-compatible protocol are not printed in a compatibility header.
That case is already covered by the `extension NSString : NotObjC {}`
test in `PrintAsObjC/protocols.swift`.
2017-03-06 16:37:27 -05:00
Caleb Davenport
46475c8257 Add a few more failing tests.
- Extension that only contains non-objc-compatible members
- Extensions that only add non-objc-compatible protocol conformances
2017-03-06 15:46:48 -05:00
Caleb Davenport
cf420e4012 Add failing tests. 2017-03-05 18:22:43 -05:00
Jordan Rose
79b5ea022d [PrintAsObjC] Handle generic parameters in extensions. (#7818)
Most of the time, "generics" means "cannot be exposed to Objective-C"
and certainly "cannot be exposed in the generated header", but there
is one exception: imported Objective-C parameterized types, and their
extensions. We were previously dropping this on the floor and printing
`Foo</* BarType */>` in the generated header, which is nonsense.

https://bugs.swift.org/browse/SR-3480
2017-02-28 12:52:10 -08:00
Tim Bodeit
96c964ee0d [test/PrintAsObjc] Adjust existing test cases for SWIFT_WARN_UNUSED_RESULT 2016-12-10 01:48:49 +01:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Dmitri Gribenko
55864d10cb Tests: use 'mkdir -p' 2016-09-02 21:36:45 -07:00
Jordan Rose
3a0a07d666 [PrintAsObjC] Handle forward-declarations for 'Class <SomeProto>'.
Merge pull request #4219 from jrose-apple/PrintAsObjC-existential-metatypes-are-metatypes-too

rdar://problem/27746149
2016-08-11 11:41:44 -07:00
Dmitri Gribenko
d175b3b66d Migrate FileCheck to %FileCheck in tests 2016-08-10 23:52:02 -07:00
Jordan Rose
ca009b7604 [PrintAsObjC] Handle forward-declarations for 'Class <SomeProto>'.
rdar://problem/27746149
2016-08-10 19:45:51 -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
Kevin Ballard
66369468aa Use new-style _Nullable keywords in obj-c header
Use the keywords `_Nullable`, `_Nonnull`, and `_Null_unspecified`
instead of the older compatibility forms `__nullable`, `__nonnull`, and
`__null_unspecified`.

Part of rdar://problem/23614638
2015-12-05 19:54:27 -08:00
Jordan Rose
df93e366f1 [PrintAsObjC] Make sure to use a class's custom name when extended or subclassed.
Otherwise, we end up with an invalid generated header.

rdar://problem/21929752

Swift SVN r30474
2015-07-21 23:56:35 +00:00
Jordan Rose
8106a11dac Disallow @objc on non-ObjC-rooted classes.
These classes don't show up well in generated headers (rdar://problem/20855568),
can't actually be allocated from Objective-C (rdar://problem/17184317), and
make the story of "what is exposed to Objective-C" more complicated. Better
to just disallow them.

All classes are still "id-compatible" in that they can be converted to
AnyObject and passed to Objective-C, they secretly implement NSObjectProtocol
(via our SwiftObject root class), and their members can still be individually
exposed to Objective-C.

The frontend flag -disable-objc-attr-requires-foundation-module will disable
this requirement as well, which is still necessary for both the standard
library and a variety of tests I didn't feel like transforming.

Swift SVN r29760
2015-06-27 16:27:56 +00:00
Dmitri Hrybenko
f43843f25c tests: use the new substitution for the mock SDK
This is required to correctly use the mock SDK when the SDK overlay is
built and tested separately.  (Otherwise, the mock SDK might not get
used, because the overlay SDK options would expand from the
%-substitution, appear first on the command line, and shadow the mock
SDK in the search path).

Swift SVN r25185
2015-02-11 18:57:29 +00:00
Graham Batty
83b4384fac Update test flags for linux failures and support.
Also removed the sdk 'feature' in favour of the more specific
objc_interop.

Swift SVN r24856
2015-01-30 21:31:48 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +00:00
Dmitri Hrybenko
1eea220932 Use one module cache directory for all the lit tests to speed them up
Doing so is safe even though we have mock SDK.  The include paths for
modules with the same name in the real and mock SDKs are different, and
the module files will be distinct (because they will have a different
hash).

This reduces test runtime on OS X by 30% and brings it under a minute on
a 16-core machine.

This also uncovered some problems with some tests -- even when run for
iOS configurations, some tests would still run with macosx triple.  I
fixed the tests where I noticed this issue.

rdar://problem/19125022

Swift SVN r23683
2014-12-04 11:21:48 +00:00
Graham Batty
83f27a8af7 Revert "Mark tests that don't pass on linux as XFAIL."
This reverts commit 2711ca86de7bf6a7885ccea24219a48a590b1e95.

Swift SVN r23577
2014-11-24 17:42:13 +00:00
Graham Batty
198402dcfe Mark tests that don't pass on linux as XFAIL.
Swift SVN r23573
2014-11-24 17:40:37 +00:00
Doug Gregor
96a9751372 PrintAsObjC: Print nullability type specifiers to capture optionality.
Part of rdar://problem/19005653. There is still some work to do to
produce pretty output here, but the current output is correct.

Swift SVN r23444
2014-11-19 21:08:23 +00:00
Jordan Rose
1c7b8f2972 [PrintAsObjC] Don't print nested classes as nested in Objective-C.
There are still problems with nested classes:
- They're much more likely to have colliding compile-time names
  (since the outer class's name is dropped).
- They're only picked up if the outer class is also @objc.

But at least now we won't generate invalid Objective-C. Unless the inner
classes have the same name.

rdar://problem/18187877

Swift SVN r21677
2014-09-03 18:18:59 +00:00
Jordan Rose
0331b7298c [PrintAsObjC] Don't print extensions of CF types.
Although Swift thinks of these as Objective-C classes, Objective-C itself
does not.

<rdar://problem/17867708>

Swift SVN r20867
2014-08-01 00:26:58 +00:00
Jordan Rose
c90cd11aff [PrintAsObjC] Only include internal decls if we have a bridging header.
The upshot of this is that internal decls in an app target will be in the
generated header but internal decls in a framework target will not. This
is important since the generated header is part of a framework's public
interface. Users always have the option to add members via category to an
internal framework type they need to use from Objective-C, or to write the
@interface themselves if the entire type is missing. Only internal protocols
are left out by this.

The presence of the bridging header isn't a /perfect/ way to decide this,
but it's close enough. In an app target without a bridging header, it's
unlikely that there will be ObjC sources depending on the generated header.

Swift SVN r19763
2014-07-09 23:58:57 +00:00
Jordan Rose
1b7a384252 Fix a multitude of issues with extensions exposed to Objective-C (r19116).
- Category names weren't unique.
- We were using an attribute to detect if something was a Swift category,
  but attributes can't be used on categories.
- The test that this was all working was failing in a way that wasn't caught.

To solve these problems:

- We're using a macro to generate category names based on __LINE__ in addition
  to the current module.
- The importer uses the macro to detect that the category comes from Swift
  (no attribute needed).
- The test now has a deliberate error for -verify to catch.

<rdar://problem/17342287&17538553>

Swift SVN r19479
2014-07-02 20:35:25 +00:00
Jordan Rose
347f330d15 Don't import the ObjC representation of Swift extensions in frameworks.
Because extensions don't have any identity we can check against, we can't
tell when we see an Objective-C category if it came from a Swift extension.
Change PrintAsObjC to mark all such categories with SWIFT_EXTENSION, and
just skip them unilaterally when importing Objective-C code.

Also, actually give Swift extensions a name when writing them as Objective-C
categories. Previously, they were nameless categories ("class extensions"),
but methods in a class extension are supposed to be implemented in the class's
main @implementation, so people were getting unexpected warnings about missing
implementations.

<rdar://problem/17342287>

Swift SVN r19116
2014-06-24 01:23:00 +00:00
Jordan Rose
03eacc2931 [PrintAsObjC] Forward-declare things for the generated header when possible...
...and just outright import the bridging header if that's what's needed.

This means we'll use @class and @protocol whenever we're just using a class
or protocol in a type, but still import the enclosing module when we need
the definition. We'll also fall back to the module (or bridging header) if
we need something /else/ from C: a struct, a typedef, whatever.

<rdar://problem/17183425>

Swift SVN r18795
2014-06-11 00:01:58 +00:00
Ted Kremenek
fad874708e Adjust test cases.
Swift SVN r17964
2014-05-12 22:01:52 +00:00
Jordan Rose
8b9dfb017a Move test/Swift2ObjC to test/PrintAsObjC to match the library name.
Swift SVN r13799
2014-02-12 01:19:12 +00:00