Skip to content

Commit 4d354d4

Browse files
authored
Unrolled build for rust-lang#132944
Rollup merge of rust-lang#132944 - linyihai:needing-parenthases-issue-132924, r=chenyukang add parentheses when unboxing suggestion needed This PR tried to `add parentheses when unboxing suggestion needed` Fixes rust-lang#132924
2 parents 3fb7e44 + 949cf61 commit 4d354d4

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -2648,15 +2648,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26482648
}
26492649

26502650
let make_sugg = |expr: &Expr<'_>, span: Span, sugg: &str| {
2651-
let needs_parens = match expr.kind {
2652-
// parenthesize if needed (Issue #46756)
2653-
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
2654-
// parenthesize borrows of range literals (Issue #54505)
2655-
_ if is_range_literal(expr) => true,
2656-
_ => false,
2657-
};
2658-
2659-
if needs_parens {
2651+
if self.needs_parentheses(expr) {
26602652
(
26612653
vec![
26622654
(span.shrink_to_lo(), format!("{prefix}{sugg}(")),
@@ -2869,6 +2861,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28692861
return None;
28702862
}
28712863

2864+
if self.needs_parentheses(expr) {
2865+
return Some((
2866+
vec![
2867+
(span, format!("{suggestion}(")),
2868+
(expr.span.shrink_to_hi(), ")".to_string()),
2869+
],
2870+
message,
2871+
Applicability::MachineApplicable,
2872+
true,
2873+
false,
2874+
));
2875+
}
2876+
28722877
return Some((
28732878
vec![(span, suggestion)],
28742879
message,
@@ -2897,6 +2902,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28972902
false
28982903
}
28992904

2905+
fn needs_parentheses(&self, expr: &hir::Expr<'_>) -> bool {
2906+
match expr.kind {
2907+
// parenthesize if needed (Issue #46756)
2908+
hir::ExprKind::Cast(_, _) | hir::ExprKind::Binary(_, _, _) => true,
2909+
// parenthesize borrows of range literals (Issue #54505)
2910+
_ if is_range_literal(expr) => true,
2911+
_ => false,
2912+
}
2913+
}
2914+
29002915
pub(crate) fn suggest_cast(
29012916
&self,
29022917
err: &mut Diag<'_>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ check-fail
2+
fn main() {
3+
let x = Box::new(Some(1));
4+
5+
let test: Option<i32> = x;
6+
//~^ ERROR mismatched types
7+
let x = Box::new(Some(1));
8+
let test: Option<i32> = { x as Box<Option<i32>> };
9+
//~^ ERROR mismatched types
10+
11+
let x = Box::new(Some(1));
12+
let test: Option<i32> = if true { x as Box<Option<i32>> } else { None };
13+
//~^ ERROR mismatched types
14+
15+
let x = std::rc::Rc::new(Some(1));
16+
let test: Option<i32> = x as std::rc::Rc<Option<i32>>;
17+
//~^ ERROR mismatched types
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/unboxing-needing-parenthases-issue-132924.rs:5:29
3+
|
4+
LL | let test: Option<i32> = x;
5+
| ----------- ^ expected `Option<i32>`, found `Box<Option<{integer}>>`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected enum `Option<i32>`
10+
found struct `Box<Option<{integer}>>`
11+
help: consider unboxing the value
12+
|
13+
LL | let test: Option<i32> = *x;
14+
| +
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/unboxing-needing-parenthases-issue-132924.rs:8:31
18+
|
19+
LL | let test: Option<i32> = { x as Box<Option<i32>> };
20+
| ^^^^^^^^^^^^^^^^^^^^^ expected `Option<i32>`, found `Box<Option<i32>>`
21+
|
22+
= note: expected enum `Option<_>`
23+
found struct `Box<Option<_>>`
24+
help: consider unboxing the value
25+
|
26+
LL | let test: Option<i32> = { *(x as Box<Option<i32>>) };
27+
| ++ +
28+
29+
error[E0308]: mismatched types
30+
--> $DIR/unboxing-needing-parenthases-issue-132924.rs:12:39
31+
|
32+
LL | let test: Option<i32> = if true { x as Box<Option<i32>> } else { None };
33+
| ^^^^^^^^^^^^^^^^^^^^^ expected `Option<i32>`, found `Box<Option<i32>>`
34+
|
35+
= note: expected enum `Option<_>`
36+
found struct `Box<Option<_>>`
37+
help: consider unboxing the value
38+
|
39+
LL | let test: Option<i32> = if true { *(x as Box<Option<i32>>) } else { None };
40+
| ++ +
41+
42+
error[E0308]: mismatched types
43+
--> $DIR/unboxing-needing-parenthases-issue-132924.rs:16:29
44+
|
45+
LL | let test: Option<i32> = x as std::rc::Rc<Option<i32>>;
46+
| ----------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<i32>`, found `Rc<Option<i32>>`
47+
| |
48+
| expected due to this
49+
|
50+
= note: expected enum `Option<_>`
51+
found struct `Rc<Option<_>>`
52+
help: consider dereferencing the type
53+
|
54+
LL | let test: Option<i32> = *(x as std::rc::Rc<Option<i32>>);
55+
| ++ +
56+
57+
error: aborting due to 4 previous errors
58+
59+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)