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
10 changes: 6 additions & 4 deletions quest/m3/ts-runtime-throws.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ JS keeps in two arrays, a pointer moved past the array JS keeps it in (`AtIndex.
as one kept in a field), `Bytes::slice_ref` of other storage, `alloc`'s bytes read as
values, ranges stepped without their type, fmt widths written as a placeholder, a read
through a substring's `str::as_ptr` outside its own bytes but inside its text's
(`s[..1].as_ptr().add(1)`, `s[1..].as_ptr().wrapping_sub(1)`), and a raw
slice of a byte in no array cast to `*const [u8; N]` by a route the body doesn't show,
which [a byte local in a buffer of its own](/quest/m3/ts-unsafe/lone-byte-buffers.md) owns
for locals.
(`s[..1].as_ptr().add(1)`, `s[1..].as_ptr().wrapping_sub(1)`), and a raw or mutable slice
of a byte in a field (a struct's, a tuple's, a closure's capture, or one an async body keeps
across an `.await`), or a cast of one to `*const [u8; N]`: a field's byte has no buffer of
its own, unlike a `u8` local whose `&raw` the body takes, so the fix is a `Uint8Array(1)`
for each `u8` field whose address is taken. A `u8` local whose address only a call takes,
as `ptr::from_mut(&mut b)` does, has none either.
The substring reads are UB under Stacked Borrows and allowed under Tree Borrows, so they
throw until Rust settles its memory model (decided 2026-11-07 in
[#634](https://github.com/moq-dev/rust2/pull/634)); if Rust rules them defined, a
Expand Down
1 change: 0 additions & 1 deletion quest/m3/ts-unsafe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,3 @@ Public API: the translator's output.
- [A raw pointer behind a reference](/quest/m3/ts-unsafe/raw-behind-ref.md) - `&raw const *r` of a value JS copies points where the reference came from
- [NonZero's unchecked arithmetic](/quest/m3/ts-unsafe/nonzero-unchecked-ops.md) - `NonZero::unchecked_add` and `unchecked_mul` translate, aborting at std's site
- [UnsafeCell's raw accessors](/quest/m3/ts-unsafe/unsafecell.md) - `UnsafeCell::raw_get`, `from_mut`, and `replace` translate
- [A byte local whose address is taken, in a buffer of its own](/quest/m3/ts-unsafe/lone-byte-buffers.md) - raw and mutable slices of a `u8` local, and casts of them to byte arrays, translate instead of being refused
32 changes: 0 additions & 32 deletions quest/m3/ts-unsafe/lone-byte-buffers.md

This file was deleted.

2 changes: 1 addition & 1 deletion ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ crate translation reaches is refused, and so are two versions of one crate it re
| `Vec<T>` | `rs.Vec<T>`: its items, the array `items`, and the capacity std's holds; `rs.vecFrom(items)` makes one of an array with room for exactly them. `rs.ByteVec` of bytes |
| `Cow<[T]>`, `Cow<str>` | `rs.Cow<T>`: the `Vec` it owns, or an `rs.Borrowed` holding the slice it borrows, `rs.Cow<T, rs.ArraySlice<S>>` holding the arrays of a slice of arrays; `rs.ByteCow` for bytes, an `rs.ByteVec` or an `rs.ByteSlice`; `rs.StrCow` for text, the `rs.String` it owns or an `rs.Borrowed` holding the string it borrows, whose text `rs.strCowStr` reads |
| `&T`, `&mut T`, `Box<T>`, `Reverse<T>`, `NonZero<T>`, `AssertUnwindSafe<T>` | the value; a `&mut` to a value JS copies, such as a number, an `Option`, or a reference, or to an `Arc`, a `Waker`, or a `LocalKey`, whose copies share what it points at, is an `rs.Place<T>`, which for what a box holds is the place holding the box |
| `*const T`, `*mut T`, `NonNull<T>` | what a `&mut T` is: the object, an `rs.Slice` of a `[T; N]`'s elements or an `rs.ByteSlice` of a `[u8; N]`'s bytes, which compares by the array or buffer and offset they are at, refused for an array of one, which `array::from_ref`, `from_mut` of a variable, and `Option::as_slice` make of their own, or an `rs.Place<T>` to a value JS copies and to a library type such as a `Vec` or an `Arc`, which into an array, slice, or `Vec` is the `rs.AtIndex` of its element, which `add`, `sub`, `offset`, their wrapping forms, `offset_from`, `ptr::copy`, `write_bytes` of bytes, and `slice::from_raw_parts` move, measure, copy between, set, and view; one to a value in no array is element 0 of an array of one, as in Rust; JS keeps that array, and a shared subslice, which JS copies, apart from what holds it in Rust, so a pointer, slice, copy, or write of bytes the body shows reaching past either, a raw or mutable slice of a byte in no array, and `offset_from` between pointers the body shows JS keeps in different arrays are refused, and through a pointer from elsewhere, such as a field, any of these, or a read or write past the array JS keeps it in but for a `Vec`'s room up to its capacity, fails at run time; a pointer into an array of values JS keeps as objects is refused, as is comparing pointers to other values JS copies, each a place JS makes of its own; `rs.NULL` for null and `rs.DANGLING` for dangling. `str::as_ptr` points into the text's UTF-8, which JS encodes anew at each call, so a read through it reads what Rust's does and a write, or a mutable slice, through it, which Rust forbids, fails at run time, as do a read before its first byte, such as a substring's pointer moved back into its text's bytes, and `offset_from` between two calls' pointers, each refused where the body shows it. A pointer to a slice cast to one to an array of its elements is the `rs.Slice` or `rs.ByteSlice` of that many from its first, read through only where Rust reads. A `*const ()` is an identity, such as a waker's data, and `rs.DANGLING_UNIT` when dangling; one to a place of `()` or from a reference to one is refused. A pointer to a value JS keeps as an object cast to one, by `as` or `cast`, is the same object, which `rs.ptrErase` notes the type of, so a cast back to a pointer to another type fails at run time, and null and dangling stay so, dangling at the alignment of the type it was cast from; one to a value JS copies is the same place where an `Arc`'s or `Rc`'s `into_raw` or `as_ptr` gave it, its value's one place, which `rs.arcErase` checks where the body doesn't show it, and refused where the body shows a borrow's, a new place each borrow, as is casting one to a view of bytes, a new one each borrow, or one of no fixed size. A write of a whole object copies into it one object deep, as an assignment through a `&mut` does, the state an `rs` class such as `rs.LazyCell` keeps private included, so a pointer to a part kept as an object, such as a field of a tuple or struct type, is refused wherever the crate may put another object there while a pointer into that memory may still be read, which a write into a fresh allocation, or a variable before any pointer into it is taken, can't, whether by index or through std's iterators over its slots; a read copies what the crate writes into in place |
| `*const T`, `*mut T`, `NonNull<T>` | what a `&mut T` is: the object, an `rs.Slice` of a `[T; N]`'s elements or an `rs.ByteSlice` of a `[u8; N]`'s bytes, which compares by the array or buffer and offset they are at, refused for an array of one, which `array::from_ref`, `from_mut` of a variable, and `Option::as_slice` make of their own, or an `rs.Place<T>` to a value JS copies and to a library type such as a `Vec` or an `Arc`, which into an array, slice, or `Vec` is the `rs.AtIndex` of its element, which `add`, `sub`, `offset`, their wrapping forms, `offset_from`, `ptr::copy`, `write_bytes` of bytes, and `slice::from_raw_parts` move, measure, copy between, set, and view; one to a value in no array is element 0 of an array of one, as in Rust, but a byte variable whose `&raw` the body takes, or that of a reference to it made there, as `&mut b as *mut u8` does, is element 0 of a `Uint8Array` of its own, which its raw, mutable, and shared slices view as they do any other bytes; JS keeps an array of one, and a shared subslice of a byte in no array, which JS copies, apart from what holds it in Rust, so a pointer, slice, copy, or write of bytes the body shows reaching past either, and `offset_from` between pointers the body shows JS keeps in different arrays, are refused, and through a pointer from elsewhere any of these, a raw or mutable slice of a byte in no array, such as a field's, or a read or write past the array JS keeps it in but for a `Vec`'s room up to its capacity, fails at run time; a pointer into an array of values JS keeps as objects is refused, as is comparing pointers to other values JS copies, each a place JS makes of its own; `rs.NULL` for null and `rs.DANGLING` for dangling. `str::as_ptr` points into the text's UTF-8, which JS encodes anew at each call, so a read through it reads what Rust's does and a write, or a mutable slice, through it, which Rust forbids, fails at run time, as do a read before its first byte, such as a substring's pointer moved back into its text's bytes, and `offset_from` between two calls' pointers, each refused where the body shows it. A pointer to a slice cast to one to an array of its elements is the `rs.Slice` or `rs.ByteSlice` of that many from its first, read through only where Rust reads. A `*const ()` is an identity, such as a waker's data, and `rs.DANGLING_UNIT` when dangling; one to a place of `()` or from a reference to one is refused. A pointer to a value JS keeps as an object cast to one, by `as` or `cast`, is the same object, which `rs.ptrErase` notes the type of, so a cast back to a pointer to another type fails at run time, and null and dangling stay so, dangling at the alignment of the type it was cast from; one to a value JS copies is the same place where an `Arc`'s or `Rc`'s `into_raw` or `as_ptr` gave it, its value's one place, which `rs.arcErase` checks where the body doesn't show it, and refused where the body shows a borrow's, a new place each borrow, as is casting one to a view of bytes, a new one each borrow, or one of no fixed size. A write of a whole object copies into it one object deep, as an assignment through a `&mut` does, the state an `rs` class such as `rs.LazyCell` keeps private included, so a pointer to a part kept as an object, such as a field of a tuple or struct type, is refused wherever the crate may put another object there while a pointer into that memory may still be read, which a write into a fresh allocation, or a variable before any pointer into it is taken, can't, whether by index or through std's iterators over its slots; a read copies what the crate writes into in place |
| `MaybeUninit<T>` | `T`: `undefined` until written, `0` for a byte, or an object whose parts kept as objects are there, holding `undefined`, as `Box::new_uninit` and each of `Box::new_uninit_slice`'s is; a slice's `write_clone_of_slice` clones into its slots without dropping what they held. `Box::new_zeroed`'s holds the value of zero bytes in each part they are one of, such as a number, `false`, `None`, a null pointer, or an enum's variant of a tag or niche of zero, and `undefined` in each part they are none of, such as a reference, for a write to fill before a read |
| `ManuallyDrop<T>` | `T`, which nothing drops but its own `drop`; its `Debug`, comparisons, and constants are refused |
| `union` | an object of slots named by offset, `at0` and `at8`, each holding the field part kept there; fields must overlap in the same type, or in a non-null pointer and an `Option` of it |
Expand Down
24 changes: 14 additions & 10 deletions ts/fixtures/lang/elements/expected/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ export function inline(data: rs.ByteSlice, out: rs.ByteVec): void {
* functions named as std's, and byte pointers into one buffer through its views.
*/
export function edges(data: rs.ByteSlice, out: rs.ByteVec): void {
const byte = new Uint8Array(1);
const y = new Uint8Array(1);
let _200!: rs.Result<[rs.u32, rs.u32], rs.u8>;
let _206!: rs.u8;
for (const n of rs.byteSliceView(data)) {
Expand All @@ -232,8 +234,8 @@ export function edges(data: rs.ByteSlice, out: rs.ByteVec): void {
rs.byteVecPush(out, rs.ptrOffsetFrom(rs.ptrAdd(at, 1), at) & 0xff);
rs.byteVecPush(out, rs.ptrOffsetFrom(rs.sliceAsPtr(rs.ptrSlice(at, 0)), at) & 0xff);
rs.byteVecPush(out, lone & 0xff);
let byte: rs.u8 = n ^ 90;
const _97: rs.ByteSlice = rs.ptrByteSlice(rs.place(() => byte, (value2) => byte = value2), 1, true);
byte[0] = n ^ 90;
const _97: rs.ByteSlice = rs.ptrByteSlice(rs.arrayAsPtr(byte), 1, true);
rs.idx(_97, 0);
rs.byteVecPush(out, _97.data[_97.start]);
const a: rs.u32[] = [n as rs.u32, 1, 2, 3];
Expand All @@ -259,9 +261,9 @@ export function edges(data: rs.ByteSlice, out: rs.ByteVec): void {
rs.byteVecPush(out, _170.data[_170.start] & 0xff);
const _177: [rs.u32, rs.u8] = [n as rs.u32, n];
let x: rs.u32 = _177[0];
let y: rs.u8 = n;
rs.byteVecPush(out, offsetFrom(rs.place(() => x, (value3) => x = value3), rs.place(() => x, (value4) => x = value4)) & 0xff);
rs.byteVecPush(out, fromRawPartsMut(rs.place(() => y, (value5) => y = value5), 2));
y[0] = n;
rs.byteVecPush(out, offsetFrom(rs.place(() => x, (value2) => x = value2), rs.place(() => x, (value3) => x = value3)) & 0xff);
rs.byteVecPush(out, fromRawPartsMut(rs.arrayAsPtr(y), 2));
const item = (undefined as unknown) as rs.Result<[rs.u32, rs.u32], rs.u8>;
const item2 = (undefined as unknown) as rs.Result<[rs.u32, rs.u32], rs.u8>;
const dst: rs.Result<[rs.u32, rs.u32], rs.u8>[] = Array.from({ length: 2 }, (): rs.Result<[rs.u32, rs.u32], rs.u8> => {
Expand All @@ -275,7 +277,7 @@ export function edges(data: rs.ByteSlice, out: rs.ByteVec): void {
} else {
_200 = rs.Ok([x, 1] as [rs.u32, rs.u32]);
}
const uninit = rs.place(() => dst[1], (value6) => dst[1] = value6);
const uninit = rs.place(() => dst[1], (value4) => dst[1] = value4);
uninit.v = _200;
uninit;
const _207: rs.Result<[rs.u32, rs.u32], rs.u8> = dst[1];
Expand Down Expand Up @@ -432,6 +434,8 @@ export function rawSlices(data: rs.ByteSlice, out: rs.ByteVec): void {
* part of which keeps the pointer as Rust's does: after moving back to address 0 it is null.
*/
export function emptyParts(data: rs.ByteSlice, out: rs.ByteVec): void {
const lone = new Uint8Array(1);
const held = new Uint8Array(1);
const _130: (a0: rs.Place<rs.u8>) => rs.u8 = (p2: rs.Place<rs.u8>): rs.u8 => {
return rs.ptrWrappingAdd(p2, -1, 1, 1) === rs.NULL ? 1 : 0;
};
Expand Down Expand Up @@ -508,12 +512,12 @@ export function emptyParts(data: rs.ByteSlice, out: rs.ByteVec): void {
rs.byteVecPush(out, rs.ptrWrappingAdd(raw.ptr, -k, 2, 2) === rs.NULL ? 1 : 0);
const part22 = rs.ptrWrappingAdd(new rs.RawSlice(byte, 0).ptr, 0, 1, 1);
rs.byteVecPush(out, _130(part22));
let lone: rs.u8 = data.length & 0xff;
const p: rs.Place<rs.u8> = rs.place(() => lone, (value) => lone = value);
lone[0] = data.length & 0xff;
const p: rs.Place<rs.u8> = rs.arrayAsPtr(lone);
rs.byteVecPush(out, rs.ptrOffsetFrom(rs.byteSliceAsPtr(rs.ptrByteSlice(p, 0, true)), p) & 0xff);
rs.byteVecPush(out, rs.ptrOffsetFrom(rs.byteSliceAsPtr(rs.ptrByteSlice(rs.ptrAdd(p, 1), 0, true)), p) & 0xff);
let held: rs.u8 = lone;
const q: rs.Place<rs.u8> = rs.place(() => held, (value2) => held = value2);
held[0] = lone[0];
const q: rs.Place<rs.u8> = rs.arrayAsPtr(held);
const r: rs.ByteSlice = rs.ptrByteSlice(q, 0, false);
rs.byteVecPush(out, rs.u8Add(r.length & 0xff, rs.ptrOffsetFrom(rs.byteSliceAsPtr(r), q) & 0xff));
}
Expand Down
4 changes: 3 additions & 1 deletion ts/fixtures/lang/pointers/expected/lib.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Generated by rust2-ts. Do not edit.

import * as rs from "./rs.ts";
import { lists, shared, places, unchecked, nested, copies, sentinels, cells, rebound, lapsed, writers, made, segments, uninitRuns, iteratedRuns, besideWritten, waiters, taken, bytes, loaded, reattached, partWritten, aimed, distinct, rawArcs, rawPlaces, arrays, arrayCasts, genericUnions } from "./pointers.ts";
import { lists, shared, places, unchecked, nested, copies, sentinels, cells, rebound, lapsed, writers, made, segments, uninitRuns, iteratedRuns, besideWritten, waiters, taken, bytes, loaded, reattached, partWritten, aimed, distinct, rawArcs, rawPlaces, arrays, arrayCasts, genericUnions, loneBytes } from "./pointers.ts";
import type * as Aimed from "./pointers.Aimed.ts";
import * as Bin from "./pointers.Bin.ts";
import type * as ByCall from "./pointers.ByCall.ts";
Expand Down Expand Up @@ -306,6 +306,8 @@ export function cases(): rs.Vec<[string, (a0: rs.u8, a1: rs.ByteSlice, a2: rs.By
arrayCasts(data28, out28);
}] as [string, (a0: rs.u8, a1: rs.ByteSlice, a2: rs.ByteVec) => undefined], ["pointers::generic_unions", (_238: rs.u8, data29: rs.ByteSlice, out29: rs.ByteVec): undefined => {
genericUnions(data29, out29);
}] as [string, (a0: rs.u8, a1: rs.ByteSlice, a2: rs.ByteVec) => undefined], ["pointers::lone_bytes", (_239: rs.u8, data30: rs.ByteSlice, out30: rs.ByteVec): undefined => {
loneBytes(data30, out30);
}] as [string, (a0: rs.u8, a1: rs.ByteSlice, a2: rs.ByteVec) => undefined]]);
}

Expand Down
Loading