Skip to content

Commit f4e26ea

Browse files
committed
Don't add capture arguments to default argument generators.
If we ever add the ability for default arguments to refer to enclosing variables, we should promote DefaultArgumentInitializer to be an always-present entity, give it an independent capture list, and make SILDeclRef refer to it directly. In fact, we may want to do that anyway. Until then, it's a weird special case in terms of not really being a reference to the function returned by getAnyFunctionRef(). rdar://24242783
1 parent 2da4c1d commit f4e26ea

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/SIL/SILFunctionType.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,10 @@ static CanSILFunctionType getSILFunctionType(SILModule &M,
572572
}
573573

574574
// Lower the capture context parameters, if any.
575-
if (constant)
575+
// But note that default arg generators can't capture anything right now,
576+
// and if we ever add that ability, it will be a different capture list
577+
// from the function to which the argument is attached.
578+
if (constant && !constant->isDefaultArgGenerator())
576579
if (auto function = constant->getAnyFunctionRef()) {
577580
auto &Types = M.Types;
578581
auto loweredCaptures = Types.getLoweredLocalCaptures(*function);

test/SILGen/default_arguments.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,14 @@ func test_r18400194() {
253253
(r18400194)(1)
254254
}
255255

256+
// rdar://24242783
257+
// Don't add capture arguments to local default argument generators.
258+
func localFunctionWithDefaultArg() {
259+
var z = 5
260+
func bar(x: Int? = nil) {
261+
z += 1
262+
}
263+
bar()
264+
}
265+
// CHECK-LABEL: sil shared @_TIFF17default_arguments27localFunctionWithDefaultArgFT_T_L_3barFTGSqSi__T_A_
266+
// CHECK-SAME: $@convention(thin) () -> Optional<Int>

0 commit comments

Comments
 (0)