Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using mutagen breaks type inference in the existing program #164

Open
joshlf opened this issue Mar 20, 2020 · 4 comments
Open

Using mutagen breaks type inference in the existing program #164

joshlf opened this issue Mar 20, 2020 · 4 comments

Comments

@joshlf
Copy link

joshlf commented Mar 20, 2020

Given the following lib.rs:

fn foo() {}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
	let x = [0u8];
	assert_eq!((&x[..]).as_ref(), []);
    }
}

Running cargo check --tests succeeds. However, if we add an attribute, #[cfg_attr(test, mutagen::mutate)], to fn foo() {}, then cargo check --tests fails:

error[E0282]: type annotations needed
 --> src/lib.rs:9:2
  |
9 |     assert_eq!((&x[..]).as_ref(), []);
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
  |
  = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

Version information

Using Mutagen pulled from this repository at 94321e5.

$ cargo version
cargo 1.43.0-nightly (bda50510d 2020-03-02)
@samuelpilz
Copy link
Contributor

This is indeed unfortunate. I investigated this same issue some time ago: https://users.rust-lang.org/t/unexpected-compilation-error-with-procedural-macros/34310

The problem also appears when adding use json::parse; (when json is a dependency) instead of applying the #[mutate] procedural macro

@joshlf
Copy link
Author

joshlf commented Mar 20, 2020

Ah so based on that issue, and from perusing the code, I suspect that just removing the dependency on json may do the trick. In particular:

  • Mutagen itself never implements PartialEq for any foreign types
  • Code generated by Mutagen doesn't import anything other than the mutagen crate

Thus, I can't think of where else the issue could be coming from. From a quick skim of the code, it looks like it should be pretty straightforward to replace the existing uses of the json crate with uses of serde_json instead.

@samuelpilz
Copy link
Contributor

samuelpilz commented Mar 20, 2020

I thought serde_json used json internally. This is not the case.

One of the latest refactorings brought the json dependency into the core crate. I will move the only function that requires json into the runner crate (which is allowed to use the json dependency and it helps a lot)

Edit: This analysis is incorrect. I will try to substitute with serde_json instead

@samuelpilz
Copy link
Contributor

serde_json seems to use the same PartialEq impls: https://github.com/serde-rs/json/blob/master/src/value/partial_eq.rs

adding use serde_json::from_str; to your example above produces the same error message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants