Skip to content

Commit 6acfad6

Browse files
committed
[SIL-opaque] Handled forward captures.
A let that isn't defined yet can be captured by a local function to which a closure is formed. Don't use an address type unless we're using lowered addresses.
1 parent c6de808 commit 6acfad6

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,13 @@ void SILGenFunction::emitCaptures(SILLocation loc,
270270
capturedArgs.push_back(emitUndef(getLoweredType(type)));
271271
break;
272272
case CaptureKind::Immutable:
273-
case CaptureKind::StorageAddress:
274-
// FIXME_addrlower: only call getAddressType for M.useLoweredAddresses()
275-
capturedArgs.push_back(emitUndef(getLoweredType(type).getAddressType()));
273+
case CaptureKind::StorageAddress: {
274+
auto ty = getLoweredType(type);
275+
if (SGM.M.useLoweredAddresses())
276+
ty = ty.getAddressType();
277+
capturedArgs.push_back(emitUndef(ty));
276278
break;
279+
}
277280
case CaptureKind::Box: {
278281
auto boxTy = SGM.Types.getContextBoxTypeForCapture(
279282
vd,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-swift-emit-silgen -verify -enable-sil-opaque-values -Xllvm -sil-full-demangle %s | %FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-%target-runtime
2+
3+
func take<T>(_ t: T) {}
4+
5+
// CHECK-LABEL: sil hidden [ossa] @$s21opaque_values_invalid27forward_capture_generic_let4withSayxGx_tlF : {{.*}} {
6+
// CHECK: [[F:%[^,]+]] = function_ref @$s21opaque_values_invalid27forward_capture_generic_let4withSayxGx_tlF1fL_yylF {{.*}}
7+
// CHECK: [[F]]<U>(undef)
8+
// CHECK-LABEL: } // end sil function '$s21opaque_values_invalid27forward_capture_generic_let4withSayxGx_tlF'
9+
func forward_capture_generic_let<U>(with u: U) -> [U] {
10+
func f() { g() } // expected-error {{closure captures 'x' before it is declared}}
11+
f()
12+
let x = u // expected-note{{captured value declared here}}
13+
func g() {
14+
take(x) // expected-note {{captured here}}
15+
}
16+
}
17+

0 commit comments

Comments
 (0)