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