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

[SR-13793] Local function forward reference crash when declared after return #56190

Closed
swift-ci opened this issue Oct 29, 2020 · 4 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13793
Radar rdar://problem/70821486
Original Reporter guitardog (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 5044c3ff6a500995411d4ad6dc423f96

Issue Description:

Attempting to compile the following program will crash the compiler:

func foo() -> Int {
  func a(_ x: Int) -> Int {
    return b(x)
  }  
  
  return a(100)  
  
  func b(_ x: Int) -> Int {
    return x + 1
  }
}

Compiler output:

> swiftc test2.swift
test2.swift:8:3: warning: code after 'return' will never be executed
  func b(_ x: Int) -> Int {
  ^
Global is external, but doesn't have external or weak linkage!
i64 (i64)* @"$s5test23fooSiyF1bL_yS2iF"
<unknown>:0: error: fatal error encountered during compilation; please file a bug report with your project and the crash log
<unknown>:0: note: Broken module found, compilation aborted!
Stack dump:
0.  Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file test2.swift -target x86_64-apple-darwin20.1.0 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk -color-diagnostics -target-sdk-version 11.0 -module-name test2 -o /var/folders/9h/l__gbp5s4z79p76b4c8vgqvr0000gn/T/test2-57ab32.o
1.  Apple Swift version 5.3.1 (swiftlang-1200.0.41 clang-1200.0.32.8)
0  swift                    0x000000010e411c15 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 37
1  swift                    0x000000010e412332 SignalHandler(int) + 610
2  libsystem_platform.dylib 0x00007fff205c0d7d _sigtramp + 29
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 18446603339973259936
4  libsystem_c.dylib        0x00007fff204ce720 abort + 120
5  swift                    0x0000000109c1a7a1 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*)::$_1::__invoke(void*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) + 1137
6  swift                    0x000000010e3751a7 llvm::report_fatal_error(llvm::Twine const&, bool) + 279
7  swift                    0x000000010e375082 llvm::report_fatal_error(char const*, bool) + 50
8  swift                    0x000000010e31639c (anonymous namespace)::VerifierLegacyPass::doFinalization(llvm::Module&) + 188
9  swift                    0x000000010e2b5ff2 llvm::FPPassManager::doFinalization(llvm::Module&) + 50
10 swift                    0x000000010e2b54a0 llvm::legacy::FunctionPassManagerImpl::doFinalization(llvm::Module&) + 80
11 swift                    0x0000000109fc58ef swift::performLLVM(swift::IRGenOptions const&, swift::DiagnosticEngine&, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 4991
12 swift                    0x0000000109c29ba8 performCompileStepsPostSILGen(swift::CompilerInstance&, swift::CompilerInvocation const&, std::__1::unique_ptr<swift::SILModule, std::__1::default_delete<swift::SILModule> >, llvm::PointerUnion<swift::ModuleDecl*, swift::SourceFile*>, swift::PrimarySpecificPaths const&, int&, swift::FrontendObserver*) + 3944
13 swift                    0x0000000109c19603 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 21363
14 swift                    0x0000000109b9a2c1 main + 1265
15 libdyld.dylib            0x00007fff20597591 start + 1
<unknown>:0: error: unable to execute command: Abort trap: 6
<unknown>:0: error: compile command failed due to signal 6 (use -v to see invocation)

Moving the return statement to after the declaration of `b` does not cause any sort of crash or compiler warning. If I'm not mistaken, this type of forward reference from function `a` to function `b` is legal in Swift.

Bug repro inspired by discussion here: https://forums.swift.org/t/clarify-scoping-behavior-with-local-functions/40558

@swift-ci
Copy link
Collaborator Author

Comment by Josh Learn (JIRA)

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Jeet Parte (JIRA)

A simpler version of this program crashes too:
func foo() -> Int {
return b(100)

func b(_ x: Int) -> Int {
return x + 1
}
}
Apple Swift version 5.4 (swiftlang-1205.0.26.9 clang-1205.0.19.55)

@mgadda
Copy link
Mannequin

mgadda mannequin commented Sep 23, 2021

Relatedly, this also produces a swiftc crash with the same call stack as produced by partj (JIRA User)'s example above.

func outer() {
  inner()
  return
  func inner() {}
} 

Produced using swiftc --version:

swift-driver version: 1.26.9 Apple Swift version 5.5 (swiftlang-1300.0.31.1 clang-1300.0.29.1)

Target: x86_64-apple-macosx11.0

It does not produce a crash with:

Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15)

Target: x86_64-apple-darwin20.6.0

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@slavapestov
Copy link
Member

@jckarter fixed this recently: #40792

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants