Skip to content

Commit 5d2a935

Browse files
committed
Make suggestion more complete
1 parent ff297fa commit 5d2a935

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
749749
})
750750
.collect::<Vec<_>>();
751751
err.multipart_suggestion(
752-
"if you change the return type to expect trait objects box the returned expressions",
752+
"if you change the return type to expect trait objects, box the returned expressions",
753753
sugg,
754754
Applicability::MaybeIncorrect,
755755
);

compiler/rustc_typeck/src/check/coercion.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1459,14 +1459,15 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14591459
}
14601460
}
14611461
if let (Some(sp), Some(fn_output)) = (fcx.ret_coercion_span.borrow().as_ref(), fn_output) {
1462-
self.add_impl_trait_explanation(&mut err, fcx, expected, *sp, fn_output);
1462+
self.add_impl_trait_explanation(&mut err, cause, fcx, expected, *sp, fn_output);
14631463
}
14641464
err
14651465
}
14661466

14671467
fn add_impl_trait_explanation<'a>(
14681468
&self,
14691469
err: &mut DiagnosticBuilder<'a>,
1470+
cause: &ObligationCause<'tcx>,
14701471
fcx: &FnCtxt<'a, 'tcx>,
14711472
expected: Ty<'tcx>,
14721473
sp: Span,
@@ -1531,6 +1532,22 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15311532
],
15321533
Applicability::MachineApplicable,
15331534
);
1535+
let sugg = vec![sp, cause.span]
1536+
.into_iter()
1537+
.flat_map(|sp| {
1538+
vec![
1539+
(sp.shrink_to_lo(), "Box::new(".to_string()),
1540+
(sp.shrink_to_hi(), ")".to_string()),
1541+
]
1542+
.into_iter()
1543+
})
1544+
.collect::<Vec<_>>();
1545+
err.multipart_suggestion(
1546+
"if you change the return type to expect trait objects, box the returned \
1547+
expressions",
1548+
sugg,
1549+
Applicability::MaybeIncorrect,
1550+
);
15341551
} else {
15351552
err.help(&format!(
15361553
"if the trait `{}` were object safe, you could return a boxed trait object",
@@ -1539,7 +1556,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15391556
}
15401557
err.note(trait_obj_msg);
15411558
}
1542-
err.help("alternatively, create a new `enum` with a variant for each returned type");
1559+
err.help("you could instead create a new `enum` with a variant for each returned type");
15431560
}
15441561

15451562
fn is_return_ty_unsized(&self, fcx: &FnCtxt<'a, 'tcx>, blk_id: hir::HirId) -> bool {

src/test/ui/impl-trait/equality.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ LL | 0_u32
2323
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
2424
= help: if the trait `Foo` were object safe, you could return a boxed trait object
2525
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
26-
= help: alternatively, create a new `enum` with a variant for each returned type
26+
= help: you could instead create a new `enum` with a variant for each returned type
2727

2828
error[E0277]: cannot add `impl Foo` to `u32`
2929
--> $DIR/equality.rs:24:11

src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | B
1414
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
1515
= help: if the trait `NotObjectSafe` were object safe, you could return a boxed trait object
1616
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
17-
= help: alternatively, create a new `enum` with a variant for each returned type
17+
= help: you could instead create a new `enum` with a variant for each returned type
1818

1919
error[E0308]: mismatched types
2020
--> $DIR/object-unsafe-trait-in-return-position-impl-trait.rs:43:5
@@ -31,11 +31,17 @@ LL | B
3131
= note: to return `impl Trait`, all returned values must be of the same type
3232
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
3333
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
34-
= help: alternatively, create a new `enum` with a variant for each returned type
34+
= help: you could instead create a new `enum` with a variant for each returned type
3535
help: you could change the return type to be a boxed trait object
3636
|
3737
LL | fn cat() -> Box<dyn ObjectSafe> {
3838
| ^^^^^^^ ^
39+
help: if you change the return type to expect trait objects, box the returned expressions
40+
|
41+
LL | return Box::new(A);
42+
LL | }
43+
LL | Box::new(B)
44+
|
3945

4046
error: aborting due to 2 previous errors
4147

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

+46-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,17 @@ LL | 1u32
1313
= note: to return `impl Trait`, all returned values must be of the same type
1414
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
1515
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
16-
= help: alternatively, create a new `enum` with a variant for each returned type
16+
= help: you could instead create a new `enum` with a variant for each returned type
1717
help: you could change the return type to be a boxed trait object
1818
|
1919
LL | fn foo() -> Box<dyn std::fmt::Display> {
2020
| ^^^^^^^ ^
21+
help: if you change the return type to expect trait objects, box the returned expressions
22+
|
23+
LL | return Box::new(0i32);
24+
LL | }
25+
LL | Box::new(1u32)
26+
|
2127

2228
error[E0308]: mismatched types
2329
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:12:16
@@ -34,11 +40,17 @@ LL | return 1u32;
3440
= note: to return `impl Trait`, all returned values must be of the same type
3541
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
3642
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
37-
= help: alternatively, create a new `enum` with a variant for each returned type
43+
= help: you could instead create a new `enum` with a variant for each returned type
3844
help: you could change the return type to be a boxed trait object
3945
|
4046
LL | fn bar() -> Box<dyn std::fmt::Display> {
4147
| ^^^^^^^ ^
48+
help: if you change the return type to expect trait objects, box the returned expressions
49+
|
50+
LL | return Box::new(0i32);
51+
LL | } else {
52+
LL | return Box::new(1u32);
53+
|
4254

4355
error[E0308]: mismatched types
4456
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:20:9
@@ -55,11 +67,17 @@ LL | 1u32
5567
= note: to return `impl Trait`, all returned values must be of the same type
5668
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
5769
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
58-
= help: alternatively, create a new `enum` with a variant for each returned type
70+
= help: you could instead create a new `enum` with a variant for each returned type
5971
help: you could change the return type to be a boxed trait object
6072
|
6173
LL | fn baz() -> Box<dyn std::fmt::Display> {
6274
| ^^^^^^^ ^
75+
help: if you change the return type to expect trait objects, box the returned expressions
76+
|
77+
LL | return Box::new(0i32);
78+
LL | } else {
79+
LL | Box::new(1u32)
80+
|
6381

6482
error[E0308]: `if` and `else` have incompatible types
6583
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:28:9
@@ -77,7 +95,7 @@ help: you could change the return type to be a boxed trait object
7795
|
7896
LL | fn qux() -> Box<dyn std::fmt::Display> {
7997
| ^^^^^^^ ^
80-
help: if you change the return type to expect trait objects box the returned expressions
98+
help: if you change the return type to expect trait objects, box the returned expressions
8199
|
82100
LL | Box::new(0i32)
83101
LL | } else {
@@ -98,11 +116,16 @@ LL | _ => 1u32,
98116
= note: to return `impl Trait`, all returned values must be of the same type
99117
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
100118
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
101-
= help: alternatively, create a new `enum` with a variant for each returned type
119+
= help: you could instead create a new `enum` with a variant for each returned type
102120
help: you could change the return type to be a boxed trait object
103121
|
104122
LL | fn bat() -> Box<dyn std::fmt::Display> {
105123
| ^^^^^^^ ^
124+
help: if you change the return type to expect trait objects, box the returned expressions
125+
|
126+
LL | 0 => return Box::new(0i32),
127+
LL | _ => Box::new(1u32),
128+
|
106129

107130
error[E0308]: mismatched types
108131
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:40:5
@@ -120,11 +143,19 @@ LL | | }
120143
= note: to return `impl Trait`, all returned values must be of the same type
121144
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
122145
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
123-
= help: alternatively, create a new `enum` with a variant for each returned type
146+
= help: you could instead create a new `enum` with a variant for each returned type
124147
help: you could change the return type to be a boxed trait object
125148
|
126149
LL | fn can() -> Box<dyn std::fmt::Display> {
127150
| ^^^^^^^ ^
151+
help: if you change the return type to expect trait objects, box the returned expressions
152+
|
153+
LL | Box::new(match 13 {
154+
LL | 0 => return Box::new(0i32),
155+
LL | 1 => 1u32,
156+
LL | _ => 2u32,
157+
LL | })
158+
|
128159

129160
error[E0308]: mismatched types
130161
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:53:13
@@ -141,11 +172,18 @@ LL | 1u32
141172
= note: to return `impl Trait`, all returned values must be of the same type
142173
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits>
143174
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types>
144-
= help: alternatively, create a new `enum` with a variant for each returned type
175+
= help: you could instead create a new `enum` with a variant for each returned type
145176
help: you could change the return type to be a boxed trait object
146177
|
147178
LL | fn cat() -> Box<dyn std::fmt::Display> {
148179
| ^^^^^^^ ^
180+
help: if you change the return type to expect trait objects, box the returned expressions
181+
|
182+
LL | return Box::new(0i32);
183+
LL | }
184+
LL | _ => {
185+
LL | Box::new(1u32)
186+
|
149187

150188
error[E0308]: `match` arms have incompatible types
151189
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:61:14
@@ -163,7 +201,7 @@ help: you could change the return type to be a boxed trait object
163201
|
164202
LL | fn dog() -> Box<dyn std::fmt::Display> {
165203
| ^^^^^^^ ^
166-
help: if you change the return type to expect trait objects box the returned expressions
204+
help: if you change the return type to expect trait objects, box the returned expressions
167205
|
168206
LL | 0 => Box::new(0i32),
169207
LL | 1 => Box::new(1u32),

0 commit comments

Comments
 (0)