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
97 changes: 97 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,102 @@
# Changelog

## Unreleased

### Added

- `map_size` of a `HashMap`, `MapSet.size`, and `length` are Rust's `len()`,
a `usize` where one is expected; so is arithmetic on them there.
- `Enum.empty?/1` and comparing with `[]` or `%{}` are `is_empty()`.
- `%{}` and `%{key => value}` build a `HashMap` or `BTreeMap` where one is
expected.
- A generator over a map or set behind a reference iterates it by reference,
copying copy keys and values and borrowing the rest.
- `first..last//step` with a positive literal step counts up between dynamic
bounds, as Rust's `first..=last` does.
- `Enum.sort/1` sorts booleans, strings, and tuples as well as integers.
- A generator over a map or set is typed, map and set specs keep their key and
value types, and an Elixir integer bound with `=` takes the integer type its
later uses need.
- `%{}` where a reference to a map is expected builds one and borrows it, and a
map key of unknown type is borrowed.
- A closure that only calls a function with its argument is that function.
- An interpolated string is `format!` of its parts, and a `for ... reduce:`
over several generators or filters reduces the comprehension of the
variables its clauses use.
- `MapSet.new()` where a reference to a set is expected is borrowed, a
fallible list concatenated propagates its error, and cloning a reference to
a reference clones the value.
- `List.replace_at/3` computes the new item, then sets it in place, leaving the
list as it is for an index past its end.
- `left ++ right` concatenates lists, growing `left` in place at its last use:
`list ++ [item]` pushes, and `list ++ other` extends.
- A comprehension's type follows all its generators, `{:some, value}` and
`{:ok, value}` patterns included, and a tuple bound with `=` types each
variable.
- A `Vec` read from Rust source is a list, as one a spec names is.
- A `case`, `if`, `cond`, or block has the type of its first branch whose type
is known, and a block's bindings are typed in order.
- `Enum.map(list, fn item -> item.clone() end)` is `cloned()` over a list of
references, and the list itself otherwise.
- `Enum.reduce/3` builds its accumulator as the expected type, `%{}` and `[]`
included, and has the reducer's type when its initial value has none.
- Indexing a map or list, and the module's own aliases named inside other
types, are typed; a `Vec`, reference, or tuple rebuilt from Rust syntax keeps
its kind; an alias of a copy type is copied.
- Copy items a slice lends are copied out for any use, a tuple pattern on a
reference binds references to its elements, Elixir's integer compares in
the Rust integer type of the other side, and a function argument that
cannot fail to match is bound with `=` rather than matched.
- A variable put into a new list or pushed while still in use is copied, and
`Map.get/3` with an empty default is `unwrap_or_default()`.
- `String.downcase/1` and `String.upcase/1` lower to `to_lowercase()` and
`to_uppercase()`, and membership among literals, as `word in ["no", "off"]`,
asks an array, looking a `String` up as a `&str`.
- A comprehension over a list of tuples still in use iterates it by
reference, copying copy elements rather than cloning every item.
- `MapSet.new/1` of listed items is `HashSet::from([...])`, and a list literal
where a slice is expected is a borrowed array, as `&[]`.
- A `for ... reduce:` whose one clause binds the accumulator to a variable
names the accumulator after it, typed, rather than matching it each turn.
- Comparing an option with `nil` is `is_some()` or `is_none()`.

### Fixed

- What indexing reaches is never taken from its collection; it is copied.
- Taking a value behind a reference copies it, so an update to a borrowed set
never changes the set it was given.
- A list matched against a list pattern binds its items by reference, and a
slice index read from one is copied.
- `Map.update/4` matches the present value, so the function and the default
may each take what they use.
- A map is indexed by a borrowed key.
- Collecting into a slice argument collects a `Vec` and borrows it.
- `Enum.concat/2` and `Enum.zip/2` take a list only at its last use, and copy
from one the other side borrows.
- An iterator collected into a list and iterated again is iterated as it is.
- A generator over one value computes it before the item moves into the pair.
- A string literal key is looked up as the `&str` it is, and comparing a string
with `""` asks whether it is empty.
- Operators read borrowed copy values a pattern binds: a comparison derefs the
borrowed side, `and`/`or` and `if` take booleans, and an atom compared with
an enum is that enum's variant.
- A count passed where a `u32` or another integer is expected is cast to it.
- `List.first/1` and `List.last/1` on an iterator collected into a list take
from the iterator.
- A nested generator over a list an earlier item holds iterates it by
reference instead of moving it out of a borrow, and a generator over a slice
types its items as references.
- A `for ... reduce:` types the items it binds, so a borrowed copy item is read
where it is stored.
- An option field reached through a reference is matched by reference, binding
a reference to what it holds.
- A binding keeps the option or result type its right-hand side gives rather
than what a later use suggests, and a tuple destructured from a fallible call
types its elements.
- An empty map whose type is left to Rust is `HashMap::new()`.
- A range bound that can fail propagates its error, and a range a method is
called on is parenthesized, as `(1..=count).into_iter()`.

## 1.0.0-rc.19 - 2026-10-26

### Added
Expand Down
1 change: 1 addition & 0 deletions lib/rustq/meta/ast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ defmodule RustQ.Meta.AST do
:=,
:!,
:..,
:..//,
:!=,
:!==,
:%,
Expand Down
1 change: 1 addition & 0 deletions lib/rustq/meta/core/call.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule RustQ.Meta.Core.Call do
:rem,
:in,
:..,
:..//,
:abs,
:min,
:max,
Expand Down
106 changes: 102 additions & 4 deletions lib/rustq/meta/desugar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ defmodule RustQ.Meta.Desugar do
comprehension collecting into it;
* `Enum.group_by/2,3` is a map filled by a loop, and `Enum.uniq/1` and `Enum.uniq_by/2`
keep the items a set has not seen;
* a generator over `Enum.with_index(list)` enumerates the list in place.
* a generator over `Enum.with_index(list)` enumerates the list in place;
* `left ++ right` is `Enum.concat(left, right)`;
* an interpolated string is `format!` of its parts;
* a `for ... reduce:` over several generators or filters reduces the comprehension of the
variables they bind.

Where a rewrite needs what only lowering knows, it leaves an internal form for lowering to
resolve: `__rustq_items__(list)` iterates a list as its ownership allows, and
`__rustq_owned__(item, list)` is an item of `list`, copied where that costs nothing and
cloned otherwise.
"""

alias RustQ.Meta.Pattern

@doc "Rewrites `body`'s standard-library calls that are other Rusty-Elixir."
@spec desugar(Macro.t()) :: Macro.t()
def desugar(body), do: Macro.prewalk(body, &rewrite/1)
Expand All @@ -32,8 +38,19 @@ defmodule RustQ.Meta.Desugar do
end)
end

def apply_function({:fn, _, [{:->, _, [[pattern], body]}]}, value),
do: quote(do: case(unquote(value), do: (unquote(pattern) -> unquote(body))))
# A pattern that cannot fail, as a tuple of variables, binds the value; another matches it.
def apply_function({:fn, _, [{:->, _, [[pattern], body]}]}, value) do
if binding?(pattern),
do:
quote(
do:
(
unquote(pattern) = unquote(value)
unquote(body)
)
),
else: quote(do: case(unquote(value), do: (unquote(pattern) -> unquote(body))))
end

def apply_function({:&, _, [{:/, _, [{{:., _, _} = remote, _, _}, 1]}]}, value),
do: {remote, [], [value]}
Expand All @@ -50,12 +67,22 @@ defmodule RustQ.Meta.Desugar do
)
end

defp binding?({name, _, ast_context}) when is_atom(name) and is_atom(ast_context), do: true
defp binding?({left, right}), do: binding?(left) and binding?(right)
defp binding?({:{}, _, elements}), do: Enum.all?(elements, &binding?/1)
defp binding?(_pattern), do: false

# Names a rewrite introduces, which shadow nothing a body writes.
defp fresh(name), do: Macro.var(:"rustq_#{name}", __MODULE__)

defp rewrite({{:., _, [{:__aliases__, _, [:Map]}, :new]}, _, [enumerable | transform]}),
do: comprehension(enumerable, transform, quote(do: HashMap.new()))

# A set of listed items is built from them as they are; see `RustQ.Meta.Lower.MapSet`.
defp rewrite({{:., _, [{:__aliases__, _, [:MapSet]}, :new]}, _, [items]} = call)
when is_list(items),
do: call

defp rewrite({{:., _, [{:__aliases__, _, [:MapSet]}, :new]}, _, [enumerable | transform]}),
do: comprehension(enumerable, transform, quote(do: HashSet.new()))

Expand Down Expand Up @@ -113,9 +140,70 @@ defmodule RustQ.Meta.Desugar do
end
end

defp rewrite({:for, meta, args}), do: {:for, meta, Enum.map(args, &indexed_generator/1)}
defp rewrite({:++, _, [left, right]}), do: quote(do: Enum.concat(unquote(left), unquote(right)))

defp rewrite({:<<>>, _, parts} = binary) do
if Enum.all?(parts, &(is_binary(&1) or interpolated(&1) != nil)) do
format = Enum.map_join(parts, &format_part/1)
{:format!, [], [format | for(part <- parts, not is_binary(part), do: interpolated(part))]}
else
binary
end
end

defp rewrite({:for, meta, args}) do
{qualifiers, options} = Enum.split_while(args, &(not Keyword.keyword?(&1) or &1 == []))
options = Enum.concat(options)

if Keyword.has_key?(options, :reduce) and match?([_, _ | _], qualifiers),
do: reduced(qualifiers, options),
else: {:for, meta, Enum.map(args, &indexed_generator/1)}
end

defp rewrite(ast), do: ast

defp reduced(qualifiers, options) do
clauses = Keyword.fetch!(options, :do)

item =
case bound_variables(qualifiers, clauses) do
[variable] -> variable
variables -> {:{}, [], variables}
end

items = {:for, [], qualifiers ++ [[do: item]]}

quote(
do:
Enum.reduce(
unquote(items),
unquote(Keyword.fetch!(options, :reduce)),
unquote(reducer(item, clauses))
)
)
end

# The variables the generators bind that the clauses use, in order.
defp bound_variables(qualifiers, clauses) do
used = clauses |> Pattern.variables() |> MapSet.new(&variable_key/1)

qualifiers
|> Enum.flat_map(&generator_variables/1)
|> Enum.uniq_by(&variable_key/1)
|> Enum.filter(&MapSet.member?(used, variable_key(&1)))
end

defp generator_variables({:<-, _, [pattern, _collection]}), do: Pattern.variables(pattern)
defp generator_variables(_filter), do: []

defp variable_key({name, _meta, context}), do: {name, context}

defp reducer(item, [{:->, _, [[accumulator], body]}]),
do: quote(do: fn unquote(item), unquote(accumulator) -> unquote(body) end)

defp reducer(item, clauses),
do: quote(do: fn unquote(item), rustq_acc -> case(rustq_acc, do: unquote(clauses)) end)

# `{item, index} <- Enum.with_index(list)` enumerates the list, binding `{index, item}`,
# rather than collecting the pairs first.
defp indexed_generator(
Expand Down Expand Up @@ -154,6 +242,16 @@ defmodule RustQ.Meta.Desugar do
end
end

defp interpolated({:"::", _, [{{:., _, [Kernel, :to_string]}, _, [value]}, {:binary, _, _}]}),
do: value

defp interpolated(_part), do: nil

defp format_part(text) when is_binary(text),
do: text |> String.replace("{", "{{") |> String.replace("}", "}}")

defp format_part(_interpolation), do: "{}"

# The collection an empty collectable names, or nil for one with items.
defp empty_collection({:%{}, _, []}), do: quote(do: HashMap.new())
defp empty_collection([]), do: quote(do: Vec.new())
Expand Down
Loading
Loading