about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_codegen_ssa/src/mir/mod.rs7
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs20
-rw-r--r--compiler/rustc_middle/src/traits/mod.rs4
-rw-r--r--compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs6
-rw-r--r--tests/ui/unsized-locals/unsized-exprs-rpass.rs5
-rw-r--r--tests/ui/unsized-locals/unsized-exprs.stderr12
-rw-r--r--tests/ui/unsized-locals/unsized-non-place-exprs.rs27
-rw-r--r--tests/ui/unsized-locals/unsized-non-place-exprs.stderr81
8 files changed, 153 insertions, 9 deletions
diff --git a/compiler/rustc_codegen_ssa/src/mir/mod.rs b/compiler/rustc_codegen_ssa/src/mir/mod.rs
index 4e35b143173..fa69820d5d2 100644
--- a/compiler/rustc_codegen_ssa/src/mir/mod.rs
+++ b/compiler/rustc_codegen_ssa/src/mir/mod.rs
@@ -141,7 +141,12 @@ enum LocalRef<'tcx, V> {
     /// `UnsizedPlace(p)`: `p` itself is a thin pointer (indirect place).
     /// `*p` is the wide pointer that references the actual unsized place.
     ///
-    /// Rust has no alloca and thus no ability to move the unsized place.
+    /// MIR only supports unsized args, not dynamically-sized locals, so
+    /// new unsized temps don't exist and we must reuse the referred-to place.
+    ///
+    /// FIXME: Since the removal of unsized locals in <https://github.com/rust-lang/rust/pull/142911>,
+    /// can we maybe use `Place` here? Or refactor it in another way? There are quite a few
+    /// `UnsizedPlace => bug` branches now.
     UnsizedPlace(PlaceRef<'tcx, V>),
     /// The backend [`OperandValue`] has already been generated.
     Operand(OperandRef<'tcx, V>),
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
index eeb8d33ef65..eb8d671c939 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
@@ -241,6 +241,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 arg_expr.span,
                 ObligationCauseCode::WellFormed(None),
             );
+
+            self.check_place_expr_if_unsized(fn_input_ty, arg_expr);
         }
 
         // First, let's unify the formal method signature with the expectation eagerly.
@@ -543,6 +545,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         }
     }
 
+    /// If `unsized_fn_params` is active, check that unsized values are place expressions. Since
+    /// the removal of `unsized_locals` in <https://github.com/rust-lang/rust/pull/142911> we can't
+    /// store them in MIR locals as temporaries.
+    ///
+    /// If `unsized_fn_params` is inactive, this will be checked in borrowck instead.
+    fn check_place_expr_if_unsized(&self, ty: Ty<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
+        if self.tcx.features().unsized_fn_params() && !expr.is_syntactic_place_expr() {
+            self.require_type_is_sized(
+                ty,
+                expr.span,
+                ObligationCauseCode::UnsizedNonPlaceExpr(expr.span),
+            );
+        }
+    }
+
     fn report_arg_errors(
         &self,
         compatibility_diagonal: IndexVec<ProvidedIdx, Compatibility<'tcx>>,
@@ -1873,7 +1890,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 });
             }
             hir::StmtKind::Semi(expr) => {
-                self.check_expr(expr);
+                let ty = self.check_expr(expr);
+                self.check_place_expr_if_unsized(ty, expr);
             }
         }
 
diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs
index 1a5a9765ce7..5bdde3a514e 100644
--- a/compiler/rustc_middle/src/traits/mod.rs
+++ b/compiler/rustc_middle/src/traits/mod.rs
@@ -412,6 +412,10 @@ pub enum ObligationCauseCode<'tcx> {
 
     /// Obligations emitted during the normalization of a free type alias.
     TypeAlias(ObligationCauseCodeHandle<'tcx>, Span, DefId),
+
+    /// Only reachable if the `unsized_fn_params` feature is used. Unsized function arguments must
+    /// be place expressions because we can't store them in MIR locals as temporaries.
+    UnsizedNonPlaceExpr(Span),
 }
 
 /// Whether a value can be extracted into a const.
diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
index 362052e9fdb..c5b85c95ba2 100644
--- a/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
+++ b/compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs
@@ -3624,6 +3624,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
                 );
                 suggest_remove_deref(err, &expr);
             }
+            ObligationCauseCode::UnsizedNonPlaceExpr(span) => {
+                err.span_note(
+                    span,
+                    "unsized values must be place expressions and cannot be put in temporaries",
+                );
+            }
         }
     }
 
diff --git a/tests/ui/unsized-locals/unsized-exprs-rpass.rs b/tests/ui/unsized-locals/unsized-exprs-rpass.rs
index 54ecd000343..ce31bd63f7c 100644
--- a/tests/ui/unsized-locals/unsized-exprs-rpass.rs
+++ b/tests/ui/unsized-locals/unsized-exprs-rpass.rs
@@ -18,11 +18,6 @@ impl std::ops::Add<i32> for A<[u8]> {
 }
 
 fn main() {
-    udrop::<[u8]>(loop {
-        break *foo();
-    });
-    udrop::<[u8]>(if true { *foo() } else { *foo() });
-    udrop::<[u8]>({ *foo() });
     udrop::<[u8]>((*foo()));
     *afoo() + 42;
     udrop as fn([u8]);
diff --git a/tests/ui/unsized-locals/unsized-exprs.stderr b/tests/ui/unsized-locals/unsized-exprs.stderr
index 1b61254870f..0455edbe8e7 100644
--- a/tests/ui/unsized-locals/unsized-exprs.stderr
+++ b/tests/ui/unsized-locals/unsized-exprs.stderr
@@ -10,7 +10,11 @@ note: required because it appears within the type `A<[u8]>`
    |
 LL | struct A<X: ?Sized>(X);
    |        ^
-   = note: structs must have a statically known size to be initialized
+note: unsized values must be place expressions and cannot be put in temporaries
+  --> $DIR/unsized-exprs.rs:19:22
+   |
+LL |     udrop::<A<[u8]>>(A { 0: *foo() });
+   |                      ^^^^^^^^^^^^^^^
 
 error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
   --> $DIR/unsized-exprs.rs:21:22
@@ -24,7 +28,11 @@ note: required because it appears within the type `A<[u8]>`
    |
 LL | struct A<X: ?Sized>(X);
    |        ^
-   = note: the return type of a function must have a statically known size
+note: unsized values must be place expressions and cannot be put in temporaries
+  --> $DIR/unsized-exprs.rs:21:22
+   |
+LL |     udrop::<A<[u8]>>(A(*foo()));
+   |                      ^^^^^^^^^
 
 error: aborting due to 2 previous errors
 
diff --git a/tests/ui/unsized-locals/unsized-non-place-exprs.rs b/tests/ui/unsized-locals/unsized-non-place-exprs.rs
new file mode 100644
index 00000000000..d724fcf81a4
--- /dev/null
+++ b/tests/ui/unsized-locals/unsized-non-place-exprs.rs
@@ -0,0 +1,27 @@
+//! `#![feature(unsized_fn_params)]` lets you use unsized function parameters. In particular this
+//! is load bearing for `Box<dyn FnOnce()>: FnOnce()`. To do that, borrowck relaxes the requirement
+//! that certain places must be `Sized`. But in #142911 we removed alloca support, so these
+//! arguments cannot be put in temporaries (or ICE at codegen) That means when `unsized_fn_params`
+//! is enabled, we must explicitly check that unsized function arguments are place expressions.
+//!
+//! Also see tests/ui/unsized_locals/unsized-exprs-rpass.rs
+
+#![feature(unsized_fn_params)]
+
+fn foo() -> Box<[u8]> {
+    Box::new(*b"foo")
+}
+
+fn udrop<T: ?Sized>(_x: T) {}
+
+fn main(){
+    // NB The ordering of the following operations matters, otherwise errors get swallowed somehow.
+
+    udrop::<[u8]>(if true { *foo() } else { *foo() }); //~ERROR the size for values of type `[u8]` cannot be known at compilation time
+    udrop::<[u8]>({ *foo() }); //~ERROR the size for values of type `[u8]` cannot be known at compilation time
+    udrop(match foo() { x => *x }); //~ERROR the size for values of type `[u8]` cannot be known at compilation time
+    udrop::<[u8]>({ loop { break *foo(); } }); //~ERROR the size for values of type `[u8]` cannot be known at compilation time
+
+    { *foo() }; //~ERROR the size for values of type `[u8]` cannot be known at compilation time
+    { loop { break *foo(); } }; //~ERROR the size for values of type `[u8]` cannot be known at compilation time
+}
diff --git a/tests/ui/unsized-locals/unsized-non-place-exprs.stderr b/tests/ui/unsized-locals/unsized-non-place-exprs.stderr
new file mode 100644
index 00000000000..f9507e9a888
--- /dev/null
+++ b/tests/ui/unsized-locals/unsized-non-place-exprs.stderr
@@ -0,0 +1,81 @@
+error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
+  --> $DIR/unsized-non-place-exprs.rs:20:19
+   |
+LL |     udrop::<[u8]>(if true { *foo() } else { *foo() });
+   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `[u8]`
+note: unsized values must be place expressions and cannot be put in temporaries
+  --> $DIR/unsized-non-place-exprs.rs:20:19
+   |
+LL |     udrop::<[u8]>(if true { *foo() } else { *foo() });
+   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
+  --> $DIR/unsized-non-place-exprs.rs:21:19
+   |
+LL |     udrop::<[u8]>({ *foo() });
+   |                   ^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `[u8]`
+note: unsized values must be place expressions and cannot be put in temporaries
+  --> $DIR/unsized-non-place-exprs.rs:21:19
+   |
+LL |     udrop::<[u8]>({ *foo() });
+   |                   ^^^^^^^^^^
+
+error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
+  --> $DIR/unsized-non-place-exprs.rs:22:11
+   |
+LL |     udrop(match foo() { x => *x });
+   |           ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `[u8]`
+note: unsized values must be place expressions and cannot be put in temporaries
+  --> $DIR/unsized-non-place-exprs.rs:22:11
+   |
+LL |     udrop(match foo() { x => *x });
+   |           ^^^^^^^^^^^^^^^^^^^^^^^
+
+error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
+  --> $DIR/unsized-non-place-exprs.rs:23:19
+   |
+LL |     udrop::<[u8]>({ loop { break *foo(); } });
+   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `[u8]`
+note: unsized values must be place expressions and cannot be put in temporaries
+  --> $DIR/unsized-non-place-exprs.rs:23:19
+   |
+LL |     udrop::<[u8]>({ loop { break *foo(); } });
+   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
+  --> $DIR/unsized-non-place-exprs.rs:25:5
+   |
+LL |     { *foo() };
+   |     ^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `[u8]`
+note: unsized values must be place expressions and cannot be put in temporaries
+  --> $DIR/unsized-non-place-exprs.rs:25:5
+   |
+LL |     { *foo() };
+   |     ^^^^^^^^^^
+
+error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
+  --> $DIR/unsized-non-place-exprs.rs:26:5
+   |
+LL |     { loop { break *foo(); } };
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `[u8]`
+note: unsized values must be place expressions and cannot be put in temporaries
+  --> $DIR/unsized-non-place-exprs.rs:26:5
+   |
+LL |     { loop { break *foo(); } };
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 6 previous errors
+
+For more information about this error, try `rustc --explain E0277`.