tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

fragment is larger than or outside of variable when compiling slices.Clone() generic function call

Open yoursunny opened this issue 1 year ago • 0 comments

main.go

package main

// Clone is copied from golang.org/x/exp/slices package.
func Clone[S ~[]E, E any](s S) S {
        // Preserve nil in case it matters.
        if s == nil {
                return nil
        }
        return append(S([]E{}), s...)
}

func main() {
        x := make([]int, 2)
        _ = Clone(x)
}

Commands and error message:

$ tinygo version
tinygo version 0.24.0 linux/amd64 (using go version go1.18.4 and LLVM version 14.0.0)

$ tinygo build -target=wasm main.go
fragment is larger than or outside of variable
  call void @llvm.dbg.value(metadata i32 %s.cap, metadata !30, metadata !DIExpression(DW_OP_LLVM_fragment, 64, 32)), !dbg !37
!30 = !DILocalVariable(name: "s", arg: 1, scope: !26, file: !10, line: 4, type: !31)
error: verification error after compiling package command-line-arguments

$ ../tinygo/build/tinygo version  # also errors when testing TinyGo compiled from `fix-issue2951` branch
tinygo version 0.25.0-dev-9554cde5 linux/amd64 (using go version go1.18.4 and LLVM version 14.0.0)

yoursunny avatar Jul 18 '22 19:07 yoursunny

Still reproducible in the dev branch. I have simplified this to the following:

package main

func Clone[S ~[]E, E any](s S) {
}

func main() {
        Clone([]int(nil))
}

aykevl avatar Aug 21 '22 14:08 aykevl

The bug appears to be that s is somehow considered to be of type any (while it is of type []int).

EDIT: upgrading golang.org/x/tools/ doesn't help, unfortunately.

EDIT 2: found a workaround to fix this issue.

aykevl avatar Aug 21 '22 15:08 aykevl

@aykevl Can you share the details/branch for your fix?

I run into this issue today and I can test the workaround in my use case with generics.

pkedy avatar Aug 21 '22 17:08 pkedy

@pkedy I made a PR, see https://github.com/tinygo-org/tinygo/pull/3086

aykevl avatar Aug 21 '22 21:08 aykevl

@aykevl I confirmed that it works for me! Thanks!

pkedy avatar Aug 22 '22 13:08 pkedy

I had to delete LLVM and rebuild everything from scratch, but then it is working properly in my use case.

$ ../tinygo/build/tinygo version
tinygo version 0.26.0-dev-f6e6aca8 linux/amd64 (using go version go1.19 and LLVM version 14.0.0)

yoursunny avatar Aug 22 '22 16:08 yoursunny

Let's leave open until next release, when we will close it.

deadprogram avatar Aug 22 '22 21:08 deadprogram