Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 9 additions & 4 deletions compiler-rust/src/macros/discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -876,6 +876,11 @@ fn fail(tctx: &mut TypeCheckContext<'_>, loc: LocIdx, message: impl Into<String>
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(_))
}
Expand Down Expand Up @@ -1173,7 +1178,7 @@ pub(crate) fn expression_extension(
expected_ty: TypeExprRef,
) -> Option<TypeCoreResult<Expression>> {
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.
Expand Down Expand Up @@ -1688,7 +1693,7 @@ pub(crate) fn pattern_extension(
extension: &Extension,
) -> Option<TypeCoreError> {
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 {
Expand Down Expand Up @@ -1745,7 +1750,7 @@ pub(crate) fn type_extension(
extension: &Extension,
) -> Option<TypeCoreError> {
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 {
Expand Down
11 changes: 11 additions & 0 deletions compiler-rust/src/macros/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
216 changes: 206 additions & 10 deletions compiler-rust/src/macros/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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),
Expand All @@ -162,10 +186,35 @@ struct QuoteScan<'a> {
bound: HashSet<String>,
/// Expressions that are a structure item of the quote on their own.
item_exprs: &'a HashSet<ExprIdx>,
/// The splice holes that are an item of a list, with the list.
splice_places: HashMap<ExprIdx, SplicePlace>,
}

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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
},
});
}
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -310,6 +424,7 @@ impl QuoteFinder<'_> {
} else {
&item_exprs
},
splice_places: HashMap::new(),
};
scan.payload(arena, &extension.1);

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Loading
Loading