Skip to content

Allow unary operand types to be inferred later - #159744

Merged
rust-bors[bot] merged 5 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-106138-unary-op-inference
Oct 12, 2026
Merged

rust-bors[bot] merged 5 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-106138-unary-op-inference

Conversation

@chenyukang

@chenyukang chenyukang commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

View all comments

Fixes #106138

Fix the inference issue by allowing Not and Neg operand types to remain unresolved until obligations and constraints can determine them.

closes #26830

@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 Jul 22, 2026
@rustbot

rustbot commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

r? @fee1-dead

rustbot has assigned @fee1-dead.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 74 candidates
  • Random selection from 20 candidates

@fee1-dead

Copy link
Copy Markdown
Member

hard for me to know if this is the right thing to do, hmm..

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Jul 23, 2026
@rustbot rustbot assigned jackh726 and unassigned fee1-dead Jul 23, 2026
@chenyukang
chenyukang force-pushed the yukang-fix-106138-unary-op-inference branch from be7c566 to d13857f Compare July 24, 2026 01:12

@jackh726 jackh726 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, this is fairly minor, but is new behavior, so I wondering if a @rust-lang/types FCP might be appropriate.

View changes since this review

Comment thread compiler/rustc_hir_typeck/src/expr.rs Outdated
Comment on lines +424 to +427
let oprnd_t = match unop {
hir::UnOp::Deref => self.structurally_resolve_type(expr.span, oprnd_t),
hir::UnOp::Not | hir::UnOp::Neg => self.resolve_vars_with_obligations(oprnd_t),
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This definitely needs a comment. But:

  1. Can we just use resolve_vars_with_obligations here for all three?
  2. If not, I think it makes sense to just move these into the match arms, rather than matching twice.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

If we also use resolve_vars_with_obligations for Deref, this code will treat a later-inferred raw pointer as an overloaded Deref, for example this code was reporting a proper E0282:

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;
}
error[E0282]: type annotations needed
  --> src/main.rs:9:9
   |
 9 |     let pointer = make();
   |         ^^^^^^^
10 |     //~^ ERROR type annotations needed
11 |     let value = unsafe { *pointer };
   |                          -------- type must be known at this point
   |
help: consider giving `pointer` an explicit type
   |
 9 |     let pointer: /* Type */ = make();
   |                ++++++++++++

For more information about this error, try `rustc --explain E0282`.

if we changed to use resolve_vars_with_obligations, the error changed to:

error[E0277]: the trait bound `*const u8: Deref` is not satisfied
  --> tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.rs:11:26
   |
11 |     let value = unsafe { *pointer };
   |                          ^^^^^^^^ the trait `Deref` is not implemented for `*const u8`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.

I'm not sure whether there is other cases, we'd better keep structurally_resolve_type for Deref, and I added this test code as a unit test.

@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 Aug 21, 2026
@lcnr lcnr added the relnotes Marks issues that should be documented in the release notes of the next release. label Aug 21, 2026
@lcnr lcnr added needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. and removed relnotes Marks issues that should be documented in the release notes of the next release. labels Aug 21, 2026
@dianne

dianne commented Aug 22, 2026

Copy link
Copy Markdown
Member

This should also close #26830, I think?

Also, for what it's worth, this would slightly expand the surface area of #114380 (also reported as #151202). Currently, the following fails to type-check:

fn main() {
    let input = Default::default();
    let output = !{ input };
    let _: u8 = output;
    println!("{}", std::any::type_name_of_val(&input));
}

Under this PR, it would compile successfully and print u8. let output = !{ input }; erroneously coerces input to the expected type of the negation as a whole, unifying its type with that of output (which is now allowed to be inferred later).

@chenyukang
chenyukang force-pushed the yukang-fix-106138-unary-op-inference branch from d13857f to 5fd2ec6 Compare August 24, 2026 03:03
@rustbot

This comment has been minimized.

@chenyukang
chenyukang force-pushed the yukang-fix-106138-unary-op-inference branch from 5fd2ec6 to 26b55d3 Compare August 24, 2026 03:14
@rustbot

This comment has been minimized.

@chenyukang

Copy link
Copy Markdown
Member Author

This should also close #26830, I think?

Also, for what it's worth, this would slightly expand the surface area of #114380 (also reported as #151202). Currently, the following fails to type-check:

fn main() {
    let input = Default::default();
    let output = !{ input };
    let _: u8 = output;
    println!("{}", std::any::type_name_of_val(&input));
}

Under this PR, it would compile successfully and print u8. let output = !{ input }; erroneously coerces input to the expected type of the negation as a whole, unifying its type with that of output (which is now allowed to be inferred later).

I added a test in this PR for #26830

@chenyukang

Copy link
Copy Markdown
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 25, 2026
@jackh726

Copy link
Copy Markdown
Member

@rfcbot merge types

Minor change here. This Not and Neg unary operators use resolve_vars_with_obligations instead of structurally_resolve_type, which lets us infer the output from later information.

@rust-rfcbot

rust-rfcbot commented Aug 25, 2026 •

Copy link
Copy Markdown
Collaborator

@jackh726 has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. and removed needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. labels Aug 25, 2026
@rustbot

This comment has been minimized.

@rust-rfcbot rust-rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Oct 3, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@traviscross traviscross added the waived-reference-pr This language change does not need a Reference PR. label Oct 3, 2026
@rust-bors

This comment has been minimized.

@chenyukang
chenyukang force-pushed the yukang-fix-106138-unary-op-inference branch from 4b2dec1 to 8600da7 Compare October 5, 2026 02:34
@rustbot

This comment has been minimized.

@traviscross traviscross added T-lang Relevant to the language team and removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-0 Lang team prioritization drag level 0.https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang. labels Oct 7, 2026

@jackh726 jackh726 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is probably good to have a test for behavior that we're locking into re. #159744 (comment)

Other than that, the changes themselves LGTM

View changes since this review

@chenyukang
chenyukang force-pushed the yukang-fix-106138-unary-op-inference branch from 8600da7 to a97890a Compare October 11, 2026 03:08
@rustbot

rustbot commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@chenyukang

Copy link
Copy Markdown
Member Author

It is probably good to have a test for behavior that we're locking into re. (earlier comment)

Other than that, the changes themselves LGTM

View changes since this review

I added more test to cover it.

@jackh726

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

📌 Commit a97890a has been approved by jackh726

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 11, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 12, 2026
Rollup of 7 pull requests

Successful merges:

 - #159744 (Allow unary operand types to be inferred later)
 - #160597 (bootstrap: always build llvm's dylib when linking to it)
 - #162810 (vectorized vec::dedup  pre-scan)
 - #162931 (directly deallocates the nodes instead of traversing every element when BTreemap dropping)
 - #162027 (std: add `fs::rename_noreplace`)
 - #163883 (Add `oneshot` `Sender::is_closed`)
 - #164146 (Nuke `Unique` (and `ptr_internals`))
@rust-bors
rust-bors Bot merged commit cf33c38 into rust-lang:main Oct 12, 2026
14 checks passed
rust-bors Bot pushed a commit that referenced this pull request Oct 12, 2026
Rollup merge of #159744 - chenyukang:yukang-fix-106138-unary-op-inference, r=jackh726

Allow unary operand types to be inferred later

Fixes #106138

Fix the inference issue by allowing `Not` and `Neg` operand types to remain unresolved until obligations and constraints can determine them.

closes #26830
@rustbot rustbot added this to the 1.101.0 milestone Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-types Relevant to the types team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting waived-reference-pr This language change does not need a Reference PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cannot infer type when expression is logically negated in closure Weird inference failure with unary minus