Skip to content

Commit 91399e3

Browse files
authored
Unrolled build for rust-lang#127987
Rollup merge of rust-lang#127987 - estebank:impl-trait-sugg, r=cjgillot More accurate suggestion for `-> Box<dyn Trait>` or `-> impl Trait` When encountering `-> Trait`, suggest `-> Box<dyn Trait>` (instead of `-> Box<Trait>`. If there's a single returned type within the `fn`, suggest `-> impl Trait`.
2 parents 41ff460 + 3ff7588 commit 91399e3

9 files changed

+71
-64
lines changed

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

+34-14
Original file line numberDiff line numberDiff line change
@@ -1793,25 +1793,42 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
17931793
err.children.clear();
17941794

17951795
let span = obligation.cause.span;
1796-
if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span)
1796+
let body = self.tcx.hir().body_owned_by(obligation.cause.body_id);
1797+
1798+
let mut visitor = ReturnsVisitor::default();
1799+
visitor.visit_body(&body);
1800+
1801+
let (pre, impl_span) = if let Ok(snip) = self.tcx.sess.source_map().span_to_snippet(span)
17971802
&& snip.starts_with("dyn ")
17981803
{
1799-
err.span_suggestion(
1800-
span.with_hi(span.lo() + BytePos(4)),
1801-
"return an `impl Trait` instead of a `dyn Trait`, \
1802-
if all returned values are the same type",
1804+
("", span.with_hi(span.lo() + BytePos(4)))
1805+
} else {
1806+
("dyn ", span.shrink_to_lo())
1807+
};
1808+
let alternatively = if visitor
1809+
.returns
1810+
.iter()
1811+
.map(|expr| self.typeck_results.as_ref().unwrap().expr_ty_adjusted_opt(expr))
1812+
.collect::<FxHashSet<_>>()
1813+
.len()
1814+
<= 1
1815+
{
1816+
err.span_suggestion_verbose(
1817+
impl_span,
1818+
"consider returning an `impl Trait` instead of a `dyn Trait`",
18031819
"impl ",
18041820
Applicability::MaybeIncorrect,
18051821
);
1806-
}
1807-
1808-
let body = self.tcx.hir().body_owned_by(obligation.cause.body_id);
1809-
1810-
let mut visitor = ReturnsVisitor::default();
1811-
visitor.visit_body(&body);
1822+
"alternatively, "
1823+
} else {
1824+
err.help("if there were a single returned type, you could use `impl Trait` instead");
1825+
""
1826+
};
18121827

1813-
let mut sugg =
1814-
vec![(span.shrink_to_lo(), "Box<".to_string()), (span.shrink_to_hi(), ">".to_string())];
1828+
let mut sugg = vec![
1829+
(span.shrink_to_lo(), format!("Box<{pre}")),
1830+
(span.shrink_to_hi(), ">".to_string()),
1831+
];
18151832
sugg.extend(visitor.returns.into_iter().flat_map(|expr| {
18161833
let span =
18171834
expr.span.find_ancestor_in_same_ctxt(obligation.cause.span).unwrap_or(expr.span);
@@ -1837,7 +1854,10 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18371854
}));
18381855

18391856
err.multipart_suggestion(
1840-
"box the return type, and wrap all of the returned values in `Box::new`",
1857+
format!(
1858+
"{alternatively}box the return type, and wrap all of the returned values in \
1859+
`Box::new`",
1860+
),
18411861
sugg,
18421862
Applicability::MaybeIncorrect,
18431863
);

tests/ui/error-codes/E0746.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ 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: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
7+
help: consider returning an `impl Trait` instead of a `dyn Trait`
88
|
99
LL | fn foo() -> impl Trait { Struct }
1010
| ~~~~
11-
help: box the return type, and wrap all of the returned values in `Box::new`
11+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
1212
|
1313
LL | fn foo() -> Box<dyn Trait> { Box::new(Struct) }
1414
| ++++ + +++++++++ +
@@ -19,10 +19,7 @@ error[E0746]: return type cannot have an unboxed trait object
1919
LL | fn bar() -> dyn Trait {
2020
| ^^^^^^^^^ doesn't have a size known at compile-time
2121
|
22-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
23-
|
24-
LL | fn bar() -> impl Trait {
25-
| ~~~~
22+
= help: if there were a single returned type, you could use `impl Trait` instead
2623
help: box the return type, and wrap all of the returned values in `Box::new`
2724
|
2825
LL ~ fn bar() -> Box<dyn Trait> {

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

+15-23
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,26 @@ 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: box the return type, and wrap all of the returned values in `Box::new`
51+
help: consider returning an `impl Trait` instead of a `dyn Trait`
52+
|
53+
LL | fn bap() -> impl Trait { Struct }
54+
| ++++
55+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
5256
|
53-
LL | fn bap() -> Box<Trait> { Box::new(Struct) }
54-
| ++++ + +++++++++ +
57+
LL | fn bap() -> Box<dyn Trait> { Box::new(Struct) }
58+
| +++++++ + +++++++++ +
5559

5660
error[E0746]: return type cannot have an unboxed trait object
5761
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:15:13
5862
|
5963
LL | fn ban() -> dyn Trait { Struct }
6064
| ^^^^^^^^^ doesn't have a size known at compile-time
6165
|
62-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
66+
help: consider returning an `impl Trait` instead of a `dyn Trait`
6367
|
6468
LL | fn ban() -> impl Trait { Struct }
6569
| ~~~~
66-
help: box the return type, and wrap all of the returned values in `Box::new`
70+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
6771
|
6872
LL | fn ban() -> Box<dyn Trait> { Box::new(Struct) }
6973
| ++++ + +++++++++ +
@@ -74,11 +78,11 @@ error[E0746]: return type cannot have an unboxed trait object
7478
LL | fn bak() -> dyn Trait { unimplemented!() }
7579
| ^^^^^^^^^ doesn't have a size known at compile-time
7680
|
77-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
81+
help: consider returning an `impl Trait` instead of a `dyn Trait`
7882
|
7983
LL | fn bak() -> impl Trait { unimplemented!() }
8084
| ~~~~
81-
help: box the return type, and wrap all of the returned values in `Box::new`
85+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
8286
|
8387
LL | fn bak() -> Box<dyn Trait> { Box::new(unimplemented!()) }
8488
| ++++ + +++++++++ +
@@ -89,10 +93,7 @@ error[E0746]: return type cannot have an unboxed trait object
8993
LL | fn bal() -> dyn Trait {
9094
| ^^^^^^^^^ doesn't have a size known at compile-time
9195
|
92-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
93-
|
94-
LL | fn bal() -> impl Trait {
95-
| ~~~~
96+
= help: if there were a single returned type, you could use `impl Trait` instead
9697
help: box the return type, and wrap all of the returned values in `Box::new`
9798
|
9899
LL ~ fn bal() -> Box<dyn Trait> {
@@ -108,10 +109,7 @@ error[E0746]: return type cannot have an unboxed trait object
108109
LL | fn bax() -> dyn Trait {
109110
| ^^^^^^^^^ doesn't have a size known at compile-time
110111
|
111-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
112-
|
113-
LL | fn bax() -> impl Trait {
114-
| ~~~~
112+
= help: if there were a single returned type, you could use `impl Trait` instead
115113
help: box the return type, and wrap all of the returned values in `Box::new`
116114
|
117115
LL ~ fn bax() -> Box<dyn Trait> {
@@ -263,10 +261,7 @@ error[E0746]: return type cannot have an unboxed trait object
263261
LL | fn bat() -> dyn Trait {
264262
| ^^^^^^^^^ doesn't have a size known at compile-time
265263
|
266-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
267-
|
268-
LL | fn bat() -> impl Trait {
269-
| ~~~~
264+
= help: if there were a single returned type, you could use `impl Trait` instead
270265
help: box the return type, and wrap all of the returned values in `Box::new`
271266
|
272267
LL ~ fn bat() -> Box<dyn Trait> {
@@ -282,10 +277,7 @@ error[E0746]: return type cannot have an unboxed trait object
282277
LL | fn bay() -> dyn Trait {
283278
| ^^^^^^^^^ doesn't have a size known at compile-time
284279
|
285-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
286-
|
287-
LL | fn bay() -> impl Trait {
288-
| ~~~~
280+
= help: if there were a single returned type, you could use `impl Trait` instead
289281
help: box the return type, and wrap all of the returned values in `Box::new`
290282
|
291283
LL ~ fn bay() -> Box<dyn Trait> {

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ error[E0746]: return type cannot have an unboxed trait object
5454
LL | fn car() -> dyn NotObjectSafe {
5555
| ^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
5656
|
57-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
58-
|
59-
LL | fn car() -> impl NotObjectSafe {
60-
| ~~~~
57+
= help: if there were a single returned type, you could use `impl Trait` instead
6158
help: box the return type, and wrap all of the returned values in `Box::new`
6259
|
6360
LL ~ fn car() -> Box<dyn NotObjectSafe> {

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ 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: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
174+
help: consider returning an `impl Trait` instead of a `dyn Trait`
175175
|
176176
LL | fn hat() -> impl std::fmt::Display {
177177
| ~~~~
178-
help: box the return type, and wrap all of the returned values in `Box::new`
178+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
179179
|
180180
LL ~ fn hat() -> Box<dyn std::fmt::Display> {
181181
LL | match 13 {
@@ -192,11 +192,11 @@ error[E0746]: return type cannot have an unboxed trait object
192192
LL | fn pug() -> dyn std::fmt::Display {
193193
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
194194
|
195-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
195+
help: consider returning an `impl Trait` instead of a `dyn Trait`
196196
|
197197
LL | fn pug() -> impl std::fmt::Display {
198198
| ~~~~
199-
help: box the return type, and wrap all of the returned values in `Box::new`
199+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
200200
|
201201
LL ~ fn pug() -> Box<dyn std::fmt::Display> {
202202
LL | match 13 {
@@ -211,10 +211,7 @@ error[E0746]: return type cannot have an unboxed trait object
211211
LL | fn man() -> dyn std::fmt::Display {
212212
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
213213
|
214-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
215-
|
216-
LL | fn man() -> impl std::fmt::Display {
217-
| ~~~~
214+
= help: if there were a single returned type, you could use `impl Trait` instead
218215
help: box the return type, and wrap all of the returned values in `Box::new`
219216
|
220217
LL ~ fn man() -> Box<dyn std::fmt::Display> {

tests/ui/issues/issue-18107.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0746]: return type cannot have an unboxed trait object
44
LL | dyn AbstractRenderer
55
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
7-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
7+
help: consider returning an `impl Trait` instead of a `dyn Trait`
88
|
99
LL | impl AbstractRenderer
1010
| ~~~~
11-
help: box the return type, and wrap all of the returned values in `Box::new`
11+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
1212
|
1313
LL ~ Box<dyn AbstractRenderer>
1414
LL |

tests/ui/unsized/box-instead-of-dyn-fn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0746]: return type cannot have an unboxed trait object
44
LL | fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> dyn Fn() + 'a {
55
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
7-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
7+
help: consider returning an `impl Trait` instead of a `dyn Trait`
88
|
99
LL | fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> impl Fn() + 'a {
1010
| ~~~~
11-
help: box the return type, and wrap all of the returned values in `Box::new`
11+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
1212
|
1313
LL ~ fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> Box<dyn Fn() + 'a> {
1414
LL |

tests/ui/unsized/issue-91801.stderr

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ error[E0746]: return type cannot have an unboxed trait object
44
LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Validator<'a> {
55
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
7-
help: box the return type, and wrap all of the returned values in `Box::new`
7+
help: consider returning an `impl Trait` instead of a `dyn Trait`
88
|
9-
LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Box<Validator<'a>> {
10-
| ++++ +
9+
LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> impl Validator<'a> {
10+
| ++++
11+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
12+
|
13+
LL | fn or<'a>(first: &'static Validator<'a>, second: &'static Validator<'a>) -> Box<dyn Validator<'a>> {
14+
| +++++++ +
1115

1216
error: aborting due to 1 previous error
1317

tests/ui/unsized/issue-91803.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0746]: return type cannot have an unboxed trait object
44
LL | fn or<'a>(first: &'static dyn Foo<'a>) -> dyn Foo<'a> {
55
| ^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
7-
help: return an `impl Trait` instead of a `dyn Trait`, if all returned values are the same type
7+
help: consider returning an `impl Trait` instead of a `dyn Trait`
88
|
99
LL | fn or<'a>(first: &'static dyn Foo<'a>) -> impl Foo<'a> {
1010
| ~~~~
11-
help: box the return type, and wrap all of the returned values in `Box::new`
11+
help: alternatively, box the return type, and wrap all of the returned values in `Box::new`
1212
|
1313
LL | fn or<'a>(first: &'static dyn Foo<'a>) -> Box<dyn Foo<'a>> {
1414
| ++++ +

0 commit comments

Comments
 (0)