Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
31802e5
parser: ${...} holes and typed payloads for macro quotes
open-session-submodule[bot] Nov 11, 2026
5b1c210
Add compile-time macros: quotes, the engine and expression macros
open-session-submodule[bot] Nov 11, 2026
2d22441
macros: @generic, called like a function with one instance per type
open-session-submodule[bot] Nov 11, 2026
09baa1b
macros: derive, type, module and item macros, %comptime
open-session-submodule[bot] Nov 11, 2026
84cd206
macros: text outside ASCII, and the sandbox in the engine script
open-session-submodule[bot] Nov 11, 2026
f887c12
macros: build Macro with the runtime, structured compile-time arguments
open-session-submodule[bot] Nov 11, 2026
e08865f
macros: one instance per type in a file
open-session-submodule[bot] Nov 11, 2026
26d05b3
macros: pattern position, submodules, and errors for misuse
open-session-submodule[bot] Nov 11, 2026
54d4510
macros: companion and expected-type tests, instance names without unit
open-session-submodule[bot] Nov 11, 2026
445a1f3
playground: a macros playground
open-session-submodule[bot] Nov 11, 2026
b28509e
docs: RFC for compile-time macros
open-session-submodule[bot] Nov 11, 2026
197bc19
playground: wrap long lines of the expanded source
open-session-submodule[bot] Nov 11, 2026
adc427f
CHANGELOG: compile-time macros
open-session-submodule[bot] Nov 11, 2026
85cd51e
macros: name instances by what differs, more examples
open-session-submodule[bot] Nov 11, 2026
236999d
parser: report $ in an interpolated template pattern as before
open-session-submodule[bot] Nov 11, 2026
7753c55
site: the RFC as a page with live examples
open-session-submodule[bot] Nov 11, 2026
a031cae
site: the source pane of an example is as tall as its result
open-session-submodule[bot] Nov 11, 2026
4c8bfa5
macros: report the error of the code, not a macro that was left
open-session-submodule[bot] Nov 11, 2026
26e9b4b
macros: an error in generated code says where the code was written
open-session-submodule[bot] Nov 11, 2026
baef1b0
macros: a range across two macro uses keeps both ends
open-session-submodule[bot] Nov 11, 2026
bec767f
docs: what macro errors say
open-session-submodule[bot] Nov 11, 2026
2f1fbbe
parser: splice holes `${...items}` in macro quotes
open-session-submodule[bot] Nov 11, 2026
5fdff8e
macros: splice holes, record entries, optional fields
open-session-submodule[bot] Nov 11, 2026
43a753e
playground: examples for records, splice holes, optional fields, attr…
open-session-submodule[bot] Nov 11, 2026
dc2abcc
docs: records from data, splice holes, optional fields, attributes
open-session-submodule[bot] Nov 11, 2026
8314d53
macros: test what splice holes and optional fields report
open-session-submodule[bot] Nov 11, 2026
3251837
macros: `%comptime(expr)` is written `%Macro.eval(expr)`
open-session-submodule[bot] Nov 12, 2026
ee28712
docs: `%Macro.eval` in the RFC and the site
open-session-submodule[bot] Nov 12, 2026
06c1889
macros: test %Macro.eval without an expression and as a pattern
open-session-submodule[bot] Nov 12, 2026
d9225eb
playground: an example with types from a database schema
open-session-submodule[bot] Nov 12, 2026
c0f343c
docs: types from a schema in the RFC
open-session-submodule[bot] Nov 12, 2026
2b68ace
macros: a hole is the body of a quoted type declaration
open-session-submodule[bot] Nov 12, 2026
74b3239
macros: `Code.module_` builds a module with a computed name
open-session-submodule[bot] Nov 12, 2026
228546b
playground: the sql example declares its types in quotes
open-session-submodule[bot] Nov 12, 2026
a3e14fa
docs: a type hole as the body of a declaration, and `Code.module_`
open-session-submodule[bot] Nov 12, 2026
818aef8
macros: a hole is the name of a module in a quote
open-session-submodule[bot] Nov 12, 2026
e1d1edd
playground: the sql example names its table modules with a hole
open-session-submodule[bot] Nov 12, 2026
373bbd4
docs: a hole for the name of a module
open-session-submodule[bot] Nov 12, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/macros_playground.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Macros playground

# Builds the playground in playground/ and deploys it to GitHub Pages.
# Started by hand: the repository has to have Pages enabled with
# "GitHub Actions" as its source.
on:
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: macros-playground
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4

# rust-toolchain.toml pins the toolchain of every Rust build here.
- name: Install the pinned Rust toolchain
run: |
rustup toolchain install
rustup target add wasm32-unknown-unknown

- name: Install wasm-pack
run: cargo install wasm-pack --version 0.14.0 --locked

- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
cache-dependency-path: playground/package-lock.json

- name: Build
working-directory: playground
run: |
npm ci
npm run build:wasm
npm run smoke
npm run build

- uses: actions/upload-pages-artifact@v3
with:
path: playground/dist

deploy:
needs: build
runs-on: ubuntu-24.04
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
steps:
- id: deployment
uses: actions/deploy-pages@v4
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#### :rocket: New Feature

- Add compile-time macros to the Rust compiler: `@macro` and `@generic` functions that return quoted code (`%code`, `%type`, `%pat` with `${...}` holes), `%Macro.eval`, and the `Macro` module. `rescript build` does not expand them yet; see `docs/comptime`. https://github.com/jfrolich/rescript-rust/pull/90

#### :bug: Bug fix

- JSX v4: keep `@directive` on an async component's own function instead of its generated sync wrapper, so `@react.component let make = @directive("'use cache';") async (...) => ...` compiles to an async function that starts with `'use cache'` (ReScript 12 dropped the directive; Next.js rejects it on a sync function).
Expand Down
50 changes: 50 additions & 0 deletions compiler-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion compiler-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ oxc_span = "0.49"

# WASM support
wasm-bindgen = { version = "0.2", optional = true }
rquickjs = { version = "0.9", optional = true }

[dev-dependencies]
proptest = "1.5"
Expand All @@ -58,7 +59,9 @@ tempfile = "3"

[features]
default = ["native"]
native = ["rayon", "env_logger", "clap"]
native = ["rayon", "env_logger", "clap", "macro-quickjs"]
# The embedded JavaScript engine that runs compile-time macros.
macro-quickjs = ["rquickjs"]
wasm = ["wasm-bindgen"]

[profile.release]
Expand Down
4 changes: 4 additions & 0 deletions compiler-rust/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ impl<T, Idx: ArenaIdx> Arena<T, Idx> {
}

/// Iterate over all items with their indices.
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
self.items.iter_mut()
}

pub fn iter(&self) -> impl Iterator<Item = (Idx, &T)> {
self.items
.iter()
Expand Down
2 changes: 2 additions & 0 deletions compiler-rust/src/cli/bsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ fn run(args: Vec<String>, registry: Option<RegistryProvider>) -> Result<()> {
.and_then(|spec| parse_package_output(spec));

let compiler_opts = CompilerOptions {
macro_host: None,
dump_lambda: opts.dump_lambda,
dump_typed_sexp: opts.dump_typed_sexp,
dump_typed_sexp_locs: opts.dump_typed_sexp_locs,
Expand Down Expand Up @@ -543,6 +544,7 @@ fn run(args: Vec<String>, registry: Option<RegistryProvider>) -> Result<()> {
gentype_declarations_only: false,
js_package_specs: Vec::new(),
capture_intermediates: false,
capture_loaded_modules: false,
dumps: Vec::new(),
skip_js_render: false,
publish_in_process_signature: true,
Expand Down
63 changes: 50 additions & 13 deletions compiler-rust/src/cmi/persistent_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ pub struct ModuleRegistry {
/// as an implicit dependency (e.g., via `-open Pervasives`) but hasn't
/// been compiled yet in the current wave.
source_fallback_paths: SharedRegistryMap<PathBuf>,
/// The source-fallback modules are the modules of `@rescript/runtime`
/// (the playground and tests, which have the runtime as source only).
source_fallback_is_runtime: bool,
/// Package roots of registered modules that carry no `InMemoryModuleInfo`
/// (namespace units), for load-path visibility.
module_package_roots: SharedRegistryMap<PathBuf>,
Expand Down Expand Up @@ -1042,6 +1045,13 @@ impl ModuleRegistry {
}
}

/// Declare the source-fallback modules to be the modules of
/// `@rescript/runtime`: the playground and tests have the runtime as
/// source only, type check it on demand and import it from the package.
pub fn set_source_fallback_is_runtime(&mut self, is_runtime: bool) {
self.source_fallback_is_runtime = is_runtime;
}

pub fn insert_source_fallback_path(&mut self, name: String, path: PathBuf) {
if self.write_to_local {
self.local_source_fallback_paths.insert(name, path);
Expand Down Expand Up @@ -1137,6 +1147,8 @@ pub struct PersistentEnv {
/// has its source path, the module is type-checked from source on the fly.
/// Shared through the build registry so worker snapshots do not clone it.
source_fallback_paths: SharedRegistryMap<PathBuf>,
/// See `ModuleRegistry::source_fallback_is_runtime`.
source_fallback_is_runtime: bool,
/// Current depth of source-fallback recursion. Each `typecheck_source_signature`
/// call increments this. Used to prevent stack overflow from deep dependency chains.
source_fallback_depth: usize,
Expand Down Expand Up @@ -1255,6 +1267,7 @@ impl PersistentEnv {
in_process_mode: false,
disk_modules: SharedRegistryMap::default(),
source_fallback_paths: SharedRegistryMap::default(),
source_fallback_is_runtime: false,
source_fallback_depth: 0,
portable_cache: SharedRegistryMap::default(),
module_package_roots: SharedRegistryMap::default(),
Expand Down Expand Up @@ -1289,6 +1302,7 @@ impl PersistentEnv {
in_process_mode: true,
disk_modules: registry.disk_modules.clone(),
source_fallback_paths: registry.source_fallback_paths.clone(),
source_fallback_is_runtime: registry.source_fallback_is_runtime,
source_fallback_depth: 0,
portable_cache: registry.cached_modules.clone(),
module_package_roots: registry.module_package_roots.clone(),
Expand Down Expand Up @@ -2304,6 +2318,7 @@ impl PersistentEnv {
in_process_mode: self.in_process_mode,
disk_modules: self.disk_modules.clone(),
source_fallback_paths: self.source_fallback_paths.clone(),
source_fallback_is_runtime: self.source_fallback_is_runtime,
source_fallback_depth: self.source_fallback_depth + 1,
portable_cache: self.portable_cache.clone(),
module_package_roots: self.module_package_roots.clone(),
Expand All @@ -2322,17 +2337,37 @@ impl PersistentEnv {
}),
};
let mut env = initial_env(ctx);
let open_modules = fallback_pers.open_modules.clone();
let nopervasives = fallback_pers.nopervasives;
let compiling_module = fallback_pers.view.compiling_module.clone();
load_pervasives(
&mut fallback_pers,
&mut env,
ctx,
&open_modules,
nopervasives,
compiling_module.as_deref(),
);
if self.source_fallback_is_runtime {
// A module of the runtime is compiled as the runtime package
// compiles it (`-nostdlib -nopervasives -open Pervasives` in its
// rescript.json), not with the standard library of the module
// that needs it: `Stdlib` itself is built from these modules.
let module_name = source_path
.file_stem()
.map(|stem| stem.to_string_lossy().into_owned());
fallback_pers.nopervasives = true;
fallback_pers.open_modules = Arc::new(vec!["Pervasives".to_string()]);
load_pervasives(
&mut fallback_pers,
&mut env,
ctx,
&["Pervasives".to_string()],
true,
module_name.as_deref(),
);
} else {
let open_modules = fallback_pers.open_modules.clone();
let nopervasives = fallback_pers.nopervasives;
let compiling_module = fallback_pers.view.compiling_module.clone();
load_pervasives(
&mut fallback_pers,
&mut env,
ctx,
&open_modules,
nopervasives,
compiling_module.as_deref(),
);
}

if source_path
.extension()
Expand Down Expand Up @@ -2385,7 +2420,7 @@ impl PersistentEnv {
}

let options = CompilerOptions {
open_modules: self.open_modules.as_ref().clone(),
open_modules: fallback_pers.open_modules.as_ref().clone(),
jsx_version: self.jsx_version,
jsx_module: self.jsx_module.clone(),
..CompilerOptions::default()
Expand Down Expand Up @@ -2989,7 +3024,9 @@ impl PersistentEnv {

pub fn module_is_runtime(&self, name: &str) -> bool {
self.in_memory_info_with(name, |info| info.is_runtime)
.unwrap_or(false)
.unwrap_or_else(|| {
self.source_fallback_is_runtime && self.source_fallback_path_entry(name).is_some()
})
}
}

Expand Down
Loading