Skip to content

Commit 300871a

Browse files
committed
ast: change tag of UnionDatatype to be a NamedType
this is much more convenient for consumers, and it reflects the requirement of the syntax to provide an Id
1 parent b7215e2 commit 300871a

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

tools/witx/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub struct StructMember {
247247

248248
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
249249
pub struct UnionDatatype {
250-
pub tag: TypeRef,
250+
pub tag: Rc<NamedType>,
251251
pub variants: Vec<UnionVariant>,
252252
}
253253

tools/witx/src/render.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,7 @@ impl StructDatatype {
206206

207207
impl UnionDatatype {
208208
pub fn to_sexpr(&self) -> SExpr {
209-
let tag_name = match self.tag {
210-
TypeRef::Name(ref tn) => &tn.name,
211-
TypeRef::Value { .. } => unreachable!("union tags must be named types"),
212-
};
213-
let header = vec![SExpr::word("union"), tag_name.to_sexpr()];
209+
let header = vec![SExpr::word("union"), self.tag.name.to_sexpr()];
214210
let variants = self
215211
.variants
216212
.iter()

tools/witx/src/validate.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl DocValidationScope<'_> {
360360
) -> Result<UnionDatatype, ValidationError> {
361361
let mut variant_scope = IdentValidation::new();
362362
let tag_id = self.get(&syntax.tag)?;
363-
let (tag, tagname, mut variant_name_uses) = match self.doc.entries.get(&tag_id) {
363+
let (tag, mut variant_name_uses) = match self.doc.entries.get(&tag_id) {
364364
Some(Entry::Typename(weak_ref)) => {
365365
let named_dt = weak_ref.upgrade().expect("weak backref to defined type");
366366
match &*named_dt.type_() {
@@ -370,8 +370,7 @@ impl DocValidationScope<'_> {
370370
.iter()
371371
.map(|v| (v.name.clone(), false))
372372
.collect::<HashMap<Id, bool>>();
373-
let name = named_dt.name.clone();
374-
Ok((TypeRef::Name(named_dt), name, uses))
373+
Ok((named_dt, uses))
375374
}
376375
other => Err(ValidationError::WrongKindName {
377376
name: syntax.tag.name().to_string(),
@@ -425,7 +424,7 @@ impl DocValidationScope<'_> {
425424
name: variant_name.name().to_string(),
426425
reason: format!(
427426
"does not correspond to variant in tag `{}`",
428-
tagname.as_str()
427+
tag.name.as_str()
429428
),
430429
location: self.location(variant_name.span()),
431430
})?,
@@ -446,7 +445,7 @@ impl DocValidationScope<'_> {
446445
.map(|i| i.as_str())
447446
.collect::<Vec<_>>()
448447
.join(", "),
449-
reason: format!("missing variants from tag `{}`", tagname.as_str()),
448+
reason: format!("missing variants from tag `{}`", tag.name.as_str()),
450449
location: self.location(span),
451450
})?;
452451
}

0 commit comments

Comments
 (0)