Skip to main content

targeted_mutation/instrument/
model.rs

1//! Data types describing discovered mutation sites.
2
3use serde::Serialize;
4
5use crate::plan::CmpOp;
6
7/// Name of the module injected into the crate root.
8pub const RUNTIME_MOD: &str = "__tmt_rt";
9
10#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
11#[serde(rename_all = "snake_case")]
12pub enum LiteralSide {
13    None,
14    Rhs(i128),
15    Lhs(i128),
16}
17
18#[derive(Clone, Copy, PartialEq, Eq, Debug)]
19pub enum BitValueType {
20    Unsigned(u32),
21    Signed(u32),
22    Bool,
23    Char,
24}
25
26impl BitValueType {
27    pub(crate) fn width(self) -> u32 {
28        match self {
29            BitValueType::Unsigned(w) | BitValueType::Signed(w) => w,
30            BitValueType::Bool => 1,
31            BitValueType::Char => 21,
32        }
33    }
34}
35
36#[derive(Clone, Copy, PartialEq, Eq, Debug)]
37pub enum LocalBitType {
38    Value(BitValueType),
39    SliceOf(BitValueType),
40}
41
42#[derive(Clone, PartialEq, Eq, Debug)]
43pub enum SiteKind {
44    Cmp {
45        op: CmpOp,
46        lhs: String,
47        rhs: String,
48        literal: LiteralSide,
49        /// Byte range of the operator and the whitespace around it.
50        gap: (usize, usize),
51    },
52    /// `lhs && rhs` or `lhs || rhs`.
53    Logic {
54        and: bool,
55        lhs: String,
56        rhs: String,
57        gap: (usize, usize),
58    },
59    /// `lhs + rhs` and friends. Only primitive numbers are mutated; for other operand
60    /// types the probe performs the original operation and reports nothing.
61    Arith {
62        op: ArithOp,
63        /// `lhs op= rhs` rather than `lhs op rhs`.
64        assign: bool,
65        lhs: String,
66        rhs: String,
67        gap: (usize, usize),
68        /// Static primitive types of local bindings referenced by `|` / `^` operands.
69        local_types: Vec<(String, LocalBitType)>,
70        /// Primitive type names shadowed by type aliases/items in this file.
71        shadowed_prims: Vec<String>,
72        /// Runtime cast-source type observation probes `(slot, start, end)` inside `|` / `^`.
73        cast_probes: Vec<(u32, usize, usize)>,
74    },
75    /// `!operand`. Only `bool` is mutated (the `!` is dropped).
76    Not {
77        /// Byte range of the `!` and whatever separates it from the operand.
78        bang: (usize, usize),
79        operand: String,
80        /// Deleting `!` would glue the preceding and following tokens (`return!x`).
81        needs_space: bool,
82    },
83    /// The result of a function whose exits reach the probe. The site's range is the
84    /// inside of the body's braces and its text is the function signature.
85    Return {
86        name: String,
87        /// `None` for a function returning `bool`; for one returning `Option<T>`, the
88        /// return type as written.
89        option: Option<String>,
90        /// Byte range of the body when it is a single expression: the function's result
91        /// is then exactly that expression's value.
92        sole_expr: Option<(usize, usize)>,
93    },
94    /// `receiver.max(argument)` or `.min(..)`: the other of the two.
95    MinMax {
96        max: bool,
97        receiver: String,
98        argument: String,
99        /// Byte ranges of `.max(` and of the closing `)`.
100        call: (usize, usize),
101        close: (usize, usize),
102    },
103    /// `receiver.first()` or `.last()` of a slice, a `Vec`, an array or a `BTreeSet`: the
104    /// other end. The two are the same element while there is at most one.
105    SliceEnd {
106        first: bool,
107        receiver: String,
108        /// Byte range of `.first()`.
109        call: (usize, usize),
110        /// Byte range of the method's name.
111        method: (usize, usize),
112    },
113    /// `receiver.starts_with(pattern)` or `.ends_with(pattern)`: relaxed to `.contains(pattern)`.
114    StrAffix {
115        starts: bool,
116        receiver: String,
117        pattern: String,
118        /// Byte range of `.starts_with(` (from receiver end to pattern start).
119        call: (usize, usize),
120        /// Byte range of the closing `)` (from pattern end to closing paren end).
121        close: (usize, usize),
122        /// Byte range of the method name.
123        method: (usize, usize),
124    },
125    /// `receiver.replace(from, to)` <-> `receiver.replacen(from, to, 1)`.
126    StrReplace {
127        /// `true` for `.replace(from, to)`, `false` for `.replacen(from, to, 1)`.
128        replace_all: bool,
129        receiver: String,
130        from: String,
131        to: String,
132        /// Byte range from receiver end to `from` start.
133        call: (usize, usize),
134        /// Byte range from `from` end to `to` start.
135        comma: (usize, usize),
136        /// Byte range from `to` end to closing `)` end (includes `, 1)` for `replacen`).
137        close: (usize, usize),
138        /// Byte range of the method name.
139        method: (usize, usize),
140    },
141    /// `receiver.clamp(low, high)`: drop lower bound (`.min(high)`) or upper bound (`.max(low)`).
142    Clamp {
143        receiver: String,
144        low: String,
145        high: String,
146        /// Byte range from receiver end to `low` start.
147        call: (usize, usize),
148        /// Byte range from `low` end to `high` start.
149        comma: (usize, usize),
150        /// Byte range from `high` end to closing `)` end.
151        close: (usize, usize),
152    },
153    /// `receiver.floor()` / `.ceil()` / `.round()` / `.trunc()` on floats.
154    Rounding {
155        op: RoundingOp,
156        receiver: String,
157        /// Byte range of `.floor()`.
158        call: (usize, usize),
159        /// Byte range of the method name.
160        method: (usize, usize),
161    },
162    /// `receiver.all(pred)` <-> `receiver.any(pred)` on an iterator with a closure predicate.
163    Quantifier {
164        all: bool,
165        receiver: String,
166        predicate: String,
167        /// Byte range from receiver end to predicate start.
168        call: (usize, usize),
169        /// Byte range from predicate end to closing `)` end.
170        close: (usize, usize),
171        /// Byte range of the method name.
172        method: (usize, usize),
173    },
174    /// `receiver.saturating_{add,sub,mul}(arg)` <-> `receiver.wrapping_{add,sub,mul}(arg)`.
175    SaturatingArith {
176        op: SaturatingOp,
177        receiver: String,
178        argument: String,
179        /// Byte range from receiver end to argument start.
180        call: (usize, usize),
181        /// Byte range from argument end to closing `)` end.
182        close: (usize, usize),
183        /// Byte range of the method name.
184        method: (usize, usize),
185    },
186    /// `receiver.trim()` / `.trim_start()` / `.trim_end()` on strings.
187    StrTrim {
188        op: TrimOp,
189        receiver: String,
190        /// Byte range of `.trim()`.
191        call: (usize, usize),
192        /// Byte range of the method name.
193        method: (usize, usize),
194    },
195    /// `receiver.eq_ignore_ascii_case(arg)` -> strict equality (`==`).
196    StrCaseEq {
197        receiver: String,
198        argument: String,
199        /// Byte range from receiver end to argument start.
200        call: (usize, usize),
201        /// Byte range from argument end to closing `)` end.
202        close: (usize, usize),
203        /// Byte range of the method name.
204        method: (usize, usize),
205    },
206    /// `receiver.abs()` -> `receiver` on signed integers and floats.
207    Abs {
208        receiver: String,
209        /// Byte range of `.abs()`.
210        call: (usize, usize),
211        /// Byte range of the method name.
212        method: (usize, usize),
213    },
214    /// `iter.take(n)` -> `iter.take(usize::MAX)` or `iter.skip(n)` -> `iter.skip(0)`.
215    IterLimit {
216        take: bool,
217        receiver: String,
218        count: String,
219        /// Byte range from receiver end to count start.
220        call: (usize, usize),
221        /// Byte range from count end to closing `)` end.
222        close: (usize, usize),
223        /// Byte range of the method name.
224        method: (usize, usize),
225    },
226    /// `x.to_be_bytes()` <-> `x.to_le_bytes()`, `x.to_be()` <-> `x.to_le()` on multi-byte ints.
227    EndianSwap {
228        op: EndianOp,
229        receiver: String,
230        /// Byte range of `.to_be_bytes()` / `.to_be()`.
231        call: (usize, usize),
232        /// Byte range of the method name.
233        method: (usize, usize),
234    },
235    /// `x.leading_zeros()` <-> `x.trailing_zeros()`, `leading_ones` <-> `trailing_ones`.
236    BitCount {
237        op: BitCountOp,
238        receiver: String,
239        /// Byte range of `.leading_zeros()`.
240        call: (usize, usize),
241        /// Byte range of the method name.
242        method: (usize, usize),
243    },
244    /// Unlabeled, value-less `break` <-> `continue` directly inside a loop.
245    LoopCtrl { is_break: bool },
246    /// `[lit1, lit2, ...].contains(&x)`: drop one literal element at a time.
247    LiteralSet {
248        receiver: String,
249        argument: String,
250        /// Byte range from receiver end to argument start (`.contains(`).
251        call: (usize, usize),
252        /// Byte range from argument end to closing `)` end.
253        close: (usize, usize),
254        /// `(element text, removal start, removal end)` for each element.
255        elements: Vec<(String, usize, usize)>,
256    },
257    /// `lhs..rhs` or `lhs..=rhs` on integers (or inside an indexing expression): the end
258    /// moves by one.
259    Range {
260        inclusive: bool,
261        lhs: String,
262        rhs: String,
263        gap: (usize, usize),
264        /// Present when the range is probed by switching an enclosing indexing expression
265        /// (`&xs[a..b]`, `&mut xs[a..b]`, or `xs[a..b].read_method(...)`).
266        index: Option<RangeIndex>,
267    },
268    /// An arm of a `match` that ends in a catch-all arm: without this arm the value falls
269    /// through to the arms below.
270    Arm {
271        pattern: String,
272        /// Where to put the probe: the start of the `=>`, or the range of an existing guard.
273        guard: ArmGuard,
274        removal: (usize, usize),
275    },
276    /// A call or assignment statement that can be deleted.
277    Stmt,
278    /// `-operand`: the minus is dropped.
279    Neg {
280        /// Byte range of the `-` and whatever separates it from the operand.
281        minus: (usize, usize),
282        operand: String,
283        /// The operand is a literal: its type is whatever the context asks for.
284        literal: bool,
285        /// Deleting `-` would glue the preceding and following tokens (`return-1`).
286        needs_space: bool,
287    },
288    /// The guard of a match arm: replaced by `true` or `false`.
289    Guard {
290        /// Whether `false` is a mutant of its own. It is not where the arm can be removed,
291        /// which is the same thing.
292        can_be_false: bool,
293    },
294    /// A field of a struct literal with a base (`..base`): the field is left to the base.
295    Field {
296        name: String,
297        /// Written without its name (`S { name, ..base }`).
298        shorthand: bool,
299        /// The expression used by a probe, where the base can safely be read again.
300        from_base: Option<String>,
301        /// The field and its trailing comma, including any attributes.
302        removal: (usize, usize),
303    },
304    /// The whole body of a function, replaced by a value of its return type. The site's
305    /// range is the inside of the body's braces and its text is the function signature.
306    Body {
307        name: String,
308        value: String,
309        /// The body when it is one expression that only computes its value (no call,
310        /// no macro, nothing assigned, no early exit): replacing the body by the value
311        /// that expression always had is then the same program for the tests.
312        sole_expr: Option<(usize, usize)>,
313    },
314}
315
316#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
317#[serde(rename_all = "snake_case")]
318pub enum RoundingOp {
319    Floor,
320    Ceil,
321    Round,
322    Trunc,
323}
324
325impl RoundingOp {
326    pub fn code(self) -> u8 {
327        match self {
328            RoundingOp::Floor => 0,
329            RoundingOp::Ceil => 1,
330            RoundingOp::Round => 2,
331            RoundingOp::Trunc => 3,
332        }
333    }
334
335    pub fn name(self) -> &'static str {
336        match self {
337            RoundingOp::Floor => "floor",
338            RoundingOp::Ceil => "ceil",
339            RoundingOp::Round => "round",
340            RoundingOp::Trunc => "trunc",
341        }
342    }
343
344    pub fn alt1(self) -> RoundingOp {
345        match self {
346            RoundingOp::Floor => RoundingOp::Trunc,
347            RoundingOp::Ceil => RoundingOp::Floor,
348            RoundingOp::Round => RoundingOp::Floor,
349            RoundingOp::Trunc => RoundingOp::Floor,
350        }
351    }
352
353    pub fn alt2(self) -> RoundingOp {
354        match self {
355            RoundingOp::Floor => RoundingOp::Ceil,
356            RoundingOp::Ceil => RoundingOp::Trunc,
357            RoundingOp::Round => RoundingOp::Trunc,
358            RoundingOp::Trunc => RoundingOp::Round,
359        }
360    }
361
362    /// Concrete input description distinguishing `self` from `other`.
363    pub fn differs_for(self, other: RoundingOp) -> &'static str {
364        match (self, other) {
365            (RoundingOp::Floor, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Floor) => {
366                "negative and not a whole number (e.g. -0.5)"
367            }
368            (RoundingOp::Ceil, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Ceil) => {
369                "positive and not a whole number (e.g. 0.5)"
370            }
371            (RoundingOp::Floor, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Floor) => {
372                "a number that rounds up (e.g. 1.5 or -1.4)"
373            }
374            (RoundingOp::Ceil, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Ceil) => {
375                "a number that rounds down (e.g. 1.4 or -1.6)"
376            }
377            (RoundingOp::Round, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Round) => {
378                "a number that rounds away from zero (e.g. 1.5 or -1.6)"
379            }
380            _ => "not a whole number (e.g. 0.5)",
381        }
382    }
383}
384
385#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
386#[serde(rename_all = "snake_case")]
387pub enum SaturatingOp {
388    SatAdd,
389    SatSub,
390    SatMul,
391    WrapAdd,
392    WrapSub,
393    WrapMul,
394}
395
396impl SaturatingOp {
397    pub fn code(self) -> u8 {
398        match self {
399            SaturatingOp::SatAdd => 0,
400            SaturatingOp::SatSub => 1,
401            SaturatingOp::SatMul => 2,
402            SaturatingOp::WrapAdd => 3,
403            SaturatingOp::WrapSub => 4,
404            SaturatingOp::WrapMul => 5,
405        }
406    }
407
408    pub fn name(self) -> &'static str {
409        match self {
410            SaturatingOp::SatAdd => "saturating_add",
411            SaturatingOp::SatSub => "saturating_sub",
412            SaturatingOp::SatMul => "saturating_mul",
413            SaturatingOp::WrapAdd => "wrapping_add",
414            SaturatingOp::WrapSub => "wrapping_sub",
415            SaturatingOp::WrapMul => "wrapping_mul",
416        }
417    }
418
419    pub fn swapped(self) -> Self {
420        match self {
421            SaturatingOp::SatAdd => SaturatingOp::WrapAdd,
422            SaturatingOp::SatSub => SaturatingOp::WrapSub,
423            SaturatingOp::SatMul => SaturatingOp::WrapMul,
424            SaturatingOp::WrapAdd => SaturatingOp::SatAdd,
425            SaturatingOp::WrapSub => SaturatingOp::SatSub,
426            SaturatingOp::WrapMul => SaturatingOp::SatMul,
427        }
428    }
429
430    pub fn overflow_phrase(self) -> &'static str {
431        match self {
432            SaturatingOp::SatAdd | SaturatingOp::WrapAdd => "whose sum overflows",
433            SaturatingOp::SatSub | SaturatingOp::WrapSub => {
434                "whose difference underflows or overflows"
435            }
436            SaturatingOp::SatMul | SaturatingOp::WrapMul => "whose product overflows",
437        }
438    }
439}
440
441#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
442#[serde(rename_all = "snake_case")]
443pub enum TrimOp {
444    Trim,
445    TrimStart,
446    TrimEnd,
447}
448
449impl TrimOp {
450    pub fn code(self) -> u8 {
451        match self {
452            TrimOp::Trim => 0,
453            TrimOp::TrimStart => 1,
454            TrimOp::TrimEnd => 2,
455        }
456    }
457
458    pub fn name(self) -> &'static str {
459        match self {
460            TrimOp::Trim => "trim",
461            TrimOp::TrimStart => "trim_start",
462            TrimOp::TrimEnd => "trim_end",
463        }
464    }
465
466    pub fn alt1(self) -> Self {
467        match self {
468            TrimOp::Trim => TrimOp::TrimStart,
469            TrimOp::TrimStart | TrimOp::TrimEnd => TrimOp::Trim,
470        }
471    }
472
473    pub fn alt2(self) -> Option<Self> {
474        match self {
475            TrimOp::Trim => Some(TrimOp::TrimEnd),
476            TrimOp::TrimStart | TrimOp::TrimEnd => None,
477        }
478    }
479
480    pub fn whitespace_phrase(self, alt: TrimOp) -> &'static str {
481        match (self, alt) {
482            (TrimOp::Trim, TrimOp::TrimStart) | (TrimOp::TrimStart, TrimOp::Trim) => {
483                "trailing whitespace"
484            }
485            (TrimOp::Trim, TrimOp::TrimEnd) | (TrimOp::TrimEnd, TrimOp::Trim) => {
486                "leading whitespace"
487            }
488            _ => "leading or trailing whitespace",
489        }
490    }
491}
492
493#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
494#[serde(rename_all = "snake_case")]
495pub enum EndianOp {
496    ToBeBytes,
497    ToLeBytes,
498    ToBe,
499    ToLe,
500}
501
502impl EndianOp {
503    pub fn code(self) -> u8 {
504        match self {
505            EndianOp::ToBeBytes => 0,
506            EndianOp::ToLeBytes => 1,
507            EndianOp::ToBe => 2,
508            EndianOp::ToLe => 3,
509        }
510    }
511
512    pub fn name(self) -> &'static str {
513        match self {
514            EndianOp::ToBeBytes => "to_be_bytes",
515            EndianOp::ToLeBytes => "to_le_bytes",
516            EndianOp::ToBe => "to_be",
517            EndianOp::ToLe => "to_le",
518        }
519    }
520
521    pub fn swapped(self) -> Self {
522        match self {
523            EndianOp::ToBeBytes => EndianOp::ToLeBytes,
524            EndianOp::ToLeBytes => EndianOp::ToBeBytes,
525            EndianOp::ToBe => EndianOp::ToLe,
526            EndianOp::ToLe => EndianOp::ToBe,
527        }
528    }
529}
530
531#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
532#[serde(rename_all = "snake_case")]
533pub enum BitCountOp {
534    LeadingZeros,
535    TrailingZeros,
536    LeadingOnes,
537    TrailingOnes,
538}
539
540impl BitCountOp {
541    pub fn code(self) -> u8 {
542        match self {
543            BitCountOp::LeadingZeros => 0,
544            BitCountOp::TrailingZeros => 1,
545            BitCountOp::LeadingOnes => 2,
546            BitCountOp::TrailingOnes => 3,
547        }
548    }
549
550    pub fn name(self) -> &'static str {
551        match self {
552            BitCountOp::LeadingZeros => "leading_zeros",
553            BitCountOp::TrailingZeros => "trailing_zeros",
554            BitCountOp::LeadingOnes => "leading_ones",
555            BitCountOp::TrailingOnes => "trailing_ones",
556        }
557    }
558
559    pub fn swapped(self) -> Self {
560        match self {
561            BitCountOp::LeadingZeros => BitCountOp::TrailingZeros,
562            BitCountOp::TrailingZeros => BitCountOp::LeadingZeros,
563            BitCountOp::LeadingOnes => BitCountOp::TrailingOnes,
564            BitCountOp::TrailingOnes => BitCountOp::LeadingOnes,
565        }
566    }
567}
568
569#[derive(Clone, Copy, PartialEq, Eq, Debug)]
570pub enum RangeIndexMode {
571    /// `&xs[a..b]`
572    Ref,
573    /// `&mut xs[a..b]`
574    RefMut,
575    /// `xs[a..b]` used as the receiver of a known `&self` read-only method.
576    Deref,
577}
578
579#[derive(Clone, PartialEq, Eq, Debug)]
580pub struct RangeIndex {
581    pub mode: RangeIndexMode,
582    /// Source text of the plain-place base expression (e.g. `xs`, `self.source`).
583    pub base: String,
584    /// Byte range from outer expression start to range start (`&xs[`, `&mut xs[`, or `xs[`).
585    pub head: (usize, usize),
586    /// Byte range from range end to closing `]` end.
587    pub close_bracket: (usize, usize),
588}
589
590#[derive(Clone, Copy, PartialEq, Eq, Debug)]
591pub enum ArmGuard {
592    /// No guard: one is added before the `=>` at this offset.
593    None(usize),
594    /// `if <expr>` with the expression at this byte range.
595    Existing(usize, usize),
596}
597
598#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
599#[serde(rename_all = "snake_case")]
600pub enum ArithOp {
601    Add,
602    Sub,
603    Mul,
604    Div,
605    Rem,
606    BitAnd,
607    BitOr,
608    BitXor,
609    Shl,
610    Shr,
611}
612
613impl ArithOp {
614    /// How tightly the operator binds: a higher number before a lower one.
615    pub fn binding(self) -> u8 {
616        match self {
617            ArithOp::Mul | ArithOp::Div | ArithOp::Rem => 5,
618            ArithOp::Add | ArithOp::Sub => 4,
619            ArithOp::Shl | ArithOp::Shr => 3,
620            ArithOp::BitAnd => 2,
621            ArithOp::BitXor => 1,
622            ArithOp::BitOr => 0,
623        }
624    }
625
626    pub const ALL: [ArithOp; 10] = [
627        ArithOp::Add,
628        ArithOp::Sub,
629        ArithOp::Mul,
630        ArithOp::Div,
631        ArithOp::Rem,
632        ArithOp::BitAnd,
633        ArithOp::BitOr,
634        ArithOp::BitXor,
635        ArithOp::Shl,
636        ArithOp::Shr,
637    ];
638
639    pub fn symbol(self) -> &'static str {
640        match self {
641            ArithOp::Add => "+",
642            ArithOp::Sub => "-",
643            ArithOp::Mul => "*",
644            ArithOp::Div => "/",
645            ArithOp::Rem => "%",
646            ArithOp::BitAnd => "&",
647            ArithOp::BitOr => "|",
648            ArithOp::BitXor => "^",
649            ArithOp::Shl => "<<",
650            ArithOp::Shr => ">>",
651        }
652    }
653
654    /// The operator the mutant uses instead.
655    pub fn swapped(self) -> ArithOp {
656        match self {
657            ArithOp::Add => ArithOp::Sub,
658            ArithOp::Sub => ArithOp::Add,
659            ArithOp::Mul => ArithOp::Div,
660            ArithOp::Div => ArithOp::Mul,
661            ArithOp::Rem => ArithOp::Div,
662            ArithOp::BitAnd => ArithOp::BitOr,
663            ArithOp::BitOr => ArithOp::BitAnd,
664            ArithOp::BitXor => ArithOp::BitAnd,
665            ArithOp::Shl => ArithOp::Shr,
666            ArithOp::Shr => ArithOp::Shl,
667        }
668    }
669
670    /// The second operator a mutant uses instead. The shifts have none.
671    pub fn swapped2(self) -> Option<ArithOp> {
672        Some(match self {
673            ArithOp::Add => ArithOp::Mul,
674            ArithOp::Sub => ArithOp::Div,
675            ArithOp::Mul => ArithOp::Add,
676            ArithOp::Div => ArithOp::Rem,
677            ArithOp::Rem => ArithOp::Add,
678            ArithOp::BitAnd | ArithOp::BitOr => ArithOp::BitXor,
679            ArithOp::BitXor => ArithOp::BitOr,
680            ArithOp::Shl | ArithOp::Shr => return None,
681        })
682    }
683
684    /// The second replacement where the operator is written as an assignment or not.
685    /// Bits collected with `|=` from zero are the same bits with `^=`, and `&=` with
686    /// `^=` is rarely a slip of the pen: as in cargo-mutants, those two are left out.
687    pub fn second(self, assign: bool) -> Option<ArithOp> {
688        if assign && matches!(self, ArithOp::BitAnd | ArithOp::BitOr) {
689            return None;
690        }
691        self.swapped2()
692    }
693
694    /// Bit operators exist for integers only, the others for floats too.
695    pub fn is_bitwise(self) -> bool {
696        matches!(
697            self,
698            ArithOp::BitAnd | ArithOp::BitOr | ArithOp::BitXor | ArithOp::Shl | ArithOp::Shr
699        )
700    }
701
702    /// The name the runtime uses for this operator in its probe methods and traits.
703    fn stem(self) -> (&'static str, &'static str) {
704        match self {
705            ArithOp::Add => ("add", "Add"),
706            ArithOp::Sub => ("sub", "Sub"),
707            ArithOp::Mul => ("mul", "Mul"),
708            ArithOp::Div => ("div", "Div"),
709            ArithOp::Rem => ("rem", "Rem"),
710            ArithOp::BitAnd => ("bitand", "BitAnd"),
711            ArithOp::BitOr => ("bitor", "BitOr"),
712            ArithOp::BitXor => ("bitxor", "BitXor"),
713            ArithOp::Shl => ("shl", "Shl"),
714            ArithOp::Shr => ("shr", "Shr"),
715        }
716    }
717
718    /// Name of the probe method in the runtime.
719    pub(crate) fn method(self, assign: bool) -> String {
720        format!(
721            "tmt_{}{}",
722            self.stem().0,
723            if assign { "_assign" } else { "" }
724        )
725    }
726
727    /// Trait with the probe for every type that is not a primitive number.
728    pub(crate) fn fallback_trait(self, assign: bool) -> String {
729        format!("Any{}{}", self.stem().1, if assign { "Assign" } else { "" })
730    }
731
732    /// Trait with the probe for primitive numbers.
733    pub(super) fn prim_trait(self, assign: bool) -> &'static str {
734        match (self.is_bitwise(), assign) {
735            (false, false) => "ArithPrim",
736            (true, false) => "BitPrim",
737            (false, true) => "ArithAssignPrim",
738            (true, true) => "BitAssignPrim",
739        }
740    }
741}
742
743#[derive(Clone, PartialEq, Eq, Debug)]
744pub struct Site {
745    pub id: u32,
746    pub kind: SiteKind,
747    /// 1-based line and column of the start of the site.
748    pub line: usize,
749    pub column: usize,
750    /// Byte range in the original source.
751    pub start: usize,
752    pub end: usize,
753    pub text: String,
754}
755
756#[derive(Debug, PartialEq, Eq)]
757pub struct TestModule {
758    pub name: String,
759    /// The `#[path = "..."]` of the declaration, relative to the declaring file's directory.
760    pub path: Option<String>,
761}
762
763#[derive(Debug, Default)]
764pub struct Discovery {
765    pub sites: Vec<Site>,
766    /// Sites that cannot be switched while the program runs: inside a `const fn`, the
767    /// body of a function that returns `impl Iterator`, a compound assignment to a place
768    /// that is more than a variable. Their mutants are built one by one
769    /// (`source_mutants`). They have no number.
770    pub rebuild_sites: Vec<Site>,
771    /// Functions that return nothing and consist of one statement that is a site: an
772    /// empty body is the mutant that deletes the statement, so it is not made twice.
773    pub single_statement_bodies: Vec<String>,
774    /// Out-of-line modules that only exist under `cfg(test)` (`#[cfg(test)] mod tests;`).
775    pub test_modules: Vec<TestModule>,
776}
777
778/// The number of a site that has none.
779pub const NO_ID: u32 = u32::MAX;