-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
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
Open
chenyukang
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
chenyukang:yukang-fix-106138-unary-op-inference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
26b55d3
Allow unary operand types to be inferred later
chenyukang f4805fc
bless mismatch for unary-op-associated-output-mismatch-issue-106138
chenyukang 29a3f14
add regression test for issue 26830
chenyukang d85b37c
fix comment for check_expr_unop with Deref
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`. |
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
| //! 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; | ||
|
|
||
| fn not_index(x: &Vec<bool>) { | ||
| let closure = |i, a: &Vec<bool>| !a[i]; | ||
| let _ = closure(0, x); | ||
| } | ||
|
|
||
| fn neg_index(x: &Vec<i32>) { | ||
| let closure = |i, a: &Vec<i32>| -a[i]; | ||
| let _ = closure(0, 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]); | ||
| output_differs_from_operand(); | ||
| output_expectation_differs_from_operand(); | ||
| } |
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.