Skip to content

Commit

Permalink
remove generating logs for slow slice types
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Feb 4, 2025
1 parent e03a830 commit 874f74a
Showing 1 changed file with 2 additions and 26 deletions.
28 changes: 2 additions & 26 deletions go/tools/astfmtgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (r *Rewriter) rewriteAstPrintf(cursor *astutil.Cursor, expr *ast.CallExpr)
Args: []ast.Expr{&ast.BasicLit{Value: `"%d"`, Kind: gotoken.STRING}, expr.Args[2+fieldnum]},
}
cursor.InsertBefore(r.rewriteLiteral(callexpr.X, "WriteString", call))
case 'n': // new directive for slices of AST nodes checked at code generation time
case 'n': // directive for slices of AST nodes checked at code generation time
inputExpr := expr.Args[2+fieldnum]
inputType := r.pkg.TypesInfo.Types[inputExpr].Type
sliceType, ok := inputType.(*types.Slice)
Expand All @@ -266,31 +266,7 @@ func (r *Rewriter) rewriteAstPrintf(cursor *astutil.Cursor, expr *ast.CallExpr)
cursor.InsertBefore(&ast.ExprStmt{X: call})
break
}
log.Printf("slow path for `n` directive with type %T", types.TypeString(inputType, noQualifier))
// Slow path: slice elements do not implement Expr
cursor.InsertBefore(&ast.ExprStmt{
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: "log"},
Sel: &ast.Ident{Name: "Printf"},
},
Args: []ast.Expr{
&ast.BasicLit{
Kind: gotoken.STRING,
Value: strconv.Quote("slow path for %n with type %T"),
},
inputExpr,
},
},
})
call := &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: callexpr.X,
Sel: &ast.Ident{Name: "formatNodes"},
},
Args: []ast.Expr{inputExpr},
}
cursor.InsertBefore(&ast.ExprStmt{X: call})
panic("slow path for `n` directive for slice of type other than Expr")
default:
panic(fmt.Sprintf("unsupported escape %q", token))
}
Expand Down

0 comments on commit 874f74a

Please sign in to comment.