Skip to content

Commit 948108a

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 ab6d51b commit 948108a

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/ir/analysis/derive_partial_eq.rs

Lines changed: 6 additions & 0 deletions
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::CanTriviallyDerivePartialEq;
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 CannotDerivePartialEq<'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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,20 @@ impl Builder {
499499
output_vector.push(path.into());
500500
}
501501

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

@@ -1098,6 +1112,13 @@ impl Builder {
10981112
))
10991113
}
11001114
}
1115+
1116+
/// Don't derive `PartialEq` for a given type. Regular
1117+
/// expressions are supported.
1118+
pub fn no_partialeq(mut self, arg: String) -> Builder {
1119+
self.options.no_partialeq_types.insert(arg);
1120+
self
1121+
}
11011122
}
11021123

11031124
/// Configuration options for generated bindings.
@@ -1272,6 +1293,9 @@ pub struct BindgenOptions {
12721293
/// The absolute path to the rustfmt configuration file, if None, the standard rustfmt
12731294
/// options are used.
12741295
pub rustfmt_configuration_file: Option<PathBuf>,
1296+
1297+
/// The set of types that we should not derive `PartialEq` for.
1298+
pub no_partialeq_types: RegexSet,
12751299
}
12761300

12771301
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -1361,6 +1385,7 @@ impl Default for BindgenOptions {
13611385
time_phases: false,
13621386
rustfmt_bindings: false,
13631387
rustfmt_configuration_file: None,
1388+
no_partialeq_types: Default::default(),
13641389
}
13651390
}
13661391
}

src/options.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,13 @@ where
262262
.takes_value(true)
263263
.multiple(false)
264264
.number_of_values(1),
265+
Arg::with_name("no-partialeq")
266+
.long("no-partialeq")
267+
.help("Avoid deriving PartialEq for types matching <regex>.")
268+
.value_name("regex")
269+
.takes_value(true)
270+
.multiple(true)
271+
.number_of_values(1),
265272
]) // .args()
266273
.get_matches_from(args);
267274

@@ -530,6 +537,12 @@ where
530537
builder = builder.rustfmt_configuration_file(Some(path));
531538
}
532539

540+
if let Some(no_partialeq) = matches.values_of("no-partialeq") {
541+
for regex in no_partialeq {
542+
builder = builder.no_partialeq(String::from(regex));
543+
}
544+
}
545+
533546
let verbose = matches.is_present("verbose");
534547

535548
Ok((builder, output, verbose))

0 commit comments

Comments
 (0)