-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix incorrect suggestion for boxing tail expression in blocks
- Loading branch information
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/ui/typeck/suggest-box-on-divergent-if-else-arms.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 24 additions & 0 deletions
24
tests/ui/typeck/suggest-box-on-divergent-if-else-arms.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |