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

Remove "consider specifying this binding's type" when reference differs in mutability #115648

Closed
wants to merge 1 commit into from

Conversation

jmintb
Copy link
Contributor

@jmintb jmintb commented Sep 7, 2023

This is a draft PR for #113767. It introduces a new test case and a mitigation which seems to work. I am submitting this now to get feedback before refining it further. I am pretty sure I am not covering all cases yet and I need to cleanup the rather insane destructuring haha.

The idea is to check for cases where the original value of a binding is immutable and then avoid suggesting that the type be specified in those cases. As that would not help.

To do this we first find the initial expression's kind HIR ID from the information in local.init.kind, then use a map of HIR IDs to find a Node. Finally we determine it's mutability by destructing the Node to find it's type information.

I have a few questions:

  1. Does this approach seem reasonable to you?
  2. There are a lot of cases to cover here, should this be moved to a separate function/utility for determining binding mutability? or is there already something that does this perhaps?

This change causes three existing tests to fail tests/ui/borrowck/borrowck-borrow-mut-base-ptr-in-aliasable-loc.rs, tests/ui/issues/issue-51515.rs and tests/ui/process/nofile-limit.rs, As far as I can tell the code is working correctly and the test needs to be updated.

We might be able to provide a better suggestion instead of just removing the existing one. Like "consider changing the mutability of the function/value used to initaliase your variable". Formulated more elegantly 😅

@rustbot
Copy link
Collaborator

rustbot commented Sep 7, 2023

r? @jackh726

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 7, 2023
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@jmintb jmintb changed the title Remove incorrect mutability help suggestion Remove "consider specifying this binding's type" when reference differs in mutability Sep 8, 2023
@bors
Copy link
Contributor

bors commented Sep 11, 2023

☔ The latest upstream changes (presumably #115308) made this pull request unmergeable. Please resolve the merge conflicts.

issue: rust-lang#113767

This commit disables the "consider changing this bindings type"
suggestion in cases where the initial value of a binding is not mutable
and the suggestion is therefore invalid.
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check-tidy failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Prepare all required actions
Getting action download info
Download action repository 'actions/checkout@v3' (SHA:f43a0e5ff2bd294095638e18286ca9a3d1956744)
Download action repository 'actions/upload-artifact@v3' (SHA:a8a3f3ad30e3422c9c7b888a15615d19a852ae32)
Complete job name: PR - mingw-check-tidy
git config --global core.autocrlf false
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
---
Removing intermediate container 9f99108c992a
 ---> 319ce8931466
Step 6/10 : COPY host-x86_64/mingw-check/reuse-requirements.txt /tmp/
 ---> 364711e6311c
Step 7/10 : RUN pip3 install --no-deps --no-cache-dir --require-hashes -r /tmp/reuse-requirements.txt     && pip3 install virtualenv
Collecting binaryornot==0.4.4
  Downloading binaryornot-0.4.4-py2.py3-none-any.whl (9.0 kB)
Collecting boolean-py==4.0
  Downloading boolean.py-4.0-py3-none-any.whl (25 kB)
---
Building wheels for collected packages: reuse
  Building wheel for reuse (pyproject.toml): started
  Building wheel for reuse (pyproject.toml): finished with status 'done'
  Created wheel for reuse: filename=reuse-1.1.0-cp310-cp310-manylinux_2_35_x86_64.whl size=180117 sha256=2196c9034bf565528bbb1ee6dad4f753eb813f58822363e6b768f09c73e4d4ff
  Stored in directory: /tmp/pip-ephem-wheel-cache-85uoh2cm/wheels/c2/3c/b9/1120c2ab4bd82694f7e6f0537dc5b9a085c13e2c69a8d0c76d
Installing collected packages: boolean-py, binaryornot, setuptools, reuse, python-debian, markupsafe, license-expression, jinja2, chardet
  Attempting uninstall: setuptools
    Found existing installation: setuptools 59.6.0
    Not uninstalling setuptools at /usr/lib/python3/dist-packages, outside environment /usr
---
  Downloading virtualenv-20.24.5-py3-none-any.whl (3.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.7/3.7 MB 78.4 MB/s eta 0:00:00
Collecting filelock<4,>=3.12.2
  Downloading filelock-3.12.3-py3-none-any.whl (11 kB)
Collecting platformdirs<4,>=3.9.1
  Downloading platformdirs-3.10.0-py3-none-any.whl (17 kB)
Collecting distlib<1,>=0.3.7
  Downloading distlib-0.3.7-py2.py3-none-any.whl (468 kB)
Collecting typing-extensions>=4.7.1
  Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB)
  Downloading typing_extensions-4.7.1-py3-none-any.whl (33 kB)
Installing collected packages: distlib, typing-extensions, platformdirs, filelock, virtualenv
Successfully installed distlib-0.3.7 filelock-3.12.3 platformdirs-3.10.0 typing-extensions-4.7.1 virtualenv-20.24.5
Removing intermediate container 7175bcefb48f
 ---> fe96c384541d
Step 8/10 : COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/
 ---> 129acdd5f09f
 ---> 129acdd5f09f
Step 9/10 : COPY host-x86_64/mingw-check/validate-error-codes.sh /scripts/
 ---> 8ab35508b5a1
Step 10/10 : ENV SCRIPT TIDY_PRINT_DIFF=1 python2.7 ../x.py test            --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint
Removing intermediate container bff53372c447
 ---> c6178bffe0f4
Successfully built c6178bffe0f4
Successfully tagged rust-ci:latest
Successfully tagged rust-ci:latest
##[endgroup]
Built container sha256:c6178bffe0f43ddea2717e82e691411ab9fa2800fd7a74df8bc359c619bc7549
Uploading finished image sha256:c6178bffe0f43ddea2717e82e691411ab9fa2800fd7a74df8bc359c619bc7549 to https://ci-caches.rust-lang.org/docker/31024a61e3c099341c4151411b61536c5a15e77991c1c58c8adf475bd61d8568f58aad59ac3149cca672919d01d664a203adb87f86665b006743ece22accf3f1
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
c6178bffe0f4   1 second ago     /bin/sh -c #(nop)  ENV SCRIPT=TIDY_PRINT_DIF…   0B        
129acdd5f09f   2 seconds ago    /bin/sh -c #(nop) COPY file:078ea1d11e7b7cda…   367B      
fe96c384541d   3 seconds ago    |1 DEBIAN_FRONTEND=noninteractive /bin/sh -c…   24.1MB    
364711e6311c   9 seconds ago    /bin/sh -c #(nop) COPY file:ac591dd6bc5afa66…   5.33kB    
319ce8931466   10 seconds ago   |1 DEBIAN_FRONTEND=noninteractive /bin/sh -c…   23.1MB    
---
<missing>      3 weeks ago      /bin/sh -c #(nop)  LABEL org.opencontainers.…   0B        
<missing>      3 weeks ago      /bin/sh -c #(nop)  ARG LAUNCHPAD_BUILD_ARCH     0B        
<missing>      3 weeks ago      /bin/sh -c #(nop)  ARG RELEASE                  0B        

<botocore.awsrequest.AWSRequest object at 0x7fd136c19d50>
gzip: stdout: Broken pipe
xargs: docker: terminated by signal 13
https://ci-caches.rust-lang.org/docker/31024a61e3c099341c4151411b61536c5a15e77991c1c58c8adf475bd61d8568f58aad59ac3149cca672919d01d664a203adb87f86665b006743ece22accf3f1
sha256:c6178bffe0f43ddea2717e82e691411ab9fa2800fd7a74df8bc359c619bc7549
---
DirectMap4k:      182208 kB
DirectMap2M:     7157760 kB
DirectMap1G:    11534336 kB
##[endgroup]
Executing TIDY_PRINT_DIFF=1 python2.7 ../x.py test            --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint
+ TIDY_PRINT_DIFF=1 python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py:lint
    Finished dev [unoptimized] target(s) in 0.03s
##[endgroup]
downloading https://ci-artifacts.rust-lang.org/rustc-builds-alt/366dab13f711df90a6891411458544199d159cbc/rust-dev-nightly-x86_64-unknown-linux-gnu.tar.xz
extracting /checkout/obj/build/cache/llvm-366dab13f711df90a6891411458544199d159cbc-true/rust-dev-nightly-x86_64-unknown-linux-gnu.tar.xz to /checkout/obj/build/x86_64-unknown-linux-gnu/ci-llvm
---
    Finished release [optimized] target(s) in 25.75s
##[endgroup]
fmt check
tidy check
tidy error: following path contains more than 865 entries, you should move the test to some relevant subdirectory (current: 867): /checkout/tests/ui
tidy: Skipping binary file check, read-only filesystem
##[error]tidy error: /checkout/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs:1262: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
##[error]tidy error: /checkout/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs:1265: trailing whitespace
##[error]tidy error: /checkout/compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs:1267: TODO is used for tasks that should be done before merging a PR; If you want to leave a message in the codebase use FIXME
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (23.2.1)
Collecting black==23.3.0 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 7))
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 28.1 MB/s eta 0:00:00
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 28.1 MB/s eta 0:00:00
Collecting click==8.1.3 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 34))
  Downloading click-8.1.3-py3-none-any.whl (96 kB)
Collecting importlib-metadata==6.7.0 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 38))
  Downloading importlib_metadata-6.7.0-py3-none-any.whl (22 kB)
  Downloading importlib_metadata-6.7.0-py3-none-any.whl (22 kB)
Collecting mypy-extensions==1.0.0 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 42))
  Downloading mypy_extensions-1.0.0-py3-none-any.whl (4.7 kB)
Collecting packaging==23.1 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 46))
  Downloading packaging-23.1-py3-none-any.whl (48 kB)
Collecting pathspec==0.11.1 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 50))
  Downloading pathspec-0.11.1-py3-none-any.whl (29 kB)
  Downloading pathspec-0.11.1-py3-none-any.whl (29 kB)
Collecting platformdirs==3.6.0 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 54))
  Downloading platformdirs-3.6.0-py3-none-any.whl (16 kB)
Collecting ruff==0.0.272 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 58))
  Downloading ruff-0.0.272-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.9 MB)
Collecting tomli==2.0.1 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 77))
  Downloading tomli-2.0.1-py3-none-any.whl (12 kB)
Collecting typed-ast==1.5.4 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 81))
  Downloading typed_ast-1.5.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (877 kB)
  Downloading typed_ast-1.5.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (877 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 877.7/877.7 kB 161.1 MB/s eta 0:00:00
Collecting typing-extensions==4.6.3 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 107))
  Downloading typing_extensions-4.6.3-py3-none-any.whl (31 kB)
Collecting zipp==3.15.0 (from -r /checkout/src/tools/tidy/config/requirements.txt (line 114))
  Downloading zipp-3.15.0-py3-none-any.whl (6.8 kB)
Installing collected packages: zipp, typing-extensions, typed-ast, tomli, ruff, platformdirs, pathspec, packaging, mypy-extensions, click, importlib-metadata, black
Successfully installed black-23.3.0 click-8.1.3 importlib-metadata-6.7.0 mypy-extensions-1.0.0 packaging-23.1 pathspec-0.11.1 platformdirs-3.6.0 ruff-0.0.272 tomli-2.0.1 typed-ast-1.5.4 typing-extensions-4.6.3 zipp-3.15.0
some tidy checks failed
Build completed unsuccessfully in 0:00:50
  local time: Tue Sep 12 05:43:54 UTC 2023
  network time: Tue, 12 Sep 2023 05:43:55 GMT

Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

It's times like this that I think that actually just "applying" suggested fix and checking if the new code would compile would be super helpful.

I think this is just an improvement, even if not perfect. Please bless the broken tests, address the review comments, and fix formatting. Should move the added test to a proper directory with a useful name - and add a comment on what the test is testing. (Alternatively, could move the test case into an existing test if there's one that fits.

Comment on lines 1220 to 1221
if let Some(hir_id) = hir_id
&& let Some(hir::Node::Local(local)) = hir_map.find(hir_id)
Copy link
Member

Choose a reason for hiding this comment

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

So, the added checks here don't cover this else case. Which can have the same problems?

Some(hir::ExprKind::Path(hir::QPath::Resolved(_, hir::Path{res: Res::Local(id), ..} ))) => hir_map.find(*id),
Some(hir::ExprKind::Call(Expr { kind: ExprKind::Path(QPath::Resolved(_, Path {res: Res::Def(DefKind::Fn, defid ) , ..} ), ..), ..}, .. )) => {
let Some(local_id) = defid.as_local() else {
todo!("What do we do here?")
Copy link
Member

Choose a reason for hiding this comment

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

We could have a fallback case that just has a more lax "you may be able to change the mutability here". I'm not sure if the binding info is available cross-crates.

),
..
})) => mut_ty.mutbl,
_ => Mutability::Mut, // TODO: this probably is not correct handling of this case.
Copy link
Member

Choose a reason for hiding this comment

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

Eh, it's okay. Just leave a note that other cases might not be covered.


// If the inital value of an expression is not mutable then suggesting that the user
// changes the type of the expression to be mutable is incorrect is it will not help.
// TODO: Could we provide an alternative suggestions around altering the mutability
Copy link
Member

Choose a reason for hiding this comment

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

This is fine to leave as a FIXME, or just remove.

@@ -1224,6 +1278,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
format!(": {message}"),
),
};
// FOUND IT
Copy link
Member

Choose a reason for hiding this comment

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

??

@jackh726 jackh726 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 Oct 7, 2023
@bors
Copy link
Contributor

bors commented Dec 5, 2023

☔ The latest upstream changes (presumably #118076) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Member

@jmintb any updates on this

@jmintb
Copy link
Contributor Author

jmintb commented Feb 19, 2024

@jmintb any updates on this

Sorry I forgot to come back to this. Will get on it soon.

@Dylan-DPC
Copy link
Member

@jmintb any updates on this? thanks

@Dylan-DPC
Copy link
Member

Closing this as inactive. Feel free to reöpen this pr or create a new pr if you get the time to work on this. Thanks

@Dylan-DPC Dylan-DPC closed this Sep 26, 2024
@Dylan-DPC Dylan-DPC added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants