Skip to content

Commit e09db53

Browse files
committed
Provide boxing suggestion for type Alias = dyn Trait return type
1 parent f5ee3b4 commit e09db53

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,17 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18921892
// type T = dyn Trait;
18931893
// fn foo() -> impl T { .. }
18941894
err.span_note(self.tcx.def_span(def_id), "this type alias is unsized");
1895+
err.multipart_suggestion(
1896+
format!(
1897+
"consider boxing the return type, and wrapping all of the returned values in \
1898+
`Box::new`",
1899+
),
1900+
vec![
1901+
(ty.span.shrink_to_lo(), "Box<".to_string()),
1902+
(ty.span.shrink_to_hi(), ">".to_string()),
1903+
],
1904+
Applicability::MaybeIncorrect,
1905+
);
18951906
return false;
18961907
}
18971908

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ run-rustfix
2+
type T = dyn core::fmt::Debug;
3+
//~^ NOTE this type alias is unsized
4+
5+
fn f() -> Box<T> { loop {} }
6+
//~^ ERROR the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
7+
//~| HELP the trait `Sized` is not implemented for `(dyn Debug + 'static)`
8+
//~| NOTE doesn't have a size known at compile-time
9+
//~| NOTE the return type of a function must have a statically known size
10+
//~| HELP consider boxing the return type
11+
12+
fn main() {
13+
f();
14+
//~^ ERROR the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
15+
//~| HELP the trait `Sized` is not implemented for `(dyn Debug + 'static)`
16+
//~| NOTE doesn't have a size known at compile-time
17+
//~| NOTE the return type of a function must have a statically known size
18+
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@ run-rustfix
12
type T = dyn core::fmt::Debug;
23
//~^ NOTE this type alias is unsized
34

@@ -6,5 +7,12 @@ fn f() -> T { loop {} }
67
//~| HELP the trait `Sized` is not implemented for `(dyn Debug + 'static)`
78
//~| NOTE doesn't have a size known at compile-time
89
//~| NOTE the return type of a function must have a statically known size
10+
//~| HELP consider boxing the return type
911

10-
fn main() {}
12+
fn main() {
13+
f();
14+
//~^ ERROR the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
15+
//~| HELP the trait `Sized` is not implemented for `(dyn Debug + 'static)`
16+
//~| NOTE doesn't have a size known at compile-time
17+
//~| NOTE the return type of a function must have a statically known size
18+
}
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
2-
--> $DIR/dyn-trait-type-alias-return-type.rs:4:11
2+
--> $DIR/dyn-trait-type-alias-return-type.rs:5:11
33
|
44
LL | fn f() -> T { loop {} }
55
| ^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Debug + 'static)`
88
note: this type alias is unsized
9-
--> $DIR/dyn-trait-type-alias-return-type.rs:1:1
9+
--> $DIR/dyn-trait-type-alias-return-type.rs:2:1
1010
|
1111
LL | type T = dyn core::fmt::Debug;
1212
| ^^^^^^
1313
= note: the return type of a function must have a statically known size
14+
help: consider boxing the return type, and wrapping all of the returned values in `Box::new`
15+
|
16+
LL | fn f() -> Box<T> { loop {} }
17+
| ++++ +
18+
19+
error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
20+
--> $DIR/dyn-trait-type-alias-return-type.rs:13:5
21+
|
22+
LL | f();
23+
| ^^^ doesn't have a size known at compile-time
24+
|
25+
= help: the trait `Sized` is not implemented for `(dyn Debug + 'static)`
26+
= note: the return type of a function must have a statically known size
1427

15-
error: aborting due to 1 previous error
28+
error: aborting due to 2 previous errors
1629

1730
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)