Skip to content

go/types: dot imports might cause errors where (types.Error).Pos is not part of the (types.Error).Fset #71272

Open
@mateusz834

Description

Reproducer:

package main

import (
	"fmt"
	"go/ast"
	"go/importer"
	"go/parser"
	"go/token"
	"go/types"
)

func main() {
	fmt.Println(run())
}

const src = `package main

import . "net/http"

type Request struct{}
`

func run() error {
	fset := token.NewFileSet()
	f, err := parser.ParseFile(fset, "test.go", src, parser.SkipObjectResolution)
	if err != nil {
		return err
	}

	cfg := types.Config{
		Importer: importer.Default(),
		Error: func(err error) {
			e := err.(types.Error)
			fmt.Println(e.Msg, e.Pos, e.Fset.Position(e.Pos))
		},
	}

	cfg.Check("test", fset, []*ast.File{f}, nil)

	return nil
}
$ go run .
Request already declared through dot-import of package http ("net/http") 41 test.go:5:6
        other declaration of Request 2883740 -
"net/http" imported and not used 22 test.go:3:8
<nil>

Note the second error other declaration of Request 2883740 -, which tries to reference the file in the net/http package.

CC @adonovan @griesemer @findleyr

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions