about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2022-05-30 15:57:29 -0700
committerGitHub <noreply@github.com>2022-05-30 15:57:29 -0700
commit6718723c96e7ad21e0232af9be941e5a5b9a542d (patch)
tree3f57ddcc348798f7d81e551f4337812782b5e0d1
parent12ba87b9964314535e68a1ae9a5616acac4a438a (diff)
parent126ef8ee318d5562c92b351868f77b1b9db0b830 (diff)
downloadrust-6718723c96e7ad21e0232af9be941e5a5b9a542d.tar.gz
rust-6718723c96e7ad21e0232af9be941e5a5b9a542d.zip
Rollup merge of #97431 - compiler-errors:issue-97413, r=oli-obk
don't do `Sized` and other return type checks on RPIT's real type

Fixes an ICE where we're doing `Sized` check against the RPIT's real type, instead of against the opaque type. This differs from what we're doing in MIR typeck, which causes ICE #97226.

This regressed in #96516 -- this adjusts that fix to be a bit more conservative. That PR was backported and thus the ICE is also present in stable. Not sure if it's worth to beta and/or stable backport, probably not the latter but I could believe the former.

r? `@oli-obk`

cc: another attempt to fix this ICE #97413. I believe this PR addresses the root cause.
-rw-r--r--compiler/rustc_typeck/src/check/check.rs13
-rw-r--r--src/test/ui/impl-trait/rpit-not-sized.rs6
-rw-r--r--src/test/ui/impl-trait/rpit-not-sized.stderr12
3 files changed, 24 insertions, 7 deletions
diff --git a/compiler/rustc_typeck/src/check/check.rs b/compiler/rustc_typeck/src/check/check.rs
index 09854336599..7499e5efdee 100644
--- a/compiler/rustc_typeck/src/check/check.rs
+++ b/compiler/rustc_typeck/src/check/check.rs
@@ -105,12 +105,6 @@ pub(super) fn check_fn<'a, 'tcx>(
             DUMMY_SP,
             param_env,
         ));
-    // HACK(oli-obk): we rewrite the declared return type, too, so that we don't end up inferring all
-    // unconstrained RPIT to have `()` as their hidden type. This would happen because further down we
-    // compare the ret_coercion with declared_ret_ty, and anything uninferred would be inferred to the
-    // opaque type itself. That again would cause writeback to assume we have a recursive call site
-    // and do the sadly stabilized fallback to `()`.
-    let declared_ret_ty = ret_ty;
     fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
     fcx.ret_type_span = Some(decl.output.span());
 
@@ -254,7 +248,12 @@ pub(super) fn check_fn<'a, 'tcx>(
             fcx.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::DynReturnFn, span });
         debug!("actual_return_ty replaced with {:?}", actual_return_ty);
     }
-    fcx.demand_suptype(span, declared_ret_ty, actual_return_ty);
+
+    // HACK(oli-obk, compiler-errors): We should be comparing this against
+    // `declared_ret_ty`, but then anything uninferred would be inferred to
+    // the opaque type itself. That again would cause writeback to assume
+    // we have a recursive call site and do the sadly stabilized fallback to `()`.
+    fcx.demand_suptype(span, ret_ty, actual_return_ty);
 
     // Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !`
     if let Some(panic_impl_did) = tcx.lang_items().panic_impl()
diff --git a/src/test/ui/impl-trait/rpit-not-sized.rs b/src/test/ui/impl-trait/rpit-not-sized.rs
new file mode 100644
index 00000000000..bd25940780a
--- /dev/null
+++ b/src/test/ui/impl-trait/rpit-not-sized.rs
@@ -0,0 +1,6 @@
+fn foo() -> impl ?Sized {
+    //~^ ERROR the size for values of type `impl ?Sized` cannot be known at compilation time
+    ()
+}
+
+fn main() {}
diff --git a/src/test/ui/impl-trait/rpit-not-sized.stderr b/src/test/ui/impl-trait/rpit-not-sized.stderr
new file mode 100644
index 00000000000..608c94fc072
--- /dev/null
+++ b/src/test/ui/impl-trait/rpit-not-sized.stderr
@@ -0,0 +1,12 @@
+error[E0277]: the size for values of type `impl ?Sized` cannot be known at compilation time
+  --> $DIR/rpit-not-sized.rs:1:13
+   |
+LL | fn foo() -> impl ?Sized {
+   |             ^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `impl ?Sized`
+   = note: the return type of a function must have a statically known size
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.