Skip to content

No ERROR reported when a wrong type given in function's variable argument in specified code #2785

@Littlefisher619

Description

@Littlefisher619

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    go version go1.20.3 linux/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    Build info
    ----------
    golang.org/x/tools/gopls v0.11.0
        golang.org/x/tools/gopls@v0.11.0 h1:/nvKHdTtePQmrv9XN3gIUN9MOdUrKzO/dcqgbG6x8EY=
        github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
        github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
        github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
        golang.org/x/exp@v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
        golang.org/x/exp/typeparams@v0.0.0-20221031165847-c99f073a8326 h1:fl8k2zg28yA23264d82M4dp+YlJ3ngDcpuB1bewkQi4=
        golang.org/x/mod@v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
        golang.org/x/sync@v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
        golang.org/x/sys@v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
        golang.org/x/text@v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
        golang.org/x/tools@v0.3.1-0.20221213193459-ca17b2c27ca8 h1:7/HkGkN/2ktghBCSRRgp31wAww4syfsW52tj7yirjWk=
        golang.org/x/vuln@v0.0.0-20221109205719-3af8368ee4fe h1:qptQiQwEpETwDiz85LKtChqif9xhVkAm8Nhxs0xnTww=
        honnef.co/go/tools@v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA=
        mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
        mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
    go: go1.20.3
    
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    1.78.2
    b3e4e68a0bc097f0ae7907b217c1119af9e03435
    x64
    
  • Check your installed extensions to get the version of the VS Code Go extension
    Name: Go
    Id: golang.go
    Description: Rich Go language support for Visual Studio Code
    Version: 0.38.0
    Publisher: Go Team at Google
    VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=golang.Go
    
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
    Checking configured tools....
    GOBIN: /data/go/bin
    toolsGopath: 
    gopath: /data/go
    GOROOT: /root/sdk/go1.20.3
    PATH: /root/sdk/go1.20.3/bin:/root/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/data/go/bin:/root/.local/bin
    PATH (vscode launched with): /root/.vscode-server/bin/b3e4e68a0bc097f0ae7907b217c1119af9e03435/bin/remote-cli:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/data/go/bin:/root/.local/bin
    
        go:	/root/sdk/go1.20.3/bin/go: go version go1.20.3 linux/amd64
    
        gotests:	/data/go/bin/gotests	(version: v1.6.0 built with go: go1.20.3)
        gomodifytags:	/data/go/bin/gomodifytags	(version: v1.16.0 built with go: go1.20.3)
        impl:	/data/go/bin/impl	(version: v1.1.0 built with go: go1.20.3)
        goplay:	/data/go/bin/goplay	(version: v1.0.0 built with go: go1.20.3)
        dlv:	/data/go/bin/dlv	(version: v1.20.2 built with go: go1.20.3)
        golint:	/data/go/bin/golint	(version: v0.0.0-20210508222113-6edffad5e616 built with go: go1.20.3)
        gopls:	/data/go/bin/gopls	(version: v0.11.0 built with go: go1.20.3)
    
    go env
    Workspace Folder (bugtest): /data/devhome/bugtest
        GO111MODULE="on"
        GOARCH="amd64"
        GOBIN="/data/go/bin"
        GOCACHE="/root/.cache/go-build"
        GOENV="/root/.config/go/env"
        GOEXE=""
        GOEXPERIMENT=""
        GOFLAGS=""
        GOHOSTARCH="amd64"
        GOHOSTOS="linux"
        GOINSECURE="<private>"
        GOMODCACHE="/data/go/pkg/mod"
        GONOPROXY=""
        GONOSUMDB=""
        GOOS="linux"
        GOPATH="/data/go"
        GOPRIVATE=""
        GOPROXY="https://<private>,direct"
        GOROOT="/root/sdk/go1.20.3"
        GOSUMDB="<private>"
        GOTMPDIR=""
        GOTOOLDIR="/root/sdk/go1.20.3/pkg/tool/linux_amd64"
        GOVCS=""
        GOVERSION="go1.20.3"
        GCCGO="gccgo"
        GOAMD64="v1"
        AR="ar"
        CC="gcc"
        CXX="g++"
        CGO_ENABLED="1"
        GOMOD="/data/devhome/bugtest/go.mod"
        GOWORK=""
        CGO_CFLAGS="-O2 -g"
        CGO_CPPFLAGS=""
        CGO_CXXFLAGS="-O2 -g"
        CGO_FFLAGS="-O2 -g"
        CGO_LDFLAGS="-O2 -g"
        PKG_CONFIG="pkg-config"
        GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build203008646=/tmp/go-build -gno-record-gcc-switches"
    

Share the Go related settings you have added/edited

{
    "go.autocompleteUnimportedPackages": true,
    "go.lintTool": "golint"
}

Describe the bug

A clear and concise description of what the bug:

  • When put a mismatched type into the variable argument, there are no error shown on the editor.

A clear and concise description of what you expected to happen:

  • An error report should be shown includes a syntax error on mismatched type given in function's variable argument
  • The expected error message like this should be output: cannot use "test" (constant of type string) as MeterOption value in argument to x.(MeterProvider).Meter: string does not implement MeterOption (missing method applyMeter) with hint complier(InvalidIfaceAssign)
image

I know it is really looks odd. But it does. And I asked my friends to reproduce with my code. All of them reproduced.

Steps to reproduce the behavior:

  1. Init the environment the version info given above.
  2. Put the code to editor as main.go.
package main

import "go.opentelemetry.io/otel/metric"

type MeterProvider interface {
    Meter(instrumentationName string, opts ...metric.MeterOption) metric.Meter
}

func main() {
    var x interface{} = 1

    x.(MeterProvider).Meter("test", "bla")
                                // bug --^---
                                // error should be reported here
}
  1. Reproduced. Check your problems output in vscode. You see there are no errors.
No errors in problems

Other ways to reproduce the bug:

Other ways to reproduce
Code
package main

import "go.opentelemetry.io/otel/metric"

type MeterProvider interface {
	Meter(instrumentationName string, opts ...metric.MeterOption) metric.Meter
}
type MyProvider interface {
	Reproduce(xxx string, opts ...metric.MeterOption) metric.Meter
}
type VariableFuncInterface interface {
	Func(xxx string, opts ...int) string
}

func main() {
	var x interface{} = 1

	x.(MeterProvider).Meter("test", "test")
    x.(metric.MeterProvider).Meter("test", 123)
    x.(MyProvider).Reproduce("test", 123)
                                // bug --^---
                                // error shoud be reported here
	x.(VariableFuncInterface).Func("", "")
                                // not produced here
}

Note:

  • I reproduced this bug only when import the go.opentelemetry.io/otel/metric package. Whatever the function is, the point is that choose a variadic function and call it. For me, I tried to call Meter() func to a metric.MeterProvider and call Float64Histogram the metric.Meter. All of them can produce the bug.
  • When all the related code had copied from the source package into my project's code, the bug was not happened. Maybe I missed somewhere matters.
Code & Screenshot
Not Produced
package main

import (
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/metric"
)

type MeterProvider interface {
	Meter(instrumentationName string, opts MeterOption) metric.Meter
}

// MeterConfig contains options for Meters.
type MeterConfig struct {
	instrumentationVersion string
	schemaURL              string
	attrs                  attribute.Set

	// Ensure forward compatibility by explicitly making this not comparable.
	noCmp [0]func() //nolint: unused  // This is indeed used.
}

// InstrumentationVersion returns the version of the library providing
// instrumentation.
func (cfg MeterConfig) InstrumentationVersion() string {
	return cfg.instrumentationVersion
}

// InstrumentationAttributes returns the attributes associated with the library
// providing instrumentation.
func (cfg MeterConfig) InstrumentationAttributes() attribute.Set {
	return cfg.attrs
}

// SchemaURL is the schema_url of the library providing instrumentation.
func (cfg MeterConfig) SchemaURL() string {
	return cfg.schemaURL
}

// MeterOption is an interface for applying Meter options.
type MeterOption interface {
	// applyMeter is used to set a MeterOption value of a MeterConfig.
	applyMeter(MeterConfig) MeterConfig
}

// NewMeterConfig creates a new MeterConfig and applies
// all the given options.
func NewMeterConfig(opts ...MeterOption) MeterConfig {
	var config MeterConfig
	for _, o := range opts {
		config = o.applyMeter(config)
	}
	return config
}

type meterOptionFunc func(MeterConfig) MeterConfig

func (fn meterOptionFunc) applyMeter(cfg MeterConfig) MeterConfig {
	return fn(cfg)
}

// WithInstrumentationVersion sets the instrumentation version.
func WithInstrumentationVersion(version string) MeterOption {
	return meterOptionFunc(func(config MeterConfig) MeterConfig {
		config.instrumentationVersion = version
		return config
	})
}

// WithInstrumentationAttributes sets the instrumentation attributes.
//
// The passed attributes will be de-duplicated.
func WithInstrumentationAttributes(attr ...attribute.KeyValue) MeterOption {
	return meterOptionFunc(func(config MeterConfig) MeterConfig {
		config.attrs = attribute.NewSet(attr...)
		return config
	})
}

// WithSchemaURL sets the schema URL.
func WithSchemaURL(schemaURL string) MeterOption {
	return meterOptionFunc(func(config MeterConfig) MeterConfig {
		config.schemaURL = schemaURL
		return config
	})
}


func main() {
	var x interface{} = 1

	x.(MeterProvider).Meter("test", "test")
}

Screenshots or recordings

Simple POC

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.goplsgopls related issues

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions