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-7883] Compiler segfault while trying to use optional method of objc protocol #50418

Open
swift-ci opened this issue Jun 5, 2018 · 11 comments
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

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jun 5, 2018

Previous ID SR-7883
Radar rdar://problem/40834654
Original Reporter Lebedev (JIRA User)
Type Bug

Attachment: Download

Environment

Xcode Version 10.0 beta (10L176w)

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

md5: 5d158e90f50907bcb98263b0fba4fc29

Issue Description:

@protocol FormField <NSObject>
 
@optional
- (NSNumber*) maxLength;
 

then usage, segfaulting the compiler with code 11:

let value = field.maxLength?().intValue ?? defaultMaxLength

one of the workarounds was to add more `?`:

let maxLength = f.maxLength?()?.intValue ?? defaultMaxLength

Which kinda makese sense as both method and return value are "optional" in objc declaration, nevertheless diagnostics could be imroved, probably.

Please see example attached.

@xedin
Copy link
Member

xedin commented Jun 6, 2018

@swift-ci create

@xedin
Copy link
Member

xedin commented Jun 6, 2018

Looks like this only happens when methods are imported, since this swift code is going to produce a correct error about missing '?':

import Foundation

struct S {
  var fn: (() -> NSNumber?)!
}

func foo(_ s: S) {
  _ = s.fn?().intValue
}

@xedin
Copy link
Member

xedin commented Jun 6, 2018

/cc @rudkx Looks like this is related to new IOUs, in the code like in my previous comment optional is not going to be stripped from NSNumber, new disjunction is going to be `(() -> NSNumber?)?` and `() -> NSNumber?` where in attached example it's going to be `() -> NSNumber?` and `() -> NSNumber` which seems strange...

@belkadan
Copy link
Contributor

belkadan commented Jun 6, 2018

It would be really good if we could preserve this particular IUO, even apart from the crash.

@xedin
Copy link
Member

xedin commented Jun 7, 2018

@rudkx I'd like to help with this one but I'd appreciate a pointer, because it seems like we shouldn't always strip way optional from the result type but only when declaration has IOU attribute set otherwise we'd produce a solution which can't be applied to AST, but there is no way to get to declaration from the function type if it's passed as one of the parameters, is that right?

@belkadan
Copy link
Contributor

belkadan commented Jun 7, 2018

In this case there's a declaration with an IUO return type (the imported maxLength method) that's also optional. I can't actually find where we handle optional (see OptionalAttr).

@belkadan
Copy link
Contributor

belkadan commented Jun 7, 2018

…sorry, "and therefore there's no parameter involved".

@xedin
Copy link
Member

xedin commented Jun 7, 2018

@belkadan Yes, that's the case, it's going to be IOU declaration (but it wouldn't have wrapping optional type) and type of the result is going to be optional because we don't allow to write things like (() -> Int) anymore, it's replaced with (() -> Int?)!

@belkadan
Copy link
Contributor

belkadan commented Jun 7, 2018

Yes, I get what's happening. It might be worth figuring out a way to model this besides a single type (() -> Int?)?, so as not to break the otherwise-reasonable original code.

@rudkx
Copy link
Member

rudkx commented Jun 8, 2018

@xedin I spent time this afternoon looking at this. It's not an easy fix. I'm going to look at reworking how some of the constraints are set up for IUOs and how some of the rewriting is done.

@xedin
Copy link
Member

xedin commented Jun 8, 2018

@rudkx No problem, I'm going to assign to one to you then.

@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
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
Projects
None yet
Development

No branches or pull requests

5 participants