diff --git a/compiler-rust/src/macros/discover.rs b/compiler-rust/src/macros/discover.rs index 3e9415bfd2..10aaaa40f8 100644 --- a/compiler-rust/src/macros/discover.rs +++ b/compiler-rust/src/macros/discover.rs @@ -23,7 +23,7 @@ use crate::parser::ast::{ StructureItem as ParsedStructureItem, StructureItemDesc as ParsedStructureItemDesc, }; use crate::parser::longident::Longident; -use crate::parser::module::HOLE_EXTENSION; +use crate::parser::module::{HOLE_EXTENSION, SPLICE_HOLE_EXTENSION}; use crate::types::context::TypeContext; use crate::types::decl::ValueDescription; use crate::types::env::Env; @@ -876,6 +876,11 @@ fn fail(tctx: &mut TypeCheckContext<'_>, loc: LocIdx, message: impl Into const HOLE_OUTSIDE_QUOTE: &str = "A hole `${...}` can only be used inside a quote: %code(...), %type(...) or %pat(...)."; +/// `${...}` or `${...items}`: both belong in a quote. +fn is_hole_name(name: &str) -> bool { + name == HOLE_EXTENSION || name == SPLICE_HOLE_EXTENSION +} + fn discovering(tctx: &TypeCheckContext<'_>) -> bool { matches!(tctx.macro_mode, MacroMode::Discover(_)) } @@ -1173,7 +1178,7 @@ pub(crate) fn expression_extension( expected_ty: TypeExprRef, ) -> Option> { let name = tctx.arena.get_string(extension.0.txt).to_string(); - let is_hole = name == HOLE_EXTENSION; + let is_hole = is_hole_name(&name); if is_hole || quote::is_quote_name(&name) { // Quotes are lowered before a discovery pass; one that is left has // no payload. @@ -1688,7 +1693,7 @@ pub(crate) fn pattern_extension( extension: &Extension, ) -> Option { let name = tctx.arena.get_string(extension.0.txt).to_string(); - if name == HOLE_EXTENSION { + if is_hole_name(&name) { return Some(if matches!(tctx.macro_mode, MacroMode::Off) { required(tctx, loc) } else { @@ -1745,7 +1750,7 @@ pub(crate) fn type_extension( extension: &Extension, ) -> Option { let name = tctx.arena.get_string(extension.0.txt).to_string(); - if name == HOLE_EXTENSION { + if is_hole_name(&name) { return Some(if matches!(tctx.macro_mode, MacroMode::Off) { required(tctx, loc) } else { diff --git a/compiler-rust/src/macros/expand.rs b/compiler-rust/src/macros/expand.rs index c8562b81ff..9bcee66394 100644 --- a/compiler-rust/src/macros/expand.rs +++ b/compiler-rust/src/macros/expand.rs @@ -156,6 +156,17 @@ pub fn expand_source( text = Arc::new(text.apply(quote_edits)); continue; } + if let Some((start, end)) = quote::stray_splice(parser.arena(), &structure) { + return Err(diagnostic( + BUG, + source, + &text, + filename, + (start, end), + false, + quote::SPLICE_OUT_OF_PLACE, + )); + } let opens = top_level_opens(&parser, &structure, &text); let mut frontend_output = String::new(); diff --git a/compiler-rust/src/macros/quote.rs b/compiler-rust/src/macros/quote.rs index a5ccfc3500..bb54acbe57 100644 --- a/compiler-rust/src/macros/quote.rs +++ b/compiler-rust/src/macros/quote.rs @@ -18,21 +18,27 @@ //! is in. The hole expressions stay ordinary code of the macro: they are //! type checked, and a quote nested in a hole is lowered by the next pass. //! +//! A splice hole `${...items}` stands for any number of items of a list: +//! the arguments of a call, the items of an array or a tuple, the fields of +//! a record, the cases of a switch. It is lowered to `Macro.__spliceItems`, +//! `Macro.__spliceEntries` or `Macro.__spliceCases` by the list it is in. +//! //! Hygiene, as far as syntax decides it: a name the quote uses that the //! macro's file defines at its top level is written with the module's name //! (`encode` in `Json.res` becomes `Json.encode`), so it means the same where //! the macro is used. Names the quote binds itself and all other names are //! left as written and resolve at the use. -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use crate::binary_ast::ast_iter::{self, AstIter}; use crate::parse_arena::{CoreTypeIdx, ExprIdx, LocIdx, ParseArena, PatIdx}; use crate::parser::ast::{ - CoreTypeDesc, ExpressionDesc, Extension, PatternDesc, Payload, StructureItem, StructureItemDesc, + ArgLabel, CoreTypeDesc, ExpressionDesc, Extension, PatternDesc, Payload, StructureItem, + StructureItemDesc, }; use crate::parser::longident::Longident; -use crate::parser::module::HOLE_EXTENSION; +use crate::parser::module::{HOLE_EXTENSION, SPLICE_HOLE_EXTENSION}; use super::edit::{Edit, LineTable, Segment, SourceText}; @@ -136,14 +142,32 @@ fn top_level(arena: &ParseArena, structure: &[StructureItem]) -> TopLevel { names } +/// The list a splice hole `${...items}` stands in. +#[derive(Clone, Copy, PartialEq, Eq)] +enum SplicePlace { + /// Arguments of a call or a constructor, items of an array or a tuple. + Items, + /// Fields of a record. + Entries, + /// Cases of a switch. + Cases, +} + #[derive(Clone, Copy, PartialEq, Eq)] enum HoleKind { Code, Type, Pat, Item, + Splice(SplicePlace), + /// A splice hole where no list is written. It is left in the lowered + /// code, where [`stray_splice`] finds it. + SpliceOutOfPlace, } +/// The message for a splice hole that is not in a list of a quote. +pub const SPLICE_OUT_OF_PLACE: &str = "A splice `${...items}` stands for the items of a list in a quote: the arguments of a call, the items of an array or a tuple, the fields of a record, or the cases of a switch."; + struct Hole { /// The hole, `${...}`. outer: (u32, u32), @@ -162,10 +186,35 @@ struct QuoteScan<'a> { bound: HashSet, /// Expressions that are a structure item of the quote on their own. item_exprs: &'a HashSet, + /// The splice holes that are an item of a list, with the list. + splice_places: HashMap, +} + +fn is_splice(arena: &ParseArena, extension: &Extension) -> bool { + arena.get_string(extension.0.txt) == SPLICE_HOLE_EXTENSION +} + +fn is_splice_expr(arena: &ParseArena, idx: ExprIdx) -> bool { + matches!(&arena.get_expr(idx).pexp_desc, ExpressionDesc::Pexp_extension(extension) if is_splice(arena, extension)) } +/// Whether `idx` is an operator, as the function of `a + b` is. +fn is_operator(arena: &ParseArena, idx: ExprIdx) -> bool { + let ExpressionDesc::Pexp_ident(lid) = &arena.get_expr(idx).pexp_desc else { + return false; + }; + match arena.get_longident(lid.txt) { + Longident::Lident(name) => !arena + .get_string(*name) + .starts_with(|ch: char| ch.is_alphabetic() || ch == '_'), + _ => false, + } +} + +/// The range of the expression in a hole or a splice hole. fn hole_inner(arena: &ParseArena, extension: &Extension) -> Option<(u32, u32)> { - if arena.get_string(extension.0.txt) != HOLE_EXTENSION { + let name = arena.get_string(extension.0.txt); + if name != HOLE_EXTENSION && name != SPLICE_HOLE_EXTENSION { return None; } let Payload::PStr(items) = &extension.1 else { @@ -180,13 +229,70 @@ fn hole_inner(arena: &ParseArena, extension: &Extension) -> Option<(u32, u32)> { Some(range(arena, arena.get_expr(eval.0).pexp_loc)) } +impl QuoteScan<'_> { + /// Record the splice holes that are an item of a list of `idx`. + fn find_splice_places(&mut self, arena: &ParseArena, idx: ExprIdx) { + let mut place = |item: ExprIdx, place: SplicePlace| { + if is_splice_expr(arena, item) { + self.splice_places.insert(item, place); + } + }; + match &arena.get_expr(idx).pexp_desc { + // `f(a, b)`, and not `a + b`, which is an application as well. + ExpressionDesc::Pexp_apply(apply) if !is_operator(arena, apply.funct) => { + for (label, arg) in arena.apply_args(apply.args) { + if matches!(label, ArgLabel::Nolabel) { + place(*arg, SplicePlace::Items); + } + } + } + ExpressionDesc::Pexp_tuple(items) | ExpressionDesc::Pexp_array(items) => { + for item in arena.expr_list(*items) { + place(*item, SplicePlace::Items); + } + } + ExpressionDesc::Pexp_construct(_, args) => { + for arg in arena.expr_list(args.txt) { + place(*arg, SplicePlace::Items); + } + } + ExpressionDesc::Pexp_variant(inner) => { + for arg in arena.expr_list(inner.1.txt) { + place(*arg, SplicePlace::Items); + } + } + ExpressionDesc::Pexp_record(inner) => { + for field in arena.expr_field_list(inner.0) { + place(field.expr, SplicePlace::Entries); + } + } + ExpressionDesc::Pexp_match(inner) => { + for case in arena.case_list(inner.1) { + // The parser makes a case without a bar of a splice hole + // only: `| _ => ${...cases}` is not one. + if case.pc_bar.is_none() { + place(case.pc_rhs, SplicePlace::Cases); + } + } + } + _ => {} + } + } +} + impl AstIter for QuoteScan<'_> { fn expr(&mut self, arena: &ParseArena, idx: ExprIdx) { + self.find_splice_places(arena, idx); let expr = arena.get_expr(idx); match &expr.pexp_desc { ExpressionDesc::Pexp_extension(extension) => { if let Some(inner) = hole_inner(arena, extension) { - let kind = if self.item_exprs.contains(&idx) { + let kind = if is_splice(arena, extension) { + match self.splice_places.get(&idx) { + Some(place) => HoleKind::Splice(*place), + None => HoleKind::SpliceOutOfPlace, + } + } else if self.item_exprs.contains(&idx) { HoleKind::Item } else { HoleKind::Code @@ -232,7 +338,11 @@ impl AstIter for QuoteScan<'_> { self.holes.push(Hole { outer: range(arena, pat.ppat_loc), inner, - kind: HoleKind::Pat, + kind: if is_splice(arena, extension) { + HoleKind::SpliceOutOfPlace + } else { + HoleKind::Pat + }, }); } } @@ -255,7 +365,11 @@ impl AstIter for QuoteScan<'_> { self.holes.push(Hole { outer: range(arena, typ.ptyp_loc), inner, - kind: HoleKind::Type, + kind: if is_splice(arena, extension) { + HoleKind::SpliceOutOfPlace + } else { + HoleKind::Type + }, }); return; } @@ -310,6 +424,7 @@ impl QuoteFinder<'_> { } else { &item_exprs }, + splice_places: HashMap::new(), }; scan.payload(arena, &extension.1); @@ -335,6 +450,23 @@ impl QuoteFinder<'_> { } scan.holes.sort_by_key(|hole| hole.outer.0); + // The comma after a splice hole of items is the hole's: the hole + // writes it when it has items, so that none leave no comma behind. + let mut trailing_comma = vec![false; scan.holes.len()]; + for (hole, trailing) in scan.holes.iter_mut().zip(&mut trailing_comma) { + if !matches!( + hole.kind, + HoleKind::Splice(SplicePlace::Items | SplicePlace::Entries) + ) { + continue; + } + let rest = self.text.slice(hole.outer.1, payload.1); + let blank = rest.chars().take_while(|ch| ch.is_whitespace()).count() as u32; + if rest.chars().nth(blank as usize) == Some(',') { + hole.outer.1 += blank + 1; + *trailing = true; + } + } substitutions.sort_by_key(|at| at.0); let in_hole = |at: (u32, u32)| { scan.holes @@ -398,17 +530,33 @@ impl QuoteFinder<'_> { ))]; for (index, hole) in scan.holes.iter().enumerate() { let wrapper = match hole.kind { - HoleKind::Code => "__holeCode", + HoleKind::Code | HoleKind::SpliceOutOfPlace => "__holeCode", HoleKind::Type => "__holeType", HoleKind::Pat => "__holePat", HoleKind::Item => "__holeItem", + HoleKind::Splice(SplicePlace::Items) => "__spliceItems", + HoleKind::Splice(SplicePlace::Entries) => "__spliceEntries", + HoleKind::Splice(SplicePlace::Cases) => "__spliceCases", }; segments.push(Segment::Text(format!( "{}Macro.{wrapper}(", if index == 0 { "" } else { ", " } ))); - segments.push(Segment::Copy(hole.inner.0, hole.inner.1)); - segments.push(Segment::Text(")".into())); + match hole.kind { + // The whole hole, `${...items}`, stays in the code. + HoleKind::SpliceOutOfPlace => { + segments.push(Segment::Copy(hole.outer.0, hole.outer.1)); + } + _ => segments.push(Segment::Copy(hole.inner.0, hole.inner.1)), + } + segments.push(Segment::Text( + match hole.kind { + HoleKind::Splice(SplicePlace::Items | SplicePlace::Entries) => { + format!(", {})", trailing_comma[index]) + } + _ => ")".into(), + }, + )); } segments.push(Segment::Text(format!("], {}))", string_literal(self.file)))); self.edits.push(Edit { @@ -459,3 +607,51 @@ pub fn lowering_edits( ast_iter::walk_structure(&mut finder, arena, structure); finder.edits } + +/// Finds a splice hole that no quote lowered. +struct StraySplice { + found: Option<(u32, u32)>, +} + +impl AstIter for StraySplice { + fn expr(&mut self, arena: &ParseArena, idx: ExprIdx) { + let expr = arena.get_expr(idx); + if let ExpressionDesc::Pexp_extension(extension) = &expr.pexp_desc + && is_splice(arena, extension) + { + self.found.get_or_insert(range(arena, expr.pexp_loc)); + return; + } + ast_iter::walk_expr(self, arena, idx); + } + + fn pat(&mut self, arena: &ParseArena, idx: PatIdx) { + let pat = arena.get_pat(idx); + if let PatternDesc::Ppat_extension(extension) = &pat.ppat_desc + && is_splice(arena, extension) + { + self.found.get_or_insert(range(arena, pat.ppat_loc)); + return; + } + ast_iter::walk_pat(self, arena, idx); + } + + fn typ(&mut self, arena: &ParseArena, idx: CoreTypeIdx) { + let typ = arena.get_core_type(idx); + if let CoreTypeDesc::Ptyp_extension(extension) = &typ.ptyp_desc + && is_splice(arena, extension) + { + self.found.get_or_insert(range(arena, typ.ptyp_loc)); + return; + } + ast_iter::walk_typ(self, arena, idx); + } +} + +/// The first splice hole `${...items}` of `structure` that is not in a list +/// of a quote, once the quotes are lowered: see [`SPLICE_OUT_OF_PLACE`]. +pub fn stray_splice(arena: &ParseArena, structure: &[StructureItem]) -> Option<(u32, u32)> { + let mut finder = StraySplice { found: None }; + ast_iter::walk_structure(&mut finder, arena, structure); + finder.found +} diff --git a/compiler-rust/src/parser/expr.rs b/compiler-rust/src/parser/expr.rs index b421bdf312..b8ea79a6ed 100644 --- a/compiler-rust/src/parser/expr.rs +++ b/compiler-rust/src/parser/expr.rs @@ -135,6 +135,13 @@ fn is_valid_bigint_literal(s: &str) -> bool { // Placeholder Transformation // ============================================================================ +/// Whether the next token continues a block with another expression. A +/// splice hole `${...cases}` after a case of a switch does not: it stands +/// for more cases, not for an expression of the case before it. +fn block_continues(p: &mut Parser<'_>) -> bool { + grammar::is_block_expr_start(&p.token) && !super::module::at_splice_hole(p) +} + /// Check and emit "consecutive expressions" error if expressions on same line without semicolon. /// /// Matches OCaml's `parse_newline_or_semicolon_expr_block`: @@ -147,7 +154,7 @@ pub fn parse_newline_or_semicolon_expr_block(p: &mut Parser<'_>) { return; } - if grammar::is_block_expr_start(&p.token) { + if block_continues(p) { // Check if we're on the same line as the previous token // OCaml: if p.prev_end_pos.pos_lnum < p.start_pos.pos_lnum then () else err if p.prev_end_pos.line >= p.start_pos.line { @@ -3177,6 +3184,12 @@ fn parse_braced_or_record_expr(p: &mut Parser<'_>) -> Expression { p.expect(Token::Rbrace); expr } + // `{${...fields}}`: a record whose first row is a splice hole. + Token::DollarLbrace if super::module::at_splice_hole(p) => { + let expr = parse_record_expr(p, start_pos_idx, None, vec![]); + p.expect(Token::Rbrace); + expr + } Token::Lident(ref s) if s == "async" && super::core::is_es6_arrow_expression(p, false) => { p.expect(Token::Lident("async".into())); let expr = @@ -3376,6 +3389,14 @@ fn remove_module_name_from_punned_field_value(p: &mut Parser<'_>, exp: Expressio /// OCaml `parse_record_expr_row` (compiler/syntax/src/res_core.ml:3440-3508). fn parse_record_expr_row(p: &mut Parser<'_>) -> Option { + if super::module::at_splice_hole(p) { + let (lid, expr) = parse_splice_hole_row(p); + return Some(ExpressionRecordField { + lid, + expr, + opt: false, + }); + } let attrs = parse_attributes(p); if p.token == Token::DotDotDot { p.err(DiagnosticCategory::Message( @@ -3688,7 +3709,7 @@ fn parse_expr_block_item(p: &mut Parser<'_>) -> Expression { let (rec_flag, bindings) = super::module::parse_let_bindings_item(p, unwrap, attrs, start_pos_idx); parse_newline_or_semicolon_expr_block(p); - let next = if grammar::is_block_expr_start(&p.token) { + let next = if block_continues(p) { parse_expr_block(p) } else { let loc = p.mk_loc_current(); @@ -3731,7 +3752,7 @@ fn parse_expr_block_item(p: &mut Parser<'_>) -> Expression { e1.pexp_attributes = all; } parse_newline_or_semicolon_expr_block(p); - if grammar::is_block_expr_start(&p.token) { + if block_continues(p) { let e2 = parse_expr_block(p); let loc = p.mk_loc_spanning_preserve_ghost(e1.pexp_loc, e2.pexp_loc); let e1 = p.arena_mut().push_expr(e1); @@ -3761,7 +3782,7 @@ fn parse_expr_block_first(p: &mut Parser<'_>, first: Option) -> Expr None => parse_expr_block_item(p), }; parse_newline_or_semicolon_expr_block(p); - let block_expr = if grammar::is_block_expr_start(&p.token) { + let block_expr = if block_continues(p) { let next = parse_expr_block_item(p); let loc = p.mk_loc_spanning_preserve_ghost(item.pexp_loc, next.pexp_loc); let item = p.arena_mut().push_expr(item); @@ -4161,8 +4182,39 @@ fn parse_pattern_matching(p: &mut Parser<'_>) -> Vec { cases } +/// A splice hole that stands for rows of a record or cases of a switch: +/// the hole as an expression, and a name for the place the row or case +/// keeps for it. Only the lowering of a quote reads it. +fn parse_splice_hole_row(p: &mut Parser<'_>) -> (Loc, ExprIdx) { + let start_pos_idx = p.start_pos_idx(); + let extension = super::module::parse_hole_extension(p); + let loc = p.mk_loc_idx_to_prev_end(start_pos_idx); + let expr = p.arena_mut().push_expr(Expression { + pexp_desc: ExpressionDesc::Pexp_extension(Box::new(extension)), + pexp_loc: loc, + pexp_attributes: Attributes::new(), + }); + let lid = p.push_lident_static(super::module::SPLICE_HOLE_EXTENSION); + (with_loc(lid, loc), expr) +} + /// OCaml `parse_pattern_match_case` (compiler/syntax/src/res_core.ml:3972-3995). fn parse_pattern_match_case(p: &mut Parser<'_>) -> Option { + // `switch x { | A => 1 ${...cases} }`: cases a macro quote splices in. + if super::module::at_splice_hole(p) { + let (lid, rhs) = parse_splice_hole_row(p); + let lhs = p.arena_mut().push_pat(Pattern { + ppat_desc: PatternDesc::Ppat_any, + ppat_loc: lid.loc, + ppat_attributes: Attributes::new(), + }); + return Some(Case { + pc_bar: None, + pc_lhs: lhs, + pc_guard: None, + pc_rhs: rhs, + }); + } p.begin_region(); p.leave_breadcrumb(Grammar::PatternMatchCase); if p.token != Token::Bar { diff --git a/compiler-rust/src/parser/grammar.rs b/compiler-rust/src/parser/grammar.rs index 2f81aed7fb..9b2805188c 100644 --- a/compiler-rust/src/parser/grammar.rs +++ b/compiler-rust/src/parser/grammar.rs @@ -529,7 +529,8 @@ pub fn is_dict_row_start(token: &Token<'_>) -> bool { pub fn is_record_row_start(token: &Token<'_>) -> bool { matches!( token, - Token::DotDotDot | Token::Uident(_) | Token::Lident(_) + // `${`: a splice hole of a macro quote, `{a: 1, ${...fields}}`. + Token::DotDotDot | Token::Uident(_) | Token::Lident(_) | Token::DollarLbrace ) || token.is_keyword() } diff --git a/compiler-rust/src/parser/module.rs b/compiler-rust/src/parser/module.rs index 051118a2f1..ea352d263e 100644 --- a/compiler-rust/src/parser/module.rs +++ b/compiler-rust/src/parser/module.rs @@ -4112,18 +4112,42 @@ pub fn parse_extension_id(p: &mut Parser<'_>, start_pos_idx: PosIdx) -> Located< /// Name of the extension a macro quote hole `${expr}` is kept as. pub const HOLE_EXTENSION: &str = "res.hole"; +/// Name of the extension a splice hole `${...expr}` is kept as. +pub const SPLICE_HOLE_EXTENSION: &str = "res.hole.splice"; + +/// Whether the parser is at `${...`: the start of a splice hole. +pub(super) fn at_splice_hole(p: &mut Parser<'_>) -> bool { + p.token == Token::DollarLbrace + && p.lookahead(|p| { + p.next(); + p.token == Token::DotDotDot + }) +} + /// `${expr}`: a hole in a macro quote (`%code(...)`, `%type(...)`, /// `%pat(...)`). It is an extension node named [`HOLE_EXTENSION`] whose /// payload is the expression, so no parsetree node kind is added. +/// +/// `${...expr}` is a splice hole: it stands for the items of a list (the +/// arguments of a call, the fields of a record, the cases of a switch), and +/// is the extension [`SPLICE_HOLE_EXTENSION`]. pub(super) fn parse_hole_extension(p: &mut Parser<'_>) -> Extension { let start_pos_idx = p.start_pos_idx(); p.expect(Token::DollarLbrace); + let splice = p.token == Token::DotDotDot; + if splice { + p.next(); + } let expr = super::expr::parse_expr(p); p.expect(Token::Rbrace); let loc = p.mk_loc_idx_to_prev_end(start_pos_idx); let expr_loc = expr.pexp_loc; let expr_idx = p.arena_mut().push_expr(expr); - let name = p.arena_mut().push_string(HOLE_EXTENSION); + let name = p.arena_mut().push_string(if splice { + SPLICE_HOLE_EXTENSION + } else { + HOLE_EXTENSION + }); let item = StructureItem { pstr_desc: StructureItemDesc::Pstr_eval(Box::new((expr_idx, Attributes::new()))), pstr_loc: expr_loc, diff --git a/compiler-rust/tests/macro_errors.rs b/compiler-rust/tests/macro_errors.rs index 4a24f8691f..d7a36ee3ad 100644 --- a/compiler-rust/tests/macro_errors.rs +++ b/compiler-rust/tests/macro_errors.rs @@ -214,3 +214,50 @@ fn comptime_errors() { ); }); } + +const SPLICED: &str = r#"open Macro + +// Entries of a record, one of them a quote with the wrong type. +@macro +let point = (x: code): code<'r> => { + let entries = [Code.entry("x", x), Code.entry("y", %code("two"))] + %code({${...entries}}) +} + +@macro +let flagged = (source: typ): typ => + switch Type.info(source) { + | Record({fields}) => Type.record(fields->Array.map(f => {...f, optional: true})) + | _ => error("flagged needs a record") + } +"#; + +#[test] +fn splice_holes_and_optional_fields() { + on_a_big_stack(|| { + let mut project = Project::new(); + project.compile("Spliced.res", SPLICED).unwrap(); + // A splice hole stands in a list of a quote, and nowhere else. + check( + &mut project, + "spliceOutOfPlace", + "open Macro\n@macro\nlet sum = (a: code): code => %code(1 + ${...[a]})\n", + ); + check( + &mut project, + "spliceOutsideAQuote", + "let xs = [1]\nlet ys = [0, ${...xs}]\n", + ); + // Spliced code keeps the place it was written at. + check( + &mut project, + "splicedEntryType", + "type point = {x: int, y: int}\nlet p: point = %Spliced.point(1)\n", + ); + check( + &mut project, + "optionalFieldByHand", + "type user = {name: string}\ntype t = %Spliced.flagged(user)\n", + ); + }); +} diff --git a/compiler-rust/tests/macro_errors/optionalFieldByHand.txt b/compiler-rust/tests/macro_errors/optionalFieldByHand.txt new file mode 100644 index 0000000000..a40ea22639 --- /dev/null +++ b/compiler-rust/tests/macro_errors/optionalFieldByHand.txt @@ -0,0 +1,15 @@ +type user = {name: string} +type t = %Spliced.flagged(user) +---- + + We've found a bug for you! + OptionalFieldByHand.res:2:10-31 + + 1 │ type user = {name: string} + 2 │ type t = %Spliced.flagged(user) + 3 │ + + The optional field `name` has the type `string`, and the type of an optional field is an option. Type.optional makes a field optional. + + This is reported by the macro `Spliced.flagged`. + diff --git a/compiler-rust/tests/macro_errors/spliceOutOfPlace.txt b/compiler-rust/tests/macro_errors/spliceOutOfPlace.txt new file mode 100644 index 0000000000..7e64a59631 --- /dev/null +++ b/compiler-rust/tests/macro_errors/spliceOutOfPlace.txt @@ -0,0 +1,15 @@ +open Macro +@macro +let sum = (a: code): code => %code(1 + ${...[a]}) +---- + + We've found a bug for you! + SpliceOutOfPlace.res:3:50-58 + + 1 │ open Macro + 2 │ @macro + 3 │ let sum = (a: code): code => %code(1 + ${...[a]}) + 4 │ + + A splice `${...items}` stands for the items of a list in a quote: the arguments of a call, the items of an array or a tuple, the fields of a record, or the cases of a switch. + diff --git a/compiler-rust/tests/macro_errors/spliceOutsideAQuote.txt b/compiler-rust/tests/macro_errors/spliceOutsideAQuote.txt new file mode 100644 index 0000000000..43c8b6bc0a --- /dev/null +++ b/compiler-rust/tests/macro_errors/spliceOutsideAQuote.txt @@ -0,0 +1,13 @@ +let xs = [1] +let ys = [0, ${...xs}] +---- + + We've found a bug for you! + SpliceOutsideAQuote.res:2:14-21 + + 1 │ let xs = [1] + 2 │ let ys = [0, ${...xs}] + 3 │ + + A splice `${...items}` stands for the items of a list in a quote: the arguments of a call, the items of an array or a tuple, the fields of a record, or the cases of a switch. + diff --git a/compiler-rust/tests/macro_errors/splicedEntryType.txt b/compiler-rust/tests/macro_errors/splicedEntryType.txt new file mode 100644 index 0000000000..43fae1c944 --- /dev/null +++ b/compiler-rust/tests/macro_errors/splicedEntryType.txt @@ -0,0 +1,24 @@ +type point = {x: int, y: int} +let p: point = %Spliced.point(1) +---- + + We've found a bug for you! + SplicedEntryType.res:2:16-32 + + 1 │ type point = {x: int, y: int} + 2 │ let p: point = %Spliced.point(1) + 3 │ + + This is in the code the macro generated, written at Spliced.res:6:60-64 + + 4 │ @macro + 5 │ let point = (x: code): code<'r> => { + 6 │ let entries = [Code.entry("x", x), Code.entry("y", %code("two"))] + 7 │ %code({${...entries}}) + 8 │ } + + This has type: string + But the record field y is expected to have type: int + + You can convert string to int with Int.fromString. + diff --git a/compiler-rust/tests/macros.rs b/compiler-rust/tests/macros.rs index c9e6194aa7..0e080010e9 100644 --- a/compiler-rust/tests/macros.rs +++ b/compiler-rust/tests/macros.rs @@ -224,7 +224,7 @@ open Macro @macro let t = (source: typ): typ => switch Type.info(source) { - | Record({fields}) => Type.record(fields->Array.map(f => {...f, optional: true})) + | Record({fields}) => Type.record(fields->Array.map(Type.optional)) | _ => error("Partial.t needs a record type") } "#, @@ -649,7 +649,7 @@ let rec value = (_: code): code<'a> => | Bool => %code(false) | Array(_) => %code([]) | Option(_) => %code(None) - | Record({fields}) => Code.record(fields->Array.map(f => (f.name, %code(value())))) + | Record({fields}) => Code.record(fields->Array.map(f => Code.entryOf(f, %code(value())))) | _ => error(`Default.value: no default for ${Type.name(expected())}`) } "#, @@ -665,3 +665,264 @@ let rec value = (_: code): code<'a> => assert!(js.contains("retries: 0"), "{js}"); assert!(js.contains("verbose: false"), "{js}"); } + +const BUILD: &str = r#" +open Macro + +// Entries of different types, a spread and an optional entry in one record. +@macro +let labelled = (base: code<'r>, x: code, label: code>): code<'r> => + Code.record([Code.entry("x", x), Code.spread(base), Code.entry("label", label, ~optional=true)]) + +// The fields of the record, with a field of the quote before them. +@generic +let withId = (value: code<'a>, id: code): code<'a> => + switch Type.info(typeOf(value)) { + | Record({fields}) => + let rest = + fields->Array.filter(f => f.name != "id")->Array.map(f => Code.entryOf(f, value->Code.field(f.name))) + %code({id: ${id}, ${...rest}}) + | _ => error("withId needs a record") + } + +// The fields of the record, after a spread. +@generic +let cleared = (value: code<'a>): code<'a> => + switch Type.info(typeOf(value)) { + | Record({fields}) => + let cleared = fields->Array.filter(f => f.optional)->Array.map(f => Code.entryOf(f, %code(None))) + %code({...${value}, ${...cleared}}) + | _ => error("cleared needs a record") + } + +@macro +let call = (f: code<'f>, a: code<'a>, b: code<'b>): code<'c> => + %code(${f}(${...[Code.untyped(a), Code.untyped(b)]})) + +// Arguments of a constructor. +@macro +let twin = (a: code): code<'p> => %code(Pair(${...[a, a]})) + +// Holes with no items at the start, in the middle and at the end of a list. +@macro +let items = (a: code): code<(array, (int, int, int), int)> => { + let none: array> = [] + let two = [a, a] + %code(([${...none}, 0, ${...two}, ${...none}, 9, ${...none}], (1, ${...two}), Math.Int.max(${...none}, ${...two}))) +} + +// Cases of a switch, between cases of the quote. +@generic +let name = (value: code<'a>): code => + switch Type.info(typeOf(value)) { + | Variant({constructors}) => + let cases = constructors->Array.filterMap(c => + c.name == "Unknown" + ? None + : { + let (pattern, _) = Pat.constructor(c) + Some((pattern, Code.string(String.toLowerCase(c.name)))) + } + ) + %code( + switch ${value} { + | Unknown => "?" + ${...cases} + } + ) + | _ => error("name needs a variant") + } + +@macro +let quiet = (value: code<'a>): code<'a> => Code.withAttr(value, "warning", ~payload=Code.string("-27")) +"#; + +#[test] +fn record_entries() { + let mut project = Project::new(); + compile_ok(&mut project, "Build.res", BUILD); + let js = compile_ok( + &mut project, + "Points.res", + r#" +type point = {x: int, y: int, label?: string} +let origin: point = {x: 0, y: 0} +let named = %Build.labelled(origin, 3, Some("three")) +"#, + ); + let expanded = project.expanded.clone().unwrap(); + // The spread comes first, as a record has it. + assert!( + expanded.contains(r#"{...origin, x: 3, label: ?(Some("three"))}"#), + "{expanded}" + ); + assert!(js.contains("newrecord.label = \"three\""), "{js}"); +} + +#[test] +fn splice_holes() { + let mut project = Project::new(); + compile_ok(&mut project, "Build.res", BUILD); + let js = compile_ok( + &mut project, + "Splices.res", + r#" +type user = {id: int, name: string, age?: int} +let ada: user = {id: 1, name: "Ada", age: 36} +let renumbered = Build.withId(ada, 2) +let ageless = Build.cleared(ada) + +let pair = %Build.call((a, b) => (a, b), 1, "one") +let (list, triple, most) = %Build.items(5) + +type pair = Pair(int, int) +let twin = %Build.twin(4) + +type state = Unknown | Idle | Running +let label = (state: state) => Build.name(state) +"#, + ); + let expanded = project.expanded.clone().unwrap(); + // An optional field is copied as an option, with `?`. + assert!(expanded.contains("{id: (b$), name: a$.name, age: ?a$.age}"), "{expanded}"); + assert!(expanded.contains("{...(a$), age: ?None}"), "{expanded}"); + assert!(expanded.contains(r#"((a, b) => (a, b))(1, "one")"#), "{expanded}"); + // No items: no comma is left behind. + assert!(expanded.contains("[ 0, 5, 5, 9, ]"), "{expanded}"); + assert!(expanded.contains("(1, 5, 5)"), "{expanded}"); + assert!(expanded.contains("Math.Int.max( 5, 5)"), "{expanded}"); + assert!(expanded.contains("Pair(4, 4)"), "{expanded}"); + assert!(expanded.contains("| Idle => \"idle\"\n| Running => \"running\""), "{expanded}"); + assert_eq!(project.warnings, "", "{}", project.warnings); + assert!(js.contains("\"running\""), "{js}"); +} + +#[test] +fn splice_hole_out_of_place() { + let mut project = Project::new(); + let error = compile_err( + &mut project, + "Wrong.res", + "open Macro\n@macro\nlet sum = (a: code): code => %code(1 + ${...[a]})\n", + ); + assert!(error.contains("A splice `${...items}` stands for the items of a list"), "{error}"); + assert!(error.contains("Wrong.res:3:"), "{error}"); + + let error = compile_err(&mut project, "Outside.res", "let xs = [1]\nlet ys = [0, ${...xs}]\n"); + assert!(error.contains("A splice `${...items}` stands for the items of a list"), "{error}"); +} + +#[test] +fn optional_fields() { + let mut project = Project::new(); + compile_ok(&mut project, "Json.res", JSON); + compile_ok( + &mut project, + "Fields.res", + r#" +open Macro + +@generic +let describe = (value: code<'a>): code => + switch Type.info(typeOf(value)) { + | Record({fields}) => + Code.string( + fields->Array.map(f => f.name ++ (f.optional ? "?" : "") ++ ": " ++ Type.name(f.typ))->Array.join(", "), + ) + | _ => error("describe needs a record") + } + +@macro +let same = (source: typ): typ => + switch Type.info(source) { + | Record({fields}) => Type.record(fields) + | _ => error("same needs a record") + } + +@macro +let partial = (source: typ): typ => + switch Type.info(source) { + | Record({fields}) => Type.record(fields->Array.map(Type.optional)) + | _ => error("partial needs a record") + } + +@macro +let required = (source: typ): typ => + switch Type.info(source) { + | Record({fields}) => Type.record(fields->Array.map(Type.required)) + | _ => error("required needs a record") + } + +@macro +let flagged = (source: typ): typ => + switch Type.info(source) { + | Record({fields}) => Type.record(fields->Array.map(f => {...f, optional: true})) + | _ => error("flagged needs a record") + } + +@generic +let default = (_: code): code<'a> => + switch Type.info(expected()) { + | Int => %code(0) + | String => %code("") + | Option(_) => %code(None) + | Record({fields}) => Code.record(fields->Array.map(f => Code.entryOf(f, %code((default(): ${f.typ}))))) + | _ => error("no default") + } +"#, + ); + let js = compile_ok( + &mut project, + "User.res", + r#" +type user = {name: string, age?: int} +let ada: user = {name: "Ada", age: 36} +let fields = Fields.describe(ada) + +type copy = %Fields.same(user) +let copy: copy = {name: "Ada", age: 36} + +type patch = %Fields.partial(user) +let patch: patch = {age: 37} + +type full = %Fields.required(user) +let full: full = {name: "Ada", age: 36} + +let empty: user = Fields.default() + +type shape = Circle({radius: float, label?: string}) | Dot +let encode = (shape: shape) => Json.encode(shape) +"#, + ); + let expanded = project.expanded.clone().unwrap(); + // The type of an optional field is the type of `value.field`. + assert!(expanded.contains(r#""name: string, age?: option""#), "{expanded}"); + assert!(expanded.contains("type copy = {name: string, age?: int}"), "{expanded}"); + assert!(expanded.contains("type patch = {name?: string, age?: int}"), "{expanded}"); + assert!(expanded.contains("type full = {name: string, age: int}"), "{expanded}"); + assert!(expanded.contains("age: ?((default$option_int(): (option)))"), "{expanded}"); + // The pattern of an inline record binds an optional field as an option: + // the switch has a case for a shape without a label. + assert!(expanded.contains("Circle({radius: radius$0, label: ?label$1})"), "{expanded}"); + assert_eq!(project.warnings, "", "{}", project.warnings); + assert!(!js.contains("Match_failure"), "{js}"); + + let error = compile_err( + &mut project, + "Flagged.res", + "type user = {name: string}\ntype t = %Fields.flagged(user)\n", + ); + assert!(error.contains("Type.optional makes a field optional"), "{error}"); +} + +#[test] +fn attribute_on_generated_code() { + let mut project = Project::new(); + compile_ok(&mut project, "Build.res", BUILD); + compile_ok(&mut project, "Loud.res", "let size = unused => 2\n"); + assert!(project.warnings.contains("unused variable unused"), "{}", project.warnings); + compile_ok(&mut project, "Quiet.res", "let size = %Build.quiet(unused => 2)\n"); + let expanded = project.expanded.clone().unwrap(); + assert!(expanded.contains(r#"@warning("-27") (unused => 2)"#), "{expanded}"); + assert_eq!(project.warnings, "", "{}", project.warnings); +} diff --git a/docs/comptime/IMPLEMENTATION.md b/docs/comptime/IMPLEMENTATION.md index 6632f54615..a86d8f1518 100644 --- a/docs/comptime/IMPLEMENTATION.md +++ b/docs/comptime/IMPLEMENTATION.md @@ -28,6 +28,13 @@ compiler. Read that first. `res.hole` with the expression as payload, where an expression, a type or a pattern is expected. The scanner did not accept `$` outside template strings before, so no existing source changes meaning. +- `${...expr}` is a splice hole, the extension `res.hole.splice`. It parses + where a hole does, and in two places of its own: as a row of a record + (`{a: 1, ${...rest}}`, a field named `res.hole.splice` whose value is the + extension) and as a case of a switch (a case without a bar, with the + pattern `_` and the extension as its right-hand side). A block does not continue into a splice + hole, so one after a case is the next case and not an expression of that + case's body. - `%code(...)` has the ordinary payload of an extension: an expression or structure items. `%type(...)` takes a type and `%pat(...)` a pattern. - An extension whose name starts with an uppercase letter takes @@ -145,6 +152,27 @@ that stands where a whole declaration does. A quote of declarations lowers to `__quoteDecls`. At run time a quote is string concatenation, with parentheses around each filled hole except a declaration. +A splice hole is lowered by the list it is an item of, which the scan of a +quote records for each one (`SplicePlace`): + +| The list | The hole becomes | Its argument | +| --- | --- | --- | +| Arguments of a call or a constructor, items of an array or a tuple | `Macro.__spliceItems(items, trailing)` | `array>` | +| Fields of a record | `Macro.__spliceEntries(entries, trailing)` | `array` | +| Cases of a switch | `Macro.__spliceCases(cases)` | `array<(pat, code<'a>)>` | + +The operands of an operator are an application as well, and are not a list: +`1 + ${...xs}` is not a place for a splice. The comma after a hole of items +or entries belongs to the hole (`trailing`), which writes it when it has +items: a hole with none leaves no comma in the text. A splice hole in any +other place is left in the lowered code, where `quote::stray_splice` finds +it and expansion reports it at its position, as it does for one outside a +quote. + +A record value is built from entries (`Code.entry`, `Code.entryOf`, +`Code.spread`): `Code.record` and a splice hole in a record both take them, +and both put a spread before the fields, where the syntax wants it. + While it copies a piece, the lowering puts the file's module name in front of the top-level value and module names of the macro's own file, so that `encode(v)` inside a quote in `Json.res` reads `Json.encode(v)` at the use. diff --git a/docs/comptime/README.md b/docs/comptime/README.md index 0afb35deb4..8bb18e781b 100644 --- a/docs/comptime/README.md +++ b/docs/comptime/README.md @@ -84,6 +84,9 @@ code. `%type(...)` and `%pat(...)` quote a type and a pattern. A quote is checked for syntax where it is written. Its types are checked where the macro is used, after the holes are filled. +A hole stands for one thing. `${...items}` is a splice hole: it stands for +any number of items of a list. See [Lists in a quote](#lists-in-a-quote). + ```rescript @@ -276,10 +279,9 @@ let fromJson = (json: code): code<'a> => ) | Record({fields}) => Code.record( - fields->Array.map(f => ( - f.name, - %code((fromJson(field(${json}, ${Code.string(f.jsName)})): ${f.typ})), - )), + fields->Array.map(f => + Code.entryOf(f, %code((fromJson(field(${json}, ${Code.string(f.jsName)})): ${f.typ}))) + ), ) // ... } @@ -290,6 +292,10 @@ let user: user = Decode.fromJson(json) +`Code.entryOf(f, value)` is the field `f` of a record value. The next +sections are about it: [a record built from data](#a-record-built-from-data) +and [optional fields](#optional-fields). + The same idea gives a value for every type: @@ -322,6 +328,90 @@ let equal = (a: code<'a>, b: code<'a>): code => +### A record built from data + +A quote has the shape it is written with. A record whose fields are known +only when the macro runs is built from entries, one per field. +`Code.entry(name, value)` is `name: value` and `Code.spread(value)` is +`...value`. The values can have different types: the record is type checked +where the macro is used. + + + +```rescript +// Convert.res: a record of the expected type, from the fields of another +@generic +let to = (value: code<'a>): code<'b> => + switch (Type.info(typeOf(value)), Type.info(expected())) { + | (Record({fields: sources}), Record({fields: targets})) => + Code.record( + targets->Array.filterMap(target => { + let read = value->Code.field(target.name) + switch sources->Array.find(source => source.name == target.name) { + | Some({optional: false}) => Some(Code.entry(target.name, read)) + | Some(_) if target.optional => Some(Code.entry(target.name, read, ~optional=true)) + | None if target.optional => None + // ... the other two are errors at the call + } + }), + ) + | _ => error("Convert.to converts a record to a record") + } + +// Main.res +let shown: publicUser = Convert.to(ada) +``` + + + +`~optional=true` writes `name: ?value`: the value is an option, and `None` +leaves the field out of the record. + +### Lists in a quote + +`${...items}` is a splice hole. It stands for any number of items of a list +in a quote, and the rest of the quote stays ordinary code. What it takes +follows from the list it is in: + +| The list | The hole takes | | +| --- | --- | --- | +| Arguments of a call or a constructor, items of an array or a tuple | `array>` | `f(${...args})`, `[first, ${...rest}]` | +| Fields of a record | `array` | `{id: 1, ${...fields}}` | +| Cases of a switch | `array<(pat, code<'a>)>` | `switch x { ${...cases} }` | + + + +```rescript +// Fields of a record, after fields the quote writes itself. +@generic +let withKey = (value: code<'a>, key: code): code<'b> => + switch Type.info(typeOf(value)) { + | Record({fields}) => + let rest = fields->Array.map(f => Code.entryOf(f, value->Code.field(f.name))) + %code({key: ${key}, version: 1, ${...rest}}) + | _ => error("Splice.withKey needs a record") + } + +// Cases of a switch, before a case of the quote. +let cases = constructors->Array.map(c => ( + Pat.string(String.toLowerCase(c.name)), + %code(Some(${Code.ident(c.name)})), +)) +%code( + switch String.toLowerCase(${text}) { + ${...cases} + | _ => None + } +) +``` + + + +A splice hole with no items leaves nothing behind, the comma after it +included. Items of different types go in one array as `Code.untyped(item)`. +A splice hole that is not in one of these lists is an error where it is +written. + ### Declarations from a type A macro attached to a type receives the declaration and returns @@ -335,17 +425,18 @@ let derive = (t: Type.decl): decls => switch Type.info(t.typ) { | Variant({constructors}) => let label = (c: Type.constructor) => c.attrs->Attr.string("Enum.label")->Option.getOr(c.jsName) - let toString = Code.switch_( - %code(value), - constructors->Array.map(c => { - let (pattern, _) = Pat.constructor(c) - (pattern, Code.string(label(c))) - }), - ) + // One case per constructor: a pattern and the code of the case. + let names = constructors->Array.map(c => { + let (pattern, _) = Pat.constructor(c) + (pattern, Code.string(label(c))) + }) %code( - let ${Pat.var(t.name ++ "ToString")} = (value: ${t.typ}): string => ${toString} + let ${Pat.var(t.name ++ "ToString")} = (value: ${t.typ}): string => + switch value { + ${...names} + } let ${Pat.var(t.name ++ "Values")}: array<${t.typ}> = - ${Code.array(constructors->Array.map(c => Code.ident(c.name)))} + [${...constructors->Array.map(c => Code.ident(c.name))}] ) | _ => error("Enum.derive needs a variant type") } @@ -386,7 +477,7 @@ A macro in a type position takes types and returns a type. @macro let t = (source: typ): typ => switch Type.info(source) { - | Record({fields}) => Type.record(fields->Array.map(f => {...f, optional: true})) + | Record({fields}) => Type.record(fields->Array.map(Type.optional)) | _ => error(`Partial.t needs a record type, and ${Type.name(source)} is not one`) } @@ -396,6 +487,89 @@ type userPatch = %Partial.t(user) +### Optional fields + +The `typ` of a field is the type of `value.field`. For `nickname?: string` +that is `option`, and `optional` is true. Everything that reads, +writes, binds or declares a field agrees with that type: + +| | For `nickname?: string` | +| --- | --- | +| `value->Code.field(f.name)` | Code of type `option`. | +| `Code.entryOf(f, value)` | `nickname: ?value`, for a value of type `option`. `None` leaves the field out. | +| `Pat.constructor(c)` | Binds the field of an inline record as an `option`. | +| `Type.record([f])` | Declares `nickname?: string`. | +| `Type.optional(f)`, `Type.required(f)` | `age?: int` from `age: int`, and back. The `typ` follows. | + +A macro that treats every field the same is right for optional fields +without a special case. It looks at `f.optional` when it wants to do +something else for them. + + + +```rescript +// %Patch.t(user): every field optional +@macro +let t = (source: typ): typ => + switch Type.info(source) { + | Record({fields}) => Type.record(fields->Array.map(Type.optional)) + | _ => error(`Patch.t needs a record type, and ${Type.name(source)} is not one`) + } + +// Patch.apply(value, patch): the fields of the patch that have a value +@generic +let apply = (value: code<'a>, patch: code<'p>): code<'a> => + switch Type.info(typeOf(value)) { + | Record({fields}) => + Code.record( + fields->Array.map(f => { + let current = value->Code.field(f.name) + let change = patch->Code.field(f.name) + Code.entryOf( + f, + f.optional ? %code(Option.orElse(${change}, ${current})) : %code(Option.getOr(${change}, ${current})), + ) + }), + ) + | _ => error("Patch.apply needs a record") + } +``` + + + +A field that is made optional by hand, `{...f, optional: true}`, keeps a +`typ` that is not an option. `Type.record` reports that and names +`Type.optional`. + +### Attributes on generated code + +An attribute belongs to an expression, and generated code is an expression: +a quote writes the attribute in front of it. A field of a record value has +no place for an attribute, so it goes on the value of the field, or on the +whole record. + + + +```rescript +// Mock.make(): a record of functions that fail when they are called. Their +// parameters are not used, and the user of the macro gets no warning for it. +| Function({params}) => + let named = params->Array.mapWithIndex((typ, i) => Code.param("arg" ++ Int.toString(i + 1), typ)) + let stub = Code.fn(named, %code(throw(Failure(${Code.string(f.name ++ " is not mocked")})))) + Code.entryOf(f, %code(@warning("-27") ${stub})) + +// An attribute the macro computes: `@warning("-27") value` +@macro +let allow = (warning: int, value: code<'a>): code<'a> => + Code.withAttr(value, "warning", ~payload=Code.string("-" ++ Int.toString(warning))) +``` + + + +A hole cannot stand in an attribute: `Code.withAttr` is for a name or a +payload that is computed. The attributes of a field of a record type are +data, the `attrs` and the `jsName` of a `Type.field`. + ### Patterns and checked literals A macro that returns `pat` is used where a pattern is written. A macro that @@ -571,7 +745,7 @@ it is in the expanded source. So is generated code that does not parse: ## Reference -### The seven forms +### The forms | Form | Where | Meaning | | --- | --- | --- | @@ -581,6 +755,7 @@ it is in the expanded source. So is generated code that does not parse: | `%type(...)` | expression | A quoted type. | | `%pat(...)` | expression | A quoted pattern. | | `${...}` | inside a quote | A hole: an expression of type `code`, `typ` or `pat`, by its position. | +| `${...items}` | in a list of a quote | A splice hole: an array of the items of the list. The three dots are written. | | `%comptime(expr)` | expression | The value of `expr`, computed while compiling. | Everything else is the module `Macro` of the standard library @@ -627,9 +802,14 @@ compile-time parameter has to be called directly. | `Type.name(t)` | The type as source text: `array`. | | `Type.companion(t, name)` | The value `name` in the module that defines `t`, as code. | | `Type.record(fields)`, `Type.variant(constructors)`, `Type.field`, `Type.constructor` | Build a type. | +| `Type.option(t)` | `option`. | +| `Type.optional(field)`, `Type.required(field)` | The field as `age?: int`, and as `age: int`. | | `Code.string`, `int`, `float`, `bool`, `unit` | Literals. | | `Code.ident(name)` | A name that is resolved where the macro is used. | | `Code.field`, `array`, `tuple`, `record`, `call`, `switch_`, `fn` | Build code whose shape depends on data: a field access, a switch with one arm per constructor, a function with one parameter per name. | +| `Code.entry(name, value, ~optional=?)`, `Code.entryOf(field, value)`, `Code.spread(value)` | The entries `Code.record` and a splice hole in a record take: `name: value`, `name: ?value`, `...value`. | +| `Code.withAttr(value, name, ~payload=?)` | `@name(payload) value`. | +| `Code.untyped(code)` | The same code as `code`, to put code of different types in one array. | | `Code.print(code)` | The source text of a piece of code. | | `Code.raw(text)` | Code from source text. Nothing checks it until the macro's result is compiled. | | `Code.decls(items)` | Several declarations in a row. | @@ -653,8 +833,9 @@ type info = | Unknown // inference has not settled it ``` -A `field` has `name`, `jsName` (the `@as` name), `typ`, `optional`, -`mutable_` and `attrs`. A `constructor` has `name`, `jsName`, `args`, +A `field` has `name`, `jsName` (the `@as` name), `typ` (the type of +`value.name`: an option for an optional field), `optional`, `mutable_` and +`attrs`. A `constructor` has `name`, `jsName`, `args`, `fields` (of an inline record) and `attrs`. ### Rules @@ -782,6 +963,9 @@ implemented: - **A quote is checked where it is used.** An error in generated code names the place in the quote, but only once a use expands it: a macro whose quote cannot have the type the macro declares compiles until then. +- **Splice holes in patterns and types.** `${...items}` stands in lists of + expressions. The fields of a record type are built with `Type.record`, + and the arguments of a constructor pattern with `Pat.constructor`. - **Attribute payloads beyond one string.** `@Json.key("user_age")` reaches the macro; `@Json.codec({strict: true})` does not yet pass its argument. - **Parametric types.** `Type.info` of `result` or a record with type diff --git a/packages/@rescript/runtime/Macro.res b/packages/@rescript/runtime/Macro.res index 1e05fb6e0d..d0ca3c5c46 100644 --- a/packages/@rescript/runtime/Macro.res +++ b/packages/@rescript/runtime/Macro.res @@ -28,9 +28,10 @@ type decls = code /* The representation the compiler and the engine share: the source text, and for a type the compiler's handle of it. `m` says which parts of the text are text from elsewhere, an argument of the macro or a quote: the - compiler reports an error in generated code with it. */ + compiler reports an error in generated code with it. `o`: the type + inside, for an option type a macro built with `Type.option`. */ type span -type rec node = {c: string, m?: array, id?: int, t?: node, raw?: bool} +type rec node = {c: string, m?: array, id?: int, t?: node, raw?: bool, o?: node} external toNode: 'a => node = "%identity" external ofNode: node => 'a = "%identity" @@ -146,6 +147,39 @@ let __quoteDecls = (pieces, holes, file): decls => ofNode(quote(pieces, holes, f let __quoteType = (pieces, holes, file): typ => ofNode(quote(pieces, holes, file)) let __quotePat = (pieces, holes, file): pat => ofNode(quote(pieces, holes, file)) +/* Splice holes. `${...items}` in a list of a quote stands for any number + of its items; the compiler picks the function by the list. `trailing`: + a comma follows the hole. That comma is the hole's own, so that a hole + with no items leaves no comma behind. */ +let splice = (items: array, trailing: bool): hole => + ofNode({...cat([join(items, ", "), text(trailing && items->Array.length > 0 ? "," : "")]), raw: true}) + +/** A field of a record value: see `Code.entry`. */ +type recordEntry + +/* The spread of a record comes before its fields. */ +let entryNodes = (entries: array): array => { + let nodes = entries->Array.map(toNode) + let isSpread = (node: node) => node.c->String.startsWith("...") + let spreads = nodes->Array.filter(isSpread) + if spreads->Array.length > 1 { + host->fail( + "A record can have one spread, and these fields have more: " ++ spreads->Array.map(node => node.c)->Array.join(", "), + None, + ) + } else { + spreads->Array.concat(nodes->Array.filter(node => !isSpread(node))) + } +} + +let caseNode = ((pattern, body): (pat, code<'a>)): node => + cat([text("| "), toNode(pattern), text(" => "), toNode(body)]) + +let __spliceItems = (items: array>, trailing: bool): hole => splice(items->Array.map(toNode), trailing) +let __spliceEntries = (entries: array, trailing: bool): hole => splice(entryNodes(entries), trailing) +let __spliceCases = (cases: array<(pat, code<'a>)>): hole => + ofNode({...join(cases->Array.map(caseNode), "\n"), raw: true}) + let isSimple: string => bool = %raw(`function (text) { return /^[A-Za-z_][A-Za-z0-9_$.]*$/.test(text) }`) @@ -179,7 +213,13 @@ module Type = { name: string, /** The name at run time: the `@as` name when there is one. */ jsName: string, + /** + The type of `value.name`. For an optional field `age?: int` that is + `option`: reading the field, `Code.entryOf` and `Pat.constructor` + all use this type. + */ typ: typ, + /** `age?: int`. Change it with `Type.optional` and `Type.required`. */ optional: bool, mutable_: bool, attrs: array, @@ -252,13 +292,53 @@ module Type = { ) ->Array.join("") + /** `option` */ + let option = (t: typ): typ => ofNode({c: "option<" ++ source(t) ++ ">", o: toNode(t)}) + + /* The `t` of `option`. */ + let optionOf = (t: typ): option => { + let node = toNode(t) + switch (node.o, node.id) { + | (Some(inner), _) => Some(ofNode(inner)) + | (None, Some(_)) => + switch info(t) { + | Option(inner) => Some(inner) + | _ => None + } + | (None, None) => None + } + } + + /* The type an optional field is declared with: `int` for `age?: int`, + where `typ` is `option`. */ + let declared = (field: field): typ => + switch optionOf(field.typ) { + | Some(inner) => inner + | None => + error( + "The optional field `" ++ + field.name ++ + "` has the type `" ++ + source(field.typ) ++ "`, and the type of an optional field is an option. Type.optional makes a field optional.", + ) + } + + /** + The field as an optional one: `age?: int`. Its `typ` becomes + `option`, the type of `value.age`. An optional field stays as it is. + */ + let optional = (field: field): field => + field.optional ? field : {...field, optional: true, typ: option(field.typ)} + + /** The field as a required one: `age: int` for `age?: int`. */ + let required = (field: field): field => + field.optional ? {...field, optional: false, typ: declared(field)} : field + let fieldText = (field: field): string => attrText(field.attrs) ++ (field.jsName != field.name ? "@as(" ++ JSON.stringify(JSON.Encode.string(field.jsName)) ++ ") " : "") ++ (field.mutable_ ? "mutable " : "") ++ - field.name ++ - (field.optional ? "?: " : ": ") ++ - source(field.typ) + field.name ++ (field.optional ? "?: " ++ source(declared(field)) : ": " ++ source(field.typ)) /** A record type with these fields. */ let record = (fields: array): typ => make("{" ++ fields->Array.map(fieldText)->Array.join(", ") ++ "}") @@ -333,30 +413,49 @@ module Code = { let tuple = (items: array>): code<'b> => build([text("("), join(items->Array.map(toNode), ", "), text(")")]) - /** `{name: value, ...}` */ - let record = (fields: array<(string, code<'a>)>): code<'b> => + /** The same code without its type, to put code of different types in one array. */ + external untyped: code<'a> => code = "%identity" + + /** `@name value`, or `@name(payload) value`. */ + let withAttr = (value: code<'a>, name: string, ~payload: option>=?): code<'a> => build([ - text("{"), - join(fields->Array.map(((name, value)) => cat([text(name ++ ": "), toNode(value)])), ", "), - text("}"), + text("@" ++ name), + switch payload { + | Some(payload) => cat([text("("), toNode(payload), text(") ")]) + | None => text(" ") + }, + atomNode(toNode(value)), ]) + /** A field of a record value: `name: value`, `name: ?value` or `...value`. */ + type entry = recordEntry + + /** + `name: value`. With `~optional=true`, `name: ?value`: the value is an + option, and `None` leaves the field out. + */ + let entry = (name: string, value: code<'a>, ~optional=false): entry => + build([text(name ++ (optional ? ": ?" : ": ")), optional ? atomNode(toNode(value)) : toNode(value)]) + + /** + The entry of a field of a record type, for a value of the type `field.typ`: + `name: ?value` when the field is optional, `name: value` otherwise. + */ + let entryOf = (field: Type.field, value: code<'a>): entry => entry(field.name, value, ~optional=field.optional) + + /** `...value`: the fields of another record of the same type. */ + let spread = (value: code<'a>): entry => build([text("..."), atomNode(toNode(value))]) + + /** `{name: value, ...}` */ + let record = (entries: array): code<'a> => build([text("{"), join(entryNodes(entries), ", "), text("}")]) + /** `f(a, b)` */ let call = (f: code<'a>, args: array>): code<'c> => build([atomNode(toNode(f)), text("("), join(args->Array.map(toNode), ", "), text(")")]) /** `switch value { | pattern => body ... }` */ let switch_ = (value: code<'a>, arms: array<(pat, code<'b>)>): code<'b> => - build([ - text("switch "), - atomNode(toNode(value)), - text(" {\n"), - join( - arms->Array.map(((pattern, body)) => cat([text("| "), toNode(pattern), text(" => "), toNode(body)])), - "\n", - ), - text("\n}"), - ]) + build([text("switch "), atomNode(toNode(value)), text(" {\n"), join(arms->Array.map(caseNode), "\n"), text("\n}")]) /** A parameter of `fn`. */ type param = string @@ -400,7 +499,10 @@ module Pat = { make( c.name ++ "({" ++ - c.fields->Array.mapWithIndex((f, i) => f.name ++ ": " ++ names->Array.getUnsafe(i))->Array.join(", ") ++ "})", + c.fields + // `?`: the name is bound to the option of an optional field, absent or not. + ->Array.mapWithIndex((f, i) => f.name ++ (f.optional ? ": ?" : ": ") ++ names->Array.getUnsafe(i)) + ->Array.join(", ") ++ "})", ), c.fields->Array.mapWithIndex((f, i) => typed(names->Array.getUnsafe(i), f.typ)), ) diff --git a/packages/@rescript/runtime/lib/es6/Macro.mjs b/packages/@rescript/runtime/lib/es6/Macro.mjs index e689b651aa..9329e3bcea 100644 --- a/packages/@rescript/runtime/lib/es6/Macro.mjs +++ b/packages/@rescript/runtime/lib/es6/Macro.mjs @@ -133,6 +133,55 @@ function __quotePat(pieces, holes, file) { return quote(pieces, holes, file); } +function splice(items, trailing) { + return { + ...cat([ + join(items, ", "), + text(trailing && items.length !== 0 ? "," : "") + ]), + raw: true + }; +} + +function entryNodes(entries) { + let nodes = entries.map(prim => prim); + let isSpread = node => node.c.startsWith("..."); + let spreads = nodes.filter(isSpread); + if (spreads.length > 1) { + return __rescript_macro.fail("A record can have one spread, and these fields have more: " + spreads.map(node => node.c).join(", "), undefined); + } else { + return spreads.concat(nodes.filter(node => !node.c.startsWith("..."))); + } +} + +function caseNode(param) { + return cat([ + { + c: "| " + }, + param[0], + { + c: " => " + }, + param[1] + ]); +} + +function __spliceItems(items, trailing) { + return splice(items.map(prim => prim), trailing); +} + +function __spliceEntries(entries, trailing) { + return splice(entryNodes(entries), trailing); +} + +function __spliceCases(cases) { + return { + ...join(cases.map(caseNode), "\n"), + raw: true + }; +} + let isSimple = (function (text) { return /^[A-Za-z_][A-Za-z0-9_$.]*$/.test(text) }); @@ -214,14 +263,77 @@ function attrText(attrs) { }).join(""); } +function option(t) { + return { + c: "option<" + t.c + ">", + o: t + }; +} + +function optionOf(t) { + let match = t.o; + let match$1 = t.id; + if (match !== undefined) { + return Primitive_option.some(match); + } + if (match$1 === undefined) { + return; + } + let inner = info(t); + if (typeof inner !== "object" || inner.TAG !== "Option") { + return; + } else { + return Primitive_option.some(inner._0); + } +} + +function declared(field) { + let inner = optionOf(field.typ); + if (inner !== undefined) { + return Primitive_option.valFromOption(inner); + } else { + return __rescript_macro.fail("The optional field `" + field.name + "` has the type `" + field.typ.c + "`, and the type of an optional field is an option. Type.optional makes a field optional.", undefined); + } +} + +function optional(field) { + if (field.optional) { + return field; + } else { + return { + name: field.name, + jsName: field.jsName, + typ: option(field.typ), + optional: true, + mutable_: field.mutable_, + attrs: field.attrs + }; + } +} + +function required(field) { + if (field.optional) { + return { + name: field.name, + jsName: field.jsName, + typ: declared(field), + optional: false, + mutable_: field.mutable_, + attrs: field.attrs + }; + } else { + return field; + } +} + function fieldText(field) { return attrText(field.attrs) + ( field.jsName !== field.name ? "@as(" + JSON.stringify(field.jsName) + ") " : "" ) + ( field.mutable_ ? "mutable " : "" ) + field.name + ( - field.optional ? "?: " : ": " - ) + field.typ.c; + field.optional ? "?: " + declared(field).c : ": " + field.typ.c + ); } function record(fields) { @@ -266,6 +378,11 @@ let Type = { name: name, companion: companion, attrText: attrText, + option: option, + optionOf: optionOf, + declared: declared, + optional: optional, + required: required, fieldText: fieldText, record: record, variant: variant, @@ -333,15 +450,53 @@ function tuple(items) { ]); } -function record$1(fields) { +function withAttr(value, name, payload) { + return cat([ + text("@" + name), + payload !== undefined ? cat([ + { + c: "(" + }, + Primitive_option.valFromOption(payload), + { + c: ") " + } + ]) : ({ + c: " " + }), + atomNode(value) + ]); +} + +function entry(name, value, optionalOpt) { + let optional = optionalOpt !== undefined ? optionalOpt : false; + return cat([ + text(name + ( + optional ? ": ?" : ": " + )), + optional ? atomNode(value) : value + ]); +} + +function entryOf(field, value) { + return entry(field.name, value, field.optional); +} + +function spread(value) { + return cat([ + { + c: "..." + }, + atomNode(value) + ]); +} + +function record$1(entries) { return cat([ { c: "{" }, - join(fields.map(param => cat([ - text(param[0] + ": "), - param[1] - ])), ", "), + join(entryNodes(entries), ", "), { c: "}" } @@ -370,16 +525,7 @@ function switch_(value, arms) { { c: " {\n" }, - join(arms.map(param => cat([ - { - c: "| " - }, - param[0], - { - c: " => " - }, - param[1] - ])), "\n"), + join(arms.map(caseNode), "\n"), { c: "\n}" } @@ -417,6 +563,10 @@ let Code = { field: field$1, array: array, tuple: tuple, + withAttr: withAttr, + entry: entry, + entryOf: entryOf, + spread: spread, record: record$1, call: call, switch_: switch_, @@ -448,7 +598,9 @@ function constructor$1(c) { if (c.fields.length !== 0) { let names = c.fields.map((f, i) => f.name + "$" + i.toString()); return [ - make(c.name + "({" + c.fields.map((f, i) => f.name + ": " + names[i]).join(", ") + "})"), + make(c.name + "({" + c.fields.map((f, i) => f.name + ( + f.optional ? ": ?" : ": " + ) + names[i]).join(", ") + "})"), c.fields.map((f, i) => typed(names[i], f.typ)) ]; } @@ -494,6 +646,12 @@ export { __quoteDecls, __quoteType, __quotePat, + splice, + entryNodes, + caseNode, + __spliceItems, + __spliceEntries, + __spliceCases, isSimple, atom, atomNode, diff --git a/packages/@rescript/runtime/lib/js/Macro.cjs b/packages/@rescript/runtime/lib/js/Macro.cjs index 2bcca5c0d7..74c224e2f5 100644 --- a/packages/@rescript/runtime/lib/js/Macro.cjs +++ b/packages/@rescript/runtime/lib/js/Macro.cjs @@ -133,6 +133,55 @@ function __quotePat(pieces, holes, file) { return quote(pieces, holes, file); } +function splice(items, trailing) { + return { + ...cat([ + join(items, ", "), + text(trailing && items.length !== 0 ? "," : "") + ]), + raw: true + }; +} + +function entryNodes(entries) { + let nodes = entries.map(prim => prim); + let isSpread = node => node.c.startsWith("..."); + let spreads = nodes.filter(isSpread); + if (spreads.length > 1) { + return __rescript_macro.fail("A record can have one spread, and these fields have more: " + spreads.map(node => node.c).join(", "), undefined); + } else { + return spreads.concat(nodes.filter(node => !node.c.startsWith("..."))); + } +} + +function caseNode(param) { + return cat([ + { + c: "| " + }, + param[0], + { + c: " => " + }, + param[1] + ]); +} + +function __spliceItems(items, trailing) { + return splice(items.map(prim => prim), trailing); +} + +function __spliceEntries(entries, trailing) { + return splice(entryNodes(entries), trailing); +} + +function __spliceCases(cases) { + return { + ...join(cases.map(caseNode), "\n"), + raw: true + }; +} + let isSimple = (function (text) { return /^[A-Za-z_][A-Za-z0-9_$.]*$/.test(text) }); @@ -214,14 +263,77 @@ function attrText(attrs) { }).join(""); } +function option(t) { + return { + c: "option<" + t.c + ">", + o: t + }; +} + +function optionOf(t) { + let match = t.o; + let match$1 = t.id; + if (match !== undefined) { + return Primitive_option.some(match); + } + if (match$1 === undefined) { + return; + } + let inner = info(t); + if (typeof inner !== "object" || inner.TAG !== "Option") { + return; + } else { + return Primitive_option.some(inner._0); + } +} + +function declared(field) { + let inner = optionOf(field.typ); + if (inner !== undefined) { + return Primitive_option.valFromOption(inner); + } else { + return __rescript_macro.fail("The optional field `" + field.name + "` has the type `" + field.typ.c + "`, and the type of an optional field is an option. Type.optional makes a field optional.", undefined); + } +} + +function optional(field) { + if (field.optional) { + return field; + } else { + return { + name: field.name, + jsName: field.jsName, + typ: option(field.typ), + optional: true, + mutable_: field.mutable_, + attrs: field.attrs + }; + } +} + +function required(field) { + if (field.optional) { + return { + name: field.name, + jsName: field.jsName, + typ: declared(field), + optional: false, + mutable_: field.mutable_, + attrs: field.attrs + }; + } else { + return field; + } +} + function fieldText(field) { return attrText(field.attrs) + ( field.jsName !== field.name ? "@as(" + JSON.stringify(field.jsName) + ") " : "" ) + ( field.mutable_ ? "mutable " : "" ) + field.name + ( - field.optional ? "?: " : ": " - ) + field.typ.c; + field.optional ? "?: " + declared(field).c : ": " + field.typ.c + ); } function record(fields) { @@ -266,6 +378,11 @@ let Type = { name: name, companion: companion, attrText: attrText, + option: option, + optionOf: optionOf, + declared: declared, + optional: optional, + required: required, fieldText: fieldText, record: record, variant: variant, @@ -333,15 +450,53 @@ function tuple(items) { ]); } -function record$1(fields) { +function withAttr(value, name, payload) { + return cat([ + text("@" + name), + payload !== undefined ? cat([ + { + c: "(" + }, + Primitive_option.valFromOption(payload), + { + c: ") " + } + ]) : ({ + c: " " + }), + atomNode(value) + ]); +} + +function entry(name, value, optionalOpt) { + let optional = optionalOpt !== undefined ? optionalOpt : false; + return cat([ + text(name + ( + optional ? ": ?" : ": " + )), + optional ? atomNode(value) : value + ]); +} + +function entryOf(field, value) { + return entry(field.name, value, field.optional); +} + +function spread(value) { + return cat([ + { + c: "..." + }, + atomNode(value) + ]); +} + +function record$1(entries) { return cat([ { c: "{" }, - join(fields.map(param => cat([ - text(param[0] + ": "), - param[1] - ])), ", "), + join(entryNodes(entries), ", "), { c: "}" } @@ -370,16 +525,7 @@ function switch_(value, arms) { { c: " {\n" }, - join(arms.map(param => cat([ - { - c: "| " - }, - param[0], - { - c: " => " - }, - param[1] - ])), "\n"), + join(arms.map(caseNode), "\n"), { c: "\n}" } @@ -417,6 +563,10 @@ let Code = { field: field$1, array: array, tuple: tuple, + withAttr: withAttr, + entry: entry, + entryOf: entryOf, + spread: spread, record: record$1, call: call, switch_: switch_, @@ -448,7 +598,9 @@ function constructor$1(c) { if (c.fields.length !== 0) { let names = c.fields.map((f, i) => f.name + "$" + i.toString()); return [ - make(c.name + "({" + c.fields.map((f, i) => f.name + ": " + names[i]).join(", ") + "})"), + make(c.name + "({" + c.fields.map((f, i) => f.name + ( + f.optional ? ": ?" : ": " + ) + names[i]).join(", ") + "})"), c.fields.map((f, i) => typed(names[i], f.typ)) ]; } @@ -493,6 +645,12 @@ exports.__quoteCode = __quoteCode; exports.__quoteDecls = __quoteDecls; exports.__quoteType = __quoteType; exports.__quotePat = __quotePat; +exports.splice = splice; +exports.entryNodes = entryNodes; +exports.caseNode = caseNode; +exports.__spliceItems = __spliceItems; +exports.__spliceEntries = __spliceEntries; +exports.__spliceCases = __spliceCases; exports.isSimple = isSimple; exports.atom = atom; exports.atomNode = atomNode; diff --git a/playground/examples/attrs/Main.res b/playground/examples/attrs/Main.res new file mode 100644 index 0000000000..d234663912 --- /dev/null +++ b/playground/examples/attrs/Main.res @@ -0,0 +1,31 @@ +type user = {id: int, name: string} + +type api = { + load: int => user, + save: (user, bool) => unit, + count: unit => int, +} + +// Every field is `@warning("-27") (arg1: ...) => throw(...)`. +let mock: api = Mock.make() + +// A test replaces what it uses. The rest fails when it is reached. +let api = {...mock, count: () => 3} +Console.log(api.count()) +Console.log( + try api.load(1).name catch { + | Failure(message) => message + }, +) + +// The attribute on a whole record, with the number as an argument: +// `user` and `notify` are not used, and nothing is reported. +let silent: api = %Mock.allow( + 27, + { + load: id => {id, name: "nobody"}, + save: (user, notify) => (), + count: () => 0, + }, +) +Console.log(silent.load(7)) diff --git a/playground/examples/attrs/Mock.res b/playground/examples/attrs/Mock.res new file mode 100644 index 0000000000..b31de5e38e --- /dev/null +++ b/playground/examples/attrs/Mock.res @@ -0,0 +1,35 @@ +open Macro + +// Mock.make() is a record of functions that fail when they are called: +// what a test starts from, before it replaces the functions it uses. +// +// The parameters of a generated function have names, to read well in the +// expansion, and are not used: the compiler would warn about each one. +// The macro puts @warning("-27") on the function, so whoever uses the +// macro gets no warning for code they did not write. +// +// An attribute belongs to an expression. A field of a record has no place +// for one, so it goes on the field's value. +@generic +let make = (_: code): code<'a> => + switch Type.info(expected()) { + | Record({fields}) => + Code.record( + fields->Array.map(f => + switch Type.info(f.typ) { + | Function({params}) => + let named = params->Array.mapWithIndex((typ, i) => Code.param("arg" ++ Int.toString(i + 1), typ)) + let stub = Code.fn(named, %code(throw(Failure(${Code.string(f.name ++ " is not mocked")})))) + Code.entryOf(f, %code(@warning("-27") ${stub})) + | _ => error(`Mock.make: the field ${f.name} is not a function`) + } + ), + ) + | _ => error("Mock.make fills a record of functions") + } + +// An attribute a quote cannot write, because the macro computes it: +// Code.withAttr(value, name, ~payload) is `@name(payload) value`. +@macro +let allow = (warning: int, value: code<'a>): code<'a> => + Code.withAttr(value, "warning", ~payload=Code.string("-" ++ Int.toString(warning))) diff --git a/playground/examples/decode/Decode.res b/playground/examples/decode/Decode.res index 8176d24b7c..2040df9ad5 100644 --- a/playground/examples/decode/Decode.res +++ b/playground/examples/decode/Decode.res @@ -51,11 +51,12 @@ let fromJson = (json: code): code<'a> => } ) | Record({fields}) => + // `entryOf` writes `name: value`, and `name: ?value` for an optional + // field: its type is an option, and a key that is missing leaves it out. Code.record( - fields->Array.map(f => ( - f.name, - %code((fromJson(field(${json}, ${Code.string(f.jsName)})): ${f.typ})), - )), + fields->Array.map(f => + Code.entryOf(f, %code((fromJson(field(${json}, ${Code.string(f.jsName)})): ${f.typ}))) + ), ) | _ => error(`Decode.fromJson cannot decode to ${Type.name(expected())}`) } diff --git a/playground/examples/decode/Main.res b/playground/examples/decode/Main.res index fdc43a5225..1c4d405bdf 100644 --- a/playground/examples/decode/Main.res +++ b/playground/examples/decode/Main.res @@ -4,6 +4,7 @@ type user = { name: string, age: int, email: option, + nickname?: string, addresses: array
, } diff --git a/playground/examples/default/Default.res b/playground/examples/default/Default.res index ad8e831da6..a08e4c955b 100644 --- a/playground/examples/default/Default.res +++ b/playground/examples/default/Default.res @@ -10,7 +10,7 @@ let value = (_: code): code<'a> => | Bool => %code(false) | Array(_) => %code([]) | Option(_) => %code(None) - | Record({fields}) => Code.record(fields->Array.map(f => (f.name, %code(value())))) + | Record({fields}) => Code.record(fields->Array.map(f => Code.entryOf(f, %code(value())))) | Variant({constructors}) => switch constructors->Array.find(c => c.args->Array.length == 0 && c.fields->Array.length == 0) { | Some(c) => Code.ident(c.name) diff --git a/playground/examples/derive/Enum.res b/playground/examples/derive/Enum.res index dd1ca08aeb..b7998f39b4 100644 --- a/playground/examples/derive/Enum.res +++ b/playground/examples/derive/Enum.res @@ -14,20 +14,25 @@ let derive = (t: Type.decl): decls => let label = (c: Type.constructor) => c.attrs->Attr.string("Enum.label")->Option.getOr(c.jsName) let value = (c: Type.constructor): code => Code.ident(c.name) - let toString = Code.switch_( - %code(value), - constructors->Array.map(c => { - let (pattern, _) = Pat.constructor(c) - (pattern, Code.string(label(c))) - }), - ) + // One case per constructor: a pattern and the code of the case. + let names = constructors->Array.map(c => { + let (pattern, _) = Pat.constructor(c) + (pattern, Code.string(label(c))) + }) let known = constructors->Array.map(c => (Pat.string(label(c)), %code(Some(${value(c)})))) - let fromString = Code.switch_(%code(text), [...known, (Pat.any, %code(None))]) + // `${...cases}` puts the cases in a switch of the quote. %code( - let ${Pat.var(t.name ++ "ToString")} = (value: ${t.typ}): string => ${toString} - let ${Pat.var(t.name ++ "FromString")} = (text: string): option<${t.typ}> => ${fromString} - let ${Pat.var(t.name ++ "Values")}: array<${t.typ}> = ${Code.array(constructors->Array.map(value))} + let ${Pat.var(t.name ++ "ToString")} = (value: ${t.typ}): string => + switch value { + ${...names} + } + let ${Pat.var(t.name ++ "FromString")} = (text: string): option<${t.typ}> => + switch text { + ${...known} + | _ => None + } + let ${Pat.var(t.name ++ "Values")}: array<${t.typ}> = [${...constructors->Array.map(value)}] ) | _ => error("Enum.derive needs a variant type") } diff --git a/playground/examples/index.json b/playground/examples/index.json index cff6c61c70..b890122cf8 100644 --- a/playground/examples/index.json +++ b/playground/examples/index.json @@ -109,6 +109,42 @@ "Main.res" ] }, + { + "id": "record", + "title": "A record built from data", + "summary": "Convert.to(user) fills a record of the expected type from the fields of another one, an entry per field.", + "files": [ + "Convert.res", + "Main.res" + ] + }, + { + "id": "splice", + "title": "Splice holes: lists in a quote", + "summary": "${...items} stands for any number of record fields, call arguments or switch cases in a quote.", + "files": [ + "Splice.res", + "Main.res" + ] + }, + { + "id": "optional", + "title": "Optional fields", + "summary": "%Patch.t(user) makes every field optional and Patch.apply merges a patch: optional fields need no special case.", + "files": [ + "Patch.res", + "Main.res" + ] + }, + { + "id": "attrs", + "title": "Attributes on generated code", + "summary": "An attribute on the value of a field, and on a whole record: here @warning(\"-27\") for functions the macro writes.", + "files": [ + "Mock.res", + "Main.res" + ] + }, { "id": "pattern", "title": "Pattern macros and checked literals", diff --git a/playground/examples/optional/Main.res b/playground/examples/optional/Main.res new file mode 100644 index 0000000000..3ae2bcca49 --- /dev/null +++ b/playground/examples/optional/Main.res @@ -0,0 +1,16 @@ +type user = { + name: string, + age: int, + nickname?: string, +} + +// {name?: string, age?: int, nickname?: string} +type userPatch = %Patch.t(user) + +let update = (user: user, patch: userPatch): user => Patch.apply(user, patch) + +let ada: user = {name: "Ada", age: 36} + +Console.log(ada->update({age: 37})) +Console.log(ada->update({nickname: "countess"})) +Console.log(ada->update({nickname: "countess"})->update({name: "Ada Lovelace"})) diff --git a/playground/examples/optional/Patch.res b/playground/examples/optional/Patch.res new file mode 100644 index 0000000000..fb753adbc6 --- /dev/null +++ b/playground/examples/optional/Patch.res @@ -0,0 +1,37 @@ +open Macro + +// The type of an optional field is the type of `value.field`: for +// `nickname?: string` that is option. Reading the field, writing it +// with Code.entryOf and declaring it with Type.record all agree on it, so a +// macro needs no special case to be right for optional fields. + +// %Patch.t(user): the record type with every field optional. +// Type.optional leaves a field that is optional already as it is. +@macro +let t = (source: typ): typ => + switch Type.info(source) { + | Record({fields}) => Type.record(fields->Array.map(Type.optional)) + | _ => error(`Patch.t needs a record type, and ${Type.name(source)} is not one`) + } + +// Patch.apply(value, patch): the record with the fields of the patch that +// have a value. +@generic +let apply = (value: code<'a>, patch: code<'p>): code<'a> => + switch Type.info(typeOf(value)) { + | Record({fields}) => + Code.record( + fields->Array.map(f => { + let current = value->Code.field(f.name) + let change = patch->Code.field(f.name) + // entryOf takes a value of the type f.typ. It writes `name: value`, + // and `name: ?value` for an optional field, which leaves the field + // out of the record when the value is None. + Code.entryOf( + f, + f.optional ? %code(Option.orElse(${change}, ${current})) : %code(Option.getOr(${change}, ${current})), + ) + }), + ) + | _ => error("Patch.apply needs a record") + } diff --git a/playground/examples/record/Convert.res b/playground/examples/record/Convert.res new file mode 100644 index 0000000000..6d1e31516a --- /dev/null +++ b/playground/examples/record/Convert.res @@ -0,0 +1,31 @@ +open Macro + +// Convert.to(value) is a record of the type the call site expects, filled +// from the fields of `value` that have the same names. +// +// Which fields there are is known when the macro runs, not when it is +// written, so the record cannot be one quote. It is built from entries, one +// per field: Code.entry(name, value) is `name: value`. +@generic +let to = (value: code<'a>): code<'b> => + switch (Type.info(typeOf(value)), Type.info(expected())) { + | (Record({fields: sources}), Record({fields: targets})) => + Code.record( + targets->Array.filterMap(target => { + let read = value->Code.field(target.name) + switch sources->Array.find(source => source.name == target.name) { + // `name: value.name` + | Some({optional: false}) => Some(Code.entry(target.name, read)) + // Optional on both sides: `name: ?value.name` passes on an option, + // and leaves the field out when there is no value. + | Some(_) if target.optional => Some(Code.entry(target.name, read, ~optional=true)) + | Some(_) => + error(`Convert.to: ${target.name} is optional in ${Type.name(typeOf(value))} and required in ${Type.name(expected())}`) + // A field that is not there stays out when it may. + | None if target.optional => None + | None => error(`Convert.to: ${Type.name(typeOf(value))} has no field ${target.name}`) + } + }), + ) + | _ => error("Convert.to converts a record to a record") + } diff --git a/playground/examples/record/Main.res b/playground/examples/record/Main.res new file mode 100644 index 0000000000..26f6e83151 --- /dev/null +++ b/playground/examples/record/Main.res @@ -0,0 +1,26 @@ +type user = { + id: int, + name: string, + email: string, + passwordHash: string, + nickname?: string, +} + +// What the API returns: a user without the email and the password hash. +type publicUser = { + id: int, + name: string, + nickname?: string, + avatar?: string, +} + +let ada: user = {id: 1, name: "Ada", email: "ada@example.com", passwordHash: "x91f", nickname: "countess"} +let grace: user = {id: 2, name: "Grace", email: "grace@example.com", passwordHash: "b07c"} + +let shown: publicUser = Convert.to(ada) +Console.log(shown) +Console.log([ada, grace]->Array.map((user): publicUser => Convert.to(user))) + +// A field the source cannot give is a compile error at the call: +// type login = {name: string, password: string} +// let login: login = Convert.to(ada) diff --git a/playground/examples/splice/Main.res b/playground/examples/splice/Main.res new file mode 100644 index 0000000000..0dc317472a --- /dev/null +++ b/playground/examples/splice/Main.res @@ -0,0 +1,19 @@ +type point = {x: float, y: float} +type storedPoint = {key: string, version: int, x: float, y: float} + +let point = {x: 3.0, y: 4.0} + +// {key: "p1", version: 1, x: point.x, y: point.y} +let stored: storedPoint = Splice.withKey(point, "p1") +Console.log(stored) + +// distance(point.x, point.y) +let distance = (x, y) => Math.sqrt(x *. x +. y *. y) +Console.log(Splice.call(distance, point)) + +type level = Debug | Info | Warning | Error + +// One case per constructor, then the case of the quote. +let level: option = Splice.parse("Warning") +let unknown: option = Splice.parse("loud") +Console.log2(level, unknown) diff --git a/playground/examples/splice/Splice.res b/playground/examples/splice/Splice.res new file mode 100644 index 0000000000..9b9dc0c434 --- /dev/null +++ b/playground/examples/splice/Splice.res @@ -0,0 +1,48 @@ +open Macro + +// `${...items}` is a splice hole: it stands for any number of items of a +// list in a quote. The rest of the quote stays ordinary code. + +// Fields of a record, after fields the quote writes itself. +// `rest` is an array of entries, as Code.record takes. +@generic +let withKey = (value: code<'a>, key: code): code<'b> => + switch Type.info(typeOf(value)) { + | Record({fields}) => + let rest = fields->Array.map(f => Code.entryOf(f, value->Code.field(f.name))) + %code({key: ${key}, version: 1, ${...rest}}) + | _ => error("Splice.withKey needs a record") + } + +// Arguments of a call: an array of code. +@generic +let call = (f: code<'f>, value: code<'a>): code<'b> => + switch Type.info(typeOf(value)) { + | Record({fields}) => + let args = fields->Array.map(f => value->Code.field(f.name)) + %code(${f}(${...args})) + | _ => error("Splice.call needs a record") + } + +// Cases of a switch, before a case of the quote: an array of a pattern and +// the code of its case. +@generic +let parse = (text: code): code<'a> => + switch Type.info(expected()) { + | Option(inner) => + switch Type.info(inner) { + | Variant({constructors}) => + let cases = constructors->Array.map(c => ( + Pat.string(String.toLowerCase(c.name)), + %code(Some(${Code.ident(c.name)})), + )) + %code( + switch String.toLowerCase(${text}) { + ${...cases} + | _ => None + } + ) + | _ => error("Splice.parse needs a variant") + } + | _ => error("Splice.parse returns an option") + } diff --git a/playground/examples/types/Partial.res b/playground/examples/types/Partial.res index a0722961a5..946bc4393c 100644 --- a/playground/examples/types/Partial.res +++ b/playground/examples/types/Partial.res @@ -4,6 +4,6 @@ open Macro @macro let t = (source: typ): typ => switch Type.info(source) { - | Record({fields}) => Type.record(fields->Array.map(f => {...f, optional: true})) + | Record({fields}) => Type.record(fields->Array.map(Type.optional)) | _ => error(`Partial.t needs a record type, and ${Type.name(source)} is not one`) }