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-7621] Compiler segfault when capturing weak object #50163

Closed
swift-ci opened this issue May 7, 2018 · 3 comments
Closed

[SR-7621] Compiler segfault when capturing weak object #50163

swift-ci opened this issue May 7, 2018 · 3 comments
Labels
accepts invalid Bug: Accepts invalid 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 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 7, 2018

Previous ID SR-7621
Radar rdar://problem/40036644
Original Reporter mohsenr (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Swift 4.1 – Xcode 9.3 (9E145)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, AcceptsInvalid, CompilerCrash, TypeChecker
Assignee None
Priority Medium

md5: 0fff273ce8c21a137a47b9c372a58939

duplicates:

  • SR-5870 Crash when capture list references parameter of outer single-expression closure

Issue Description:

Code to reproduce:

func outer(with augment: (Any) -> Void) {}
func innerVoid(onNext: (() -> Void)) {} 
outer { captured in
    innerVoid { [weak captured] in
    }
}

This is reproducible from both Xcode and `swift` command line. See the attachment for more examples.

Some observations:

  1. The crash is in SILGen so the code must be valid Swift to hit the issue

  2. Strong capture of the object in the inner closure is fine.

  3. Compiler is fine if the object being captured is global, crash happens only if it’s an argument.

To make sure this is not a duplicate, tried the snippet from the following bugs in the same version of compiler, none of them caused a segfault:

@belkadan
Copy link
Contributor

belkadan commented May 7, 2018

The code isn't valid; you can't use a weak reference with Any. (Just because the compiler fails to emit an error doesn't mean the code is valid; maybe that's the bug, like it is here.)

@swift-ci create

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 7, 2018

Comment by Mo Ramezanpoor (JIRA)

Thanks, you’re right. I used “valid” incorrectly there. Though that doesn’t actually make a difference if you check the other samples. Using `AnyObject`, or even a concert type still causes it to crash. Given:

class MyClass {}
func outer(_ work: (MyClass) -> Void) {}
func inner(_ work: (() -> Void)) {}

outer { captured in
    inner { [weak captured] in
    }
}

We have a segfault with this output:

While silgen closureexpr SIL function "@_T03BugyAA7MyClassCcfU_".
 for expression at [<path>/Bug.swift:5:7 - line:8:1] RangeText="{ captured in
    inner { [weak captured] in
    }
}"

which made me think the issue is in SILGen.

@swift-ci
Copy link
Collaborator Author

Comment by Mo Ramezanpoor (JIRA)

I’ve managed to narrow down what the problem is, but I’m not sure how it should be fixed:

  1. The type checker performs a pre-check before solving the types of `outer` invocation. here

  2. As part of the pre-check, if it’s a single-expression closure, it’ll pre-check the body, which is `inner` invocation. here

  3. The inner closure’s capture list is type checked during this pre-check. here

  4. The problem is the outer `captured` does not have a type at this point. This causes it to be marked as invalid. here

  5. The invalid type is propagated to the captured list:

    (var_decl "captured" type='<<error type>>' interface type='<<error type>>' access=private storage_kind=stored)
      
    
  6. The type checker then continues and “fixes” the outer `captured`’s type, but the inner type is not fixed: here

    (parameter "captured" type='AnyObject' interface type='AnyObject')
       
    
  7. Later, this is caught by the AST verifier, which aborts the process. here

The bug only occurs when the closure is single-expression. This, for example, compiles fine:

outer { captured in
    1
    inner { [weak captured] in
    }
}

@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
accepts invalid Bug: Accepts invalid 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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants