@@ -25,6 +25,7 @@ import (
25
25
"golang.org/x/tools/gopls/internal/file"
26
26
"golang.org/x/tools/gopls/internal/protocol"
27
27
"golang.org/x/tools/gopls/internal/protocol/semtok"
28
+ "golang.org/x/tools/gopls/internal/util/bug"
28
29
"golang.org/x/tools/gopls/internal/util/safetoken"
29
30
"golang.org/x/tools/internal/event"
30
31
)
@@ -392,24 +393,11 @@ func (tv *tokenVisitor) ident(id *ast.Ident) {
392
393
case * types.Builtin :
393
394
emit (semtok .TokFunction , "defaultLibrary" )
394
395
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 {
398
400
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
413
401
}
414
402
case * types.Func :
415
403
emit (semtok .TokFunction )
@@ -479,10 +467,9 @@ func (tv *tokenVisitor) isParam(pos token.Pos) bool {
479
467
// but in that case it is all best-effort from the parse tree.
480
468
func (tv * tokenVisitor ) unkIdent (id * ast.Ident ) (semtok.TokenType , []string ) {
481
469
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]
483
471
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
486
473
return "" , nil
487
474
}
488
475
switch parent := tv .stack [n ].(type ) {
@@ -693,7 +680,7 @@ func (tv *tokenVisitor) definitionFor(id *ast.Ident, obj types.Object) (semtok.T
693
680
}
694
681
// can't happen
695
682
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
697
684
}
698
685
699
686
func isTypeParam (id * ast.Ident , t * ast.FuncType ) bool {
@@ -786,12 +773,10 @@ func (tv *tokenVisitor) importSpec(spec *ast.ImportSpec) {
786
773
tv .token (start , len (depMD .Name ), semtok .TokNamespace , nil )
787
774
}
788
775
789
- // errorf logs an error. When debugging is enabled, it panics .
776
+ // errorf logs an error and reports a bug .
790
777
func (tv * tokenVisitor ) errorf (format string , args ... any ) {
791
778
msg := fmt .Sprintf (format , args ... )
792
- if semDebug {
793
- panic (msg )
794
- }
779
+ bug .Report (msg )
795
780
event .Error (tv .ctx , tv .strStack (), errors .New (msg ))
796
781
}
797
782
0 commit comments