mirror of
https://gitee.com/openharmony/third_party_rust_bindgen
synced 2024-12-11 17:14:31 +00:00
Define extra assertion macros
This commit defines a new set of assertion macros that are only checked in testing/CI when the `testing_only_extra_assertions` feature is enabled. This makes it so that *users* of bindgen that happen to be making a debug build don't enable all these extra and expensive assertions. Additionally, this removes the `testing_only_assert_no_dangling_items` feature, and runs the assertions that were previously gated on that feature when the new `testing_only_extra_assertions` feature is enabled.
This commit is contained in:
parent
88b7cd66a2
commit
5004ed43b2
@ -70,6 +70,6 @@ static = []
|
|||||||
|
|
||||||
# These features only exist for CI testing -- don't use them if you're not hacking
|
# These features only exist for CI testing -- don't use them if you're not hacking
|
||||||
# on bindgen!
|
# on bindgen!
|
||||||
testing_only_assert_no_dangling_items = []
|
|
||||||
testing_only_docs = []
|
testing_only_docs = []
|
||||||
|
testing_only_extra_assertions = []
|
||||||
testing_only_llvm_stable = []
|
testing_only_llvm_stable = []
|
||||||
|
@ -6,10 +6,13 @@ cd "$(dirname "$0")/.."
|
|||||||
# Regenerate the test headers' bindings in debug and release modes, and assert
|
# Regenerate the test headers' bindings in debug and release modes, and assert
|
||||||
# that we always get the expected generated bindings.
|
# that we always get the expected generated bindings.
|
||||||
|
|
||||||
cargo test --features "$BINDGEN_FEATURES testing_only_assert_no_dangling_items"
|
cargo test --features "$BINDGEN_FEATURES"
|
||||||
./ci/assert-no-diff.sh
|
./ci/assert-no-diff.sh
|
||||||
|
|
||||||
cargo test --release --features "$BINDGEN_FEATURES testing_only_assert_no_dangling_items"
|
cargo test --features "$BINDGEN_FEATURES testing_only_extra_assertions"
|
||||||
|
./ci/assert-no-diff.sh
|
||||||
|
|
||||||
|
cargo test --release --features "$BINDGEN_FEATURES testing_only_extra_assertions"
|
||||||
./ci/assert-no-diff.sh
|
./ci/assert-no-diff.sh
|
||||||
|
|
||||||
# Now test the expectations' size and alignment tests.
|
# Now test the expectations' size and alignment tests.
|
||||||
|
@ -2642,7 +2642,7 @@ impl TryToRustTy for TemplateInstantiation {
|
|||||||
// This can happen if we generated an opaque type for a partial
|
// This can happen if we generated an opaque type for a partial
|
||||||
// template specialization, and we've hit an instantiation of
|
// template specialization, and we've hit an instantiation of
|
||||||
// that partial specialization.
|
// that partial specialization.
|
||||||
debug_assert!(ctx.resolve_type_through_type_refs(decl)
|
extra_assert!(ctx.resolve_type_through_type_refs(decl)
|
||||||
.is_opaque());
|
.is_opaque());
|
||||||
return Err(error::Error::InstantiationOfOpaqueType);
|
return Err(error::Error::InstantiationOfOpaqueType);
|
||||||
}
|
}
|
||||||
|
30
src/extra_assertions.rs
Normal file
30
src/extra_assertions.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
//! Macros for defining extra assertions that should only be checked in testing
|
||||||
|
//! and/or CI when the `testing_only_extra_assertions` feature is enabled.
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! extra_assert {
|
||||||
|
( $cond:expr ) => {
|
||||||
|
if cfg!(feature = "testing_only_extra_assertions") {
|
||||||
|
assert!($cond);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
( $cond:expr , $( $arg:tt )+ ) => {
|
||||||
|
if cfg!(feature = "testing_only_extra_assertions") {
|
||||||
|
assert!($cond, $( $arg )* )
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! extra_assert_eq {
|
||||||
|
( $lhs:expr , $rhs:expr ) => {
|
||||||
|
if cfg!(feature = "testing_only_extra_assertions") {
|
||||||
|
assert_eq!($lhs, $rhs);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
( $lhs:expr , $rhs:expr , $( $arg:tt )+ ) => {
|
||||||
|
if cfg!(feature = "testing_only_extra_assertions") {
|
||||||
|
assert!($lhs, $rhs, $( $arg )* );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
@ -584,12 +584,11 @@ impl<'ctx> BindgenContext<'ctx> {
|
|||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
/// When the `testing_only_assert_no_dangling_items` feature is enabled,
|
/// When the `testing_only_extra_assertions` feature is enabled, this
|
||||||
/// this function walks the IR graph and asserts that we do not have any
|
/// function walks the IR graph and asserts that we do not have any edges
|
||||||
/// edges referencing an ItemId for which we do not have an associated IR
|
/// referencing an ItemId for which we do not have an associated IR item.
|
||||||
/// item.
|
|
||||||
fn assert_no_dangling_references(&self) {
|
fn assert_no_dangling_references(&self) {
|
||||||
if cfg!(feature = "testing_only_assert_no_dangling_items") {
|
if cfg!(feature = "testing_only_extra_assertions") {
|
||||||
for _ in self.assert_no_dangling_item_traversal() {
|
for _ in self.assert_no_dangling_item_traversal() {
|
||||||
// The iterator's next method does the asserting for us.
|
// The iterator's next method does the asserting for us.
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ pub trait ItemAncestors {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(debug_assertions)] {
|
if #[cfg(testing_only_extra_assertions)] {
|
||||||
type DebugOnlyItemSet = ItemSet;
|
type DebugOnlyItemSet = ItemSet;
|
||||||
} else {
|
} else {
|
||||||
struct DebugOnlyItemSet;
|
struct DebugOnlyItemSet;
|
||||||
@ -123,7 +123,7 @@ impl<'a, 'b> Iterator for ItemAncestorsIter<'a, 'b>
|
|||||||
} else {
|
} else {
|
||||||
self.item = item.parent_id();
|
self.item = item.parent_id();
|
||||||
|
|
||||||
debug_assert!(!self.seen.contains(&item.id()));
|
extra_assert!(!self.seen.contains(&item.id()));
|
||||||
self.seen.insert(item.id());
|
self.seen.insert(item.id());
|
||||||
|
|
||||||
Some(item.id())
|
Some(item.id())
|
||||||
@ -614,7 +614,7 @@ impl Item {
|
|||||||
let mut item = self;
|
let mut item = self;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
debug_assert!(!targets_seen.contains(&item.id()));
|
extra_assert!(!targets_seen.contains(&item.id()));
|
||||||
targets_seen.insert(item.id());
|
targets_seen.insert(item.id());
|
||||||
|
|
||||||
if self.annotations().use_instead_of().is_some() {
|
if self.annotations().use_instead_of().is_some() {
|
||||||
|
@ -371,7 +371,7 @@ impl<'ctx, 'gen> MonotoneFramework for UsedTemplateParameters<'ctx, 'gen> {
|
|||||||
fn constrain(&mut self, id: ItemId) -> bool {
|
fn constrain(&mut self, id: ItemId) -> bool {
|
||||||
// Invariant: all hash map entries' values are `Some` upon entering and
|
// Invariant: all hash map entries' values are `Some` upon entering and
|
||||||
// exiting this method.
|
// exiting this method.
|
||||||
debug_assert!(self.used.values().all(|v| v.is_some()));
|
extra_assert!(self.used.values().all(|v| v.is_some()));
|
||||||
|
|
||||||
// Take the set for this id out of the hash map while we mutate it based
|
// Take the set for this id out of the hash map while we mutate it based
|
||||||
// on other hash map entries. We *must* put it back into the hash map at
|
// on other hash map entries. We *must* put it back into the hash map at
|
||||||
@ -437,7 +437,7 @@ impl<'ctx, 'gen> MonotoneFramework for UsedTemplateParameters<'ctx, 'gen> {
|
|||||||
|
|
||||||
// Put the set back in the hash map and restore our invariant.
|
// Put the set back in the hash map and restore our invariant.
|
||||||
self.used.insert(id, Some(used_by_this_id));
|
self.used.insert(id, Some(used_by_this_id));
|
||||||
debug_assert!(self.used.values().all(|v| v.is_some()));
|
extra_assert!(self.used.values().all(|v| v.is_some()));
|
||||||
|
|
||||||
new_len != original_len
|
new_len != original_len
|
||||||
}
|
}
|
||||||
|
@ -37,6 +37,9 @@ extern crate log;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod log_stubs;
|
mod log_stubs;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
mod extra_assertions;
|
||||||
|
|
||||||
// A macro to declare an internal module for which we *must* provide
|
// A macro to declare an internal module for which we *must* provide
|
||||||
// documentation for. If we are building with the "testing_only_docs" feature,
|
// documentation for. If we are building with the "testing_only_docs" feature,
|
||||||
// then the module is declared public, and our `#![deny(missing_docs)]` pragma
|
// then the module is declared public, and our `#![deny(missing_docs)]` pragma
|
||||||
|
Loading…
Reference in New Issue
Block a user