forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#120261 - estebank:issue-102629, r=wesleywiser
Provide structured suggestion to use trait objects in some cases of `if` arm type divergence ``` error[E0308]: `if` and `else` have incompatible types --> $DIR/suggest-box-on-divergent-if-else-arms.rs:15:9 | LL | let _ = if true { | _____________- LL | | Struct | | ------ expected because of this LL | | } else { LL | | foo() | | ^^^^^ expected `Struct`, found `Box<dyn Trait>` LL | | }; | |_____- `if` and `else` have incompatible types | = note: expected struct `Struct` found struct `Box<dyn Trait>` help: `Struct` implements `Trait` so you can box it to coerce to the trait object `Box<dyn Trait>` | LL | Box::new(Struct) | +++++++++ + error[E0308]: `if` and `else` have incompatible types --> $DIR/suggest-box-on-divergent-if-else-arms.rs:20: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[E0308]: `if` and `else` have incompatible types --> $DIR/suggest-box-on-divergent-if-else-arms.rs:25:9 | LL | fn bar() -> impl Trait { | ---------- the found opaque type ... LL | let _ = if true { | _____________- LL | | Struct | | ------ expected because of this LL | | } else { LL | | bar() | | ^^^^^ expected `Struct`, found opaque type LL | | }; | |_____- `if` and `else` have incompatible types | = note: expected struct `Struct` found opaque type `impl Trait` help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>` | LL ~ Box::new(Struct) as Box<dyn Trait> LL | } else { LL ~ Box::new(bar()) | error[E0308]: `if` and `else` have incompatible types --> $DIR/suggest-box-on-divergent-if-else-arms.rs:30:9 | LL | fn bar() -> impl Trait { | ---------- the expected opaque type ... LL | let _ = if true { | _____________- LL | | bar() | | ----- expected because of this LL | | } else { LL | | Struct | | ^^^^^^ expected opaque type, found `Struct` LL | | }; | |_____- `if` and `else` have incompatible types | = note: expected opaque type `impl Trait` found struct `Struct` help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>` | LL ~ Box::new(bar()) as Box<dyn Trait> LL | } else { LL ~ Box::new(Struct) | ``` Partially address rust-lang#102629.
- Loading branch information
Showing
5 changed files
with
267 additions
and
17 deletions.
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
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
32 changes: 32 additions & 0 deletions
32
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,32 @@ | ||
// run-rustfix | ||
trait Trait {} | ||
struct Struct; | ||
impl Trait for Struct {} | ||
fn foo() -> Box<dyn Trait> { | ||
Box::new(Struct) | ||
} | ||
fn bar() -> impl Trait { | ||
Struct | ||
} | ||
fn main() { | ||
let _ = if true { | ||
Box::new(Struct) | ||
} else { | ||
foo() //~ ERROR E0308 | ||
}; | ||
let _ = if true { | ||
foo() | ||
} else { | ||
Box::new(Struct) //~ ERROR E0308 | ||
}; | ||
let _ = if true { | ||
Box::new(Struct) as Box<dyn Trait> | ||
} else { | ||
Box::new(bar()) //~ ERROR E0308 | ||
}; | ||
let _ = if true { | ||
Box::new(bar()) as Box<dyn Trait> | ||
} 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,32 @@ | ||
// run-rustfix | ||
trait Trait {} | ||
struct Struct; | ||
impl Trait for Struct {} | ||
fn foo() -> Box<dyn Trait> { | ||
Box::new(Struct) | ||
} | ||
fn bar() -> impl Trait { | ||
Struct | ||
} | ||
fn main() { | ||
let _ = if true { | ||
Struct | ||
} else { | ||
foo() //~ ERROR E0308 | ||
}; | ||
let _ = if true { | ||
foo() | ||
} else { | ||
Struct //~ ERROR E0308 | ||
}; | ||
let _ = if true { | ||
Struct | ||
} else { | ||
bar() //~ ERROR E0308 | ||
}; | ||
let _ = if true { | ||
bar() | ||
} else { | ||
Struct //~ ERROR E0308 | ||
}; | ||
} |
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
error[E0308]: `if` and `else` have incompatible types | ||
--> $DIR/suggest-box-on-divergent-if-else-arms.rs:15:9 | ||
| | ||
LL | let _ = if true { | ||
| _____________- | ||
LL | | Struct | ||
| | ------ expected because of this | ||
LL | | } else { | ||
LL | | foo() | ||
| | ^^^^^ expected `Struct`, found `Box<dyn Trait>` | ||
LL | | }; | ||
| |_____- `if` and `else` have incompatible types | ||
| | ||
= note: expected struct `Struct` | ||
found struct `Box<dyn Trait>` | ||
help: `Struct` implements `Trait` so you can box it to coerce to the trait object `Box<dyn Trait>` | ||
| | ||
LL | Box::new(Struct) | ||
| +++++++++ + | ||
|
||
error[E0308]: `if` and `else` have incompatible types | ||
--> $DIR/suggest-box-on-divergent-if-else-arms.rs:20: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[E0308]: `if` and `else` have incompatible types | ||
--> $DIR/suggest-box-on-divergent-if-else-arms.rs:25:9 | ||
| | ||
LL | fn bar() -> impl Trait { | ||
| ---------- the found opaque type | ||
... | ||
LL | let _ = if true { | ||
| _____________- | ||
LL | | Struct | ||
| | ------ expected because of this | ||
LL | | } else { | ||
LL | | bar() | ||
| | ^^^^^ expected `Struct`, found opaque type | ||
LL | | }; | ||
| |_____- `if` and `else` have incompatible types | ||
| | ||
= note: expected struct `Struct` | ||
found opaque type `impl Trait` | ||
help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>` | ||
| | ||
LL ~ Box::new(Struct) as Box<dyn Trait> | ||
LL | } else { | ||
LL ~ Box::new(bar()) | ||
| | ||
|
||
error[E0308]: `if` and `else` have incompatible types | ||
--> $DIR/suggest-box-on-divergent-if-else-arms.rs:30:9 | ||
| | ||
LL | fn bar() -> impl Trait { | ||
| ---------- the expected opaque type | ||
... | ||
LL | let _ = if true { | ||
| _____________- | ||
LL | | bar() | ||
| | ----- expected because of this | ||
LL | | } else { | ||
LL | | Struct | ||
| | ^^^^^^ expected opaque type, found `Struct` | ||
LL | | }; | ||
| |_____- `if` and `else` have incompatible types | ||
| | ||
= note: expected opaque type `impl Trait` | ||
found struct `Struct` | ||
help: `Struct` implements `Trait` so you can box both arms and coerce to the trait object `Box<dyn Trait>` | ||
| | ||
LL ~ Box::new(bar()) as Box<dyn Trait> | ||
LL | } else { | ||
LL ~ Box::new(Struct) | ||
| | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |