Skip to content

Commit

Permalink
all: add slides and sametype code
Browse files Browse the repository at this point in the history
  • Loading branch information
matloob committed May 16, 2019
1 parent 0d60d3d commit 61acae2
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
Binary file added presentations/singapore.key
Binary file not shown.
Binary file added presentations/tokyo.key
Binary file not shown.
85 changes: 85 additions & 0 deletions sametype/sametype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package sametype

import (
"go/ast"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
"golang.org/x/tools/go/types/typeutil"
)

var Analyzer = &analysis.Analyzer {
Name: "cmpequal",
Doc: "Check arg types of cmp.Equal",
Requires:
[]*analysis.Analyzer{inspect.Analyzer},
FactTypes: []analysis.Fact{(*SameType)(nil)},
Run: run,

}

type SameType struct{}

func (s *SameType) AFact() {}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
checkForFact := func(n ast.Node) {
call := n.(*ast.CallExpr)
fn, _ := typeutil.Callee(pass.TypesInfo, call).(*types.Func)
if fn == nil {
return // not a function call
}

var sameType SameType
if !pass.ImportObjectFact(fn, &sameType) {
return
}

typ0 := pass.TypesInfo.Types[call.Args[0]].Type
typ1 := pass.TypesInfo.Types[call.Args[1]].Type
if !types.Identical(typ0, typ1) {
pass.Reportf(call.Pos(),
"Calls to %v must have arguments of the same type; "+
"is called with %v and %v",
fn.Name(), typ0, typ1)
}}
maybeAddFact := func(n ast.Node, push bool, stack []ast.Node) bool {
if !push {
return true
}

call := n.(*ast.CallExpr)
fn, _ := typeutil.Callee(pass.TypesInfo, call).(*types.Func)
if fn == nil {
return false // not a function call
}

if fn.FullName() != "annotate.SameType" {
return false // not an annotation
}

var enclosingFunc *ast.FuncDecl
for _, node := range stack {
if v, ok := node.(*ast.FuncDecl); ok {
enclosingFunc = v
break
}
}
if enclosingFunc == nil {
return false // we didn't find the enclosing call
} else if len(enclosingFunc.Type.Params.List) != 2 {
pass.Reportf(call.Pos(), "SameType annotation can only be added to funcs with two arguments")
}

obj := pass.TypesInfo.Defs[enclosingFunc.Name]
pass.ExportObjectFact(obj, &SameType{})
return false
}
inspect.WithStack([]ast.Node{(*ast.CallExpr)(nil)}, maybeAddFact)
inspect.Preorder(
[]ast.Node{(*ast.CallExpr)(nil)},
checkForFact)
return nil, nil
}
17 changes: 17 additions & 0 deletions sametype/sametype_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package sametype_test

import (
"testing"

"github.com/matloob/analysistalk/sametype"
"golang.org/x/tools/go/analysis/analysistest"
)

func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, sametype.Analyzer, "a")
}
16 changes: 16 additions & 0 deletions sametype/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package a

import (
"fmt"
"testing"

"mycmp"
)

func TestSomething(t *testing.T) {
var x int = 0
var y int = 0
if !mycmp.Equal(x, &y) { // want "Calls to Equal must have arguments of the same type"
fmt.Println("but they're not equal!")
}
}
4 changes: 4 additions & 0 deletions sametype/testdata/src/annotate/sametype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package annotate

func SameType() {
}
11 changes: 11 additions & 0 deletions sametype/testdata/src/mycmp/equal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mycmp

import (
"annotate"
"reflect"
)

func Equal(a, b interface{}) bool {
annotate.SameType()
return reflect.DeepEqual(a, b)
}

0 comments on commit 61acae2

Please sign in to comment.