Separate language-server and SSA tooling
Separate language-server and SSA tooling
A language server such as gopls answers editor requests for diagnostics, navigation and refactoring using workspace and unsaved-buffer state. An SSA representation (static single assignment) models computations and control flow for analysis, with values assigned once in that representation; merge values represent control-flow joins. The x/tools/go/ssa representation is a source-analysis IR, not the compiler’s optimized machine-oriented SSA. Neither a source walk nor a language server guarantees all possible runtime behavior.
package main
import ( "fmt" "unicode/utf16" "unicode/utf8")
func main() { line := "a😀b" prefix := line[:len("a😀")] fmt.Println(len(prefix), utf8.RuneCountInString(prefix), len(utf16.Encode([]rune(prefix))))}The offset before b is five UTF-8 bytes, two code points and three UTF-16 code units. An editor using UTF-16 positions needs the last count and a zero-based line index. Use the negotiated position encoding, not an unconditional conversion. Validate snapshot version and cancellation before returning results from asynchronous analysis. This is a conversion demonstration, not an LSP implementation.
Expected output:
5 2 3Distinguish branch returns from a join
package main
import ( "fmt")
func choose(flag bool) int { left, right := 10, 20 if flag { return left } return right}func main() { fmt.Println(choose(true), choose(false))}A conceptual IR can name the branch values v1 and v2, then form a merge value at a join if both paths continue to a common block. Actual SSA construction depends on control flow; this function returns directly from two paths, so do not assume it has a phi instruction. For real SSA, load typed packages with packages.Load in LoadSyntax or LoadAllSyntax mode, check package errors, call ssautil.Packages, then Program.Build before inspecting function blocks. For navigation and refactoring, gopls uses workspace-aware semantic information. No external SSA or gopls process is executed in this chapter.
Expected output:
10 20External integration sketch: run this driver with its working directory set to a separate module containing a package-level choose function. It reports package paths and whether choose has blocks; exact block counts are not a stable contract. Pin compatible x/tools and Go versions. This snippet is formatted but not compiled or executed here.
package main
import ( "fmt" "golang.org/x/tools/go/packages" "golang.org/x/tools/go/ssa/ssautil")
func main() { ps, err := packages.Load(&packages.Config{Mode: packages.LoadSyntax}, ".") if err != nil { panic(err) } if packages.PrintErrors(ps) > 0 { panic("package errors") } prog, ss := ssautil.Packages(ps, 0) prog.Build() for _, p := range ss { if p != nil { fmt.Println(p.Pkg.Path()) if f := p.Func("choose"); f != nil { fmt.Println(len(f.Blocks) > 0) } } }}Try it locally
For é😀x, print the UTF-8 byte count and UTF-16 code-unit count before x. Use the exact prefix, not a byte slice through a character.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Developer tooling 08: Separate language-server and SSA tooling
Check the lesson objective before moving on.
Why should an editor reject a stale diagnostic result?
How does x/tools/go/ssa differ from compiler SSA?