-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathdefault-values-swift4.swift
140 lines (110 loc) · 4.95 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
// 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 '@usableFromInline' or public}}
fileprivate func fileprivateFunction() {}
// expected-note@-1 2{{global function 'fileprivateFunction()' is not '@usableFromInline' or public}}
func internalFunction() {}
// expected-note@-1 2{{global function 'internalFunction()' is not '@usableFromInline' or public}}
@usableFromInline func usableFromInlineFunction() {}
public func publicFunction() {}
func internalIntFunction() -> Int {}
// expected-note@-1 {{global function 'internalIntFunction()' is not '@usableFromInline' or 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
usableFromInlineFunction()
// OK
internalFunction()
// OK
fileprivateFunction()
// OK
privateFunction()
// OK
return 0
}(),
y: Int = internalIntFunction()) {}
@usableFromInline func usableFromInlineFunctionWithDefaultValue(
x: Int = {
struct Nested {}
// expected-error@-1 {{type 'Nested' cannot be nested inside a default argument value}}
publicFunction()
// OK
usableFromInlineFunction()
// 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()
usableFromInlineFunction()
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://github.com/apple/swift/issues/48131
public class MyClass {
public func method<T>(_: T.Type = T.self) -> T { }
}
public func evilCode(
x: Int = {
let _ = publicFunction()
let _ = usableFromInlineFunction()
func localFunction() {
publicFunction()
usableFromInlineFunction()
}
return 0
}()) {}
private func privateIntFunction() -> Int {} // expected-note {{global function 'privateIntFunction()' is not '@usableFromInline' or public}}
public struct HasSubscript {
public subscript(x: Int = {
struct Nested {}
// expected-error@-1 {{type 'Nested' cannot be nested inside a default argument value}}
publicFunction()
usableFromInlineFunction()
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 {}
}
}