Skip to content

Commit

Permalink
long doubles rust-lang#2403: Get tests passing locally
Browse files Browse the repository at this point in the history
  • Loading branch information
GKFX committed Feb 5, 2023
1 parent 4a873bf commit c41408d
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 37 deletions.
5 changes: 4 additions & 1 deletion bindgen-tests/tests/expectations/tests/complex_global.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions bindgen-tests/tests/expectations/tests/convert-floats-win64.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion bindgen-tests/tests/expectations/tests/convert-floats.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion bindgen-tests/tests/expectations/tests/long_double.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions bindgen-tests/tests/headers/convert-floats-win64.h
@@ -0,0 +1,9 @@
// bindgen-flags: -- --target=x86_64-pc-windows-msvc

struct foo {
float bar, baz;
double bazz;
long double* bazzz;
float _Complex complexFloat;
double _Complex complexDouble;
};
2 changes: 1 addition & 1 deletion bindgen-tests/tests/headers/convert-floats.h
@@ -1,4 +1,4 @@
// bindgen-flags: --no-convert-floats
// bindgen-flags: --no-convert-floats -- --target=x86_64-unknown-linux-gnu

struct foo {
float bar, baz;
Expand Down
20 changes: 16 additions & 4 deletions bindgen/codegen/helpers.rs
Expand Up @@ -187,19 +187,31 @@ pub mod ast_ty {
pub fn float_kind_rust_type(
ctx: &BindgenContext,
fk: FloatKind,
layout: Option<Layout>,
mut layout: Option<Layout>,
n_parts: usize,
) -> TokenStream {
let bits = layout.map(|l| l.size * 8);
let bits = match layout {
Some(Layout { ref mut size, .. }) => {
*size /= n_parts;
Some(*size * 8)
}
None => None,
};
match (fk, ctx.options().convert_floats, bits) {
// TODO: What about narrower floats?
(_, true, Some(32)) => quote! { f32 },
(_, true, Some(64)) => quote! { f64 },
// FIXME: The (double, true, None) case is an artefact of macro parsing.
(_, true, Some(64)) | (FloatKind::Double, true, None) => {
quote! { f64 }
}
(FloatKind::Float, ..) => raw_type(ctx, "c_float"),
(FloatKind::Double, ..) => raw_type(ctx, "c_double"),
(FloatKind::LongDouble, ..) => {
// TODO(emilio): If rust ever gains f80/f128 we should
// use it here and below.
ctx.generated_bindgen_long_double(layout.expect("unknown layout for long double"));
ctx.generated_bindgen_long_double(
layout.expect("unknown layout for long double"),
);
if ctx.options().enable_cxx_namespaces {
quote! {
root::__BindgenLongDouble
Expand Down
4 changes: 2 additions & 2 deletions bindgen/codegen/mod.rs
Expand Up @@ -3741,11 +3741,11 @@ impl TryToRustTy for Type {
}
}
TypeKind::Float(fk) => {
Ok(float_kind_rust_type(ctx, fk, self.layout(ctx)))
Ok(float_kind_rust_type(ctx, fk, self.layout(ctx), 1))
}
TypeKind::Complex(fk) => {
let float_path =
float_kind_rust_type(ctx, fk, self.layout(ctx));
float_kind_rust_type(ctx, fk, self.layout(ctx), 2);

ctx.generated_bindgen_complex();
Ok(if ctx.options().enable_cxx_namespaces {
Expand Down
61 changes: 34 additions & 27 deletions bindgen/ir/analysis/never_by_value.rs
Expand Up @@ -64,7 +64,7 @@ impl<'ctx> NeverByValue<'ctx> {

fn insert<Id: Into<ItemId>>(&mut self, id: Id) -> ConstrainResult {
let id = id.into();
info!("inserting {id:?} into the never_by_value set");
trace!("inserting {id:?} into the never_by_value set");

let was_not_already_in_set = self.never_by_value.insert(id);
assert!(
Expand Down Expand Up @@ -99,10 +99,10 @@ impl<'ctx> MonotoneFramework for NeverByValue<'ctx> {
}

fn constrain(&mut self, id: ItemId) -> ConstrainResult {
info!("constrain: {id:?}");
trace!("constrain: {id:?}");

if self.never_by_value.contains(&id) {
info!(" already in set");
trace!(" already in set");
return ConstrainResult::Same;
}

Expand All @@ -123,25 +123,30 @@ impl<'ctx> MonotoneFramework for NeverByValue<'ctx> {
TypeKind::ObjCId |
TypeKind::ObjCSel |
TypeKind::BlockPointer(_) => {
info!(" simple type that is not float");
trace!(" simple type that is not float");
ConstrainResult::Same
}

TypeKind::Float(..) | TypeKind::Complex(..) => {
let size = ty
.layout(self.ctx)
.expect("float with unknown layout")
.size;
match (ty.kind(), size) {
(TypeKind::Float(..), 4 | 8) |
(TypeKind::Complex(..), 8 | 16) => {
info!(" skipped f32 or f64");
ConstrainResult::Same
}
_ => {
info!(" extended float size {size}");
self.insert(id)
if let Some(layout) = ty.layout(self.ctx) {
match (ty.kind(), layout.size) {
(TypeKind::Float(..), 4 | 8) |
(TypeKind::Complex(..), 8 | 16) => {
trace!(" skipped f32 or f64");
ConstrainResult::Same
}
_ => {
trace!(
" extended float size {}",
layout.size
);
self.insert(id)
}
}
} else {
// This case comes up with macro constants and doesn't seem relevant.
trace!(" unknown float");
ConstrainResult::Same
}
}

Expand All @@ -151,10 +156,12 @@ impl<'ctx> MonotoneFramework for NeverByValue<'ctx> {
TypeKind::TemplateAlias(t, _) |
TypeKind::Vector(t, _) => {
if self.never_by_value.contains(&t.into()) {
info!(" contains/aliases matching type, so matches");
trace!(
" contains/aliases matching type, so matches"
);
self.insert(id)
} else {
info!(" does not contain/alias matching type");
trace!(" does not contain/alias matching type");
ConstrainResult::Same
}
}
Expand All @@ -164,7 +171,7 @@ impl<'ctx> MonotoneFramework for NeverByValue<'ctx> {
self.never_by_value.contains(&base.ty.into())
});
if bases_have {
info!(" bases have float, so we also have");
trace!(" bases have float, so we also have");
return self.insert(id);
}
let fields_have = info.fields().iter().any(|f| match *f {
Expand All @@ -178,11 +185,11 @@ impl<'ctx> MonotoneFramework for NeverByValue<'ctx> {
}
});
if fields_have {
info!(" fields have float, so we also have");
trace!(" fields have float, so we also have");
return self.insert(id);
}

info!(" comp doesn't have float");
trace!(" comp doesn't have float");
ConstrainResult::Same
}

Expand All @@ -192,24 +199,24 @@ impl<'ctx> MonotoneFramework for NeverByValue<'ctx> {
.iter()
.any(|arg| self.never_by_value.contains(&arg.into()));
if args_have {
info!(" template args match, so instantiation also matches");
trace!(" template args match, so instantiation also matches");
return self.insert(id);
}

let def_has = self
.never_by_value
.contains(&template.template_definition().into());
if def_has {
info!(" template definition has float, so instantiation also has");
trace!(" template definition has float, so instantiation also has");
return self.insert(id);
}

info!(" template instantiation does not match");
trace!(" template instantiation does not match");
ConstrainResult::Same
}
}
} else {
info!(" not a type; skipped");
trace!(" not a type; skipped");
ConstrainResult::Same
}
}
Expand All @@ -220,7 +227,7 @@ impl<'ctx> MonotoneFramework for NeverByValue<'ctx> {
{
if let Some(edges) = self.dependencies.get(&id) {
for item in edges {
info!("enqueue {item:?} into worklist");
trace!("enqueue {item:?} into worklist");
f(*item);
}
}
Expand Down

0 comments on commit c41408d

Please sign in to comment.