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-11509] Compiler crash when referencing out-of-scope value in property wrapper parameters #53910

Closed
JaviSoto opened this issue Sep 23, 2019 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software property wrappers Feature: property wrappers

Comments

@JaviSoto
Copy link
Contributor

Previous ID SR-11509
Radar None
Original Reporter @JaviSoto
Type Bug
Status Resolved
Resolution Done
Environment

Swift 5.1 in Xcode 11 GM2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, CompilerCrash, PropertyWrappers
Assignee @theblixguy
Priority Medium

md5: 864e467dd8ecdba5276417cf6e2fecdd

Issue Description:

The following code crashes the compiler:

@propertyWrapper
public struct Wrapper<Value> {
    public init(someValue unused: Int) {
        _ = unused
    }
    
    public var wrappedValue: Value?
}


func functionScope() {
    let scopedValue = 3
    
    enum StaticScope {
        @Wrapper(someValue: scopedValue)
        static var foo: String
    }
    
    _ = StaticScope.foo
}

With this error:

Untitled 3.swift:11:6: warning: initialization of immutable value 'scopedValue' was never used; consider replacing with assignment to '_' or removing it
        let scopedValue = 3
        ~~~~^~~~~~~~~~~
        _
Global is external, but doesn't have external or weak linkage!
i8* ()* @"$s4main13functionScopeyyF11scopedValueL_Sivau"
<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/Xcode11.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file Untitled 3.swift -target x86_64-apple-darwin18.7.0 -enable-objc-interop -sdk /Applications/Xcode11.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -module-name main -o /var/folders/h6/fxpl2k1d24q8ylv1m_rknt280000gn/T/Untitled 3-ab7650.o 
0  swift                    0x000000010b17feb3 PrintStackTraceSignalHandler(void*) + 51
1  swift                    0x000000010b17f686 SignalHandler(int) + 358
2  libsystem_platform.dylib 0x00007fff5b252b5d _sigtramp + 29
3  libsystem_platform.dylib 0x00000001169a6b76 _sigtramp + 3145023542
4  libsystem_c.dylib        0x00007fff5b10c6a6 abort + 127
5  swift                    0x0000000106dce330 std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_delete<swift::DiagnosticConsumer> > llvm::function_ref<std::__1::unique_ptr<swift::DiagnosticConsumer, std::__1::default_delete<swift::DiagnosticConsumer> > (swift::InputFile const&)>::callback_fn<createSerializedDiagnosticConsumerIfNeeded(swift::FrontendInputsAndOutputs const&)::$_15>(long, swift::InputFile const&) + 0
6  swift                    0x000000010b0f8cd6 llvm::report_fatal_error(llvm::Twine const&, bool) + 278
7  swift                    0x000000010b0f8bbb llvm::report_fatal_error(char const*, bool) + 43
8  swift                    0x000000010b0b8e9c (anonymous namespace)::VerifierLegacyPass::doFinalization(llvm::Module&) + 204
9  swift                    0x000000010b05a423 llvm::FPPassManager::doFinalization(llvm::Module&) + 51
10 swift                    0x000000010b062a72 llvm::legacy::FunctionPassManagerImpl::doFinalization(llvm::Module&) + 82
11 swift                    0x0000000106fe60f5 swift::performLLVM(swift::IRGenOptions&, swift::DiagnosticEngine*, llvm::sys::SmartMutex<false>*, llvm::GlobalVariable*, llvm::Module*, llvm::TargetMachine*, swift::version::Version const&, llvm::StringRef, swift::UnifiedStatsReporter*) + 4677
12 swift                    0x0000000106dd7de3 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 38307
13 swift                    0x0000000106dcb034 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 6820
14 swift                    0x0000000106d585a3 main + 1219
15 libdyld.dylib            0x00007fff5b0673d5 start + 1
16 libdyld.dylib            0x000000000000000e start + 2767817786
<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)

Note the warning as well, it seems the compiler doesn't realize we're using the scopedValue variable, if it did, it may be able to diagnose that that's not correct (in fact, this works and we get a correct compiler error if scopedValue is a member of a type functionScope is part of instead of a local variable of the function)

@theblixguy
Copy link
Collaborator

It doesn't crash for me on master and instead I get the following diagnostics:

/Users/suyashsrijan/Desktop/test.swift:15:4: error: generic parameter 'Value' could not be inferred
                @Wrapper(someValue: scopedValue)
                 ^
/Users/suyashsrijan/Desktop/test.swift:2:23: note: 'Value' declared as parameter to type 'Wrapper'
public struct Wrapper<Value> {
                      ^
/Users/suyashsrijan/Desktop/test.swift:15:4: error: cannot convert value of type 'String' to specified type 'Any?'
                @Wrapper(someValue: scopedValue)
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                 as Any

@theblixguy
Copy link
Collaborator

I do get the following assertion failure if I change the type of foo to be String?:

Assertion failed: (!getDeclContext()->isLocalContext() && "not a global variable!"), function isLazilyInitializedGlobal, file /Users/suyashsrijan/Documents/swift-src/swift/lib/AST/Decl.cpp, line 5113.

@theblixguy
Copy link
Collaborator

Fixed on master by #27311

@JaviSoto
Copy link
Contributor Author

Wow that was fast, thank you!!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
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 crash Bug: A crash, i.e., an abnormal termination of software property wrappers Feature: property wrappers
Projects
None yet
Development

No branches or pull requests

3 participants