Skip to content

Commit 44ed6df

Browse files
committed
Implement CanDeriveDebug for TemplateInstantiation
We need the template instantiation's layout to determine if we can derive debug for it when the instantiation's template definition has non-type parameters.
1 parent 6939189 commit 44ed6df

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/ir/comp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ impl CanDeriveDefault for CompInfo {
864864

865865
return layout.unwrap_or_else(Layout::zero)
866866
.opaque()
867-
.can_derive_debug(ctx, ());
867+
.can_derive_default(ctx, ());
868868
}
869869

870870
self.detect_derive_default_cycle.set(true);

src/ir/template.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
use clang;
3131
use parse::ClangItemParser;
3232
use super::context::{BindgenContext, ItemId};
33-
use super::derive::CanDeriveCopy;
33+
use super::derive::{CanDeriveCopy, CanDeriveDebug};
34+
use super::layout::Layout;
3435
use super::item::Item;
3536
use super::traversal::{EdgeKind, Trace, Tracer};
3637

@@ -148,6 +149,33 @@ impl<'a> CanDeriveCopy<'a> for TemplateInstantiation {
148149
}
149150
}
150151

152+
impl CanDeriveDebug for TemplateInstantiation {
153+
type Extra = Option<Layout>;
154+
155+
fn can_derive_debug(&self,
156+
ctx: &BindgenContext,
157+
layout: Option<Layout>)
158+
-> bool {
159+
self.args.iter().all(|arg| arg.can_derive_debug(ctx, ())) &&
160+
ctx.resolve_type(self.definition)
161+
.as_comp()
162+
.and_then(|c| {
163+
// For non-type template parameters, we generate an opaque
164+
// blob, and in this case the instantiation has a better
165+
// idea of the layout than the definition does.
166+
if c.has_non_type_template_params() {
167+
let opaque = layout.unwrap_or(Layout::zero()).opaque();
168+
Some(opaque.can_derive_debug(ctx, ()))
169+
} else {
170+
None
171+
}
172+
})
173+
.unwrap_or_else(|| {
174+
self.definition.can_derive_debug(ctx, ())
175+
})
176+
}
177+
}
178+
151179
impl Trace for TemplateInstantiation {
152180
type Extra = ();
153181

src/ir/ty.rs

+3
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ impl CanDeriveDebug for Type {
636636
TypeKind::Comp(ref info) => {
637637
info.can_derive_debug(ctx, self.layout(ctx))
638638
}
639+
TypeKind::TemplateInstantiation(ref inst) => {
640+
inst.can_derive_debug(ctx, self.layout(ctx))
641+
}
639642
_ => true,
640643
}
641644
}

0 commit comments

Comments
 (0)