Repository: rust-lang/rust
Pull request #140632: add a test for issue rust-lang/rust#81317
Author: login: Skgland
Labels: name: T-compiler
description: Relevant to the compiler team, which will review and decide on the PR/issue.
name: S-waiting-on-bors
description: Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Created: 2025-05-03T20:52:10Z | Merged: 2025-05-06T16:51:33Z

closes rust-lang/rust#81317



Diff:
diff --git a/tests/ui/type-inference/regression-issue-81317.rs b/tests/ui/type-inference/regression-issue-81317.rs
new file mode 100644
index 0000000000000..0b1266e6a0fd2
--- /dev/null
+++ b/tests/ui/type-inference/regression-issue-81317.rs
@@ -0,0 +1,71 @@
+// Regression test for #81317: type can no longer be infered as of 1.49
+//
+// The problem is that the xor operator and the index.into() call
+// each have two candidate impls that could apply
+// { S as BitXor<S>, S as BitXor<&'a S> } for xor and
+// { T::I as Into<u64>, T::I as Into<S> } for index.into()
+// previously inference was able to infer that the only valid combination was
+// S as BitXor<S> and T::I as Into<S>
+//
+// after rust-lang/rust#73905 this is no longer infered
+//
+// the error message could be better e.g.
+// when iv is unused or has an an explicitly specified type S
+// there is currently the following help message
+//
+// error[E0284]: type annotations needed
+//   --> src/main.rs:13:24
+//    |
+// 44 |     let iv = S ^ index.into();
+//    |                -       ^^^^
+//    |                |
+//    |                type must be known at this point
+//    |
+//    = note: cannot satisfy `<S as BitXor<_>>::Output == _`
+// help: try using a fully qualified path to specify the expected types
+//    |
+// 44 -     let iv = S ^ index.into();
+// 44 +     let iv = S ^ <<T as P>::I as Into<T>>::into(index);
+//
+// this is better as it's actually sufficent to fix the problem,
+// while just specifying the type of iv as currently suggested is insufficent
+//
+//@ check-fail
+
+use std::ops::BitXor;
+
+pub struct S;
+
+pub trait P {
+    type I: Into<u64> + Into<S>;
+}
+
+pub fn decrypt_portion<T: P>(index: T::I) {
+    let iv = S ^ index.into();
+    //~^ ERROR type annotations needed
+    &iv.to_bytes_be();
+}
+
+impl S {
+    fn to_bytes_be(&self) -> &[u8] {
+        &[]
+    }
+}
+
+impl BitXor for S {
+    type Output = S;
+
+    fn bitxor(self, _rhs: Self) -> Self::Output {
+        S
+    }
+}
+
+impl<'a> BitXor<&'a S> for S {
+    type Output = S;
+
+    fn bitxor(self, _rhs: &'a S) -> Self::Output {
+        S
+    }
+}
+
+fn main() {}
diff --git a/tests/ui/type-inference/regression-issue-81317.stderr b/tests/ui/type-inference/regression-issue-81317.stderr
new file mode 100644
index 0000000000000..fcd3fca06e18b
--- /dev/null
+++ b/tests/ui/type-inference/regression-issue-81317.stderr
@@ -0,0 +1,17 @@
+error[E0282]: type annotations needed
+  --> $DIR/regression-issue-81317.rs:44:9
+   |
+LL |     let iv = S ^ index.into();
+   |         ^^
+LL |
+LL |     &iv.to_bytes_be();
+   |      -- type must be known at this point
+   |
+help: consider giving `iv` an explicit type
+   |
+LL |     let iv: /* Type */ = S ^ index.into();
+   |           ++++++++++++
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0282`.


Reviews:
user: login: oli-obk
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-05T09:18:18Z
user: login: Skgland
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-05T18:42:36Z
user: login: oli-obk
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-05-06T08:32:59Z

Review comments:
diff_hunk: @@ -0,0 +1,40 @@
+// Regression test for #81317: type can no longer be infered as of 1.49
path: tests/ui/type-inference/regression-issue-81317.rs
user: login: oli-obk
body: Document the reason this test is failing so someone reading or modifying it in the future doesn't have to reestablish context from scratch
created_at: 2025-05-05T09:18:18Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,40 @@
+// Regression test for #81317: type can no longer be infered as of 1.49
path: tests/ui/type-inference/regression-issue-81317.rs
user: login: Skgland
body: How about now?
created_at: 2025-05-05T18:42:36Z
author_association: CONTRIBUTOR
diff_hunk: @@ -0,0 +1,40 @@
+// Regression test for #81317: type can no longer be infered as of 1.49
path: tests/ui/type-inference/regression-issue-81317.rs
user: login: oli-obk
body: thanks, that's great.
created_at: 2025-05-06T08:32:59Z
author_association: CONTRIBUTOR

PR comments:
user: login: rustbot
created_at: 2025-05-03T20:52:14Z
body: r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use `r?` to explicitly pick a reviewer
author_association: COLLABORATOR
user: login: jieyouxu
created_at: 2025-05-04T07:19:25Z
body: I'm slightly confused -- is this a regression test, or does it just try to make sure *even further* changes to this code pattern is observed in PR test diffs? (Because the MCVE still doesn't compile, since 1.49, due to inference changes.) Would be good clarifying that in the test comment.
r? @oli-obk
author_association: MEMBER
user: login: rust-log-analyzer
created_at: 2025-05-05T18:50:37Z
body: 
The job **`mingw-check-tidy`** failed! Check out the build log: [(web)](https://github.com/rust-lang/rust/actions/runs/14843699352/job/41672492604) [(plain)](https://github.com/rust-lang/rust/commit/7fc84ac964b45955cc9beb7ec269d3d06a3591ab/checks/41672492604/logs)

<details><summary><i>Click to see the possible cause of the failure (guessed by this bot)</i></summary>

```plain
info: removing rustup binaries
info: rustup is uninstalled
##[group]Image checksum input
mingw-check-tidy
# We use the ghcr base image because ghcr doesn't have a rate limit
# and the mingw-check-tidy job doesn't cache docker images in CI.
FROM ghcr.io/rust-lang/ubuntu:22.04

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
  g++ \
---

COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/
COPY host-x86_64/mingw-check/validate-error-codes.sh /scripts/

# NOTE: intentionally uses python2 for x.py so we can test it still works.
# validate-toolstate only runs in our CI, so it's ok for it to only support python3.
ENV SCRIPT TIDY_PRINT_DIFF=1 python2.7 ../x.py test \
           --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
#    pip-compile --allow-unsafe --generate-hashes reuse-requirements.in
---
#12 2.750 Building wheels for collected packages: reuse
#12 2.751   Building wheel for reuse (pyproject.toml): started
#12 2.963   Building wheel for reuse (pyproject.toml): finished with status 'done'
#12 2.964   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132719 sha256=d2a2565e7037ad3883fb9337653f2e25bbb588534fbef3697286cbc26d1bf634
#12 2.964   Stored in directory: /tmp/pip-ephem-wheel-cache-0n3mimmz/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#12 2.967 Successfully built reuse
#12 2.967 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#12 3.374 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#12 3.374 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#12 3.899 Collecting virtualenv
#12 3.938   Downloading virtualenv-20.31.0-py3-none-any.whl (6.1 MB)
#12 4.011      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.1/6.1 MB 86.6 MB/s eta 0:00:00
#12 4.068 Collecting platformdirs<5,>=3.9.1
#12 4.070   Downloading platformdirs-4.3.7-py3-none-any.whl (18 kB)
#12 4.107 Collecting filelock<4,>=3.12.2
#12 4.110   Downloading filelock-3.18.0-py3-none-any.whl (16 kB)
#12 4.127 Collecting distlib<1,>=0.3.7
#12 4.130   Downloading distlib-0.3.9-py2.py3-none-any.whl (468 kB)
#12 4.137      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 469.0/469.0 KB 97.2 MB/s eta 0:00:00
#12 4.218 Installing collected packages: distlib, platformdirs, filelock, virtualenv
#12 4.409 Successfully installed distlib-0.3.9 filelock-3.18.0 platformdirs-4.3.7 virtualenv-20.31.0
#12 4.409 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#12 DONE 4.5s

#13 [7/8] COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/
#13 DONE 0.0s
---
DirectMap4k:      110528 kB
DirectMap2M:     8278016 kB
DirectMap1G:    10485760 kB
##[endgroup]
Executing TIDY_PRINT_DIFF=1 python2.7 ../x.py test            --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
+ TIDY_PRINT_DIFF=1 python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
##[group]Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.05s
##[endgroup]
WARN: currently no CI rustc builds have rustc debug assertions enabled. Please either set `rust.debug-assertions` to `false` if you want to use download CI rustc or set `rust.download-rustc` to `false`.
WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI rustc is not currently built with debug assertions.
---
fmt check
fmt: checked 6001 files
tidy check
tidy: Skipping binary file check, read-only filesystem
##[error]tidy error: /checkout/tests/ui/type-inference/regression-issue-81317.rs:3: line longer than 100 chars
removing old virtual environment
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'venv'
creating virtual environment at '/checkout/obj/build/venv' using 'python3.10' and 'virtualenv'
Requirement already satisfied: pip in ./build/venv/lib/python3.10/site-packages (25.1)
Collecting pip
  Using cached pip-25.1.1-py3-none-any.whl.metadata (3.6 kB)
Using cached pip-25.1.1-py3-none-any.whl (1.8 MB)
Installing collected packages: pip
  Attempting uninstall: pip
---
All checks passed!
checking python file formatting
26 files already formatted
checking C++ file formatting
some tidy checks failed
Command has failed. Rerun with -v to see more details.
Build completed unsuccessfully in 0:01:45
  local time: Mon May  5 18:50:22 UTC 2025
  network time: Mon, 05 May 2025 18:50:22 GMT
##[error]Process completed with exit code 1.
Post job cleanup.
```

</details>

author_association: COLLABORATOR
user: login: oli-obk
created_at: 2025-05-06T08:33:18Z
body: @bors r+ rollup
author_association: CONTRIBUTOR
user: login: bors
created_at: 2025-05-06T08:33:21Z
body: :pushpin: Commit fb8784585eece42a22c782bb01c1c2f188f70505 has been approved by `oli-obk`

It is now in the [queue](https://bors.rust-lang.org/homu/queue/rust) for this repository.

<!-- @bors r=oli-obk fb8784585eece42a22c782bb01c1c2f188f70505 -->
<!-- homu: {"type":"Approved","sha":"fb8784585eece42a22c782bb01c1c2f188f70505","approver":"oli-obk","queue":"https://bors.rust-lang.org/homu/queue/rust"} -->
author_association: COLLABORATOR