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

Fix panic in 'remove semicolon' when types are not local #81991

Merged
merged 4 commits into from
Feb 21, 2021

Conversation

osa1
Copy link
Contributor

@osa1 osa1 commented Feb 11, 2021

It's not possible to check if removing a semicolon fixes the type error
when checking match arms and one or both of the last arm's and the
current arm's return types are imported "opaque" types. In these cases
we don't generate a "consider removing semicolon" suggestions.

Fixes #81839


I'm not sure how to add a test for this. I think the test would need at least two crates. Do we have any existing tests that do this so that I can take a look?

@rust-highfive
Copy link
Collaborator

r? @lcnr

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 11, 2021
@osa1
Copy link
Contributor Author

osa1 commented Feb 11, 2021

I'm not sure if this is the best "fix" for #81839. This is the error message with this patch: (without the patch the repro panics)

  --> src/transitions.rs:26:14
   |
24 | /     match num {
25 | |         1 => { cx.answer_str("hi"); }
   | |                -------------------- this is found to be of type `()`
26 | |         _ => cx.answer_str("hi")
   | |              ^^^^^^^^^^^^^^^^^^^ expected `()`, found opaque type
27 | |     }
   | |_____- `match` arms have incompatible types
   |
  ::: /home/omer/rust/teloxide/src/dispatching/update_with_cx.rs:36:51
   |
36 |       pub async fn answer_str<T>(&self, text: T) -> ResponseResult<Message>
   |                                                     ----------------------- the `Output` of this `async fn`'s found opaque type
   |
   = note:     expected type `()`
           found opaque type `impl futures::Future`

There should be some way of comparing two non-local opaque types for type checking. We should be able to use the same method to compare types of these two branches and generate the suggestion. I couldn't find how to do the comparison though.. See also my question in Zulip.

@osa1
Copy link
Contributor Author

osa1 commented Feb 11, 2021

Last commit brings back the "consider removing this semicolon" help. Current error message:

error[E0308]: `match` arms have incompatible types
  --> src/transitions.rs:26:14
   |
24 | /     match num {
25 | |         1 => { cx.answer_str("hi"); }
   | |                --------------------
   | |                |                  |
   | |                |                  help: consider removing this semicolon
   | |                this is found to be of type `()`
26 | |         _ => cx.answer_str("hi")
   | |              ^^^^^^^^^^^^^^^^^^^ expected `()`, found opaque type
27 | |     }
   | |_____- `match` arms have incompatible types
   |
  ::: /home/omer/rust/teloxide/src/dispatching/update_with_cx.rs:36:51
   |
36 |       pub async fn answer_str<T>(&self, text: T) -> ResponseResult<Message>
   |                                                     ----------------------- the `Output` of this `async fn`'s found opaque type
   |
   = note:     expected type `()`
           found opaque type `impl futures::Future`

error: aborting due to previous error

This help is not great IMO, but I think improving it is outside of the scope of #81839.

@osa1
Copy link
Contributor Author

osa1 commented Feb 11, 2021

Should I add a 'run_make' test for this? Does anyone know a better way to test this?

@rust-log-analyzer

This comment has been minimized.

@osa1
Copy link
Contributor Author

osa1 commented Feb 11, 2021

Hm, I think the new output of the failing test is actually an improvement. Relevant parts of the test:

async fn async_dummy() {}

async fn async_extra_semicolon_same() {
    let _ = match true {
        true => {
            async_dummy();
        }
        false => async_dummy(),
    };
}

Error message in master branch:

error[E0308]: `match` arms have incompatible types
  --> test.rs:10:18
   |
3  |   async fn async_dummy() {}
   |                          - the `Output` of this `async fn`'s found opaque type
...
6  |       let _ = match true {
   |  _____________-
7  | |         true => {
8  | |             async_dummy();
   | |             -------------- this is found to be of type `()`
9  | |         }
10 | |         false => async_dummy(),
   | |                  ^^^^^^^^^^^^^ expected `()`, found opaque type
11 | |     };
   | |_____- `match` arms have incompatible types
   |
   = note:     expected type `()`
           found opaque type `impl Future`
help: consider `await`ing on the `Future`
   |
10 |         false => async_dummy().await,
   |                               ^^^^^^
help: consider removing this semicolon and boxing the expressions
   |
8  |             Box::new(async_dummy())
9  |         }
10 |         false => Box::new(async_dummy()),
   |

error: aborting due to previous error

Error message with this PR:

error[E0308]: `match` arms have incompatible types
  --> test.rs:10:18
   |
3  |   async fn async_dummy() {}
   |                          - the `Output` of this `async fn`'s found opaque type
...
6  |       let _ = match true {
   |  _____________-
7  | |         true => {
8  | |             async_dummy();
   | |             -------------- this is found to be of type `()`
9  | |         }
10 | |         false => async_dummy(),
   | |                  ^^^^^^^^^^^^^ expected `()`, found opaque type
11 | |     };
   | |_____- `match` arms have incompatible types
   |
   = note:     expected type `()`
           found opaque type `impl Future`
help: consider `await`ing on the `Future`
   |
10 |         false => async_dummy().await,
   |                               ^^^^^^
help: consider removing this semicolon
   |
8  |             async_dummy()
   |                         --

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.

So we no longer suggest boxing, which is a good thing because just deleting the semicolon is enough to make this program type check.

@osa1
Copy link
Contributor Author

osa1 commented Feb 15, 2021

Ping @lcnr

@lcnr
Copy link
Contributor

lcnr commented Feb 15, 2021

yes, you can have multicrate tests by adding // aux-build:file.rs, see https://github.com/rust-lang/rust/blob/master/src/test/ui/inner-static.rs for example

It's not possible to check if removing a semicolon fixes the type error
when checking match arms and one or both of the last arm's and the
current arm's return types are imported "opaque" types. In these cases
we don't generate a "consider removing semicolon" suggestions.

Fixes rust-lang#81839
@osa1
Copy link
Contributor Author

osa1 commented Feb 18, 2021

Thanks @lcnr , I added a regression test now and rebased the branch.

Let me know if it looks good and I'll squash the commits.

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method seems a bit weird to me, even when ignoring your changes so I want to look at it in a bit more depth. Don't have a lot of time rn so I might not get to it during the next two weeks.

The added tests looks good 👍

@estebank would be interested in taking this over?

@estebank
Copy link
Contributor

r? @estebank

@rust-highfive rust-highfive assigned estebank and unassigned lcnr Feb 18, 2021
@estebank
Copy link
Contributor

@bors r+ rollup

It's great to get the review by the time all my potential nits have been addressed :)

Looks great!

@bors
Copy link
Contributor

bors commented Feb 19, 2021

📌 Commit 9ef67e0 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 19, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Feb 19, 2021
Fix panic in 'remove semicolon' when types are not local

It's not possible to check if removing a semicolon fixes the type error
when checking match arms and one or both of the last arm's and the
current arm's return types are imported "opaque" types. In these cases
we don't generate a "consider removing semicolon" suggestions.

Fixes rust-lang#81839

---

I'm not sure how to add a test for this. I think the test would need at least two crates. Do we have any existing tests that do this so that I can take a look?
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Feb 20, 2021
Fix panic in 'remove semicolon' when types are not local

It's not possible to check if removing a semicolon fixes the type error
when checking match arms and one or both of the last arm's and the
current arm's return types are imported "opaque" types. In these cases
we don't generate a "consider removing semicolon" suggestions.

Fixes rust-lang#81839

---

I'm not sure how to add a test for this. I think the test would need at least two crates. Do we have any existing tests that do this so that I can take a look?
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 20, 2021
…laumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#80595 (`impl PartialEq<Punct> for char`; symmetry for rust-lang#78636)
 - rust-lang#81991 (Fix panic in 'remove semicolon' when types are not local)
 - rust-lang#82176 (fix MIR fn-ptr pretty-printing)
 - rust-lang#82244 (Keep consistency in example for Stdin StdinLock)
 - rust-lang#82260 (rustc: Show ``@path`` usage in stable)
 - rust-lang#82316 (Fix minor mistake in LTO docs.)
 - rust-lang#82332 (Don't generate src link on dummy spans)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 39af025 into rust-lang:master Feb 21, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"DefId isn't local" ICE
7 participants