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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! 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, Copy, PartialEq, Eq, Debug)]
pub enum BitValueType {
Unsigned(u32),
Signed(u32),
Bool,
Char,
}
impl BitValueType {
pub(crate) fn width(self) -> u32 {
match self {
BitValueType::Unsigned(w) | BitValueType::Signed(w) => w,
BitValueType::Bool => 1,
BitValueType::Char => 21,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum LocalBitType {
Value(BitValueType),
SliceOf(BitValueType),
}
#[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),
/// Static primitive types of local bindings referenced by `|` / `^` operands.
local_types: Vec<(String, LocalBitType)>,
/// Primitive type names shadowed by type aliases/items in this file.
shadowed_prims: Vec<String>,
/// Runtime cast-source type observation probes `(slot, start, end)` inside `|` / `^`.
cast_probes: Vec<(u32, 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),
},
/// `receiver.starts_with(pattern)` or `.ends_with(pattern)`: relaxed to `.contains(pattern)`.
StrAffix {
starts: bool,
receiver: String,
pattern: String,
/// Byte range of `.starts_with(` (from receiver end to pattern start).
call: (usize, usize),
/// Byte range of the closing `)` (from pattern end to closing paren end).
close: (usize, usize),
/// Byte range of the method name.
method: (usize, usize),
},
/// `receiver.replace(from, to)` <-> `receiver.replacen(from, to, 1)`.
StrReplace {
/// `true` for `.replace(from, to)`, `false` for `.replacen(from, to, 1)`.
replace_all: bool,
receiver: String,
from: String,
to: String,
/// Byte range from receiver end to `from` start.
call: (usize, usize),
/// Byte range from `from` end to `to` start.
comma: (usize, usize),
/// Byte range from `to` end to closing `)` end (includes `, 1)` for `replacen`).
close: (usize, usize),
/// Byte range of the method name.
method: (usize, usize),
},
/// `receiver.clamp(low, high)`: drop lower bound (`.min(high)`) or upper bound (`.max(low)`).
Clamp {
receiver: String,
low: String,
high: String,
/// Byte range from receiver end to `low` start.
call: (usize, usize),
/// Byte range from `low` end to `high` start.
comma: (usize, usize),
/// Byte range from `high` end to closing `)` end.
close: (usize, usize),
},
/// `receiver.floor()` / `.ceil()` / `.round()` / `.trunc()` on floats.
Rounding {
op: RoundingOp,
receiver: String,
/// Byte range of `.floor()`.
call: (usize, usize),
/// Byte range of the method name.
method: (usize, usize),
},
/// `receiver.all(pred)` <-> `receiver.any(pred)` on an iterator with a closure predicate.
Quantifier {
all: bool,
receiver: String,
predicate: String,
/// Byte range from receiver end to predicate start.
call: (usize, usize),
/// Byte range from predicate end to closing `)` end.
close: (usize, usize),
/// Byte range of the method name.
method: (usize, usize),
},
/// `[lit1, lit2, ...].contains(&x)`: drop one literal element at a time (source-built).
LiteralSet {
receiver: String,
argument: String,
/// `(element text, removal start, removal end)` for each element.
elements: Vec<(String, 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, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RoundingOp {
Floor,
Ceil,
Round,
Trunc,
}
impl RoundingOp {
pub fn code(self) -> u8 {
match self {
RoundingOp::Floor => 0,
RoundingOp::Ceil => 1,
RoundingOp::Round => 2,
RoundingOp::Trunc => 3,
}
}
pub fn name(self) -> &'static str {
match self {
RoundingOp::Floor => "floor",
RoundingOp::Ceil => "ceil",
RoundingOp::Round => "round",
RoundingOp::Trunc => "trunc",
}
}
pub fn alt1(self) -> RoundingOp {
match self {
RoundingOp::Floor => RoundingOp::Trunc,
RoundingOp::Ceil => RoundingOp::Floor,
RoundingOp::Round => RoundingOp::Floor,
RoundingOp::Trunc => RoundingOp::Floor,
}
}
pub fn alt2(self) -> RoundingOp {
match self {
RoundingOp::Floor => RoundingOp::Ceil,
RoundingOp::Ceil => RoundingOp::Trunc,
RoundingOp::Round => RoundingOp::Trunc,
RoundingOp::Trunc => RoundingOp::Round,
}
}
/// Concrete input description distinguishing `self` from `other`.
pub fn differs_for(self, other: RoundingOp) -> &'static str {
match (self, other) {
(RoundingOp::Floor, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Floor) => {
"negative and not a whole number (e.g. -0.5)"
}
(RoundingOp::Ceil, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Ceil) => {
"positive and not a whole number (e.g. 0.5)"
}
(RoundingOp::Floor, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Floor) => {
"a number that rounds up (e.g. 1.5 or -1.4)"
}
(RoundingOp::Ceil, RoundingOp::Round) | (RoundingOp::Round, RoundingOp::Ceil) => {
"a number that rounds down (e.g. 1.4 or -1.6)"
}
(RoundingOp::Round, RoundingOp::Trunc) | (RoundingOp::Trunc, RoundingOp::Round) => {
"a number that rounds away from zero (e.g. 1.5 or -1.6)"
}
_ => "not a whole number (e.g. 0.5)",
}
}
}
#[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;