Skip to content

Commit 4bc74c3

Browse files
committed
gopls/internal/golang: enable bug.Report in semantic tokens
Also, simplify the handling of constants and address a couple of TODOs from the preceding refactoring. Change-Id: Ic2e2231a4eb2172b365969d2d2b251572287e511 Reviewed-on: https://go-review.googlesource.com/c/tools/+/564655 Reviewed-by: Peter Weinberger <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 32d3139 commit 4bc74c3

File tree

1 file changed

+10
-25
lines changed

1 file changed

+10
-25
lines changed

gopls/internal/golang/semtok.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"golang.org/x/tools/gopls/internal/file"
2626
"golang.org/x/tools/gopls/internal/protocol"
2727
"golang.org/x/tools/gopls/internal/protocol/semtok"
28+
"golang.org/x/tools/gopls/internal/util/bug"
2829
"golang.org/x/tools/gopls/internal/util/safetoken"
2930
"golang.org/x/tools/internal/event"
3031
)
@@ -392,24 +393,11 @@ func (tv *tokenVisitor) ident(id *ast.Ident) {
392393
case *types.Builtin:
393394
emit(semtok.TokFunction, "defaultLibrary")
394395
case *types.Const:
395-
switch t := obj.Type().(type) {
396-
// TODO(adonovan): set defaultLibrary modified for symbols in types.Universe.
397-
case *types.Basic:
396+
if is[*types.Named](obj.Type()) &&
397+
(id.Name == "iota" || id.Name == "true" || id.Name == "false") {
398+
emit(semtok.TokVariable, "readonly", "defaultLibrary")
399+
} else {
398400
emit(semtok.TokVariable, "readonly")
399-
case *types.Named:
400-
if id.Name == "iota" {
401-
// TODO(adonovan): this is almost certainly reachable.
402-
// Add a test and do something sensible, e.g.
403-
// emit(semtok.TokVariable, "readonly", "defaultLibrary")
404-
tv.errorf("iota:%T", t)
405-
}
406-
if is[*types.Basic](t.Underlying()) {
407-
emit(semtok.TokVariable, "readonly")
408-
} else {
409-
tv.errorf("%q/%T", id.Name, t)
410-
}
411-
default:
412-
tv.errorf("%s %T %#v", id.Name, t, t) // can't happen
413401
}
414402
case *types.Func:
415403
emit(semtok.TokFunction)
@@ -479,10 +467,9 @@ func (tv *tokenVisitor) isParam(pos token.Pos) bool {
479467
// but in that case it is all best-effort from the parse tree.
480468
func (tv *tokenVisitor) unkIdent(id *ast.Ident) (semtok.TokenType, []string) {
481469
def := []string{"definition"}
482-
n := len(tv.stack) - 2 // parent of Ident
470+
n := len(tv.stack) - 2 // parent of Ident; stack is [File ... Ident]
483471
if n < 0 {
484-
// TODO(adonovan): I suspect this reachable for the "package p" declaration.
485-
tv.errorf("no stack?")
472+
tv.errorf("no stack") // can't happen
486473
return "", nil
487474
}
488475
switch parent := tv.stack[n].(type) {
@@ -693,7 +680,7 @@ func (tv *tokenVisitor) definitionFor(id *ast.Ident, obj types.Object) (semtok.T
693680
}
694681
// can't happen
695682
tv.errorf("failed to find the decl for %s", safetoken.Position(tv.pgf.Tok, id.Pos()))
696-
return "", []string{""} // TODO(adonovan): shouldn't that be []string{}?
683+
return "", nil
697684
}
698685

699686
func isTypeParam(id *ast.Ident, t *ast.FuncType) bool {
@@ -786,12 +773,10 @@ func (tv *tokenVisitor) importSpec(spec *ast.ImportSpec) {
786773
tv.token(start, len(depMD.Name), semtok.TokNamespace, nil)
787774
}
788775

789-
// errorf logs an error. When debugging is enabled, it panics.
776+
// errorf logs an error and reports a bug.
790777
func (tv *tokenVisitor) errorf(format string, args ...any) {
791778
msg := fmt.Sprintf(format, args...)
792-
if semDebug {
793-
panic(msg)
794-
}
779+
bug.Report(msg)
795780
event.Error(tv.ctx, tv.strStack(), errors.New(msg))
796781
}
797782

0 commit comments

Comments
 (0)