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 (source-built).
247    LiteralSet {
248        receiver: String,
249        argument: String,
250        /// `(element text, removal start, removal end)` for each element.
251        elements: Vec<(String, usize, usize)>,
252    },
253    /// `lhs..rhs` or `lhs..=rhs` on integers: the end moves by one.
254    Range {
255        inclusive: bool,
256        lhs: String,
257        rhs: String,
258        gap: (usize, usize),
259    },
260    /// An arm of a `match` that ends in a catch-all arm: without this arm the value falls
261    /// through to the arms below.
262    Arm {
263        pattern: String,
264        /// Where to put the probe: the start of the `=>`, or the range of an existing guard.
265        guard: ArmGuard,
266        removal: (usize, usize),
267    },
268    /// A call or assignment statement that can be deleted.
269    Stmt,
270    /// `-operand`: the minus is dropped.
271    Neg {
272        /// Byte range of the `-` and whatever separates it from the operand.
273        minus: (usize, usize),
274        operand: String,
275        /// The operand is a literal: its type is whatever the context asks for.
276        literal: bool,
277        /// Deleting `-` would glue the preceding and following tokens (`return-1`).
278        needs_space: bool,
279    },
280    /// The guard of a match arm: replaced by `true` or `false`.
281    Guard {
282        /// Whether `false` is a mutant of its own. It is not where the arm can be removed,
283        /// which is the same thing.
284        can_be_false: bool,
285    },
286    /// A field of a struct literal with a base (`..base`): the field is left to the base.
287    Field {
288        name: String,
289        /// Written without its name (`S { name, ..base }`).
290        shorthand: bool,
291        /// The expression used by a probe, where the base can safely be read again.
292        from_base: Option<String>,
293        /// The field and its trailing comma, including any attributes.
294        removal: (usize, usize),
295    },
296    /// The whole body of a function, replaced by a value of its return type. The site's
297    /// range is the inside of the body's braces and its text is the function signature.
298    Body {
299        name: String,
300        value: String,
301        /// The body when it is one expression that only computes its value (no call,
302        /// no macro, nothing assigned, no early exit): replacing the body by the value
303        /// that expression always had is then the same program for the tests.
304        sole_expr: Option<(usize, usize)>,
305    },
306}
307
308#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
309#[serde(rename_all = "snake_case")]
310pub enum RoundingOp {
311    Floor,
312    Ceil,
313    Round,
314    Trunc,
315}
316
317impl RoundingOp {
318    pub fn code(self) -> u8 {
319        match self {
320            RoundingOp::Floor => 0,
321            RoundingOp::Ceil => 1,
322            RoundingOp::Round => 2,
323            RoundingOp::Trunc => 3,
324        }
325    }
326
327    pub fn name(self) -> &'static str {
328        match self {
329            RoundingOp::Floor => "floor",
330            RoundingOp::Ceil => "ceil",
331            RoundingOp::Round => "round",
332            RoundingOp::Trunc => "trunc",
333        }
334    }
335
336    pub fn alt1(self) -> RoundingOp {
337        match self {
338            RoundingOp::Floor => RoundingOp::Trunc,
339            RoundingOp::Ceil => RoundingOp::Floor,
340            RoundingOp::Round => RoundingOp::Floor,
341            RoundingOp::Trunc => RoundingOp::Floor,
342        }
343    }
344
345    pub fn alt2(self) -> RoundingOp {
346        match self {
347            RoundingOp::Floor => RoundingOp::Ceil,
348            RoundingOp::Ceil => RoundingOp::Trunc,
349            RoundingOp::Round => RoundingOp::Trunc,
350            RoundingOp::Trunc => RoundingOp::Round,
351        }
352    }
353
354    /// Concrete input description distinguishing `self` from `other`.
355    pub fn differs_for(self, other: RoundingOp) -> &'static str {
356        match (self, other) {
357            (RoundingOp::Floor, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Floor) => {
358                "negative and not a whole number (e.g. -0.5)"
359            }
360            (RoundingOp::Ceil, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Ceil) => {
361                "positive and not a whole number (e.g. 0.5)"
362            }
363            (RoundingOp::Floor, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Floor) => {
364                "a number that rounds up (e.g. 1.5 or -1.4)"
365            }
366            (RoundingOp::Ceil, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Ceil) => {
367                "a number that rounds down (e.g. 1.4 or -1.6)"
368            }
369            (RoundingOp::Round, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Round) => {
370                "a number that rounds away from zero (e.g. 1.5 or -1.6)"
371            }
372            _ => "not a whole number (e.g. 0.5)",
373        }
374    }
375}
376
377#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
378#[serde(rename_all = "snake_case")]
379pub enum SaturatingOp {
380    SatAdd,
381    SatSub,
382    SatMul,
383    WrapAdd,
384    WrapSub,
385    WrapMul,
386}
387
388impl SaturatingOp {
389    pub fn code(self) -> u8 {
390        match self {
391            SaturatingOp::SatAdd => 0,
392            SaturatingOp::SatSub => 1,
393            SaturatingOp::SatMul => 2,
394            SaturatingOp::WrapAdd => 3,
395            SaturatingOp::WrapSub => 4,
396            SaturatingOp::WrapMul => 5,
397        }
398    }
399
400    pub fn name(self) -> &'static str {
401        match self {
402            SaturatingOp::SatAdd => "saturating_add",
403            SaturatingOp::SatSub => "saturating_sub",
404            SaturatingOp::SatMul => "saturating_mul",
405            SaturatingOp::WrapAdd => "wrapping_add",
406            SaturatingOp::WrapSub => "wrapping_sub",
407            SaturatingOp::WrapMul => "wrapping_mul",
408        }
409    }
410
411    pub fn swapped(self) -> Self {
412        match self {
413            SaturatingOp::SatAdd => SaturatingOp::WrapAdd,
414            SaturatingOp::SatSub => SaturatingOp::WrapSub,
415            SaturatingOp::SatMul => SaturatingOp::WrapMul,
416            SaturatingOp::WrapAdd => SaturatingOp::SatAdd,
417            SaturatingOp::WrapSub => SaturatingOp::SatSub,
418            SaturatingOp::WrapMul => SaturatingOp::SatMul,
419        }
420    }
421
422    pub fn overflow_phrase(self) -> &'static str {
423        match self {
424            SaturatingOp::SatAdd | SaturatingOp::WrapAdd => "whose sum overflows",
425            SaturatingOp::SatSub | SaturatingOp::WrapSub => {
426                "whose difference underflows or overflows"
427            }
428            SaturatingOp::SatMul | SaturatingOp::WrapMul => "whose product overflows",
429        }
430    }
431}
432
433#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
434#[serde(rename_all = "snake_case")]
435pub enum TrimOp {
436    Trim,
437    TrimStart,
438    TrimEnd,
439}
440
441impl TrimOp {
442    pub fn code(self) -> u8 {
443        match self {
444            TrimOp::Trim => 0,
445            TrimOp::TrimStart => 1,
446            TrimOp::TrimEnd => 2,
447        }
448    }
449
450    pub fn name(self) -> &'static str {
451        match self {
452            TrimOp::Trim => "trim",
453            TrimOp::TrimStart => "trim_start",
454            TrimOp::TrimEnd => "trim_end",
455        }
456    }
457
458    pub fn alt1(self) -> Self {
459        match self {
460            TrimOp::Trim => TrimOp::TrimStart,
461            TrimOp::TrimStart | TrimOp::TrimEnd => TrimOp::Trim,
462        }
463    }
464
465    pub fn alt2(self) -> Option<Self> {
466        match self {
467            TrimOp::Trim => Some(TrimOp::TrimEnd),
468            TrimOp::TrimStart | TrimOp::TrimEnd => None,
469        }
470    }
471
472    pub fn whitespace_phrase(self, alt: TrimOp) -> &'static str {
473        match (self, alt) {
474            (TrimOp::Trim, TrimOp::TrimStart) | (TrimOp::TrimStart, TrimOp::Trim) => {
475                "trailing whitespace"
476            }
477            (TrimOp::Trim, TrimOp::TrimEnd) | (TrimOp::TrimEnd, TrimOp::Trim) => {
478                "leading whitespace"
479            }
480            _ => "leading or trailing whitespace",
481        }
482    }
483}
484
485#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
486#[serde(rename_all = "snake_case")]
487pub enum EndianOp {
488    ToBeBytes,
489    ToLeBytes,
490    ToBe,
491    ToLe,
492}
493
494impl EndianOp {
495    pub fn code(self) -> u8 {
496        match self {
497            EndianOp::ToBeBytes => 0,
498            EndianOp::ToLeBytes => 1,
499            EndianOp::ToBe => 2,
500            EndianOp::ToLe => 3,
501        }
502    }
503
504    pub fn name(self) -> &'static str {
505        match self {
506            EndianOp::ToBeBytes => "to_be_bytes",
507            EndianOp::ToLeBytes => "to_le_bytes",
508            EndianOp::ToBe => "to_be",
509            EndianOp::ToLe => "to_le",
510        }
511    }
512
513    pub fn swapped(self) -> Self {
514        match self {
515            EndianOp::ToBeBytes => EndianOp::ToLeBytes,
516            EndianOp::ToLeBytes => EndianOp::ToBeBytes,
517            EndianOp::ToBe => EndianOp::ToLe,
518            EndianOp::ToLe => EndianOp::ToBe,
519        }
520    }
521}
522
523#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
524#[serde(rename_all = "snake_case")]
525pub enum BitCountOp {
526    LeadingZeros,
527    TrailingZeros,
528    LeadingOnes,
529    TrailingOnes,
530}
531
532impl BitCountOp {
533    pub fn code(self) -> u8 {
534        match self {
535            BitCountOp::LeadingZeros => 0,
536            BitCountOp::TrailingZeros => 1,
537            BitCountOp::LeadingOnes => 2,
538            BitCountOp::TrailingOnes => 3,
539        }
540    }
541
542    pub fn name(self) -> &'static str {
543        match self {
544            BitCountOp::LeadingZeros => "leading_zeros",
545            BitCountOp::TrailingZeros => "trailing_zeros",
546            BitCountOp::LeadingOnes => "leading_ones",
547            BitCountOp::TrailingOnes => "trailing_ones",
548        }
549    }
550
551    pub fn swapped(self) -> Self {
552        match self {
553            BitCountOp::LeadingZeros => BitCountOp::TrailingZeros,
554            BitCountOp::TrailingZeros => BitCountOp::LeadingZeros,
555            BitCountOp::LeadingOnes => BitCountOp::TrailingOnes,
556            BitCountOp::TrailingOnes => BitCountOp::LeadingOnes,
557        }
558    }
559}
560
561#[derive(Clone, Copy, PartialEq, Eq, Debug)]
562pub enum ArmGuard {
563    /// No guard: one is added before the `=>` at this offset.
564    None(usize),
565    /// `if <expr>` with the expression at this byte range.
566    Existing(usize, usize),
567}
568
569#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
570#[serde(rename_all = "snake_case")]
571pub enum ArithOp {
572    Add,
573    Sub,
574    Mul,
575    Div,
576    Rem,
577    BitAnd,
578    BitOr,
579    BitXor,
580    Shl,
581    Shr,
582}
583
584impl ArithOp {
585    /// How tightly the operator binds: a higher number before a lower one.
586    pub fn binding(self) -> u8 {
587        match self {
588            ArithOp::Mul | ArithOp::Div | ArithOp::Rem => 5,
589            ArithOp::Add | ArithOp::Sub => 4,
590            ArithOp::Shl | ArithOp::Shr => 3,
591            ArithOp::BitAnd => 2,
592            ArithOp::BitXor => 1,
593            ArithOp::BitOr => 0,
594        }
595    }
596
597    pub const ALL: [ArithOp; 10] = [
598        ArithOp::Add,
599        ArithOp::Sub,
600        ArithOp::Mul,
601        ArithOp::Div,
602        ArithOp::Rem,
603        ArithOp::BitAnd,
604        ArithOp::BitOr,
605        ArithOp::BitXor,
606        ArithOp::Shl,
607        ArithOp::Shr,
608    ];
609
610    pub fn symbol(self) -> &'static str {
611        match self {
612            ArithOp::Add => "+",
613            ArithOp::Sub => "-",
614            ArithOp::Mul => "*",
615            ArithOp::Div => "/",
616            ArithOp::Rem => "%",
617            ArithOp::BitAnd => "&",
618            ArithOp::BitOr => "|",
619            ArithOp::BitXor => "^",
620            ArithOp::Shl => "<<",
621            ArithOp::Shr => ">>",
622        }
623    }
624
625    /// The operator the mutant uses instead.
626    pub fn swapped(self) -> ArithOp {
627        match self {
628            ArithOp::Add => ArithOp::Sub,
629            ArithOp::Sub => ArithOp::Add,
630            ArithOp::Mul => ArithOp::Div,
631            ArithOp::Div => ArithOp::Mul,
632            ArithOp::Rem => ArithOp::Div,
633            ArithOp::BitAnd => ArithOp::BitOr,
634            ArithOp::BitOr => ArithOp::BitAnd,
635            ArithOp::BitXor => ArithOp::BitAnd,
636            ArithOp::Shl => ArithOp::Shr,
637            ArithOp::Shr => ArithOp::Shl,
638        }
639    }
640
641    /// The second operator a mutant uses instead. The shifts have none.
642    pub fn swapped2(self) -> Option<ArithOp> {
643        Some(match self {
644            ArithOp::Add => ArithOp::Mul,
645            ArithOp::Sub => ArithOp::Div,
646            ArithOp::Mul => ArithOp::Add,
647            ArithOp::Div => ArithOp::Rem,
648            ArithOp::Rem => ArithOp::Add,
649            ArithOp::BitAnd | ArithOp::BitOr => ArithOp::BitXor,
650            ArithOp::BitXor => ArithOp::BitOr,
651            ArithOp::Shl | ArithOp::Shr => return None,
652        })
653    }
654
655    /// The second replacement where the operator is written as an assignment or not.
656    /// Bits collected with `|=` from zero are the same bits with `^=`, and `&=` with
657    /// `^=` is rarely a slip of the pen: as in cargo-mutants, those two are left out.
658    pub fn second(self, assign: bool) -> Option<ArithOp> {
659        if assign && matches!(self, ArithOp::BitAnd | ArithOp::BitOr) {
660            return None;
661        }
662        self.swapped2()
663    }
664
665    /// Bit operators exist for integers only, the others for floats too.
666    pub fn is_bitwise(self) -> bool {
667        matches!(
668            self,
669            ArithOp::BitAnd | ArithOp::BitOr | ArithOp::BitXor | ArithOp::Shl | ArithOp::Shr
670        )
671    }
672
673    /// The name the runtime uses for this operator in its probe methods and traits.
674    fn stem(self) -> (&'static str, &'static str) {
675        match self {
676            ArithOp::Add => ("add", "Add"),
677            ArithOp::Sub => ("sub", "Sub"),
678            ArithOp::Mul => ("mul", "Mul"),
679            ArithOp::Div => ("div", "Div"),
680            ArithOp::Rem => ("rem", "Rem"),
681            ArithOp::BitAnd => ("bitand", "BitAnd"),
682            ArithOp::BitOr => ("bitor", "BitOr"),
683            ArithOp::BitXor => ("bitxor", "BitXor"),
684            ArithOp::Shl => ("shl", "Shl"),
685            ArithOp::Shr => ("shr", "Shr"),
686        }
687    }
688
689    /// Name of the probe method in the runtime.
690    pub(crate) fn method(self, assign: bool) -> String {
691        format!(
692            "tmt_{}{}",
693            self.stem().0,
694            if assign { "_assign" } else { "" }
695        )
696    }
697
698    /// Trait with the probe for every type that is not a primitive number.
699    pub(crate) fn fallback_trait(self, assign: bool) -> String {
700        format!("Any{}{}", self.stem().1, if assign { "Assign" } else { "" })
701    }
702
703    /// Trait with the probe for primitive numbers.
704    pub(super) fn prim_trait(self, assign: bool) -> &'static str {
705        match (self.is_bitwise(), assign) {
706            (false, false) => "ArithPrim",
707            (true, false) => "BitPrim",
708            (false, true) => "ArithAssignPrim",
709            (true, true) => "BitAssignPrim",
710        }
711    }
712}
713
714#[derive(Clone, PartialEq, Eq, Debug)]
715pub struct Site {
716    pub id: u32,
717    pub kind: SiteKind,
718    /// 1-based line and column of the start of the site.
719    pub line: usize,
720    pub column: usize,
721    /// Byte range in the original source.
722    pub start: usize,
723    pub end: usize,
724    pub text: String,
725}
726
727#[derive(Debug, PartialEq, Eq)]
728pub struct TestModule {
729    pub name: String,
730    /// The `#[path = "..."]` of the declaration, relative to the declaring file's directory.
731    pub path: Option<String>,
732}
733
734#[derive(Debug, Default)]
735pub struct Discovery {
736    pub sites: Vec<Site>,
737    /// Sites that cannot be switched while the program runs: inside a `const fn`, the
738    /// body of a function that returns `impl Iterator`, a compound assignment to a place
739    /// that is more than a variable. Their mutants are built one by one
740    /// (`source_mutants`). They have no number.
741    pub rebuild_sites: Vec<Site>,
742    /// Functions that return nothing and consist of one statement that is a site: an
743    /// empty body is the mutant that deletes the statement, so it is not made twice.
744    pub single_statement_bodies: Vec<String>,
745    /// Out-of-line modules that only exist under `cfg(test)` (`#[cfg(test)] mod tests;`).
746    pub test_modules: Vec<TestModule>,
747}
748
749/// The number of a site that has none.
750pub const NO_ID: u32 = u32::MAX;