summaryrefslogtreecommitdiffstats
path: root/src/test/ui/nll/closure-requirements/escape-argument.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /src/test/ui/nll/closure-requirements/escape-argument.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/ui/nll/closure-requirements/escape-argument.rs')
-rw-r--r--src/test/ui/nll/closure-requirements/escape-argument.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/test/ui/nll/closure-requirements/escape-argument.rs b/src/test/ui/nll/closure-requirements/escape-argument.rs
new file mode 100644
index 000000000..066cd4360
--- /dev/null
+++ b/src/test/ui/nll/closure-requirements/escape-argument.rs
@@ -0,0 +1,42 @@
+// Test closure that:
+//
+// - takes an argument `y`
+// - stores `y` into another, longer-lived spot
+//
+// but is invoked with a spot that doesn't live long
+// enough to store `y`.
+//
+// The error is reported in the caller: invoking the closure links the
+// lifetime of the variable that is given as `y` (via subtyping) and
+// thus forces the corresponding borrow to live too long. This is
+// basically checking that the MIR type checker correctly enforces the
+// closure signature.
+
+// compile-flags:-Zverbose
+
+#![feature(rustc_attrs)]
+
+#[rustc_regions]
+fn test() {
+ let x = 44;
+ let mut p = &x;
+
+ {
+ let y = 22;
+ let mut closure = expect_sig(|p, y| *p = y);
+ closure(&mut p, &y);
+ //~^ ERROR `y` does not live long enough [E0597]
+ }
+
+ deref(p);
+}
+
+fn expect_sig<F>(f: F) -> F
+ where F: for<'a, 'b> FnMut(&'a mut &'b i32, &'b i32)
+{
+ f
+}
+
+fn deref(_p: &i32) { }
+
+fn main() { }