lang::module
lang/src/module.mo
Module loading infrastructure for the self-hosted compiler. Parses source text and builds scope data from declarations.
pub def prelude_module_path : ModulePathModule path for the prelude
pub def init_module_path : ModulePathpub def std_module_path : ModulePathpub def parse_all_decls (input : String) : ParseResult (List Decl)Parse all declarations from source text.
Uses decls_parser_located, so every term carries its source position
as a Term.ctx wrapper -- on EVERY path, not just compile --debug.
This is the one production parse site (load_module_decls/
parse_module reach it, and through them check, test, compile
and pretty), so locating here is what gives the whole compiler a
single term shape. It used to use the plain decls_parser, and
compile --debug then RE-READ and RE-PARSED the entire dependency
graph through with_located_decls to add the wrappers afterwards.
Two consequences, both bad:
- two parses on the default path, one of them thrown away;
- and, worse, a tree shape that only
compile --debugever saw.Term.ctxis supposed to be semantically transparent (see its own doc comment,lang/types.mo), but the ~180 sites that match on term SHAPE only actually stay transparent if something exercises them. Nothing did: CI compiles with--release, andcheck/testnever built a wrapper at all.infer_carrier_type(lang/scope.mo) had noTerm.ctxarm, so under--debugevery++on a String failed to resolve itsAppendinstance andmonad compile cli/src/main.mo-- the DEFAULT invocation -- died atno instance found forAppend.append``. With one shape, the 1467-test corpus exercises wrapper transparency continuously, and--release/--debuggoes back to meaning what it should: whether DWARF is EMITTED, not what the compiler decides.
Runs the result through expand_decls (macro expansion,
lang.typecheck.macro_queue) before returning — this is the real
pipeline's own LENIENT parse site (feeds build_scope_from_decls
via load_module_decls/parse_module),
one of the two sites the macro-expansion plan calls out by name;
its strict twin is try_parse_decls_strict below. ParseResult's
own remaining is untouched -- only the parsed payload changes.
(try_parse_decls_strict is still on the PLAIN parser: it only feeds
rendered parse-error diagnostics on a cold path, so it has no reason
to build wrappers. Locating it would be harmless, not useful.)
pub def parse_all_decls_cfg (env : CfgEnv) (input : String) : ParseResult (List Decl)parse_all_decls under a feature set (lang/src/cfg.mo): declarations
whose #[cfg] is false under env are dropped before expansion.
pub def cfg_env_for_file (file_path : String) (content : String) : IO CfgEnvThe #[cfg] feature set for file_path: its mote's manifest
[features] (default included) plus MONAD_FEATURES
(mote/feature,...). A file with no #[cfg skips the manifest walk.
Mirrors filter_cfg_feature_decls (rust/core/src/term/module.rs).
pub def try_parse_decls (input : String) : Option (List Decl)Parse source text, returning the parsed declarations or none on parse error.
pub def try_parse_decls_strict (input : String) (path : Option String) : Result String (List Decl)A try_parse_decls twin built on decls_parser_strict instead of
the lenient decls_parser — where try_parse_decls silently returns
Option.none on ANY failure (indistinguishable from "the file is
just empty" and, thanks to decls_parser's own leniency, in
practice almost never even reached — see decls_parser_strict's doc
comment, lang/parser.mo), this surfaces a real, rendered,
Rust-diag.rs-style diagnostic on a genuine parse failure. path is
threaded through only for the --> path:L:C line — pass
Option.none if unknown.
The macro-expansion plan's own STRICT parse site — feeds the real
per-decl typecheck walk (check_file/check_file_cached via
check_module_with_scope), so expand_decls runs here too, same
as parse_all_decls above.
pub def try_parse_decls_strict_diag (input : String) : Result ParseError (List Decl)try_parse_decls_strict without the rendering: the same strict parse
and macro expansion, but the raw ParseError on failure. A caller that
must distinguish "the input ended mid-construct" from "the input is
wrong" needs the error's remaining text, which the printed diagnostic
no longer carries (the REPL's continuation signal; see
parse_error_at_eof).
pub def parse_error_at_eof (e : ParseError) : BoolDid this parse failure happen at the end of the input? The strict
parser reports the text it could not consume; only whitespace left
means the construct was cut off rather than malformed. A failure with
real text remaining (1 +* 2) is a genuine error, not a continuation.
pub def show_parse_error (input : String) (path : Option String) (e : ParseError) : StringRender a strict-parse failure the way try_parse_decls_strict does.
pub type DeclKind {
dk_def,
dk_type,
dk_struct,
dk_class,
dk_instance,
dk_infix,
dk_use,
dk_open,
dk_defmacro,
dk_macro,
dk_mote,
}One top-level declaration's extent, with enough identity to be useful as an outline entry.
The range's own path is Option.none: the parser is handed text, not
a filename. A consumer that knows the file -- a language server has the
URI -- fills it in.
A declaration's kind for an outline. An enum, not a spelling: the
wire vocabulary lives in decl_kind_text, so a consumer that wants
the text renders it once instead of matching strings.
pub def decl_kind_text (k : DeclKind) : StringThe Rust reference's own kind vocabulary ("def", "type", "struct", "class", "instance"), so the wire output reads like the tool being replaced rather than like this compiler's internals.
pub struct DeclRange {
name : NamePath,
kind : DeclKind,
range : SourceRange,
}pub def decl_range_name (dr : DeclRange) : NamePathpub def decl_range_kind (dr : DeclRange) : DeclKindpub def decl_range_span (dr : DeclRange) : SourceRangepub def located_decl_list (l : LocatedDecls) : List Declpub def located_decl_spans (l : LocatedDecls) : List SourceRangepub def inductive_decl_name (i : Inductive) : NamePathpub def struct_decl_name (s : Struct) : Identifierpub def class_decl_name (c : Class) : Identifierpub def decl_ranges_of_source (src : String) : List DeclRangeEvery top-level declaration in src, with its name, kind and range.
List.empty on a parse failure. A caller that wants the failure itself
-- check_file_cached_from_source_ranged does -- parses separately
rather than reading it out of an empty list, because a parse error is
not a declaration range and pretending otherwise would make "clean
file" and "unparseable file" the same answer.
PRE-EXPANSION, deliberately: this is what the user wrote, not what a
macro generated. parse_all_decls runs expand_decls after lowering
and this does not.
pub def extract_use_decls (decl_list : List Decl) : List ModulePathExtract use declarations from a list of declarations
pub def file_path_to_module_path (path : String) : ModulePathConvert a file path to a ModulePath
pub def extract_directory (file_path : String) : StringExtract the directory from a file path e.g., "init/src/process.mo" -> "init/src"
Delegates to std.path's raw_parent_dir rather than keeping its own
backwards scan: llvm/src/link.mo needs the same operation to create a
compile target's directory, and llvm cannot depend on lang.
pub def path_directory (p : Path) : Stringextract_directory's Path twin: a typed path's directory as the
String the mote/TOML APIs still take.
pub def directory_of_path (p : Path) : PathA typed path's containing DIRECTORY as a Path. A path with no
directory component ("main.mo") lives in the current directory, which
this spells . -- the same spelling Path.normalize gives an empty
parent -- so a loader directory is a total Path and never the empty
sentinel Path.of refuses.
pub def base_dir_path (s : String) : PathA directory given as text (a CLI argument, an extract_directory
result) as a Path; empty means the current directory.
pub def module_name_from_path (file_path : String) : StringDerive a module name from a file path — e.g. "examples/foo.mo" -> "foo".
Factored out of load_file_modules (below) so check_file can reuse
the exact same convention without a caller-supplied mod_name.
pub def resolve_runtime_src (base_dir : String) : IO StringThe runtime C source, located from the first candidate that exists:
the target mote's own declared [dependencies.runtime] path, the
workspace root above the working directory, an installed toolchain
root, and finally Runtime.c_path.
The WORKSPACE tier outranks the installed toolchain, and the order
is the lesson that taught it: ~/.monad/active names a NIGHTLY, and
a dev checkout testing natives its own runtime.c adds (the proc
family, 2026-10-14) silently linked the nightly's stale C and failed
at undefined reference -- the same staleness this repo's build
script's own input scan exists to stop. Reaching the workspace tier
at all means a workspace marker ([workspace] members) was found,
which only a checkout of this very toolchain has; an end user's
project has none, and keeps getting the installed runtime.
The first tier is ABSOLUTE by construction -- a manifest path may
be absolute -- and that matters because clang
resolves this argument against the COMPILER's own CWD. Only a relative
spelling is CWD-sensitive, so an absolute candidate sidesteps the
question the last two exist to answer. Those two stay relative: the
walk's answer is spelled from the working directory it walked from
(../runtime/src/runtime.c from inside cli/, where
runtime/src/runtime.c does not exist), and Runtime.c_path is the
checkout-root-relative literal, which is the right answer exactly when
the working directory IS that root.
The winner is deliberately NOT passed through normalize_path, even
though a walked spelling still carries its ..: that spelling is valid
only relative to the CWD it was walked from, so collapsing it would name
a file that does not exist from inside cli/. normalize_path is safe
for module resolution because the resolved path is read in the same
process that resolved it; this one is handed to a CHILD process.
Falls back to Runtime.c_path when no candidate exists, rather than
reporting a miss here: the child is what knows how to say "no such file
or directory", and its message names the path it could not open.
base_dir is the directory of the file being compiled or tested, which
is what the manifest tier needs -- the mote whose [dependencies.runtime]
says where the C runtime lives. That is the tier an EXTERNAL mote relies
on (one in its own repository, with no checkout to walk up into), and it
is why this no longer takes the working directory as its only anchor.
pub because cli is a different mote from lang and its compile/run
paths are the callers: this moved here from cli/src/main.mo when the
codegen harnesses below needed the same resolver, and a def that crosses a
mote boundary has to say so.
pub struct LoadedAndCache {
loaded : Result String LoadedModules,
cache : ModuleInfoCache,
}Walk a module's transitive use-dependency closure AND load each
reached module with its OWN resolved directory as the base, returning
the loaded ModuleInfos directly. This replaces the old two-step
extract_all_dependencies_go (which returns only ModulePaths) +
load_dependency_entries (load_module_with_info main_base_dir) pattern
in load_file_modules: that second step re-loaded every dependency
from the TARGET's own directory, so a dependency that shares a
single-segment name with a file in the target's directory got
mis-resolved. The real case: checking lang/parser/combinators.mo,
whose target dir lang/parser/ contains a string.mo (the
self-hosted string-LITERAL parser), shadowed init/string.mo (the
String.length/String.get/... stdlib) when init's pub use string
dep was re-resolved from lang/parser/ -- so combinators saw the
parser's string module instead of the stdlib, and every
String.length/String.get/String.drop reference went
unknown variable. By loading each module exactly once during the
walk -- where base_dir is already the PARENT module's resolved
directory, not the target's -- init/string.mo is what gets loaded
for the string dep, matching the Rust reference's own per-module
resolution. Modules that fail to load are skipped (matching
extract_all_dependencies_go's own existing lenient skip), not
fatal -- the canonical pipeline surfaces genuine "module not found"
failures via elaborate_loaded_modules's own Result path.
The walk's result plus the (extended) cache it built along the way.
pub struct InfosAndCache {
infos : List ModuleInfo,
cache : ModuleInfoCache,
}pub struct PendingModule {
path : ModulePath,
base_dir : Path,
}One queued dependency, paired with the directory it must be resolved FROM -- its importer's own directory.
The walk used to thread a single base_dir through a flat worklist and
replace it with each module's directory as it went, so a queued module
was resolved from wherever the PREVIOUSLY loaded module happened to
live. That is only ever right when the worklist is a straight chain.
It went unnoticed because every mote-qualified path in this workspace
resolves from the working directory regardless of base_dir; a mote
found through its own manifest (motes/demo) does not, and the
dependency silently failed to load -- surfacing much later as an
"unknown variable" in the importing file.
pub def typecheck_module_with_scope (scope : Scope) (decl_list : List Decl) (locals : LocalScope) : IO BoolType check all declarations in a module with a given scope. A thin
Bool-returning wrapper over check_module_with_scope (the richer,
IO-returning, diagnostics-producing walk check/elaborate_loaded_ modules already use) rather than a second, independently-maintained
walk of the same logic -- the two used to be hand-copied and had
quietly drifted apart (this Bool-returning family never skolemized a
def's own implicit type params the way check_def_with_scope did,
among other gaps check_module_with_scope's own richer walk already
covers: struct_d/class_d, scoped_open_d recursion, ...). Used by
slow_tests/*.mo's own corpus-checking #[test]s.
pub def check_module_with_scope (scope : Scope) (decl_list : List Decl) (locals : LocalScope) (path : Option String) (verbose : Bool) : IO (List String)A module's type diagnostics, then its termination diagnostics --
lang/src/termination.mo's port of the host's check_termination_all
(rust/core/src/eval/termination.rs), which the host likewise runs once per
module over that module's own declarations. Before this, both attributes
parsed and were ignored self-hosted, so a definition that loops forever
checked clean here while the host rejected it.
The termination pass is a decl-list walk, not a per-decl one, because it needs the whole module to build its call graph; appending its result here keeps the two gates in the one place the host has them.
SCOPE: ordinary typing + termination ONLY. The opaque/view safety rules
are added by check_module_with_scope_classified (and so by
check_module_with_scope_path, the compile/run/test/eval path; the LSP's
ranged path adds them too). This def is the ordinary half those call --
do NOT use it as a whole-module check on source that may carry
#[opaque], #[proof] or a view annotation: it passes an empty or
multi-constructor opaque type clean. Its only direct callers are the
Bool wrapper above and this module's own tests.
pub struct Diagnostic {
message : String,
range : Option SourceRange,
}One diagnostic, with the declaration it was reported against.
range is Option because not every diagnostic HAS a declaration: the
termination pass reports per module and a load failure reports per file.
Option.none is that case, stated rather than filled in with an
invented range.
NO SEVERITY FIELD, and the reason binds whoever adds one. The string
channel now carries severity -- FileCheckResult.warnings beside
diagnostics, counted separately by run_check_loop -- but this
struct is the RANGED walk's, and the ranged walk carries only what
check_decl_with_scope already returns, all errors. When a warning
must reach the editor, the field lands here together with the ranged
walk's own warning producer; adding it before that producer exists
would be an untestable channel.
message IS THE RENDERED MULTI-LINE STRING, not a one-line summary, and
that is a binding note on the wire layer rather than a defect. The ranged
walk tags what check_decl_with_scope already returns, and that is
render_type_error's / render_parse_error's output: a header line, a
--> path:line:col arrow, and three lines of source with a caret. Right
for check's terminal, wrong for an editor, which wants the header line
alone in its popup. The reduction belongs where the wire diagnostic is
built (motes/toolkit's diagnostic module), and the plain forms to
reduce TO already exist: type_error_message and error_message
(lang/src/typecheck/diagnostic.mo:40, lang/src/parser/diagnostic.mo:23).
Do not add a second message field here -- check reads this one.
pub def diagnostic_message (dg : Diagnostic) : Stringpub def diagnostic_range (dg : Diagnostic) : Option SourceRangepub def check_module_with_scope_ranged (scope : Scope) (decl_list : List Decl) (locals : LocalScope)
(path : Option String) (decl_ranges : List DeclRange) (verbose : Bool) : IO (List Diagnostic)check_module_with_scope's ranged sibling: the same two gates in the
same order, the per-declaration walk and then the module-level
termination pass.
check_termination_all reports on the module as a whole -- it is a
decl-list walk building a call graph, not a per-decl check -- so its
messages carry Option.none rather than a range borrowed from whichever
declaration happens to sit nearby.
pub struct CheckDiagnostics {
errors : List String,
warnings : List String,
}A module's diagnostics, already classified by the mote's policy.
errors fail the build; warnings are printed and counted.
pub def CheckDiagnostics.errors (d : CheckDiagnostics) : List Stringpub def CheckDiagnostics.warnings (d : CheckDiagnostics) : List Stringpub def check_module_with_scope_classified (mode : AffineMode) (scope : Scope) (decl_list : List Decl) (locals : LocalScope) (path : Option String) (verbose : Bool) : IO CheckDiagnosticsThe default path: the same ordinary diagnostics as
check_module_with_scope, plus the affine rule's, classified by
mode. ctors/borrows are built ONCE per module rather than per
def (affine.mo's own doc on check_def; per-node lookup would be a
full scope scan at every application).
pub def check_module_with_scope_path (scope : Scope) (decl_list : List Decl) (locals : LocalScope) (file_path : String) (verbose : Bool) : IO CheckDiagnosticsThe classified check with the policy resolved from the file's own
mote.toml. This is what compile/run/test/eval call: they
already hold a path and must not thread a policy through every
intermediate signature.
pub def check_module_with_scope_affine (scope : Scope) (decl_list : List Decl) (locals : LocalScope) (path : Option String) (verbose : Bool) : IO (List String)Strict counterpart kept for tools and tests that want the rule's
diagnostics in one list regardless of any manifest:
bench/src/affine_probe.mo and this module's own end-to-end tests.
pub def declared_type_diagnostics (name_str : String) (typ : Term) (scope : Scope)
(locals : LocalScope) (path : Option String) : List StringCheck the declared type itself, not only the body against it. Before
this, def bad : Vec (-1) I64 := Vec.nil checked cleanly: the body check
used the annotation as an expected type and no entry point ever asked
whether the annotation was a well-formed TYPE. A declared type must infer
a sort; the inductive-application rule then checks the indices inside it.
pub def def_body_diagnostics (df : Def) (scope : Scope) (locals : LocalScope) (path : Option String) : List StringThe PURE half of check_def_with_scope: the diagnostics for one def's
body, with no printing and no IO. Split out so a pin can drive the real
rule (lang/src/tests/module_tests.mo) instead of only the helper it
calls -- the wiring is exactly what regressed silently before.
pub def body_signature_diagnostics (name_str : String) (typ : Term) (body : Term)
(scope : Scope) (locals : LocalScope) (path : Option String) : List StringThe pure signature check shared by both def-body entry points: check
body against the def's own declared typ, and REPORT a mismatch.
typ, not Term.hole: a def's declared typ is ALREADY the full
Pi-chain matching its body's Term.lam chain (lang/parser.mo's
build_param_pi_chain), so checking the body against it lets
expected-type information flow into the body -- e.g. into a match arm
whose result is an unannotated struct literal, which otherwise can't
infer its own struct type. This is what the expected_type threading
through match-arm checking was added for.
The body_type_error call at the end is the part that was missing:
type_check THREADED the declared type and then both entry points
threw the checked result away, which made the threading vacuous for
every arm that returns its own type without comparing. That comparison
and its guards live in lang/typecheck/infer.mo, because the lambda
rule needs exactly the same ones for the parameterised half.
type_check's own error is reported UNCONDITIONALLY: body_type_error
gates only the signature COMPARISON. Gating the error too was tried and
broke test_class_default_body_is_checked and three class-method
pins -- a generic class default body is exactly what the typevar guard
skips, and its unbound-name error must still surface.
pub def elaborate_module_decls_best_effort (scope : Scope) (decl_list : List Decl) (locals : LocalScope) : List DeclBest-effort sibling for codegen's own WHOLE-GRAPH use (codegen. emit/codegen.test_driver) rather than check's own gate
(which wants elaborate_module_decls's all-or-nothing, real-
diagnostics behavior above): a def that fails to elaborate is left
COMPLETELY UNCHANGED rather than aborting the whole pass. This
matters because the loaded graph for even a trivial program includes
the whole prelude/init closure, and a single unrelated, pre-existing
gap ANYWHERE in it (a class methods own higher-kinded implicit param this pass doesn't skolemize, a constrained-instance method whose own dict-forwarding doesn't yet re-typecheck cleanly -- both real, already-known, separately-tracked gaps, not something codegen should have to wait on) must never block the whole graph's worth of elaboration -- resolve_class_calls_decls` (the old syntactic pass)
still gets a chance at whatever this couldn't resolve, afterward,
same as always. Never fails; always returns as much elaborated as
possible.
pub def decl_display_name (d : Decl) : StringA decl's own name, for the --verbose report below. Only Decl.def_d
carries a body that can fail to elaborate in a way worth naming; every
other shape reports its kind, which is enough to say "not a def".
pub struct BestEffortElab {
decls : List Decl,
failed : List String,
errors : List String,
}What elaborate_module_decls_best_effort produced, plus the names of the
decls it silently left unchanged.
The failed half exists because swallowing those errors is what made a
whole class of bug invisible. A def that fails to elaborate keeps its
un-elaborated body, and codegen's syntactic fallbacks then have to cover
for it -- when one of THOSE has a gap too, the symptom surfaces stages
later as no instance found for Append.append`` in a def whose real
problem was that it never elaborated. Nothing printed anything in
between. Now --verbose says how many decls this pass gave up on, and
names the first few, so the next one starts with a location instead of a
bisect.
pub def best_effort_decls (r : BestEffortElab) : List Declpub def best_effort_failed (r : BestEffortElab) : List Stringpub def best_effort_errors (r : BestEffortElab) : List StringOne name: message line per failed decl, parallel to best_effort_failed.
pub def elaborate_module_decls_reporting (scope : Scope) (decl_list : List Decl) (locals : LocalScope) : BestEffortElabpub struct FileCheckResult {
path : Path,
diagnostics : List String,
warnings : List String,
}One file's check outcome, severity-split. diagnostics are ERRORS --
a non-empty list fails the file and the run -- while warnings ride
alongside without counting toward that verdict. Match coverage used to
be this channel's first producer; strict-exhaustiveness.md Phase 4
flipped that rule to an error (validate_match_coverage,
lang/typecheck/infer.mo), so it now reports through diagnostics
instead, and the warning-side collector that used to mirror it here was
deleted rather than left to double-report. warnings today carries only
the affine rule's own diagnostics under [affine] mode := "warn".
Rendered warnings still appear in the block Build.check_block prints
and stores, so a cache HIT replays them verbatim; only the error count
decides the exit code.
pub struct FileCheckAndCache {
result : FileCheckResult,
cache : ModuleInfoCache,
}check_file_cached's own result bundled with the (possibly updated)
ModuleInfoCache, so run_check_loop can thread it forward to the
next file in the same run.
pub def check_file_cached_from_source (cache : ModuleInfoCache) (file_path : String) (source : String) (verbose : Bool) : IO FileCheckAndCachecheck_file_cached for text that is not (yet) on disk -- the language
server's entry point. The same body minus the file_exists gate: a
buffer is checked from what the editor holds, so whether a file of
that name exists is no longer the question.
The checking is check_module_with_scope_classified, byte-identical to
the disk path's, so a buffer and the file it stands for report the same
diagnostics once saved. The policy comes from the buffer's own
mote.toml.
verbose must be false on any path whose stdout is a protocol stream.
pub struct RangedFileCheck {
path : Path,
diagnostics : List Diagnostic,
cache : ModuleInfoCache,
ranges : List DeclRange,
scope : Option Scope,
located : LocatedDecls,
}FileCheckAndCache for the ranged path: the same three pieces, with
Diagnostic in place of the rendered string -- plus the two things
navigation needs and nothing else in the compiler produces.
WHY THE SCOPE IS HERE. A language server answers hover and
go-to-definition by resolving an identifier against the scope of the
file it was typed in, and that scope exists for exactly as long as the
check that built it: check_buffer_ranged has it in em.scope and used
to drop it on the floor, so the only way for a server to resolve two
identifiers in the same file would have been to re-elaborate the whole
closure per keystroke. Carrying it out of the check is what makes a
warm recheck worth having -- and it is the reason this struct grew
rather than navigation growing its own entry point. Option.none on
every path that did not elaborate: a file that did not parse has no
scope, and neither has one whose elaboration failed.
WHY THE RANGES ARE HERE. decl_ranges_of_located was already computed
on the elaborated path (the ranged diagnostics key on it), and on the
two failure paths the located parse is in the caller's hand, so the
outline costs nothing extra -- and it is most wanted exactly when the
file does not compile. List.empty only when there was no located
parse at all.
WHY THE LOCATED PARSE IS HERE. located is the buffer's own lowered,
position-carrying declarations -- what tooling (toolkit::navigation)
walks to find the binder a local under the cursor names. The buffer is in no module
cache, so this is the only place they survive the check. Empty exactly
when ranges is.
pub def ranged_file_path (f : RangedFileCheck) : Stringpub def ranged_file_diagnostics (f : RangedFileCheck) : List Diagnosticpub def ranged_file_cache (f : RangedFileCheck) : ModuleInfoCachepub def ranged_file_ranges (f : RangedFileCheck) : List DeclRangepub def ranged_file_scope (f : RangedFileCheck) : Option Scopepub def ranged_file_located (f : RangedFileCheck) : LocatedDeclspub def check_file_cached_from_source_ranged (cache : ModuleInfoCache) (file_path : String)
(source : String) (verbose : Bool) : IO RangedFileCheckcheck_file_cached_from_source's ranged sibling: the language server's
entry point, and the whole reason the in-memory load path exists.
The buffer is parsed ONCE up front, for two reasons. The ranges have to
come from the text the editor is showing, which is the buffer and not
the file on disk. And a buffer that does not parse has to be reportable
WITH a range, which the elaborate path cannot do -- it reports load
failures as a rendered String, load_module_decls_of_text included.
When the parse does not complete, this returns that one diagnostic and does NOT call elaborate. Both paths run the same grammar over the same text and the same remainder gate, so continuing would report one problem twice.
verbose must be false on any path whose stdout is a protocol stream.
Note the one hazard this does not remove: load_module_decls_of_text
prints its truncation message with an UNCONDITIONAL println (stdout,
not gated on verbose), so a DEPENDENCY that fails to parse -- a real
file on disk, reached by the walk -- still writes to stdout. That is
fine for check and must be dealt with before a server relies on it;
see the language-server plan's stdout rule.
pub def check_file_cached (cache : ModuleInfoCache) (file_path : String) (verbose : Bool) : IO FileCheckAndCacheThe file checker every caller uses. It resolves the mote's
[affine] mode once, so the affine rule rides every ordinary check
path exactly the way the termination pass does -- there is no flag
and no opt-in.
pub def expand_check_paths (paths : List String) : IO (List String)Expand a list of CLI path arguments into a flat file list — any
entry that's a directory is recursively walked for .mo files via
collect_mo_files; a plain file argument is kept as-is (even if it
doesn't end in .mo, matching check's existing behavior of
trusting an explicit file argument literally).
pub struct ModuleInfo {
path : ModulePath,
file_path : Path,
decl_list : List Decl,
}pub struct ModuleInfoCache {
entries : ModuleMap ModuleInfo,
hits : I64,
misses : I64,
}pub def module_info_cache_empty : ModuleInfoCachepub def module_info_cache_lookup (key : ModulePath) (cache : ModuleInfoCache) : Option ModuleInfopub def module_info_cache_entries (cache : ModuleInfoCache) : List ModuleInfoEvery cached module, in no particular order.
pub def module_info_cache_insert (key : ModulePath) (info : ModuleInfo) (cache : ModuleInfoCache) : ModuleInfoCachepub struct InfoAndCache {
info : Option ModuleInfo,
cache : ModuleInfoCache,
}A load_module_with_info that consults (and extends) the cache.
pub instance Show ModuleInfo {
def show (m : ModuleInfo) : String := show_module_info m
}pub struct LoadedModules {
main_module : ModuleInfo,
all_modules : List ModuleInfo,
}pub def make_module_info (path : ModulePath) (file_path : Path) (decls : List Decl) : ModuleInfoBuild a ModuleInfo from already-parsed declarations. The in-memory
caller (the browser REPL's engine) parses its own sources and hands the
result to elaborate_loaded_modules_pure; this keeps the constructor
spelling in one place and makes the loader's field order explicit.
pub def make_loaded_modules (main : ModuleInfo) (all : List ModuleInfo) : LoadedModulesThe LoadedModules a loader would have produced, from a main module and
its already-resolved dependency list (the main module is included in
all too, matching load_file_modules_cached_go).
pub instance Show LoadedModules {
def show (m : LoadedModules) : String := show_loaded_modules m
}pub def get_loaded_main (loaded : LoadedModules) : ModuleInfopub def get_loaded_all (loaded : LoadedModules) : List ModuleInfopub struct ModuleSource {
path : ModulePath,
mote : String,
file_path : Path,
source : String,
}One module's source text with the identity the loader assigns it:
path is the module path a use names, file_path is the label
diagnostics use, and mote is the owning mote the use lib:: rewrite
needs.
pub def make_module_source (path : ModulePath) (mote : String)
(file_path : Path) (source : String) : ModuleSourcepub def module_info_of_source (ms : ModuleSource) : Result String ModuleInfoParse one supplied module exactly as the disk loader parses a file:
the strict parser, then the lib rewrite for the module's own mote.
pub def load_modules_from_sources (main : ModuleInfo) (seeds : List ModulePath)
(sources : List ModuleSource) (reject : ModulePath -> Option String)
: Result String LoadedModulesThe dependency closure of main over supplied sources: seeds plus
main's own use lines, then each reached module's own use lines,
deduplicated by module path. Pure -- parsing and rewriting only -- so
it is usable on the wasm target.
pub def resolve_open_aliases_in_modules (modules : List ModuleInfo) : List ModuleInforesolve_open_aliases_in_module_info applied to every loaded
module -- the actual entry point codegen.emit/codegen. test_driver call, on get_loaded_all's own List ModuleInfo,
BEFORE collect_all_decls_from_modules flattens it. known_names
(every real def name across the WHOLE loaded program) is computed
once here and threaded to every module's own alias validation --
see filter_valid_open_aliases's own doc comment for why this
validation step is required at all. root_aliases (see its own doc
comment) is ALSO computed once and merged into every module's own
alias list, not just prelude/init/std's own.
pub def rewrite_lib_aliases_of (file_path : String) (decls : List Decl) : IO (List Decl)resolve_lib_alias_decls for a main module named by file_path: the
loading directory is derived here. The disk loader runs the rewrite
internally for its target module; a caller whose main module never
touches the disk loader (the REPL's session) needs it directly.
pub def rewrite_lib_uses (mote : String) (decls : List Decl) : List Declpub def load_module_with_info (base_dir : String) (mp : ModulePath) : IO (Option ModuleInfo)The String-shaped public entry: a CLI/LSP caller names a directory as
text; the loader below threads it as a Path.
pub def ownership_refusal_of_loaded (loaded : LoadedModules) : IO (Option String)Refuse ownership mode for a loaded closure that still contains an
[affine] mode = "warn" mote.
Ownership drop glue is only sound on code the CHECKER accepts under the
error-mode rule (plans/bootstrapping/ownership-regions-memory.md O0):
a warn-mode mote has known affine violations the ownership analysis
would have to guess about. The check lives here, beside the module set,
because the CLI and the codegen test driver both have a LoadedModules
in hand and reading manifests is IO. MONAD_OWNERSHIP unset, empty or
"0" is off; any other value is on.
pub def collect_link_libs (infos : List ModuleInfo) : IO (List String)The C libraries a whole program links against: the union of every
loaded module's mote's [link] libs, deduplicated, first-declared
order preserved.
Collected over the DEPENDENCY CLOSURE, not just the root mote: if a
mote you depend on calls into libm, your binary needs -lm, and you
should not have to restate that mote's build details in your own
manifest. The loaded set already IS the closure, so walking it is all
that is needed.
This is why #[extern "c"] carries no lib := "...". Which C symbol
a def binds to is a property of the declaration (link_name); what
the linker is handed is a property of the package, and belongs in the
manifest the same way Cargo keeps -l flags out of extern "C".
pub def collect_link_pkgs (infos : List ModuleInfo) : IO (List String)The pkg-config packages a whole program's link resolves its library
search paths from: the union of every loaded module's mote's
[link] pkg-config, over the same closure and with the same dedup as
collect_link_libs -- a program depending on motes/tls needs
openssl's -L for the same reason it needs its -lssl.
pub def load_modules_from_main (main : ModuleInfo) (cache : ModuleInfoCache) (verbose : Bool) : IO LoadedAndCacheThe dependency half of the filesystem loader: main is already parsed
and resolved (from disk, or from the caller's text), and this seeds
prelude/init/std into the walk and loads main's transitive use
closure from disk. Extracted from load_file_modules_cached_go so a
caller whose main module never comes from a file -- the REPL's session
-- uses the same standard loader instead of re-walking the closure.
The seed goes into the WALK rather than being prepended to the walk's
already-deduplicated result: prepending after the fact can reintroduce
a duplicate (prelude/init reached again via an explicit use). Each
dependency is resolved from its OWN importer's directory
(collect_dep_module_infos's own doc explains the shadowing bug that
fixes).
A supplied main cannot fail to load, so loaded is always Result.ok.
pub def load_file_modules (file_path : String) (verbose : Bool) : IO (Result String LoadedModules)Backwards-compatible wrapper: a standalone load with a fresh cache.
Every caller that isn't threading a whole-run cache uses this.
verbose forwards to the per-module trace (std/src/log.mo).
pub def bench_step (verbose : Bool) (label : String) (t0 : I64) (forced : I64) : IO I64Report one elaboration sub-step's elapsed time and hand back a fresh
timestamp for the next one, so a chain of lets can be timed by
threading the return value through it. Silent (and still returns a
timestamp) when verbose is false.
forced is not read: it exists so the caller can pass something that
consumes the step's own result (a List.length, a count), which
guarantees the work happens INSIDE the span rather than at some later
use. The evaluator is strict call-by-value -- rust/core/src/core_eval.rs's
App arm reduces both sides before applying, and let x := v in b
desugars to App(Lam b, v) -- so a let-bound step is already forced
where it is bound and forced is belt-and-braces. AGENTS.md item 25
claims the opposite ("the language is lazy", so a Bench.report
around a let measures nothing); whatever caused that measurement to
print nothing, laziness is not it. The check that matters either way
is arithmetic: these sub-times must add up to the enclosing
elaborate_loaded_modules total that cli/src/main.mo already prints.
If they do not, the spans are wrong -- do not reason about which.
pub struct ElaboratedModules {
scope : Scope,
target_decls : List Decl,
elaborated_decls : List Decl,
/// The `load_file_modules` result this elaboration was built from.
///
/// Carried on the result so a caller that needs the raw module set
/// too -- `compile`/`test`, which hand it to codegen -- can reuse
/// THIS load instead of calling `load_file_modules` a second time.
/// Before this field existed, one `monad compile` read and parsed
/// the target's entire transitive closure (prelude and init
/// included) twice: once here for the typecheck gate, once again in
/// `compile_file_codegen`.
loaded : LoadedModules,
}pub struct ElaboratedAndCache {
elaborated : Result String ElaboratedModules,
cache : ModuleInfoCache,
}pub def elaborate_def_typs (decls : List Decl) (known_names : List Identifier) : List DeclForall-wrap each def_d's declared type via elaborate_def
(lang.elaborate), using the WHOLE-GRAPH name set as known_names so
genuine globals (String, I64, List, ...) are filtered out and
NOT Forall-wrapped, while a def's own free/constraint-only type vars
(e.g. F in def Lens [Functor F] {F : Type -> Type} ..., whose
{F} clause the parser deliberately drops -- see def_implicit_close,
lang/parser.mo) get re-introduced as leading Forall F. ... binders.
Only def_d is touched here -- inductive/class/struct elaboration is
tracked separately (inductive-constructor and class-method param
skolemization). Non-def_d decls pass through unchanged. This is the
"wire elaborate.mo in" half of the constraint-vars-as-implicit fix;
locals_with_def_typevars's forall_chain_binder_names walk is the
consume-the-Forall-chain half that actually skolemizes those binders.
pub struct GraphExpansion {
graph : List Decl,
target : List Decl,
/// The SAME expansion as `target`, applied to the target's own RAW
/// (pre-elaboration) decl list instead of its prepared one -- see
/// `expand_decls_graph`'s own doc comment for why the codegen
/// pipeline needs this third form.
raw_target : List Decl,
changed : Bool,
/// Whether `raw_target` actually differs from the raw list handed in.
/// The caller patches the MAIN module's own `decl_list` only when it
/// does, so a file that expands nothing keeps the exact list it
/// loaded (and `loaded` is a pure value the caller may keep sharing).
raw_changed : Bool,
/// One entry per DEPENDENCY module whose own raw `decl_list` gained
/// decl-gen/`reflect_type_info!` output -- a derive written in a
/// dependency mote (not the file being compiled) otherwise never
/// reaches codegen, which reads each module's own `decl_list`
/// verbatim (`collect_all_decls_from_modules`, `codegen.decls`).
/// Always empty when `changed` is false, by the same monotonicity
/// `raw_changed` relies on: a decl-gen call that doesn't survive
/// substitution over the WHOLE graph cannot survive over any one
/// module's own subset of it either.
raw_all : List (Pair ModulePath (List Decl)),
}expand_decls_graph's result, plus whether it actually CHANGED
anything. The caller rebuilds its Scope from the expanded decls,
which is one full build_scope_from_decls over the whole dependency
graph -- measured at 1523ms of elaborate_loaded_modules' 5255ms on
examples/hello.mo. For the overwhelming majority of files nothing
expands (only std/derive.mo genuinely invokes reflect_type_info!
in this corpus), so changed = false lets the caller keep the scope
it already built instead of rebuilding an identical one.
Three decl lists, because there are three consumers and they need the expansion in two DIFFERENT shapes.
graph (whole dependency graph) and target (the target's own
decls) are the PREPARED lists -- the same infix/promote/dict-param
form dict_paramed is in -- so the caller can rebuild its Scope
from them and type-check against it. raw_target is the same
expansion applied to the target's own RAW decl list (the
main_module.decl_list the parser produced), and it exists because
the CODEGEN paths do not consume a prepared list at all: both
codegen.emit's compile_loaded_modules_to_ir and
codegen.test_driver's compile_loaded_modules_to_test_ir take
LoadedModules and run the elaboration passes THEMSELVES, over each
module's own raw decl_list (qualify_modules first, so a generated
decl's bare names -- String.concat, Lens, the Point.x lens's
own lens -- get the same module qualification every parsed decl
gets). Handing them an already-prepared list is NOT an option:
promote_instance_defs APPENDS the promoted defs to the list it is
given, so running the passes over their own output duplicates every
instance method and dictionary value. A generated decl is not a
prepared decl, so it has to enter the pipeline at the same place a
parsed one does -- which is what raw_target is for.
raw_target covers the MAIN module only. A derive in a DEPENDENCY
module reaches graph/target (and so the checker) the same way, but
codegen reads each module's own decl_list directly -- raw_all
(below) is the fourth list that covers that case: the same expansion,
applied to each DEPENDENCY module's own raw decl_list in turn.
pub def elaborate_loaded_modules_cached (file_path : String) (check_deps : Bool) (cache : ModuleInfoCache) (verbose : Bool) : IO ElaboratedAndCacheRead the target module from disk and elaborate it. The shape every
non-server caller wants; check_file_cached and elaborate_loaded_ modules both land here.
pub def elaborate_loaded_modules_pure
(loaded : LoadedModules) (check_deps : Bool) : Result String ElaboratedModulesThe real body; source is threaded down to the target module's load,
so a language server can elaborate a buffer. Option.none is every
existing caller -- see the wrapper above.
The post-load half of elaborate_loaded_modules_cached_go, split out so
an in-memory caller (the browser REPL's engine) can run the exact same
preparation the CLI runs, without a filesystem. Pure: no reads, no
manifest/use-completeness gates, and no bench_step -- those timing
spans inside this half collapse to the caller's one aggregate line,
which is the deliberate cost of sharing one implementation.
Everything below is the original body's shape unchanged: one scope for the whole visible graph, the same infix/promotion/dict-param passes, and the same guarded post-expansion scope rebuild.
pub def elaborate_loaded_modules_pure_for_targets
(loaded : LoadedModules) (targets : List ModulePath) (check_deps : Bool)
: Result String ElaboratedModuleselaborate_loaded_modules_pure with every TARGET of a batch named.
The only difference from the single-target half is the visible
flatten: every named target keeps its own priv decls, every other
module's are dropped. Everything else -- the infix/promotion/dict-param
passes, the one scope, the whole-graph expansion, known_names -- is
built once over the union, which is what lets a multi-file check
stop re-elaborating the closure per file. The expansion's own
"target" is still the first loaded module (get_loaded_main); a batch
caller checks the other targets against the resulting shared scope.
pub struct BatchCheckResult {
results : List (Pair String FileCheckResult),
cache : ModuleInfoCache,
}pub def check_files_cached (cache : ModuleInfoCache) (files : List String)
(verbose : Bool) : IO BatchCheckResultCheck a whole run's files with one closure load and one elaboration per distinct closure. Results are looked up by path, so grouping order does not reach the caller; a load/gate/macro failure falls back to the per-file path for that group, so the answer is the same either way.
pub def gate_loaded_modules (source : Option String)
(r : Result String LoadedModules) : IO (Result String LoadedModules)The three pre-elaboration gates the compiled pipeline runs over a loaded set. Manifest enforcement (package-system.md 2a/5d) comes first: a mote reaching into one it never declared, or one that names no target that exists, is a manifest error -- and saying so beats letting it surface as whatever name happens to go missing first.
Two sibling gates rather than one, each first-error-wins and each
independently revertable, so a bisect separates a bad dependency check
from a bad target check. The completeness gate needs the ORIGINAL
source (pre-expansion decls), so source is threaded here; it runs on
the main module only, exactly like its host-side twin.
pub def elaborate_loaded_set (source : Option String)
(loaded_result : Result String LoadedModules) (check_deps : Bool)
: IO (Result String ElaboratedModules)The post-load half of the front end, shared by the disk path and the
in-memory-main path: gates, then elaborate_loaded_modules_pure.
pub def elaborate_loaded_modules_from_main (main : ModuleInfo) (source : Option String)
(check_deps : Bool) (cache : ModuleInfoCache) (verbose : Bool)
: IO ElaboratedAndCacheThe whole front end for an already-parsed main module: the standard
filesystem dependency walk, the same gates the compiled pipeline runs,
and the pure elaboration. This is elaborate_loaded_modules's body
with the target supplied instead of read from disk, which an unsaved
session needs: check_file_cached_from_source cannot serve it, because
load_module_with_info_from_source resolves the target path on disk
first.
pub def elaborate_loaded_modules (file_path : String) (check_deps : Bool) (verbose : Bool) : IO (Result String ElaboratedModules)Backwards-compatible wrapper: elaborate with a fresh cache.