From 6effe431bbdef0580b6552a3246f7ec6b3751a2c Mon Sep 17 00:00:00 2001 From: Casper Date: Tue, 26 Jan 2021 11:09:29 -0500 Subject: [PATCH] Rust: remove inner attributes (#6410) * remove inner attributes * Added test for outdir in Rust * add bin/outdir * Moved outdir test to its own package and only run it if flatc is available Co-authored-by: Casper Neo --- samples/monster_generated.rs | 1 - samples/sample_binary.rs | 1 + src/idl_gen_rust.cpp | 4 ++- tests/RustTest.sh | 8 +++++ tests/include_test/include_test1_generated.rs | 1 - tests/include_test/sub/include_test2_generated.rs | 1 - tests/monster_test_generated.rs | 1 - tests/namespace_test/namespace_test1_generated.rs | 1 - tests/namespace_test/namespace_test2_generated.rs | 1 - tests/optional_scalars_generated.rs | 1 - tests/rust_usage_test/Cargo.toml | 1 - tests/rust_usage_test/outdir/Cargo.toml | 10 ++++++ tests/rust_usage_test/outdir/build.rs | 39 +++++++++++++++++++++++ tests/rust_usage_test/outdir/src/main.rs | 38 ++++++++++++++++++++++ tests/rust_usage_test/tests/integration_test.rs | 5 +++ 15 files changed, 104 insertions(+), 9 deletions(-) create mode 100644 tests/rust_usage_test/outdir/Cargo.toml create mode 100644 tests/rust_usage_test/outdir/build.rs create mode 100644 tests/rust_usage_test/outdir/src/main.rs diff --git a/samples/monster_generated.rs b/samples/monster_generated.rs index 13b2cf1..cc2051e 100644 --- a/samples/monster_generated.rs +++ b/samples/monster_generated.rs @@ -1,7 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify -#![allow(unused_imports, dead_code)] use std::mem; use std::cmp::Ordering; diff --git a/samples/sample_binary.rs b/samples/sample_binary.rs index 1b08f20..67f05e6 100644 --- a/samples/sample_binary.rs +++ b/samples/sample_binary.rs @@ -18,6 +18,7 @@ extern crate flatbuffers; // import the generated code +#[allow(dead_code, unused_imports)] #[path = "./monster_generated.rs"] #[allow(clippy::approx_constant)] // We use low precision PI as a default value. mod monster_generated; diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 1677ce8..424112c 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -2497,7 +2497,9 @@ class RustGenerator : public BaseGenerator { } void GenNamespaceImports(const int white_spaces) { - if (white_spaces == 0) { code_ += "#![allow(unused_imports, dead_code)]"; } + // DO not use global attributes (i.e. #![...]) since it interferes + // with users who include! generated files. + // See: https://github.com/google/flatbuffers/issues/6261 std::string indent = std::string(white_spaces, ' '); code_ += ""; if (!parser_.opts.generate_all) { diff --git a/tests/RustTest.sh b/tests/RustTest.sh index 7fa6492..71258c9 100755 --- a/tests/RustTest.sh +++ b/tests/RustTest.sh @@ -48,6 +48,14 @@ check_test_result "No Cargo clippy lints test" cargo bench $TARGET_FLAG +# This test is dependent on flatc. +if [[ -f ../../flatc ]]; then + cd outdir + cargo test + check_test_result "Rust generated file in \$OUT_DIR" + cd .. +fi + # RUST_NIGHTLY environment variable set in dockerfile. if [[ $RUST_NIGHTLY == 1 ]]; then rustup +nightly component add miri diff --git a/tests/include_test/include_test1_generated.rs b/tests/include_test/include_test1_generated.rs index 4015a3c..d7511f9 100644 --- a/tests/include_test/include_test1_generated.rs +++ b/tests/include_test/include_test1_generated.rs @@ -1,7 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify -#![allow(unused_imports, dead_code)] use crate::include_test2_generated::*; use std::mem; diff --git a/tests/include_test/sub/include_test2_generated.rs b/tests/include_test/sub/include_test2_generated.rs index 9b3c7ee..787ec19 100644 --- a/tests/include_test/sub/include_test2_generated.rs +++ b/tests/include_test/sub/include_test2_generated.rs @@ -1,7 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify -#![allow(unused_imports, dead_code)] use crate::include_test1_generated::*; use std::mem; diff --git a/tests/monster_test_generated.rs b/tests/monster_test_generated.rs index bae1066..a514c7a 100644 --- a/tests/monster_test_generated.rs +++ b/tests/monster_test_generated.rs @@ -1,7 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify -#![allow(unused_imports, dead_code)] use crate::include_test1_generated::*; use crate::include_test2_generated::*; diff --git a/tests/namespace_test/namespace_test1_generated.rs b/tests/namespace_test/namespace_test1_generated.rs index 88c7cb2..aa4dde6 100644 --- a/tests/namespace_test/namespace_test1_generated.rs +++ b/tests/namespace_test/namespace_test1_generated.rs @@ -1,7 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify -#![allow(unused_imports, dead_code)] use std::mem; use std::cmp::Ordering; diff --git a/tests/namespace_test/namespace_test2_generated.rs b/tests/namespace_test/namespace_test2_generated.rs index f86e424..e0ecd72 100644 --- a/tests/namespace_test/namespace_test2_generated.rs +++ b/tests/namespace_test/namespace_test2_generated.rs @@ -1,7 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify -#![allow(unused_imports, dead_code)] use std::mem; use std::cmp::Ordering; diff --git a/tests/optional_scalars_generated.rs b/tests/optional_scalars_generated.rs index 73ebee3..81483b5 100644 --- a/tests/optional_scalars_generated.rs +++ b/tests/optional_scalars_generated.rs @@ -1,7 +1,6 @@ // automatically generated by the FlatBuffers compiler, do not modify -#![allow(unused_imports, dead_code)] use std::mem; use std::cmp::Ordering; diff --git a/tests/rust_usage_test/Cargo.toml b/tests/rust_usage_test/Cargo.toml index 664396d..45ecca6 100644 --- a/tests/rust_usage_test/Cargo.toml +++ b/tests/rust_usage_test/Cargo.toml @@ -36,7 +36,6 @@ path = "../../samples/sample_flexbuffers_serde.rs" name = "sample_flatbuffers" path = "../../samples/sample_binary.rs" - [dev-dependencies] quickcheck = "0.6" # TODO(rw): look into moving to criterion.rs diff --git a/tests/rust_usage_test/outdir/Cargo.toml b/tests/rust_usage_test/outdir/Cargo.toml new file mode 100644 index 0000000..8387d17 --- /dev/null +++ b/tests/rust_usage_test/outdir/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "outdir" +version = "0.1.0" +authors = ["Casper Neo "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +flatbuffers = { path = "../../../rust/flatbuffers" } diff --git a/tests/rust_usage_test/outdir/build.rs b/tests/rust_usage_test/outdir/build.rs new file mode 100644 index 0000000..663ca99 --- /dev/null +++ b/tests/rust_usage_test/outdir/build.rs @@ -0,0 +1,39 @@ +fn main() { + use std::process::Command; + + let project_root = std::env::current_dir() + .unwrap() + .parent() // flatbuffers/tests/rust_usage test + .unwrap() + .parent() // flatbuffers/tests + .unwrap() + .parent() // flatbuffers/ + .unwrap() + .to_path_buf(); + + let sample_schema = { + let mut s = project_root.to_path_buf(); + s.push("samples"); + s.push("monster.fbs"); + s + }; + + let flatc = { + let mut f = project_root.to_path_buf(); + f.push("flatc"); + f + }; + + let out_dir = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).to_path_buf(); + + Command::new(&flatc) + .arg("--rust") + .arg(&sample_schema) + .arg("--filename-suffix") + .arg("_gen") + .output() + .expect("Failed to generate file"); + + let genfile = "monster_gen.rs"; + std::fs::rename(&genfile, out_dir.join("monster_generated.rs")).unwrap(); +} diff --git a/tests/rust_usage_test/outdir/src/main.rs b/tests/rust_usage_test/outdir/src/main.rs new file mode 100644 index 0000000..b5fcaf8 --- /dev/null +++ b/tests/rust_usage_test/outdir/src/main.rs @@ -0,0 +1,38 @@ +// In this example, a build.rs file generates the code and then copies it into $OUT_DIR. +extern crate flatbuffers; + +#[cfg(target_family = "unix")] +#[allow(dead_code, unused_imports)] +mod generated { + include!(concat!(env!("OUT_DIR"), "/monster_generated.rs")); +} + +#[cfg(target_family = "windows")] +#[allow(dead_code, unused_imports)] +mod generated { + include!(concat!(env!("OUT_DIR"), "\\monster_generated.rs")); +} + +use generated::my_game::sample::{Monster, MonsterArgs}; + + +fn main() { + let mut fbb = flatbuffers::FlatBufferBuilder::new(); + let name = Some(fbb.create_string("bob")); + let m = Monster::create(&mut fbb, &MonsterArgs { + hp: 1, + mana: 2, + name, + ..Default::default() + }); + fbb.finish(m, None); + let mon = flatbuffers::root::(fbb.finished_data()).unwrap(); + assert_eq!(mon.hp(), 1); + assert_eq!(mon.mana(), 2); + assert_eq!(mon.name().unwrap(), "bob"); +} + +#[test] +fn test_main() { + main() +} diff --git a/tests/rust_usage_test/tests/integration_test.rs b/tests/rust_usage_test/tests/integration_test.rs index 652a098..fa68647 100644 --- a/tests/rust_usage_test/tests/integration_test.rs +++ b/tests/rust_usage_test/tests/integration_test.rs @@ -31,18 +31,23 @@ extern crate quickcheck_derive; mod flexbuffers_tests; mod optional_scalars_test; +#[allow(dead_code, unused_imports)] #[path = "../../include_test/include_test1_generated.rs"] pub mod include_test1_generated; +#[allow(dead_code, unused_imports)] #[path = "../../include_test/sub/include_test2_generated.rs"] pub mod include_test2_generated; +#[allow(dead_code, unused_imports)] #[path = "../../namespace_test/namespace_test1_generated.rs"] pub mod namespace_test1_generated; +#[allow(dead_code, unused_imports)] #[path = "../../namespace_test/namespace_test2_generated.rs"] pub mod namespace_test2_generated; +#[allow(dead_code, unused_imports)] #[path = "../../monster_test_generated.rs"] mod monster_test_generated; pub use monster_test_generated::my_game; -- 2.7.4