Skip to content

Commit

Permalink
nogo: instantiate type info for generic types when running under Go >…
Browse files Browse the repository at this point in the history
…=1.18

This mirrors what golang.org/x/tools/go/packages does when loading packages.

Should fix #3211 (and probably #3164 as well).
  • Loading branch information
farhaven committed Jul 18, 2022
1 parent ed56d7e commit 848477d
Show file tree
Hide file tree
Showing 15 changed files with 6,624 additions and 2,810 deletions.
4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ go_repository(
go_repository(
name = "org_golang_x_mod",
importpath = "golang.org/x/mod",
sum = "h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=",
version = "v0.4.2",
sum = "h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=",
version = "v0.6.0-dev.0.20220419223038-86c51ed26bb4",
)

go_repository(
Expand Down
10 changes: 5 additions & 5 deletions go/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ def go_rules_dependencies(force = False):
wrapper(
http_archive,
name = "org_golang_x_tools",
# v0.1.9, latest as of 2022-03-14
# v0.1.11, latest as of 2022-07-14
urls = [
"https://mirror.bazel.build/github.com/golang/tools/archive/v0.1.9.zip",
"https://github.com/golang/tools/archive/v0.1.9.zip",
"https://mirror.bazel.build/github.com/golang/tools/archive/refs/tags/v0.1.11.zip",
"https://github.com/golang/tools/archive/refs/tags/v0.1.11.zip",
],
sha256 = "1d338afb3cd8013cfb035da6831dea2210efb0386c17b9c99b5e84724e3d733a",
strip_prefix = "tools-0.1.9",
sha256 = "d31521bddf3380e4ae2ae1ce6dcfca301bce6072527a07d612e13902c93916ef",
strip_prefix = "tools-0.1.11",
patches = [
# deletegopls removes the gopls subdirectory. It contains a nested
# module with additional dependencies. It's not needed by rules_go.
Expand Down
2 changes: 2 additions & 0 deletions go/tools/builders/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ go_source(
"env.go",
"flags.go",
"nogo_main.go",
"nogo_typeparams_go117.go",
"nogo_typeparams_go118.go",
"pack.go",
],
# //go/tools/builders:nogo_srcs is considered a different target by
Expand Down
3 changes: 3 additions & 0 deletions go/tools/builders/nogo_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ func load(packagePath string, imp *importer, filenames []string) (*goPackage, er
Scopes: make(map[ast.Node]*types.Scope),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}

initInstanceInfo(info)

types, err := config.Check(packagePath, pkg.fset, syntax, info)
if err != nil {
pkg.illTyped, pkg.typeCheckError = true, err
Expand Down
23 changes: 23 additions & 0 deletions go/tools/builders/nogo_typeparams_go117.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright 2022 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//go:build !go1.18
// +build !go1.18

package main

import "go/types"

func initInstanceInfo(*types.Info) {}
28 changes: 28 additions & 0 deletions go/tools/builders/nogo_typeparams_go118.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2022 The Bazel Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//go:build go1.18
// +build go1.18

package main

import (
"go/ast"
"go/types"
)

func initInstanceInfo(info *types.Info) {
info.Instances = make(map[*ast.Ident]types.Instance)
}
6 changes: 6 additions & 0 deletions tests/core/nogo/generics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")

go_bazel_test(
name = "generics_test",
srcs = ["generics_test.go"],
)
18 changes: 18 additions & 0 deletions tests/core/nogo/generics/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
nogo analyzers run against code using generics
==============================================

.. _nogo: /go/nogo.rst
.. _buildssa: https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/buildssa
.. _nilness: https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/nilness

Tests to ensure that `nogo`_ analyzers that run on code using generics get correct
type instantiation information.

.. contents::

generics_test
-------------

Verifies that code using generic types gets loaded including all type instantiation
information, so that analyzers based on the `buildssa`_ analyzer (such as `nilness`_) get
a complete picture of all types in the code.
85 changes: 85 additions & 0 deletions tests/core/nogo/generics/generics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2022 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package generics_test

import (
"bytes"
"testing"

"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
)

func TestMain(m *testing.M) {
bazel_testing.TestMain(m, bazel_testing.Args{
Nogo: "@//:nogo",
Main: `
-- BUILD.bazel --
load("@io_bazel_rules_go//go:def.bzl", "go_library", "nogo")
nogo(
name = "nogo",
visibility = ["//visibility:public"],
deps = ["@org_golang_x_tools//go/analysis/passes/buildssa"],
)
go_library(
name = "src",
srcs = ["src.go"],
importpath = "src",
)
-- src.go --
package src
type Set[T comparable] struct {
m map[T]struct{}
}
func New[T comparable](s ...T) *Set[T] {
set := &Set[T]{}
set.Add(s...)
return set
}
func (set *Set[T]) Add(s ...T) {
if set.m == nil {
set.m = make(map[T]struct{})
}
for _, s := range s {
set.m[s] = struct{}{}
}
}
func S(x ...string) *Set[string] {
return New[string](x...)
}
`,
})
}

func Test(t *testing.T) {
cmd := bazel_testing.BazelCmd("build", "//:src")
var stderr bytes.Buffer
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
t.Log("output:", stderr.String())
t.Fatal("unexpected error:", err)
}

if bytes.Contains(stderr.Bytes(), []byte("panic")) {
t.Errorf("found panic in Bazel output: \n%s", stderr.String())
}
}
3 changes: 3 additions & 0 deletions tests/integration/popular_repos/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ test_suite(
"@org_golang_x_tools//internal/jsonrpc2:jsonrpc2_test",
"@org_golang_x_tools//internal/jsonrpc2/servertest:servertest_test",
"@org_golang_x_tools//internal/jsonrpc2_v2:jsonrpc2_v2_test",
"@org_golang_x_tools//internal/lsp/bug:bug_test",
"@org_golang_x_tools//internal/lsp/debug:debug_test",
"@org_golang_x_tools//internal/lsp/lsppos:lsppos_test",
"@org_golang_x_tools//internal/lsp/progress:progress_test",
"@org_golang_x_tools//internal/lsp/regtest:regtest_test",
"@org_golang_x_tools//internal/lsp/source/completion:completion_test",
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/popular_repos/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ This runs tests from the repository `golang.org/x/tools <https://golang.org/x/to
* @org_golang_x_tools//internal/jsonrpc2:jsonrpc2_test
* @org_golang_x_tools//internal/jsonrpc2/servertest:servertest_test
* @org_golang_x_tools//internal/jsonrpc2_v2:jsonrpc2_v2_test
* @org_golang_x_tools//internal/lsp/bug:bug_test
* @org_golang_x_tools//internal/lsp/debug:debug_test
* @org_golang_x_tools//internal/lsp/lsppos:lsppos_test
* @org_golang_x_tools//internal/lsp/progress:progress_test
* @org_golang_x_tools//internal/lsp/regtest:regtest_test
* @org_golang_x_tools//internal/lsp/source/completion:completion_test
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/popular_repos/popular_repos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ def popular_repos():
go_repository,
name = "org_golang_x_mod",
importpath = "golang.org/x/mod",
commit = "c8bb1bd8a2aaa5c50fa106c8116850d503792d16",
commit = "86c51ed26bb44749b7d60a57bab0e7524656fe8a",
)
8 changes: 7 additions & 1 deletion tests/integration/popular_repos/popular_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
"cmd/gorename:gorename_test", # TODO(#417)
"cmd/guru/testdata/src/referrers:referrers_test", # Not a real test
"cmd/guru:guru_test", # Needs testdata directory
"cmd/signature-fuzzer/fuzz-driver:fuzz-driver_test", # requires working GOROOT
"cmd/signature-fuzzer/fuzz-runner:fuzz-runner_test", # requires working GOROOT
"cmd/signature-fuzzer/internal/fuzz-generator:fuzz-generator_test", # requires working GOROOT
"cmd/stringer:stringer_test", # Needs testdata directory
"container/intsets:intsets_test", # TODO(#413): External test depends on symbols defined in internal test.
"copyright:copyright_test", # # requires runfiles
Expand All @@ -111,6 +114,7 @@
"go/analysis/passes/buildtag:buildtag_test", # Needs testdata directory
"go/analysis/passes/cgocall:cgocall_test", # Needs testdata directory
"go/analysis/passes/composite:composite_test", # Needs testdata directory
"go/analysis/passes/composite/testdata/src/a:a_test", # Does not compile
"go/analysis/passes/copylock:copylock_test", # Needs testdata directory
"go/analysis/passes/ctrlflow:ctrlflow_test", # Needs testdata directory
"go/analysis/passes/deepequalerrors:deepequalerrors_test", # requires go list
Expand Down Expand Up @@ -174,6 +178,7 @@
"internal/apidiff:apidiff_test", # Needs testdata directory
"internal/gocommand:gocommand_test", # Needs go tool
"internal/imports:imports_test", # Needs testdata directory
"internal/lsp/analysis/embeddirective:embeddirective_test", # requires GOROOT
"internal/lsp/analysis/fillreturns:fillreturns_test", # Needs go tool
"internal/lsp/analysis/fillstruct:fillstruct_test", # Needs go tool
"internal/lsp/analysis/infertypeargs:infertypeargs_test", # Needs go tool
Expand All @@ -195,6 +200,7 @@
"internal/lsp/fuzzy:fuzzy_test", # has additional deps
"internal/lsp/lsprpc:lsprpc_test", # has additional deps
"internal/lsp/mod:mod_test", # has additional deps
"internal/lsp/safetoken:safetoken_test", # requires build cache
"internal/lsp/snippet:snippet_test", # has additional deps
"internal/lsp/source:source_test", # Needs testdata directory
"internal/lsp/testdata/analyzer:analyzer_test", # is testdata
Expand Down Expand Up @@ -233,7 +239,7 @@
dict(
name = "org_golang_x_mod",
importpath = "golang.org/x/mod",
commit = "c8bb1bd8a2aaa5c50fa106c8116850d503792d16",
commit = "86c51ed26bb44749b7d60a57bab0e7524656fe8a",
excludes = [
"sumdb/tlog:tlog_test", # Needs network, not available on RBE
"zip:zip_test", # Needs vcs tools, not available on RBE
Expand Down
Loading

0 comments on commit 848477d

Please sign in to comment.