Skip to content

go/types: types.Error.go116end end position derived from placeholder Ident.Name is garbage #81876

Open

Description

@adonovan

Type-checking the code below results in a types.Error whose hidden go116end field (which gopls accesses via reflection) is invalid:

package p

type I interface{ M() }

var m map[int]int

func _() {
	var a int
	var b I
	a, b = m[0] // error: cannot use ok value of (comma, ok) expression ... as I value
	_, _ = a, b
}

Standalone repro:

package main

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

func main() {
	for _, src := range []string{
		`package p; type I interface{ M() }; var m map[int]int; func _() { var a int; var b I; a, b = m[0]; _, _ = a, b }`,
		`package p; type I interface{ M() }; func f() (int, int); func _() { var a I; var b int; a, b = f(); _, _ = a, b }`,
	} {
		fset := token.NewFileSet()
		f, _ := parser.ParseFile(fset, "a.go", src, 0)
		conf := types.Config{Error: func(err error) {
			e := err.(types.Error)
			v := reflect.ValueOf(e)
			start, end := v.FieldByName("go116start").Int(), v.FieldByName("go116end").Int()
			fmt.Printf("%s\n\tspan [%d,%d) len=%d; file size %d\n", e.Msg, start, end, end-start, len(src))
		}}
		conf.Check("p", fset, []*ast.File{f}, nil)
	}
}

It prints:

cannot use ok value of (comma, ok) expression (value of type bool) as I value in multiple assignment: bool does not implement I (missing method M)
	span [94,128) len=34; file size 112
cannot use 1st function result (value of type int) as I value in multiple assignment: int does not implement I (missing method M)
	span [96,115) len=19; file size 113

The start position is accurate but the end is not, as it is derived from the length of the error description crammed by (*Checker).multiExpr into the Ident.Name field.So len("ok value of (comma, ok) expression") == 34 and len("1st function result") == 19. Both values fall past EOF and land in an unrelated file. A similar pattern appears in genericExprList. These bogus end positions are the root cause of the crash in gopls described in #81814.

A fix would be to use the real extent for the error span and carry the description separately.

Activity

  1. gopherbot commented on Oct 3, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/844245 mentions this issue: go/types: preserve source spans for synthetic operands

  2. dmitshur commented on Oct 5, 2026

    @dmitshur
    Member
  3. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Oct 5, 2026
  4. added this to the Backlog milestone on Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions