check

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package check is the type checker (sections 4 through 8 and 10 of the specification). It walks every module of a loaded program, including code nothing calls, and reports every rule the specification calls a compile error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallInfo

type CallInfo struct {
	Fn       types.Func
	Names    []string
	Defaults []bool
	Callee   *Object
	// Slots gives, for each argument in order, the index of the parameter it fills, or -1.
	Slots []int
	// Pipe is set when the call is the right side of a pipe, whose first argument is the left
	// side (section 5, Pipes).
	Pipe bool
	// Recv is set for a call through a dot, x.f(...), whose receiver x fills the first parameter
	// (section 5, Calls through .).
	Recv bool
}

CallInfo is what a call applied: the function type, the parameter names and which have defaults for a declared function, the callee when it is a named one, and which parameter each argument fills.

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

Checker holds the state of one check.

type Config

type Config struct {
	Namespaces []HostNamespace
	Removed    []string
	// Info, when set, is filled with what the checker learns (NewInfo).
	Info *Info
	// Prior is the number of errors reported before checking, by the loader: a syntax error
	// recovery read past, or an import that failed. What follows from one is not reported again.
	Prior int
}

Config adjusts the prelude a program sees, and asks for Info.

type Diagnostic

type Diagnostic = loader.Diagnostic

Diagnostic is one checker error, of the loader's shape.

func Program

func Program(prog *loader.Program, cfg Config) []Diagnostic

Program checks every module of prog and returns the errors sorted by position. A program whose root is a declaration file (section 10, Declaration files) has no module to check: the file's declarations join the prelude as a host namespace's do, and the errors are those of its signatures.

type HostNamespace

type HostNamespace struct {
	Name  string
	Decls *ast.File
}

HostNamespace is a host namespace's parsed signatures.

type Info

type Info struct {
	// Types holds the type of each expression as it was checked: for a variable, the type at
	// that use, narrowed (section 7). An expression checked more than once, as a lambda body may
	// be, keeps the last type.
	Types map[ast.Expr]types.Type
	// Objects maps each identifier to what it refers to.
	Objects map[*ast.Ident]*Object
	// Members maps each field access to the member or field it reads.
	Members map[*ast.Field]*Object
	// TypeNames maps each type name to what each part of its path refers to: a namespace or a
	// module, then the type or type parameter.
	TypeNames map[*ast.TypeName][]*Object
	// Decls maps each declaring node to its object: a *ast.FuncDecl, a *ast.TypeDecl, a
	// *ast.NameTarget, a *ast.Param, the *ast.Lambda whose x it is, a *ast.Import that binds a
	// namespace, or a *ast.ImportName.
	Decls map[ast.Node]*Object
	// Calls maps each call to the signature it applied.
	Calls map[*ast.Call]*CallInfo
	// Lambdas maps each lambda to the type of its x, or nil for a lambda of arity zero.
	Lambdas map[*ast.Lambda]types.Type
	// Entries maps each named entry of a record literal to the field it sets.
	Entries map[*ast.RecordEntry]*Object
	// Prelude lists the prelude's names, sorted: functions, types, and namespaces.
	Prelude []*Object
	// Modules maps each module's path to its names.
	Modules map[string]*ModuleInfo
	// contains filtered or unexported fields
}

Info is what the checker learned about a program beyond its errors, for an editor: the type of every expression, what every name refers to, and the signature every call applies. It is filled when Config.Info is set, and read once Program has returned.

func NewInfo

func NewInfo() *Info

NewInfo returns an empty Info for Config.

func (*Info) Namespace

func (in *Info) Namespace(t types.Type) *Object

Namespace is the namespace whose functions a call through a dot reads on a value of type t (section 5, Calls through .): a type, which holds the functions declared on it, a prelude namespace, or an interface; nil when t has none, as a number has, or its members share none.

func (*Info) TakesFirst added in v0.3.2

func (in *Info) TakesFirst(fn *Object, t types.Type) bool

TakesFirst reports whether a value of type t can be the first argument of the function fn, as a pipe or a call through a dot passes it (section 5): the value solves the type parameters the first parameter mentions as a call's inference would (section 4, Inference), each solution fits its bound, and the value fits the solved parameter. Type parameters only the other arguments solve are left open, as are bounds that name them.

type Kind

type Kind int

Kind says what an Object is.

const (
	Var       Kind = iota // a variable, top-level or local, or a lambda's x
	Param                 // a parameter
	Func                  // a function, declared or of a namespace or type
	TypeKind              // a declared type
	TypeParam             // a type parameter
	Namespace             // a prelude or host namespace
	Module                // an imported module
	Const                 // a namespace constant
	Field                 // a field of a record type
)

func (Kind) String

func (k Kind) String() string

type ModuleInfo

type ModuleInfo struct {
	Path    string
	Names   []*Object
	Decls   []*Object
	Exports []*Object
}

ModuleInfo lists a module's top-level names, its own declarations among them, and its exports.

type Object

type Object struct {
	Name string
	Kind Kind
	// Decl is the node that declares it: a *ast.FuncDecl, *ast.TypeDecl, *ast.VarDecl of the
	// prelude or a module, a *ast.NameTarget, a *ast.Param, a *ast.FieldType, a *ast.Lambda for
	// its x, or nil for a namespace with no declaration, a type parameter, or the field of an
	// anonymous record type.
	Decl ast.Node
	// Path is the module the declaration is in: "prelude.d.ant" for the prelude, the host
	// namespace's name for a host declaration, or "" when there is no declaration.
	Path     string
	Exported bool
	// Owner names the type or namespace a function, type, or constant belongs to, or "".
	Owner string
	// Fixed is set for a variable that cannot be reassigned: a let, a parameter, a lambda's x, a
	// for variable, or a constant (section 3).
	Fixed bool
	// contains filtered or unexported fields
}

Object is a named thing: what an identifier, a member, or a type name refers to.

func (*Object) Defaults

func (o *Object) Defaults() []bool

Defaults reports, for a declared function, which parameters have a default.

func (*Object) Doc

func (o *Object) Doc() string

Doc is the declaration's doc comment or a namespace's section label, or "".

func (*Object) Members

func (o *Object) Members() []*Object

Members lists what a namespace, a module, or a type holds: its functions, types, and constants, and for a record type its fields, sorted by name. Only the exports of a module.

func (*Object) Sig

func (o *Object) Sig() string

Sig is the declaration's signature as written (doc.md section 1), or "".

func (*Object) Type

func (o *Object) Type() types.Type

Type is the object's type: a variable's or parameter's declared type, a function's type with its return type inferred, a type's own, a type parameter's, a constant's, or a field's; nil for a namespace or a module.

func (*Object) TypeDecl

func (o *Object) TypeDecl() *types.Decl

TypeDecl is the declaration of a type, with its fields and kind, or nil.

func (*Object) TypeParams

func (o *Object) TypeParams() []*types.ParamDecl

TypeParams lists a generic function's or type's type parameters, or nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL