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
2 changes: 1 addition & 1 deletion quest/m2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ The fork's feature quests folded into the [exporter](/quest/m1/ffi-export.md);

- [MIR's codec as fast as THIR's](/quest/m2/ts-mir-codec.md) - the codec from MIR no slower than from THIR in any engine
- [Lower bodies from MIR](/quest/m2/ts-mir.md) - the MIR spike's prototype becomes the only lowering, and the THIR one goes
- [Enum variant field order](/quest/m2/ts-variant-order.md) - a variant's fields evaluate in the order written, not declared
- [Stable names](/quest/m2/ts-stable-names.md) - generic instances and trait-impl methods always carry their suffix, so callers' names never shift
- [Struct update on a std range](/quest/m2/ts-range-update.md) - `Range { start, ..r }` reads its base, or is refused with a span, instead of dropping it or panicking the driver
- [Cheaper byte slices](/quest/m2/ts-byte-views.md) - splitting a `&[u8]` stops making a `Uint8Array` view per split
- [Wide-integer fast paths](/quest/m2/ts-wide-fast.md) - a fused `from_halves` and an unboxed limb product, kept only if measured faster
- [Drop in the TypeScript translator](/quest/m2/ts-drop.md) - every value that drops in moq-net and kio drops where Rust drops it
Expand Down
19 changes: 19 additions & 0 deletions quest/m2/ts-range-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# [XS] Struct update syntax on a std range

## Goal

[rust2-ts](/ts/README.md) translates `Range { start, ..r }` with the base's fields, or
refuses it with an error at its span, under both lowerings. Today the THIR lowering
drops the base when it builds the range, and panics the driver on an index when a
`for` loop iterates it.

## Plan

Found in [#49](https://github.com/kixelated/rust2/pull/49). The std arm of the THIR
aggregate lowering returns before the base is read, and the `for` loop and slicing
paths assume every field is written. MIR's aggregate carries every field, so it may
already be right: prove it with a differential case before deciding whether THIR
supports it or refuses it. If [Lower bodies from MIR](/quest/m2/ts-mir.md) lands
first, only the MIR check remains.

Public API: none.
19 changes: 0 additions & 19 deletions quest/m2/ts-variant-order.md

This file was deleted.

8 changes: 5 additions & 3 deletions ts/fixtures/lang/expected/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ints, strings, derived, written, panics } from "./fmt.ts";
import { methods } from "./generics.ts";
import { u8Op, u16Op, u32Op, u64Op, u128Op, usizeOp, i8Op, i16Op, i32Op, i64Op, i128Op, isizeOp, i8Neg, i16Neg, i32Neg, i64Neg, i128Neg, isizeNeg, cast, convert } from "./ints.ts";
import { adaptors, consumers, ranges, options, own, values } from "./iters.ts";
import { copies, discriminants, loops, patterns, closures, library } from "./shapes.ts";
import { copies, order, discriminants, loops, patterns, closures, library } from "./shapes.ts";

/** Run one case. */
export function run(input: Uint8Array): rt.ByteVec {
Expand All @@ -17,7 +17,7 @@ export function run(input: Uint8Array): rt.ByteVec {
const kind = input[1];
const data = input.subarray(2, input.length - 0);
const [op2, kind2] = [op, kind] as [rt.u8, rt.u8];
const value = rt.u8.rem(op2, 33);
const value = rt.u8.rem(op2, 34);
if (value === 0) {
u8Op(kind2, data, out);
} else if (value === 1) {
Expand Down Expand Up @@ -102,8 +102,10 @@ export function run(input: Uint8Array): rt.ByteVec {
options(data, out);
} else if (value === 31) {
own(data, out);
} else {
} else if (value === 32) {
values(data, out);
} else {
order(data, out);
}
return out;
}
Expand Down
15 changes: 15 additions & 0 deletions ts/fixtures/lang/expected/shapes.Moved.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";

export function fmt(self: Moved, f: rt.Formatter): rt.Result<undefined, rt.FmtError> {
return f.debugTuple("Moved").field((f: rt.Formatter) => rt.u8.display(self._0, f)).field((f: rt.Formatter) => rt.u16.display(self._1, f)).finish();
}

/** A tuple struct that is not `Copy`, so struct update syntax reads its base in place. */
export interface Moved {
_0: rt.u8;
_1: rt.u16;
}

//# sourceMappingURL=shapes.Moved.ts.map
67 changes: 67 additions & 0 deletions ts/fixtures/lang/expected/shapes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Generated by rust2-ts. Do not edit.

import * as rt from "./rt.ts";
import * as Pair from "./fmt.Pair.ts";
import type * as Dir from "./shapes.Dir.ts";
import * as Meters from "./shapes.Meters.ts";
import * as Moved from "./shapes.Moved.ts";
import * as Point from "./shapes.Point.ts";
import * as Shape from "./shapes.Shape.ts";
import type * as Signed from "./shapes.Signed.ts";
Expand Down Expand Up @@ -159,6 +161,71 @@ export function mark(out: rt.ByteVec, b: rt.u8): rt.usize {
return out.length;
}

/**
* Aggregates written with their fields out of declaration order evaluate them as
* written: each field logs as it runs.
*/
export function order(data: Uint8Array, out: rt.ByteVec): void {
const at = (i: rt.usize): rt.u8 => {
return rt.option.unwrapOr(rt.slice.get(data, i), 0);
};
const arg = mark(out, at(0)) & 0xffff;
const arg2 = mark(out, at(1)) & 0xffff;
const field = mark(out, 2) | 0;
const field2 = arg;
const field3 = arg2;
const rect = { tag: "Rect", corner: { x: mark(out, 3) | 0, y: field }, width: field3, height: field2 } as Shape.Shape;
const field4 = Point.new(mark(out, 4) | 0, 0);
const line = { tag: "Line", _0: Point.new(mark(out, 5) | 0, 0), _1: field4 } as Shape.Shape;
const field5 = mark(out, 6) as rt.i64;
const pair = { _0: mark(out, 7) & 0xff, _1: field5 };
let n: Uint8Array = Uint8Array.of(at(2));
const field6 = n[0] as rt.i32;
const read = { x: increment(n) as rt.i32, y: field6 };
let base: Moved.Moved = { _0: at(4), _1: 0 };
const field7 = reset(base);
const updated = { _0: base._0, _1: field7 };
let taken: Shape.Shape;
if (rt.u8.rem(at(3), 2) === 0) {
const field8 = mark(out, 8) & 0xffff;
taken = { tag: "Rect", corner: Point.new(0, mark(out, 9) | 0), width: field8, height: 1 };
} else {
taken = { tag: "Dot" };
}
const field9 = mark(out, 10);
const range = new rt.Range(rt.usize.div(mark(out, 11), 2), field9);
let sum: rt.usize = 0;
const end = mark(out, 12);
for (let i2 = rt.usize.sub(mark(out, 13), 4); i2 < end; i2++) {
sum = rt.usize.add(sum, i2);
}
const len = data.length;
const arg3 = logged(data, out);
const field10 = rt.min(mark(out, 14), len);
const slice = rt.slice.range(arg3, rt.usize.div(rt.min(mark(out, 15), len), 2), field10);
const text = rt.fmt.format([(f: rt.Formatter) => Shape.fmt(rect, f), " ", (f: rt.Formatter) => Shape.fmt(line, f), " ", (f: rt.Formatter) => Pair.fmt(pair, f), " ", (f: rt.Formatter) => Point.fmt(read, f), " ", (f: rt.Formatter) => Moved.fmt(updated, f), " ", (f: rt.Formatter) => Shape.fmt(taken, f), " ", (f: rt.Formatter) => rt.usize.display(range.start, f), " ", (f: rt.Formatter) => rt.usize.display(range.end, f), " ", (f: rt.Formatter) => rt.usize.display(sum, f), " ", (f: rt.Formatter) => rt.fmt.list(slice, f, rt.u8.display)]);
out.extendFromSlice(rt.string.asBytes(text));
}

/** `data`, logging that it was read. */
export function logged(data: Uint8Array, out: rt.ByteVec): Uint8Array {
out.push(0xee);
return data;
}

/** `n`'s byte, incremented. */
export function increment(n: Uint8Array): rt.u8 {
const value = rt.u8.wrappingAdd(n[0], 1);
n[0] = value;
return n[0];
}

/** Changes `m`'s first field. */
export function reset(m: Moved.Moved): rt.u16 {
m._0 = 9;
return 1;
}

/** Fieldless enums cast to every width, past their discriminant's range and sign. */
export function discriminants(data: Uint8Array, out: rt.ByteVec): void {
for (const item of data) {
Expand Down
5 changes: 3 additions & 2 deletions ts/fixtures/lang/src/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn run(input: &[u8]) -> Vec<u8> {
return out;
};
let (op, kind) = (*op, *kind);
match op % 33 {
match op % 34 {
0 => ints::u8_op(kind, data, &mut out),
1 => ints::u16_op(kind, data, &mut out),
2 => ints::u32_op(kind, data, &mut out),
Expand Down Expand Up @@ -62,7 +62,8 @@ pub fn run(input: &[u8]) -> Vec<u8> {
29 => iters::ranges(data, &mut out),
30 => iters::options(data, &mut out),
31 => iters::own(data, &mut out),
_ => iters::values(data, &mut out),
32 => iters::values(data, &mut out),
_ => shapes::order(data, &mut out),
}
out
}
92 changes: 92 additions & 0 deletions ts/fixtures/lang/src/shapes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Control flow, patterns, aggregates, closures, and traits.

use crate::fmt::Pair;

/// A `Copy` struct, which Rust copies where JS would share.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Point {
Expand Down Expand Up @@ -27,6 +29,10 @@ impl Point {
#[derive(Clone, Copy)]
pub struct Meters(pub u64);

/// A tuple struct that is not `Copy`, so struct update syntax reads its base in place.
#[derive(Debug)]
pub struct Moved(pub u8, pub u16);

/// An enum with data, and one without.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Shape {
Expand Down Expand Up @@ -245,6 +251,92 @@ fn mark(out: &mut Vec<u8>, b: u8) -> usize {
out.len()
}

/// Aggregates written with their fields out of declaration order evaluate them as
/// written: each field logs as it runs.
pub fn order(data: &[u8], out: &mut Vec<u8>) {
let at = |i: usize| data.get(i).copied().unwrap_or(0);
let rect = Shape::Rect {
height: mark(out, at(0)) as u16,
width: mark(out, at(1)) as u16,
corner: Point {
y: mark(out, 2) as i32,
x: mark(out, 3) as i32,
},
};
let line = Shape::Line {
1: Point::new(mark(out, 4) as i32, 0),
0: Point::new(mark(out, 5) as i32, 0),
};
let pair = Pair {
1: mark(out, 6) as i64,
0: mark(out, 7) as u8,
};
// A local read before a later field changes it.
let mut n = [at(2)];
let read = Point {
y: n[0] as i32,
x: increment(&mut n) as i32,
};
// The base is read after the fields, which change it.
let mut base = Moved(at(4), 0);
let updated = Moved {
1: reset(&mut base),
..base
};
// Only the branch taken runs its fields.
let taken = match at(3) % 2 {
0 => Shape::Rect {
width: mark(out, 8) as u16,
height: 1,
corner: Point::new(0, mark(out, 9) as i32),
},
_ => Shape::Dot,
};
let range = std::ops::Range {
end: mark(out, 10),
start: mark(out, 11) / 2,
};
let mut sum = 0;
for i in (std::ops::Range {
end: mark(out, 12),
start: mark(out, 13) - 4,
}) {
sum += i;
}
let len = data.len();
// The sliced value comes before the range's fields.
let slice = &logged(data, out)[std::ops::Range {
end: mark(out, 14).min(len),
start: mark(out, 15).min(len) / 2,
}];
let text = format!(
"{rect:?} {line:?} {pair:?} {read:?} {updated:?} {taken:?} {} {} {sum} {slice:?}",
range.start, range.end
);
out.extend_from_slice(text.as_bytes());
}

/// `data`, logging that it was read.
#[inline(never)]
fn logged<'a>(data: &'a [u8], out: &mut Vec<u8>) -> &'a [u8] {
out.push(0xee);
data
}

/// `n`'s byte, incremented.
#[inline(never)]
fn increment(n: &mut [u8; 1]) -> u8 {
n[0] = n[0].wrapping_add(1);
n[0]
}

/// Changes `m`'s first field.
#[inline(never)]
fn reset(m: &mut Moved) -> u16 {
m.0 = 9;
1
}

/// Fieldless enums cast to every width, past their discriminant's range and sign.
pub fn discriminants(data: &[u8], out: &mut Vec<u8>) {
for &b in data {
Expand Down
Loading