Skip to content

[5.2][Stdlib] Eagerly realize EmptyDictionarySingleton and EmptySetSingleton. #29849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/core/DictionaryStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ internal class __RawDictionaryStorage: __SwiftNativeNSDictionary {
// renamed. The old name must not be used in the new runtime.
@_fixed_layout
@usableFromInline
@_objc_non_lazy_realization
internal class __EmptyDictionarySingleton: __RawDictionaryStorage {
@nonobjc
internal override init(_doNotCallMe: ()) {
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/SetStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ internal class __RawSetStorage: __SwiftNativeNSSet {
// The old names must not be used in the new runtime.
@_fixed_layout
@usableFromInline
@_objc_non_lazy_realization
internal class __EmptySetSingleton: __RawSetStorage {
@nonobjc
override internal init(_doNotCallMe: ()) {
Expand Down
48 changes: 48 additions & 0 deletions test/stdlib/EmptyCollectionSingletonRealization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: objc_interop

import Foundation

// Test to make sure that empty collections don't cause a crash if we smuggle
// them into the ObjC runtime without doing anything that would trigger
// realization. The ObjC runtime expects all classes to have been realized
// (i.e. runtime data structures initialized, triggered the first time a class
// is accessed or used) before being queried in any way.
//
// Note: this test deliberately avoids StdlibUnittest to make sure
// no other code runs that might inadvertently trigger realization behind our
// back.

@objc protocol P {}

do {
let d: [NSObject: NSObject] = [:]
let c: AnyClass? = object_getClass(d)
let conforms = class_conformsToProtocol(c, P.self)
print("Dictionary: ", conforms) // CHECK: Dictionary: false
}

do {
let a: [NSObject] = []
let c: AnyClass? = object_getClass(a)
let p = objc_getProtocol("NSObject")
let conforms = class_conformsToProtocol(c, p)
print("Array:", conforms) // CHECK: Array: false
}

do {
let s: Set<NSObject> = []
let c: AnyClass? = object_getClass(s)
let p = objc_getProtocol("NSObject")
let conforms = class_conformsToProtocol(c, p)
print("Set:", conforms) // CHECK: Set: false
}