Skip to content

Commit c9f550c

Browse files
committed
Parse: Parse an explicit 'nil' default argument as DefaultArgumentKind::NilLiteral
We support a special DefaultArgumentKind::NilLiteral, but it was only used for synthesized and imported default arguments as well. Let's also use it for parsed default arguments that are exactly 'nil', so that rethrows checking can special-case them.
1 parent 717a132 commit c9f550c

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

lib/Parse/ParsePattern.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ static DefaultArgumentKind getDefaultArgKind(Expr *init) {
3939
if (!init)
4040
return DefaultArgumentKind::None;
4141

42+
// Parse an as-written 'nil' expression as the special NilLiteral kind,
43+
// which is emitted by the caller and can participate in rethrows
44+
// checking.
45+
if (isa<NilLiteralExpr>(init))
46+
return DefaultArgumentKind::NilLiteral;
47+
4248
auto magic = dyn_cast<MagicIdentifierLiteralExpr>(init);
4349
if (!magic)
4450
return DefaultArgumentKind::Normal;

test/SILGen/constrained_extensions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extension Array where Element == Int {
4444

4545
// CHECK-LABEL: sil non_abi [serialized] [ossa] @$sSa22constrained_extensionsSiRszlE12staticMethod1eS2iSg_tFZfA_ : $@convention(thin) () -> Optional<Int>
4646
// CHECK-LABEL: sil [ossa] @$sSa22constrained_extensionsSiRszlE12staticMethod1eS2iSg_tFZ : $@convention(method) (Optional<Int>, @thin Array<Int>.Type) -> Int
47-
public static func staticMethod(e: Element? = nil) -> Element {
47+
public static func staticMethod(e: Element? = (nil)) -> Element {
4848
return e!
4949
}
5050

@@ -104,7 +104,7 @@ extension Dictionary where Key == Int {
104104
// CHECK-LABEL: sil non_abi [serialized] [ossa] @$sSD22constrained_extensionsSiRszrlE12staticMethod1k1vq_SiSg_q_SgtFZfA_ : $@convention(thin) <Key, Value where Key == Int> () -> Optional<Int>
105105
// CHECK-LABEL: sil non_abi [serialized] [ossa] @$sSD22constrained_extensionsSiRszrlE12staticMethod1k1vq_SiSg_q_SgtFZfA0_ : $@convention(thin) <Key, Value where Key == Int> () -> @out Optional<Value>
106106
// CHECK-LABEL: sil [ossa] @$sSD22constrained_extensionsSiRszrlE12staticMethod1k1vq_SiSg_q_SgtFZ : $@convention(method) <Key, Value where Key == Int> (Optional<Int>, @in_guaranteed Optional<Value>, @thin Dictionary<Int, Value>.Type) -> @out Value
107-
public static func staticMethod(k: Key? = nil, v: Value? = nil) -> Value {
107+
public static func staticMethod(k: Key? = (nil), v: Value? = (nil)) -> Value {
108108
return v!
109109
}
110110

@@ -191,7 +191,7 @@ extension Array where Element == Int {
191191

192192
// CHECK-LABEL: sil hidden [ossa] @$sSa22constrained_extensionsSiRszlE6NestedV10hasDefault1eySiSg_tFfA_ : $@convention(thin) () -> Optional<Int>
193193
// CHECK-LABEL: sil hidden [ossa] @$sSa22constrained_extensionsSiRszlE6NestedV10hasDefault1eySiSg_tF : $@convention(method) (Optional<Int>, @inout Array<Int>.Nested) -> ()
194-
mutating func hasDefault(e: Element? = nil) {
194+
mutating func hasDefault(e: Element? = (nil)) {
195195
self.e = e
196196
}
197197
}

test/SILGen/default_arguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func test_r18400194() {
284284
// Don't add capture arguments to local default argument generators.
285285
func localFunctionWithDefaultArg() {
286286
var z = 5
287-
func bar(_ x: Int? = nil) {
287+
func bar(_ x: Int? = (nil)) {
288288
z += 1
289289
}
290290
bar()

test/SILGen/default_arguments_inherited.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// derived class generic signature.
88

99
class Puppy<T, U> {
10-
init(t: T? = nil, u: U? = nil) {}
10+
init(t: T? = (nil), u: U? = (nil)) {}
1111
}
1212

1313
class Chipmunk : Puppy<Int, String> {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
2+
3+
public func takesAnInt(_: Int? = nil) {}
4+
5+
// CHECK-LABEL: sil [ossa] @$s21default_arguments_nil15callsTakesAnIntyyF : $@convention(thin) () -> () {
6+
// CHECK: [[NIL:%.*]] = enum $Optional<Int>, #Optional.none!enumelt
7+
// CHECK: [[FN:%.*]] = function_ref @$s21default_arguments_nil10takesAnIntyySiSgF
8+
// CHECK: apply [[FN]]([[NIL]]) : $@convention(thin) (Optional<Int>) -> ()
9+
// CHECK: return
10+
public func callsTakesAnInt() {
11+
takesAnInt()
12+
}

0 commit comments

Comments
 (0)