@@ -680,42 +680,53 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
680
680
ex. span ,
681
681
format ! ( "cannot apply unary operator `{}`" , op. as_str( ) ) ,
682
682
) ;
683
- match actual. kind ( ) {
684
- Uint ( _) if op == hir:: UnOp :: Neg => {
685
- err. note ( "unsigned values cannot be negated" ) ;
686
-
687
- if let hir:: ExprKind :: Unary (
688
- _,
689
- hir:: Expr {
690
- kind :
691
- hir:: ExprKind :: Lit ( Spanned {
692
- node : ast:: LitKind :: Int ( 1 , _) ,
693
- ..
694
- } ) ,
695
- ..
696
- } ,
697
- ) = ex. kind
698
- {
699
- err. span_suggestion (
700
- ex. span ,
701
- & format ! (
702
- "you may have meant the maximum value of `{}`" ,
703
- actual
704
- ) ,
705
- format ! ( "{}::MAX" , actual) ,
706
- Applicability :: MaybeIncorrect ,
707
- ) ;
683
+
684
+ let sp = self . tcx . sess . source_map ( ) . start_point ( ex. span ) ;
685
+ if let Some ( sp) =
686
+ self . tcx . sess . parse_sess . ambiguous_block_expr_parse . borrow ( ) . get ( & sp)
687
+ {
688
+ // If the previous expression was a block expression, suggest parentheses
689
+ // (turning this into a binary subtraction operation instead.)
690
+ // for example, `{2} - 2` -> `({2}) - 2` (see src\test\ui\parser\expr-as-stmt.rs)
691
+ self . tcx . sess . parse_sess . expr_parentheses_needed ( & mut err, * sp) ;
692
+ } else {
693
+ match actual. kind ( ) {
694
+ Uint ( _) if op == hir:: UnOp :: Neg => {
695
+ err. note ( "unsigned values cannot be negated" ) ;
696
+
697
+ if let hir:: ExprKind :: Unary (
698
+ _,
699
+ hir:: Expr {
700
+ kind :
701
+ hir:: ExprKind :: Lit ( Spanned {
702
+ node : ast:: LitKind :: Int ( 1 , _) ,
703
+ ..
704
+ } ) ,
705
+ ..
706
+ } ,
707
+ ) = ex. kind
708
+ {
709
+ err. span_suggestion (
710
+ ex. span ,
711
+ & format ! (
712
+ "you may have meant the maximum value of `{}`" ,
713
+ actual
714
+ ) ,
715
+ format ! ( "{}::MAX" , actual) ,
716
+ Applicability :: MaybeIncorrect ,
717
+ ) ;
718
+ }
719
+ }
720
+ Str | Never | Char | Tuple ( _) | Array ( _, _) => { }
721
+ Ref ( _, ref lty, _) if * lty. kind ( ) == Str => { }
722
+ _ => {
723
+ let missing_trait = match op {
724
+ hir:: UnOp :: Neg => "std::ops::Neg" ,
725
+ hir:: UnOp :: Not => "std::ops::Not" ,
726
+ hir:: UnOp :: Deref => "std::ops::UnDerf" ,
727
+ } ;
728
+ suggest_impl_missing ( & mut err, operand_ty, & missing_trait) ;
708
729
}
709
- }
710
- Str | Never | Char | Tuple ( _) | Array ( _, _) => { }
711
- Ref ( _, ref lty, _) if * lty. kind ( ) == Str => { }
712
- _ => {
713
- let missing_trait = match op {
714
- hir:: UnOp :: Neg => "std::ops::Neg" ,
715
- hir:: UnOp :: Not => "std::ops::Not" ,
716
- hir:: UnOp :: Deref => "std::ops::UnDerf" ,
717
- } ;
718
- suggest_impl_missing ( & mut err, operand_ty, & missing_trait) ;
719
730
}
720
731
}
721
732
err. emit ( ) ;
0 commit comments