From da6042e82eb7fe5ba40a5c17959a31e19a44c3e8 Mon Sep 17 00:00:00 2001 From: David Chase Date: Fri, 14 Oct 2022 16:24:44 -0400 Subject: [PATCH] cmd/compile: fix position of fake receivers; be more careful in logopt The src.NoXPos in fake receivers was leaking, through a series of mishaps, all the way to logopt. If done just so, this can lead to a compiler crash. This makes logopt crash-proof and eliminates the root cause as well. I'm reluctant to write a test for this because it's kinda slow and involved; my working test is "compile something that mentions the flag package with -json=0,$TMPDIR flag, then be sure that $TMPDIR/flag/__unnamed__.json was not created". Change-Id: I384b717c0e7522953d22d61f7e06319e11192d7d Reviewed-on: https://go-review.googlesource.com/c/go/+/443156 Run-TryBot: David Chase TryBot-Result: Gopher Robot Reviewed-by: Michael Pratt --- src/cmd/compile/internal/logopt/log_opts.go | 3 +++ src/cmd/compile/internal/types/type.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cmd/compile/internal/logopt/log_opts.go b/src/cmd/compile/internal/logopt/log_opts.go index 711b5b9d75..5c7f580f0b 100644 --- a/src/cmd/compile/internal/logopt/log_opts.go +++ b/src/cmd/compile/internal/logopt/log_opts.go @@ -405,6 +405,9 @@ func uriIfy(f string) DocumentURI { // Return filename, replacing a first occurrence of $GOROOT with the // actual value of the GOROOT (because LSP does not speak "$GOROOT"). func uprootedPath(filename string) string { + if filename == "" { + return "__unnamed__" + } if buildcfg.GOROOT == "" || !strings.HasPrefix(filename, "$GOROOT/") { return filename } diff --git a/src/cmd/compile/internal/types/type.go b/src/cmd/compile/internal/types/type.go index f4e6b92bd6..8329837a30 100644 --- a/src/cmd/compile/internal/types/type.go +++ b/src/cmd/compile/internal/types/type.go @@ -1680,7 +1680,7 @@ func FakeRecvType() *Type { } func FakeRecv() *Field { - return NewField(src.NoXPos, nil, FakeRecvType()) + return NewField(base.AutogeneratedPos, nil, FakeRecvType()) } var (