Skip to content

Commit c1aef63

Browse files
author
bors-servo
authored
Auto merge of #1005 - fitzgen:some-little-deriving-cleanups, r=emilio
Some little deriving cleanups See each commit for details. r? @emilio
2 parents fcbe260 + 337fbb4 commit c1aef63

File tree

6 files changed

+145
-177
lines changed

6 files changed

+145
-177
lines changed

src/ir/analysis/derive_partial_eq_or_partial_ord.rs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! Determining which types for which we can emit `#[derive(PartialEq)]`.
1+
//! Determining which types for which we cannot emit `#[derive(PartialEq,
2+
//! PartialOrd)]`.
23
34
use super::{ConstrainResult, MonotoneFramework, generate_dependencies};
45
use ir::comp::CompKind;
@@ -13,31 +14,39 @@ use ir::ty::TypeKind;
1314
use std::collections::HashMap;
1415
use std::collections::HashSet;
1516

16-
/// An analysis that finds for each IR item whether partialeq or partialord cannot be derived.
17+
/// An analysis that finds for each IR item whether `PartialEq`/`PartialOrd`
18+
/// cannot be derived.
1719
///
18-
/// We use the monotone constraint function `cannot_derive_partialeq_or_partialord`, defined as
19-
/// follows:
20+
/// We use the monotone constraint function
21+
/// `cannot_derive_partialeq_or_partialord`, defined as follows:
2022
///
2123
/// * If T is Opaque and layout of the type is known, get this layout as opaque
2224
/// type and check whether it can be derived using trivial checks.
23-
/// * If T is Array type, partialeq or partialord cannot be derived if the length of
24-
/// the array is larger than the limit or the type of data the array contains cannot derive
25-
/// partialeq or partialord.
25+
///
26+
/// * If T is Array type, `PartialEq` or partialord cannot be derived if the
27+
/// length of the array is larger than the limit or the type of data the array
28+
/// contains cannot derive `PartialEq`/`PartialOrd`.
29+
///
2630
/// * If T is a type alias, a templated alias or an indirection to another type,
27-
/// partialeq or partialord cannot be derived if the type T refers to cannot be derived partialeq or partialord.
28-
/// * If T is a compound type, partialeq or partialord cannot be derived if any of its base member
29-
/// or field cannot be derived partialeq or partialord.
30-
/// * If T is a pointer, T cannot be derived partialeq or partialord if T is a function pointer
31-
/// and the function signature cannot be derived partialeq or partialord.
31+
/// `PartialEq`/`PartialOrd` cannot be derived if the type T refers to cannot be
32+
/// derived `PartialEq`/`PartialOrd`.
33+
///
34+
/// * If T is a compound type, `PartialEq`/`PartialOrd` cannot be derived if any
35+
/// of its base member or field cannot be derived `PartialEq`/`PartialOrd`.
36+
///
37+
/// * If T is a pointer, T cannot be derived `PartialEq`/`PartialOrd` if T is a
38+
/// function pointer and the function signature cannot be derived
39+
/// `PartialEq`/`PartialOrd`.
40+
///
3241
/// * If T is an instantiation of an abstract template definition, T cannot be
33-
/// derived partialeq or partialord if any of the template arguments or template definition
34-
/// cannot derive partialeq or partialord.
42+
/// derived `PartialEq`/`PartialOrd` if any of the template arguments or
43+
/// template definition cannot derive `PartialEq`/`PartialOrd`.
3544
#[derive(Debug, Clone)]
3645
pub struct CannotDerivePartialEqOrPartialOrd<'ctx> {
3746
ctx: &'ctx BindgenContext,
3847

3948
// The incremental result of this analysis's computation. Everything in this
40-
// set cannot derive partialeq or partialord.
49+
// set cannot derive `PartialEq`/`PartialOrd`.
4150
cannot_derive_partialeq_or_partialord: HashSet<ItemId>,
4251

4352
// Dependencies saying that if a key ItemId has been inserted into the
@@ -46,15 +55,15 @@ pub struct CannotDerivePartialEqOrPartialOrd<'ctx> {
4655
//
4756
// This is a subset of the natural IR graph with reversed edges, where we
4857
// only include the edges from the IR graph that can affect whether a type
49-
// can derive partialeq or partialord.
58+
// can derive `PartialEq`/`PartialOrd`.
5059
dependencies: HashMap<ItemId, Vec<ItemId>>,
5160
}
5261

5362
impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
5463
fn consider_edge(kind: EdgeKind) -> bool {
5564
match kind {
5665
// These are the only edges that can affect whether a type can derive
57-
// partialeq or partialord.
66+
// `PartialEq`/`PartialOrd`.
5867
EdgeKind::BaseMember |
5968
EdgeKind::Field |
6069
EdgeKind::TypeReference |
@@ -115,7 +124,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
115124
trace!("constrain: {:?}", id);
116125

117126
if self.cannot_derive_partialeq_or_partialord.contains(&id) {
118-
trace!(" already know it cannot derive PartialEq or PartialOrd");
127+
trace!(" already know it cannot derive `PartialEq`/`PartialOrd`");
119128
return ConstrainResult::Same;
120129
}
121130

@@ -140,10 +149,10 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
140149
return if layout_can_derive &&
141150
!(ty.is_union() &&
142151
self.ctx.options().rust_features().untagged_union()) {
143-
trace!(" we can trivially derive PartialEq or PartialOrd for the layout");
152+
trace!(" we can trivially derive `PartialEq`/`PartialOrd` for the layout");
144153
ConstrainResult::Same
145154
} else {
146-
trace!(" we cannot derive PartialEq or PartialOrd for the layout");
155+
trace!(" we cannot derive `PartialEq`/`PartialOrd` for the layout");
147156
self.insert(id)
148157
};
149158
}
@@ -176,24 +185,24 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
176185
TypeKind::ObjCInterface(..) |
177186
TypeKind::ObjCId |
178187
TypeKind::ObjCSel => {
179-
trace!(" simple type that can always derive PartialEq or PartialOrd");
188+
trace!(" simple type that can always derive `PartialEq`/`PartialOrd`");
180189
ConstrainResult::Same
181190
}
182191

183192
TypeKind::Array(t, len) => {
184193
if self.cannot_derive_partialeq_or_partialord.contains(&t) {
185194
trace!(
186-
" arrays of T for which we cannot derive PartialEq or PartialOrd \
187-
also cannot derive PartialEq or PartialOrd"
195+
" arrays of T for which we cannot derive `PartialEq`/`PartialOrd` \
196+
also cannot derive `PartialEq`/`PartialOrd`"
188197
);
189198
return self.insert(id);
190199
}
191200

192201
if len <= RUST_DERIVE_IN_ARRAY_LIMIT {
193-
trace!(" array is small enough to derive PartialEq or PartialOrd");
202+
trace!(" array is small enough to derive `PartialEq`/`PartialOrd`");
194203
ConstrainResult::Same
195204
} else {
196-
trace!(" array is too large to derive PartialEq or PartialOrd");
205+
trace!(" array is too large to derive `PartialEq`/`PartialOrd`");
197206
self.insert(id)
198207
}
199208
}
@@ -204,7 +213,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
204213
if let TypeKind::Function(ref sig) = *inner_type.kind() {
205214
if !sig.can_trivially_derive_partialeq_or_partialord() {
206215
trace!(
207-
" function pointer that can't trivially derive PartialEq or PartialOrd"
216+
" function pointer that can't trivially derive `PartialEq`/`PartialOrd`"
208217
);
209218
return self.insert(id);
210219
}
@@ -216,11 +225,11 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
216225
TypeKind::Function(ref sig) => {
217226
if !sig.can_trivially_derive_partialeq_or_partialord() {
218227
trace!(
219-
" function that can't trivially derive PartialEq or PartialOrd"
228+
" function that can't trivially derive `PartialEq`/`PartialOrd`"
220229
);
221230
return self.insert(id);
222231
}
223-
trace!(" function can derive PartialEq or PartialOrd");
232+
trace!(" function can derive `PartialEq`/`PartialOrd`");
224233
ConstrainResult::Same
225234
}
226235

@@ -230,13 +239,13 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
230239
if self.cannot_derive_partialeq_or_partialord.contains(&t) {
231240
trace!(
232241
" aliases and type refs to T which cannot derive \
233-
PartialEq or PartialOrd also cannot derive PartialEq or PartialOrd"
242+
`PartialEq`/`PartialOrd` also cannot derive `PartialEq`/`PartialOrd`"
234243
);
235244
self.insert(id)
236245
} else {
237246
trace!(
238247
" aliases and type refs to T which can derive \
239-
PartialEq or PartialOrd can also derive PartialEq or PartialOrd"
248+
`PartialEq`/`PartialOrd` can also derive `PartialEq`/`PartialOrd`"
240249
);
241250
ConstrainResult::Same
242251
}
@@ -250,7 +259,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
250259

251260
if info.kind() == CompKind::Union {
252261
if self.ctx.options().rust_features().untagged_union() {
253-
trace!(" cannot derive PartialEq or PartialOrd for Rust unions");
262+
trace!(" cannot derive `PartialEq`/`PartialOrd` for Rust unions");
254263
return self.insert(id);
255264
}
256265

@@ -259,11 +268,11 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
259268
})
260269
{
261270
trace!(
262-
" union layout can trivially derive PartialEq or PartialOrd"
271+
" union layout can trivially derive `PartialEq`/`PartialOrd`"
263272
);
264273
return ConstrainResult::Same;
265274
} else {
266-
trace!(" union layout cannot derive PartialEq or PartialOrd");
275+
trace!(" union layout cannot derive `PartialEq`/`PartialOrd`");
267276
return self.insert(id);
268277
}
269278
}
@@ -275,7 +284,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
275284
});
276285
if bases_cannot_derive {
277286
trace!(
278-
" base members cannot derive PartialEq or PartialOrd, so we can't \
287+
" base members cannot derive `PartialEq`/`PartialOrd`, so we can't \
279288
either"
280289
);
281290
return self.insert(id);
@@ -312,7 +321,7 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
312321
});
313322
if fields_cannot_derive {
314323
trace!(
315-
" fields cannot derive PartialEq or PartialOrd, so we can't either"
324+
" fields cannot derive `PartialEq`/`PartialOrd`, so we can't either"
316325
);
317326
return self.insert(id);
318327
}
@@ -328,8 +337,8 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
328337
});
329338
if args_cannot_derive {
330339
trace!(
331-
" template args cannot derive PartialEq or PartialOrd, so \
332-
insantiation can't either"
340+
" template args cannot derive `PartialEq`/`PartialOrd`, so \
341+
insantiation can't either"
333342
);
334343
return self.insert(id);
335344
}
@@ -343,13 +352,13 @@ impl<'ctx> MonotoneFramework for CannotDerivePartialEqOrPartialOrd<'ctx> {
343352
);
344353
if def_cannot_derive {
345354
trace!(
346-
" template definition cannot derive PartialEq or PartialOrd, so \
347-
insantiation can't either"
355+
" template definition cannot derive `PartialEq`/`PartialOrd`, so \
356+
insantiation can't either"
348357
);
349358
return self.insert(id);
350359
}
351360

352-
trace!(" template instantiation can derive PartialEq or PartialOrd");
361+
trace!(" template instantiation can derive `PartialEq`/`PartialOrd`");
353362
ConstrainResult::Same
354363
}
355364

src/ir/context.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ pub struct BindgenContext {
239239

240240
/// The set of (`ItemId`s of) types that can't derive hash.
241241
///
242-
/// This is populated when we enter codegen by `compute_can_derive_partialeq`
243-
/// and is always `None` before that and `Some` after.
242+
/// This is populated when we enter codegen by
243+
/// `compute_cannot_derive_partialord_partialeq_or_eq` and is always `None`
244+
/// before that and `Some` after.
244245
cannot_derive_partialeq_or_partialord: Option<HashSet<ItemId>>,
245246

246247
/// The set of (`ItemId's of`) types that has vtable.
@@ -2163,8 +2164,7 @@ impl BindgenContext {
21632164
}
21642165
}
21652166

2166-
/// Look up whether the item with `id` can
2167-
/// derive partialeq or partialord.
2167+
/// Look up whether the item with `id` can derive `Partial{Eq,Ord}`.
21682168
pub fn lookup_item_id_can_derive_partialeq_or_partialord(&self, id: ItemId) -> bool {
21692169
assert!(
21702170
self.in_codegen_phase(),
@@ -2176,8 +2176,7 @@ impl BindgenContext {
21762176
!self.cannot_derive_partialeq_or_partialord.as_ref().unwrap().contains(&id)
21772177
}
21782178

2179-
/// Look up whether the item with `id` can
2180-
/// derive copy or not.
2179+
/// Look up whether the item with `id` can derive `Copy` or not.
21812180
pub fn lookup_item_id_can_derive_copy(&self, id: ItemId) -> bool {
21822181
assert!(
21832182
self.in_codegen_phase(),

0 commit comments

Comments
 (0)