Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash when compiling async let body with if expression #75294

Open
jumhyn-browser opened this issue Jul 17, 2024 · 2 comments
Open

Crash when compiling async let body with if expression #75294

jumhyn-browser opened this issue Jul 17, 2024 · 2 comments
Labels
assertion failure Bug → crash: An assertion failure async & await Feature → concurrency: asynchronous function aka the async/await pattern bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself concurrency Feature: umbrella label for concurrency language features crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions if else Feature → statements: 'if' statements let & var Feature: constant and variable declarations SILGen Area → compiler: The SIL generation stage swift 6.0

Comments

@jumhyn-browser
Copy link

Description

The following produces a crash on godbolt's nightly build, and confusing diagnostics in Xcode 16 beta 3

Reproduction

func f() async {
    let s1: String? = ""
    async let val = if let s2 = s1 {
        g(s2)
    } else {
        0
    }

    await print(val)
}

func g(_: String) async -> Int {
    return 1
}

In Xcode, we get the following diagnostic:

% swiftc test.swift
test.swift:3:21: error: closure captures 's2' before it is declared
 1 | func f() async {
 2 |     let s1: String? = ""
 3 |     async let val = if let s2 = s1 {
   |                     |      `- note: captured value declared here
   |                     `- error: closure captures 's2' before it is declared
 4 |         g(s2)
   |           `- note: captured here
 5 |     } else {
 6 |         0

Stack dump

Godbolt doesn't produced a symbolicated stack, but in case it's useful:

swift-frontend: /home/build-user/swift/lib/SILGen/SILGenDecl.cpp:914: void (anonymous namespace)::LetValueInitialization::bindValue(swift::SILValue, swift::Lowering::SILGenFunction &, bool, swift::SILLocation): Assertion `!SGF.VarLocs.count(vd) && "Already emitted this vardecl?"' failed.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/swift-nightly/usr/bin/swift-frontend -frontend -S -primary-file <source> -target x86_64-unknown-linux-gnu -disable-objc-interop -g -debug-info-format=dwarf -dwarf-version=4 -swift-version 6 -empty-abi-descriptor -Xcc -working-directory -Xcc /app -resource-dir /opt/compiler-explorer/swift-nightly/usr/lib/swift -enable-anonymous-context-mangled-names -file-compilation-dir /app -Xllvm --x86-asm-syntax=intel -module-name output -in-process-plugin-server-path /opt/compiler-explorer/swift-nightly/usr/lib/swift/host/libSwiftInProcPluginServer.so -plugin-path /opt/compiler-explorer/swift-nightly/usr/lib/swift/host/plugins -plugin-path /opt/compiler-explorer/swift-nightly/usr/local/lib/swift/host/plugins -o /app/output.s
1.	Swift version 6.0-dev (LLVM 0ad8ad0245d47b4, Swift 01bd2b4f8c84d45)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "<source>")
4.	While silgen emitFunction SIL function "@$s6output1fyyYaF".
 for 'f()' (at <source>:1:1)
5.	While silgen closureexpr SIL function "@$s6output1fyyYaFSiyYacfu_".
 for expression at [<source>:3:21 - line:7:5] RangeText="if let s2 = s1 {
        g(s2)
    } else {
        0
    "
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend 0x00005ccac74856b7
1  swift-frontend 0x00005ccac74833be
2  swift-frontend 0x00005ccac7485d2f
3  libc.so.6      0x000078fbb3442520
4  libc.so.6      0x000078fbb34969fc pthread_kill + 300
5  libc.so.6      0x000078fbb3442476 raise + 22
6  libc.so.6      0x000078fbb34287f3 abort + 211
7  libc.so.6      0x000078fbb342871b
8  libc.so.6      0x000078fbb3439e96
9  swift-frontend 0x00005ccac0f6f62c
10 swift-frontend 0x00005ccac0f6f0b2
11 swift-frontend 0x00005ccac0f71cd7
12 swift-frontend 0x00005ccac0f673b6
13 swift-frontend 0x00005ccac0f71289
14 swift-frontend 0x00005ccac0f71f20
15 swift-frontend 0x00005ccac0ef2359
16 swift-frontend 0x00005ccac0ef20fb
17 swift-frontend 0x00005ccac0f81e72
18 swift-frontend 0x00005ccac0f6b8a6
19 swift-frontend 0x00005ccac1038a98
20 swift-frontend 0x00005ccac103630d
21 swift-frontend 0x00005ccac0fa3c4b
22 swift-frontend 0x00005ccac0f914f4
23 swift-frontend 0x00005ccac0f81f46
24 swift-frontend 0x00005ccac103af19
25 swift-frontend 0x00005ccac0fb44b3
26 swift-frontend 0x00005ccac0edce84
27 swift-frontend 0x00005ccac0ee6bd6
28 swift-frontend 0x00005ccac19b48af
29 swift-frontend 0x00005ccac0ee0185
30 swift-frontend 0x00005ccac0fa5f02
31 swift-frontend 0x00005ccac0fa5c75
32 swift-frontend 0x00005ccac0f9106b
33 swift-frontend 0x00005ccac0f8524c
34 swift-frontend 0x00005ccac0f61432
35 swift-frontend 0x00005ccac0f6150a
36 swift-frontend 0x00005ccac0f0fff9
37 swift-frontend 0x00005ccac0f6a8f2
38 swift-frontend 0x00005ccac0f72722
39 swift-frontend 0x00005ccac1037761
40 swift-frontend 0x00005ccac103630d
41 swift-frontend 0x00005ccac0fb413f
42 swift-frontend 0x00005ccac0eddb22
43 swift-frontend 0x00005ccac0edeee2
44 swift-frontend 0x00005ccac0edbf6c
45 swift-frontend 0x00005ccac0ee2190
46 swift-frontend 0x00005ccac1035c34
47 swift-frontend 0x00005ccac0ee7b15
48 swift-frontend 0x00005ccac0ee302c
49 swift-frontend 0x00005ccac0729e1f
50 swift-frontend 0x00005ccac073b009
51 swift-frontend 0x00005ccac072d1e1
52 swift-frontend 0x00005ccac072bf01
53 swift-frontend 0x00005ccac0550754
54 libc.so.6      0x000078fbb3429d90
55 libc.so.6      0x000078fbb3429e40 __libc_start_main + 128
56 swift-frontend 0x00005ccac054f7ee

*** Signal 11: Backtracing from 0x78fbb3428898... done ***

*** Program crashed: Bad pointer dereference at 0x0000000000000000 ***

Thread 0 "swift-frontend" crashed:

0  0x000078fbb3428898 <unknown> in libc.so.6


Registers:

rax 0x0000000000000000  0
rdx 0x000078fbb3a7ed00  00 ed a7 b3 fb 78 00 00 10 f7 a7 b3 fb 78 00 00  ·í§³ûx···÷§³ûx··
rcx 0x000078fbb34969fc  41 89 c5 41 f7 dd 3d 00 f0 ff ff b8 00 00 00 00  A·ÅA÷Ý=·ðÿÿ¸····
rbx 0x0000000000000006  6
rsi 0x0000000000000001  1
rdi 0x0000000000000001  1
rbp 0x000078fbb361be90  01 00 00 00 01 00 00 00 00 ed a7 b3 fb 78 00 00  ·········í§³ûx··
rsp 0x00007ffd43ba2b70  20 00 00 00 00 00 00 00 a0 b6 61 b3 fb 78 00 00   ······· ¶a³ûx··
 r8 0x0000000000000000  0
 r9 0x0000000000000000  0
r10 0x0000000000000008  8
r11 0x0000000000000246  582
r12 0x00005ccac9106469  2f 68 6f 6d 65 2f 62 75 69 6c 64 2d 75 73 65 72  /home/build-user
r13 0x0000000000000392  914
r14 0x00005ccac91dfb94  21 53 47 46 2e 56 61 72 4c 6f 63 73 2e 63 6f 75  !SGF.VarLocs.cou
r15 0x00005ccacb5d4450  88 b1 57 c7 ca 5c 00 00 01 00 00 00 00 00 00 00  ·±WÇÊ\··········
rip 0x000078fbb3428898  f4 83 3d 00 36 1f 00 05 75 14 c7 05 f4 35 1f 00  ô·=·6···u·Ç·ô5··

rflags 0x0000000000010246  ZF PF

cs 0x0033  fs 0x0000  gs 0x0000


Images (27 omitted):

0x000078fbb3400000–0x000078fbb35bc341 490fef8403240c91833978d494d39e537409b92e libc.so.6 /lib/x86_64-linux-gnu/libc.so.6

Backtrace took 0.01s

Program terminated with signal: SIGSEGV
Compiler returned: 139


### Expected behavior

Successfully compiles

### Environment

Godbolt:

Swift version 6.0-dev (LLVM 0ad8ad0245d47b4, Swift 01bd2b4)
Target: x86_64-unknown-linux-gnu

Xcode:

swift-driver version: 1.111.2 Apple Swift version 6.0 (swiftlang-6.0.0.5.15 clang-1600.0.22.6)
Target: arm64-apple-macosx14.0


### Additional information

_No response_
@jumhyn-browser jumhyn-browser added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Jul 17, 2024
@AnthonyLatsis
Copy link
Collaborator

Reduction:

func test() async {
  async let val = if let i = Optional<Int>.none {
    i
  } else {
    0
  }
}

Symbolicated:

Assertion failed: (!SGF.VarLocs.count(vd) && "Already emitted this vardecl?"), function bindValue, file SILGenDecl.cpp, line 876.
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: bin/swift-frontend /Users/mac/Desktop/test.swift -emit-silgen
1.	Swift version 6.0-dev (LLVM c7c87ee42989d4b, Swift 8493312b06605f1)
2.	Compiling with effective version 5.10
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for module test)
4.	While silgen emitFunction SIL function "@$s4test1fyyYaF".
 for 'f()' (at /Users/mac/Desktop/test.swift:294:1)
5.	While silgen closureexpr SIL function "@$s4test1fyyYaFSiyYaYbcfu_".
 for expression at [/Users/mac/Desktop/test.swift:295:19 - line:299:3] RangeText="if let s2 = Optional<Int>.none {
    s2
  } else {
    0
  "
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  swift-frontend           0x00000001113601b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 40
1  swift-frontend           0x000000011135e605 llvm::sys::RunSignalHandlers() + 85
2  swift-frontend           0x000000011136080e SignalHandler(int) + 270
3  libsystem_platform.dylib 0x00007ff80d870fdd _sigtramp + 29
4  swift-frontend           0x000000010ad0495a llvm::DenseMapBase<llvm::DenseMap<swift::Lowering::TypeConverter::CachingTypeKey, swift::Lowering::TypeLowering const*, llvm::DenseMapInfo<swift::Lowering::TypeConverter::CachingTypeKey, void>, llvm::detail::DenseMapPair<swift::Lowering::TypeConverter::CachingTypeKey, swift::Lowering::TypeLowering const*>>, swift::Lowering::TypeConverter::CachingTypeKey, swift::Lowering::TypeLowering const*, llvm::DenseMapInfo<swift::Lowering::TypeConverter::CachingTypeKey, void>, llvm::detail::DenseMapPair<swift::Lowering::TypeConverter::CachingTypeKey, swift::Lowering::TypeLowering const*>>::makeIterator(llvm::detail::DenseMapPair<swift::Lowering::TypeConverter::CachingTypeKey, swift::Lowering::TypeLowering const*>*, llvm::detail::DenseMapPair<swift::Lowering::TypeConverter::CachingTypeKey, swift::Lowering::TypeLowering const*>*, llvm::DebugEpochBase&, bool) + 202
5  libsystem_c.dylib        0x00007ff80d767a79 abort + 126
6  libsystem_c.dylib        0x00007ff80d766d68 err + 0
7  swift-frontend           0x0000000109937071 (anonymous namespace)::LetValueInitialization::bindValue(swift::SILValue, swift::Lowering::SILGenFunction&, bool, swift::SILLocation) + 193
8  swift-frontend           0x000000010993630c (anonymous namespace)::LetValueInitialization::copyOrInitValueInto(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, bool) + 300
9  swift-frontend           0x0000000109942a6d (anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7::operator()(swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr&&) const + 2765
10 swift-frontend           0x0000000109941f7a decltype(std::declval<(anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7&>()(std::declval<swift::Lowering::ManagedValue>(), std::declval<swift::Lowering::SwitchCaseFullExpr>())) std::__1::__invoke[abi:v160006]<(anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7&, swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr>((anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7&, swift::Lowering::ManagedValue&&, swift::Lowering::SwitchCaseFullExpr&&) + 74
11 swift-frontend           0x0000000109941f05 void std::__1::__invoke_void_return_wrapper<void, true>::__call<(anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7&, swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr>((anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7&, swift::Lowering::ManagedValue&&, swift::Lowering::SwitchCaseFullExpr&&) + 37
12 swift-frontend           0x0000000109941ecd std::__1::__function::__alloc_func<(anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7, std::__1::allocator<(anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7>, void (swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr&&)>::operator()[abi:v160006](swift::Lowering::ManagedValue&&, swift::Lowering::SwitchCaseFullExpr&&) + 45
13 swift-frontend           0x0000000109941149 std::__1::__function::__func<(anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7, std::__1::allocator<(anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&)::$_7>, void (swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr&&)>::operator()(swift::Lowering::ManagedValue&&, swift::Lowering::SwitchCaseFullExpr&&) + 41
14 swift-frontend           0x000000010983eef2 std::__1::__function::__value_func<void (swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr&&)>::operator()[abi:v160006](swift::Lowering::ManagedValue&&, swift::Lowering::SwitchCaseFullExpr&&) const + 66
15 swift-frontend           0x000000010983e2b7 std::__1::function<void (swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr&&)>::operator()(swift::Lowering::ManagedValue, swift::Lowering::SwitchCaseFullExpr&&) const + 55
16 swift-frontend           0x000000010983dcc1 swift::Lowering::SwitchEnumBuilder::emit() && + 3617
17 swift-frontend           0x000000010993e723 (anonymous namespace)::EnumElementPatternInitialization::emitEnumMatch(swift::Lowering::ManagedValue, swift::EnumElementDecl*, swift::Lowering::Initialization*, swift::Lowering::JumpDest, swift::SILLocation, swift::Lowering::SILGenFunction&) + 1123
18 swift-frontend           0x0000000109943a62 (anonymous namespace)::EnumElementPatternInitialization::copyOrInitValueInto(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::ManagedValue, bool) + 338
19 swift-frontend           0x000000010981d1fe void copyOrInitValuesInto<((anonymous namespace)::ImplodeKind)1>(swift::Lowering::Initialization*, llvm::ArrayRef<swift::Lowering::ManagedValue>&, swift::CanType, swift::SILLocation, swift::Lowering::SILGenFunction&) + 350
20 swift-frontend           0x000000010981d068 swift::Lowering::RValue::forwardInto(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::Initialization*) && + 296
21 swift-frontend           0x000000010996554a swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 570
22 swift-frontend           0x0000000109931086 swift::Lowering::SILGenFunction::emitStmtCondition(llvm::MutableArrayRef<swift::StmtConditionElement>, swift::Lowering::JumpDest, swift::SILLocation, swift::ProfileCounter, swift::ProfileCounter) + 790
23 swift-frontend           0x0000000109adcea6 (anonymous namespace)::StmtEmitter::visitIfStmt(swift::IfStmt*) + 838
24 swift-frontend           0x0000000109ad609b swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 171
25 swift-frontend           0x0000000109ad5fba swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 42
26 swift-frontend           0x00000001099ac1c6 (anonymous namespace)::RValueEmitter::visitSingleValueStmtExpr(swift::SingleValueStmtExpr*, swift::Lowering::SGFContext)::$_30::operator()() const + 54
27 swift-frontend           0x0000000109996ada (anonymous namespace)::RValueEmitter::visitSingleValueStmtExpr(swift::SingleValueStmtExpr*, swift::Lowering::SGFContext) + 682
28 swift-frontend           0x00000001099763fc swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 5900
29 swift-frontend           0x0000000109965671 swift::Lowering::SILGenFunction::emitRValue(swift::Expr*, swift::Lowering::SGFContext) + 209
30 swift-frontend           0x0000000109965475 swift::Lowering::SILGenFunction::emitExprInto(swift::Expr*, swift::Lowering::Initialization*, std::__1::optional<swift::SILLocation>) + 357
31 swift-frontend           0x0000000109ad7507 swift::Lowering::SILGenFunction::emitReturnExpr(swift::SILLocation, swift::Expr*) + 631
32 swift-frontend           0x00000001099c27a3 swift::Lowering::SILGenFunction::emitClosure(swift::AbstractClosureExpr*) + 1059
33 swift-frontend           0x0000000109847d4b swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 3355
34 swift-frontend           0x000000010986b337 swift::Lowering::SILGenModule::emitClosure(swift::AbstractClosureExpr*, swift::Lowering::FunctionTypeInfo const&)::$_4::operator()() const + 311
35 swift-frontend           0x000000010986b1f5 void llvm::function_ref<void ()>::callback_fn<swift::Lowering::SILGenModule::emitClosure(swift::AbstractClosureExpr*, swift::Lowering::FunctionTypeInfo const&)::$_4>(long) + 21
36 swift-frontend           0x0000000108858529 llvm::function_ref<void ()>::operator()() const + 25
37 swift-frontend           0x000000010acee459 swift::Lowering::TypeConverter::withClosureTypeInfo(swift::AbstractClosureExpr*, swift::Lowering::FunctionTypeInfo const&, llvm::function_ref<void ()>) + 313
38 swift-frontend           0x000000010984d585 swift::Lowering::SILGenModule::emitClosure(swift::AbstractClosureExpr*, swift::Lowering::FunctionTypeInfo const&) + 261
39 swift-frontend           0x000000010999a173 (anonymous namespace)::RValueEmitter::emitClosureReference(swift::AbstractClosureExpr*, swift::Lowering::FunctionTypeInfo const&) + 67
40 swift-frontend           0x0000000109999d83 (anonymous namespace)::RValueEmitter::tryEmitConvertedClosure(swift::AbstractClosureExpr*, swift::CanTypeWrapper<swift::AnyFunctionType>, swift::Lowering::Conversion const&) + 259
41 swift-frontend           0x00000001099999f1 (anonymous namespace)::RValueEmitter::visitAbstractClosureExpr(swift::AbstractClosureExpr*, swift::Lowering::SGFContext) + 289
42 swift-frontend           0x000000010998acc6 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visitAutoClosureExpr(swift::AutoClosureExpr*, swift::Lowering::SGFContext) + 70
43 swift-frontend           0x0000000109975603 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::visit(swift::Expr*, swift::Lowering::SGFContext) + 2323
44 swift-frontend           0x0000000109965671 swift::Lowering::SILGenFunction::emitRValue(swift::Expr*, swift::Lowering::SGFContext) + 209
45 swift-frontend           0x000000010996a4c0 swift::Lowering::SILGenFunction::emitRValueAsSingleValue(swift::Expr*, swift::Lowering::SGFContext) + 80
46 swift-frontend           0x000000010992c53a swift::Lowering::SILGenFunction::emitConvertedRValue(swift::Expr*, swift::Lowering::Conversion const&, swift::Lowering::SGFContext)::$_8::operator()(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext) const + 74
47 swift-frontend           0x000000010992c4a3 swift::Lowering::ManagedValue llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>::callback_fn<swift::Lowering::SILGenFunction::emitConvertedRValue(swift::Expr*, swift::Lowering::Conversion const&, swift::Lowering::SGFContext)::$_8>(long, swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext) + 83
48 swift-frontend           0x000000010991b3f7 llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>::operator()(swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext) const + 87
49 swift-frontend           0x000000010991fb96 swift::Lowering::SILGenFunction::emitConvertedRValue(swift::SILLocation, swift::Lowering::Conversion const&, swift::Lowering::SGFContext, llvm::function_ref<swift::Lowering::ManagedValue (swift::Lowering::SILGenFunction&, swift::SILLocation, swift::Lowering::SGFContext)>) + 630
50 swift-frontend           0x000000010991fd13 swift::Lowering::SILGenFunction::emitConvertedRValue(swift::Expr*, swift::Lowering::Conversion const&, swift::Lowering::SGFContext) + 163
51 swift-frontend           0x00000001098871cf swift::Lowering::SILGenFunction::emitAsyncLetStart(swift::SILLocation, swift::SILValue, swift::AbstractClosureExpr*, swift::SILValue) + 1007
52 swift-frontend           0x000000010992fa09 swift::Lowering::SILGenFunction::emitPatternBinding(swift::PatternBindingDecl*, unsigned int, bool) + 937
53 swift-frontend           0x0000000109930326 swift::Lowering::SILGenFunction::visitPatternBindingDecl(swift::PatternBindingDecl*, bool) + 150
54 swift-frontend           0x0000000109945142 swift::ASTVisitor<swift::Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 1522
55 swift-frontend           0x0000000109adc461 (anonymous namespace)::StmtEmitter::visitBraceStmt(swift::BraceStmt*) + 2241
56 swift-frontend           0x0000000109ad6041 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 81
57 swift-frontend           0x0000000109ad5fba swift::Lowering::SILGenFunction::emitStmt(swift::Stmt*) + 42
58 swift-frontend           0x00000001099c220c swift::Lowering::SILGenFunction::emitFunction(swift::FuncDecl*) + 956
59 swift-frontend           0x00000001098481d0 swift::Lowering::SILGenModule::emitFunctionDefinition(swift::SILDeclRef, swift::SILFunction*) + 4512
60 swift-frontend           0x000000010984b9c5 swift::Lowering::SILGenModule::emitOrDelayFunction(swift::SILDeclRef) + 533
61 swift-frontend           0x0000000109847002 swift::Lowering::SILGenModule::emitFunction(swift::FuncDecl*) + 450
62 swift-frontend           0x0000000109846e2d swift::Lowering::SILGenModule::visitFuncDecl(swift::FuncDecl*) + 29
63 swift-frontend           0x0000000109846af4 swift::ASTVisitor<swift::Lowering::SILGenModule, void, void, void, void, void, void>::visit(swift::Decl*) + 996
64 swift-frontend           0x0000000109846700 swift::Lowering::SILGenModule::visit(swift::Decl*) + 64
65 swift-frontend           0x0000000109850c44 (anonymous namespace)::SILGenModuleRAII::emitSourceFile(swift::SourceFile*) + 340
66 swift-frontend           0x00000001098507ef swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const + 879
67 swift-frontend           0x0000000109ad518e std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::__1::integer_sequence<unsigned long, 0ul>) const + 142
68 swift-frontend           0x0000000109ad50a8 swift::SimpleRequest<swift::ASTLoweringRequest, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::evaluateRequest(swift::ASTLoweringRequest const&, swift::Evaluator&) + 40
69 swift-frontend           0x000000010986c9e8 swift::ASTLoweringRequest::OutputType swift::Evaluator::getResultUncached<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 264
70 swift-frontend           0x000000010986c8c8 swift::ASTLoweringRequest::OutputType swift::Evaluator::operator()<swift::ASTLoweringRequest, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'(), (void*)0>(swift::ASTLoweringRequest const&, swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest)::'lambda'()) + 40
71 swift-frontend           0x0000000109851404 swift::ASTLoweringRequest::OutputType swift::evaluateOrFatal<swift::ASTLoweringRequest>(swift::Evaluator&, swift::ASTLoweringRequest) + 36
72 swift-frontend           0x0000000109851320 swift::performASTLowering(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) + 256
73 swift-frontend           0x0000000108a678fb swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 283
74 swift-frontend           0x0000000108a98fd3 performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*)::$_29::operator()(swift::CompilerInstance&) const + 147
75 swift-frontend           0x0000000108a98f2d bool llvm::function_ref<bool (swift::CompilerInstance&)>::callback_fn<performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*)::$_29>(long, swift::CompilerInstance&) + 29
76 swift-frontend           0x0000000108a982f1 llvm::function_ref<bool (swift::CompilerInstance&)>::operator()(swift::CompilerInstance&) const + 33
77 swift-frontend           0x0000000108a97288 withSemanticAnalysis(swift::CompilerInstance&, swift::FrontendObserver*, llvm::function_ref<bool (swift::CompilerInstance&)>, bool) + 392
78 swift-frontend           0x0000000108a8f645 performAction(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 1349
79 swift-frontend           0x0000000108a6ad86 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) + 262
80 swift-frontend           0x0000000108a6978a swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2794
81 swift-frontend           0x0000000108725afa run_driver(llvm::StringRef, llvm::ArrayRef<char const*>, llvm::ArrayRef<char const*>) + 2410
82 swift-frontend           0x0000000108724737 swift::mainEntry(int, char const**) + 1543
83 swift-frontend           0x0000000108723e02 main + 34
84 dyld                     0x00007ff80d4b6366 start + 1942
Abort trap: 6
Anthony-Latsis:swift-macosx-x86_64 mac$ 

@AnthonyLatsis AnthonyLatsis added concurrency Feature: umbrella label for concurrency language features compiler The Swift compiler itself SILGen Area → compiler: The SIL generation stage async & await Feature → concurrency: asynchronous function aka the async/await pattern expressions Feature: expressions let & var Feature: constant and variable declarations if else Feature → statements: 'if' statements swift 6.0 assertion failure Bug → crash: An assertion failure and removed triage needed This issue needs more specific labels labels Jul 18, 2024
@oO0oO0oO0o0o00
Copy link

oO0oO0oO0o0o00 commented Dec 24, 2024

Another variant:

import Foundation

func sample(arg: Int?) async {
  // error: closure captures 'arg' before it is declared
  async let exampleOne = if let arg { arg } else { 0 }
  async let exampleTwo = switch arg {
    // error: closure captures 'value' before it is declared
    case .some(let value): value
    case .none: 0
  }
}

Getting errors:

a.swift:4:26: error: closure captures 'arg' before it is declared
 2 |
 3 | func sample(arg: Int?) async {
 4 |   async let exampleOne = if let arg { arg } else { 0 }
   |                          |      |     `- note: captured here
   |                          |      `- note: captured value declared here
   |                          `- error: closure captures 'arg' before it is declared
 5 |   async let exampleTwo = switch arg {
 6 |     case .some(let value): value

a.swift:5:26: error: closure captures 'value' before it is declared
 3 | func sample(arg: Int?) async {
 4 |   async let exampleOne = if let arg { arg } else { 0 }
 5 |   async let exampleTwo = switch arg {
   |                          `- error: closure captures 'value' before it is declared
 6 |     case .some(let value): value
   |                    |       `- note: captured here
   |                    `- note: captured value declared here
 7 |     case .none: 0
 8 |   }

Seems that the issue appears as early as in sema, and may be related to combining autoclosure with single-stmt expr which is only possible with async let

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
assertion failure Bug → crash: An assertion failure async & await Feature → concurrency: asynchronous function aka the async/await pattern bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself concurrency Feature: umbrella label for concurrency language features crash Bug: A crash, i.e., an abnormal termination of software expressions Feature: expressions if else Feature → statements: 'if' statements let & var Feature: constant and variable declarations SILGen Area → compiler: The SIL generation stage swift 6.0
Projects
None yet
Development

No branches or pull requests

3 participants