Skip to content

[go1.19] Fix build issue and add override signature directive #1260

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

Merged
merged 2 commits into from
Jan 19, 2024

Conversation

grantnelson-wf
Copy link
Collaborator

@grantnelson-wf grantnelson-wf commented Jan 16, 2024

  • The build updates changes I did will still remove imports which are needed by directive only (i.e. unsafe for go:linkname and embed for go:embed). These directives are also read from the top-level comments by the compiler so we may not simply clear out the top-level comments.

  • The import remover also had to deal with files like src/math/rand/race_test.go which has a dot import ( import . "math/rand"). Typically we leave dot imports alone because it is hard to determine if they are used or not, however in this case, the test is removed making the import unused. So now, if the file is empty of anything but imports or the go:linkname directive (we don't have to worry about the embed since that needs a target variable), clear out everything including dot and blank (_) imports.

  • Add //gopherjs:override-signature directive which allows us to replace a function signature with another while not modifying the body of the function. The two functions will still have to match FuncKey which is based off the receiver and function name.

@grantnelson-wf grantnelson-wf marked this pull request as ready for review January 16, 2024 20:51
@grantnelson-wf grantnelson-wf changed the title Fix build issue and add replace signature directive [go1.19] Fix build issue and add replace signature directive Jan 16, 2024
@grantnelson-wf grantnelson-wf changed the title [go1.19] Fix build issue and add replace signature directive [go1.19] Fix build issue and add override signature directive Jan 16, 2024
Copy link
Member

@nevkontakte nevkontakte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a separate pragma for overriding signatures, given that we already have the ability to override whole functions? It seems like it would only be useful in a very narrow set of cases.

Also, could you please add all newly added directives to doc/pragma.md? I forgot to ask about that in the earlier PR, sorry.

@grantnelson-wf
Copy link
Collaborator Author

grantnelson-wf commented Jan 19, 2024

@nevkontakte TBH we probably don't need to have //gopherjs:override-signature forever. After go1.19 it could probably be removed since it was for a specific reason. There are also probably other simple ways to deal with this problem that I haven't thought of yet.

The main reason I added it was for some methods in crypto that have type parameters (e.g. func (curve *nistCurve[Point]) pointFromAffine(x, y *big.Int) (p Point, err error)). I did not want to copy/paste and maybe accidentally mess up cryptology for GopherJS. Since it dealt with generics //gopherjs:keep-original would not work for what we needed. So I felt it was better to just override the signature as a way to remove the type parameters and replace the type parameter usage in the arguments with a wrapper object that implements the methods called inside of that function. It wouldn't have worked if the type parameter was for a specific type constraint (e.g. func Sort[S ~[]E, E cmp.Ordered](x S)) instead of an interface constraint. Here is how I used that in the native override files.

@grantnelson-wf
Copy link
Collaborator Author

@nevkontakte I also added to doc/pragma.md. I'm very dyslexic and make grammar/spelling mistakes all the time so if you find anything I need to correct please let me know. I try really hard to triple check anything I write (sadly English is my only language, if you don't count science 😄. I joke a lot about dreaming in math but not in English).

@nevkontakte
Copy link
Member

Thanks for the explanation! I was not opposed to addition of the directive per se, but I wanted to make sure I fully understand the logic behind it instead of making my own assumptions :)

I did not want to copy/paste and maybe accidentally mess up cryptology for GopherJS.

That's actually a strong argument. Messing with cryptography makes bad things happen 🙃

I'm very dyslexic and make grammar/spelling mistakes all the time so if you find anything I need to correct please let me know.

That makes the two of us, as you may have notices from typos in comments 😄 No worries! If I spot anything, I'll let you know.

Copy link
Member

@nevkontakte nevkontakte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution!

@nevkontakte nevkontakte merged commit 1faba3b into gopherjs:go1.19 Jan 19, 2024
@grantnelson-wf grantnelson-wf deleted the fixBuildIssue branch January 19, 2024 20:23
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