Skip to content

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

Closed
@mateusz834

Description

@mateusz834

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

Metadata

Assignees

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.NeedsFixThe path to resolution is known, but the work has not been done.TestingAn issue that has been verified to require only test changes, not just a test failure.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions