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

Is it possible to replace try! with ? #7

Closed
kornelski opened this issue Apr 15, 2018 · 4 comments
Closed

Is it possible to replace try! with ? #7

kornelski opened this issue Apr 15, 2018 · 4 comments

Comments

@kornelski
Copy link
Contributor

I'm trying to write a rule that replaces try!(x) with x?, but it seems tricky.

This seems to compile (I've had to add a return type):

fn rule1<T,E,X: From<E>>(r: Result<T,E>) -> Result<T,X> {
    replace!(try!(r) => r?);
    Ok(r?)
}

but it doesn't match anything. Without return type I can't use try! nor ? in the pattern, since they expand to return, which rustc typechecks.

Can this replacement be done?

@davidlattimore
Copy link
Contributor

Thanks for the report. I tried your rule exactly as written and it worked for me. Initially however I wasn't getting any matches. After some investigation, it seems that the code that does a sort of local "cargo clean" isn't working any more, so it wasn't attempting to match any code at all. Could you try doing a "cargo clean" before running your rule and see if that helps?

If cargo clean doesn't help, could you supply some code that you think should be matched but isn't?

For background, when you run cargo rerast, it runs "cargo check", but with rerast as the compiler. If cargo thinks everything has already been built/checked, it never calls rerast. So I have a stage that queries cargo to determine bits of the local crate that can be built, then asks cargo to clean those artifacts. That way when it runs cargo check, it'll rerun the "compiler" (rerast) for those targets.

Not sure what might be wrong with it, but I'll have a look this evening.

@davidlattimore
Copy link
Contributor

Oh, and regarding the use of return, yep that's the way to do it. I should probably expand the README to cover this case. Actually, perhaps I should add a new section to the documentation with a larger number of sample rules. You can if you prefer replace the last line of your rule with unreachable!() - that's what I do in my tests that have return types.

@kornelski
Copy link
Contributor Author

Thanks! It turned out it was PEBKAC. I've been testing on cc-rs, where only uses of try! are in a module that is compiled only conditionally, not on my platform.

More examples in the README would be super helpful.

@davidlattimore
Copy link
Contributor

Cool. Glad you tracked it down and thanks for using rerast. I've raised #10 to resolve the issue I mentioned above. I'll also try to have a look at the other issues you raised. Thanks for the reports!

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