Commit Graph

3245 Commits

Author SHA1 Message Date
Tim Kientzle
33888255f3 Log a message when Obj-C -hash is invoked for a Swift object that is Equatable but not Hashable
This is worth telling people about:

Since we're being asked for the hash value, that probably means someone is
trying to put this object into some form of set or dictionary.  But we don't know
anything about the Equatable implementation, so the only valid hash value we can
possibly return is a constant.  And hash table implementations typically degrade
into unsorted lists when provided constant hash values.

Note: In order to avoid hammering the logs, we only emit the
warning once for each class that hits this path.
2023-11-01 14:19:12 -07:00
Tim Kientzle
716b58e956 Handle ObjC -hash differently for Equatable and non-Equatable types
For an Equatable type, we need a hash implementation that
is compatible with any possible definition of `==`.
Conservatively, that means `-hash` must return a constant.

For non-Equatable types, we know that `==` is identity based,
so we can get better hash behavior by using the object address.

Caveat:  This means that non-Equatable types will do two
protocol conformance checks on every call to `hash`.
2023-10-31 14:26:20 -07:00
Tim Kientzle
8cc0fe73d7 Allow "incomplete hashing" when testing ObjC bridged objects
Swift objects can implement Equatable without implementing
Hashable.  Obj-C NSObject always provides both.

Now that we bridge Swift Equatable to Obj-C `-isEqual:`,
we have to be conservative with how we expose `-hash`
for Swift types that are Equatable but not Hashable.
This looks like "incomplete hashing" of such types.
2023-10-31 11:37:10 -07:00
Tim Kientzle
6fd39741d6 Fix a number of issues (review feedback)
* Fix how we were looking for common parent class
* Use a constant hashValue for types that are Equatable but not Hashable
2023-10-30 15:57:47 -07:00
Tim Kientzle
a840c69392 Use Swift Equatable and Hashable conformances from ObjC
If a Swift type implements Equatable and/or Hashable and
we then pass that object into ObjC, we want ObjC
`isEqual:` and `hashValue` to use that.  This allows
ObjC code to build ObjC collections of Swift objects.

* Support for Hashable struct/enum types was implemented in #4124
* Support for Equatable struct/enum types was implemented in #68720
* This implements support for Hashable and Equatable _class_ types

Caveats:
1. This does a lot of dynamic lookup work for each operation, so is
   inherently rather slow.  Unlike the struct/enum case, there is no convenient
   place to cache the conformance information, so it's not clear that there is a
   viable way to make it significantly faster.
2. This is a behavioral change to low-level support code.  There is a
   risk of breaking code that may be relying on the old behavior.
2023-10-27 12:01:44 -07:00
Yuta Saito
b49e99a206 [test][stdlib] Fix calling convention mismatch in test/stdlib/Runtime.swift.gyb
`swift_demangle` is defined with C calling convention, but the test case
used SwiftCC.
2023-10-24 01:35:25 +00:00
swift-ci
bb57981112 Merge remote-tracking branch 'origin/main' into rebranch 2023-10-09 22:55:17 -07:00
Saleem Abdulrasool
d61b8855e9 stdlib: map wchar_t to UInt16 on Windows
This is an ABI breaking change for Windows.  `WCHAR` on Windows is
mapped to `short` (`-fshort-wchar` makes it `unsigned short`).  When C++
interop is enabled, `WCHAR` will be mapped to `wchar_t` which is then
mapped to `short` (or `unsigned short` if `-fshort-wchar` is specified).
Correct the mapping type to get the desired behaviour.
2023-10-09 20:02:48 -07:00
Evan Wilde
fcc880863b Merge pull request #68956 from etcwilde/ewilde/ignore-more-gnus
Ignore another GNU exported symbol
2023-10-04 07:28:55 -07:00
Evan Wilde
a0fe86a3af Ignore another GNU exported symbol
I ignored this symbol on swiftRemoteMirror, but forgot to ignore it in
swiftCore, so ignoring it now

_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJRKjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_
2023-10-03 20:36:37 -07:00
swift-ci
8cf5e2bf4d Merge remote-tracking branch 'origin/main' into rebranch 2023-09-29 12:36:08 -07:00
Tim Kientzle
659d1cfb13 Make ObjC isEqual: delegate to Equatable when Hashable isn't available
When a Swift struct gets bridged to Obj-C, we box it into an opaque
`_SwiftValue` Obj-C object.  This object previously supported the
Obj-C `isEqual:` and `hash` selectors by dispatching to the Swift
Hashable conformance, if present.

This does not work if the Swift struct conforms to Equatable but
does not conform to Hashable.  This case seems to have been
overlooked in PR #4124.

This PR extends the earlier work to support `isEqual:` by
first checking for a Hashable conformance, then falling back
on an Equatable conformance if there is no Hashable conformance.

Resolves rdar://114294889
2023-09-22 17:58:22 -07:00
Evan Wilde
1291bdb5ef Merge pull request #68635 from etcwilde/ewilde/add-another-stdlib-symbol
Adding another C++ stdlib function to ignore list
2023-09-19 21:35:29 -07:00
Evan Wilde
8a7c3c98b6 Adding another C++ stdlib function to ignore list
Looks like rebranch is finding another realloc_insert function on
vector causing failures on the Ubuntu 20.04 aarch64 bot.

_ZNSt6vectorIjSaIjEE17_M_realloc_insertIJRKjEEEvN9__gnu_cxx17__normal_iteratorIPjS1_EEDpOT_

We'll go ahead and ignore it for now.
2023-09-19 11:58:52 -07:00
Erik Eckstein
5bc036661c SIL optimizer: add the LetPropertyLowering pass
It lowers let property accesses of classes.
Lowering consists of two tasks:

* In class initializers, insert `end_init_let_ref` instructions at places where all let-fields are initialized.
  This strictly separates the life-range of the class into a region where let fields are still written during
  initialization and a region where let fields are truly immutable.

* Add the `[immutable]` flag to all `ref_element_addr` instructions (for let-fields) which are in the "immutable"
  region. This includes the region after an inserted `end_init_let_ref` in an class initializer, but also all
  let-field accesses in other functions than the initializer and the destructor.

This pass should run after DefiniteInitialization but before RawSILInstLowering (because it relies on `mark_uninitialized` still present in the class initializer).
Note that it's not mandatory to run this pass. If it doesn't run, SIL is still correct.

Simplified example (after lowering):

  bb0(%0 : @owned C):                           // = self of the class initializer
    %1 = mark_uninitialized %0
    %2 = ref_element_addr %1, #C.l              // a let-field
    store %init_value to %2
    %3 = end_init_let_ref %1                    // inserted by lowering
    %4 = ref_element_addr [immutable] %3, #C.l  // set to immutable by lowering
    %5 = load %4
2023-09-19 15:10:30 +02:00
Erik Eckstein
e5eb15dcbe Swift SIL: replace the set_deallocating instruction with begin_dealloc_ref
Codegen is the same, but `begin_dealloc_ref` consumes the operand and produces a new SSA value.
This cleanly splits the liferange to the region before and within the destructor of a class.
2023-09-19 15:10:30 +02:00
Guillaume Lessard
c30307e21f [test] replace uses of String(validatingUTF8:)
- use the new name `String(validatingCString:)`
2023-09-11 14:17:05 -07:00
Saleem Abdulrasool
99453fc2e8 test: introduce a new %swift-plugin-dir macro
Use this to define the macro location rather than the "host" dir (which
is actually for the build and not the host).  Furthermore, on Windows,
the build dir is /usr/lib/swift as the host content is in the SDK.

This prepares the tests for Windows.
2023-09-04 13:04:18 -07:00
Nate Cook
c7577bbe41 Disable stdlib tests under the OS stdlib
These two tests verify recent fixes, so they fail when run against
older versions of the stdlib.
2023-08-29 17:35:05 -05:00
Nate Cook
ad19d35b26 Disable StringAPI test on OS stdlib
These tests verify fixes in the stdlib post-5.9, which means
that they fail when tested on older versions of the stdlib.

Fixes rdar://114581543
2023-08-28 15:07:11 -05:00
Stephen Canon
b9a07dcf89 Disable stdlib/Duration.swift tests on old OSes.
These tests exercise a code path that has a known bug on older OS versions (fixed with Swift 5.9), so disable it for testing in those environments.
2023-08-28 13:29:29 -04:00
Saleem Abdulrasool
898f9efe3e Merge pull request #68099 from hjyamauchi/abort
Fix a fatal error not-terminating issue in Windows
2023-08-27 12:08:32 -07:00
Hiroshi Yamauchi
5fc3ad3a81 Fix a fatal error not-terminating issue in Windows.
Fix the issue that fatal errors in certain cases don't terminate the
process and the process keeps running in Windows by disabling the
exception swallowing that supressed the illegal instruction exceptions
coming from llvm.trap.
2023-08-25 13:04:36 -07:00
Nishith Kumar M Shah
056b4943a3 Fix String.hasPrefix and String.hasSuffix when self and arg are in NFC form (#67729) 2023-08-23 11:23:59 -05:00
Michael Spencer
b2640e15e4 [test] Rename all module.map files to module.modulemap
`module.map` as a module map name has been discouraged since 2014, and
Clang will soon warn on its usage. This patch renames all instances of
`module.map` in the Swift tests to `module.modulemap` in preparation
for this change to Clang.

rdar://106123303
2023-08-21 15:58:59 -07:00
Doug Gregor
1d246629a4 Merge pull request #67998 from DougGregor/observer-macros-on-computed-properties
Improve checking of macro-generated accessors against documented names
2023-08-17 20:46:35 -07:00
Doug Gregor
5d6746d974 Improve checking of macro-generated accessors against documented names
The checking of the accessors generated by a macro against the
documented set of accessors for the macro is slightly too strict and
produces misleading error messages. Make the check slightly looser in
the case where an observer-producing macro (such as
`@ObservationIgnored`) is applied to a computed property. Here, we
would diagnose that the observer did not in fact produce any
observers, even though it couldn't have: computed properties don't get
observers. Remove the diagnostic in this case.

While here, add some tests and improve the wording of diagnostics a
bit.

Fixes rdar://113710199.
2023-08-17 16:59:42 -07:00
swift-ci
5f5b68e38e Merge pull request #67962 from DougGregor/init-accessors-is-not-experimental
Remove unnecessary `--enable-experimental-feature InitAccessors` from tests
2023-08-16 11:33:38 -07:00
Doug Gregor
5edfc26156 Remove unnecessary --enable-experimental-feature InitAccessors from tests
This feature was accepted, so we no longer need to pass this flag. Worse,
it triggers errors in non-Asserts compilers. Fixes rdar://113708096.
2023-08-16 09:00:39 -07:00
Ben Rimmington
98ada1b200 [stdlib] Fix buffer size of small-capacity strings (#67929) 2023-08-16 10:00:42 +01:00
Allan Shortlidge
024ed957cd NFC: Re-enable ObservableAvailabilityCycle.swift test. 2023-08-08 15:58:08 -07:00
Slava Pestov
c9225db156 Disable test/stdlib/Observation/ObservableAvailabilityCycle.swift 2023-08-04 10:20:04 -04:00
Doug Gregor
c098175059 Add test case involving circular references with @Observable
Add a test case for Observable types that are extended from other
source files. Prior to the recent changes to make
`TypeRefinementContext` more lazy, this would trigger circular
references through the `TypeRefinementContextBuilder`.

Finishes rdar://112079160.
2023-08-02 15:07:09 -07:00
Allan Shortlidge
7c0a9cc92e Merge pull request #67636 from tshortli/enum-unavailable-element-derived-equatable-hashable
Sema: Fix unavailable enum element cases in derived hashable/equatable impls
2023-08-01 16:22:28 -07:00
Allan Shortlidge
2442737a7d NFC: Fix %target-run-simple-swift invocations in a couple of tests.
The `-unavailable-decl-optimization` flag is a frontend flag and therefore
requires `-Xfrontend` when passed through the driver.
2023-07-31 17:26:34 -07:00
Pavel Yaskevich
2a0651e8b0 [Frontend/NFC] SE-0400: Enable InitAccessors feature by default 2023-07-31 13:18:59 -07:00
Allan Shortlidge
7996c5b30c Sema: Avoid decoding unavailable enum elements in derived Codable conformances.
The compiler derived implementations of `Codable` conformances for enums did
not take enum element unavailability into account. This could result in
unavailable values being instantiated at runtime, leading to a general
violation of the invariant that unavailable code is unreachable at runtime.
This problem is possible because synthesized code is not type checked; had the
conformances been hand-written, they would have been rejected for referencing
unavailable declarations inside of available declarations.

This change specifically alters derivation for the following declarations:
- `Decodable.init(from:)`
- `Encodable.encode(to:)`
- `CodingKey.init(stringValue:)`

Resolves rdar://110098469
2023-07-26 22:48:25 -07:00
Max Desiatov
1f99204897 test: disable crashing tests on WASI (#67531)
WASI doesn't support spawning a subprocess, so crash tests crashes the test harness itself. Those should be skipped until proper subprocess support is available.
2023-07-26 18:43:02 +01:00
Philippe Hausler
0fca5199e7 [Observation] Forward availability and defines to extensions (#67412)
* [Observation] Forward availability and defines to extensions

* Simplify availability slightly from review feedback

* Simplify availability for extensions to use `.with`
2023-07-26 08:46:57 -07:00
Slava Pestov
e454458e2a Runtime: Lift prohibition on packs in swift_getTypeByMangledName() overload used by mirrors
Fixes rdar://problem/112866068.
2023-07-25 23:15:01 -04:00
Erik Eckstein
7f54c63b29 tests: Disable some tests which fail due to problems in Foundation
Those tests should be part of the Foundation overlay, which is no longer part of the Swift project.

rdar://112643333
2023-07-24 08:34:06 +02:00
Max Desiatov
29fa1c0806 test: Disable test/stdlib/Duration.swift on 32bit platforms (#67469)
128-bit types are not provided by LLVM for 32-bit targets.
2023-07-22 22:48:30 +01:00
Max Desiatov
a7142c896a test/stdlib/UnsafeRawPointer.swift: disable crashing tests on WASI 2023-07-22 14:44:16 +01:00
Max Desiatov
ed7043cc0a test/stdlib/TemporaryAllocation.swift: disable crashing tests on WASI 2023-07-22 14:42:48 +01:00
Max Desiatov
94271b53f1 test/stdlib/StringAPICString.swift: disable crashing tests on WASI 2023-07-22 14:41:31 +01:00
Max Desiatov
2b69325821 test/stdlib/StringAPI.swift: disable crashing tests on WASI 2023-07-22 14:38:04 +01:00
Max Desiatov
8b52415184 test/stdlib/Error.swift: disable crashing tests on WASI 2023-07-22 14:36:57 +01:00
Nate Cook
e68395c923 Disable diagnostic test again for now 2023-07-21 09:29:20 -05:00
Nate Cook
90d341421e Remove dump-macro flag in test 2023-07-19 16:07:13 -05:00
Nate Cook
5b9d68b023 Merge branch 'main' into observation_peer_macros 2023-07-19 16:05:43 -05:00