-
Notifications
You must be signed in to change notification settings - Fork 10.6k
SILGen: Don't consider a function pointer call to lie about nonoptional returns. #2368
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
Conversation
|
@swift-ci Please test and merge |
lib/SILGen/SILGenExpr.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A non-object pointer type should also get this behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably need a new canBeNullableInC predicate then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, mayLieAboutNonOptionalReturn already does this check; it just does it incorrectly because it doesn't notice that the decl might be a member. (And it also gets it wrong in the same way.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrose-apple It isn't clear to me how mayLieAboutNonOptionalReturn currently handles UnsafePointers. AFAICT it only checks hasReferenceSemantics, which is only true for class, metatype, and function types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's what I meant: it looks like we already get this wrong. I'm not happy about us now checking in two places; it seems redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this extra check because ApplyExprs can only lie about their return nullability if the result of the application is nullable in C. The existing code recurs into the ApplyExpr's subexpression but loses that context.
|
@slavapestov @bitjammer Do you recognize this test failure? |
|
There's a related-looking failure in the Linux build too. |
|
@swift-ci Please test and merge |
|
@jckarter Jordan already disabled the test. I'm working on it now. |
|
@slavapestov @jrose-apple The |
|
@swift-ci Please test |
lib/SILGen/SILGenExpr.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrose-apple I updated the mayLieAboutOptionalReturn code to check for pointer types and factored out the isNullableTypeInC check into a separate helper function. This look good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, there are several more pointer types: OpaquePointer, Selector, and…well, I guess we don't have to care about NSZone.
I'm not happy about this list being hardcoded in more and more places, though. I suppose it's not enough to check the Clang type because that would include things like va_list, which might coincidentally be pointers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using hasReferenceSemantics to cover (thin) function pointers also feels a little sketchy, but I suppose we're not ever going to change that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a list somewhere in the Clang importer we could adapt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately it's not defined in terms of Swift types. https://github.com/apple/swift/blob/master/lib/ClangImporter/ClangImporter.cpp#L1533
…ugh, it looks like that code is wrong too.
|
@swift-ci Please test |
lib/SILGen/SILGenExpr.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrose-apple Updated again to handle OpaquePointer and Selector. Any other concerns?
|
@swift-ci Please test |
- Now that *Pointer types are imported with nullability, there's the potential for non-object pointer APIs to lie about their nullability, so extend the hack to cover them. - We would incorrectly consider a call like "struct.functionPointer()", where functionPointer is a property of block or function pointer type, to be able to lie about its return type nullability, even if the function pointer's own return type was a value type that isn't nullable. This would lead us to generate nonsense bitcast instructions from () to ()? that would get lowered to traps in IRGen. Fix this by checking for nullable types only on the types of expressions, where we have semantic information enough to understand what the types really mean, and only checking the Clang-ness of declarations. Fixes rdar://problem/23346344.
|
@swift-ci Please test |
|
@jrose-apple As we discussed via IM, I've refactored |
|
Yeah, makes sense. Thanks for working through this! |
|
No problem, thanks for the help @jrose-apple! |
We would incorrectly consider a call like
struct.functionPointer(), where functionPointer is a property of block or function pointer type, to be able to lie about its return type nullability, even if the function pointer's own return type was a value type that isn't nullable. This would lead us to generate nonsense bitcast instructions from()to()?that would get lowered to traps in IRGen. Fixes rdar://problem/23346344.