run-rs 0.6.30

Run a subset of Rust as an interpreted script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Small helpers shared by the compile modules.

use anyhow::Result;
use syn::punctuated::Punctuated;
use syn::{BinOp, Expr, Lit, Pat, UnOp};

use crate::interpreter::bytecode::BinKind;
use crate::interpreter::numeric::IntWidth;

pub(super) fn is_assign_op(op: &BinOp) -> bool {
    use BinOp::{
        AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, MulAssign, RemAssign,
        ShlAssign, ShrAssign, SubAssign,
    };
    matches!(
        op,
        AddAssign(_)
            | SubAssign(_)
            | MulAssign(_)
            | DivAssign(_)
            | RemAssign(_)
            | BitAndAssign(_)
            | BitOrAssign(_)
            | BitXorAssign(_)
            | ShlAssign(_)
            | ShrAssign(_)
    )
}

pub(super) fn bin_kind(op: &BinOp) -> Option<BinKind> {
    use BinOp::{
        Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div,
        DivAssign, Eq, Ge, Gt, Le, Lt, Mul, MulAssign, Ne, Rem, RemAssign, Shl, ShlAssign, Shr,
        ShrAssign, Sub, SubAssign,
    };
    Some(match op {
        Add(_) | AddAssign(_) => BinKind::Add,
        Sub(_) | SubAssign(_) => BinKind::Sub,
        Mul(_) | MulAssign(_) => BinKind::Mul,
        Div(_) | DivAssign(_) => BinKind::Div,
        Rem(_) | RemAssign(_) => BinKind::Rem,
        Eq(_) => BinKind::Eq,
        Ne(_) => BinKind::Ne,
        Lt(_) => BinKind::Lt,
        Le(_) => BinKind::Le,
        Gt(_) => BinKind::Gt,
        Ge(_) => BinKind::Ge,
        BitAnd(_) | BitAndAssign(_) => BinKind::BitAnd,
        BitOr(_) | BitOrAssign(_) => BinKind::BitOr,
        BitXor(_) | BitXorAssign(_) => BinKind::BitXor,
        Shl(_) | ShlAssign(_) => BinKind::Shl,
        Shr(_) | ShrAssign(_) => BinKind::Shr,
        _ => return None,
    })
}

#[derive(Clone, Copy, Eq, PartialEq)]
pub(in crate::interpreter) enum FloatTy {
    F32,
    F64,
}

/// A non literal init retags through a runtime cast, a no-op on an already typed value.
#[derive(Clone, Copy)]
pub(in crate::interpreter) enum NumericTy {
    Int(IntWidth),
    Float(FloatTy),
}

pub(super) fn numeric_annotation(ty: &syn::Type) -> Option<NumericTy> {
    let syn::Type::Path(p) = ty else {
        return None;
    };
    let seg = p.path.segments.last()?;
    if !matches!(seg.arguments, syn::PathArguments::None) {
        return None;
    }
    let name = seg.ident.to_string();
    match name.as_str() {
        "f32" => Some(NumericTy::Float(FloatTy::F32)),
        "f64" => Some(NumericTy::Float(FloatTy::F64)),
        _ => IntWidth::parse(&name).map(NumericTy::Int),
    }
}

/// Including a negated one, seen through parens.
pub(super) fn int_literal(e: &Expr) -> Option<i64> {
    match e {
        Expr::Lit(l) => match &l.lit {
            Lit::Int(i) => i.base10_parse::<i64>().ok(),
            Lit::Byte(b) => Some(i64::from(b.value())),
            _ => None,
        },
        Expr::Unary(u) if matches!(u.op, UnOp::Neg(_)) => match &*u.expr {
            Expr::Lit(l) => match &l.lit {
                Lit::Int(i) => i.base10_parse::<i64>().ok().map(|v| -v),
                _ => None,
            },
            _ => None,
        },
        Expr::Paren(p) => int_literal(&p.expr),
        Expr::Group(g) => int_literal(&g.expr),
        _ => None,
    }
}

pub fn first_generic_type(seg: &syn::PathSegment) -> Option<&syn::Type> {
    if let syn::PathArguments::AngleBracketed(ab) = &seg.arguments {
        for a in &ab.args {
            if let syn::GenericArgument::Type(t) = a {
                return Some(t);
            }
        }
    }
    None
}

pub(super) fn collect_pattern_names(pat: &Pat, out: &mut Vec<String>) {
    match pat {
        Pat::Ident(id) if super::pattern::is_unit_variant_ident(id) => {}
        Pat::Ident(id) => {
            out.push(id.ident.to_string());
            if let Some(sub) = &id.subpat {
                collect_pattern_names(&sub.1, out);
            }
        }
        Pat::Tuple(t) => t.elems.iter().for_each(|p| collect_pattern_names(p, out)),
        Pat::TupleStruct(ts) => ts.elems.iter().for_each(|p| collect_pattern_names(p, out)),
        Pat::Slice(s) => s.elems.iter().for_each(|p| collect_pattern_names(p, out)),
        Pat::Struct(s) => s
            .fields
            .iter()
            .for_each(|f| collect_pattern_names(&f.pat, out)),
        Pat::Reference(r) => collect_pattern_names(&r.pat, out),
        Pat::Paren(p) => collect_pattern_names(&p.pat, out),
        Pat::Type(t) => collect_pattern_names(&t.pat, out),
        Pat::Or(o) => {
            // every alternative binds the same names
            if let Some(first) = o.cases.first() {
                collect_pattern_names(first, out);
            }
        }
        _ => {}
    }
}

pub(super) fn is_name(arg: &str) -> bool {
    !arg.is_empty()
        && arg.parse::<usize>().is_err()
        && arg.chars().all(|c| c.is_alphanumeric() || c == '_')
        && arg
            .chars()
            .next()
            .is_some_and(|c| c.is_alphabetic() || c == '_')
}

pub(super) fn inline_holes(template: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut chars = template.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '{' {
            if chars.peek() == Some(&'{') {
                chars.next();
                continue;
            }
            let mut inner = String::new();
            for ic in chars.by_ref() {
                if ic == '}' {
                    break;
                }
                inner.push(ic);
            }
            // `{:w$}` names a variable after the colon, that is a hole too
            if let Some((_, spec)) = inner.split_once(':') {
                let mut token = String::new();
                for c in spec.chars() {
                    if c.is_alphanumeric() || c == '_' {
                        token.push(c);
                        continue;
                    }
                    if c == '$' && is_name(&token) {
                        out.push(token.clone());
                    }
                    token.clear();
                }
            }
            let arg = inner.split(':').next().unwrap_or("").trim();
            if is_name(arg) {
                out.push(arg.to_string());
            }
        } else if c == '}' && chars.peek() == Some(&'}') {
            chars.next();
        }
    }
    out
}

pub(super) fn macro_yields_value(mac: &syn::Macro) -> bool {
    let name = mac
        .path
        .segments
        .last()
        .map(|s| s.ident.to_string())
        .unwrap_or_default();
    matches!(name.as_str(), "format" | "vec" | "matches" | "dbg")
}

pub(super) fn parse_exprs(mac: &syn::Macro) -> Result<Vec<Expr>> {
    Ok(mac
        .parse_body_with(Punctuated::<Expr, syn::Token![,]>::parse_terminated)?
        .into_iter()
        .collect())
}

pub(super) fn parse_vec_repeat(input: syn::parse::ParseStream) -> syn::Result<(Expr, Expr)> {
    let value: Expr = input.parse()?;
    input.parse::<syn::Token![;]>()?;
    let count: Expr = input.parse()?;
    Ok((value, count))
}

pub(super) fn parse_matches(mac: &syn::Macro) -> Result<(Expr, syn::Pat, Option<Expr>)> {
    pub(super) fn inner(
        input: syn::parse::ParseStream,
    ) -> syn::Result<(Expr, syn::Pat, Option<Expr>)> {
        let expr: Expr = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let pat = syn::Pat::parse_multi_with_leading_vert(input)?;
        let guard = if input.peek(syn::Token![if]) {
            input.parse::<syn::Token![if]>()?;
            Some(input.parse()?)
        } else {
            None
        };
        Ok((expr, pat, guard))
    }
    Ok(mac.parse_body_with(inner)?)
}

/// `&serde_json::Value` is `Value` and `&[String]` is `Vec`. The coverage check only asks what
/// the receiver is.
pub(super) fn type_head(ty: &syn::Type) -> Option<String> {
    match ty {
        syn::Type::Reference(r) => type_head(&r.elem),
        syn::Type::Paren(p) => type_head(&p.elem),
        syn::Type::Group(g) => type_head(&g.elem),
        syn::Type::Slice(_) | syn::Type::Array(_) => Some("Vec".to_string()),
        syn::Type::Path(p) => p.path.segments.last().map(|s| s.ident.to_string()),
        _ => None,
    }
}

pub(super) fn expr_kind(expr: &Expr) -> &'static str {
    match expr {
        Expr::Infer(_) => "_ placeholder",
        Expr::Let(_) => "let expression",
        Expr::TryBlock(_) => "try block",
        Expr::Yield(_) => "yield",
        Expr::Const(_) => "const block",
        Expr::Verbatim(_) => "unparsed tokens",
        _ => "this expression",
    }
}

/// Whether the pattern binds a name by value, `Some(x)`, which moves or copies out of the
/// scrutinee. `ref` bindings and wildcards do not.
pub(super) fn pattern_owns(pat: &Pat) -> bool {
    match pat {
        Pat::Ident(id) if super::pattern::is_unit_variant_ident(id) => false,
        Pat::Ident(id) => {
            id.by_ref.is_none() || id.subpat.as_ref().is_some_and(|sub| pattern_owns(&sub.1))
        }
        Pat::Tuple(t) => t.elems.iter().any(pattern_owns),
        Pat::TupleStruct(ts) => ts.elems.iter().any(pattern_owns),
        Pat::Slice(s) => s.elems.iter().any(pattern_owns),
        Pat::Struct(s) => s.fields.iter().any(|f| pattern_owns(&f.pat)),
        Pat::Paren(p) => pattern_owns(&p.pat),
        Pat::Type(t) => pattern_owns(&t.pat),
        Pat::Or(o) => o.cases.iter().any(pattern_owns),
        // a `&x` pattern binds through a reference, the scrutinee is not moved
        _ => false,
    }
}

/// Whether the pattern has a `ref` binding, which must see the scrutinee's own storage.
pub(super) fn pattern_borrows(pat: &Pat) -> bool {
    match pat {
        Pat::Ident(id) => {
            id.by_ref.is_some()
                || id
                    .subpat
                    .as_ref()
                    .is_some_and(|sub| pattern_borrows(&sub.1))
        }
        Pat::Tuple(t) => t.elems.iter().any(pattern_borrows),
        Pat::TupleStruct(ts) => ts.elems.iter().any(pattern_borrows),
        Pat::Slice(s) => s.elems.iter().any(pattern_borrows),
        Pat::Struct(s) => s.fields.iter().any(|f| pattern_borrows(&f.pat)),
        Pat::Paren(p) => pattern_borrows(&p.pat),
        Pat::Type(t) => pattern_borrows(&t.pat),
        Pat::Reference(r) => pattern_borrows(&r.pat),
        Pat::Or(o) => o.cases.iter().any(pattern_borrows),
        _ => false,
    }
}

/// `for x in EXPR` consumes `EXPR` unless it is a borrow or an iterator method on a borrowed
/// receiver. Only a consumed vec hands its items to the loop.
pub(super) fn iterable_is_owned(expr: &Expr) -> bool {
    match expr {
        Expr::Paren(p) => iterable_is_owned(&p.expr),
        Expr::Group(g) => iterable_is_owned(&g.expr),
        Expr::Reference(_) | Expr::Range(_) => false,
        // a fresh collection or an owning chain, `v.clone()` or `v.into_iter().rev()`, is the
        // loop's own. Shared with a statement end temporary drop it would drop twice.
        Expr::MethodCall(_) => init_is_owned(expr) || chain_owns_items(expr),
        _ => true,
    }
}

/// Whether a `let mut` init already owns unique storage. A local or a place read is handled by
/// `Own`, a constructor or a call is fresh, and a method in the list hands back a fresh value.
/// Anything else may share storage with a live value and the binding copies first.
pub(super) fn init_is_unique(expr: &Expr) -> bool {
    match expr {
        Expr::Paren(p) => init_is_unique(&p.expr),
        Expr::Group(g) => init_is_unique(&g.expr),
        Expr::Try(t) => init_is_unique(&t.expr),
        Expr::Await(a) => init_is_unique(&a.base),
        Expr::MethodCall(m) => matches!(
            m.method.to_string().as_str(),
            "clone"
                | "cloned"
                | "copied"
                | "to_vec"
                | "to_owned"
                | "to_string"
                | "collect"
                | "pop"
                | "remove"
                | "take"
                | "replace"
                | "swap_remove"
                | "split_off"
                | "drain"
                | "into_iter"
                | "iter"
                | "iter_mut"
                | "chars"
                | "bytes"
                | "lines"
                | "split"
                | "split_whitespace"
                | "map"
                | "filter"
                | "rev"
                | "enumerate"
                | "zip"
                | "keys"
                | "values"
                | "new"
                | "with_capacity"
                | "parse"
                | "join"
                | "concat"
                | "format"
                | "trim"
                | "len"
                | "is_empty"
        ),
        _ => true,
    }
}

/// Whether a temporary made for the expression owns a fresh value, so the statement end drops
/// it. A place read, a path, a reference and a closure hand out a handle into storage that
/// lives on, and a guard is released by `release_guard_temps`.
pub(super) fn temp_is_owned(expr: &Expr) -> bool {
    match expr {
        Expr::Paren(p) => temp_is_owned(&p.expr),
        Expr::Group(g) => temp_is_owned(&g.expr),
        Expr::Path(_)
        | Expr::Field(_)
        | Expr::Index(_)
        | Expr::Reference(_)
        | Expr::Closure(_)
        | Expr::Lit(_)
        | Expr::Range(_)
        | Expr::Cast(_) => false,
        Expr::Unary(u) => !matches!(u.op, syn::UnOp::Deref(_)) && temp_is_owned(&u.expr),
        Expr::MethodCall(m) => match m.method.to_string().as_str() {
            "borrow" | "borrow_mut" | "try_borrow" | "try_borrow_mut" | "lock" => false,
            _ => init_is_owned(expr),
        },
        other => init_is_owned(other),
    }
}

/// Whether an iterator chain hands out items of its own. `into_iter` and `drain` take them out
/// of the collection, `cloned` makes fresh ones, `iter` lends handles into the collection.
/// `map` hands out what its closure returns, a handle when the closure lends, so it owns only
/// over an owning receiver. A collection receiver lends too, `vec![..].last()` is the slice
/// method. An iterator held in a binding is unknown, so it counts as lending.
fn chain_owns_items(expr: &Expr) -> bool {
    match expr {
        Expr::Paren(p) => chain_owns_items(&p.expr),
        Expr::Group(g) => chain_owns_items(&g.expr),
        Expr::MethodCall(m) => match m.method.to_string().as_str() {
            "into_iter" | "into_keys" | "into_values" | "drain" | "cloned" | "copied" | "chars"
            | "bytes" | "char_indices" | "lines" | "split" | "split_whitespace" => true,
            "map" | "filter_map" | "flat_map" | "filter" | "take" | "skip" | "rev"
            | "enumerate" | "step_by" | "take_while" | "skip_while" | "peekable" | "by_ref"
            | "fuse" | "inspect" | "flatten" | "scan" | "map_while" => {
                chain_owns_items(&m.receiver)
            }
            "zip" | "chain" => chain_owns_items(&m.receiver) && m.args.iter().all(chain_owns_items),
            _ => false,
        },
        Expr::Range(_) => true,
        _ => false,
    }
}

/// Whether a `let` init hands the binding a value of its own, so scope end drops it. A borrow
/// or an accessor that hands out a handle into other storage does not. An unknown method is
/// treated as a borrow, a missed drop is safer than a drop of storage someone else owns.
pub(super) fn init_is_owned(expr: &Expr) -> bool {
    match expr {
        Expr::Paren(p) => init_is_owned(&p.expr),
        Expr::Group(g) => init_is_owned(&g.expr),
        Expr::Try(t) => init_is_owned(&t.expr),
        Expr::Await(a) => init_is_owned(&a.base),
        Expr::Reference(_) => false,
        Expr::MethodCall(m) => match m.method.to_string().as_str() {
            "clone" | "cloned" | "copied" | "to_vec" | "to_owned" | "to_string" | "collect"
            | "pop" | "remove" | "take" | "replace" | "swap_remove" | "split_off"
            | "into_inner" | "new" | "default" | "with_capacity" | "borrow" | "borrow_mut"
            | "try_borrow" | "try_borrow_mut" | "lock" | "concat" | "repeat" | "join"
            | "into_iter" | "into_keys" | "into_values" | "drain" | "split_at" | "insert"
            | "then_some" | "then" | "into" => true,
            "unwrap" | "expect" | "unwrap_or" | "unwrap_or_else" | "unwrap_or_default" | "ok"
            | "err" | "map" | "map_err" | "and_then" | "await" | "or" | "and" | "xor" | "zip"
            | "ok_or" | "ok_or_else" | "map_or" | "map_or_else" | "or_else" | "filter"
            | "flatten" | "take_if" | "transpose" => init_is_owned(&m.receiver),
            // an iterator terminal hands out an item of the chain's own only when the chain
            // owns its items
            "last" | "nth" | "next" | "next_back" | "max" | "min" | "max_by" | "min_by"
            | "max_by_key" | "min_by_key" | "fold" | "reduce" | "find" | "find_map"
            | "partition" | "unzip" | "peek" => chain_owns_items(&m.receiver),
            _ => false,
        },
        Expr::Block(b) => b.block.stmts.last().is_some_and(|stmt| match stmt {
            syn::Stmt::Expr(e, None) => init_is_owned(e),
            _ => false,
        }),
        Expr::If(i) => i
            .then_branch
            .stmts
            .last()
            .is_some_and(|stmt| matches!(stmt, syn::Stmt::Expr(e, None) if init_is_owned(e))),
        Expr::Match(m) => m.arms.iter().any(|arm| init_is_owned(&arm.body)),
        _ => true,
    }
}