Skip to content

Commit 666190c

Browse files
Merge branch 'master' into SR-11295-warning-unecessary-casts
2 parents cb4e6ae + 7e5e00a commit 666190c

File tree

173 files changed

+3116
-2006
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+3116
-2006
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,29 @@ CHANGELOG
2626
Swift Next
2727
----------
2828

29+
* [SE-0253][]:
30+
31+
Values of types that declare `func callAsFunction` methods can be called
32+
like functions. The call syntax is shorthand for applying
33+
`func callAsFunction` methods.
34+
35+
```swift
36+
struct Adder {
37+
var base: Int
38+
func callAsFunction(_ x: Int) -> Int {
39+
return x + base
40+
}
41+
}
42+
var adder = Adder(base: 3)
43+
adder(10) // returns 13, same as `adder.callAsFunction(10)`
44+
```
45+
46+
* `func callAsFunction` argument labels are required at call sites.
47+
* Multiple `func callAsFunction` methods on a single type are supported.
48+
* `mutating func callAsFunction` is supported.
49+
* `func callAsFunction` works with `throws` and `rethrows`.
50+
* `func callAsFunction` works with trailing closures.
51+
2952
* [SR-4206][]:
3053

3154
A method override is no longer allowed to have a generic signature with
@@ -7711,6 +7734,7 @@ Swift 1.0
77117734
[SE-0244]: <https://github.com/apple/swift-evolution/blob/master/proposals/0244-opaque-result-types.md>
77127735
[SE-0245]: <https://github.com/apple/swift-evolution/blob/master/proposals/0245-array-uninitialized-initializer.md>
77137736
[SE-0252]: <https://github.com/apple/swift-evolution/blob/master/proposals/0252-keypath-dynamic-member-lookup.md>
7737+
[SE-0253]: <https://github.com/apple/swift-evolution/blob/master/proposals/0253-callable.md>
77147738
[SE-0254]: <https://github.com/apple/swift-evolution/blob/master/proposals/0254-static-subscripts.md>
77157739

77167740
[SR-106]: <https://bugs.swift.org/browse/SR-106>

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ set(SWIFT_BENCH_MODULES
8787
single-source/Exclusivity
8888
single-source/ExistentialPerformance
8989
single-source/Fibonacci
90+
single-source/FindStringNaive
9091
single-source/FlattenList
9192
single-source/FloatingPointParsing
9293
single-source/FloatingPointPrinting
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
//===--- FindStringNaive.swift --------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
// Mini benchmark implementing a naive String search algorithm that
16+
// at the moment shows a lot of ARC traffic.
17+
let t: [BenchmarkCategory] = [.String, .refcount]
18+
let N = 100
19+
20+
var longStringFoFoFoFox: String?
21+
var longArrayFoFoFoFox: [UInt8]?
22+
23+
public let FindStringNaive = [
24+
BenchmarkInfo(
25+
name: "FindString.Loop1.Substring",
26+
runFunction: runBenchLoop1Substring,
27+
tags: t,
28+
setUpFunction: {
29+
longStringFoFoFoFox = String(repeating: "fo", count: 5_000) + "fox <-- needle"
30+
}),
31+
BenchmarkInfo(
32+
name: "FindString.Rec3.String",
33+
runFunction: runBenchRecursive3String,
34+
tags: t,
35+
setUpFunction: {
36+
longStringFoFoFoFox = String(repeating: "fo", count: 500) + "fox <-- needle"
37+
}),
38+
BenchmarkInfo(
39+
name: "FindString.Rec3.Substring",
40+
runFunction: runBenchRecursive3Substring,
41+
tags: t,
42+
setUpFunction: {
43+
longStringFoFoFoFox = String(repeating: "fo", count: 500) + "fox <-- needle"
44+
}),
45+
BenchmarkInfo(
46+
name: "FindString.Loop1.Array",
47+
runFunction: runBenchLoop1Array,
48+
tags: t,
49+
setUpFunction: {
50+
longArrayFoFoFoFox = []
51+
longArrayFoFoFoFox!.reserveCapacity(1_100_000)
52+
for _ in 0 ..< 500_000 {
53+
longArrayFoFoFoFox!.append(contentsOf: "fo".utf8)
54+
}
55+
longArrayFoFoFoFox!.append(contentsOf: "fox <-- needle".utf8)
56+
}),
57+
BenchmarkInfo(
58+
name: "FindString.Rec3.Array",
59+
runFunction: runBenchRecursive3ArrayOfUTF8,
60+
tags: t,
61+
setUpFunction: {
62+
longArrayFoFoFoFox = []
63+
longArrayFoFoFoFox!.reserveCapacity(11_000)
64+
for _ in 0 ..< 5_000 {
65+
longArrayFoFoFoFox!.append(contentsOf: "fo".utf8)
66+
}
67+
longArrayFoFoFoFox!.append(contentsOf: "fox <-- needle".utf8)
68+
}),
69+
]
70+
71+
func findOne<S: StringProtocol>(
72+
_ string: S,
73+
needle: Character
74+
) -> String.Index? {
75+
var index = string.startIndex
76+
while index < string.endIndex {
77+
let nextIndex = string.index(after: index)
78+
if string[index] == needle {
79+
return index
80+
}
81+
index = nextIndex
82+
}
83+
return nil
84+
}
85+
86+
func findThreeRecursive<S: StringProtocol>(
87+
_ string: S,
88+
needle1: Character,
89+
needle2: Character?,
90+
needle3: Character?
91+
) -> String.Index? {
92+
var index = string.startIndex
93+
while index < string.endIndex {
94+
let nextIndex = string.index(after: index)
95+
if string[index] == needle1 {
96+
// Check subsequent needles recursively (if applicable)
97+
guard let needle2 = needle2 else { return index }
98+
99+
if findThreeRecursive(
100+
string[nextIndex...].prefix(2), needle1: needle2, needle2: needle3, needle3: nil
101+
) == nextIndex {
102+
return index
103+
}
104+
}
105+
index = nextIndex
106+
}
107+
return nil
108+
}
109+
110+
func findOneOnUTF8Collection<Bytes: Collection>(
111+
_ string: Bytes,
112+
needle: UInt8
113+
) -> Bytes.Index? where Bytes.Element == UInt8 {
114+
var index = string.startIndex
115+
while index < string.endIndex {
116+
let nextIndex = string.index(after: index)
117+
if string[index] == needle {
118+
return index
119+
}
120+
index = nextIndex
121+
}
122+
return nil
123+
}
124+
125+
func findThreeOnUTF8Collection<Bytes: Collection>(
126+
_ string: Bytes,
127+
needle1: UInt8,
128+
needle2: UInt8?,
129+
needle3: UInt8?
130+
) -> Bytes.Index? where Bytes.Element == UInt8 {
131+
var index = string.startIndex
132+
while index < string.endIndex {
133+
let nextIndex = string.index(after: index)
134+
if string[index] == needle1 {
135+
// Check subsequent needles recursively (if applicable)
136+
guard let needle2 = needle2 else { return index }
137+
138+
if findThreeOnUTF8Collection(
139+
string[nextIndex...].prefix(2), needle1: needle2, needle2: needle3, needle3: nil
140+
) == nextIndex {
141+
return index
142+
}
143+
}
144+
index = nextIndex
145+
}
146+
return nil
147+
}
148+
149+
@inline(never)
150+
func runBenchLoop1Substring(iterations: Int) {
151+
for _ in 0 ..< iterations {
152+
precondition(findOne(longStringFoFoFoFox![...], needle: "x") != nil)
153+
}
154+
}
155+
156+
@inline(never)
157+
func runBenchLoop1Array(iterations: Int) {
158+
for _ in 0 ..< iterations {
159+
precondition(findOneOnUTF8Collection(longArrayFoFoFoFox!, needle: UInt8(ascii: "x")) != nil)
160+
}
161+
}
162+
163+
@inline(never)
164+
func runBenchRecursive3Substring(iterations: Int) {
165+
for _ in 0 ..< iterations {
166+
precondition(findThreeRecursive(longStringFoFoFoFox![...], needle1: "f", needle2: "o", needle3: "x") != nil)
167+
}
168+
}
169+
170+
@inline(never)
171+
func runBenchRecursive3String(iterations: Int) {
172+
for _ in 0 ..< iterations {
173+
precondition(findThreeRecursive(longStringFoFoFoFox!, needle1: "f", needle2: "o", needle3: "x") != nil)
174+
}
175+
}
176+
177+
@inline(never)
178+
func runBenchRecursive3ArrayOfUTF8(iterations: Int) {
179+
for _ in 0 ..< iterations {
180+
precondition(findThreeOnUTF8Collection(longArrayFoFoFoFox!,
181+
needle1: UInt8(ascii: "f"),
182+
needle2: UInt8(ascii: "o"),
183+
needle3: UInt8(ascii: "x")) != nil)
184+
}
185+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import ErrorHandling
7575
import Exclusivity
7676
import ExistentialPerformance
7777
import Fibonacci
78+
import FindStringNaive
7879
import FlattenList
7980
import FloatingPointParsing
8081
import FloatingPointPrinting
@@ -253,6 +254,7 @@ registerBenchmark(ErrorHandling)
253254
registerBenchmark(Exclusivity)
254255
registerBenchmark(ExistentialPerformance)
255256
registerBenchmark(Fibonacci)
257+
registerBenchmark(FindStringNaive)
256258
registerBenchmark(FlattenListLoop)
257259
registerBenchmark(FlattenListFlatMap)
258260
registerBenchmark(FloatingPointParsing)

cmake/modules/AddSwift.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2370,7 +2370,7 @@ function(_add_swift_executable_single name)
23702370
# NOTE(compnerd) use the C linker language to invoke `clang` rather than
23712371
# `clang++` as we explicitly link against the C++ runtime. We were previously
23722372
# actually passing `-nostdlib++` to avoid the C++ runtime linkage.
2373-
if(SWIFTEXE_SINGLE_SDK STREQUAL ANDROID)
2373+
if(${SWIFTEXE_SINGLE_SDK} STREQUAL ANDROID)
23742374
set_property(TARGET "${name}" PROPERTY
23752375
LINKER_LANGUAGE "C")
23762376
else()

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ namespace swift {
6969
class LazyGenericContextData;
7070
class LazyIterableDeclContextData;
7171
class LazyMemberLoader;
72-
class LazyMemberParser;
7372
class LazyResolver;
7473
class PatternBindingDecl;
7574
class PatternBindingInitializer;
@@ -425,12 +424,6 @@ class ASTContext final {
425424
void setLazyResolver(LazyResolver *resolver);
426425

427426
public:
428-
/// Add a lazy parser for resolving members later.
429-
void addLazyParser(LazyMemberParser *parser);
430-
431-
/// Remove a lazy parser.
432-
void removeLazyParser(LazyMemberParser *parser);
433-
434427
/// getIdentifier - Return the uniqued and AST-Context-owned version of the
435428
/// specified string.
436429
Identifier getIdentifier(StringRef Str) const;
@@ -822,12 +815,6 @@ class ASTContext final {
822815
LazyContextData *getOrCreateLazyContextData(const DeclContext *decl,
823816
LazyMemberLoader *lazyLoader);
824817

825-
/// Use the lazy parsers associated with the context to populate the members
826-
/// of the given decl context.
827-
///
828-
/// \param IDC The context whose member decls should be lazily parsed.
829-
void parseMembers(IterableDeclContext *IDC);
830-
831818
/// Get the lazy function data for the given generic context.
832819
///
833820
/// \param lazyLoader If non-null, the lazy loader to use when creating the

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ SWIFT_TYPEID(AncestryFlags)
3333
SWIFT_TYPEID_NAMED(GenericSignature *, GenericSignature)
3434
SWIFT_TYPEID_NAMED(GenericTypeParamType *, GenericTypeParamType)
3535
SWIFT_TYPEID(Requirement)
36+
SWIFT_TYPEID_NAMED(IterableDeclContext *, IterableDeclContext)

include/swift/AST/ASTTypeIDs.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,26 @@
1717
#ifndef SWIFT_AST_ASTTYPEIDS_H
1818
#define SWIFT_AST_ASTTYPEIDS_H
1919

20+
#include "swift/Basic/LLVM.h"
2021
#include "swift/Basic/TypeID.h"
2122
namespace swift {
2223

2324
class CustomAttr;
25+
class Decl;
2426
class GenericSignature;
2527
class GenericTypeParamType;
28+
class IterableDeclContext;
2629
class NominalTypeDecl;
30+
class OperatorDecl;
2731
struct PropertyWrapperBackingPropertyInfo;
2832
struct PropertyWrapperTypeInfo;
2933
enum class CtorInitializerKind;
3034
struct PropertyWrapperMutability;
35+
class ProtocolDecl;
3136
class Requirement;
37+
enum class ResilienceExpansion : unsigned;
3238
class Type;
39+
class ValueDecl;
3340
class VarDecl;
3441
class TypeAliasDecl;
3542
class Type;

include/swift/AST/Attr.def

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,7 @@ SIMPLE_DECL_ATTR(_weakLinked, WeakLinked,
362362
OnSubscript | OnConstructor | OnEnumElement | OnExtension | UserInaccessible,
363363
75)
364364
SIMPLE_DECL_ATTR(frozen, Frozen,
365-
OnEnum | OnStruct |
366-
UserInaccessible,
365+
OnEnum | OnStruct,
367366
76)
368367
DECL_ATTR_ALIAS(_frozen, Frozen)
369368
SIMPLE_DECL_ATTR(_forbidSerializingReference, ForbidSerializingReference,

include/swift/AST/Builtins.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,14 @@ BUILTIN_MISC_OPERATION(Strideof, "strideof", "n", Special)
418418
/// IsPOD has type T.Type -> Bool
419419
BUILTIN_MISC_OPERATION(IsPOD, "ispod", "n", Special)
420420

421+
/// IsConcrete has type (T.Type) -> Bool
422+
///
423+
/// If the meta type T is concrete, we can always transform this to `true` at
424+
/// any time in SIL. If it's generic, then we lower it to `false` right before
425+
/// IRGen in IRGenPrepare. This allows for the optimizer to specialize this at
426+
/// -O and eliminate conditional code.
427+
BUILTIN_MISC_OPERATION(IsConcrete, "isConcrete", "n", Special)
428+
421429
/// IsBitwiseTakable has type T.Type -> Bool
422430
BUILTIN_MISC_OPERATION(IsBitwiseTakable, "isbitwisetakable", "n", Special)
423431

include/swift/AST/Decl.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5594,6 +5594,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
55945594
SourceRange BodyRange;
55955595
};
55965596

5597+
friend class ParseAbstractFunctionBodyRequest;
5598+
55975599
CaptureInfo Captures;
55985600

55995601
/// Location of the 'throws' token.
@@ -7248,6 +7250,17 @@ void simple_display(llvm::raw_ostream &out, const Decl *decl);
72487250
/// Display ValueDecl subclasses.
72497251
void simple_display(llvm::raw_ostream &out, const ValueDecl *decl);
72507252

7253+
/// Display ExtensionDecls.
7254+
inline void simple_display(llvm::raw_ostream &out, const ExtensionDecl *decl) {
7255+
simple_display(out, static_cast<const Decl *>(decl));
7256+
}
7257+
7258+
/// Display NominalTypeDecls.
7259+
inline void simple_display(llvm::raw_ostream &out,
7260+
const NominalTypeDecl *decl) {
7261+
simple_display(out, static_cast<const Decl *>(decl));
7262+
}
7263+
72517264
/// Extract the source location from the given declaration.
72527265
SourceLoc extractNearestSourceLoc(const Decl *decl);
72537266

@@ -7261,6 +7274,11 @@ inline SourceLoc extractNearestSourceLoc(const GenericTypeDecl *type) {
72617274
return extractNearestSourceLoc(static_cast<const Decl *>(type));
72627275
}
72637276

7277+
/// Extract the source location from the given declaration.
7278+
inline SourceLoc extractNearestSourceLoc(const NominalTypeDecl *type) {
7279+
return extractNearestSourceLoc(static_cast<const Decl *>(type));
7280+
}
7281+
72647282
/// Extract the source location from the given declaration.
72657283
inline SourceLoc extractNearestSourceLoc(const AbstractFunctionDecl *func) {
72667284
return extractNearestSourceLoc(static_cast<const Decl *>(func));

0 commit comments

Comments
 (0)