@@ -426,35 +426,46 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
426426 return Ty :: new_error ( tcx, guar) ;
427427 }
428428
429- let oprnd_t = self . structurally_resolve_type ( expr. span , oprnd_t) ;
429+ // Resolve what we can for `Not` and `Neg`, but allow later constraints
430+ // to determine an unresolved operand type,
431+ // so we call `deeply_resolve_ignoring_regions_with_obligations` for them.
430432 match unop {
431- hir:: UnOp :: Deref => self . lookup_derefing ( expr, oprnd, oprnd_t) . unwrap_or_else ( || {
432- let mut err =
433- self . dcx ( ) . create_err ( CantDereference { span : expr. span , ty : oprnd_t } ) ;
434- let sp = tcx. sess . source_map ( ) . start_point ( expr. span ) . with_parent ( None ) ;
435- if let Some ( sp) =
436- tcx. sess . psess . complete_stmt_exprs_before_bin_op_lookalike . borrow ( ) . get ( & sp)
437- {
438- err. subdiagnostic ( ExprParenthesesNeeded :: surrounding ( * sp) ) ;
439- }
440- // The operand may be an uncalled function, in which case it is its return type
441- // the user meant to dereference. Only suggest the call when that return type is
442- // itself dereferenceable, mirroring the checks `lookup_derefing` just failed.
443- self . suggest_fn_call ( & mut err, oprnd, oprnd_t, |output| {
444- output. builtin_deref ( true ) . is_some ( )
445- || self . tcx . lang_items ( ) . deref_trait ( ) . is_some_and ( |deref_trait| {
446- self . type_implements_trait ( deref_trait, [ output] , self . param_env )
447- . may_apply ( )
448- } )
449- } ) ;
450- Ty :: new_error ( tcx, err. emit_err ( ) )
451- } ) ,
433+ hir:: UnOp :: Deref => {
434+ // Dereferencing must distinguish builtin pointers from
435+ // overloaded `Deref`, so it still requires a structurally resolved type.
436+ //
437+ // This is necessary as raw pointers do not implement `Deref`.
438+ let oprnd_t = self . structurally_resolve_type ( expr. span , oprnd_t) ;
439+ self . lookup_derefing ( expr, oprnd, oprnd_t) . unwrap_or_else ( || {
440+ let mut err =
441+ self . dcx ( ) . create_err ( CantDereference { span : expr. span , ty : oprnd_t } ) ;
442+ let sp = tcx. sess . source_map ( ) . start_point ( expr. span ) . with_parent ( None ) ;
443+ if let Some ( sp) =
444+ tcx. sess . psess . complete_stmt_exprs_before_bin_op_lookalike . borrow ( ) . get ( & sp)
445+ {
446+ err. subdiagnostic ( ExprParenthesesNeeded :: surrounding ( * sp) ) ;
447+ }
448+ // The operand may be an uncalled function, in which case it is its return type
449+ // the user meant to dereference. Only suggest the call when that return type is
450+ // itself dereferenceable, mirroring the checks `lookup_derefing` just failed.
451+ self . suggest_fn_call ( & mut err, oprnd, oprnd_t, |output| {
452+ output. builtin_deref ( true ) . is_some ( )
453+ || self . tcx . lang_items ( ) . deref_trait ( ) . is_some_and ( |deref_trait| {
454+ self . type_implements_trait ( deref_trait, [ output] , self . param_env )
455+ . may_apply ( )
456+ } )
457+ } ) ;
458+ Ty :: new_error ( tcx, err. emit_err ( ) )
459+ } )
460+ }
452461 hir:: UnOp :: Not => {
462+ let oprnd_t = self . deeply_resolve_ignoring_regions_with_obligations ( oprnd_t) ;
453463 let result = self . check_user_unop ( expr, oprnd_t, unop, expected_inner) ;
454464 // If it's builtin, we can reuse the type, this helps inference.
455465 if oprnd_t. is_integral ( ) || * oprnd_t. kind ( ) == ty:: Bool { oprnd_t } else { result }
456466 }
457467 hir:: UnOp :: Neg => {
468+ let oprnd_t = self . deeply_resolve_ignoring_regions_with_obligations ( oprnd_t) ;
458469 let result = self . check_user_unop ( expr, oprnd_t, unop, expected_inner) ;
459470 // If it's builtin, we can reuse the type, this helps inference.
460471 if oprnd_t. is_numeric ( ) { oprnd_t } else { result }
0 commit comments