gopherjs / gopherjs/gopherjs

Second enclosed function can't access pointer

Open
#402 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Go
Stars
13.2k
Forks
573
PR merge metrics
No merged PRs in 30d

Description

Consider the following code:

package main

import "fmt"

func main() {
    var x int
    go func() {
        fmt.Println(&x)
    }()
    go func() {
        fmt.Println(&x)
    }()
}

The output of 'gopherjs run' is:

$ gopherjs run x.go
gopherjs: Source maps disabled. Use Node.js 4.x with source-map-support module for nice stack traces.
0x1
/home/jonhall/go/src/github.com/flimzy/x/x.go.705403117:1472
        throw err;
        ^

ReferenceError: x$24ptr is not defined
    at $b (/home/jonhall/go/src/github.com/flimzy/x/x.go.705403117:15912:37)
    at $goroutine (/home/jonhall/go/src/github.com/flimzy/x/x.go.705403117:1463:19)
    at $runScheduled [as _onTimeout] (/home/jonhall/go/src/github.com/flimzy/x/x.go.705403117:1504:7)
    at Timer.listOnTimeout (timers.js:92:15)

When I look at the compiled JS output, I see the apparent problem (with line numbers):

15902       $go((function $b() {
15903           var $ptr, _r, x$24ptr, $s, $r;

15909       $go((function $b() {
15910           var $ptr, _r, $s, $r;

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided program with gopherjs run and inspect the generated JavaScript around lines 15902-15910. Compare how x$24ptr is declared in the two generated goroutines. Done means both goroutines run successfully and print the pointer without a ReferenceError.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.