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

Avoid exporting a pointer to a loop variable in linker #1389

Merged
merged 1 commit into from Jun 24, 2021

Commits on Jun 22, 2021

  1. Avoid exporting a pointer to a loop variable in linker

    The Bazel nogo (Go lint config) errored when I tried to compile esbuild:
    
        compilepkg: nogo: errors found by nogo during build-time code analysis:
        external/com_github_evanw_esbuild/internal/bundler/linker.go:3309:27:
         exporting a pointer for the loop variable stmt (export_loop_ref)
    
    The simplified code nogo complains about is:
    
        for _, stmt := range partStmts {
          stmt.Data = &js_ast.SImport{
            StarNameLoc: &stmt.Loc,
          }
        }
    
    The problem is `&stmt.Loc` points to the mutated loop variable `stmt`.  After
    the loop iteration ends, all stored pointers will point to the last value of
    `partStmts[-1].Loc`.
    
    An alternative solution is to shadow `stmt` at the beginning of the loop, but
    this felt cleaner:
    
        stmt := stmt
    
    The lint rule is defined by https://github.com/kyoh86/exportloopref.
    jschaf committed Jun 22, 2021
    Copy the full SHA
    f2dd0d8 View commit details
    Browse the repository at this point in the history