diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5ef794..b8b1f0b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,8 @@ jobs: - run: cargo clippy -- -D warnings + - run: cd compatibility-tests/compile-fail && cargo test + secondary: runs-on: ubuntu-latest strategy: diff --git a/Cargo.toml b/Cargo.toml index e0ae395..c2e47e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,8 +15,6 @@ documentation = "https://docs.rs/sxd-document" license = "MIT" [features] -compile_failure = [] - __internal_expose_string_pool = [] [dependencies] diff --git a/compatibility-tests/compile-fail/Cargo.toml b/compatibility-tests/compile-fail/Cargo.toml new file mode 100644 index 0000000..aaf0d36 --- /dev/null +++ b/compatibility-tests/compile-fail/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "compile-fail" +version = "0.1.0" +edition = "2021" + +[dependencies] +sxd-document = { path = "../..", features = ["__internal_expose_string_pool"] } + +trybuild = "1.0" diff --git a/compatibility-tests/compile-fail/src/lib.rs b/compatibility-tests/compile-fail/src/lib.rs new file mode 100644 index 0000000..870c2f9 --- /dev/null +++ b/compatibility-tests/compile-fail/src/lib.rs @@ -0,0 +1,5 @@ +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/*.rs"); +} diff --git a/compatibility-tests/compile-fail/tests/ui/string_pool.rs b/compatibility-tests/compile-fail/tests/ui/string_pool.rs new file mode 100644 index 0000000..b9ac643 --- /dev/null +++ b/compatibility-tests/compile-fail/tests/ui/string_pool.rs @@ -0,0 +1,10 @@ +use sxd_document::__internal::StringPool; + +fn string_cannot_outlive_the_pool() { + let _s = { + let pool = StringPool::new(); + pool.intern("hello") + }; +} + +fn main() {} diff --git a/compatibility-tests/compile-fail/tests/ui/string_pool.stderr b/compatibility-tests/compile-fail/tests/ui/string_pool.stderr new file mode 100644 index 0000000..3ab56da --- /dev/null +++ b/compatibility-tests/compile-fail/tests/ui/string_pool.stderr @@ -0,0 +1,11 @@ +error[E0597]: `pool` does not live long enough + --> tests/ui/string_pool.rs:6:9 + | +4 | let _s = { + | -- borrow later stored here +5 | let pool = StringPool::new(); + | ---- binding `pool` declared here +6 | pool.intern("hello") + | ^^^^ borrowed value does not live long enough +7 | }; + | - `pool` dropped here while still borrowed diff --git a/compatibility-tests/compile-fail/tests/ui/thindom.rs b/compatibility-tests/compile-fail/tests/ui/thindom.rs new file mode 100644 index 0000000..a918307 --- /dev/null +++ b/compatibility-tests/compile-fail/tests/ui/thindom.rs @@ -0,0 +1,48 @@ +use sxd_document::Package; + +fn cannot_mutate_connections_while_iterating_over_root_children() { + let package = Package::new(); + let (s, mut c) = package.as_thin_document(); + + let alpha = s.create_element("alpha"); + + for _ in c.root_children() { + c.append_root_child(alpha); + } +} + +fn cannot_mutate_connections_while_iterating_over_element_children() { + let package = Package::new(); + let (s, mut c) = package.as_thin_document(); + + let alpha = s.create_element("alpha"); + let beta = s.create_element("beta"); + + for _ in c.element_children(alpha) { + c.append_element_child(alpha, beta); + } +} + +fn cannot_mutate_connections_while_iterating_over_siblings() { + let package = Package::new(); + let (s, mut c) = package.as_thin_document(); + + let alpha = s.create_element("alpha"); + let beta = s.create_element("beta"); + + for _ in c.element_preceding_siblings(alpha) { + c.append_element_child(alpha, beta); + } +} + +fn nodes_cannot_live_outside_of_the_document() { + let package = Package::new(); + + let _node = { + let (s, _) = package.as_thin_document(); + + s.create_element("hello") + }; +} + +fn main() {} diff --git a/compatibility-tests/compile-fail/tests/ui/thindom.stderr b/compatibility-tests/compile-fail/tests/ui/thindom.stderr new file mode 100644 index 0000000..8c5a6c0 --- /dev/null +++ b/compatibility-tests/compile-fail/tests/ui/thindom.stderr @@ -0,0 +1,45 @@ +error[E0502]: cannot borrow `c` as mutable because it is also borrowed as immutable + --> tests/ui/thindom.rs:10:9 + | +9 | for _ in c.root_children() { + | ----------------- + | | + | immutable borrow occurs here + | immutable borrow later used here +10 | c.append_root_child(alpha); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here + +error[E0502]: cannot borrow `c` as mutable because it is also borrowed as immutable + --> tests/ui/thindom.rs:22:9 + | +21 | for _ in c.element_children(alpha) { + | ------------------------- + | | + | immutable borrow occurs here + | immutable borrow later used here +22 | c.append_element_child(alpha, beta); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here + +error[E0502]: cannot borrow `c` as mutable because it is also borrowed as immutable + --> tests/ui/thindom.rs:34:9 + | +33 | for _ in c.element_preceding_siblings(alpha) { + | ----------------------------------- + | | + | immutable borrow occurs here + | immutable borrow later used here +34 | c.append_element_child(alpha, beta); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here + +error[E0597]: `s` does not live long enough + --> tests/ui/thindom.rs:44:9 + | +41 | let _node = { + | ----- borrow later stored here +42 | let (s, _) = package.as_thin_document(); + | - binding `s` declared here +43 | +44 | s.create_element("hello") + | ^ borrowed value does not live long enough +45 | }; + | - `s` dropped here while still borrowed diff --git a/src/dom.rs b/src/dom.rs index 9728b8f..65a295a 100644 --- a/src/dom.rs +++ b/src/dom.rs @@ -1616,16 +1616,4 @@ mod test { let element = doc.root().children()[0].element().unwrap(); assert_qname_eq!(element.name(), "hello"); } - - #[test] - #[cfg(feature = "compile_failure")] - fn nodes_cannot_live_outside_of_the_document() { - let package = Package::new(); - - let _ = { - let doc = package.as_document(); - - doc.create_element("hello") - }; - } } diff --git a/src/string_pool.rs b/src/string_pool.rs index 330e701..53b6bd7 100644 --- a/src/string_pool.rs +++ b/src/string_pool.rs @@ -230,15 +230,6 @@ mod test { assert_eq!(interned1.as_bytes().as_ptr(), interned2.as_bytes().as_ptr()); } - #[test] - #[cfg(feature = "compile_failure")] - fn string_cannot_outlive_the_pool() { - let z = { - let s = StringPool::new(); - s.intern("hello") - }; - } - #[test] fn ignores_the_lifetime_of_the_input_string() { let s = StringPool::new(); diff --git a/src/thindom.rs b/src/thindom.rs index 9059066..2baae1d 100644 --- a/src/thindom.rs +++ b/src/thindom.rs @@ -985,61 +985,4 @@ mod test { let element = children[0].element().unwrap(); assert_qname_eq!(element.name(), "hello"); } - - #[test] - #[cfg(feature = "compile_failure")] - //cannot borrow `c` as mutable because it is also borrowed as immutable - fn cannot_mutate_connections_while_iterating_over_root_children() { - let package = Package::new(); - let (s, mut c) = package.as_thin_document(); - - let alpha = s.create_element("alpha"); - - for _ in c.root_children() { - c.append_root_child(alpha); - } - } - - #[test] - #[cfg(feature = "compile_failure")] - //cannot borrow `c` as mutable because it is also borrowed as immutable - fn cannot_mutate_connections_while_iterating_over_element_children() { - let package = Package::new(); - let (s, mut c) = package.as_thin_document(); - - let alpha = s.create_element("alpha"); - let beta = s.create_element("beta"); - - for _ in c.element_children(alpha) { - c.append_element_child(alpha, beta); - } - } - - #[test] - #[cfg(feature = "compile_failure")] - //cannot borrow `c` as mutable because it is also borrowed as immutable - fn cannot_mutate_connections_while_iterating_over_siblings() { - let package = Package::new(); - let (s, mut c) = package.as_thin_document(); - - let alpha = s.create_element("alpha"); - let beta = s.create_element("beta"); - - for _ in c.element_preceding_siblings(alpha) { - c.append_element_child(alpha, beta); - } - } - - #[test] - #[cfg(feature = "compile_failure")] - //`s` does not live long enough - fn nodes_cannot_live_outside_of_the_document() { - let package = Package::new(); - - let _ = { - let (s, _) = package.as_thin_document(); - - s.create_element("hello") - }; - } }