internal/lsp: handle definitions for variables with error types

Fixes golang/go#31465

Change-Id: I2f79fe2167bab79b497125995efc938f2b63d274
Reviewed-on: https://go-review.googlesource.com/c/tools/+/172117
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
diff --git a/internal/lsp/cmd/definition_test.go b/internal/lsp/cmd/definition_test.go
index 06951a5..f16948d 100644
--- a/internal/lsp/cmd/definition_test.go
+++ b/internal/lsp/cmd/definition_test.go
@@ -24,7 +24,7 @@
 )
 
 const (
-	expectedDefinitionsCount     = 25
+	expectedDefinitionsCount     = 26
 	expectedTypeDefinitionsCount = 2
 )
 
diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go
index 22a502b..ef87e79 100644
--- a/internal/lsp/lsp_test.go
+++ b/internal/lsp/lsp_test.go
@@ -40,7 +40,7 @@
 	const expectedCompletionsCount = 64
 	const expectedDiagnosticsCount = 16
 	const expectedFormatCount = 4
-	const expectedDefinitionsCount = 16
+	const expectedDefinitionsCount = 17
 	const expectedTypeDefinitionsCount = 2
 	const expectedHighlightsCount = 2
 	const expectedSymbolsCount = 1
diff --git a/internal/lsp/source/definition.go b/internal/lsp/source/definition.go
index 64da398..2ee7592 100644
--- a/internal/lsp/source/definition.go
+++ b/internal/lsp/source/definition.go
@@ -111,6 +111,10 @@
 	}
 	result.Type.Object = typeToObject(typ)
 	if result.Type.Object != nil {
+		// Identifiers with the type "error" are a special case with no position.
+		if types.IsInterface(result.Type.Object.Type()) && result.Type.Object.Pkg() == nil && result.Type.Object.Name() == "error" {
+			return result, nil
+		}
 		if result.Type.Range, err = objToRange(ctx, v, result.Type.Object); err != nil {
 			return nil, err
 		}
diff --git a/internal/lsp/testdata/godef/a/a.go b/internal/lsp/testdata/godef/a/a.go
index e9c9674..a927333 100644
--- a/internal/lsp/testdata/godef/a/a.go
+++ b/internal/lsp/testdata/godef/a/a.go
@@ -2,10 +2,15 @@
 
 package a
 
+import "fmt"
+
 type A string //@A
 
 func Stuff() { //@Stuff
 	x := 5
 	Random2(x) //@godef("dom2", Random2)
 	Random()   //@godef("()", Random)
+
+	var err error         //@err
+	fmt.Printf("%v", err) //@godef("err", err)
 }