diff options
author | Nick Fitzgerald <fitzgen@gmail.com> | 2017-03-09 13:53:20 -0800 |
---|---|---|
committer | Nick Fitzgerald <fitzgen@gmail.com> | 2017-03-09 13:56:53 -0800 |
commit | b662443c1bf0c6c0103b91aec30e6b95ea5c4ff4 (patch) | |
tree | bdfbbbf4bec63ade4956837fbf09ed01a5667374 /src/codegen/mod.rs | |
parent | 72ab471539fcf2809f82e2dd980c59352a67eba4 (diff) |
Generate better opaque blobs in the face of non-type parameters
When instantiating templates whose definitions have non-type generic parameters,
prefer the layout of the instantiation type to the garbage we get from the
definition's layout. In general, an instantiation's layout will always be a
better choice than the definition's layout, regardless of non-type parameters.
Fixes #569
Diffstat (limited to 'src/codegen/mod.rs')
-rw-r--r-- | src/codegen/mod.rs | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 49b320ae..fe93783f 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -561,7 +561,13 @@ impl CodeGenerator for Type { let layout = self.layout(ctx).unwrap_or_else(Layout::zero); BlobTyBuilder::new(layout).build() } else { - inner_item.to_rust_ty(ctx) + let inner_rust_ty = inner_item.to_rust_ty(ctx); + if inner_rust_ty == aster::AstBuilder::new().ty().unit() { + let layout = self.layout(ctx).unwrap_or_else(|| Layout::for_size(1)); + BlobTyBuilder::new(layout).build() + } else { + inner_rust_ty + } }; { @@ -2356,12 +2362,14 @@ impl ToRustTy for TemplateInstantiation { let decl = self.template_definition(); let mut ty = decl.to_rust_ty(ctx).unwrap(); - // If we gave up when making a type for the template definition, - // check if maybe we can make a better opaque blob for the - // instantiation. if ty == aster::AstBuilder::new().ty().unit().unwrap() { + // If we gave up when making a type for the template definition, + // check if maybe we can make a better opaque blob for the + // instantiation. If not, at least don't use a zero-sized type. if let Some(layout) = self_ty.layout(ctx) { return BlobTyBuilder::new(layout).build(); + } else { + return quote_ty!(ctx.ext_cx(), u8); } } |