Skip to content

Commit

Permalink
Remove return type sized check hack from hir typeck
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed May 18, 2023
1 parent 795fdf7 commit 3db2bcf
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 92 deletions.
20 changes: 2 additions & 18 deletions compiler/rustc_hir_typeck/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,24 +103,8 @@ pub(super) fn check_fn<'a, 'tcx>(

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

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

// We insert the deferred_generator_interiors entry after visiting the body.
// This ensures that all nested generators appear before the entry of this generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn bax() -> dyn Trait { //~ ERROR E0746
if true {
Struct
} else {
42 //~ ERROR `if` and `else` have incompatible types
42
}
}
fn bam() -> Box<dyn Trait> {
Expand Down
14 changes: 1 addition & 13 deletions tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,6 @@ LL | }
LL ~ Box::new(42)
|

error[E0308]: `if` and `else` have incompatible types
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:29:9
|
LL | / if true {
LL | | Struct
| | ------ expected because of this
LL | | } else {
LL | | 42
| | ^^ expected `Struct`, found integer
LL | | }
| |_____- `if` and `else` have incompatible types

error[E0746]: return type cannot have an unboxed trait object
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:25:13
|
Expand Down Expand Up @@ -305,7 +293,7 @@ LL | } else {
LL ~ Box::new(42)
|

error: aborting due to 20 previous errors
error: aborting due to 19 previous errors

Some errors have detailed explanations: E0277, E0308, E0746.
For more information about an error, try `rustc --explain E0277`.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn hat() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed
fn pug() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed trait object
match 13 {
0 => 0i32,
1 => 1u32, //~ ERROR `match` arms have incompatible types
1 => 1u32,
_ => 2u32,
}
}
Expand All @@ -86,7 +86,7 @@ fn man() -> dyn std::fmt::Display { //~ ERROR return type cannot have an unboxed
if false {
0i32
} else {
1u32 //~ ERROR `if` and `else` have incompatible types
1u32
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,6 @@ LL | _ => {
LL ~ Box::new(1u32)
|

error[E0308]: `match` arms have incompatible types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:80:14
|
LL | / match 13 {
LL | | 0 => 0i32,
| | ---- this is found to be of type `i32`
LL | | 1 => 1u32,
| | ^^^^ expected `i32`, found `u32`
LL | | _ => 2u32,
LL | | }
| |_____- `match` arms have incompatible types
|
help: change the type of the numeric literal from `u32` to `i32`
|
LL | 1 => 1i32,
| ~~~

error[E0746]: return type cannot have an unboxed trait object
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:77:13
|
Expand All @@ -222,23 +205,6 @@ LL ~ 1 => Box::new(1u32),
LL ~ _ => Box::new(2u32),
|

error[E0308]: `if` and `else` have incompatible types
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:89:9
|
LL | / if false {
LL | | 0i32
| | ---- expected because of this
LL | | } else {
LL | | 1u32
| | ^^^^ expected `i32`, found `u32`
LL | | }
| |_____- `if` and `else` have incompatible types
|
help: change the type of the numeric literal from `u32` to `i32`
|
LL | 1i32
| ~~~

error[E0746]: return type cannot have an unboxed trait object
--> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:85:13
|
Expand All @@ -258,7 +224,7 @@ LL | } else {
LL ~ Box::new(1u32)
|

error: aborting due to 14 previous errors
error: aborting due to 12 previous errors

Some errors have detailed explanations: E0308, E0746.
For more information about an error, try `rustc --explain E0308`.
1 change: 0 additions & 1 deletion tests/ui/unsized/box-instead-of-dyn-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ fn print_on_or_the_other<'a>(a: i32, b: &'a String) -> dyn Fn() + 'a {
move || println!("{a}")
} else {
Box::new(move || println!("{}", b))
//~^ ERROR `if` and `else` have incompatible types
}
}

Expand Down
24 changes: 2 additions & 22 deletions tests/ui/unsized/box-instead-of-dyn-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,3 @@
error[E0308]: `if` and `else` have incompatible types
--> $DIR/box-instead-of-dyn-fn.rs:10:9
|
LL | / if a % 2 == 0 {
LL | | move || println!("{a}")
| | -----------------------
| | |
| | the expected closure
| | expected because of this
LL | | } else {
LL | | Box::new(move || println!("{}", b))
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected closure, found `Box<[closure@box-instead-of-dyn-fn.rs:10:18]>`
LL | |
LL | | }
| |_____- `if` and `else` have incompatible types
|
= note: expected closure `[closure@$DIR/box-instead-of-dyn-fn.rs:8:9: 8:16]`
found struct `Box<[closure@$DIR/box-instead-of-dyn-fn.rs:10:18: 10:25]>`

error[E0746]: return type cannot have an unboxed trait object
--> $DIR/box-instead-of-dyn-fn.rs:5:56
|
Expand All @@ -35,7 +16,6 @@ LL | if a % 2 == 0 {
LL ~ Box::new(move || println!("{a}"))
|

error: aborting due to 2 previous errors
error: aborting due to previous error

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

0 comments on commit 3db2bcf

Please sign in to comment.