mutagen icon indicating copy to clipboard operation
mutagen copied to clipboard

Mutation incorrectly infers integer literals

Open elichai opened this issue 4 years ago • 4 comments

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

elichai avatar Jan 23 '20 11:01 elichai

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.

samuelpilz avatar Jan 23 '20 12:01 samuelpilz

Types are not corrupted. They are incorrectly inferred.

Right sorry, English isn't my native language :) Thanks.

elichai avatar Jan 23 '20 13:01 elichai

@power-fungus this used to work. What changed?

llogiq avatar Jan 23 '20 18:01 llogiq

Indeed. I will investigate this. Stay tuned.

samuelpilz avatar Jan 24 '20 09:01 samuelpilz