Skip to content

Commit

Permalink
Remove the special case for empty records. (#312)
Browse files Browse the repository at this point in the history
* Remove the special case for empty records.

WebAssembly/component-model#104 is closed, so remove the corresponding
special case from wit-bindgen too.

* Update unit tests.
  • Loading branch information
sunfishcode authored Sep 13, 2022
1 parent 9ef6717 commit 346241b
Showing 1 changed file with 45 additions and 65 deletions.
110 changes: 45 additions & 65 deletions crates/wit-parser/src/mangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,14 @@ impl Interface {
}

fn mangle_recordtype(&self, fields: &[Field]) -> String {
if fields.is_empty() {
"record {}".to_owned()
} else {
format!(
"record {{ {} }}",
fields
.iter()
.map(|f| format!("{}: {}", f.name, self.mangle_valtype(f.ty)))
.collect::<Vec<_>>()
.join(", ")
)
}
format!(
"record {{ {} }}",
fields
.iter()
.map(|f| format!("{}: {}", f.name, self.mangle_valtype(f.ty)))
.collect::<Vec<_>>()
.join(", ")
)
}

fn mangle_tupletype(&self, ts: &[Type]) -> String {
Expand All @@ -106,63 +102,47 @@ impl Interface {
}

fn mangle_flags(&self, labels: &[Flag]) -> String {
if labels.is_empty() {
"flags {}".to_owned()
} else {
format!(
"flags {{ {} }}",
labels
.iter()
.map(|f| f.name.clone())
.collect::<Vec<_>>()
.join(", ")
)
}
format!(
"flags {{ {} }}",
labels
.iter()
.map(|f| f.name.clone())
.collect::<Vec<_>>()
.join(", ")
)
}

fn mangle_varianttype(&self, cases: &[Case]) -> String {
if cases.is_empty() {
"variant {}".to_owned()
} else {
format!(
"variant {{ {} }}",
cases
.iter()
.map(|c| format!("{}{}", c.name, format!("({})", self.mangle_valtype(c.ty)),))
.collect::<Vec<_>>()
.join(", ")
)
}
format!(
"variant {{ {} }}",
cases
.iter()
.map(|c| format!("{}{}", c.name, format!("({})", self.mangle_valtype(c.ty)),))
.collect::<Vec<_>>()
.join(", ")
)
}

fn mangle_enumtype(&self, labels: &[EnumCase]) -> String {
if labels.is_empty() {
"enum {}".to_owned()
} else {
format!(
"enum {{ {} }}",
labels
.iter()
.map(|l| l.name.clone())
.collect::<Vec<_>>()
.join(", ")
)
}
format!(
"enum {{ {} }}",
labels
.iter()
.map(|l| l.name.clone())
.collect::<Vec<_>>()
.join(", ")
)
}

fn mangle_uniontype(&self, cases: &[UnionCase]) -> String {
if cases.is_empty() {
"union {}".to_owned()
} else {
format!(
"union {{ {} }}",
cases
.iter()
.map(|case| self.mangle_valtype(case.ty))
.collect::<Vec<_>>()
.join(", ")
)
}
format!(
"union {{ {} }}",
cases
.iter()
.map(|case| self.mangle_valtype(case.ty))
.collect::<Vec<_>>()
.join(", ")
)
}

fn mangle_optiontype(&self, t: Type) -> String {
Expand Down Expand Up @@ -286,7 +266,7 @@ mod tests {
let iface = Interface::default();
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Record(Record { fields: Vec::new() })),
"record {}"
"record { }"
);
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Record(Record {
Expand Down Expand Up @@ -343,7 +323,7 @@ mod tests {
let iface = Interface::default();
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Flags(Flags { flags: Vec::new() })),
"flags {}"
"flags { }"
);
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Flags(Flags {
Expand Down Expand Up @@ -376,7 +356,7 @@ mod tests {
let iface = Interface::default();
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Variant(Variant { cases: Vec::new() })),
"variant {}"
"variant { }"
);
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Variant(Variant {
Expand Down Expand Up @@ -412,7 +392,7 @@ mod tests {
let iface = Interface::default();
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Enum(Enum { cases: Vec::new() })),
"enum {}"
"enum { }"
);
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Enum(Enum {
Expand Down Expand Up @@ -445,7 +425,7 @@ mod tests {
let iface = Interface::default();
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Union(Union { cases: Vec::new() })),
"union {}"
"union { }"
);
assert_eq!(
iface.mangle_valtypedef(&TypeDefKind::Union(Union {
Expand Down

0 comments on commit 346241b

Please sign in to comment.