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

Invalid behavior when calling variadic methods #900

Open
jopbrown opened this issue Oct 10, 2021 · 2 comments
Open

Invalid behavior when calling variadic methods #900

jopbrown opened this issue Oct 10, 2021 · 2 comments
Assignees
Labels
bug Bug: something already implemented does not work as it should emitter/builder Related to VM's emitter and builder InvalidBehavior A valid or not valid code has an invalid behavior at runtime needsInvestigation Need to investigate

Comments

@jopbrown
Copy link

Here is a go package chain with chain call API

package chain

func Echo(name string, args ...string) {
	for _, arg := range args {
		println(name, arg)
	}
}

type Chain struct {
	name string
}

func New() *Chain {
	return &Chain{}
}

func (c *Chain) SetName(name string) *Chain {
	return &Chain{name: name}
}

func (c *Chain) Echo(args ...string) *Chain {
	for _, arg := range args {
		println(c.name, arg)
	}

	return c
}

Import into scriggo

IMPORT chain

Execute code

package main

import "chain"

func main() {
	println("echo without chain")
	chain.Echo("name1")               // print nothing
	chain.Echo("name2", "aaa", "bbb") // print 2 lines

	println()

	println("echo with chain")
	chain.New().SetName("name3").Echo()             // print nothing
	chain.New().SetName("name4").Echo("aaa", "bbb") // print 2 lines
}

Scriggo output

echo without chain
name2 aaa
name2 bbb

echo with chain
name3 name3
name4 aaa
name4 bbb
name4 bbb

gc output

echo without chain
name2 aaa
name2 bbb

echo with chain
name4 aaa
name4 bbb
@zapateo zapateo added bug Bug: something already implemented does not work as it should InvalidBehavior A valid or not valid code has an invalid behavior at runtime needsInvestigation Need to investigate labels Oct 11, 2021
@zapateo zapateo changed the title Invalid behavior in chain call on variadic function Invalid behavior when calling variadic methods Oct 11, 2021
@zapateo
Copy link
Member

zapateo commented Oct 11, 2021

Thanks @jopbrown for reporting this issue, we're investigating on it.

@zapateo
Copy link
Member

zapateo commented Oct 11, 2021

The invalid behaviour can be reduced to:

// Scriggo code.
package main

import "chain"

func main() {
	t := chain.T{}
	t.M("a")
}
// Native code.
package chain

import "fmt"

type T struct{}

func (c T) M(args ...string) {
	fmt.Printf("%#v (len %d)\n", args, len(args))
}

which should print:

[]string{"a"} (len 1)

but instead it prints:

[]string{"a", ""} (len 2)

@gazerro gazerro assigned zapateo and unassigned gazerro Oct 11, 2021
@gazerro gazerro added the emitter/builder Related to VM's emitter and builder label Oct 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug: something already implemented does not work as it should emitter/builder Related to VM's emitter and builder InvalidBehavior A valid or not valid code has an invalid behavior at runtime needsInvestigation Need to investigate
Projects
None yet
Development

No branches or pull requests

3 participants