Repository navigation
Allow unary operand types to be inferred later #159744
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
Merged
rust-bors
merged 5 commits into
rust-lang:main
from
chenyukang:yukang-fix-106138-unary-op-inference
Oct 12, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b60321b
Allow unary operand types to be inferred later
chenyukang 9fce4c4
bless mismatch for unary-op-associated-output-mismatch-issue-106138
chenyukang 722a9e4
add regression test for issue 26830
chenyukang 01ae025
fix comment for check_expr_unop with Deref
chenyukang a97890a
add test unary operand autoderef behavior
chenyukang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| //! Regression test for https://github.com/rust-lang/rust/issues/106138. | ||
| //! An unresolved dereference must not treat a raw pointer as an overloaded `Deref`. | ||
|
|
||
| fn make<T>() -> T { | ||
| loop {} | ||
| } | ||
|
|
||
| fn main() { | ||
| let pointer = make(); | ||
| //~^ ERROR type annotations needed | ||
| let value = unsafe { *pointer }; | ||
| let _: *const u8 = pointer; | ||
| let _: u8 = value; | ||
| } |
17 changes: 17 additions & 0 deletions
17
tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| error[E0282]: type annotations needed | ||
| --> $DIR/unary-deref-late-raw-pointer-inference-issue-106138.rs:9:9 | ||
| | | ||
| LL | let pointer = make(); | ||
| | ^^^^^^^ | ||
| LL | | ||
| LL | let value = unsafe { *pointer }; | ||
| | -------- type must be known at this point | ||
| | | ||
| help: consider giving `pointer` an explicit type | ||
| | | ||
| LL | let pointer: /* Type */ = make(); | ||
| | ++++++++++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0282`. |
27 changes: 27 additions & 0 deletions
27
tests/ui/inference/unary-neg-late-operand-inference-issue-26830.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| //! Regression test for https://github.com/rust-lang/rust/issues/26830. | ||
| //! Unary negation should allow later constraints to determine its operand type. | ||
|
|
||
| //@ check-pass | ||
|
|
||
| fn make<T>() -> T { | ||
| loop {} | ||
| } | ||
|
|
||
| fn constrained_by_value() { | ||
| let input = make(); | ||
| let output = -input; | ||
| let _: i32 = input; | ||
| let _: i32 = output; | ||
| } | ||
|
|
||
| fn constrained_by_borrow() { | ||
| let input = make(); | ||
| let output = -input; | ||
| let _: &i32 = &input; | ||
| let _: i32 = output; | ||
| } | ||
|
|
||
| fn main() { | ||
| constrained_by_value(); | ||
| constrained_by_borrow(); | ||
| } |
42 changes: 42 additions & 0 deletions
42
tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //! Regression test for https://github.com/rust-lang/rust/issues/106138. | ||
| //! From comment https://github.com/rust-lang/rust/pull/107567#discussion_r1093589448 | ||
| //! Unary operator inference must not assume that the associated output type equals `Self`. | ||
|
|
||
| #[derive(Copy, Clone, Default)] | ||
| struct A; | ||
|
|
||
| struct B; | ||
|
|
||
| impl std::ops::Not for A { | ||
| type Output = B; | ||
|
|
||
| fn not(self) -> B { | ||
| B | ||
| } | ||
| } | ||
|
|
||
| #[derive(Copy, Clone)] | ||
| struct NoNot; | ||
|
|
||
| fn make<T>() -> T { | ||
| loop {} | ||
| } | ||
|
|
||
| fn resolved_without_impl() { | ||
| let value = make(); | ||
| let _ = !value; | ||
| //~^ ERROR the trait bound `NoNot: Not` is not satisfied | ||
| let _: NoNot = value; | ||
| } | ||
|
|
||
| fn unconstrained() { | ||
| let value = make(); | ||
| //~^ ERROR type annotations needed | ||
| let _ = !value; | ||
| } | ||
|
|
||
| fn main() { | ||
| let x = Default::default(); | ||
| //~^ ERROR cannot call associated function on trait | ||
| let _: A = !x; | ||
| } |
43 changes: 43 additions & 0 deletions
43
tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| error[E0277]: the trait bound `NoNot: Not` is not satisfied | ||
| --> $DIR/unary-op-associated-output-mismatch-issue-106138.rs:27:13 | ||
| | | ||
| LL | let _ = !value; | ||
| | ^^^^^^ unsatisfied trait bound | ||
| | | ||
| help: the trait `Not` is not implemented for `NoNot` | ||
| --> $DIR/unary-op-associated-output-mismatch-issue-106138.rs:19:1 | ||
| | | ||
| LL | struct NoNot; | ||
| | ^^^^^^^^^^^^ | ||
|
|
||
| error[E0284]: type annotations needed | ||
| --> $DIR/unary-op-associated-output-mismatch-issue-106138.rs:33:9 | ||
| | | ||
| LL | let value = make(); | ||
| | ^^^^^ | ||
| LL | | ||
| LL | let _ = !value; | ||
| | ------ type must be known at this point | ||
| | | ||
| = note: cannot satisfy `<_ as Not>::Output == _` | ||
| = note: the type must also implement `Not` | ||
| help: consider giving `value` an explicit type | ||
| | | ||
| LL | let value: /* Type */ = make(); | ||
| | ++++++++++++ | ||
|
|
||
| error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type | ||
| --> $DIR/unary-op-associated-output-mismatch-issue-106138.rs:39:13 | ||
| | | ||
| LL | let x = Default::default(); | ||
| | ^^^^^^^^^^^^^^^^^^ cannot call associated function of trait | ||
| | | ||
| help: use a fully-qualified path to a specific available implementation | ||
| | | ||
| LL | let x = </* self type */ as Default>::default(); | ||
| | +++++++++++++++++++ + | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| Some errors have detailed explanations: E0277, E0284, E0790. | ||
| For more information about an error, try `rustc --explain E0277`. |
65 changes: 65 additions & 0 deletions
65
tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| //! Regression test for https://github.com/rust-lang/rust/issues/106138. | ||
| //! Unary operators should allow their operand types to be inferred by later constraints. | ||
|
|
||
| //@ check-pass | ||
|
|
||
| use std::ops::Not; | ||
|
|
||
| // Indexing is a place projection, so `a[i]` autoderefs `a` and yields a `bool`. | ||
| // `!` is applied to that value: as a value operator it does not autoderef its | ||
| // operand itself, unlike the place projection `a[i]`. | ||
| fn not_index(x: &Vec<bool>) { | ||
| let closure = |i, a: &Vec<bool>| !a[i]; | ||
| let _ = closure(0, x); | ||
| } | ||
|
|
||
| // Likewise, the `-` is applied to the `i32` produced by `a[i]`. | ||
| fn neg_index(x: &Vec<i32>) { | ||
| let closure = |i, a: &Vec<i32>| -a[i]; | ||
| let _ = closure(0, x); | ||
| } | ||
|
|
||
| // Since the unary operators do not autoderef, these use `impl Not for &u32` and | ||
| // `impl Neg for &i32` rather than dereferencing to `u32`/`i32` first. | ||
| fn not_reference(x: &u32) -> u32 { | ||
| !x | ||
| } | ||
|
|
||
| fn neg_reference(x: &i32) -> i32 { | ||
| -x | ||
| } | ||
|
|
||
| #[derive(Copy, Clone, Default)] | ||
| struct Input; | ||
|
|
||
| struct Output; | ||
|
|
||
| impl Not for Input { | ||
| type Output = Output; | ||
|
|
||
| fn not(self) -> Self::Output { | ||
| Output | ||
| } | ||
| } | ||
|
|
||
| fn output_differs_from_operand() { | ||
| let input = Default::default(); | ||
| let output = !input; | ||
| let _: Input = input; | ||
| let _: Output = output; | ||
| } | ||
|
|
||
| fn output_expectation_differs_from_operand() { | ||
| let input = Default::default(); | ||
| let _: Output = !input; | ||
| let _: Input = input; | ||
| } | ||
|
|
||
| fn main() { | ||
| not_index(&vec![true]); | ||
| neg_index(&vec![1]); | ||
| let _ = not_reference(&1); | ||
| let _ = neg_reference(&1); | ||
| output_differs_from_operand(); | ||
| output_expectation_differs_from_operand(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| use std::ops::{Deref, Index, Neg, Not}; | ||
|
|
||
| struct Inner { | ||
| value: u32, | ||
| } | ||
|
|
||
| // `Inner` implements everything that the operators and place projections look | ||
| // up, so the contrast below is only about whether `Wrapper` is autoderef'd. | ||
| impl Not for Inner { | ||
| type Output = Inner; | ||
|
|
||
| fn not(self) -> Inner { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Neg for Inner { | ||
| type Output = Inner; | ||
|
|
||
| fn neg(self) -> Inner { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl Index<usize> for Inner { | ||
| type Output = u32; | ||
|
|
||
| fn index(&self, _: usize) -> &u32 { | ||
| &self.value | ||
| } | ||
| } | ||
|
|
||
| struct Wrapper(Inner); | ||
|
|
||
| impl Deref for Wrapper { | ||
| type Target = Inner; | ||
|
|
||
| fn deref(&self) -> &Inner { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let wrapper = Wrapper(Inner { value: 0 }); | ||
|
|
||
| // Field access and indexing are place projections, so they autoderef | ||
| // `Wrapper` to `Inner` and both of these compile. | ||
| let _ = wrapper.value; | ||
| let _ = wrapper[0]; | ||
|
|
||
| // `!` and `-` are value operators instead: they look up `Not` and `Neg` on | ||
| // the operand type itself and do not autoderef it. `Wrapper` implements | ||
| // neither trait, so both are rejected even though `Inner` does implement | ||
| // them and `Wrapper` derefs to `Inner`. | ||
| let _ = !wrapper; | ||
| //~^ ERROR cannot apply unary operator `!` to type `Wrapper` | ||
|
|
||
| let _ = -wrapper; | ||
| //~^ ERROR cannot apply unary operator `-` to type `Wrapper` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| error[E0600]: cannot apply unary operator `!` to type `Wrapper` | ||
| --> $DIR/unary-op-no-autoderef.rs:55:13 | ||
| | | ||
| LL | let _ = !wrapper; | ||
| | ^^^^^^^^ cannot apply unary operator `!` | ||
| | | ||
| note: an implementation of `Not` might be missing for `Wrapper` | ||
| --> $DIR/unary-op-no-autoderef.rs:33:1 | ||
| | | ||
| LL | struct Wrapper(Inner); | ||
| | ^^^^^^^^^^^^^^ must implement `Not` | ||
| note: the trait `Not` must be implemented | ||
| --> $SRC_DIR/core/src/ops/bit.rs:LL:COL | ||
|
|
||
| error[E0600]: cannot apply unary operator `-` to type `Wrapper` | ||
| --> $DIR/unary-op-no-autoderef.rs:58:13 | ||
| | | ||
| LL | let _ = -wrapper; | ||
| | ^^^^^^^^ cannot apply unary operator `-` | ||
| | | ||
| note: an implementation of `Neg` might be missing for `Wrapper` | ||
| --> $DIR/unary-op-no-autoderef.rs:33:1 | ||
| | | ||
| LL | struct Wrapper(Inner); | ||
| | ^^^^^^^^^^^^^^ must implement `Neg` | ||
| note: the trait `Neg` must be implemented | ||
| --> $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0600`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.