Skip to content

Commit

Permalink
Allow go statements in certain circumstances
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 24, 2024
1 parent 9fefde4 commit af67c21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (c *compiler) compilePackage(p *packages.Package, colors functionColors) er
compiled := false
if color != nil || containsColoredFuncLit(decl, colorsByFunc) {
// Reject certain language features for now.
if err := unsupported(decl, p.TypesInfo); err != nil {
if err := c.unsupported(decl, p.TypesInfo, colorsByFunc); err != nil {
return err
}
scope := &scope{compiler: c, colors: colorsByFunc}
Expand Down Expand Up @@ -433,7 +433,7 @@ func (c *compiler) compilePackage(p *packages.Package, colors functionColors) er
return nil
}

func containsColoredFuncLit(decl *ast.FuncDecl, colorsByFunc map[ast.Node]*types.Signature) (yes bool) {
func containsColoredFuncLit(decl ast.Node, colorsByFunc map[ast.Node]*types.Signature) (yes bool) {
ast.Inspect(decl, func(n ast.Node) bool {
if lit, ok := n.(*ast.FuncLit); ok {
if _, ok := colorsByFunc[lit]; ok {
Expand Down
22 changes: 20 additions & 2 deletions compiler/unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,35 @@ import (
"go/ast"
"go/token"
"go/types"
"log"
)

// unsupported checks a function for unsupported language features.
func unsupported(decl ast.Node, info *types.Info) (err error) {
func (c *compiler) unsupported(decl ast.Node, info *types.Info, colorsByFunc map[ast.Node]*types.Signature) (err error) {
ast.Inspect(decl, func(node ast.Node) bool {
switch nn := node.(type) {
case ast.Stmt:
switch n := nn.(type) {
// Not yet supported:
case *ast.GoStmt:
err = fmt.Errorf("not implemented: go")
if _, inColoredFunc := colorsByFunc[decl]; inColoredFunc {
err = fmt.Errorf("not implemented: go")
} else {
// Allow go statement in certain limited circumstances.
_, goColoredFunc := colorsByFunc[n.Call.Fun]
if !goColoredFunc {
switch fn := n.Call.Fun.(type) {
case *ast.FuncLit:
goColoredFunc = containsColoredFuncLit(fn, colorsByFunc)
}
}
if goColoredFunc {
err = fmt.Errorf("not implemented: go")
} else {
pos := c.fset.Position(n.Pos())
log.Printf("warning: goroutine mutations at %s may not be durable", pos)
}
}

// Partially supported:
case *ast.BranchStmt:
Expand Down

0 comments on commit af67c21

Please sign in to comment.