Rust: remove inner attributes (#6410)
authorCasper <casperneo@uchicago.edu>
Tue, 26 Jan 2021 16:09:29 +0000 (11:09 -0500)
committerGitHub <noreply@github.com>
Tue, 26 Jan 2021 16:09:29 +0000 (11:09 -0500)
* 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 <cneo@google.com>
15 files changed:
samples/monster_generated.rs
samples/sample_binary.rs
src/idl_gen_rust.cpp
tests/RustTest.sh
tests/include_test/include_test1_generated.rs
tests/include_test/sub/include_test2_generated.rs
tests/monster_test_generated.rs
tests/namespace_test/namespace_test1_generated.rs
tests/namespace_test/namespace_test2_generated.rs
tests/optional_scalars_generated.rs
tests/rust_usage_test/Cargo.toml
tests/rust_usage_test/outdir/Cargo.toml [new file with mode: 0644]
tests/rust_usage_test/outdir/build.rs [new file with mode: 0644]
tests/rust_usage_test/outdir/src/main.rs [new file with mode: 0644]
tests/rust_usage_test/tests/integration_test.rs

index 13b2cf1..cc2051e 100644 (file)
@@ -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;
index 1b08f20..67f05e6 100644 (file)
@@ -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;
index 1677ce8..424112c 100644 (file)
@@ -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) {
index 7fa6492..71258c9 100755 (executable)
@@ -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
index 4015a3c..d7511f9 100644 (file)
@@ -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;
index 9b3c7ee..787ec19 100644 (file)
@@ -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;
index bae1066..a514c7a 100644 (file)
@@ -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::*;
index 88c7cb2..aa4dde6 100644 (file)
@@ -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;
index f86e424..e0ecd72 100644 (file)
@@ -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;
index 73ebee3..81483b5 100644 (file)
@@ -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;
index 664396d..45ecca6 100644 (file)
@@ -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 (file)
index 0000000..8387d17
--- /dev/null
@@ -0,0 +1,10 @@
+[package]
+name = "outdir"
+version = "0.1.0"
+authors = ["Casper Neo <cneo@google.com>"]
+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 (file)
index 0000000..663ca99
--- /dev/null
@@ -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 (file)
index 0000000..b5fcaf8
--- /dev/null
@@ -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::<Monster>(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()
+}
index 652a098..fa68647 100644 (file)
@@ -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;