Skip to content

Commit 77af166

Browse files
committed
On fn with no borrowed argument, explain that &dyn Trait could have been an option
1 parent 1cb18f9 commit 77af166

File tree

8 files changed

+22
-2
lines changed

8 files changed

+22
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20022002
},
20032003
Applicability::MachineApplicable,
20042004
);
2005+
} else {
2006+
err.help(
2007+
"if the returned value came from a borrowed argument that implements the trait, \
2008+
then you could return `&dyn Trait`",
2009+
);
20052010
}
20062011

20072012
true

tests/ui/error-codes/E0746.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0746]: return type cannot have an unboxed trait object
44
LL | fn foo() -> dyn Trait { Struct }
55
| ^^^^^^^^^ doesn't have a size known at compile-time
66
|
7+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
78
help: return an `impl Trait` instead of a `dyn Trait`
89
|
910
LL | fn foo() -> impl Trait { Struct }
@@ -19,6 +20,7 @@ error[E0746]: return type cannot have an unboxed trait object
1920
LL | fn bar() -> dyn Trait {
2021
| ^^^^^^^^^ doesn't have a size known at compile-time
2122
|
23+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
2224
help: return an `impl Trait` instead of a `dyn Trait`
2325
|
2426
LL | fn bar() -> impl Trait {

tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ error[E0746]: return type cannot have an unboxed trait object
4848
LL | fn bap() -> Trait { Struct }
4949
| ^^^^^ doesn't have a size known at compile-time
5050
|
51+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
5152
help: return an `impl Trait` instead of a `dyn Trait`
5253
|
5354
LL | fn bap() -> impl Trait { Struct }
@@ -63,6 +64,7 @@ error[E0746]: return type cannot have an unboxed trait object
6364
LL | fn ban() -> dyn Trait { Struct }
6465
| ^^^^^^^^^ doesn't have a size known at compile-time
6566
|
67+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
6668
help: return an `impl Trait` instead of a `dyn Trait`
6769
|
6870
LL | fn ban() -> impl Trait { Struct }
@@ -78,6 +80,7 @@ error[E0746]: return type cannot have an unboxed trait object
7880
LL | fn bak() -> dyn Trait { unimplemented!() }
7981
| ^^^^^^^^^ doesn't have a size known at compile-time
8082
|
83+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
8184
help: return an `impl Trait` instead of a `dyn Trait`
8285
|
8386
LL | fn bak() -> impl Trait { unimplemented!() }
@@ -101,6 +104,7 @@ LL | return Struct;
101104
LL | }
102105
LL | 42
103106
| ^^ this returned value has type `{integer}`
107+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
104108
help: box the return type to make a boxed trait object, and wrap all of the returned values in `Box::new`
105109
|
106110
LL ~ fn bal() -> Box<dyn Trait> {
@@ -124,6 +128,7 @@ LL | Struct
124128
LL | } else {
125129
LL | 42
126130
| ^^ this returned value has type `{integer}`
131+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
127132
help: box the return type to make a boxed trait object, and wrap all of the returned values in `Box::new`
128133
|
129134
LL ~ fn bax() -> Box<dyn Trait> {
@@ -275,6 +280,7 @@ error[E0746]: return type cannot have an unboxed trait object
275280
LL | fn bat() -> dyn Trait {
276281
| ^^^^^^^^^ doesn't have a size known at compile-time
277282
|
283+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
278284
help: return an `impl Trait` instead of a `dyn Trait`
279285
|
280286
LL | fn bat() -> impl Trait {
@@ -294,6 +300,7 @@ error[E0746]: return type cannot have an unboxed trait object
294300
LL | fn bay() -> dyn Trait {
295301
| ^^^^^^^^^ doesn't have a size known at compile-time
296302
|
303+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
297304
help: return an `impl Trait` instead of a `dyn Trait`
298305
|
299306
LL | fn bay() -> impl Trait {

tests/ui/impl-trait/object-unsafe-trait-in-return-position-dyn-trait.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ help: trait `NotObjectSafe` is not object safe, so you can't return a boxed trai
9292
|
9393
LL | fn car() -> dyn NotObjectSafe {
9494
| ^^^^^^^^^^^^^
95+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
9596

9697
error[E0038]: the trait `NotObjectSafe` cannot be made into an object
9798
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:31:16

tests/ui/impl-trait/point-to-type-err-cause-on-impl-trait-return.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ error[E0746]: return type cannot have an unboxed trait object
171171
LL | fn hat() -> dyn std::fmt::Display {
172172
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
173173
|
174+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
174175
help: return an `impl Trait` instead of a `dyn Trait`
175176
|
176177
LL | fn hat() -> impl std::fmt::Display {
@@ -192,6 +193,7 @@ error[E0746]: return type cannot have an unboxed trait object
192193
LL | fn pug() -> dyn std::fmt::Display {
193194
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
194195
|
196+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
195197
help: return an `impl Trait` instead of a `dyn Trait`
196198
|
197199
LL | fn pug() -> impl std::fmt::Display {
@@ -219,6 +221,7 @@ LL | 0i32
219221
LL | } else {
220222
LL | 1u32
221223
| ^^^^ this returned value has type `u32`
224+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
222225
help: box the return type to make a boxed trait object, and wrap all of the returned values in `Box::new`
223226
|
224227
LL ~ fn man() -> Box<dyn std::fmt::Display> {

tests/ui/inference/ice-ifer-var-leaked-out-of-rollback-122098.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ LL | struct Query<'q> {}
3636
= help: consider removing `'q`, referring to it in a field, or using a marker such as `PhantomData`
3737

3838
error[E0277]: the size for values of type `Self` cannot be known at compilation time
39-
--> $DIR/ice-ifer-var-leaked-out-of-rollback-122098.rs:7:17
39+
--> $DIR/ice-ifer-var-leaked-out-of-rollback-122098.rs:7:21
4040
|
4141
LL | fn for_each(mut self, mut f: Box<dyn FnMut(Self::Item<'_>) + 'static>) {}
42-
| ^^^^^^^^ doesn't have a size known at compile-time
42+
| ^^^^ doesn't have a size known at compile-time
4343
|
4444
= help: unsized fn params are gated as an unstable feature
4545
help: consider further restricting `Self`

tests/ui/traits/bound/not-on-bare-trait-2021.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ error[E0746]: return type cannot have an unboxed trait object
6060
LL | fn qux() -> Foo {
6161
| ^^^ doesn't have a size known at compile-time
6262
|
63+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
6364
help: return an `impl Trait` instead of a `dyn Trait`
6465
|
6566
LL | fn qux() -> impl Foo {

tests/ui/traits/bound/not-on-bare-trait.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ error[E0746]: return type cannot have an unboxed trait object
154154
LL | fn qux() -> Foo {
155155
| ^^^ doesn't have a size known at compile-time
156156
|
157+
= help: if the returned value came from a borrowed argument that implements the trait, then you could return `&dyn Trait`
157158
help: return an `impl Trait` instead of a `dyn Trait`
158159
|
159160
LL | fn qux() -> impl Foo {

0 commit comments

Comments
 (0)