From 3c5e93484526cf88d9fa12b86d351e849bb2fbbb Mon Sep 17 00:00:00 2001
From: Alex Crichton <alex@alexcrichton.com>
Date: Thu, 2 Feb 2023 10:39:53 -0800
Subject: [PATCH] Update to the latest `wit-parser`

This notably pulls in support in WIT for types-in-worlds.
---
 Cargo.lock                                    |  4 +-
 Cargo.toml                                    |  2 +-
 .../tests/codegen/worlds-with-types.wit       | 14 ++++++
 crates/wit-bindgen/src/lib.rs                 | 44 ++++++++++++-------
 crates/wit-bindgen/src/types.rs               |  3 ++
 supply-chain/audits.toml                      |  6 +++
 6 files changed, 53 insertions(+), 20 deletions(-)
 create mode 100644 crates/component-macro/tests/codegen/worlds-with-types.wit

diff --git a/Cargo.lock b/Cargo.lock
index cd943a6a2e76..0ca0bbf75855 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4062,9 +4062,9 @@ dependencies = [
 
 [[package]]
 name = "wit-parser"
-version = "0.4.1"
+version = "0.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2e60c4242d4cf4394fac7587c0d96c39b3c0fb09e638815c3f244f6bb5356f04"
+checksum = "896b2d88f139eb303e8e76fd72925115b11aad1944887ec2e5b2eeac1e58526f"
 dependencies = [
  "anyhow",
  "id-arena",
diff --git a/Cargo.toml b/Cargo.toml
index f02ac04fc951..3c18e714bdec 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -167,7 +167,7 @@ wasmprinter = "0.2.49"
 wasm-encoder = "0.22.0"
 wasm-smith = "0.12.0"
 wasm-mutate = "0.2.16"
-wit-parser = "0.4.1"
+wit-parser = "0.5.0"
 windows-sys = "0.42.0"
 env_logger = "0.9"
 rustix = "0.36.7"
diff --git a/crates/component-macro/tests/codegen/worlds-with-types.wit b/crates/component-macro/tests/codegen/worlds-with-types.wit
new file mode 100644
index 000000000000..25db9036da12
--- /dev/null
+++ b/crates/component-macro/tests/codegen/worlds-with-types.wit
@@ -0,0 +1,14 @@
+interface i {
+  type t = u16
+}
+
+default world foo {
+  use self.i.{t as u}
+
+  type t = u32
+
+  record r {
+  }
+
+  export f: func() -> tuple<t, u, r>
+}
diff --git a/crates/wit-bindgen/src/lib.rs b/crates/wit-bindgen/src/lib.rs
index f08011fb557e..5e7e04f0e74b 100644
--- a/crates/wit-bindgen/src/lib.rs
+++ b/crates/wit-bindgen/src/lib.rs
@@ -132,6 +132,7 @@ impl Wasmtime {
                 );
                 Import::Interface { snake }
             }
+            WorldItem::Type(_) => unreachable!(),
         };
 
         self.imports.push(import);
@@ -149,6 +150,12 @@ impl Wasmtime {
                 self.exports.funcs.push(body);
                 (format!("wasmtime::component::Func"), getter)
             }
+            WorldItem::Type(ty) => {
+                gen.define_type(name, *ty);
+                let body = mem::take(&mut gen.src);
+                self.src.push_str(&body);
+                return;
+            }
             WorldItem::Interface(id) => {
                 gen.current_interface = Some(*id);
                 gen.types(*id);
@@ -465,23 +472,26 @@ impl<'a> InterfaceGenerator<'a> {
 
     fn types(&mut self, id: InterfaceId) {
         for (name, id) in self.resolve.interfaces[id].types.iter() {
-            let id = *id;
-            let ty = &self.resolve.types[id];
-            match &ty.kind {
-                TypeDefKind::Record(record) => self.type_record(id, name, record, &ty.docs),
-                TypeDefKind::Flags(flags) => self.type_flags(id, name, flags, &ty.docs),
-                TypeDefKind::Tuple(tuple) => self.type_tuple(id, name, tuple, &ty.docs),
-                TypeDefKind::Enum(enum_) => self.type_enum(id, name, enum_, &ty.docs),
-                TypeDefKind::Variant(variant) => self.type_variant(id, name, variant, &ty.docs),
-                TypeDefKind::Option(t) => self.type_option(id, name, t, &ty.docs),
-                TypeDefKind::Result(r) => self.type_result(id, name, r, &ty.docs),
-                TypeDefKind::Union(u) => self.type_union(id, name, u, &ty.docs),
-                TypeDefKind::List(t) => self.type_list(id, name, t, &ty.docs),
-                TypeDefKind::Type(t) => self.type_alias(id, name, t, &ty.docs),
-                TypeDefKind::Future(_) => todo!("generate for future"),
-                TypeDefKind::Stream(_) => todo!("generate for stream"),
-                TypeDefKind::Unknown => unreachable!(),
-            }
+            self.define_type(name, *id);
+        }
+    }
+
+    fn define_type(&mut self, name: &str, id: TypeId) {
+        let ty = &self.resolve.types[id];
+        match &ty.kind {
+            TypeDefKind::Record(record) => self.type_record(id, name, record, &ty.docs),
+            TypeDefKind::Flags(flags) => self.type_flags(id, name, flags, &ty.docs),
+            TypeDefKind::Tuple(tuple) => self.type_tuple(id, name, tuple, &ty.docs),
+            TypeDefKind::Enum(enum_) => self.type_enum(id, name, enum_, &ty.docs),
+            TypeDefKind::Variant(variant) => self.type_variant(id, name, variant, &ty.docs),
+            TypeDefKind::Option(t) => self.type_option(id, name, t, &ty.docs),
+            TypeDefKind::Result(r) => self.type_result(id, name, r, &ty.docs),
+            TypeDefKind::Union(u) => self.type_union(id, name, u, &ty.docs),
+            TypeDefKind::List(t) => self.type_list(id, name, t, &ty.docs),
+            TypeDefKind::Type(t) => self.type_alias(id, name, t, &ty.docs),
+            TypeDefKind::Future(_) => todo!("generate for future"),
+            TypeDefKind::Stream(_) => todo!("generate for stream"),
+            TypeDefKind::Unknown => unreachable!(),
         }
     }
 
diff --git a/crates/wit-bindgen/src/types.rs b/crates/wit-bindgen/src/types.rs
index 4b2765f8621c..c4a2faeec390 100644
--- a/crates/wit-bindgen/src/types.rs
+++ b/crates/wit-bindgen/src/types.rs
@@ -55,6 +55,9 @@ impl Types {
                         self.type_info_func(resolve, f, import);
                     }
                 }
+                WorldItem::Type(id) => {
+                    self.type_id_info(resolve, *id);
+                }
             }
         }
     }
diff --git a/supply-chain/audits.toml b/supply-chain/audits.toml
index db4c3c577a8a..827fb0ec1a1d 100644
--- a/supply-chain/audits.toml
+++ b/supply-chain/audits.toml
@@ -1167,3 +1167,9 @@ criteria = "safe-to-deploy"
 version = "0.4.1"
 notes = "The Bytecode Alliance is the author of this crate."
 
+[[audits.wit-parser]]
+who = "Alex Crichton <alex@alexcrichton.com>"
+criteria = "safe-to-deploy"
+version = "0.5.0"
+notes = "The Bytecode Alliance is the author of this crate."
+