From ab0262bd94c25bc91d5e0d831b75729c253dfde6 Mon Sep 17 00:00:00 2001 From: Adin Scannell Date: Thu, 23 Jul 2020 18:00:12 -0700 Subject: Convert go_generics tests to starlark. For some reason these tests were broken when run via the bazel docker container. The mechanism used was a bit crazy (self-extracting bundle), so convert them to use straight-forward starlark rules. This has the added advantaged that they are now independent tests. --- images/default/Dockerfile | 2 +- tools/go_generics/BUILD | 24 -- tools/go_generics/defs.bzl | 5 +- .../go_generics/generics_tests/all_stmts/input.go | 290 --------------------- .../go_generics/generics_tests/all_stmts/opts.txt | 1 - .../generics_tests/all_stmts/output/output.go | 288 -------------------- .../go_generics/generics_tests/all_types/input.go | 43 --- .../generics_tests/all_types/lib/lib.go | 17 -- .../go_generics/generics_tests/all_types/opts.txt | 1 - .../generics_tests/all_types/output/output.go | 41 --- tools/go_generics/generics_tests/anon/input.go | 46 ---- tools/go_generics/generics_tests/anon/opts.txt | 1 - .../generics_tests/anon/output/output.go | 42 --- tools/go_generics/generics_tests/consts/input.go | 26 -- tools/go_generics/generics_tests/consts/opts.txt | 1 - .../generics_tests/consts/output/output.go | 26 -- tools/go_generics/generics_tests/imports/input.go | 24 -- tools/go_generics/generics_tests/imports/opts.txt | 1 - .../generics_tests/imports/output/output.go | 27 -- .../generics_tests/remove_typedef/input.go | 37 --- .../generics_tests/remove_typedef/opts.txt | 1 - .../generics_tests/remove_typedef/output/output.go | 29 --- tools/go_generics/generics_tests/simple/input.go | 45 ---- tools/go_generics/generics_tests/simple/opts.txt | 1 - .../generics_tests/simple/output/output.go | 43 --- tools/go_generics/go_generics_unittest.sh | 70 ----- tools/go_generics/tests/BUILD | 0 tools/go_generics/tests/all_stmts/BUILD | 10 + tools/go_generics/tests/all_stmts/input.go | 290 +++++++++++++++++++++ tools/go_generics/tests/all_stmts/output.go | 288 ++++++++++++++++++++ tools/go_generics/tests/all_types/BUILD | 10 + tools/go_generics/tests/all_types/input.go | 45 ++++ tools/go_generics/tests/all_types/lib/lib.go | 17 ++ tools/go_generics/tests/all_types/output.go | 43 +++ tools/go_generics/tests/anon/BUILD | 12 + tools/go_generics/tests/anon/input.go | 46 ++++ tools/go_generics/tests/anon/output.go | 42 +++ tools/go_generics/tests/consts/BUILD | 17 ++ tools/go_generics/tests/consts/input.go | 26 ++ tools/go_generics/tests/consts/output.go | 26 ++ tools/go_generics/tests/defs.bzl | 67 +++++ tools/go_generics/tests/imports/BUILD | 18 ++ tools/go_generics/tests/imports/input.go | 24 ++ tools/go_generics/tests/imports/output.go | 27 ++ tools/go_generics/tests/remove_typedef/BUILD | 10 + tools/go_generics/tests/remove_typedef/input.go | 37 +++ tools/go_generics/tests/remove_typedef/output.go | 29 +++ tools/go_generics/tests/simple/BUILD | 11 + tools/go_generics/tests/simple/input.go | 45 ++++ tools/go_generics/tests/simple/output.go | 43 +++ 50 files changed, 1187 insertions(+), 1128 deletions(-) delete mode 100644 tools/go_generics/generics_tests/all_stmts/input.go delete mode 100644 tools/go_generics/generics_tests/all_stmts/opts.txt delete mode 100644 tools/go_generics/generics_tests/all_stmts/output/output.go delete mode 100644 tools/go_generics/generics_tests/all_types/input.go delete mode 100644 tools/go_generics/generics_tests/all_types/lib/lib.go delete mode 100644 tools/go_generics/generics_tests/all_types/opts.txt delete mode 100644 tools/go_generics/generics_tests/all_types/output/output.go delete mode 100644 tools/go_generics/generics_tests/anon/input.go delete mode 100644 tools/go_generics/generics_tests/anon/opts.txt delete mode 100644 tools/go_generics/generics_tests/anon/output/output.go delete mode 100644 tools/go_generics/generics_tests/consts/input.go delete mode 100644 tools/go_generics/generics_tests/consts/opts.txt delete mode 100644 tools/go_generics/generics_tests/consts/output/output.go delete mode 100644 tools/go_generics/generics_tests/imports/input.go delete mode 100644 tools/go_generics/generics_tests/imports/opts.txt delete mode 100644 tools/go_generics/generics_tests/imports/output/output.go delete mode 100644 tools/go_generics/generics_tests/remove_typedef/input.go delete mode 100644 tools/go_generics/generics_tests/remove_typedef/opts.txt delete mode 100644 tools/go_generics/generics_tests/remove_typedef/output/output.go delete mode 100644 tools/go_generics/generics_tests/simple/input.go delete mode 100644 tools/go_generics/generics_tests/simple/opts.txt delete mode 100644 tools/go_generics/generics_tests/simple/output/output.go delete mode 100755 tools/go_generics/go_generics_unittest.sh create mode 100644 tools/go_generics/tests/BUILD create mode 100644 tools/go_generics/tests/all_stmts/BUILD create mode 100644 tools/go_generics/tests/all_stmts/input.go create mode 100644 tools/go_generics/tests/all_stmts/output.go create mode 100644 tools/go_generics/tests/all_types/BUILD create mode 100644 tools/go_generics/tests/all_types/input.go create mode 100644 tools/go_generics/tests/all_types/lib/lib.go create mode 100644 tools/go_generics/tests/all_types/output.go create mode 100644 tools/go_generics/tests/anon/BUILD create mode 100644 tools/go_generics/tests/anon/input.go create mode 100644 tools/go_generics/tests/anon/output.go create mode 100644 tools/go_generics/tests/consts/BUILD create mode 100644 tools/go_generics/tests/consts/input.go create mode 100644 tools/go_generics/tests/consts/output.go create mode 100644 tools/go_generics/tests/defs.bzl create mode 100644 tools/go_generics/tests/imports/BUILD create mode 100644 tools/go_generics/tests/imports/input.go create mode 100644 tools/go_generics/tests/imports/output.go create mode 100644 tools/go_generics/tests/remove_typedef/BUILD create mode 100644 tools/go_generics/tests/remove_typedef/input.go create mode 100644 tools/go_generics/tests/remove_typedef/output.go create mode 100644 tools/go_generics/tests/simple/BUILD create mode 100644 tools/go_generics/tests/simple/input.go create mode 100644 tools/go_generics/tests/simple/output.go diff --git a/images/default/Dockerfile b/images/default/Dockerfile index 397082b02..2b38e6c58 100644 --- a/images/default/Dockerfile +++ b/images/default/Dockerfile @@ -1,7 +1,7 @@ FROM fedora:31 # Install bazel. RUN dnf install -y dnf-plugins-core && dnf copr enable -y vbatts/bazel -RUN dnf install -y git gcc make golang gcc-c++ glibc-devel python3 which python3-pip python3-devel libffi-devel openssl-devel pkg-config glibc-static libstdc++-static patch +RUN dnf install -y git gcc make golang gcc-c++ glibc-devel python3 which python3-pip python3-devel libffi-devel openssl-devel pkg-config glibc-static libstdc++-static patch diffutils RUN pip install pycparser RUN dnf install -y bazel3 # Install gcloud. diff --git a/tools/go_generics/BUILD b/tools/go_generics/BUILD index 32a949c93..558826bf1 100644 --- a/tools/go_generics/BUILD +++ b/tools/go_generics/BUILD @@ -12,27 +12,3 @@ go_binary( visibility = ["//:sandbox"], deps = ["//tools/go_generics/globals"], ) - -genrule( - name = "go_generics_tests", - srcs = glob(["generics_tests/**"]) + [":go_generics"], - outs = ["go_generics_tests.tgz"], - cmd = "tar -czvhf $@ $(SRCS)", -) - -genrule( - name = "go_generics_test_bundle", - srcs = [ - ":go_generics_tests.tgz", - ":go_generics_unittest.sh", - ], - outs = ["go_generics_test.sh"], - cmd = "cat $(location :go_generics_unittest.sh) $(location :go_generics_tests.tgz) > $@", - executable = True, -) - -sh_test( - name = "go_generics_test", - size = "small", - srcs = ["go_generics_test.sh"], -) diff --git a/tools/go_generics/defs.bzl b/tools/go_generics/defs.bzl index ec047a644..7fe7d7e59 100644 --- a/tools/go_generics/defs.bzl +++ b/tools/go_generics/defs.bzl @@ -100,7 +100,8 @@ def _go_template_instance_impl(ctx): # Build the argument list. args = ["-i=%s" % template.file.path, "-o=%s" % output.path] - args += ["-p=%s" % ctx.attr.package] + if ctx.attr.package: + args += ["-p=%s" % ctx.attr.package] if len(ctx.attr.prefix) > 0: args += ["-prefix=%s" % ctx.attr.prefix] @@ -151,7 +152,7 @@ go_template_instance = rule( "consts": attr.string_dict(), "imports": attr.string_dict(), "anon": attr.bool(mandatory = False, default = False), - "package": attr.string(mandatory = True), + "package": attr.string(mandatory = False), "out": attr.output(mandatory = True), "_tool": attr.label(executable = True, cfg = "host", default = Label("//tools/go_generics")), }, diff --git a/tools/go_generics/generics_tests/all_stmts/input.go b/tools/go_generics/generics_tests/all_stmts/input.go deleted file mode 100644 index 4791d1ff1..000000000 --- a/tools/go_generics/generics_tests/all_stmts/input.go +++ /dev/null @@ -1,290 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 tests - -import ( - "sync" -) - -type T int - -func h(T) { -} - -type s struct { - a, b int - c []int -} - -func g(T) *s { - return &s{} -} - -func f() (T, []int) { - // Branch. - goto T - goto R - - // Labeled. -T: - _ = T(0) - - // Empty. -R: - ; - - // Assignment with definition. - a, b, c := T(1), T(2), T(3) - _, _, _ = a, b, c - - // Assignment without definition. - g(T(0)).a, g(T(1)).b, c = int(T(1)), int(T(2)), T(3) - _, _, _ = a, b, c - - // Block. - { - var T T - T = 0 - _ = T - } - - // Declarations. - type Type T - const Const T = 10 - var g1 func(T, int, ...T) (int, T) - var v T - var w = T(0) - { - var T struct { - f []T - } - _ = T - } - - // Defer. - defer g1(T(0), 1) - - // Expression. - h(v + w + T(1)) - - // For statements. - for i := T(0); i < T(10); i++ { - var T func(int) T - v := T(0) - _ = v - } - - for { - var T func(int) T - v := T(0) - _ = v - } - - // Go. - go g1(T(0), 1) - - // If statements. - if a != T(1) { - var T func(int) T - v := T(0) - _ = v - } - - if a := T(0); a != T(1) { - var T func(int) T - v := T(0) - _ = v - } - - if a := T(0); a != T(1) { - var T func(int) T - v := T(0) - _ = v - } else if b := T(0); b != T(1) { - var T func(int) T - v := T(0) - _ = v - } else if T := T(0); T != 1 { - T++ - } else { - T-- - } - - if a := T(0); a != T(1) { - var T func(int) T - v := T(0) - _ = v - } else { - var T func(int) T - v := T(0) - _ = v - } - - // Inc/Dec statements. - (*(*T)(nil))++ - (*(*T)(nil))-- - - // Range statements. - for g(T(0)).a, g(T(1)).b = range g(T(10)).c { - var d T - _ = d - } - - for T, b := range g(T(10)).c { - _ = T - _ = b - } - - // Select statement. - { - var fch func(T) chan int - - select { - case <-fch(T(30)): - var T T - T = 0 - _ = T - default: - var T T - T = 0 - _ = T - case T := <-fch(T(30)): - T = 0 - _ = T - case g(T(0)).a = <-fch(T(30)): - var T T - T = 0 - _ = T - case fch(T(30)) <- int(T(0)): - var T T - T = 0 - _ = T - } - } - - // Send statements. - { - var ch chan T - var fch func(T) chan int - - ch <- T(0) - fch(T(1)) <- g(T(10)).a - } - - // Switch statements. - { - var a T - var b int - switch { - case a == T(0): - var T T - T = 0 - _ = T - case a < T(0), b < g(T(10)).a: - var T T - T = 0 - _ = T - default: - var T T - T = 0 - _ = T - } - } - - switch T(g(T(10)).a) { - case T(0): - var T T - T = 0 - _ = T - case T(1), T(g(T(10)).a): - var T T - T = 0 - _ = T - default: - var T T - T = 0 - _ = T - } - - switch b := g(T(10)); T(b.a) + T(10) { - case T(0): - var T T - T = 0 - _ = T - case T(1), T(g(T(10)).a): - var T T - T = 0 - _ = T - default: - var T T - T = 0 - _ = T - } - - // Type switch statements. - { - var interfaceFunc func(T) interface{} - - switch interfaceFunc(T(0)).(type) { - case *T, T, int: - var T T - T = 0 - _ = T - case sync.Mutex, **T: - var T T - T = 0 - _ = T - default: - var T T - T = 0 - _ = T - } - - switch x := interfaceFunc(T(0)).(type) { - case *T, T, int: - var T T - T = 0 - _ = T - _ = x - case sync.Mutex, **T: - var T T - T = 0 - _ = T - default: - var T T - T = 0 - _ = T - } - - switch t := T(0); x := interfaceFunc(T(0) + t).(type) { - case *T, T, int: - var T T - T = 0 - _ = T - _ = x - case sync.Mutex, **T: - var T T - T = 0 - _ = T - default: - var T T - T = 0 - _ = T - } - } - - // Return statement. - return T(10), g(T(11)).c -} diff --git a/tools/go_generics/generics_tests/all_stmts/opts.txt b/tools/go_generics/generics_tests/all_stmts/opts.txt deleted file mode 100644 index c9d0e09bf..000000000 --- a/tools/go_generics/generics_tests/all_stmts/opts.txt +++ /dev/null @@ -1 +0,0 @@ --t=T=Q diff --git a/tools/go_generics/generics_tests/all_stmts/output/output.go b/tools/go_generics/generics_tests/all_stmts/output/output.go deleted file mode 100644 index a53d84535..000000000 --- a/tools/go_generics/generics_tests/all_stmts/output/output.go +++ /dev/null @@ -1,288 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 main - -import ( - "sync" -) - -func h(Q) { -} - -type s struct { - a, b int - c []int -} - -func g(Q) *s { - return &s{} -} - -func f() (Q, []int) { - // Branch. - goto T - goto R - - // Labeled. -T: - _ = Q(0) - - // Empty. -R: - ; - - // Assignment with definition. - a, b, c := Q(1), Q(2), Q(3) - _, _, _ = a, b, c - - // Assignment without definition. - g(Q(0)).a, g(Q(1)).b, c = int(Q(1)), int(Q(2)), Q(3) - _, _, _ = a, b, c - - // Block. - { - var T Q - T = 0 - _ = T - } - - // Declarations. - type Type Q - const Const Q = 10 - var g1 func(Q, int, ...Q) (int, Q) - var v Q - var w = Q(0) - { - var T struct { - f []Q - } - _ = T - } - - // Defer. - defer g1(Q(0), 1) - - // Expression. - h(v + w + Q(1)) - - // For statements. - for i := Q(0); i < Q(10); i++ { - var T func(int) Q - v := T(0) - _ = v - } - - for { - var T func(int) Q - v := T(0) - _ = v - } - - // Go. - go g1(Q(0), 1) - - // If statements. - if a != Q(1) { - var T func(int) Q - v := T(0) - _ = v - } - - if a := Q(0); a != Q(1) { - var T func(int) Q - v := T(0) - _ = v - } - - if a := Q(0); a != Q(1) { - var T func(int) Q - v := T(0) - _ = v - } else if b := Q(0); b != Q(1) { - var T func(int) Q - v := T(0) - _ = v - } else if T := Q(0); T != 1 { - T++ - } else { - T-- - } - - if a := Q(0); a != Q(1) { - var T func(int) Q - v := T(0) - _ = v - } else { - var T func(int) Q - v := T(0) - _ = v - } - - // Inc/Dec statements. - (*(*Q)(nil))++ - (*(*Q)(nil))-- - - // Range statements. - for g(Q(0)).a, g(Q(1)).b = range g(Q(10)).c { - var d Q - _ = d - } - - for T, b := range g(Q(10)).c { - _ = T - _ = b - } - - // Select statement. - { - var fch func(Q) chan int - - select { - case <-fch(Q(30)): - var T Q - T = 0 - _ = T - default: - var T Q - T = 0 - _ = T - case T := <-fch(Q(30)): - T = 0 - _ = T - case g(Q(0)).a = <-fch(Q(30)): - var T Q - T = 0 - _ = T - case fch(Q(30)) <- int(Q(0)): - var T Q - T = 0 - _ = T - } - } - - // Send statements. - { - var ch chan Q - var fch func(Q) chan int - - ch <- Q(0) - fch(Q(1)) <- g(Q(10)).a - } - - // Switch statements. - { - var a Q - var b int - switch { - case a == Q(0): - var T Q - T = 0 - _ = T - case a < Q(0), b < g(Q(10)).a: - var T Q - T = 0 - _ = T - default: - var T Q - T = 0 - _ = T - } - } - - switch Q(g(Q(10)).a) { - case Q(0): - var T Q - T = 0 - _ = T - case Q(1), Q(g(Q(10)).a): - var T Q - T = 0 - _ = T - default: - var T Q - T = 0 - _ = T - } - - switch b := g(Q(10)); Q(b.a) + Q(10) { - case Q(0): - var T Q - T = 0 - _ = T - case Q(1), Q(g(Q(10)).a): - var T Q - T = 0 - _ = T - default: - var T Q - T = 0 - _ = T - } - - // Type switch statements. - { - var interfaceFunc func(Q) interface{} - - switch interfaceFunc(Q(0)).(type) { - case *Q, Q, int: - var T Q - T = 0 - _ = T - case sync.Mutex, **Q: - var T Q - T = 0 - _ = T - default: - var T Q - T = 0 - _ = T - } - - switch x := interfaceFunc(Q(0)).(type) { - case *Q, Q, int: - var T Q - T = 0 - _ = T - _ = x - case sync.Mutex, **Q: - var T Q - T = 0 - _ = T - default: - var T Q - T = 0 - _ = T - } - - switch t := Q(0); x := interfaceFunc(Q(0) + t).(type) { - case *Q, Q, int: - var T Q - T = 0 - _ = T - _ = x - case sync.Mutex, **Q: - var T Q - T = 0 - _ = T - default: - var T Q - T = 0 - _ = T - } - } - - // Return statement. - return Q(10), g(Q(11)).c -} diff --git a/tools/go_generics/generics_tests/all_types/input.go b/tools/go_generics/generics_tests/all_types/input.go deleted file mode 100644 index 3575d02ec..000000000 --- a/tools/go_generics/generics_tests/all_types/input.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 tests - -import "./lib" - -type T int - -type newType struct { - a T - b lib.T - c *T - d (T) - e chan T - f <-chan T - g chan<- T - h []T - i [10]T - j map[T]T - k func(T, T) (T, T) - l interface { - f(T) - } - m struct { - T - a T - } -} - -func f(...T) { -} diff --git a/tools/go_generics/generics_tests/all_types/lib/lib.go b/tools/go_generics/generics_tests/all_types/lib/lib.go deleted file mode 100644 index 988786496..000000000 --- a/tools/go_generics/generics_tests/all_types/lib/lib.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 lib - -type T int32 diff --git a/tools/go_generics/generics_tests/all_types/opts.txt b/tools/go_generics/generics_tests/all_types/opts.txt deleted file mode 100644 index c9d0e09bf..000000000 --- a/tools/go_generics/generics_tests/all_types/opts.txt +++ /dev/null @@ -1 +0,0 @@ --t=T=Q diff --git a/tools/go_generics/generics_tests/all_types/output/output.go b/tools/go_generics/generics_tests/all_types/output/output.go deleted file mode 100644 index 41fd147a1..000000000 --- a/tools/go_generics/generics_tests/all_types/output/output.go +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 main - -import "./lib" - -type newType struct { - a Q - b lib.T - c *Q - d (Q) - e chan Q - f <-chan Q - g chan<- Q - h []Q - i [10]Q - j map[Q]Q - k func(Q, Q) (Q, Q) - l interface { - f(Q) - } - m struct { - Q - a Q - } -} - -func f(...Q) { -} diff --git a/tools/go_generics/generics_tests/anon/input.go b/tools/go_generics/generics_tests/anon/input.go deleted file mode 100644 index 44086d522..000000000 --- a/tools/go_generics/generics_tests/anon/input.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2019 The gVisor Authors. -// -// 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 tests - -type T interface { - Apply(T) T -} - -type Foo struct { - T - Bar map[string]T `json:"bar,omitempty"` -} - -type Baz struct { - T someTypeNotT -} - -func (f Foo) GetBar(name string) T { - b, ok := f.Bar[name] - if ok { - b = f.Apply(b) - } else { - b = f.T - } - return b -} - -func foobar() { - a := Baz{} - a.T = 0 // should not be renamed, this is a limitation - - b := otherpkg.UnrelatedType{} - b.T = 0 // should not be renamed, this is a limitation -} diff --git a/tools/go_generics/generics_tests/anon/opts.txt b/tools/go_generics/generics_tests/anon/opts.txt deleted file mode 100644 index a5e9d26de..000000000 --- a/tools/go_generics/generics_tests/anon/opts.txt +++ /dev/null @@ -1 +0,0 @@ --t=T=Q -suffix=New -anon diff --git a/tools/go_generics/generics_tests/anon/output/output.go b/tools/go_generics/generics_tests/anon/output/output.go deleted file mode 100644 index 160cddf79..000000000 --- a/tools/go_generics/generics_tests/anon/output/output.go +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2019 The gVisor Authors. -// -// 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 main - -type FooNew struct { - Q - Bar map[string]Q `json:"bar,omitempty"` -} - -type BazNew struct { - T someTypeNotT -} - -func (f FooNew) GetBar(name string) Q { - b, ok := f.Bar[name] - if ok { - b = f.Apply(b) - } else { - b = f.Q - } - return b -} - -func foobarNew() { - a := BazNew{} - a.Q = 0 // should not be renamed, this is a limitation - - b := otherpkg.UnrelatedType{} - b.Q = 0 // should not be renamed, this is a limitation -} diff --git a/tools/go_generics/generics_tests/consts/input.go b/tools/go_generics/generics_tests/consts/input.go deleted file mode 100644 index 04b95fcc6..000000000 --- a/tools/go_generics/generics_tests/consts/input.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 tests - -const c1 = 10 -const x, y, z = 100, 200, 300 -const v float32 = 1.0 + 2.0 -const s = "abc" -const ( - A = 10 - B, C, D = 10, 20, 30 - S = "abc" - T, U, V string = "abc", "def", "ghi" -) diff --git a/tools/go_generics/generics_tests/consts/opts.txt b/tools/go_generics/generics_tests/consts/opts.txt deleted file mode 100644 index 4fb59dce8..000000000 --- a/tools/go_generics/generics_tests/consts/opts.txt +++ /dev/null @@ -1 +0,0 @@ --c=c1=20 -c=z=600 -c=v=3.3 -c=s="def" -c=A=20 -c=C=100 -c=S="def" -c=T="ABC" diff --git a/tools/go_generics/generics_tests/consts/output/output.go b/tools/go_generics/generics_tests/consts/output/output.go deleted file mode 100644 index 18d316cc9..000000000 --- a/tools/go_generics/generics_tests/consts/output/output.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 main - -const c1 = 20 -const x, y, z = 100, 200, 600 -const v float32 = 3.3 -const s = "def" -const ( - A = 20 - B, C, D = 10, 100, 30 - S = "def" - T, U, V string = "ABC", "def", "ghi" -) diff --git a/tools/go_generics/generics_tests/imports/input.go b/tools/go_generics/generics_tests/imports/input.go deleted file mode 100644 index 0f032c2a1..000000000 --- a/tools/go_generics/generics_tests/imports/input.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 tests - -type T int - -var global T - -const ( - m = 0 - n = 0 -) diff --git a/tools/go_generics/generics_tests/imports/opts.txt b/tools/go_generics/generics_tests/imports/opts.txt deleted file mode 100644 index 87324be79..000000000 --- a/tools/go_generics/generics_tests/imports/opts.txt +++ /dev/null @@ -1 +0,0 @@ --t=T=sync.Mutex -c=n=math.Uint32 -c=m=math.Uint64 -import=sync=sync -import=math=mymathpath diff --git a/tools/go_generics/generics_tests/imports/output/output.go b/tools/go_generics/generics_tests/imports/output/output.go deleted file mode 100644 index 2488ca58c..000000000 --- a/tools/go_generics/generics_tests/imports/output/output.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 main - -import ( - __generics_imported1 "mymathpath" - __generics_imported0 "sync" -) - -var global __generics_imported0.Mutex - -const ( - m = __generics_imported1.Uint64 - n = __generics_imported1.Uint32 -) diff --git a/tools/go_generics/generics_tests/remove_typedef/input.go b/tools/go_generics/generics_tests/remove_typedef/input.go deleted file mode 100644 index cf632bae7..000000000 --- a/tools/go_generics/generics_tests/remove_typedef/input.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 tests - -func f(T) Q { - return Q{} -} - -type T struct{} - -type Q struct{} - -func (*T) f() { -} - -func (T) g() { -} - -func (*Q) f(T) T { - return T{} -} - -func (*Q) g(T) *T { - return nil -} diff --git a/tools/go_generics/generics_tests/remove_typedef/opts.txt b/tools/go_generics/generics_tests/remove_typedef/opts.txt deleted file mode 100644 index 9c8ecaada..000000000 --- a/tools/go_generics/generics_tests/remove_typedef/opts.txt +++ /dev/null @@ -1 +0,0 @@ --t=T=U diff --git a/tools/go_generics/generics_tests/remove_typedef/output/output.go b/tools/go_generics/generics_tests/remove_typedef/output/output.go deleted file mode 100644 index d44fd8e1c..000000000 --- a/tools/go_generics/generics_tests/remove_typedef/output/output.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 main - -func f(U) Q { - return Q{} -} - -type Q struct{} - -func (*Q) f(U) U { - return U{} -} - -func (*Q) g(U) *U { - return nil -} diff --git a/tools/go_generics/generics_tests/simple/input.go b/tools/go_generics/generics_tests/simple/input.go deleted file mode 100644 index 2a917f16c..000000000 --- a/tools/go_generics/generics_tests/simple/input.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 tests - -type T int - -var global T - -func f(_ T, a int) { -} - -func g(a T, b int) { - var c T - _ = c - - d := (*T)(nil) - _ = d -} - -type R struct { - T - a *T -} - -var ( - Z *T = (*T)(nil) -) - -const ( - X T = (T)(0) -) - -type Y T diff --git a/tools/go_generics/generics_tests/simple/opts.txt b/tools/go_generics/generics_tests/simple/opts.txt deleted file mode 100644 index 7832ef66f..000000000 --- a/tools/go_generics/generics_tests/simple/opts.txt +++ /dev/null @@ -1 +0,0 @@ --t=T=Q -suffix=New diff --git a/tools/go_generics/generics_tests/simple/output/output.go b/tools/go_generics/generics_tests/simple/output/output.go deleted file mode 100644 index 6bfa0b25b..000000000 --- a/tools/go_generics/generics_tests/simple/output/output.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2018 The gVisor Authors. -// -// 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 main - -var globalNew Q - -func fNew(_ Q, a int) { -} - -func gNew(a Q, b int) { - var c Q - _ = c - - d := (*Q)(nil) - _ = d -} - -type RNew struct { - Q - a *Q -} - -var ( - ZNew *Q = (*Q)(nil) -) - -const ( - XNew Q = (Q)(0) -) - -type YNew Q diff --git a/tools/go_generics/go_generics_unittest.sh b/tools/go_generics/go_generics_unittest.sh deleted file mode 100755 index 44b22db91..000000000 --- a/tools/go_generics/go_generics_unittest.sh +++ /dev/null @@ -1,70 +0,0 @@ -#!/bin/bash - -# Copyright 2018 The gVisor Authors. -# -# 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. - -# Bash "safe-mode": Treat command failures as fatal (even those that occur in -# pipes), and treat unset variables as errors. -set -eu -o pipefail - -# This file will be generated as a self-extracting shell script in order to -# eliminate the need for any runtime dependencies. The tarball at the end will -# include the go_generics binary, as well as a subdirectory named -# generics_tests. See the BUILD file for more information. -declare -r temp=$(mktemp -d) -function cleanup() { - rm -rf "${temp}" -} -# trap cleanup EXIT - -# Print message in "$1" then exit with status 1. -function die () { - echo "$1" 1>&2 - exit 1 -} - -# This prints the line number of __BUNDLE__ below, that should be the last line -# of this script. After that point, the concatenated archive will be the -# contents. -declare -r tgz=`awk '/^__BUNDLE__/ {print NR + 1; exit 0; }' $0` -tail -n+"${tgz}" $0 | tar -xzv -C "${temp}" - -# The target for the test. -declare -r binary="$(find ${temp} -type f -a -name go_generics)" -declare -r input_dirs="$(find ${temp} -type d -a -name generics_tests)/*" - -# Go through all test cases. -for f in ${input_dirs}; do - base=$(basename "${f}") - - # Run go_generics on the input file. - opts=$(head -n 1 ${f}/opts.txt) - out="${f}/output/generated.go" - expected="${f}/output/output.go" - ${binary} ${opts} "-i=${f}/input.go" "-o=${out}" || die "go_generics failed for test case \"${base}\"" - - # Compare the outputs. - diff ${expected} ${out} - if [ $? -ne 0 ]; then - echo "Expected:" - cat ${expected} - echo "Actual:" - cat ${out} - die "Actual output is different from expected for test \"${base}\"" - fi -done - -echo "PASS" -exit 0 -__BUNDLE__ diff --git a/tools/go_generics/tests/BUILD b/tools/go_generics/tests/BUILD new file mode 100644 index 000000000..e69de29bb diff --git a/tools/go_generics/tests/all_stmts/BUILD b/tools/go_generics/tests/all_stmts/BUILD new file mode 100644 index 000000000..fbc07dbff --- /dev/null +++ b/tools/go_generics/tests/all_stmts/BUILD @@ -0,0 +1,10 @@ +load("//tools/go_generics/tests:defs.bzl", "go_generics_test") + +go_generics_test( + name = "all_stmts", + inputs = ["input.go"], + output = "output.go", + types = { + "T": "Q", + }, +) diff --git a/tools/go_generics/tests/all_stmts/input.go b/tools/go_generics/tests/all_stmts/input.go new file mode 100644 index 000000000..4791d1ff1 --- /dev/null +++ b/tools/go_generics/tests/all_stmts/input.go @@ -0,0 +1,290 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 tests + +import ( + "sync" +) + +type T int + +func h(T) { +} + +type s struct { + a, b int + c []int +} + +func g(T) *s { + return &s{} +} + +func f() (T, []int) { + // Branch. + goto T + goto R + + // Labeled. +T: + _ = T(0) + + // Empty. +R: + ; + + // Assignment with definition. + a, b, c := T(1), T(2), T(3) + _, _, _ = a, b, c + + // Assignment without definition. + g(T(0)).a, g(T(1)).b, c = int(T(1)), int(T(2)), T(3) + _, _, _ = a, b, c + + // Block. + { + var T T + T = 0 + _ = T + } + + // Declarations. + type Type T + const Const T = 10 + var g1 func(T, int, ...T) (int, T) + var v T + var w = T(0) + { + var T struct { + f []T + } + _ = T + } + + // Defer. + defer g1(T(0), 1) + + // Expression. + h(v + w + T(1)) + + // For statements. + for i := T(0); i < T(10); i++ { + var T func(int) T + v := T(0) + _ = v + } + + for { + var T func(int) T + v := T(0) + _ = v + } + + // Go. + go g1(T(0), 1) + + // If statements. + if a != T(1) { + var T func(int) T + v := T(0) + _ = v + } + + if a := T(0); a != T(1) { + var T func(int) T + v := T(0) + _ = v + } + + if a := T(0); a != T(1) { + var T func(int) T + v := T(0) + _ = v + } else if b := T(0); b != T(1) { + var T func(int) T + v := T(0) + _ = v + } else if T := T(0); T != 1 { + T++ + } else { + T-- + } + + if a := T(0); a != T(1) { + var T func(int) T + v := T(0) + _ = v + } else { + var T func(int) T + v := T(0) + _ = v + } + + // Inc/Dec statements. + (*(*T)(nil))++ + (*(*T)(nil))-- + + // Range statements. + for g(T(0)).a, g(T(1)).b = range g(T(10)).c { + var d T + _ = d + } + + for T, b := range g(T(10)).c { + _ = T + _ = b + } + + // Select statement. + { + var fch func(T) chan int + + select { + case <-fch(T(30)): + var T T + T = 0 + _ = T + default: + var T T + T = 0 + _ = T + case T := <-fch(T(30)): + T = 0 + _ = T + case g(T(0)).a = <-fch(T(30)): + var T T + T = 0 + _ = T + case fch(T(30)) <- int(T(0)): + var T T + T = 0 + _ = T + } + } + + // Send statements. + { + var ch chan T + var fch func(T) chan int + + ch <- T(0) + fch(T(1)) <- g(T(10)).a + } + + // Switch statements. + { + var a T + var b int + switch { + case a == T(0): + var T T + T = 0 + _ = T + case a < T(0), b < g(T(10)).a: + var T T + T = 0 + _ = T + default: + var T T + T = 0 + _ = T + } + } + + switch T(g(T(10)).a) { + case T(0): + var T T + T = 0 + _ = T + case T(1), T(g(T(10)).a): + var T T + T = 0 + _ = T + default: + var T T + T = 0 + _ = T + } + + switch b := g(T(10)); T(b.a) + T(10) { + case T(0): + var T T + T = 0 + _ = T + case T(1), T(g(T(10)).a): + var T T + T = 0 + _ = T + default: + var T T + T = 0 + _ = T + } + + // Type switch statements. + { + var interfaceFunc func(T) interface{} + + switch interfaceFunc(T(0)).(type) { + case *T, T, int: + var T T + T = 0 + _ = T + case sync.Mutex, **T: + var T T + T = 0 + _ = T + default: + var T T + T = 0 + _ = T + } + + switch x := interfaceFunc(T(0)).(type) { + case *T, T, int: + var T T + T = 0 + _ = T + _ = x + case sync.Mutex, **T: + var T T + T = 0 + _ = T + default: + var T T + T = 0 + _ = T + } + + switch t := T(0); x := interfaceFunc(T(0) + t).(type) { + case *T, T, int: + var T T + T = 0 + _ = T + _ = x + case sync.Mutex, **T: + var T T + T = 0 + _ = T + default: + var T T + T = 0 + _ = T + } + } + + // Return statement. + return T(10), g(T(11)).c +} diff --git a/tools/go_generics/tests/all_stmts/output.go b/tools/go_generics/tests/all_stmts/output.go new file mode 100644 index 000000000..a53d84535 --- /dev/null +++ b/tools/go_generics/tests/all_stmts/output.go @@ -0,0 +1,288 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 main + +import ( + "sync" +) + +func h(Q) { +} + +type s struct { + a, b int + c []int +} + +func g(Q) *s { + return &s{} +} + +func f() (Q, []int) { + // Branch. + goto T + goto R + + // Labeled. +T: + _ = Q(0) + + // Empty. +R: + ; + + // Assignment with definition. + a, b, c := Q(1), Q(2), Q(3) + _, _, _ = a, b, c + + // Assignment without definition. + g(Q(0)).a, g(Q(1)).b, c = int(Q(1)), int(Q(2)), Q(3) + _, _, _ = a, b, c + + // Block. + { + var T Q + T = 0 + _ = T + } + + // Declarations. + type Type Q + const Const Q = 10 + var g1 func(Q, int, ...Q) (int, Q) + var v Q + var w = Q(0) + { + var T struct { + f []Q + } + _ = T + } + + // Defer. + defer g1(Q(0), 1) + + // Expression. + h(v + w + Q(1)) + + // For statements. + for i := Q(0); i < Q(10); i++ { + var T func(int) Q + v := T(0) + _ = v + } + + for { + var T func(int) Q + v := T(0) + _ = v + } + + // Go. + go g1(Q(0), 1) + + // If statements. + if a != Q(1) { + var T func(int) Q + v := T(0) + _ = v + } + + if a := Q(0); a != Q(1) { + var T func(int) Q + v := T(0) + _ = v + } + + if a := Q(0); a != Q(1) { + var T func(int) Q + v := T(0) + _ = v + } else if b := Q(0); b != Q(1) { + var T func(int) Q + v := T(0) + _ = v + } else if T := Q(0); T != 1 { + T++ + } else { + T-- + } + + if a := Q(0); a != Q(1) { + var T func(int) Q + v := T(0) + _ = v + } else { + var T func(int) Q + v := T(0) + _ = v + } + + // Inc/Dec statements. + (*(*Q)(nil))++ + (*(*Q)(nil))-- + + // Range statements. + for g(Q(0)).a, g(Q(1)).b = range g(Q(10)).c { + var d Q + _ = d + } + + for T, b := range g(Q(10)).c { + _ = T + _ = b + } + + // Select statement. + { + var fch func(Q) chan int + + select { + case <-fch(Q(30)): + var T Q + T = 0 + _ = T + default: + var T Q + T = 0 + _ = T + case T := <-fch(Q(30)): + T = 0 + _ = T + case g(Q(0)).a = <-fch(Q(30)): + var T Q + T = 0 + _ = T + case fch(Q(30)) <- int(Q(0)): + var T Q + T = 0 + _ = T + } + } + + // Send statements. + { + var ch chan Q + var fch func(Q) chan int + + ch <- Q(0) + fch(Q(1)) <- g(Q(10)).a + } + + // Switch statements. + { + var a Q + var b int + switch { + case a == Q(0): + var T Q + T = 0 + _ = T + case a < Q(0), b < g(Q(10)).a: + var T Q + T = 0 + _ = T + default: + var T Q + T = 0 + _ = T + } + } + + switch Q(g(Q(10)).a) { + case Q(0): + var T Q + T = 0 + _ = T + case Q(1), Q(g(Q(10)).a): + var T Q + T = 0 + _ = T + default: + var T Q + T = 0 + _ = T + } + + switch b := g(Q(10)); Q(b.a) + Q(10) { + case Q(0): + var T Q + T = 0 + _ = T + case Q(1), Q(g(Q(10)).a): + var T Q + T = 0 + _ = T + default: + var T Q + T = 0 + _ = T + } + + // Type switch statements. + { + var interfaceFunc func(Q) interface{} + + switch interfaceFunc(Q(0)).(type) { + case *Q, Q, int: + var T Q + T = 0 + _ = T + case sync.Mutex, **Q: + var T Q + T = 0 + _ = T + default: + var T Q + T = 0 + _ = T + } + + switch x := interfaceFunc(Q(0)).(type) { + case *Q, Q, int: + var T Q + T = 0 + _ = T + _ = x + case sync.Mutex, **Q: + var T Q + T = 0 + _ = T + default: + var T Q + T = 0 + _ = T + } + + switch t := Q(0); x := interfaceFunc(Q(0) + t).(type) { + case *Q, Q, int: + var T Q + T = 0 + _ = T + _ = x + case sync.Mutex, **Q: + var T Q + T = 0 + _ = T + default: + var T Q + T = 0 + _ = T + } + } + + // Return statement. + return Q(10), g(Q(11)).c +} diff --git a/tools/go_generics/tests/all_types/BUILD b/tools/go_generics/tests/all_types/BUILD new file mode 100644 index 000000000..9144b0811 --- /dev/null +++ b/tools/go_generics/tests/all_types/BUILD @@ -0,0 +1,10 @@ +load("//tools/go_generics/tests:defs.bzl", "go_generics_test") + +go_generics_test( + name = "all_types", + inputs = ["input.go"], + output = "output.go", + types = { + "T": "Q", + }, +) diff --git a/tools/go_generics/tests/all_types/input.go b/tools/go_generics/tests/all_types/input.go new file mode 100644 index 000000000..6f85bbb69 --- /dev/null +++ b/tools/go_generics/tests/all_types/input.go @@ -0,0 +1,45 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 tests + +import ( + "./lib" +) + +type T int + +type newType struct { + a T + b lib.T + c *T + d (T) + e chan T + f <-chan T + g chan<- T + h []T + i [10]T + j map[T]T + k func(T, T) (T, T) + l interface { + f(T) + } + m struct { + T + a T + } +} + +func f(...T) { +} diff --git a/tools/go_generics/tests/all_types/lib/lib.go b/tools/go_generics/tests/all_types/lib/lib.go new file mode 100644 index 000000000..988786496 --- /dev/null +++ b/tools/go_generics/tests/all_types/lib/lib.go @@ -0,0 +1,17 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 lib + +type T int32 diff --git a/tools/go_generics/tests/all_types/output.go b/tools/go_generics/tests/all_types/output.go new file mode 100644 index 000000000..c0bbebfe7 --- /dev/null +++ b/tools/go_generics/tests/all_types/output.go @@ -0,0 +1,43 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 main + +import ( + "./lib" +) + +type newType struct { + a Q + b lib.T + c *Q + d (Q) + e chan Q + f <-chan Q + g chan<- Q + h []Q + i [10]Q + j map[Q]Q + k func(Q, Q) (Q, Q) + l interface { + f(Q) + } + m struct { + Q + a Q + } +} + +func f(...Q) { +} diff --git a/tools/go_generics/tests/anon/BUILD b/tools/go_generics/tests/anon/BUILD new file mode 100644 index 000000000..1890f2817 --- /dev/null +++ b/tools/go_generics/tests/anon/BUILD @@ -0,0 +1,12 @@ +load("//tools/go_generics/tests:defs.bzl", "go_generics_test") + +go_generics_test( + name = "anon", + anon = True, + inputs = ["input.go"], + output = "output.go", + suffix = "New", + types = { + "T": "Q", + }, +) diff --git a/tools/go_generics/tests/anon/input.go b/tools/go_generics/tests/anon/input.go new file mode 100644 index 000000000..44086d522 --- /dev/null +++ b/tools/go_generics/tests/anon/input.go @@ -0,0 +1,46 @@ +// Copyright 2019 The gVisor Authors. +// +// 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 tests + +type T interface { + Apply(T) T +} + +type Foo struct { + T + Bar map[string]T `json:"bar,omitempty"` +} + +type Baz struct { + T someTypeNotT +} + +func (f Foo) GetBar(name string) T { + b, ok := f.Bar[name] + if ok { + b = f.Apply(b) + } else { + b = f.T + } + return b +} + +func foobar() { + a := Baz{} + a.T = 0 // should not be renamed, this is a limitation + + b := otherpkg.UnrelatedType{} + b.T = 0 // should not be renamed, this is a limitation +} diff --git a/tools/go_generics/tests/anon/output.go b/tools/go_generics/tests/anon/output.go new file mode 100644 index 000000000..7fa791853 --- /dev/null +++ b/tools/go_generics/tests/anon/output.go @@ -0,0 +1,42 @@ +// Copyright 2019 The gVisor Authors. +// +// 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 main + +type FooNew struct { + Q + Bar map[string]Q `json:"bar,omitempty"` +} + +type BazNew struct { + T someTypeNotT +} + +func (f FooNew) GetBar(name string) Q { + b, ok := f.Bar[name] + if ok { + b = f.Apply(b) + } else { + b = f.Q + } + return b +} + +func foobarNew() { + a := BazNew{} + a.Q = 0 + + b := otherpkg.UnrelatedType{} + b.Q = 0 +} diff --git a/tools/go_generics/tests/consts/BUILD b/tools/go_generics/tests/consts/BUILD new file mode 100644 index 000000000..1e3574593 --- /dev/null +++ b/tools/go_generics/tests/consts/BUILD @@ -0,0 +1,17 @@ +load("//tools/go_generics/tests:defs.bzl", "go_generics_test") + +go_generics_test( + name = "consts", + consts = { + "c1": "20", + "z": "600", + "v": "3.3", + "s": "\"def\"", + "A": "20", + "C": "100", + "S": "\"def\"", + "T": "\"ABC\"", + }, + inputs = ["input.go"], + output = "output.go", +) diff --git a/tools/go_generics/tests/consts/input.go b/tools/go_generics/tests/consts/input.go new file mode 100644 index 000000000..04b95fcc6 --- /dev/null +++ b/tools/go_generics/tests/consts/input.go @@ -0,0 +1,26 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 tests + +const c1 = 10 +const x, y, z = 100, 200, 300 +const v float32 = 1.0 + 2.0 +const s = "abc" +const ( + A = 10 + B, C, D = 10, 20, 30 + S = "abc" + T, U, V string = "abc", "def", "ghi" +) diff --git a/tools/go_generics/tests/consts/output.go b/tools/go_generics/tests/consts/output.go new file mode 100644 index 000000000..18d316cc9 --- /dev/null +++ b/tools/go_generics/tests/consts/output.go @@ -0,0 +1,26 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 main + +const c1 = 20 +const x, y, z = 100, 200, 600 +const v float32 = 3.3 +const s = "def" +const ( + A = 20 + B, C, D = 10, 100, 30 + S = "def" + T, U, V string = "ABC", "def", "ghi" +) diff --git a/tools/go_generics/tests/defs.bzl b/tools/go_generics/tests/defs.bzl new file mode 100644 index 000000000..038629852 --- /dev/null +++ b/tools/go_generics/tests/defs.bzl @@ -0,0 +1,67 @@ +"""Generics tests.""" + +load("//tools/go_generics:defs.bzl", "go_template", "go_template_instance") + +def _go_generics_test_impl(ctx): + runner = ctx.actions.declare_file(ctx.label.name) + runner_content = "\n".join([ + "#!/bin/bash", + "exec diff --ignore-blank-lines --ignore-matching-lines=^[[:space:]]*// %s %s" % ( + ctx.files.template_output[0].short_path, + ctx.files.expected_output[0].short_path, + ), + "", + ]) + ctx.actions.write(runner, runner_content, is_executable = True) + return [DefaultInfo( + executable = runner, + runfiles = ctx.runfiles( + files = ctx.files.template_output + ctx.files.expected_output, + collect_default = True, + collect_data = True, + ), + )] + +_go_generics_test = rule( + implementation = _go_generics_test_impl, + attrs = { + "template_output": attr.label(mandatory = True, allow_single_file = True), + "expected_output": attr.label(mandatory = True, allow_single_file = True), + }, + test = True, +) + +""" +Instantiates a generics test. + +Args: + name: the name of the test. + inputs: all the input files. + output: the output files. + opts: the template options. +""" + +def go_generics_test(name, inputs, output, types = None, consts = None, **kwargs): + if types == None: + types = dict() + if consts == None: + consts = dict() + go_template( + name = name + "_template", + srcs = inputs, + types = types.keys(), + consts = consts.keys(), + ) + go_template_instance( + name = name + "_output", + template = ":" + name + "_template", + out = name + "_output.go", + types = types, + consts = consts, + **kwargs + ) + _go_generics_test( + name = name + "_test", + template_output = name + "_output.go", + expected_output = output, + ) diff --git a/tools/go_generics/tests/imports/BUILD b/tools/go_generics/tests/imports/BUILD new file mode 100644 index 000000000..c4424ee9c --- /dev/null +++ b/tools/go_generics/tests/imports/BUILD @@ -0,0 +1,18 @@ +load("//tools/go_generics/tests:defs.bzl", "go_generics_test") + +go_generics_test( + name = "imports", + consts = { + "n": "math.Uint32", + "m": "math.Uint64", + }, + imports = { + "sync": "sync", + "math": "mymathpath", + }, + inputs = ["input.go"], + output = "output.go", + types = { + "T": "sync.Mutex", + }, +) diff --git a/tools/go_generics/tests/imports/input.go b/tools/go_generics/tests/imports/input.go new file mode 100644 index 000000000..0f032c2a1 --- /dev/null +++ b/tools/go_generics/tests/imports/input.go @@ -0,0 +1,24 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 tests + +type T int + +var global T + +const ( + m = 0 + n = 0 +) diff --git a/tools/go_generics/tests/imports/output.go b/tools/go_generics/tests/imports/output.go new file mode 100644 index 000000000..2488ca58c --- /dev/null +++ b/tools/go_generics/tests/imports/output.go @@ -0,0 +1,27 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 main + +import ( + __generics_imported1 "mymathpath" + __generics_imported0 "sync" +) + +var global __generics_imported0.Mutex + +const ( + m = __generics_imported1.Uint64 + n = __generics_imported1.Uint32 +) diff --git a/tools/go_generics/tests/remove_typedef/BUILD b/tools/go_generics/tests/remove_typedef/BUILD new file mode 100644 index 000000000..84f6ac1ab --- /dev/null +++ b/tools/go_generics/tests/remove_typedef/BUILD @@ -0,0 +1,10 @@ +load("//tools/go_generics/tests:defs.bzl", "go_generics_test") + +go_generics_test( + name = "remove_typedef", + inputs = ["input.go"], + output = "output.go", + types = { + "T": "U", + }, +) diff --git a/tools/go_generics/tests/remove_typedef/input.go b/tools/go_generics/tests/remove_typedef/input.go new file mode 100644 index 000000000..cf632bae7 --- /dev/null +++ b/tools/go_generics/tests/remove_typedef/input.go @@ -0,0 +1,37 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 tests + +func f(T) Q { + return Q{} +} + +type T struct{} + +type Q struct{} + +func (*T) f() { +} + +func (T) g() { +} + +func (*Q) f(T) T { + return T{} +} + +func (*Q) g(T) *T { + return nil +} diff --git a/tools/go_generics/tests/remove_typedef/output.go b/tools/go_generics/tests/remove_typedef/output.go new file mode 100644 index 000000000..d44fd8e1c --- /dev/null +++ b/tools/go_generics/tests/remove_typedef/output.go @@ -0,0 +1,29 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 main + +func f(U) Q { + return Q{} +} + +type Q struct{} + +func (*Q) f(U) U { + return U{} +} + +func (*Q) g(U) *U { + return nil +} diff --git a/tools/go_generics/tests/simple/BUILD b/tools/go_generics/tests/simple/BUILD new file mode 100644 index 000000000..204952b90 --- /dev/null +++ b/tools/go_generics/tests/simple/BUILD @@ -0,0 +1,11 @@ +load("//tools/go_generics/tests:defs.bzl", "go_generics_test") + +go_generics_test( + name = "simple", + inputs = ["input.go"], + output = "output.go", + suffix = "New", + types = { + "T": "Q", + }, +) diff --git a/tools/go_generics/tests/simple/input.go b/tools/go_generics/tests/simple/input.go new file mode 100644 index 000000000..2a917f16c --- /dev/null +++ b/tools/go_generics/tests/simple/input.go @@ -0,0 +1,45 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 tests + +type T int + +var global T + +func f(_ T, a int) { +} + +func g(a T, b int) { + var c T + _ = c + + d := (*T)(nil) + _ = d +} + +type R struct { + T + a *T +} + +var ( + Z *T = (*T)(nil) +) + +const ( + X T = (T)(0) +) + +type Y T diff --git a/tools/go_generics/tests/simple/output.go b/tools/go_generics/tests/simple/output.go new file mode 100644 index 000000000..6bfa0b25b --- /dev/null +++ b/tools/go_generics/tests/simple/output.go @@ -0,0 +1,43 @@ +// Copyright 2018 The gVisor Authors. +// +// 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 main + +var globalNew Q + +func fNew(_ Q, a int) { +} + +func gNew(a Q, b int) { + var c Q + _ = c + + d := (*Q)(nil) + _ = d +} + +type RNew struct { + Q + a *Q +} + +var ( + ZNew *Q = (*Q)(nil) +) + +const ( + XNew Q = (Q)(0) +) + +type YNew Q -- cgit v1.2.3