-
Notifications
You must be signed in to change notification settings - Fork 928
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
Calling a function literal associated with a function type breaks compilation #320
Comments
Thank you for the find! Yes, that's clearly a bug. There are probably a few of those left in the compiler but they can be hard to find unless someone stumbles on it. A patch would be appreciated. But you can do this even simpler: --- a/compiler/compiler.go
+++ b/compiler/compiler.go
@@ -1338,7 +1338,11 @@ func (c *Compiler) parseCall(frame *Frame, instr *ssa.CallCommon) (llvm.Value, e
}
// This is a func value, which cannot be called directly. We have to
// extract the function pointer and context first from the func value.
- funcPtr, context, err := c.decodeFuncValue(value, instr.Value.Type().(*types.Signature))
+ funcPtr, context, err := c.decodeFuncValue(value, instr.Value.Type().Underlying().(*types.Signature))
if err != nil {
return llvm.Value{}, err
} Every type has an |
When compiling a piece of code where a function value is called, the compiler panics if the function value's type is a defined type, and not just a type literal (function signature): The type assertion (*types.Signature) fails, because the type of the func value is a *types.Named. This patch fixes this by using the type's underlying type, so that a types.Named is properly turned into its underlying types.Signature, before the type assertion takes place. It takes advantage of the property that all types have an underlying type (both are the same, if a type is not named). Fixes tinygo-org#320
Thanks for pointing out the simplification. |
The fix for this has been merged. |
When compiling a piece of code where a function value is called, the compiler panics if the function value's type is a defined type, and not just a type literal (function signature): The type assertion (*types.Signature) fails, because the type of the func value is a *types.Named. This patch fixes this by using the type's underlying type, so that a types.Named is properly turned into its underlying types.Signature, before the type assertion takes place. It takes advantage of the property that all types have an underlying type (both are the same, if a type is not named). Fixes tinygo-org#320
When trying to compile a program calling a function literal of a named type, like in this functional options example, tinygo stops compilation with a message like:
compiler/compiler.go:1341
It looks as if it is sufficient to call the
Underlying
method on the Named type before doing the type cast for*types.Signature
, as in this patch:I can send in a pull request if needed.
The text was updated successfully, but these errors were encountered: