Fix incorrect suggestion for boxing tail expression in blocks
This commit is contained in:
parent
88189a71e4
commit
390ef9ba02
@ -44,7 +44,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
|| self.suggest_non_zero_new_unwrap(err, expr, expected, expr_ty)
|
|| self.suggest_non_zero_new_unwrap(err, expr, expected, expr_ty)
|
||||||
|| self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty)
|
|| self.suggest_calling_boxed_future_when_appropriate(err, expr, expected, expr_ty)
|
||||||
|| self.suggest_no_capture_closure(err, expected, expr_ty)
|
|| self.suggest_no_capture_closure(err, expected, expr_ty)
|
||||||
|| self.suggest_boxing_when_appropriate(err, expr.span, expr.hir_id, expected, expr_ty)
|
|| self.suggest_boxing_when_appropriate(
|
||||||
|
err,
|
||||||
|
expr.peel_blocks().span,
|
||||||
|
expr.hir_id,
|
||||||
|
expected,
|
||||||
|
expr_ty,
|
||||||
|
)
|
||||||
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
|
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
|
||||||
|| self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected)
|
|| self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected)
|
||||||
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)
|
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)
|
||||||
|
14
tests/ui/typeck/suggest-box-on-divergent-if-else-arms.fixed
Normal file
14
tests/ui/typeck/suggest-box-on-divergent-if-else-arms.fixed
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// run-rustfix
|
||||||
|
trait Trait {}
|
||||||
|
struct Struct;
|
||||||
|
impl Trait for Struct {}
|
||||||
|
fn foo() -> Box<dyn Trait> {
|
||||||
|
Box::new(Struct)
|
||||||
|
}
|
||||||
|
fn main() {
|
||||||
|
let _ = if true {
|
||||||
|
foo()
|
||||||
|
} else {
|
||||||
|
Box::new(Struct) //~ ERROR E0308
|
||||||
|
};
|
||||||
|
}
|
14
tests/ui/typeck/suggest-box-on-divergent-if-else-arms.rs
Normal file
14
tests/ui/typeck/suggest-box-on-divergent-if-else-arms.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
// run-rustfix
|
||||||
|
trait Trait {}
|
||||||
|
struct Struct;
|
||||||
|
impl Trait for Struct {}
|
||||||
|
fn foo() -> Box<dyn Trait> {
|
||||||
|
Box::new(Struct)
|
||||||
|
}
|
||||||
|
fn main() {
|
||||||
|
let _ = if true {
|
||||||
|
foo()
|
||||||
|
} else {
|
||||||
|
Struct //~ ERROR E0308
|
||||||
|
};
|
||||||
|
}
|
24
tests/ui/typeck/suggest-box-on-divergent-if-else-arms.stderr
Normal file
24
tests/ui/typeck/suggest-box-on-divergent-if-else-arms.stderr
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
error[E0308]: `if` and `else` have incompatible types
|
||||||
|
--> $DIR/suggest-box-on-divergent-if-else-arms.rs:12:9
|
||||||
|
|
|
||||||
|
LL | let _ = if true {
|
||||||
|
| _____________-
|
||||||
|
LL | | foo()
|
||||||
|
| | ----- expected because of this
|
||||||
|
LL | | } else {
|
||||||
|
LL | | Struct
|
||||||
|
| | ^^^^^^ expected `Box<dyn Trait>`, found `Struct`
|
||||||
|
LL | | };
|
||||||
|
| |_____- `if` and `else` have incompatible types
|
||||||
|
|
|
||||||
|
= note: expected struct `Box<dyn Trait>`
|
||||||
|
found struct `Struct`
|
||||||
|
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
|
||||||
|
help: store this in the heap by calling `Box::new`
|
||||||
|
|
|
||||||
|
LL | Box::new(Struct)
|
||||||
|
| +++++++++ +
|
||||||
|
|
||||||
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0308`.
|
Loading…
x
Reference in New Issue
Block a user