Skip to content

Use compiletest to make sure unsafe things do not compile #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
language: rust
rust:
- stable
- beta
- nightly
env:
- RUSTFLAGS="-D warnings"
matrix:
include:
- rust: stable
script: cargo test --lib -v
- rust: beta
script: cargo test --lib -v
- rust: nightly
allow_failures:
- rust: nightly
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ gcc = { version = "0.3.52", optional = true }

[dev-dependencies]
rustyline = "1.0.0"
compiletest_rs = "0.3.0"
25 changes: 0 additions & 25 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,28 +982,3 @@ fn test_metatable() {
_ => panic!(),
};
}

// Need to use compiletest-rs or similar to make sure these don't compile.
/*
#[test]
fn should_not_compile() {
let lua = Lua::new();
let globals = lua.globals();

// Should not allow userdata borrow to outlive lifetime of AnyUserData handle
struct MyUserData;
impl UserData for MyUserData {};
let userdata_ref;
{
let touter = globals.get::<_, Table>("touter").unwrap();
touter.set("userdata", lua.create_userdata(MyUserData)).unwrap();
let userdata = touter.get::<_, AnyUserData>("userdata").unwrap();
userdata_ref = userdata.borrow::<MyUserData>();
}

// Should not allow self borrow of lua, it can change addresses
globals.set("boom", lua.create_function(|_, _| {
lua.eval::<i32>("1 + 1", None)
})).unwrap();
}
*/
18 changes: 18 additions & 0 deletions tests/compile-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extern crate compiletest_rs as compiletest;

use std::path::PathBuf;

fn run_mode(mode: &'static str) {
let mut config = compiletest::Config::default();

config.mode = mode.parse().expect("Invalid mode");
config.src_base = PathBuf::from(format!("tests/{}", mode));
config.target_rustcflags = Some("-L target/debug -L target/debug/deps".to_string());

compiletest::run_tests(&config);
}

#[test]
fn compile_test() {
run_mode("compile-fail");
}
13 changes: 13 additions & 0 deletions tests/compile-fail/self-borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate rlua;

use rlua::*;

fn main() {
let lua = Lua::new();

// Should not allow self borrow of lua, it can change addresses
let func = lua.create_function(|_, ()| -> Result<i32> {
//~^ error: closure may outlive the current function, but it borrows `lua`, which is owned by the current function
lua.eval::<i32>("1 + 1", None)
});
}
20 changes: 20 additions & 0 deletions tests/compile-fail/userdata-borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extern crate rlua;

use rlua::*;

fn main() {
let lua = Lua::new();
let globals = lua.globals();

// Should not allow userdata borrow to outlive lifetime of AnyUserData handle
struct MyUserData;
impl UserData for MyUserData {};
let userdata_ref;
{
let touter = globals.get::<_, Table>("touter").unwrap();
touter.set("userdata", lua.create_userdata(MyUserData)).unwrap();
let userdata = touter.get::<_, AnyUserData>("userdata").unwrap();
userdata_ref = userdata.borrow::<MyUserData>();
}
//~^ error: `userdata` does not live long enough
}