Skip to content

Several small fixes to generics handling #1170

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 3 commits into from
Oct 31, 2022

Conversation

nevkontakte
Copy link
Member

  • Fix gorepo test runner to handle another variant of the -gcflags="-G=3" flag.
  • Correctly return varFuncLocal declaration level for JS variables corresponding to local variables.
  • Correctly zero-initialize variables of generic types.

Updates #1013

Previously it would incorrectly return `varGenericFactory` for local
variables which type was exactly the type parameter, instead of
`varFuncLocal`. This was causing local variables not to get declared in
the list of local variables.

This change brings typeparams tests to 33 pass / 98 fail.
Since we don't know the concrete type at compile time, we use a runtime
call `TypeName.zero()` to get the zero value.

In the compiler side this works in a bit of a roundabout way:
 - Statements like `var x int` are converted to `x := 0`, where zero
   value is determined by the compiler based on the variable's
   underlying type.
 - For a generic type `T` the underlying type is interface, so the
   compiler rewrites `var x T` as `x := nil // x has type T`. This is
   not entirely correct from the language specification perspective,
   because there is no universal zero value literal for a generic type
   T. However, this is the closest approximation we can express as an
   AST.
 - When compiling a nil literal into a generic-typed expression we
   assume this was meant to represent the zero value and invoke the
   runtime `T.zero()` method for the generic type T.
@nevkontakte nevkontakte requested a review from flimzy October 27, 2022 22:32
@nevkontakte
Copy link
Member Author

/cc @paralin

@nevkontakte
Copy link
Member Author

Since these are relatively small changes, I'm going to go ahead and merge them now.

@nevkontakte nevkontakte merged commit eb675d8 into gopherjs:generics Oct 31, 2022
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.

1 participant