From b60321b6a282c5a71a7c3017d8bb39e236a227c5 Mon Sep 17 00:00:00 2001 From: Yukang Date: Thu, 23 Jul 2026 05:40:10 +0800 Subject: [PATCH 1/5] Allow unary operand types to be inferred later --- compiler/rustc_hir_typeck/src/expr.rs | 53 +++++++++++-------- ...late-raw-pointer-inference-issue-106138.rs | 14 +++++ ...-raw-pointer-inference-issue-106138.stderr | 17 ++++++ ...associated-output-mismatch-issue-106138.rs | 42 +++++++++++++++ ...ciated-output-mismatch-issue-106138.stderr | 42 +++++++++++++++ ...-op-late-operand-inference-issue-106138.rs | 49 +++++++++++++++++ 6 files changed, 195 insertions(+), 22 deletions(-) create mode 100644 tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.rs create mode 100644 tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.stderr create mode 100644 tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.rs create mode 100644 tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr create mode 100644 tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index a301cb6a397d9..caa06e7542c20 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -426,35 +426,44 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return Ty::new_error(tcx, guar); } - let oprnd_t = self.structurally_resolve_type(expr.span, oprnd_t); + // `Not` and `Neg` can be selected using an unresolved operand type, + // later constraints may still determine that type, + // so we call `resolve_vars_with_obligations` for them. match unop { - hir::UnOp::Deref => self.lookup_derefing(expr, oprnd, oprnd_t).unwrap_or_else(|| { - let mut err = - self.dcx().create_err(CantDereference { span: expr.span, ty: oprnd_t }); - let sp = tcx.sess.source_map().start_point(expr.span).with_parent(None); - if let Some(sp) = - tcx.sess.psess.complete_stmt_exprs_before_bin_op_lookalike.borrow().get(&sp) - { - err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp)); - } - // The operand may be an uncalled function, in which case it is its return type - // the user meant to dereference. Only suggest the call when that return type is - // itself dereferenceable, mirroring the checks `lookup_derefing` just failed. - self.suggest_fn_call(&mut err, oprnd, oprnd_t, |output| { - output.builtin_deref(true).is_some() - || self.tcx.lang_items().deref_trait().is_some_and(|deref_trait| { - self.type_implements_trait(deref_trait, [output], self.param_env) - .may_apply() - }) - }); - Ty::new_error(tcx, err.emit_err()) - }), + hir::UnOp::Deref => { + // Dereferencing must distinguish builtin pointers from + // overloaded `Deref`, so it still requires a structurally resolved type. + let oprnd_t = self.structurally_resolve_type(expr.span, oprnd_t); + self.lookup_derefing(expr, oprnd, oprnd_t).unwrap_or_else(|| { + let mut err = + self.dcx().create_err(CantDereference { span: expr.span, ty: oprnd_t }); + let sp = tcx.sess.source_map().start_point(expr.span).with_parent(None); + if let Some(sp) = + tcx.sess.psess.complete_stmt_exprs_before_bin_op_lookalike.borrow().get(&sp) + { + err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp)); + } + // The operand may be an uncalled function, in which case it is its return type + // the user meant to dereference. Only suggest the call when that return type is + // itself dereferenceable, mirroring the checks `lookup_derefing` just failed. + self.suggest_fn_call(&mut err, oprnd, oprnd_t, |output| { + output.builtin_deref(true).is_some() + || self.tcx.lang_items().deref_trait().is_some_and(|deref_trait| { + self.type_implements_trait(deref_trait, [output], self.param_env) + .may_apply() + }) + }); + Ty::new_error(tcx, err.emit_err()) + }) + } hir::UnOp::Not => { + let oprnd_t = self.resolve_vars_with_obligations(oprnd_t); let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner); // If it's builtin, we can reuse the type, this helps inference. if oprnd_t.is_integral() || *oprnd_t.kind() == ty::Bool { oprnd_t } else { result } } hir::UnOp::Neg => { + let oprnd_t = self.resolve_vars_with_obligations(oprnd_t); let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner); // If it's builtin, we can reuse the type, this helps inference. if oprnd_t.is_numeric() { oprnd_t } else { result } diff --git a/tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.rs b/tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.rs new file mode 100644 index 0000000000000..c2d67e02ed520 --- /dev/null +++ b/tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.rs @@ -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 { + loop {} +} + +fn main() { + let pointer = make(); + //~^ ERROR type annotations needed + let value = unsafe { *pointer }; + let _: *const u8 = pointer; + let _: u8 = value; +} diff --git a/tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.stderr b/tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.stderr new file mode 100644 index 0000000000000..f6a2a772ffe54 --- /dev/null +++ b/tests/ui/inference/unary-deref-late-raw-pointer-inference-issue-106138.stderr @@ -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`. diff --git a/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.rs b/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.rs new file mode 100644 index 0000000000000..b30535115602e --- /dev/null +++ b/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.rs @@ -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 { + 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; +} diff --git a/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr b/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr new file mode 100644 index 0000000000000..a1772e57f9420 --- /dev/null +++ b/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr @@ -0,0 +1,42 @@ +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 == _` +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 = ::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`. diff --git a/tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs b/tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs new file mode 100644 index 0000000000000..549c7a91d6f00 --- /dev/null +++ b/tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs @@ -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) { + let closure = |i, a: &Vec| !a[i]; + let _ = closure(0, x); +} + +fn neg_index(x: &Vec) { + let closure = |i, a: &Vec| -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(); +} From 9fce4c4d7eaa4d822feef4be6131f9de80c0dbdd Mon Sep 17 00:00:00 2001 From: Yukang Date: Mon, 24 Aug 2026 11:27:06 +0800 Subject: [PATCH 2/5] bless mismatch for unary-op-associated-output-mismatch-issue-106138 --- .../unary-op-associated-output-mismatch-issue-106138.stderr | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr b/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr index a1772e57f9420..30318239390e5 100644 --- a/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr +++ b/tests/ui/inference/unary-op-associated-output-mismatch-issue-106138.stderr @@ -20,6 +20,7 @@ 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(); From 722a9e4d8bfe62ba2d54f6bd6dfad5e96b9dc51f Mon Sep 17 00:00:00 2001 From: Yukang Date: Mon, 24 Aug 2026 11:45:57 +0800 Subject: [PATCH 3/5] add regression test for issue 26830 --- ...-neg-late-operand-inference-issue-26830.rs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 tests/ui/inference/unary-neg-late-operand-inference-issue-26830.rs diff --git a/tests/ui/inference/unary-neg-late-operand-inference-issue-26830.rs b/tests/ui/inference/unary-neg-late-operand-inference-issue-26830.rs new file mode 100644 index 0000000000000..9f2ad98e5d6b4 --- /dev/null +++ b/tests/ui/inference/unary-neg-late-operand-inference-issue-26830.rs @@ -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 { + 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(); +} From 01ae02505dab20a8fbcd388334923eb38ec3831a Mon Sep 17 00:00:00 2001 From: Yukang Date: Wed, 2 Sep 2026 18:13:26 +0800 Subject: [PATCH 4/5] fix comment for check_expr_unop with Deref Co-authored-by: lcnr --- compiler/rustc_hir_typeck/src/expr.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index caa06e7542c20..d0824ff8d1a46 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -426,13 +426,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return Ty::new_error(tcx, guar); } - // `Not` and `Neg` can be selected using an unresolved operand type, - // later constraints may still determine that type, - // so we call `resolve_vars_with_obligations` for them. + // Resolve what we can for `Not` and `Neg`, but allow later constraints + // to determine an unresolved operand type, + // so we call `deeply_resolve_ignoring_regions_with_obligations` for them. match unop { hir::UnOp::Deref => { // Dereferencing must distinguish builtin pointers from // overloaded `Deref`, so it still requires a structurally resolved type. + // + // This is necessary as raw pointers do not implement `Deref`. let oprnd_t = self.structurally_resolve_type(expr.span, oprnd_t); self.lookup_derefing(expr, oprnd, oprnd_t).unwrap_or_else(|| { let mut err = @@ -457,13 +459,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }) } hir::UnOp::Not => { - let oprnd_t = self.resolve_vars_with_obligations(oprnd_t); + let oprnd_t = self.deeply_resolve_ignoring_regions_with_obligations(oprnd_t); let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner); // If it's builtin, we can reuse the type, this helps inference. if oprnd_t.is_integral() || *oprnd_t.kind() == ty::Bool { oprnd_t } else { result } } hir::UnOp::Neg => { - let oprnd_t = self.resolve_vars_with_obligations(oprnd_t); + let oprnd_t = self.deeply_resolve_ignoring_regions_with_obligations(oprnd_t); let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner); // If it's builtin, we can reuse the type, this helps inference. if oprnd_t.is_numeric() { oprnd_t } else { result } From a97890a1724f90a9a11e42298e0e282f6ce07eae Mon Sep 17 00:00:00 2001 From: Yukang Date: Sun, 11 Oct 2026 11:07:26 +0800 Subject: [PATCH 5/5] add test unary operand autoderef behavior --- ...-op-late-operand-inference-issue-106138.rs | 16 +++++ tests/ui/inference/unary-op-no-autoderef.rs | 60 +++++++++++++++++++ .../ui/inference/unary-op-no-autoderef.stderr | 31 ++++++++++ 3 files changed, 107 insertions(+) create mode 100644 tests/ui/inference/unary-op-no-autoderef.rs create mode 100644 tests/ui/inference/unary-op-no-autoderef.stderr diff --git a/tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs b/tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs index 549c7a91d6f00..ca1b5eb66e550 100644 --- a/tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs +++ b/tests/ui/inference/unary-op-late-operand-inference-issue-106138.rs @@ -5,16 +5,30 @@ 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) { let closure = |i, a: &Vec| !a[i]; let _ = closure(0, x); } +// Likewise, the `-` is applied to the `i32` produced by `a[i]`. fn neg_index(x: &Vec) { let closure = |i, a: &Vec| -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; @@ -44,6 +58,8 @@ fn output_expectation_differs_from_operand() { 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(); } diff --git a/tests/ui/inference/unary-op-no-autoderef.rs b/tests/ui/inference/unary-op-no-autoderef.rs new file mode 100644 index 0000000000000..9939dd287480f --- /dev/null +++ b/tests/ui/inference/unary-op-no-autoderef.rs @@ -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 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` +} diff --git a/tests/ui/inference/unary-op-no-autoderef.stderr b/tests/ui/inference/unary-op-no-autoderef.stderr new file mode 100644 index 0000000000000..f77cdc02a2ae1 --- /dev/null +++ b/tests/ui/inference/unary-op-no-autoderef.stderr @@ -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`.