lang::typecheck::name_subst
lang/src/typecheck/name_subst.mo
NAME-based Term/Decl substitution — the primitive DECL-generating
macro templates (defmacro name params := decls { ... }) need,
sibling to [[lang/typecheck/subst.mo]]'s de-Bruijn term_shift/
term_subst (which cover the OTHER macro form, defmacro name params := <term>, and explicitly don't apply here — see that
file's own doc comment for why the two forms need genuinely
different substitution mechanisms).
A decl-gen template's own top-level decl_list have no enclosing lambda
binder — a macro param referenced inside one (typically inside a
nested Decl.macro_call_d's own args, e.g. std/derive.mo's
reflect_type_info! T derive_lens_meta) is an ordinary FREE
Term.var sentinel (DebugName.named X) reference (sentinel = -1,
see lang/typecheck/infer.mo), not a bound de Bruijn index. This
module walks a Term/Decl tree looking for exactly that shape —
a Term.var whose DebugName is named and matches the target
identifier by string equality (id_eq) — and replaces it wholesale
with a given replacement Term, leaving every bound index alone
untouched (nothing here is de-Bruijn-sensitive, so unlike
subst.mo there is no depth/cutoff bookkeeping at all).
Mirrors the Rust reference's own subst_macro/subst_decl_var
(rust/core/src/eval/macro_expand.rs), minus subst_decl_var's
subst_path_component handling of a macro param appearing inside a
generated DECL'S OWN NAME (e.g. templated def get_$field := ...)
— self-hosted's grammar has no string-interpolated-decl-name syntax
to parse that from in the first place (ModulePath/Identifier
are plain, non-templated strings throughout), so there is nothing
for that case to substitute into here.
Deliberate simplification, matching the reference's own equally
simple walk-and-replace (no shadow-tracking there either): if a
template itself locally re-binds the same name the macro param
uses (e.g. a nested def/defmacro's own param shares the outer
macro's param name), this walk does NOT stop substituting inside
that shadowed scope. Not exercised by any real corpus template
today (std/derive.mo's own templates only ever reference their
macro param from a nested macro_call_d's args, no nested
binders of the same name) — flagged here rather than silently
assumed correct.
pub def name_subst_term (target : Identifier) (replacement : Term) (t : Term) : Termpub def close_named_vars (stack : List Identifier) (t : Term) : TermCloses the checker's by-name free references (Term.var sentinel (named X),
as build_dict_field_projection_checked emits for D5 dict forwarding) over
their enclosing binders, so index-driven consumers (lower_core_ir) can
read the elaborated term. stack holds the binder names above t,
innermost first; a name with no enclosing binder is a global and is left alone.