|
| 1 | +package typecheck |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + "go/types" |
| 7 | + |
| 8 | + "golang.org/x/tools/go/analysis" |
| 9 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 10 | + "golang.org/x/tools/go/ast/inspector" |
| 11 | + "golang.org/x/tools/go/types/typeutil" |
| 12 | +) |
| 13 | + |
| 14 | +var Analyzer = &analysis.Analyzer{ |
| 15 | + Name: "bigslice_typecheck", |
| 16 | + Doc: `check bigslice func call arguments |
| 17 | +
|
| 18 | +Basic typechecker for bigslice programs that inspects session.Run and |
| 19 | +session.Must calls to ensure the arguments are compatible with the Func. |
| 20 | +Checks are limited by static analysis and are best-effort. For example, the call |
| 21 | + session.Must(ctx, chooseFunc(), args...) |
| 22 | +cannot be checked, because it uses chooseFunc() instead of a simple identifier. |
| 23 | +
|
| 24 | +Typechecking does not include any slice operations yet.`, |
| 25 | + Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 26 | + Run: run, |
| 27 | +} |
| 28 | + |
| 29 | +const ( |
| 30 | + funcFullName = "github.com/grailbio/bigslice.Func" |
| 31 | + execMustFullName = "(*github.com/grailbio/bigslice/exec.Session).Must" |
| 32 | + execRunFullName = "(*github.com/grailbio/bigslice/exec.Session).Run" |
| 33 | +) |
| 34 | + |
| 35 | +func run(pass *analysis.Pass) (interface{}, error) { |
| 36 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 37 | + |
| 38 | + // funcTypes describes the types of declared bigslice.FuncValues. |
| 39 | + // TODO: As stated below, we're only recording top-level func for now. |
| 40 | + // If that changes, this map should be keyed by a global identifier, not *ast.Ident. |
| 41 | + // TODO: We may also want to return these types as Facts to allow checking across packages. |
| 42 | + funcTypes := map[string]*types.Signature{} |
| 43 | + |
| 44 | + // Collect the types of bigslice.Funcs. |
| 45 | + // TODO: ValueSpec captures top-level vars, but maybe we should include non-top-level ones, too. |
| 46 | + inspect.Preorder([]ast.Node{&ast.ValueSpec{}}, func(node ast.Node) { |
| 47 | + valueSpec := node.(*ast.ValueSpec) |
| 48 | + for i := range valueSpec.Values { |
| 49 | + call, ok := valueSpec.Values[i].(*ast.CallExpr) |
| 50 | + if !ok { |
| 51 | + continue |
| 52 | + } |
| 53 | + fn := typeutil.StaticCallee(pass.TypesInfo, call) |
| 54 | + if fn == nil { |
| 55 | + continue |
| 56 | + } |
| 57 | + if fn.FullName() != funcFullName { |
| 58 | + continue |
| 59 | + } |
| 60 | + if len(call.Args) != 1 { |
| 61 | + panic(fmt.Errorf("unexpected arguments to bigslice.Func: %v", call.Args)) |
| 62 | + } |
| 63 | + funcType := pass.TypesInfo.TypeOf(call.Args[0]).Underlying().(*types.Signature) |
| 64 | + funcTypes[valueSpec.Names[i].Name] = funcType |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + inspect.Preorder([]ast.Node{&ast.CallExpr{}}, func(node ast.Node) { |
| 69 | + call := node.(*ast.CallExpr) |
| 70 | + fn := typeutil.StaticCallee(pass.TypesInfo, call) |
| 71 | + if fn == nil { |
| 72 | + return |
| 73 | + } |
| 74 | + if name := fn.FullName(); name != execRunFullName && name != execMustFullName { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + funcValueIdent, ok := call.Args[1].(*ast.Ident) |
| 79 | + if !ok { |
| 80 | + // This function invocation is more complicated than a simple identifier. |
| 81 | + // Give up on typechecking this call. |
| 82 | + return |
| 83 | + } |
| 84 | + funcType, ok := funcTypes[funcValueIdent.Name] |
| 85 | + if !ok { |
| 86 | + // TODO: Propagate bigslice.Func types as Facts so we can do a better job here. |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + wantArgTypes := funcType.Params() |
| 91 | + gotArgs := call.Args[2:] |
| 92 | + if want, got := wantArgTypes.Len(), len(gotArgs); want != got { |
| 93 | + pass.ReportRangef(funcValueIdent, |
| 94 | + "bigslice type error: %s requires %d arguments, but got %d", |
| 95 | + funcValueIdent.Name, want, got) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + for i, gotArg := range gotArgs { |
| 100 | + wantType := wantArgTypes.At(i).Type() |
| 101 | + gotType := pass.TypesInfo.TypeOf(gotArg) |
| 102 | + if !types.AssignableTo(gotType, wantType) { |
| 103 | + pass.ReportRangef(gotArg, |
| 104 | + "bigslice type error: func %q argument %q [%d] requires %v, but got %v", |
| 105 | + funcValueIdent.Name, wantArgTypes.At(i).Name(), i, wantType, gotType) |
| 106 | + } |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + return nil, nil |
| 111 | +} |
0 commit comments