Skip to content

Compile-time macros: RFC, implementation and playground - #90

Open
jfrolich wants to merge 38 commits into
ocaml-to-rust-rewritefrom
comptime-macros
Open

jfrolich wants to merge 38 commits into
ocaml-to-rust-rewritefrom
comptime-macros

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

What

Compile-time macros for the Rust compiler: a macro is a ReScript function the compiler runs while it compiles another file, and that returns code written as a quote.

// Debug.res
open Macro

@macro
let log = (e: code<'a>): code<'a> =>
  %code({
    let value = ${e}
    Console.log2(${Code.string(Code.print(e) ++ " =")}, value)
    value
  })

// Main.res
let total = %Debug.log(price *. Int.toFloat(quantity))

This replaces the first comptime proposal in docs/comptime (Zig-style partial evaluation, 27 special forms) with a design that has seven forms: @macro, @generic, %code, %type, %pat, ${...} and ${...items}. The rest is the Macro module of the standard library, with one macro the compiler has built in, %Macro.eval(expr).

  • RFC: docs/comptime/README.md, with docs/comptime/IMPLEMENTATION.md for the compiler side.
  • Compiler: compiler-rust/src/macros.
  • Playground: playground/, the compiler in WebAssembly with 17 example projects.

What works

Use Example
Expression macro %Debug.log(x + 1)
Generic, called like a function, one instance per type Json.encode(user), users->Array.map(Json.encode)
Compile-time arguments Power.pow(x, 10), %Units.table("meters", [("feet", 0.3048)])
Derive on a type @Enum.derive type color = Red | Green
Macro on a let @Trace.calls let f = ...
Type, pattern, module and top-level position type t = %Partial.t(user), | %Http.status("ok") =>, module K = %Gen.constants("k", 3)
Reflection Type.info, Type.companion, typeOf, expected
Compute now %Macro.eval(Sieve.primesBelow(60))
Files Macro.readFile("en.json")
Lists in a quote f(${...args}), {id: 1, ${...fields}}, switch x { ${...cases} }
A record built from data Code.record([Code.entry("x", x), Code.spread(base)]), Code.entryOf(field, value)
Optional fields Type.optional(field), Type.required(field); a field's typ is the type of value.field
Attributes on generated code %code(@warning("-27") ${stub}), Code.withAttr(value, "warning", ~payload)

How it works

An ordinary compilation stops at the first macro use, so a file without macros takes the path it always took. Expansion then works on the source text: a pass type checks the file, runs the macro at each use and records the result as an edit; the edits are applied and the next pass starts, until a pass records none. The expanded text is compiled like any file, with every position mapped back to the source as written.

Macros run as the compiled JavaScript of their module, in a JavaScript engine inside the compiler: QuickJS in native builds (feature macro-quickjs), the browser's engine in the playground. The engine has no clock, random numbers or I/O; a macro's questions (Type.info, readFile) are answered by the compiler.

What is not in this PR

  • rescript build does not expand macros yet. Macros work through driver::compile_source_in_process, which the tests and the playground use. The build system integration is the next step and is described in docs/comptime/IMPLEMENTATION.md.
  • No expansion cache, no hygiene for binders inside a quote, and a quote is type checked only where a use expands it. Splice holes stand in lists of expressions, not in patterns or types. The RFC lists these under "What is missing".
  • QuickJS is C and is new to the build: it is untested on Windows.

Other changes

  • The runtime package ships Macro (Macro.res, lib/js/Macro.cjs, lib/es6/Macro.mjs).
  • A runtime module that is type checked from source (tests, playground) is now checked with the runtime package's flags. With the user's flags, Primitive_array could not be loaded before Stdlib, so [a, ...rest] failed in the playground.
  • The playground compiles several files, runs the result, and has a manual workflow that deploys it to GitHub Pages.
  • The four documents of the first proposal are removed; they are in the history of docs/comptime.

Verification

  • cargo test --manifest-path compiler-rust/Cargo.toml --test macros --test macro_errors: 30 tests, including one that compiles every playground example, and 5 that compare the diagnostics of 27 broken programs with tests/macro_errors/*.txt.
  • npm run smoke in playground/: builds and runs the 17 examples with the compiler in WebAssembly.
  • Rebuilding packages/@rescript/runtime (106 modules) and packages/@rescript/belt (43 modules) with this branch leaves their checked-in JavaScript unchanged, apart from the new Macro module.
  • Not run: the JS output and error parity scripts against the OCaml compiler.

Started by Jaap Frolich in this Assistant session

open-session-submodule Bot and others added 26 commits November 11, 2026 13:18
A hole ${expr} parses, in expression, pattern and type position, as the
extension res.hole whose payload is the expression, so the parsetree gains
no node kind. %type(...) and %pat(...) take a type and a pattern as payload,
and an extension named by a module path takes types as arguments in a type
position. The scanner accepts $ inside identifiers only for macro-expanded
source.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A macro is a ReScript function marked @macro that the compiler runs while
it compiles another file. It returns code written as a quote, %code(...),
with ${...} holes.

- macros::quote lowers the quotes of a macro's own file to calls that build
  the quoted source text.
- macros::discover is the type checker's side: at a macro use it runs the
  macro and records the result as an edit of the source text. An ordinary
  compilation stops at the first macro use instead and asks for expansion.
- macros::expand applies the edits and type checks again until a pass finds
  nothing to expand; the compiler then compiles the expanded source with
  every position mapped back to the source as written.
- macros::quickjs embeds QuickJS as the engine of native builds, without
  clock, randomness or file access. A macro's questions to the compiler
  (types, files) are answered by running it again with the answer.
- The runtime gains the Macro module: the code, typ and pat types, the
  Type, Code, Pat, Attr and Decl helpers.

Files that use no macro compile exactly as before.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A @Generic of another file is typed at its erased type (code<'a> =>
code<JSON.t> as 'a => JSON.t) and noted. When the top-level item it is in
is fully inferred, the macro runs with the final types and its result
becomes a function in a let rec group before the item; the call is pointed
at it. Instance names contain a $, which hand-written source cannot, and
their group is left out of the module's signature.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
- @M.f on a type runs the macro with the checked declaration and adds the
  declarations it returns after the type; on a let it replaces the binding.
- %M.f(...) in a type position, as a structure item and as a module
  expression. A macro call takes several arguments.
- %comptime(expr) evaluates now and writes the value back as a literal.
- Generated instance groups carry @res.macroInstances and are neither in
  the module's signature nor exported.
- The source-fallback modules can be declared to be the runtime package,
  for hosts that have the runtime as source only.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The compiler keeps source text as one char per byte; the engine works with
the text those bytes encode. Requests and responses are converted where
they cross, and readFile and env answers are read like a source file.

host.js now hides the clock, random numbers and the page's input and
output from macro modules itself, so the browser engine gets the same
rules as the embedded one.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
- The runtime package now ships Macro as JavaScript (lib/js, lib/es6).
- A compile-time argument can be an array, tuple or record of literals.
- open Macro does not warn about shadowing Type: Macro.Type includes the
  standard library's Type.
- Pat.string and Pat.int.
- A runtime module that is type checked from source is checked with the
  runtime package's flags (-nopervasives -open Pervasives). With the
  standard library of the module that needed it, Primitive_array could not
  be loaded before Stdlib was.
- playground/examples holds the playground's example projects; a test
  compiles all of them.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
An instance that an earlier top-level item turns out to need moves to that
item's group, together with the instances it calls, instead of being
generated a second time. Macro errors get the type checker's header.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
- %M.f(...) where a pattern is written, for a macro that returns pat.
- A macro in a submodule: %Lib.Numbers.answer().
- A misuse that only expansion sees (a hole outside a quote, a macro of
  the wrong kind for its position) is reported at its place instead of as
  an unexpanded macro, and using a macro in the file that defines it says
  why that cannot work.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The playground compiles a project of several files in dependency order,
expands macros with the page's JavaScript engine, and runs the result. It
shows the expanded source, the JavaScript, the program's output and the
compiler's trees for the selected file.

- The compiler runs in a worker; a build that takes more than 20 seconds
  is stopped by replacing the worker.
- A project can be shared as a link.
- scripts/smoke.mjs builds and runs every example in Node.
- The Macros playground workflow deploys the static site to GitHub Pages
  when started by hand.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Replaces the first comptime proposal, which followed Zig's partial
evaluation, with the design that is implemented: macros are functions that
return quoted code. README.md is the RFC; IMPLEMENTATION.md describes the
compiler side.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
- An instance is named after the types at the open positions of the
  macro's signature: Decode.fromJson at user is fromJson$user, not
  fromJson$JSON_t.
- A parameter the generated code does not mention is written as _, so it
  does not warn.
- CompilerOptions::capture_loaded_modules returns the units a compilation
  loaded, for a host that keeps them for the next one.
- Examples: a JSON decoder by expected type, equality, default values,
  pattern macros and checked literals.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
After the first interpolation of a template literal in a pattern, the rest
is scanned as ordinary tokens, and OCaml's scanner reports the $ of a later
${ as an unknown character. ${ is a token now, so the pattern parser
reports it the same way. Fixes the diagnostic parity of
tests/syntax_tests/data/parsing/errors/pattern/templateLiteral.res.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The playground's index page is now the RFC, rendered from
docs/comptime/README.md. Each example block in it is a live example: the
project on the left, and on the right the source after expansion with the
lines the macros wrote marked, the JavaScript, the parse tree, typed tree
and Lambda, and the program's output. An example starts from a result built
with the site and loads the compiler when it is edited.

The full playground moves to playground.html, and IMPLEMENTATION.md gets a
page of its own.

The wasm entry point keeps the standard library modules a compile loaded,
so later compiles do not type check them again (700 ms to under 300 ms per
file), and returns the expansion formatted by the printer.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The source was limited to a fixed height while the result beside it also
holds the program's output, which left an empty strip below the source.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
When an item has an error of its own (an unknown name in the argument of a
@Generic, a type error in a %comptime expression), its macro use cannot be
expanded, and the compilation of the expanded file stopped at that use
with "macro expansion left a macro unexpanded".

The compilation of an expanded file now types a use that is left as far as
its declared type goes, so type checking gets to the error and reports it
as for any file. A failed item generates no instances, and its failure is
ordered after the problems its own macros reported.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Every character of an expanded file now knows where it comes from: its
place in the file as written, or the macro use it was generated for and,
for code written in a quote, the file, line and column of that quote.
Code values carry that through the macro: a quote's pieces come with
their position, an argument with its place in the file, and `Macro`
keeps both when it builds code from parts.

With that:

- an error in an argument of a macro points at the argument, not at the
  whole use;
- an error or warning in generated code is reported at the use, as a
  range, followed by the lines of the quote it is in; code that is not
  from a quote is shown as it is in the expanded source;
- an error in a function generated for a @Generic says which one:
  "generated by `Json.encode` for `t`";
- a macro that keeps generating a use of itself is reported at that use;
- generated code that does not parse is reported at the use, with the
  expanded source;
- an exception in a macro and a message from `Macro.error` read as such,
  and a %comptime expression that uses the file's own values says so
  without a second report inside the first.

The tests write their files to a directory, so the diagnostics show code
frames, and tests/macro_errors.rs compares the diagnostics of 21 broken
programs with tests/macro_errors/*.txt.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A range that starts in the code of one use and ends in the code of
another took the start of the second. Each end now maps by itself, and
only a range with one end in an argument is widened to the use.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The RFC's section on errors shows the diagnostics as they are now, and
the implementation notes describe where a character of an expanded file
comes from and how a diagnostic uses it. Checking a quote where it is
written replaces positions in generated code among the next steps.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1ca09-2757-709a-bbb1-e57ccc4d104f
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A splice hole stands for any number of items of a list. It is the
extension `res.hole.splice`: an expression where an expression is an item
(arguments, array and tuple items), a row of a record, and a case of a
switch. A block does not continue into a splice hole, so that one after a
case of a switch is the next case.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A splice hole `${...items}` in a quote is lowered by the list it is in:
the arguments of a call or a constructor and the items of an array or a
tuple take an array of code, the fields of a record an array of entries,
the cases of a switch an array of a pattern and its code. A hole with no
items leaves no comma behind. A splice hole anywhere else is an error at
its position.

A record value is built from entries: Code.entry, Code.entryOf and
Code.spread. Code.record takes them, so its fields can have different
types, be optional (`name: ?value`) and follow a spread.

The type of an optional field is the type of `value.field`, an option.
Code.entryOf, Pat.constructor and Type.record agree with it, and
Type.optional and Type.required change a field with its type.

Code.withAttr puts an attribute a macro computes on code.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
…ibutes

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A splice hole out of place, in a quote and outside one; an error in code
that was spliced in, which keeps the place it was written at; a field made
optional by hand.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://jakes-mac-mini.tail23a77e.ts.net:8443/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
open-session-submodule Bot and others added 2 commits November 12, 2026 16:00
Why:
    `%comptime` was the last place the first name of the feature was
    written. Everything else that runs while compiling is a macro, named
    by a module path, and a lowercase extension is a quote. The value of
    an expression is now the macro the compiler has built in: it is known
    by the name `Macro.eval`, and is an error where no expression is
    written.

Breaking: `%comptime(expr)` is not evaluated any more. Write `%Macro.eval(expr)`.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The form `%comptime(expr)` leaves the table of forms, and
`%Macro.eval(expr)` is listed with the module `Macro`.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
open-session-submodule Bot and others added 3 commits November 12, 2026 15:06
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1caab-4faf-7817-951e-2b3f9a5dae19
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
`%Sql.tables()` reads the `create table` statements of schema.sql while
compiling and is a module per table with the type of its rows.
`%Sql.query("select ...")` checks a query against the same schema: its
`row` is a record of the selected columns, its `run` takes a labelled
argument per `:parameter` with the type of the column it is compared
with, and a table or column that is not in the schema is an error of the
macro.

The row types are written with `Code.raw`: a hole in a quote is put in
parentheses, which a record type as the body of a type declaration
cannot be.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The tour shows the `sql` example after reading files: types and checked
queries from a schema file, why the schema is a file and the query a
string. What is missing lists what the example had to write as text:
a record type as the body of a quoted type declaration, and a type of
the macro's own file.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
open-session-submodule Bot and others added 7 commits November 12, 2026 22:07
Why:
    A filled hole is written in parentheses, and a record or a variant
    type cannot be: `type row = ${Type.record(fields)}` in a quote was a
    syntax error where the macro is used, so a macro that declares a
    record type had to write the declaration as text with `Code.raw`.

A type hole that is the body of a type declaration of the quote is lowered
to `Macro.__holeTypeBody`, which fills it without parentheses.

    @macro
    let shapes = (): decls =>
      %code(
        type point = ${Type.record([Type.field("x", %type(int)), Type.field("y", %type(int))])}
        type side = ${Type.variant([Type.constructor("Left"), Type.constructor("Right")])}
      )

Before (expanded):
    type point = ({x: int, y: int})
    type side = (| Left | Right)
After (expanded):
    type point = {x: int, y: int}
    type side = | Left | Right

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
A quote writes the name of a module out, so a macro that makes a module
per item of its data had to write `module Name = {` as text with
`Code.raw`. `Code.module_(name, items)` is `module Name = { items }`,
with the items a quote of declarations.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The row types are `type t = ${...}` in a quote and the module of a table
is `Code.module_`: the example has no `Code.raw` left.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
What is missing no longer lists the record type of a quoted declaration.
It lists what is left: a type of the macro's own file is not resolved in
that file, and a hole cannot be the name of a type or a module.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Why:
    A quote wrote the name of a module out, so a macro that makes a
    module per item of its data went through the builder `Code.module_`
    and passed the module's contents as an argument. With a hole for the
    name the module is written in the quote, like the rest of it.

`module ${name} = ...` takes a string. The name of a module is a string in
the parse tree, so the parser names the module `$hole`, which cannot be
written, and keeps the hole as the attribute `res.hole` of the module; no
node kind is added. The scan of a quote lowers it to
`Macro.__holeModuleName`, which reports a string that cannot be the name
of a module at the macro use. Outside a quote the module is reported as a
hole is.

    @macro
    let unit = (name: string, factor: float): decls =>
      %code(
        module ${name} = {
          type t = float
          let toBase = (value: t) => value *. ${Code.float(factor)}
        }
      )

    %%Named.unit("Feet", 0.3048)

After (expanded):
    module Feet = {
      type t = float
      let toBase = (value: t) => value *. (0.3048)
    }

Diagnostic added (a name that is not one):
Example:
    %%Named.unit("feet")
After (error):
    The name of a module starts with a capital letter, with letters, digits and `_` after it: "feet" cannot be one.

    This is reported by the macro `Named.unit`.

Breaking: `Code.module_(name, items)` is removed. Write `%code(module ${name} = { ... })`.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The module of a table is `module ${capitalize(table.sqlName)} = {...}`
in the quote that holds its declarations.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
The forms, the tour and the implementation notes describe
`module ${name} = ...`; `Code.module_` leaves the reference. What is
missing keeps the name of a type, which a quote still writes out.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/os-01a1d178-0260-7bbc-a0b0-6180a8266b98
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant