mutagen
mutagen copied to clipboard
Mutation incorrectly infers integer literals
Minimally reproducible example:
use mutagen::mutate;
#[mutate]
fn hi(mut ret: i64) -> i64 {
let sh = if ret > 5 {
0
} else {
8
};
ret += sh - 1;
ret
}
fn main() {
println!("Hello, world! {}", hi(500));
}
Without the macro it compiles fine. with the macro it returns the following:
error[E0277]: cannot add-assign `i32` to `i64`
--> src/main.rs:11:9
|
11 | ret += sh - 1;
| ^^ no implementation for `i64 += i32`
|
= help: the trait `std::ops::AddAssign<i32>` is not implemented for `i64`
mutagen dep: git+https://github.com/llogiq/mutagen.git#99d9e12bf4e28e3a734b36f216650cec09a54c6c
Types are not corrupted. They are incorrectly inferred. Through the code that is generated by mutagen, Rust can no longer do correct propagation of types and the default type for the integer 1
is chosen. Setting an explicit type to the variable sh
fixes this issue.
This is a general problem. There are code snippets where the macro has not enough information to perform a transformation that never vail to infer correct types.
It is possible to fix the given snippet and I will try to extend the current detection mechanism for integer expressions.
Types are not corrupted. They are incorrectly inferred.
Right sorry, English isn't my native language :) Thanks.
@power-fungus this used to work. What changed?
Indeed. I will investigate this. Stay tuned.