Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tuple-structs/variants in the rust backend? #25

Open
Gankra opened this issue Jun 30, 2024 · 0 comments
Open

Add support for tuple-structs/variants in the rust backend? #25

Gankra opened this issue Jun 30, 2024 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Gankra
Copy link
Owner

Gankra commented Jun 30, 2024

In kdl-script you can choose to omit the names of fields, as demonstrated by anons.kdl:

// Cases that allow a backend to encounter tuple-structs/tuple-variants
struct "MyTupleStruct" {
_ "bool"
_ "i32"
_ "u64"
}
tagged "MyTagged" {
TupleVariant {
_ "bool"
_ "i32"
_ "u64"
}
HybridVariant {
x "bool"
_ "u32"
}
StructVariant {
x "u8"
z "i64"
}
EmptyVariant {
}
NoneLike
}
fn "anons" {
inputs {
_ "u32"
_ "MyTupleStruct"
z "i8"
}
outputs {
_ "MyTagged"
}
}

While this is partially just a convenience for not writing stuff you don't care about, it is intended that the rust backend could lower these to tuple-structs/variants when applicable. The kdl-script compiler already caches/computes whether this is applicable to a struct/variant with all_fields_were_blank:

pub all_fields_were_blank: bool,

pub all_fields_were_blank: bool,

The main reason I didn't follow through on this is that:

  • if the difference affects ABI, you are Should Be Very Sad, so there's no "need" to implement this
    • ...but that assumption should be tested, huh
  • just tuple structs adds 3 grody branches in the code:

on struct definition:

if has_borrows {
writeln!(f, "struct {}<'a> {{", struct_ty.name)?;
} else {
writeln!(f, "#[derive(Copy, Clone)]")?;
writeln!(f, "struct {} {{", struct_ty.name)?;
}
f.add_indent(1);
for field in &struct_ty.fields {
let field_name = &field.ident;
let field_tyname = state
.borrowed_tynames
.get(&field.ty)
.unwrap_or(&state.tynames[&field.ty]);
writeln!(f, "{field_name}: {field_tyname},")?;

on struct field access:

// recurse into each field
for field in &struct_ty.fields {
let field_name = &field.ident;
let base = format!("{from}.{field_name}");
self.write_fields(f, state, to, &base, field.ty, vals)?;
}

on struct init:

write!(f, "{name} {{ ")?;
for (field_idx, field) in struct_ty.fields.iter().enumerate() {
if field_idx > 0 {
write!(f, ", ")?;
}
let field_name = &field.ident;
write!(f, "{field_name}: ")?;
let ref_temp_name = format!("{ref_temp_name}{field_name}_");
self.init_value(f, state, field.ty, vals, alias, &ref_temp_name, extra_decls)?;
}
write!(f, " }}")?;

@Gankra Gankra added enhancement New feature or request help wanted Extra attention is needed labels Jun 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant