Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix compiler panic when handling composite literals representing named pointer types. #1208

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 1 commit into from
Jun 24, 2023

Conversation

nevkontakte
Copy link
Member

Prior to this change, the following code would panic the compiler:

type (
	S  struct{ f int }
	PS *[]S
)

var _ = []*S{{}} // This compiles, but potentially incorrectly.
var _ = []PS{{}} // Compiler panics here.

Prior to this change, GopherJS compiler didn't expect that a pointer type could be named in this context, and would panic. This is addressed by checking the underlying type, rather than the type itself.

However, there was a bigger correctness problem here too. According to the Go spec:

Within a composite literal of array, slice, or map type T, elements or map keys that are themselves composite literals may elide the respective literal type if it is identical to the element or key type of T. Similarly, elements or keys that are addresses of composite literals may elide the &T when the element or key type is *T.

So in the example above, []PS{{}} expression is equivalent to []PS{PS(*S{})}. However, even with the first part of the fix, the code emitted by the compiler would have been equivalent to []PS{S{}}. This mostly works because at runtime GopherJS represents a pointer to the struct and the struct type as the same JS object, but it would break down when it comes to methods and non-struct types. So the second part of the fix is to generate the explicit AST for taking an address of the value type and type conversion, and compiling that.

@nevkontakte nevkontakte requested a review from flimzy June 23, 2023 20:14
…d pointer types.

Prior to this change, the following code would panic the compiler:

```go

type (
	S  struct{ f int }
	PS *[]S
)

var _ = []*S{{}} // This compiles, but potentially incorrectly.
var _ = []PS{{}} // Compiler panics here.
```

Prior to this change, GopherJS compiler didn't expect that a pointer type could be named in this context, and would panic. This is addressed by checking the underlying type, rather than the type itself.

However, there was a bigger correctness problem here too. According to the Go spec:

> Within a composite literal of array, slice, or map type T, elements or map keys that are themselves composite literals may elide the respective literal type if it is identical to the element or key type of T. Similarly, elements or keys that are addresses of composite literals may elide the &T when the element or key type is *T.

So in the example above, `[]PS{{}}` expression is equivalent to `[]PS{PS(*S{})}`. However, even with the first part of the fix, the code emitted by the compiler would have been equivalent to `[]PS{S{}}`. This mostly works because at runtime GopherJS represents a pointer to the struct and the struct type as the same JS object, but it would break down when it comes to methods and non-struct types. So the second part of the fix is to generate the explicit AST for taking an address of the value type and type conversion, and compiling that.
@nevkontakte nevkontakte merged commit aa25f6c into gopherjs:master Jun 24, 2023
@nevkontakte nevkontakte deleted the composite-lit branch June 26, 2023 19:13
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.