From 949cf61fdce2309920ebfa9f7e8363308fbdac08 Mon Sep 17 00:00:00 2001 From: Yihai Lin <1161813899@qq.com> Date: Tue, 12 Nov 2024 15:53:37 +0800 Subject: [PATCH] add parentheses when unboxing suggestion needed --- .../src/fn_ctxt/suggestions.rs | 33 ++++++++--- ...boxing-needing-parenthases-issue-132924.rs | 18 ++++++ ...ng-needing-parenthases-issue-132924.stderr | 59 +++++++++++++++++++ 3 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 tests/ui/coercion/unboxing-needing-parenthases-issue-132924.rs create mode 100644 tests/ui/coercion/unboxing-needing-parenthases-issue-132924.stderr diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 919e83724d7..c4c4c2f200b 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2648,15 +2648,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } let make_sugg = |expr: &Expr<'_>, span: Span, sugg: &str| { - let needs_parens = match expr.kind { - // parenthesize if needed (Issue #46756) - hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true, - // parenthesize borrows of range literals (Issue #54505) - _ if is_range_literal(expr) => true, - _ => false, - }; - - if needs_parens { + if self.needs_parentheses(expr) { ( vec![ (span.shrink_to_lo(), format!("{prefix}{sugg}(")), @@ -2869,6 +2861,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return None; } + if self.needs_parentheses(expr) { + return Some(( + vec![ + (span, format!("{suggestion}(")), + (expr.span.shrink_to_hi(), ")".to_string()), + ], + message, + Applicability::MachineApplicable, + true, + false, + )); + } + return Some(( vec![(span, suggestion)], message, @@ -2897,6 +2902,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { false } + fn needs_parentheses(&self, expr: &hir::Expr<'_>) -> bool { + match expr.kind { + // parenthesize if needed (Issue #46756) + hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true, + // parenthesize borrows of range literals (Issue #54505) + _ if is_range_literal(expr) => true, + _ => false, + } + } + pub(crate) fn suggest_cast( &self, err: &mut Diag<'_>, diff --git a/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.rs b/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.rs new file mode 100644 index 00000000000..fc4258fc0af --- /dev/null +++ b/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.rs @@ -0,0 +1,18 @@ +//@ check-fail +fn main() { + let x = Box::new(Some(1)); + + let test: Option = x; + //~^ ERROR mismatched types + let x = Box::new(Some(1)); + let test: Option = { x as Box> }; + //~^ ERROR mismatched types + + let x = Box::new(Some(1)); + let test: Option = if true { x as Box> } else { None }; + //~^ ERROR mismatched types + + let x = std::rc::Rc::new(Some(1)); + let test: Option = x as std::rc::Rc>; + //~^ ERROR mismatched types +} diff --git a/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.stderr b/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.stderr new file mode 100644 index 00000000000..429b1b87357 --- /dev/null +++ b/tests/ui/coercion/unboxing-needing-parenthases-issue-132924.stderr @@ -0,0 +1,59 @@ +error[E0308]: mismatched types + --> $DIR/unboxing-needing-parenthases-issue-132924.rs:5:29 + | +LL | let test: Option = x; + | ----------- ^ expected `Option`, found `Box>` + | | + | expected due to this + | + = note: expected enum `Option` + found struct `Box>` +help: consider unboxing the value + | +LL | let test: Option = *x; + | + + +error[E0308]: mismatched types + --> $DIR/unboxing-needing-parenthases-issue-132924.rs:8:31 + | +LL | let test: Option = { x as Box> }; + | ^^^^^^^^^^^^^^^^^^^^^ expected `Option`, found `Box>` + | + = note: expected enum `Option<_>` + found struct `Box>` +help: consider unboxing the value + | +LL | let test: Option = { *(x as Box>) }; + | ++ + + +error[E0308]: mismatched types + --> $DIR/unboxing-needing-parenthases-issue-132924.rs:12:39 + | +LL | let test: Option = if true { x as Box> } else { None }; + | ^^^^^^^^^^^^^^^^^^^^^ expected `Option`, found `Box>` + | + = note: expected enum `Option<_>` + found struct `Box>` +help: consider unboxing the value + | +LL | let test: Option = if true { *(x as Box>) } else { None }; + | ++ + + +error[E0308]: mismatched types + --> $DIR/unboxing-needing-parenthases-issue-132924.rs:16:29 + | +LL | let test: Option = x as std::rc::Rc>; + | ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option`, found `Rc>` + | | + | expected due to this + | + = note: expected enum `Option<_>` + found struct `Rc>` +help: consider dereferencing the type + | +LL | let test: Option = *(x as std::rc::Rc>); + | ++ + + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0308`.