-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathdefault-values-swift4.swift
147 lines (116 loc) · 5.44 KB
/
default-values-swift4.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// RUN: %target-typecheck-verify-swift -swift-version 4
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-testing
private func privateFunction() {}
// expected-note@-1 2{{global function 'privateFunction()' is not public}}
fileprivate func fileprivateFunction() {}
// expected-note@-1 2{{global function 'fileprivateFunction()' is not public}}
func internalFunction() {}
// expected-note@-1 2{{global function 'internalFunction()' is not public}}
@usableFromInline func versionedFunction() {}
// expected-note@-1 4{{global function 'versionedFunction()' is not public}}
public func publicFunction() {}
func internalIntFunction() -> Int {}
// expected-note@-1 {{global function 'internalIntFunction()' is not public}}
private func privateFunction2() {}
// expected-note@-1 {{global function 'privateFunction2()' is not '@usableFromInline' or public}}
fileprivate func fileprivateFunction2() {}
// expected-note@-1 {{global function 'fileprivateFunction2()' is not '@usableFromInline' or public}}
func internalFunction2() {}
// expected-note@-1 {{global function 'internalFunction2()' is not '@usableFromInline' or public}}
func internalIntFunction2() -> Int {}
// expected-note@-1 {{global function 'internalIntFunction2()' is not '@usableFromInline' or public}}
func internalFunctionWithDefaultValue(
x: Int = {
struct Nested {}
// OK
publicFunction()
// OK
versionedFunction()
// OK
internalFunction()
// OK
fileprivateFunction()
// OK
privateFunction()
// OK
return 0
}(),
y: Int = internalIntFunction()) {}
@usableFromInline func versionedFunctionWithDefaultValue(
x: Int = {
struct Nested {}
// expected-error@-1 {{type 'Nested' cannot be nested inside a default argument value}}
// FIXME: Some errors below are diagnosed twice
publicFunction()
// OK
versionedFunction()
// OK
internalFunction2()
// expected-error@-1 {{global function 'internalFunction2()' is internal and cannot be referenced from a default argument value}}
fileprivateFunction2()
// expected-error@-1 {{global function 'fileprivateFunction2()' is fileprivate and cannot be referenced from a default argument value}}
privateFunction2()
// expected-error@-1 {{global function 'privateFunction2()' is private and cannot be referenced from a default argument value}}
return 0
}(),
y: Int = internalIntFunction2()) {}
// expected-error@-1 {{global function 'internalIntFunction2()' is internal and cannot be referenced from a default argument value}}
public func publicFunctionWithDefaultValue(
x: Int = {
struct Nested {}
// expected-error@-1 {{type 'Nested' cannot be nested inside a default argument value}}
// FIXME: Some errors below are diagnosed twice
publicFunction()
versionedFunction()
// expected-error@-1 {{global function 'versionedFunction()' is internal and cannot be referenced from a default argument value}}
internalFunction()
// expected-error@-1 {{global function 'internalFunction()' is internal and cannot be referenced from a default argument value}}
fileprivateFunction()
// expected-error@-1 {{global function 'fileprivateFunction()' is fileprivate and cannot be referenced from a default argument value}}
privateFunction()
// expected-error@-1 {{global function 'privateFunction()' is private and cannot be referenced from a default argument value}}
return 0
}(),
y: Int = internalIntFunction()) {}
// expected-error@-1 {{global function 'internalIntFunction()' is internal and cannot be referenced from a default argument value}}
// https://bugs.swift.org/browse/SR-5559
public class MyClass {
public func method<T>(_: T.Type = T.self) -> T { }
}
public func evilCode(
x: Int = {
let _ = publicFunction()
let _ = versionedFunction()
// expected-error@-1 {{global function 'versionedFunction()' is internal and cannot be referenced from a default argument value}}
func localFunction() {
publicFunction()
versionedFunction()
// expected-error@-1 {{global function 'versionedFunction()' is internal and cannot be referenced from a default argument value}}
}
return 0
}()) {}
private func privateIntFunction() -> Int {} // expected-note {{global function 'privateIntFunction()' is not public}}
public struct HasSubscript {
public subscript(x: Int = {
struct Nested {}
// expected-error@-1 {{type 'Nested' cannot be nested inside a default argument value}}
publicFunction()
versionedFunction()
// expected-error@-1 {{global function 'versionedFunction()' is internal and cannot be referenced from a default argument value}}
internalFunction()
// expected-error@-1 {{global function 'internalFunction()' is internal and cannot be referenced from a default argument value}}
fileprivateFunction()
// expected-error@-1 {{global function 'fileprivateFunction()' is fileprivate and cannot be referenced from a default argument value}}
privateFunction()
// expected-error@-1 {{global function 'privateFunction()' is private and cannot be referenced from a default argument value}}
return 0
}()) -> Int {
get {}
set {}
}
public subscript(y y: Int = privateIntFunction()) -> Int {
// expected-error@-1 {{global function 'privateIntFunction()' is private and cannot be referenced from a default argument value}}
get {}
set {}
}
}