Skip to content

Allow unary operand types to be inferred later - #159744

Open
chenyukang wants to merge 4 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-106138-unary-op-inference
Open

Allow unary operand types to be inferred later#159744
chenyukang wants to merge 4 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

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

rustbot commented Aug 24, 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

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:

No concerns currently listed.

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
Comment thread compiler/rustc_hir_typeck/src/expr.rs
@lcnr lcnr added the I-lang-nominated Nominated for discussion during a lang team meeting. label Sep 2, 2026
@lcnr

lcnr commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

This is okay because we don't do auto-derefs/coercions here, we don't do this for indexing or field projections as x[7] can be called on x: &&&&&&[u32].

I do feel the argument that |index, y: &[u32]| !y[index] should compile to be quite strong and don't feel like ! of x: &u32 should necessarily automatically deref x 🤔

At this point this is a lang design tradeoff, cc @rust-lang/lang whether you want to be involved here. An alternative would be to allow ! and - to apply auto-derefs, we cannot support both "apply the operator to inference variables" and "apply auto-deref", as doing so must happen eagerly.

@traviscross traviscross added I-lang-radar Items that are on lang's radar and will need eventual work or consideration. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Sep 2, 2026
Co-authored-by: lcnr <rust@lcnr.de>
@BoxyUwU

BoxyUwU commented Sep 3, 2026

Copy link
Copy Markdown
Member

this seems related to #151539

would like for whoever reviews this PR to also review that PR potentially 🤔

@rust-bors

rust-bors Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

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

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. I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. 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. T-types Relevant to the types team, which will review and decide on the PR/issue.

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

9 participants