Skip to content

affected/package: #52787

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

Closed
andviro opened this issue May 9, 2022 · 1 comment
Closed

affected/package: #52787

andviro opened this issue May 9, 2022 · 1 comment

Comments

@andviro
Copy link

andviro commented May 9, 2022

What version of Go are you using (go version)?

$ go version
go version go1.18.1 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/andrew/.cache/go-build"
GOENV="/home/andrew/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/andrew/go/pkg/mod"
GOOS="linux"
GOPATH="/home/andrew/go"
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3070082678=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I'm writing a simple package for wrapping functions, similarly to HTTP middleware, which allow adding metrics, logging etc without having to resort to code generation.

package wrappers

import "context"

type Func[A, B any] func(ctx context.Context, req A) (resp B, err error)

type Wrapper[A, B any] func(src Func[A, B]) Func[A, B]

func Wrap[A, B any](src Func[A, B], wrappers ...Wrapper[A, B]) Func[A, B] {
	for i := len(wrappers) - 1; i >= 0; i-- {
		src = wrappers[i](src)
	}
	return src
}

Testing the functionality I've wrote following code:

type testA struct {
	x int
}

type testB struct {
	y string
}

func testWrapper[A, B any](dest io.Writer) wrappers.Wrapper[A, B] {
	return func(src wrappers.Func[A, B]) wrappers.Func[A, B] {
		return func(ctx context.Context, a A) (B, error) {
			fmt.Fprintf(dest, "calling with %#v\n", a)
			res, err := src(ctx, a)
			fmt.Fprintf(dest, "got: %#v, %+v", res, err)
			return res, err
		}
	}
}
func TestWrappers_Wrap(t *testing.T) {
	f := func(ctx context.Context, req *testA) (resp *testB, err error) {
		return &testB{
			y: fmt.Sprintf("(%d)", req.x),
		}, nil
	}
	buf := new(bytes.Buffer)
	f2 := wrappers.Wrap(f, testWrapper(buf))
	f2(context.TODO(), &testA{10})
	t.Logf("%s", buf.String())
}

What did you expect to see?

I expected to see the test to pass:

$go test -v
=== RUN   TestWrappers_Wrap
    wrapper_test.go:40: calling with &wrappers_test.testA{x:10}
        got: &wrappers_test.testB{y:"(10)"}, <nil>
--- PASS: TestWrappers_Wrap (0.00s)
PASS
ok      github.com/andviro/wrappers     0.003s

What did you see instead?

$go test -v
# github.com/andviro/wrappers_test [github.com/andviro/wrappers.test]
./wrapper_test.go:38:36: cannot infer A (/home/andrew/go/src/github.com/andviro/wrappers/w
rapper_test.go:21:18)
FAIL    github.com/andviro/wrappers [build failed]

I think that it's a bug in type inference system, because type A is clearly inferred from first parameter of wrappers.Wrap, the function f. Without second parameter code compiles without error.

@seankhliao
Copy link
Member

Duplicate of #47868

@seankhliao seankhliao marked this as a duplicate of #47868 May 9, 2022
@golang golang locked and limited conversation to collaborators May 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants