Stream: t-compiler/help

Topic: rustfix for `try!` to `?`


Josh Triplett (Oct 13 2019 at 10:02, on Zulip):

How hard would it be to make the current deprecation warning for try! rustfixable?

Josh Triplett (Oct 13 2019 at 10:06, on Zulip):

It looks like the current warning comes purely from the use of rustc_deprecated. Could the compiler build in support for that deprecation itself, and then offer a rustfix?

Josh Triplett (Oct 13 2019 at 10:07, on Zulip):

(I can imagine adding support for rustfix suggestions to deprecation attributes, but that would require a more general "replace this with that" syntax in the attribute, and I don't mind special-casing this for simplicity.)

Josh Triplett (Oct 13 2019 at 10:13, on Zulip):

I'm trying to figure out where the suggestion in RustcDeprecation gets used.

nagisa (Oct 13 2019 at 10:17, on Zulip):

AFAIK just replacing one with the other will not always type-solve, anymore.

nagisa (Oct 13 2019 at 10:17, on Zulip):

This might or might not be the case why we do not do it already.

Josh Triplett (Oct 13 2019 at 10:19, on Zulip):

Under what circumstances would it not?

Josh Triplett (Oct 13 2019 at 10:20, on Zulip):

/me would like to get rustfix to help converting 400+ uses of try! in a project.

Josh Triplett (Oct 13 2019 at 10:21, on Zulip):

(Also, I found late_report_deprecation where the suggestion gets used.)

lqd (Oct 13 2019 at 10:26, on Zulip):

if it's just to actually translate them, use rustfmt

lqd (Oct 13 2019 at 10:27, on Zulip):

with use_try_shorthand = true specifically

lqd (Oct 13 2019 at 10:28, on Zulip):

should be easier than to teach how to do it to the compiler :)

Josh Triplett (Oct 13 2019 at 10:28, on Zulip):

Oh, handy; I didn't know rustfmt could do that.

Josh Triplett (Oct 13 2019 at 10:28, on Zulip):

I'll have to figure out how to disable the rest of rustfmt so I don't get a pile of things mixed into one commit, but that should still be easier. :)

Josh Triplett (Oct 13 2019 at 10:30, on Zulip):

@lqd Thanks!

lqd (Oct 13 2019 at 10:37, on Zulip):

you're welcome

Josh Triplett (Oct 13 2019 at 10:37, on Zulip):

@nagisa Ah, I see; across the whole project, I did indeed have one case where switching from try! to ? resulted in insufficient information for type inference.

nagisa (Oct 13 2019 at 10:39, on Zulip):

@Josh Triplett aww and here I just crafted a convulted example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cdaaa9ad06842745213d63a786c391c8

nagisa (Oct 13 2019 at 10:40, on Zulip):

I believe this is mostly because ? is able to work with Options and other Try implementing types and r#try is just for Result.

Josh Triplett (Oct 13 2019 at 10:40, on Zulip):

Right, that's the case I ran into as well.

Josh Triplett (Oct 13 2019 at 10:41, on Zulip):
error[E0282]: type annotations needed for `&(_, _)`
Josh Triplett (Oct 13 2019 at 10:47, on Zulip):

I'm having a lot of trouble getting rustfmt to not do things. disable_all_formatting also disables use_try_shorthand...

Josh Triplett (Oct 13 2019 at 10:50, on Zulip):

I can avoid a lot of it with max_width=400, but I still have a lot of cases like this to sort through:

-        _ => {
-            return Err(format!("Remote does not have either a tag or branch named {}", tag).into())
-        }
+        _ => return Err(format!("Remote does not have either a tag or branch named {}", tag).into()),
Josh Triplett (Oct 13 2019 at 10:57, on Zulip):

/me shrugs; still less trouble to sort through than doing it by hand.

Josh Triplett (Oct 13 2019 at 10:57, on Zulip):

Thanks again.

Last update: Nov 11 2019 at 21:55UTC