// labels checks correct label use in body. func (check *Checker) labels(body *ast.BlockStmt) { // set of all labels in this body all := NewScope(nil, body.Pos(), body.End(), "label") fwdJumps := check.blockBranches(all, nil, nil, body.List) // If there are any forward jumps left, no label was found for // the corresponding goto statements. Either those labels were // never defined, or they are inside blocks and not reachable // for the respective gotos. for _, jmp := range fwdJumps { var msg string name := jmp.Label.Name if alt := all.Lookup(name); alt != nil { msg = "goto %s jumps into block" alt.(*Label).used = true // avoid another error } else { msg = "label %s not declared" } check.errorf(jmp.Label.Pos(), msg, name) } // spec: "It is illegal to define a label that is never used." for _, obj := range all.elems { if lbl := obj.(*Label); !lbl.used { check.softErrorf(lbl.pos, "label %s declared but not used", lbl.name) } } }
// lastComment returns the last comment inside the provided block. func lastComment(b *ast.BlockStmt, c []*ast.CommentGroup) (i int, last *ast.CommentGroup) { pos, end := b.Pos(), b.End() for j, cg := range c { if cg.Pos() < pos { continue } if cg.End() > end { break } i, last = j, cg } return }
// bodySize is like nodeSize but it is specialized for *ast.BlockStmt's. func (p *printer) bodySize(b *ast.BlockStmt, maxSize int) int { pos1 := b.Pos() pos2 := b.Rbrace if pos1.IsValid() && pos2.IsValid() && p.lineFor(pos1) != p.lineFor(pos2) { // opening and closing brace are on different lines - don't make it a one-liner return maxSize + 1 } if len(b.List) > 5 { // too many statements - don't make it a one-liner return maxSize + 1 } // otherwise, estimate body size bodySize := p.commentSizeBefore(p.posFor(pos2)) for i, s := range b.List { if bodySize > maxSize { break // no need to continue } if i > 0 { bodySize += 2 // space for a semicolon and blank } bodySize += p.nodeSize(s, maxSize) } return bodySize }
func (check *Checker) funcBody(decl *declInfo, name string, sig *Signature, body *ast.BlockStmt) { if trace { if name == "" { name = "<function literal>" } fmt.Printf("--- %s: %s {\n", name, sig) defer fmt.Println("--- <end>") } // set function scope extent sig.scope.pos = body.Pos() sig.scope.end = body.End() // We need to clone the scope here so that, when changing the type of a // parameter inside the function body (e. g.. when doing `if x != nil { return }` // for optionals), we don't mutate the *Var in its fixed scope, which // would affect further calls to the same function! scope := NewScope(sig.scope, sig.scope.pos, sig.scope.end, sig.scope.comment) for _, v := range sig.scope.elems { scope.Insert(v) } if sig.params != nil { for _, param := range sig.params.vars { if _, ok := scope.elems[param.name]; ok { delete(scope.elems, param.name) } scope.Insert(NewParam(param.pos, param.pkg, param.name, param.typ)) } } if sig.results != nil { for _, param := range sig.results.vars { if _, ok := scope.elems[param.name]; ok { delete(scope.elems, param.name) } scope.Insert(NewParam(param.pos, param.pkg, param.name, param.typ)) } } if sig.recv != nil { if _, ok := scope.elems[sig.recv.name]; ok { delete(scope.elems, sig.recv.name) } scope.Insert(NewParam(sig.recv.pos, sig.recv.pkg, sig.recv.name, sig.recv.typ)) } // save/restore current context and setup function context // (and use 0 indentation at function start) defer func(ctxt context, indent int) { check.context = ctxt check.indent = indent }(check.context, check.indent) check.context = context{ decl: decl, scope: scope, sig: sig, } check.indent = 0 check.stmtList(0, body.List) if check.hasLabel { check.labels(body) } if sig.results.Len() > 0 && !check.isTerminating(body, "") { check.error(body.Rbrace, "missing return") } // spec: "Implementation restriction: A compiler may make it illegal to // declare a variable inside a function body if the variable is never used." // (One could check each scope after use, but that distributes this check // over several places because CloseScope is not always called explicitly.) check.usage(sig.scope) }
// playExample synthesizes a new *ast.File based on the provided // file with the provided function body as the body of main. func playExample(file *ast.File, body *ast.BlockStmt) *ast.File { if !strings.HasSuffix(file.Name.Name, "_test") { // We don't support examples that are part of the // greater package (yet). return nil } // Find top-level declarations in the file. topDecls := make(map[*ast.Object]bool) for _, decl := range file.Decls { switch d := decl.(type) { case *ast.FuncDecl: topDecls[d.Name.Obj] = true case *ast.GenDecl: for _, spec := range d.Specs { switch s := spec.(type) { case *ast.TypeSpec: topDecls[s.Name.Obj] = true case *ast.ValueSpec: for _, id := range s.Names.List { topDecls[id.Obj] = true } } } } } // Find unresolved identifiers and uses of top-level declarations. unresolved := make(map[string]bool) usesTopDecl := false var inspectFunc func(ast.Node) bool inspectFunc = func(n ast.Node) bool { // For selector expressions, only inspect the left hand side. // (For an expression like fmt.Println, only add "fmt" to the // set of unresolved names, not "Println".) if e, ok := n.(*ast.SelectorExpr); ok { ast.Inspect(e.X, inspectFunc) return false } // For key value expressions, only inspect the value // as the key should be resolved by the type of the // composite literal. if e, ok := n.(*ast.KeyValueExpr); ok { ast.Inspect(e.Value, inspectFunc) return false } if id, ok := n.(*ast.Ident); ok { if id.Obj == nil { unresolved[id.Name] = true } else if topDecls[id.Obj] { usesTopDecl = true } } return true } ast.Inspect(body, inspectFunc) if usesTopDecl { // We don't support examples that are not self-contained (yet). return nil } // Remove predeclared identifiers from unresolved list. for n := range unresolved { if predeclaredTypes[n] || predeclaredConstants[n] || predeclaredFuncs[n] { delete(unresolved, n) } } // Use unresolved identifiers to determine the imports used by this // example. The heuristic assumes package names match base import // paths for imports w/o renames (should be good enough most of the time). namedImports := make(map[string]string) // [name]path var blankImports []ast.Spec // _ imports for _, s := range file.Imports { p, err := strconv.Unquote(s.Path.Value) if err != nil { continue } n := path.Base(p) if s.Name != nil { n = s.Name.Name switch n { case "_": blankImports = append(blankImports, s) continue case ".": // We can't resolve dot imports (yet). return nil } } if unresolved[n] { namedImports[n] = p delete(unresolved, n) } } // If there are other unresolved identifiers, give up because this // synthesized file is not going to build. if len(unresolved) > 0 { return nil } // Include documentation belonging to blank imports. var comments []*ast.CommentGroup for _, s := range blankImports { if c := s.(*ast.ImportSpec).Doc; c != nil { comments = append(comments, c) } } // Include comments that are inside the function body. for _, c := range file.Comments { if body.Pos() <= c.Pos() && c.End() <= body.End() { comments = append(comments, c) } } // Strip the "Output:" or "Unordered output:" comment and adjust body // end position. body, comments = stripOutputComment(body, comments) // Synthesize import declaration. importDecl := &ast.GenDecl{ Tok: token.IMPORT, Lparen: 1, // Need non-zero Lparen and Rparen so that printer Rparen: 1, // treats this as a factored import. } for n, p := range namedImports { s := &ast.ImportSpec{Path: &ast.BasicLit{Value: strconv.Quote(p)}} if path.Base(p) != n { s.Name = ast.NewIdent(n) } importDecl.Specs = append(importDecl.Specs, s) } importDecl.Specs = append(importDecl.Specs, blankImports...) // Synthesize main function. funcDecl := &ast.FuncDecl{ Name: ast.NewIdent("main"), Type: &ast.FuncType{Params: &ast.FieldList{}}, // FuncType.Params must be non-nil Body: body, } // Synthesize file. return &ast.File{ Name: ast.NewIdent("main"), Decls: []ast.Decl{importDecl, funcDecl}, Comments: comments, } }