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

fix: Add attribute nonnull_error if returning pointer and error from Objective-C #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jefft0
Copy link

@jefft0 jefft0 commented Aug 30, 2022

Consider the following Go code with a method that returns a byte array and an error:

package example

type TestClass struct {
}
func (req *TestClass) GetData() ([]byte, error) {
        return nil, nil
}

gobind creates an Objective-C method which returns NSData* and an error:

- (NSData* _Nullable)getData:(NSError* _Nullable* _Nullable)error {
    ....
}

The method signature seen by Swift is func getData() throws -> Data . The problem is that Data is not optional, and when the Objective-C method returns NULL the program crashes. This is the default signature as explained in the Swift documentation: https://clang.llvm.org/docs/AttributeReference.html#swift-error

But this is not appropriate for gobind since it is valid for Go to return a nil value. We don't want the crash. This pull request updates asSignature so that if the method returns an error and the method returns a nullable pointer to NSData or NSString, then add the nonnull_error attribute as explained in the Swift documentation.

With this attribute, the method signature seen by Swift is func getData() throws -> Data? . When the Go function returns nil, it is returned as a nil optional value without crashing.

@google-cla
Copy link

google-cla bot commented Aug 30, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gopherbot
Copy link
Contributor

This PR (HEAD: e9ab6b9) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/mobile/+/426595 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/426595.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

This PR (HEAD: b50c057) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/mobile/+/426595 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants