Skip to content

Commit 3db2bcf

Browse files
Remove return type sized check hack from hir typeck
1 parent 795fdf7 commit 3db2bcf

7 files changed

+9
-92
lines changed

compiler/rustc_hir_typeck/src/check.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,8 @@ pub(super) fn check_fn<'a, 'tcx>(
103103

104104
fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
105105

106-
if let ty::Dynamic(_, _, ty::Dyn) = declared_ret_ty.kind() {
107-
// FIXME: We need to verify that the return type is `Sized` after the return expression has
108-
// been evaluated so that we have types available for all the nodes being returned, but that
109-
// requires the coerced evaluated type to be stored. Moving `check_return_expr` before this
110-
// causes unsized errors caused by the `declared_ret_ty` to point at the return expression,
111-
// while keeping the current ordering we will ignore the tail expression's type because we
112-
// don't know it yet. We can't do `check_expr_kind` while keeping `check_return_expr`
113-
// because we will trigger "unreachable expression" lints unconditionally.
114-
// Because of all of this, we perform a crude check to know whether the simplest `!Sized`
115-
// case that a newcomer might make, returning a bare trait, and in that case we populate
116-
// the tail expression's type so that the suggestion will be correct, but ignore all other
117-
// possible cases.
118-
fcx.check_expr(&body.value);
119-
fcx.require_type_is_sized(declared_ret_ty, decl.output.span(), traits::SizedReturnType);
120-
} else {
121-
fcx.require_type_is_sized(declared_ret_ty, decl.output.span(), traits::SizedReturnType);
122-
fcx.check_return_expr(&body.value, false);
123-
}
106+
fcx.require_type_is_sized(declared_ret_ty, decl.output.span(), traits::SizedReturnType);
107+
fcx.check_return_expr(&body.value, false);
124108

125109
// We insert the deferred_generator_interiors entry after visiting the body.
126110
// This ensures that all nested generators appear before the entry of this generator.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn bax() -> dyn Trait { //~ ERROR E0746
2626
if true {
2727
Struct
2828
} else {
29-
42 //~ ERROR `if` and `else` have incompatible types
29+
42
3030
}
3131
}
3232
fn bam() -> Box<dyn Trait> {

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

+1-13
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,6 @@ LL | }
100100
LL ~ Box::new(42)
101101
|
102102

103-
error[E0308]: `if` and `else` have incompatible types
104-
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:29:9
105-
|
106-
LL | / if true {
107-
LL | | Struct
108-
| | ------ expected because of this
109-
LL | | } else {
110-
LL | | 42
111-
| | ^^ expected `Struct`, found integer
112-
LL | | }
113-
| |_____- `if` and `else` have incompatible types
114-
115103
error[E0746]: return type cannot have an unboxed trait object
116104
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:25:13
117105
|
@@ -305,7 +293,7 @@ LL | } else {
305293
LL ~ Box::new(42)
306294
|
307295

308-
error: aborting due to 20 previous errors
296+
error: aborting due to 19 previous errors
309297

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn hat() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed
7777
fn pug() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed trait object
7878
match 13 {
7979
0 => 0i32,
80-
1 => 1u32, //~ ERROR `match` arms have incompatible types
80+
1 => 1u32,
8181
_ => 2u32,
8282
}
8383
}
@@ -86,7 +86,7 @@ fn man() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed
8686
if false {
8787
0i32
8888
} else {
89-
1u32 //~ ERROR `if` and `else` have incompatible types
89+
1u32
9090
}
9191
}
9292

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

+1-35
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,6 @@ LL | _ => {
186186
LL ~ Box::new(1u32)
187187
|
188188

189-
error[E0308]: `match` arms have incompatible types
190-
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:80:14
191-
|
192-
LL | / match 13 {
193-
LL | | 0 => 0i32,
194-
| | ---- this is found to be of type `i32`
195-
LL | | 1 => 1u32,
196-
| | ^^^^ expected `i32`, found `u32`
197-
LL | | _ => 2u32,
198-
LL | | }
199-
| |_____- `match` arms have incompatible types
200-
|
201-
help: change the type of the numeric literal from `u32` to `i32`
202-
|
203-
LL | 1 => 1i32,
204-
| ~~~
205-
206189
error[E0746]: return type cannot have an unboxed trait object
207190
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:77:13
208191
|
@@ -222,23 +205,6 @@ LL ~ 1 => Box::new(1u32),
222205
LL ~ _ => Box::new(2u32),
223206
|
224207

225-
error[E0308]: `if` and `else` have incompatible types
226-
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:89:9
227-
|
228-
LL | / if false {
229-
LL | | 0i32
230-
| | ---- expected because of this
231-
LL | | } else {
232-
LL | | 1u32
233-
| | ^^^^ expected `i32`, found `u32`
234-
LL | | }
235-
| |_____- `if` and `else` have incompatible types
236-
|
237-
help: change the type of the numeric literal from `u32` to `i32`
238-
|
239-
LL | 1i32
240-
| ~~~
241-
242208
error[E0746]: return type cannot have an unboxed trait object
243209
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:85:13
244210
|
@@ -258,7 +224,7 @@ LL | } else {
258224
LL ~ Box::new(1u32)
259225
|
260226

261-
error: aborting due to 14 previous errors
227+
error: aborting due to 12 previous errors
262228

263229
Some errors have detailed explanations: E0308, E0746.
264230
For more information about an error, try `rustc --explain E0308`.

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

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> dyn Fn() + 'a {
88
move || println!("{a}")
99
} else {
1010
Box::new(move || println!("{}", b))
11-
//~^ ERROR `if` and `else` have incompatible types
1211
}
1312
}
1413

+2-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
error[E0308]: `if` and `else` have incompatible types
2-
--> $DIR/box-instead-of-dyn-fn.rs:10:9
3-
|
4-
LL | / if a % 2 == 0 {
5-
LL | | move || println!("{a}")
6-
| | -----------------------
7-
| | |
8-
| | the expected closure
9-
| | expected because of this
10-
LL | | } else {
11-
LL | | Box::new(move || println!("{}", b))
12-
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected closure, found `Box<[[email protected]:10:18]>`
13-
LL | |
14-
LL | | }
15-
| |_____- `if` and `else` have incompatible types
16-
|
17-
= note: expected closure `[closure@$DIR/box-instead-of-dyn-fn.rs:8:9: 8:16]`
18-
found struct `Box<[closure@$DIR/box-instead-of-dyn-fn.rs:10:18: 10:25]>`
19-
201
error[E0746]: return type cannot have an unboxed trait object
212
--> $DIR/box-instead-of-dyn-fn.rs:5:56
223
|
@@ -35,7 +16,6 @@ LL | if a % 2 == 0 {
3516
LL ~ Box::new(move || println!("{a}"))
3617
|
3718

38-
error: aborting due to 2 previous errors
19+
error: aborting due to previous error
3920

40-
Some errors have detailed explanations: E0308, E0746.
41-
For more information about an error, try `rustc --explain E0308`.
21+
For more information about this error, try `rustc --explain E0746`.

0 commit comments

Comments
 (0)