diff options
author | kyren <kerriganw@gmail.com> | 2018-02-19 17:40:48 -0500 |
---|---|---|
committer | kyren <kerriganw@gmail.com> | 2018-02-19 17:40:48 -0500 |
commit | ace5cb44f07b70f80eac5171930599ba499b2009 (patch) | |
tree | cb7f08967e120ba5e015d6e7c881ff6e84d5b6b4 /tests/compile-fail/scope_escape.rs | |
parent | 0450c9b59754c7c1feac7d4793262898b88709f4 (diff) | |
download | mlua-ace5cb44f07b70f80eac5171930599ba499b2009.zip |
Letting scope handles escape the scope was unsafe
This simplifies the Scope lifetimes, and should make it a compile error for
scope created handles to exit the scope. This should be strictly better, as you
would never WANT to do this, but I hope that I have not caused a subtle lifetime
problem that would prevent passing those created handles back into Lua. I've
tested every situation I can think of, and it doesn't appear to be an issue, but
I admit that I don't fully understand everything involved and I could be missing
something.
The reason that I needed to do this is that if you can let a scope handle escape
the scope, you have a LuaRef with an unused registry id, and that can lead to
UB. Since not letting the scope references escape is a strict improvement
ANYWAY (if I haven't caused a lifetime issue), this is the easiest fix.
This is technically a breaking change but I think in most cases if you notice it
you would be invoking UB, or you had a function that accepted a Scope or
something. I don't know if it's worth a version bump?
Diffstat (limited to 'tests/compile-fail/scope_escape.rs')
-rw-r--r-- | tests/compile-fail/scope_escape.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/tests/compile-fail/scope_escape.rs b/tests/compile-fail/scope_escape.rs new file mode 100644 index 0000000..18be7db --- /dev/null +++ b/tests/compile-fail/scope_escape.rs @@ -0,0 +1,17 @@ +extern crate rlua; + +use rlua::*; + +fn main() { + struct Test { + field: i32, + } + + let lua = Lua::new(); + let mut outer: Option<Function> = None; + lua.scope(|scope| { + let f = scope.create_function_mut(|_, ()| Ok(())).unwrap(); + outer = Some(scope.create_function_mut(|_, ()| Ok(())).unwrap()); + //~^ error: borrowed data cannot be stored outside of its closure + }); +} |