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

Properly suggest deref in else block #79755

Closed
wants to merge 2 commits into from

Conversation

mibac138
Copy link
Contributor

@mibac138 mibac138 commented Dec 6, 2020

Fixes #79736

@rust-highfive
Copy link
Collaborator

r? @ecstatic-morse

(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 Dec 6, 2020
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check of your PR failed (pretty log, raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
GITHUB_ACTION=run6
GITHUB_ACTIONS=true
GITHUB_ACTION_REF=
GITHUB_ACTION_REPOSITORY=
GITHUB_ACTOR=mibac138
GITHUB_BASE_REF=master
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_0da02bef-a6f2-48d5-bb1d-1dd327fc737d
GITHUB_EVENT_NAME=pull_request
GITHUB_EVENT_PATH=/home/runner/work/_temp/_github_workflow/event.json
GITHUB_EVENT_PATH=/home/runner/work/_temp/_github_workflow/event.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=deref-else
GITHUB_JOB=pr
GITHUB_PATH=/home/runner/work/_temp/_runner_file_commands/add_path_0da02bef-a6f2-48d5-bb1d-1dd327fc737d
GITHUB_REF=refs/pull/79755/merge
GITHUB_REPOSITORY_OWNER=rust-lang
GITHUB_RETENTION_DAYS=90
GITHUB_RUN_ID=428065677
GITHUB_RUN_NUMBER=21546
---
Building wheels for collected packages: PyYAML
  Running setup.py bdist_wheel for PyYAML: started
  Running setup.py bdist_wheel for PyYAML: finished with status 'error'
  Failed building wheel for PyYAML
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-0nvokodb/PyYAML/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/tmpy1hcq06opip-wheel- --python-tag cp36:
     or: -c --help [cmd1 cmd2 ...]
     or: -c --help-commands
     or: -c cmd --help
  
---
configure: rust.channel         := nightly
configure: rust.debug-assertions := True
configure: llvm.assertions      := True
configure: dist.missing-tools   := True
configure: build.configure-args := ['--enable-sccache', '--disable-manage-submodu ...
configure: writing `config.toml` in current directory
configure: 
configure: run `python /checkout/x.py --help`
configure: 
---
skip untracked path cpu-usage.csv during rustfmt invocations
skip untracked path src/doc/book/ during rustfmt invocations
skip untracked path src/doc/rust-by-example/ during rustfmt invocations
skip untracked path src/llvm-project/ during rustfmt invocations
Diff in /checkout/compiler/rustc_typeck/src/check/demand.rs at line 360:
     }
 
 
-    crate fn hir_id_sole_block_element(
-        &self,
-        hir_id: hir::HirId,
-    ) -> Option<&'tcx hir::Expr<'tcx>> {
+    crate fn hir_id_sole_block_element(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {
         match self.tcx.hir().find(hir_id)? {
-            Node::Expr(hir::Expr {
-                kind: hir::ExprKind::Block(block, ..),
-                ..
-            }) => block.expr,
+            Node::Expr(hir::Expr { kind: hir::ExprKind::Block(block, ..), .. }) => block.expr,
             _ => None,
     }
     }
Running `"/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/rustfmt" "--config-path" "/checkout" "--edition" "2018" "--unstable-features" "--skip-children" "--check" "/checkout/compiler/rustc_typeck/src/check/demand.rs"` failed.
If you're running `tidy`, try again with `--bless`. Or, if you just want to format code, run `./x.py fmt` instead.
Build completed unsuccessfully in 0:00:16
== clock drift check ==
  local time: Thu Dec 17 12:44:28 UTC 2020
  network time: Thu, 17 Dec 2020 00:13:25 GMT

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @rust-lang/infra. (Feature Requests)

@bors

This comment has been minimized.

@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. labels Jan 22, 2021
@camelid
Copy link
Member

camelid commented Jan 22, 2021

r? @davidtwco maybe?

@camelid
Copy link
Member

camelid commented Jan 22, 2021

Can you add a test for this as well?

fn main() {
    let a = &1;
    let b = &2;
    let val = if true {
        *a
    } else if true {
        b
    } else {
        &0
    };
}

Current output:

error[E0308]: `if` and `else` have incompatible types
  --> src/main.rs:6:12
   |
4  |        let val = if true {
   |   _______________-
5  |  |         *a
   |  |         -- expected because of this
6  |  |     } else if true {
   |  |____________^
7  | ||         b
8  | ||     } else {
9  | ||         &0
10 | ||     };
   | ||     ^
   | ||_____|
   | |______`if` and `else` have incompatible types
   |        expected integer, found `&{integer}`
   |
help: consider dereferencing the borrow
   |
6  |     } else *if true {
7  |         b
8  |     } else {
9  |         &0
10 |     };
   |

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Could you also add the test requested by @camelid?

@@ -360,6 +360,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
false
}

crate fn hir_id_sole_block_element(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
crate fn hir_id_sole_block_element(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {
crate fn maybe_get_block_expr(&self, hir_id: hir::HirId) -> Option<&'tcx hir::Expr<'tcx>> {

Could you add a doc comment too?

@davidtwco davidtwco added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 22, 2021
@JohnCSimon
Copy link
Member

@mibac138 Ping from triage can you please address the comments from the reviewer?

@crlf0710
Copy link
Member

crlf0710 commented Mar 5, 2021

@mibac138 Ping from triage: any updates on this?

@JohnCSimon JohnCSimon added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Mar 22, 2021
@JohnCSimon
Copy link
Member

Triage:
@mibac138 - closing this due to inactivity, please feel free to reopen when you're ready to continue.

@JohnCSimon JohnCSimon closed this Mar 22, 2021
@LeSeulArtichaut
Copy link
Contributor

I can push this to the finish line

JohnTitor added a commit to JohnTitor/rust that referenced this pull request Apr 5, 2021
…twco

Properly suggest deref in else block

Continues rust-lang#79755, fixes rust-lang#79736
r? `@davidtwco`
@mibac138 mibac138 deleted the deref-else branch May 5, 2021 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler suggests dereferencing an else block