Skip to content

Commit ea7e885

Browse files
committed
Elide E0308 errors in favor of E0746
When a type error involves a `dyn Trait` as the return type, do not emit the type error, as the "return type is not `Sized`" error will provide enough information to the user.
1 parent 75eabb1 commit ea7e885

File tree

6 files changed

+44
-124
lines changed

6 files changed

+44
-124
lines changed

src/librustc_typeck/check/coercion.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
12221222
};
12231223

12241224
let mut err;
1225+
let mut unsized_return = false;
12251226
match cause.code {
12261227
ObligationCauseCode::ReturnNoExpression => {
12271228
err = struct_span_err!(
@@ -1243,6 +1244,9 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
12431244
parent_id,
12441245
expression.map(|expr| (expr, blk_id)),
12451246
);
1247+
if !fcx.tcx.features().unsized_locals {
1248+
unsized_return = fcx.is_unsized_return(blk_id);
1249+
}
12461250
}
12471251
ObligationCauseCode::ReturnValue(id) => {
12481252
err = self.report_return_mismatched_types(
@@ -1254,6 +1258,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
12541258
id,
12551259
None,
12561260
);
1261+
if !fcx.tcx.features().unsized_locals {
1262+
let id = fcx.tcx.hir().get_parent_node(id);
1263+
unsized_return = fcx.is_unsized_return(id);
1264+
}
12571265
}
12581266
_ => {
12591267
err = fcx.report_mismatched_types(cause, expected, found, coercion_error);
@@ -1282,7 +1290,16 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
12821290
.filter(|e| fcx.is_assign_to_bool(e, self.expected_ty()))
12831291
.is_some();
12841292

1285-
err.emit_unless(assign_to_bool);
1293+
if unsized_return {
1294+
fcx.tcx.sess.delay_span_bug(
1295+
cause.span,
1296+
&format!(
1297+
"elided E0308 in favor of more detailed E0277 or E0746: {:?}",
1298+
cause.code
1299+
),
1300+
);
1301+
}
1302+
err.emit_unless(assign_to_bool || unsized_return);
12861303

12871304
self.final_ty = Some(fcx.tcx.types.err);
12881305
}

src/librustc_typeck/check/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4964,6 +4964,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
49644964
}
49654965
}
49664966

4967+
fn is_unsized_return(&self, blk_id: hir::HirId) -> bool {
4968+
if let Some((fn_decl, _)) = self.get_fn_decl(blk_id) {
4969+
if let hir::FunctionRetTy::Return(ty) = fn_decl.output {
4970+
let ty = AstConv::ast_ty_to_ty(self, ty);
4971+
if let ty::Dynamic(..) = ty.kind {
4972+
return true;
4973+
}
4974+
}
4975+
}
4976+
false
4977+
}
4978+
49674979
/// A possible error is to forget to add a return type that is needed:
49684980
///
49694981
/// ```

src/test/ui/error-codes/E0746.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ impl Trait for u32 {}
55

66
fn foo() -> dyn Trait { Struct }
77
//~^ ERROR E0746
8-
//~| ERROR E0308
98

109
fn bar() -> dyn Trait { //~ ERROR E0746
1110
if true {
12-
return 0; //~ ERROR E0308
11+
return 0;
1312
}
14-
42 //~ ERROR E0308
13+
42
1514
}
1615

1716
fn main() {}

src/test/ui/error-codes/E0746.stderr

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/E0746.rs:6:25
3-
|
4-
LL | fn foo() -> dyn Trait { Struct }
5-
| --------- ^^^^^^ expected trait `Trait`, found struct `Struct`
6-
| |
7-
| expected `(dyn Trait + 'static)` because of return type
8-
|
9-
= note: expected trait object `(dyn Trait + 'static)`
10-
found struct `Struct`
11-
121
error[E0746]: return type cannot have a bare trait because it must be `Sized`
132
--> $DIR/E0746.rs:6:13
143
|
@@ -22,7 +11,7 @@ LL | fn foo() -> impl Trait { Struct }
2211
| ^^^^^^^^^^
2312

2413
error[E0746]: return type cannot have a bare trait because it must be `Sized`
25-
--> $DIR/E0746.rs:10:13
14+
--> $DIR/E0746.rs:9:13
2615
|
2716
LL | fn bar() -> dyn Trait {
2817
| ^^^^^^^^^ doesn't have a size known at compile-time
@@ -33,30 +22,5 @@ help: you can use the `impl Trait` feature in the return type because all the re
3322
LL | fn bar() -> impl Trait {
3423
| ^^^^^^^^^^
3524

36-
error[E0308]: mismatched types
37-
--> $DIR/E0746.rs:12:16
38-
|
39-
LL | fn bar() -> dyn Trait {
40-
| --------- expected `(dyn Trait + 'static)` because of return type
41-
LL | if true {
42-
LL | return 0;
43-
| ^ expected trait `Trait`, found integer
44-
|
45-
= note: expected trait object `(dyn Trait + 'static)`
46-
found type `{integer}`
47-
48-
error[E0308]: mismatched types
49-
--> $DIR/E0746.rs:14:5
50-
|
51-
LL | fn bar() -> dyn Trait {
52-
| --------- expected `(dyn Trait + 'static)` because of return type
53-
...
54-
LL | 42
55-
| ^^ expected trait `Trait`, found integer
56-
|
57-
= note: expected trait object `(dyn Trait + 'static)`
58-
found type `{integer}`
59-
60-
error: aborting due to 5 previous errors
25+
error: aborting due to 2 previous errors
6126

62-
For more information about this error, try `rustc --explain E0308`.

src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,23 @@ fn bar() -> (usize, dyn Trait) { (42, Struct) }
1212
//~| ERROR E0308
1313
fn bap() -> Trait { Struct }
1414
//~^ ERROR E0746
15-
//~| ERROR E0308
1615
fn ban() -> dyn Trait { Struct }
1716
//~^ ERROR E0746
18-
//~| ERROR E0308
1917
fn bak() -> dyn Trait { unimplemented!() } //~ ERROR E0277
2018
// Suggest using `Box<dyn Trait>`
2119
fn bal() -> dyn Trait { //~ ERROR E0746
2220
if true {
23-
return Struct; //~ ERROR E0308
21+
return Struct;
2422
}
25-
42 //~ ERROR E0308
23+
42
2624
}
2725

2826
// Suggest using `impl Trait`
2927
fn bat() -> dyn Trait { //~ ERROR E0746
3028
if true {
31-
return 0; //~ ERROR E0308
29+
return 0;
3230
}
33-
42 //~ ERROR E0308
31+
42
3432
}
3533

3634
fn main() {}

src/test/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr

Lines changed: 6 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,6 @@ LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
3838
= note: required because it appears within the type `(usize, (dyn Trait + 'static))`
3939
= note: the return type of a function must have a statically known size
4040

41-
error[E0308]: mismatched types
42-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:13:21
43-
|
44-
LL | fn bap() -> Trait { Struct }
45-
| ----- ^^^^^^ expected trait `Trait`, found struct `Struct`
46-
| |
47-
| expected `(dyn Trait + 'static)` because of return type
48-
|
49-
= note: expected trait object `(dyn Trait + 'static)`
50-
found struct `Struct`
51-
5241
error[E0746]: return type cannot have a bare trait because it must be `Sized`
5342
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:13:13
5443
|
@@ -61,19 +50,8 @@ help: you can use the `impl Trait` feature in the return type because all the re
6150
LL | fn bap() -> impl Trait { Struct }
6251
| ^^^^^^^^^^
6352

64-
error[E0308]: mismatched types
65-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:16:25
66-
|
67-
LL | fn ban() -> dyn Trait { Struct }
68-
| --------- ^^^^^^ expected trait `Trait`, found struct `Struct`
69-
| |
70-
| expected `(dyn Trait + 'static)` because of return type
71-
|
72-
= note: expected trait object `(dyn Trait + 'static)`
73-
found struct `Struct`
74-
7553
error[E0746]: return type cannot have a bare trait because it must be `Sized`
76-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:16:13
54+
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:15:13
7755
|
7856
LL | fn ban() -> dyn Trait { Struct }
7957
| ^^^^^^^^^ doesn't have a size known at compile-time
@@ -85,7 +63,7 @@ LL | fn ban() -> impl Trait { Struct }
8563
| ^^^^^^^^^^
8664

8765
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
88-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:19:13
66+
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:17:13
8967
|
9068
LL | fn bak() -> dyn Trait { unimplemented!() }
9169
| ^^^^^^^^^ doesn't have a size known at compile-time
@@ -94,26 +72,14 @@ LL | fn bak() -> dyn Trait { unimplemented!() }
9472
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
9573
= note: the return type of a function must have a statically known size
9674

97-
error[E0308]: mismatched types
98-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:23:16
99-
|
100-
LL | fn bal() -> dyn Trait {
101-
| --------- expected `(dyn Trait + 'static)` because of return type
102-
LL | if true {
103-
LL | return Struct;
104-
| ^^^^^^ expected trait `Trait`, found struct `Struct`
105-
|
106-
= note: expected trait object `(dyn Trait + 'static)`
107-
found struct `Struct`
108-
10975
error[E0746]: return type cannot have a bare trait because it must be `Sized`
110-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:21:13
76+
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:19:13
11177
|
11278
LL | fn bal() -> dyn Trait {
11379
| ^^^^^^^^^ doesn't have a size known at compile-time
11480
|
11581
help: if all the returned values were of the same type you could use `impl Trait` as the return type
116-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:25:5
82+
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:23:5
11783
|
11884
LL | return Struct;
11985
| ^^^^^^
@@ -132,20 +98,8 @@ LL | }
13298
LL | Box::new(42)
13399
|
134100

135-
error[E0308]: mismatched types
136-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:25:5
137-
|
138-
LL | fn bal() -> dyn Trait {
139-
| --------- expected `(dyn Trait + 'static)` because of return type
140-
...
141-
LL | 42
142-
| ^^ expected trait `Trait`, found integer
143-
|
144-
= note: expected trait object `(dyn Trait + 'static)`
145-
found type `{integer}`
146-
147101
error[E0746]: return type cannot have a bare trait because it must be `Sized`
148-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:29:13
102+
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:27:13
149103
|
150104
LL | fn bat() -> dyn Trait {
151105
| ^^^^^^^^^ doesn't have a size known at compile-time
@@ -156,31 +110,7 @@ help: you can use the `impl Trait` feature in the return type because all the re
156110
LL | fn bat() -> impl Trait {
157111
| ^^^^^^^^^^
158112

159-
error[E0308]: mismatched types
160-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:31:16
161-
|
162-
LL | fn bat() -> dyn Trait {
163-
| --------- expected `(dyn Trait + 'static)` because of return type
164-
LL | if true {
165-
LL | return 0;
166-
| ^ expected trait `Trait`, found integer
167-
|
168-
= note: expected trait object `(dyn Trait + 'static)`
169-
found type `{integer}`
170-
171-
error[E0308]: mismatched types
172-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:33:5
173-
|
174-
LL | fn bat() -> dyn Trait {
175-
| --------- expected `(dyn Trait + 'static)` because of return type
176-
...
177-
LL | 42
178-
| ^^ expected trait `Trait`, found integer
179-
|
180-
= note: expected trait object `(dyn Trait + 'static)`
181-
found type `{integer}`
182-
183-
error: aborting due to 15 previous errors
113+
error: aborting due to 9 previous errors
184114

185115
Some errors have detailed explanations: E0277, E0308.
186116
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)