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
//! Data types describing discovered mutation sites.
use serde::Serialize;
use crate::plan::CmpOp;
/// Name of the module injected into the crate root.
pub const RUNTIME_MOD: &str = "__tmt_rt";
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum LiteralSide {
None,
Rhs(i128),
Lhs(i128),
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum SiteKind {
Cmp {
op: CmpOp,
lhs: String,
rhs: String,
literal: LiteralSide,
/// Byte range of the operator and the whitespace around it.
gap: (usize, usize),
},
/// `lhs && rhs` or `lhs || rhs`.
Logic {
and: bool,
lhs: String,
rhs: String,
gap: (usize, usize),
},
/// `lhs + rhs` and friends. Only primitive numbers are mutated; for other operand
/// types the probe performs the original operation and reports nothing.
Arith {
op: ArithOp,
/// `lhs op= rhs` rather than `lhs op rhs`.
assign: bool,
lhs: String,
rhs: String,
gap: (usize, usize),
},
/// `!operand`. Only `bool` is mutated (the `!` is dropped).
Not {
/// Byte range of the `!` and whatever separates it from the operand.
bang: (usize, usize),
operand: String,
/// Deleting `!` would glue the preceding and following tokens (`return!x`).
needs_space: bool,
},
/// The result of a function whose exits reach the probe. The site's range is the
/// inside of the body's braces and its text is the function signature.
Return {
name: String,
/// `None` for a function returning `bool`; for one returning `Option<T>`, the
/// return type as written.
option: Option<String>,
/// Byte range of the body when it is a single expression: the function's result
/// is then exactly that expression's value.
sole_expr: Option<(usize, usize)>,
},
/// `receiver.max(argument)` or `.min(..)`: the other of the two.
MinMax {
max: bool,
receiver: String,
argument: String,
/// Byte ranges of `.max(` and of the closing `)`.
call: (usize, usize),
close: (usize, usize),
},
/// `receiver.first()` or `.last()` of a slice, a `Vec`, an array or a `BTreeSet`: the
/// other end. The two are the same element while there is at most one.
SliceEnd {
first: bool,
receiver: String,
/// Byte range of `.first()`.
call: (usize, usize),
/// Byte range of the method's name.
method: (usize, usize),
},
/// `lhs..rhs` or `lhs..=rhs` on integers: the end moves by one.
Range {
inclusive: bool,
lhs: String,
rhs: String,
gap: (usize, usize),
},
/// An arm of a `match` that ends in a catch-all arm: without this arm the value falls
/// through to the arms below.
Arm {
pattern: String,
/// Where to put the probe: the start of the `=>`, or the range of an existing guard.
guard: ArmGuard,
removal: (usize, usize),
},
/// A call or assignment statement that can be deleted.
Stmt,
/// `-operand`: the minus is dropped.
Neg {
/// Byte range of the `-` and whatever separates it from the operand.
minus: (usize, usize),
operand: String,
/// The operand is a literal: its type is whatever the context asks for.
literal: bool,
/// Deleting `-` would glue the preceding and following tokens (`return-1`).
needs_space: bool,
},
/// The guard of a match arm: replaced by `true` or `false`.
Guard {
/// Whether `false` is a mutant of its own. It is not where the arm can be removed,
/// which is the same thing.
can_be_false: bool,
},
/// A field of a struct literal with a base (`..base`): the field is left to the base.
Field {
name: String,
/// Written without its name (`S { name, ..base }`).
shorthand: bool,
/// The expression used by a probe, where the base can safely be read again.
from_base: Option<String>,
/// The field and its trailing comma, including any attributes.
removal: (usize, usize),
},
/// The whole body of a function, replaced by a value of its return type. The site's
/// range is the inside of the body's braces and its text is the function signature.
Body {
name: String,
value: String,
/// The body when it is one expression that only computes its value (no call,
/// no macro, nothing assigned, no early exit): replacing the body by the value
/// that expression always had is then the same program for the tests.
sole_expr: Option<(usize, usize)>,
},
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ArmGuard {
/// No guard: one is added before the `=>` at this offset.
None(usize),
/// `if <expr>` with the expression at this byte range.
Existing(usize, usize),
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ArithOp {
Add,
Sub,
Mul,
Div,
Rem,
BitAnd,
BitOr,
BitXor,
Shl,
Shr,
}
impl ArithOp {
/// How tightly the operator binds: a higher number before a lower one.
pub fn binding(self) -> u8 {
match self {
ArithOp::Mul | ArithOp::Div | ArithOp::Rem => 5,
ArithOp::Add | ArithOp::Sub => 4,
ArithOp::Shl | ArithOp::Shr => 3,
ArithOp::BitAnd => 2,
ArithOp::BitXor => 1,
ArithOp::BitOr => 0,
}
}
pub const ALL: [ArithOp; 10] = [
ArithOp::Add,
ArithOp::Sub,
ArithOp::Mul,
ArithOp::Div,
ArithOp::Rem,
ArithOp::BitAnd,
ArithOp::BitOr,
ArithOp::BitXor,
ArithOp::Shl,
ArithOp::Shr,
];
pub fn symbol(self) -> &'static str {
match self {
ArithOp::Add => "+",
ArithOp::Sub => "-",
ArithOp::Mul => "*",
ArithOp::Div => "/",
ArithOp::Rem => "%",
ArithOp::BitAnd => "&",
ArithOp::BitOr => "|",
ArithOp::BitXor => "^",
ArithOp::Shl => "<<",
ArithOp::Shr => ">>",
}
}
/// The operator the mutant uses instead.
pub fn swapped(self) -> ArithOp {
match self {
ArithOp::Add => ArithOp::Sub,
ArithOp::Sub => ArithOp::Add,
ArithOp::Mul => ArithOp::Div,
ArithOp::Div => ArithOp::Mul,
ArithOp::Rem => ArithOp::Div,
ArithOp::BitAnd => ArithOp::BitOr,
ArithOp::BitOr => ArithOp::BitAnd,
ArithOp::BitXor => ArithOp::BitAnd,
ArithOp::Shl => ArithOp::Shr,
ArithOp::Shr => ArithOp::Shl,
}
}
/// The second operator a mutant uses instead. The shifts have none.
pub fn swapped2(self) -> Option<ArithOp> {
Some(match self {
ArithOp::Add => ArithOp::Mul,
ArithOp::Sub => ArithOp::Div,
ArithOp::Mul => ArithOp::Add,
ArithOp::Div => ArithOp::Rem,
ArithOp::Rem => ArithOp::Add,
ArithOp::BitAnd | ArithOp::BitOr => ArithOp::BitXor,
ArithOp::BitXor => ArithOp::BitOr,
ArithOp::Shl | ArithOp::Shr => return None,
})
}
/// The second replacement where the operator is written as an assignment or not.
/// Bits collected with `|=` from zero are the same bits with `^=`, and `&=` with
/// `^=` is rarely a slip of the pen: as in cargo-mutants, those two are left out.
pub fn second(self, assign: bool) -> Option<ArithOp> {
if assign && matches!(self, ArithOp::BitAnd | ArithOp::BitOr) {
return None;
}
self.swapped2()
}
/// Bit operators exist for integers only, the others for floats too.
pub fn is_bitwise(self) -> bool {
matches!(
self,
ArithOp::BitAnd | ArithOp::BitOr | ArithOp::BitXor | ArithOp::Shl | ArithOp::Shr
)
}
/// The name the runtime uses for this operator in its probe methods and traits.
fn stem(self) -> (&'static str, &'static str) {
match self {
ArithOp::Add => ("add", "Add"),
ArithOp::Sub => ("sub", "Sub"),
ArithOp::Mul => ("mul", "Mul"),
ArithOp::Div => ("div", "Div"),
ArithOp::Rem => ("rem", "Rem"),
ArithOp::BitAnd => ("bitand", "BitAnd"),
ArithOp::BitOr => ("bitor", "BitOr"),
ArithOp::BitXor => ("bitxor", "BitXor"),
ArithOp::Shl => ("shl", "Shl"),
ArithOp::Shr => ("shr", "Shr"),
}
}
/// Name of the probe method in the runtime.
pub(crate) fn method(self, assign: bool) -> String {
format!(
"tmt_{}{}",
self.stem().0,
if assign { "_assign" } else { "" }
)
}
/// Trait with the probe for every type that is not a primitive number.
pub(crate) fn fallback_trait(self, assign: bool) -> String {
format!("Any{}{}", self.stem().1, if assign { "Assign" } else { "" })
}
/// Trait with the probe for primitive numbers.
pub(super) fn prim_trait(self, assign: bool) -> &'static str {
match (self.is_bitwise(), assign) {
(false, false) => "ArithPrim",
(true, false) => "BitPrim",
(false, true) => "ArithAssignPrim",
(true, true) => "BitAssignPrim",
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Site {
pub id: u32,
pub kind: SiteKind,
/// 1-based line and column of the start of the site.
pub line: usize,
pub column: usize,
/// Byte range in the original source.
pub start: usize,
pub end: usize,
pub text: String,
}
#[derive(Debug, PartialEq, Eq)]
pub struct TestModule {
pub name: String,
/// The `#[path = "..."]` of the declaration, relative to the declaring file's directory.
pub path: Option<String>,
}
#[derive(Debug, Default)]
pub struct Discovery {
pub sites: Vec<Site>,
/// Sites that cannot be switched while the program runs: inside a `const fn`, the
/// body of a function that returns `impl Iterator`, a compound assignment to a place
/// that is more than a variable. Their mutants are built one by one
/// (`source_mutants`). They have no number.
pub rebuild_sites: Vec<Site>,
/// Functions that return nothing and consist of one statement that is a site: an
/// empty body is the mutant that deletes the statement, so it is not made twice.
pub single_statement_bodies: Vec<String>,
/// Out-of-line modules that only exist under `cfg(test)` (`#[cfg(test)] mod tests;`).
pub test_modules: Vec<TestModule>,
}
/// The number of a site that has none.
pub const NO_ID: u32 = u32::MAX;