Skip to content

Commit e224ec0

Browse files
committed
Add --no-partialeq <regex> flag
Related to rust-lang#965. - [x] Add a new RegexSet member to bindgen::Builder (similar to the whitelisted_types set). - [x] A Builder method to add strings to that RegexSet. - [x] Plumbing in src/options.rs to convert --no-partialeq <regex> CLI flags into invocations of the builder method. - [x] Make the MonotoneFramework::constrain function in src/ir/analysis/derive_partialeq.rs check if the given item is explicitly marked not to be Partialeq, and if so, insert it into the self.cannot_derive_partialeq set via return self.insert(id). - [] Tests! - [] When the no-partialeq type is transitively referenced by a whitelisted item - [] When the no-partialeq type is explicitly whitelisted - [] When the no-partialeq type is marked opaque
1 parent 1906a26 commit e224ec0

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/ir/analysis/derive_partial_eq_or_partial_ord.rs

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use ir::comp::FieldMethods;
77
use ir::context::{BindgenContext, ItemId};
88
use ir::derive::CanTriviallyDerivePartialEqOrPartialOrd;
99
use ir::item::IsOpaque;
10+
use ir::item::ItemCanonicalName;
1011
use ir::traversal::EdgeKind;
1112
use ir::ty::RUST_DERIVE_IN_ARRAY_LIMIT;
1213
use ir::ty::TypeKind;
@@ -144,6 +145,11 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
144145
};
145146
}
146147

148+
let name = item.canonical_name(self.ctx);
149+
if self.ctx.options().no_partialeq_types.matches(&name) {
150+
return self.insert(id)
151+
}
152+
147153
if ty.layout(self.ctx).map_or(false, |l| {
148154
l.align > RUST_DERIVE_IN_ARRAY_LIMIT
149155
})

src/lib.rs

+26
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,20 @@ impl Builder {
500500
output_vector.push(path.into());
501501
}
502502

503+
self.options
504+
.no_partialeq_types
505+
.get_items()
506+
.iter()
507+
.map(|item| {
508+
output_vector.push("--no-partialeq".into());
509+
output_vector.push(
510+
item.trim_left_matches("^")
511+
.trim_right_matches("$")
512+
.into(),
513+
);
514+
})
515+
.count();
516+
503517
output_vector
504518
}
505519

@@ -1105,6 +1119,13 @@ impl Builder {
11051119
))
11061120
}
11071121
}
1122+
1123+
/// Don't derive `PartialEq` for a given type. Regular
1124+
/// expressions are supported.
1125+
pub fn no_partialeq(mut self, arg: String) -> Builder {
1126+
self.options.no_partialeq_types.insert(arg);
1127+
self
1128+
}
11081129
}
11091130

11101131
/// Configuration options for generated bindings.
@@ -1279,7 +1300,11 @@ struct BindgenOptions {
12791300

12801301
/// The absolute path to the rustfmt configuration file, if None, the standard rustfmt
12811302
/// options are used.
1303+
12821304
rustfmt_configuration_file: Option<PathBuf>,
1305+
1306+
/// The set of types that we should not derive `PartialEq` for.
1307+
no_partialeq_types: RegexSet,
12831308
}
12841309

12851310
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -1365,6 +1390,7 @@ impl Default for BindgenOptions {
13651390
time_phases: false,
13661391
rustfmt_bindings: false,
13671392
rustfmt_configuration_file: None,
1393+
no_partialeq_types: Default::default(),
13681394
}
13691395
}
13701396
}

src/options.rs

+13
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ where
265265
.takes_value(true)
266266
.multiple(false)
267267
.number_of_values(1),
268+
Arg::with_name("no-partialeq")
269+
.long("no-partialeq")
270+
.help("Avoid deriving PartialEq for types matching <regex>.")
271+
.value_name("regex")
272+
.takes_value(true)
273+
.multiple(true)
274+
.number_of_values(1),
268275
]) // .args()
269276
.get_matches_from(args);
270277

@@ -537,6 +544,12 @@ where
537544
builder = builder.rustfmt_configuration_file(Some(path));
538545
}
539546

547+
if let Some(no_partialeq) = matches.values_of("no-partialeq") {
548+
for regex in no_partialeq {
549+
builder = builder.no_partialeq(String::from(regex));
550+
}
551+
}
552+
540553
let verbose = matches.is_present("verbose");
541554

542555
Ok((builder, output, verbose))

0 commit comments

Comments
 (0)