Skip to content

Commit 454be60

Browse files
adonovangopherbot
authored andcommitted
x/tools: be defensive after types.Info.Types[expr] lookups
This CL is the result of a quick audit of x/tools for places that look in the Types map and do not handle missing entries gracefully (unless dominated by a check for welltypedness, such as RunDespiteErrors:false in the analysis framework). In each case it either adds a defensive check or documents the assumption. See golang/go#69092 (comment) Updates golang/go#69092 Change-Id: I3573512fd47ee4dca2e0b4bce2803b92424d7037 Reviewed-on: https://go-review.googlesource.com/c/tools/+/617416 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent dec6bf1 commit 454be60

File tree

8 files changed

+15
-8
lines changed

8 files changed

+15
-8
lines changed

go/types/internal/play/play.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,10 @@ func handleSelectJSON(w http.ResponseWriter, req *http.Request) {
194194
if tv.Value != nil {
195195
fmt.Fprintf(out, ", and constant value %v", tv.Value)
196196
}
197-
fmt.Fprintf(out, "\n\n")
197+
} else {
198+
fmt.Fprintf(out, "%T has no type", innermostExpr)
198199
}
200+
fmt.Fprintf(out, "\n\n")
199201
}
200202

201203
// selection x.f information (if cursor is over .f)

gopls/internal/cache/testfuncs/tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (b *indexBuilder) findSubtests(parent gobTest, typ *ast.FuncType, body *ast
156156
continue // subtest has wrong signature
157157
}
158158

159-
val := info.Types[call.Args[0]].Value
159+
val := info.Types[call.Args[0]].Value // may be zero
160160
if val == nil || val.Kind() != constant.String {
161161
continue
162162
}

gopls/internal/golang/completion/printf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func printfArgKind(info *types.Info, call *ast.CallExpr, argIdx int) objKind {
4040
}
4141

4242
// Format string must be a constant.
43-
strArg := info.Types[call.Args[numParams-2]].Value
43+
strArg := info.Types[call.Args[numParams-2]].Value // may be zero
4444
if strArg == nil || strArg.Kind() != constant.String {
4545
return kindAny
4646
}

gopls/internal/golang/freesymbols.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,11 @@ func freeRefs(pkg *types.Package, info *types.Info, file *ast.File, start, end t
372372

373373
if ref != nil {
374374
ref.expr = n.(ast.Expr)
375-
ref.typ = info.Types[n.(ast.Expr)].Type
375+
if tv, ok := info.Types[ref.expr]; ok {
376+
ref.typ = tv.Type
377+
} else {
378+
ref.typ = types.Typ[types.Invalid]
379+
}
376380
free = append(free, ref)
377381
}
378382

gopls/internal/golang/highlight.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,6 @@ func highlightIdentifier(id *ast.Ident, file *ast.File, info *types.Info, result
558558
highlightWriteInExpr(n.Chan)
559559
case *ast.CompositeLit:
560560
t := info.TypeOf(n)
561-
// Every expression should have a type;
562-
// work around https://github.com/golang/go/issues/69092.
563561
if t == nil {
564562
t = types.Typ[types.Invalid]
565563
}

gopls/internal/golang/identifier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ Outer:
175175
var typ types.Type
176176
if assign, ok := ts.Assign.(*ast.AssignStmt); ok && len(assign.Rhs) == 1 {
177177
if rhs := assign.Rhs[0].(*ast.TypeAssertExpr); ok {
178-
typ = info.TypeOf(rhs.X)
178+
typ = info.TypeOf(rhs.X) // may be nil
179179
}
180180
}
181181
return objs, typ

internal/refactor/inline/escape.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ func escape(info *types.Info, root ast.Node, f func(v *types.Var, escapes bool))
4141
//
4242
// We must traverse the normal terms and check
4343
// whether any of them is an array.
44+
//
45+
// We assume TypeOf returns non-nil.
4446
if _, ok := info.TypeOf(e.X).Underlying().(*types.Array); ok {
4547
lvalue(e.X, escapes) // &a[i] on array
4648
}
4749
case *ast.SelectorExpr:
50+
// We assume TypeOf returns non-nil.
4851
if _, ok := info.TypeOf(e.X).Underlying().(*types.Struct); ok {
4952
lvalue(e.X, escapes) // &s.f on struct
5053
}

internal/typeparams/genericfeatures/features.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func ForPackage(inspect *inspector.Inspector, info *types.Info) Features {
8080
direct |= GenericFuncDecls
8181
}
8282
case *ast.InterfaceType:
83-
tv := info.Types[n]
83+
tv := info.Types[n] // may be zero
8484
if iface, _ := tv.Type.(*types.Interface); iface != nil && !iface.IsMethodSet() {
8585
direct |= EmbeddedTypeSets
8686
}

0 commit comments

Comments
 (0)