Documentation
¶
Overview ¶
Package diag provides diagnostic reporting for the type checker.
Diagnostics are categorized by severity:
- Error: Type errors that prevent correctness (mismatches, undefined vars)
- Warning: Suspicious patterns that may indicate bugs (unreachable code)
- Hint: Suggestions for improvement (style, optimization)
Each diagnostic includes position, code, message, and severity. Diagnostics render in Rust-style format with source context and optional ANSI colors.
The Collector accumulates diagnostics during type checking with automatic deduplication. It supports snapshot/restore via Truncate for speculative checking where diagnostics may need to be rolled back.
Usage:
collector := diag.NewCollector("example.lua")
collector.Add(node, diag.ErrTypeMismatch, "expected %s, got %s", want, got)
if collector.HasErrors() {
source := diag.ParseSource(sourceCode)
fmt.Print(collector.RenderAllColored(source))
}
Index ¶
- func ContextualHelp(code Code, format, _ string) (explanation, help string)
- type Code
- type CodeInfo
- type Collector
- func (c *Collector) Add(node PositionHolder, code Code, format string, args ...any)
- func (c *Collector) AddHint(node PositionHolder, code Code, format string, args ...any)
- func (c *Collector) AddWarning(node PositionHolder, code Code, format string, args ...any)
- func (c *Collector) All() []Diagnostic
- func (c *Collector) Clear()
- func (c *Collector) HasErrors() bool
- func (c *Collector) HasWarnings() bool
- func (c *Collector) Len() int
- func (c *Collector) RenderAll(source SourceLines) string
- func (c *Collector) RenderAllColored(source SourceLines) string
- func (c *Collector) Source() string
- func (c *Collector) Truncate(count int)
- type Diagnostic
- type Label
- type Position
- type PositionHolder
- type Severity
- type SourceLines
- type Span
- type SpanHolder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextualHelp ¶
ContextualHelp returns explanation and help text based on error patterns.
Types ¶
type Code ¶
type Code int
Code categorizes diagnostics by error type.
Each code has associated metadata (title, explanation) in codeInfos. Codes are formatted as E0000-E9999 for display.
const ( ErrTypeMismatch Code = iota ErrUndefined ErrNotCallable ErrWrongArity ErrNoField ErrNotIndexable ErrNoSynthesizer ErrNoHandler ErrReadonly ErrUnreachable ErrMissingReturn ErrDepthLimit ErrUnresolvedTypeRef ErrCircularReference ErrNonExhaustive ErrUseBeforeAssign ErrPreconditionViolation ErrDuplicateDeclaration ErrInvalidIndexType ErrInvalidOperand ErrUnsatConstraint ErrOptionalCall ErrNoMethod )
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector accumulates diagnostics during type checking. Thread-safe and supports deduplication by position and format string.
func NewCollector ¶
NewCollector creates a collector for the given source file.
func (*Collector) Add ¶
func (c *Collector) Add(node PositionHolder, code Code, format string, args ...any)
Add records an error diagnostic.
func (*Collector) AddHint ¶
func (c *Collector) AddHint(node PositionHolder, code Code, format string, args ...any)
AddHint records a hint diagnostic.
func (*Collector) AddWarning ¶
func (c *Collector) AddWarning(node PositionHolder, code Code, format string, args ...any)
AddWarning records a warning diagnostic.
func (*Collector) All ¶
func (c *Collector) All() []Diagnostic
All returns a copy of all diagnostics.
func (*Collector) HasWarnings ¶
HasWarnings returns true if any warning-severity diagnostics exist.
func (*Collector) RenderAll ¶
func (c *Collector) RenderAll(source SourceLines) string
RenderAll formats all diagnostics without colors.
func (*Collector) RenderAllColored ¶
func (c *Collector) RenderAllColored(source SourceLines) string
RenderAllColored formats all diagnostics with colors.
type Diagnostic ¶
type Diagnostic struct {
Position Position
Span Span
Code Code
Message string
Format string
Severity Severity
Explanation string
Help string
Labels []Label
}
Diagnostic represents a type error, warning, or hint.
A Diagnostic contains all information needed to display a rich error message:
- Position/Span: Where the error occurred for source highlighting
- Code: Categorizes the error for filtering and documentation lookup
- Message: The primary error description (formatted from Format + args)
- Explanation: Detailed description of why this is an error
- Help: Suggested fix or workaround
- Labels: Secondary source locations with annotations
Diagnostics implement the error interface for use in error handling.
func (Diagnostic) Render ¶
func (d Diagnostic) Render(source SourceLines) string
Render formats the diagnostic in Rust-style without colors.
The output format includes:
- Severity and error code header
- Source location with file:line:column
- Source line with underline highlighting
- Secondary labels (if any)
- Explanation note (if set)
- Help suggestion (if set)
func (Diagnostic) RenderColored ¶
func (d Diagnostic) RenderColored(source SourceLines) string
RenderColored formats the diagnostic in Rust-style with ANSI colors.
Uses ANSI escape codes for terminal color output:
- Red for errors
- Yellow for warnings
- Cyan for hints
- Blue for line numbers and markers
func (Diagnostic) String ¶
func (d Diagnostic) String() string
type Label ¶
Label marks a secondary source location with an annotation message.
Labels provide additional context for diagnostics, such as pointing to the declaration when an error involves both use and definition sites.
type Position ¶
Position identifies a location in source code.
Line and Column are 1-indexed to match editor conventions. File is the source file path for multi-file diagnostics.
type PositionHolder ¶
type PositionHolder interface {
Line() int
}
PositionHolder is implemented by AST nodes that have line information.
type Severity ¶
type Severity int
Severity indicates the importance level of a diagnostic.
Severity levels affect compilation behavior and IDE presentation:
- Error: Type errors that indicate likely runtime failures. May block execution.
- Warning: Suspicious patterns that may indicate bugs but are not type errors.
- Hint: Suggestions for improvement that do not indicate problems.
type SourceLines ¶
type SourceLines []string
SourceLines holds source code split by lines for rendering diagnostics.
Lines are 0-indexed internally, but GetLine accepts 1-indexed line numbers to match Position.Line convention.
func ParseSource ¶
func ParseSource(source string) SourceLines
ParseSource splits source code into lines for diagnostic rendering.
func (SourceLines) GetLine ¶
func (s SourceLines) GetLine(n int) string
GetLine returns the 1-indexed line, or empty string if out of bounds.
type Span ¶
Span defines a range in source code for highlighting.
All fields are 1-indexed. EndLine and EndCol may be 0 to indicate a point span (single character) or unknown extent.
func (Span) SingleLine ¶
SingleLine returns true if the span does not cross line boundaries.
type SpanHolder ¶
type SpanHolder interface {
PositionHolder
Column() int
LastLine() int
LastColumn() int
}
SpanHolder extends PositionHolder with full range information.