wit_parser/
resolve.rs

1use std::cmp::Ordering;
2use std::collections::hash_map;
3use std::collections::{BTreeMap, HashMap, HashSet};
4use std::fmt;
5use std::mem;
6use std::path::{Path, PathBuf};
7
8use anyhow::{Context, Result, anyhow, bail};
9use id_arena::{Arena, Id};
10use indexmap::{IndexMap, IndexSet};
11use semver::Version;
12#[cfg(feature = "serde")]
13use serde_derive::Serialize;
14
15use crate::ast::lex::Span;
16use crate::ast::{ParsedUsePath, parse_use_path};
17#[cfg(feature = "serde")]
18use crate::serde_::{serialize_arena, serialize_id_map};
19use crate::{
20    AstItem, Docs, Error, Function, FunctionKind, Handle, IncludeName, Interface, InterfaceId,
21    InterfaceSpan, LiftLowerAbi, ManglingAndAbi, PackageName, PackageNotFoundError, SourceMap,
22    Stability, Type, TypeDef, TypeDefKind, TypeId, TypeIdVisitor, TypeOwner, UnresolvedPackage,
23    UnresolvedPackageGroup, World, WorldId, WorldItem, WorldKey, WorldSpan,
24};
25
26mod clone;
27
28/// Representation of a fully resolved set of WIT packages.
29///
30/// This structure contains a graph of WIT packages and all of their contents
31/// merged together into the contained arenas. All items are sorted
32/// topologically and everything here is fully resolved, so with a `Resolve` no
33/// name lookups are necessary and instead everything is index-based.
34///
35/// Working with a WIT package requires inserting it into a `Resolve` to ensure
36/// that all of its dependencies are satisfied. This will give the full picture
37/// of that package's types and such.
38///
39/// Each item in a `Resolve` has a parent link to trace it back to the original
40/// package as necessary.
41#[derive(Default, Clone, Debug)]
42#[cfg_attr(feature = "serde", derive(Serialize))]
43pub struct Resolve {
44    /// All known worlds within this `Resolve`.
45    ///
46    /// Each world points at a `PackageId` which is stored below. No ordering is
47    /// guaranteed between this list of worlds.
48    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
49    pub worlds: Arena<World>,
50
51    /// All known interfaces within this `Resolve`.
52    ///
53    /// Each interface points at a `PackageId` which is stored below. No
54    /// ordering is guaranteed between this list of interfaces.
55    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
56    pub interfaces: Arena<Interface>,
57
58    /// All known types within this `Resolve`.
59    ///
60    /// Types are topologically sorted such that any type referenced from one
61    /// type is guaranteed to be defined previously. Otherwise though these are
62    /// not sorted by interface for example.
63    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
64    pub types: Arena<TypeDef>,
65
66    /// All known packages within this `Resolve`.
67    ///
68    /// This list of packages is not sorted. Sorted packages can be queried
69    /// through [`Resolve::topological_packages`].
70    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arena"))]
71    pub packages: Arena<Package>,
72
73    /// A map of package names to the ID of the package with that name.
74    #[cfg_attr(feature = "serde", serde(skip))]
75    pub package_names: IndexMap<PackageName, PackageId>,
76
77    /// Activated features for this [`Resolve`].
78    ///
79    /// This set of features is empty by default. This is consulted for
80    /// `@unstable` annotations in loaded WIT documents. Any items with
81    /// `@unstable` are filtered out unless their feature is present within this
82    /// set.
83    #[cfg_attr(feature = "serde", serde(skip))]
84    pub features: IndexSet<String>,
85
86    /// Activate all features for this [`Resolve`].
87    #[cfg_attr(feature = "serde", serde(skip))]
88    pub all_features: bool,
89}
90
91/// A WIT package within a `Resolve`.
92///
93/// A package is a collection of interfaces and worlds. Packages additionally
94/// have a unique identifier that affects generated components and uniquely
95/// identifiers this particular package.
96#[derive(Clone, Debug)]
97#[cfg_attr(feature = "serde", derive(Serialize))]
98pub struct Package {
99    /// A unique name corresponding to this package.
100    pub name: PackageName,
101
102    /// Documentation associated with this package.
103    #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
104    pub docs: Docs,
105
106    /// All interfaces contained in this packaged, keyed by the interface's
107    /// name.
108    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id_map"))]
109    pub interfaces: IndexMap<String, InterfaceId>,
110
111    /// All worlds contained in this package, keyed by the world's name.
112    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id_map"))]
113    pub worlds: IndexMap<String, WorldId>,
114}
115
116pub type PackageId = Id<Package>;
117
118/// All the sources used during resolving a directory or path.
119#[derive(Clone, Debug)]
120pub struct PackageSourceMap {
121    sources: Vec<Vec<PathBuf>>,
122    package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
123}
124
125impl PackageSourceMap {
126    fn from_single_source(package_id: PackageId, source: &Path) -> Self {
127        Self {
128            sources: vec![vec![source.to_path_buf()]],
129            package_id_to_source_map_idx: BTreeMap::from([(package_id, 0)]),
130        }
131    }
132
133    fn from_source_maps(
134        source_maps: Vec<SourceMap>,
135        package_id_to_source_map_idx: BTreeMap<PackageId, usize>,
136    ) -> PackageSourceMap {
137        for (package_id, idx) in &package_id_to_source_map_idx {
138            if *idx >= source_maps.len() {
139                panic!(
140                    "Invalid source map index: {}, package id: {:?}, source maps size: {}",
141                    idx,
142                    package_id,
143                    source_maps.len()
144                )
145            }
146        }
147
148        Self {
149            sources: source_maps
150                .into_iter()
151                .map(|source_map| {
152                    source_map
153                        .source_files()
154                        .map(|path| path.to_path_buf())
155                        .collect()
156                })
157                .collect(),
158            package_id_to_source_map_idx,
159        }
160    }
161
162    /// All unique source paths.
163    pub fn paths(&self) -> impl Iterator<Item = &Path> {
164        // Usually any two source map should not have duplicated source paths,
165        // but it can happen, e.g. with using [`Resolve::push_str`] directly.
166        // To be sure we use a set for deduplication here.
167        self.sources
168            .iter()
169            .flatten()
170            .map(|path_buf| path_buf.as_ref())
171            .collect::<HashSet<&Path>>()
172            .into_iter()
173    }
174
175    /// Source paths for package
176    pub fn package_paths(&self, id: PackageId) -> Option<impl Iterator<Item = &Path>> {
177        self.package_id_to_source_map_idx
178            .get(&id)
179            .map(|&idx| self.sources[idx].iter().map(|path_buf| path_buf.as_ref()))
180    }
181}
182
183enum ParsedFile {
184    #[cfg(feature = "decoding")]
185    Package(PackageId),
186    Unresolved(UnresolvedPackageGroup),
187}
188
189/// Visitor helper for performing topological sort on a group of packages.
190fn visit<'a>(
191    pkg: &'a UnresolvedPackage,
192    pkg_details_map: &'a BTreeMap<PackageName, (UnresolvedPackage, usize)>,
193    order: &mut IndexSet<PackageName>,
194    visiting: &mut HashSet<&'a PackageName>,
195    source_maps: &[SourceMap],
196) -> Result<()> {
197    if order.contains(&pkg.name) {
198        return Ok(());
199    }
200
201    match pkg_details_map.get(&pkg.name) {
202        Some(pkg_details) => {
203            let (_, source_maps_index) = pkg_details;
204            source_maps[*source_maps_index].rewrite_error(|| {
205                for (i, (dep, _)) in pkg.foreign_deps.iter().enumerate() {
206                    let span = pkg.foreign_dep_spans[i];
207                    if !visiting.insert(dep) {
208                        bail!(Error::new(span, "package depends on itself"));
209                    }
210                    if let Some(dep) = pkg_details_map.get(dep) {
211                        let (dep_pkg, _) = dep;
212                        visit(dep_pkg, pkg_details_map, order, visiting, source_maps)?;
213                    }
214                    assert!(visiting.remove(dep));
215                }
216                assert!(order.insert(pkg.name.clone()));
217                Ok(())
218            })
219        }
220        None => panic!("No pkg_details found for package when doing topological sort"),
221    }
222}
223
224impl Resolve {
225    /// Creates a new [`Resolve`] with no packages/items inside of it.
226    pub fn new() -> Resolve {
227        Resolve::default()
228    }
229
230    /// Parse WIT packages from the input `path`.
231    ///
232    /// The input `path` can be one of:
233    ///
234    /// * A directory containing a WIT package with an optional `deps` directory
235    ///   for any dependent WIT packages it references.
236    /// * A single standalone WIT file.
237    /// * A wasm-encoded WIT package as a single file in the wasm binary format.
238    /// * A wasm-encoded WIT package as a single file in the wasm text format.
239    ///
240    /// In all of these cases packages are allowed to depend on previously
241    /// inserted packages into this `Resolve`. Resolution for packages is based
242    /// on the name of each package and reference.
243    ///
244    /// This method returns a `PackageId` and additionally a `PackageSourceMap`.
245    /// The `PackageId` represent the main package that was parsed. For example if a single WIT
246    /// file was specified  this will be the main package found in the file. For a directory this
247    /// will be all the main package in the directory itself. The `PackageId` value is useful
248    /// to pass to [`Resolve::select_world`] to take a user-specified world in a
249    /// conventional fashion and select which to use for bindings generation.
250    ///
251    /// The returned [`PackageSourceMap`] contains all the sources used during this operation.
252    /// This can be useful for systems that want to rebuild or regenerate bindings based on files modified,
253    /// or for ones which like to identify the used files for a package.
254    ///
255    /// More information can also be found at [`Resolve::push_dir`] and
256    /// [`Resolve::push_file`].
257    pub fn push_path(&mut self, path: impl AsRef<Path>) -> Result<(PackageId, PackageSourceMap)> {
258        self._push_path(path.as_ref())
259    }
260
261    fn _push_path(&mut self, path: &Path) -> Result<(PackageId, PackageSourceMap)> {
262        if path.is_dir() {
263            self.push_dir(path).with_context(|| {
264                format!(
265                    "failed to resolve directory while parsing WIT for path [{}]",
266                    path.display()
267                )
268            })
269        } else {
270            let id = self.push_file(path)?;
271            Ok((id, PackageSourceMap::from_single_source(id, path)))
272        }
273    }
274
275    fn sort_unresolved_packages(
276        &mut self,
277        main: UnresolvedPackageGroup,
278        deps: Vec<UnresolvedPackageGroup>,
279    ) -> Result<(PackageId, PackageSourceMap)> {
280        let mut pkg_details_map = BTreeMap::new();
281        let mut source_maps = Vec::new();
282
283        let mut insert = |group: UnresolvedPackageGroup| {
284            let UnresolvedPackageGroup {
285                main,
286                nested,
287                source_map,
288            } = group;
289            let i = source_maps.len();
290            source_maps.push(source_map);
291
292            for pkg in nested.into_iter().chain([main]) {
293                let name = pkg.name.clone();
294                let my_span = pkg.package_name_span;
295                let (prev_pkg, prev_i) = match pkg_details_map.insert(name.clone(), (pkg, i)) {
296                    Some(pair) => pair,
297                    None => continue,
298                };
299                let loc1 = source_maps[i].render_location(my_span);
300                let loc2 = source_maps[prev_i].render_location(prev_pkg.package_name_span);
301                bail!(
302                    "\
303package {name} is defined in two different locations:\n\
304  * {loc1}\n\
305  * {loc2}\n\
306                     "
307                )
308            }
309            Ok(())
310        };
311
312        let main_name = main.main.name.clone();
313        insert(main)?;
314        for dep in deps {
315            insert(dep)?;
316        }
317
318        // Perform a simple topological sort which will bail out on cycles
319        // and otherwise determine the order that packages must be added to
320        // this `Resolve`.
321        let mut order = IndexSet::new();
322        let mut visiting = HashSet::new();
323        for pkg_details in pkg_details_map.values() {
324            let (pkg, _) = pkg_details;
325            visit(
326                pkg,
327                &pkg_details_map,
328                &mut order,
329                &mut visiting,
330                &source_maps,
331            )?;
332        }
333
334        // Ensure that the final output is topologically sorted. Use a set to ensure that we render
335        // the buffers for each `SourceMap` only once, even though multiple packages may reference
336        // the same `SourceMap`.
337        let mut package_id_to_source_map_idx = BTreeMap::new();
338        let mut main_pkg_id = None;
339        for name in order {
340            let (pkg, source_map_index) = pkg_details_map.remove(&name).unwrap();
341            let source_map = &source_maps[source_map_index];
342            let is_main = pkg.name == main_name;
343            let id = self.push(pkg, source_map)?;
344            if is_main {
345                assert!(main_pkg_id.is_none());
346                main_pkg_id = Some(id);
347            }
348            package_id_to_source_map_idx.insert(id, source_map_index);
349        }
350
351        Ok((
352            main_pkg_id.unwrap(),
353            PackageSourceMap::from_source_maps(source_maps, package_id_to_source_map_idx),
354        ))
355    }
356
357    /// Parses the filesystem directory at `path` as a WIT package and returns
358    /// a fully resolved [`PackageId`] list as a result.
359    ///
360    /// The directory itself is parsed with [`UnresolvedPackageGroup::parse_dir`]
361    /// and then all packages found are inserted into this `Resolve`. The `path`
362    /// specified may have a `deps` subdirectory which is probed automatically
363    /// for any other WIT dependencies.
364    ///
365    /// The `deps` folder may contain:
366    ///
367    /// * `$path/deps/my-package/*.wit` - a directory that may contain multiple
368    ///   WIT files. This is parsed with [`UnresolvedPackageGroup::parse_dir`]
369    ///   and then inserted into this [`Resolve`]. Note that cannot recursively
370    ///   contain a `deps` directory.
371    /// * `$path/deps/my-package.wit` - a single-file WIT package. This is
372    ///   parsed with [`Resolve::push_file`] and then added to `self` for
373    ///   name resolution.
374    /// * `$path/deps/my-package.{wasm,wat}` - a wasm-encoded WIT package either
375    ///   in the text for binary format.
376    ///
377    /// In all cases entries in the `deps` folder are added to `self` first
378    /// before adding files found in `path` itself. All WIT packages found are
379    /// candidates for name-based resolution that other packages may use.
380    ///
381    /// This function returns a tuple of two values. The first value is a
382    /// [`PackageId`], which represents the main WIT package found within
383    /// `path`. This argument is useful for passing to [`Resolve::select_world`]
384    /// for choosing something to bindgen with.
385    ///
386    /// The second value returned is a [`PackageSourceMap`], which contains all the sources
387    /// that were parsed during resolving. This can be useful for:
388    /// * build systems that want to rebuild bindings whenever one of the files changed
389    /// * or other tools, which want to identify the sources for the resolved packages
390    pub fn push_dir(&mut self, path: impl AsRef<Path>) -> Result<(PackageId, PackageSourceMap)> {
391        self._push_dir(path.as_ref())
392    }
393
394    fn _push_dir(&mut self, path: &Path) -> Result<(PackageId, PackageSourceMap)> {
395        let top_pkg = UnresolvedPackageGroup::parse_dir(path)
396            .with_context(|| format!("failed to parse package: {}", path.display()))?;
397        let deps = path.join("deps");
398        let deps = self
399            .parse_deps_dir(&deps)
400            .with_context(|| format!("failed to parse dependency directory: {}", deps.display()))?;
401
402        self.sort_unresolved_packages(top_pkg, deps)
403    }
404
405    fn parse_deps_dir(&mut self, path: &Path) -> Result<Vec<UnresolvedPackageGroup>> {
406        let mut ret = Vec::new();
407        if !path.exists() {
408            return Ok(ret);
409        }
410        let mut entries = path
411            .read_dir()
412            .and_then(|i| i.collect::<std::io::Result<Vec<_>>>())
413            .context("failed to read directory")?;
414        entries.sort_by_key(|e| e.file_name());
415        for dep in entries {
416            let path = dep.path();
417            let pkg = if dep.file_type()?.is_dir() || path.metadata()?.is_dir() {
418                // If this entry is a directory or a symlink point to a
419                // directory then always parse it as an `UnresolvedPackage`
420                // since it's intentional to not support recursive `deps`
421                // directories.
422                UnresolvedPackageGroup::parse_dir(&path)
423                    .with_context(|| format!("failed to parse package: {}", path.display()))?
424            } else {
425                // If this entry is a file then we may want to ignore it but
426                // this may also be a standalone WIT file or a `*.wasm` or
427                // `*.wat` encoded package.
428                let filename = dep.file_name();
429                match Path::new(&filename).extension().and_then(|s| s.to_str()) {
430                    Some("wit") | Some("wat") | Some("wasm") => match self._push_file(&path)? {
431                        #[cfg(feature = "decoding")]
432                        ParsedFile::Package(_) => continue,
433                        ParsedFile::Unresolved(pkg) => pkg,
434                    },
435
436                    // Other files in deps dir are ignored for now to avoid
437                    // accidentally including things like `.DS_Store` files in
438                    // the call below to `parse_dir`.
439                    _ => continue,
440                }
441            };
442            ret.push(pkg);
443        }
444        Ok(ret)
445    }
446
447    /// Parses the contents of `path` from the filesystem and pushes the result
448    /// into this `Resolve`.
449    ///
450    /// The `path` referenced here can be one of:
451    ///
452    /// * A WIT file. Note that in this case this single WIT file will be the
453    ///   entire package and any dependencies it has must already be in `self`.
454    /// * A WIT package encoded as WebAssembly, either in text or binary form.
455    ///   In this the package and all of its dependencies are automatically
456    ///   inserted into `self`.
457    ///
458    /// In both situations the `PackageId`s of the resulting resolved packages
459    /// are returned from this method. The return value is mostly useful in
460    /// conjunction with [`Resolve::select_world`].
461    pub fn push_file(&mut self, path: impl AsRef<Path>) -> Result<PackageId> {
462        match self._push_file(path.as_ref())? {
463            #[cfg(feature = "decoding")]
464            ParsedFile::Package(id) => Ok(id),
465            ParsedFile::Unresolved(pkg) => self.push_group(pkg),
466        }
467    }
468
469    fn _push_file(&mut self, path: &Path) -> Result<ParsedFile> {
470        let contents = std::fs::read(path)
471            .with_context(|| format!("failed to read path for WIT [{}]", path.display()))?;
472
473        // If decoding is enabled at compile time then try to see if this is a
474        // wasm file.
475        #[cfg(feature = "decoding")]
476        {
477            use crate::decoding::{DecodedWasm, decode};
478
479            #[cfg(feature = "wat")]
480            let is_wasm = wat::Detect::from_bytes(&contents).is_wasm();
481            #[cfg(not(feature = "wat"))]
482            let is_wasm = wasmparser::Parser::is_component(&contents);
483
484            if is_wasm {
485                #[cfg(feature = "wat")]
486                let contents = wat::parse_bytes(&contents).map_err(|mut e| {
487                    e.set_path(path);
488                    e
489                })?;
490
491                match decode(&contents)? {
492                    DecodedWasm::Component(..) => {
493                        bail!("found an actual component instead of an encoded WIT package in wasm")
494                    }
495                    DecodedWasm::WitPackage(resolve, pkg) => {
496                        let remap = self.merge(resolve)?;
497                        return Ok(ParsedFile::Package(remap.packages[pkg.index()]));
498                    }
499                }
500            }
501        }
502
503        // If this wasn't a wasm file then assume it's a WIT file.
504        let text = match std::str::from_utf8(&contents) {
505            Ok(s) => s,
506            Err(_) => bail!("input file is not valid utf-8 [{}]", path.display()),
507        };
508        let pkgs = UnresolvedPackageGroup::parse(path, text)?;
509        Ok(ParsedFile::Unresolved(pkgs))
510    }
511
512    /// Appends a new [`UnresolvedPackage`] to this [`Resolve`], creating a
513    /// fully resolved package with no dangling references.
514    ///
515    /// All the dependencies of `unresolved` must already have been loaded
516    /// within this `Resolve` via previous calls to `push` or other methods such
517    /// as [`Resolve::push_path`].
518    ///
519    /// Any dependency resolution error or otherwise world-elaboration error
520    /// will be returned here, if successful a package identifier is returned
521    /// which corresponds to the package that was just inserted.
522    pub fn push(
523        &mut self,
524        unresolved: UnresolvedPackage,
525        source_map: &SourceMap,
526    ) -> Result<PackageId> {
527        let ret = source_map.rewrite_error(|| Remap::default().append(self, unresolved));
528        if ret.is_ok() {
529            #[cfg(debug_assertions)]
530            self.assert_valid();
531        }
532        ret
533    }
534
535    /// Appends new [`UnresolvedPackageGroup`] to this [`Resolve`], creating a
536    /// fully resolved package with no dangling references.
537    ///
538    /// Any dependency resolution error or otherwise world-elaboration error
539    /// will be returned here, if successful a package identifier is returned
540    /// which corresponds to the package that was just inserted.
541    ///
542    /// The returned [`PackageId`]s are listed in topologically sorted order.
543    pub fn push_group(&mut self, unresolved_group: UnresolvedPackageGroup) -> Result<PackageId> {
544        let (pkg_id, _) = self.sort_unresolved_packages(unresolved_group, Vec::new())?;
545        Ok(pkg_id)
546    }
547
548    /// Convenience method for combining [`UnresolvedPackageGroup::parse`] and
549    /// [`Resolve::push_group`].
550    ///
551    /// The `path` provided is used for error messages but otherwise is not
552    /// read. This method does not touch the filesystem. The `contents` provided
553    /// are the contents of a WIT package.
554    pub fn push_str(&mut self, path: impl AsRef<Path>, contents: &str) -> Result<PackageId> {
555        self.push_group(UnresolvedPackageGroup::parse(path.as_ref(), contents)?)
556    }
557
558    pub fn all_bits_valid(&self, ty: &Type) -> bool {
559        match ty {
560            Type::U8
561            | Type::S8
562            | Type::U16
563            | Type::S16
564            | Type::U32
565            | Type::S32
566            | Type::U64
567            | Type::S64
568            | Type::F32
569            | Type::F64 => true,
570
571            Type::Bool | Type::Char | Type::String | Type::ErrorContext => false,
572
573            Type::Id(id) => match &self.types[*id].kind {
574                TypeDefKind::List(_)
575                | TypeDefKind::Variant(_)
576                | TypeDefKind::Enum(_)
577                | TypeDefKind::Option(_)
578                | TypeDefKind::Result(_)
579                | TypeDefKind::Future(_)
580                | TypeDefKind::Stream(_) => false,
581                TypeDefKind::Type(t) | TypeDefKind::FixedSizeList(t, ..) => self.all_bits_valid(t),
582
583                TypeDefKind::Handle(h) => match h {
584                    crate::Handle::Own(_) => true,
585                    crate::Handle::Borrow(_) => true,
586                },
587
588                TypeDefKind::Resource => false,
589                TypeDefKind::Record(r) => r.fields.iter().all(|f| self.all_bits_valid(&f.ty)),
590                TypeDefKind::Tuple(t) => t.types.iter().all(|t| self.all_bits_valid(t)),
591
592                // FIXME: this could perhaps be `true` for multiples-of-32 but
593                // seems better to probably leave this as unconditionally
594                // `false` for now, may want to reconsider later?
595                TypeDefKind::Flags(_) => false,
596
597                TypeDefKind::Unknown => unreachable!(),
598            },
599        }
600    }
601
602    /// Merges all the contents of a different `Resolve` into this one. The
603    /// `Remap` structure returned provides a mapping from all old indices to
604    /// new indices
605    ///
606    /// This operation can fail if `resolve` disagrees with `self` about the
607    /// packages being inserted. Otherwise though this will additionally attempt
608    /// to "union" packages found in `resolve` with those found in `self`.
609    /// Unioning packages is keyed on the name/url of packages for those with
610    /// URLs present. If found then it's assumed that both `Resolve` instances
611    /// were originally created from the same contents and are two views
612    /// of the same package.
613    pub fn merge(&mut self, resolve: Resolve) -> Result<Remap> {
614        log::trace!(
615            "merging {} packages into {} packages",
616            resolve.packages.len(),
617            self.packages.len()
618        );
619
620        let mut map = MergeMap::new(&resolve, &self);
621        map.build()?;
622        let MergeMap {
623            package_map,
624            interface_map,
625            type_map,
626            world_map,
627            interfaces_to_add,
628            worlds_to_add,
629            ..
630        } = map;
631
632        // With a set of maps from ids in `resolve` to ids in `self` the next
633        // operation is to start moving over items and building a `Remap` to
634        // update ids.
635        //
636        // Each component field of `resolve` is moved into `self` so long as
637        // its ID is not within one of the maps above. If it's present in a map
638        // above then that means the item is already present in `self` so a new
639        // one need not be added. If it's not present in a map that means it's
640        // not present in `self` so it must be added to an arena.
641        //
642        // When adding an item to an arena one of the `remap.update_*` methods
643        // is additionally called to update all identifiers from pointers within
644        // `resolve` to becoming pointers within `self`.
645        //
646        // Altogether this should weave all the missing items in `self` from
647        // `resolve` into one structure while updating all identifiers to
648        // be local within `self`.
649
650        let mut remap = Remap::default();
651        let Resolve {
652            types,
653            worlds,
654            interfaces,
655            packages,
656            package_names,
657            features: _,
658            ..
659        } = resolve;
660
661        let mut moved_types = Vec::new();
662        for (id, mut ty) in types {
663            let new_id = match type_map.get(&id).copied() {
664                Some(id) => {
665                    update_stability(&ty.stability, &mut self.types[id].stability)?;
666                    id
667                }
668                None => {
669                    log::debug!("moving type {:?}", ty.name);
670                    moved_types.push(id);
671                    remap.update_typedef(self, &mut ty, None)?;
672                    self.types.alloc(ty)
673                }
674            };
675            assert_eq!(remap.types.len(), id.index());
676            remap.types.push(Some(new_id));
677        }
678
679        let mut moved_interfaces = Vec::new();
680        for (id, mut iface) in interfaces {
681            let new_id = match interface_map.get(&id).copied() {
682                Some(id) => {
683                    update_stability(&iface.stability, &mut self.interfaces[id].stability)?;
684                    id
685                }
686                None => {
687                    log::debug!("moving interface {:?}", iface.name);
688                    moved_interfaces.push(id);
689                    remap.update_interface(self, &mut iface, None)?;
690                    self.interfaces.alloc(iface)
691                }
692            };
693            assert_eq!(remap.interfaces.len(), id.index());
694            remap.interfaces.push(Some(new_id));
695        }
696
697        let mut moved_worlds = Vec::new();
698        for (id, mut world) in worlds {
699            let new_id = match world_map.get(&id).copied() {
700                Some(world_id) => {
701                    update_stability(&world.stability, &mut self.worlds[world_id].stability)?;
702                    for from_import in world.imports.iter() {
703                        Resolve::update_world_imports_stability(
704                            from_import,
705                            &mut self.worlds[world_id].imports,
706                            &interface_map,
707                        )?;
708                    }
709                    for from_export in world.exports.iter() {
710                        Resolve::update_world_imports_stability(
711                            from_export,
712                            &mut self.worlds[world_id].exports,
713                            &interface_map,
714                        )?;
715                    }
716                    world_id
717                }
718                None => {
719                    log::debug!("moving world {}", world.name);
720                    moved_worlds.push(id);
721                    let mut update = |map: &mut IndexMap<WorldKey, WorldItem>| -> Result<_> {
722                        for (mut name, mut item) in mem::take(map) {
723                            remap.update_world_key(&mut name, None)?;
724                            match &mut item {
725                                WorldItem::Function(f) => remap.update_function(self, f, None)?,
726                                WorldItem::Interface { id, .. } => {
727                                    *id = remap.map_interface(*id, None)?
728                                }
729                                WorldItem::Type(i) => *i = remap.map_type(*i, None)?,
730                            }
731                            map.insert(name, item);
732                        }
733                        Ok(())
734                    };
735                    update(&mut world.imports)?;
736                    update(&mut world.exports)?;
737                    self.worlds.alloc(world)
738                }
739            };
740            assert_eq!(remap.worlds.len(), id.index());
741            remap.worlds.push(Some(new_id));
742        }
743
744        for (id, mut pkg) in packages {
745            let new_id = match package_map.get(&id).copied() {
746                Some(id) => id,
747                None => {
748                    for (_, id) in pkg.interfaces.iter_mut() {
749                        *id = remap.map_interface(*id, None)?;
750                    }
751                    for (_, id) in pkg.worlds.iter_mut() {
752                        *id = remap.map_world(*id, None)?;
753                    }
754                    self.packages.alloc(pkg)
755                }
756            };
757            assert_eq!(remap.packages.len(), id.index());
758            remap.packages.push(new_id);
759        }
760
761        for (name, id) in package_names {
762            let id = remap.packages[id.index()];
763            if let Some(prev) = self.package_names.insert(name, id) {
764                assert_eq!(prev, id);
765            }
766        }
767
768        // Fixup all "parent" links now.
769        //
770        // Note that this is only done for items that are actually moved from
771        // `resolve` into `self`, which is tracked by the various `moved_*`
772        // lists built incrementally above. The ids in the `moved_*` lists
773        // are ids within `resolve`, so they're translated through `remap` to
774        // ids within `self`.
775        for id in moved_worlds {
776            let id = remap.map_world(id, None)?;
777            if let Some(pkg) = self.worlds[id].package.as_mut() {
778                *pkg = remap.packages[pkg.index()];
779            }
780        }
781        for id in moved_interfaces {
782            let id = remap.map_interface(id, None)?;
783            if let Some(pkg) = self.interfaces[id].package.as_mut() {
784                *pkg = remap.packages[pkg.index()];
785            }
786        }
787        for id in moved_types {
788            let id = remap.map_type(id, None)?;
789            match &mut self.types[id].owner {
790                TypeOwner::Interface(id) => *id = remap.map_interface(*id, None)?,
791                TypeOwner::World(id) => *id = remap.map_world(*id, None)?,
792                TypeOwner::None => {}
793            }
794        }
795
796        // And finally process items that were present in `resolve` but were
797        // not present in `self`. This is only done for merged packages as
798        // documents may be added to `self.documents` but wouldn't otherwise be
799        // present in the `documents` field of the corresponding package.
800        for (name, pkg, iface) in interfaces_to_add {
801            let prev = self.packages[pkg]
802                .interfaces
803                .insert(name, remap.map_interface(iface, None)?);
804            assert!(prev.is_none());
805        }
806        for (name, pkg, world) in worlds_to_add {
807            let prev = self.packages[pkg]
808                .worlds
809                .insert(name, remap.map_world(world, None)?);
810            assert!(prev.is_none());
811        }
812
813        log::trace!("now have {} packages", self.packages.len());
814
815        #[cfg(debug_assertions)]
816        self.assert_valid();
817        Ok(remap)
818    }
819
820    fn update_world_imports_stability(
821        from_item: (&WorldKey, &WorldItem),
822        into_items: &mut IndexMap<WorldKey, WorldItem>,
823        interface_map: &HashMap<Id<Interface>, Id<Interface>>,
824    ) -> Result<()> {
825        match from_item.0 {
826            WorldKey::Name(_) => {
827                // No stability info to update here, only updating import/include stability
828                Ok(())
829            }
830            key @ WorldKey::Interface(_) => {
831                let new_key = MergeMap::map_name(key, interface_map);
832                if let Some(into) = into_items.get_mut(&new_key) {
833                    match (from_item.1, into) {
834                        (
835                            WorldItem::Interface {
836                                id: aid,
837                                stability: astability,
838                            },
839                            WorldItem::Interface {
840                                id: bid,
841                                stability: bstability,
842                            },
843                        ) => {
844                            let aid = interface_map.get(aid).copied().unwrap_or(*aid);
845                            assert_eq!(aid, *bid);
846                            update_stability(astability, bstability)?;
847                            Ok(())
848                        }
849                        _ => unreachable!(),
850                    }
851                } else {
852                    // we've already matched all the imports/exports by the time we are calling this
853                    // so this is unreachable since we should always find the item
854                    unreachable!()
855                }
856            }
857        }
858    }
859
860    /// Merges the world `from` into the world `into`.
861    ///
862    /// This will attempt to merge one world into another, unioning all of its
863    /// imports and exports together. This is an operation performed by
864    /// `wit-component`, for example where two different worlds from two
865    /// different libraries were linked into the same core wasm file and are
866    /// producing a singular world that will be the final component's
867    /// interface.
868    ///
869    /// This operation can fail if the imports/exports overlap.
870    pub fn merge_worlds(&mut self, from: WorldId, into: WorldId) -> Result<()> {
871        let mut new_imports = Vec::new();
872        let mut new_exports = Vec::new();
873
874        let from_world = &self.worlds[from];
875        let into_world = &self.worlds[into];
876
877        log::trace!("merging {} into {}", from_world.name, into_world.name);
878
879        // First walk over all the imports of `from` world and figure out what
880        // to do with them.
881        //
882        // If the same item exists in `from` and `into` then merge it together
883        // below with `merge_world_item` which basically asserts they're the
884        // same. Otherwise queue up a new import since if `from` has more
885        // imports than `into` then it's fine to add new imports.
886        for (name, from_import) in from_world.imports.iter() {
887            let name_str = self.name_world_key(name);
888            match into_world.imports.get(name) {
889                Some(into_import) => {
890                    log::trace!("info/from shared import on `{name_str}`");
891                    self.merge_world_item(from_import, into_import)
892                        .with_context(|| format!("failed to merge world import {name_str}"))?;
893                }
894                None => {
895                    log::trace!("new import: `{name_str}`");
896                    new_imports.push((name.clone(), from_import.clone()));
897                }
898            }
899        }
900
901        // Build a set of interfaces which are required to be imported because
902        // of `into`'s exports. This set is then used below during
903        // `ensure_can_add_world_export`.
904        //
905        // This is the set of interfaces which exports depend on that are
906        // themselves not exports.
907        let mut must_be_imported = HashMap::new();
908        for (key, export) in into_world.exports.iter() {
909            for dep in self.world_item_direct_deps(export) {
910                if into_world.exports.contains_key(&WorldKey::Interface(dep)) {
911                    continue;
912                }
913                self.foreach_interface_dep(dep, &mut |id| {
914                    must_be_imported.insert(id, key.clone());
915                });
916            }
917        }
918
919        // Next walk over exports of `from` and process these similarly to
920        // imports.
921        for (name, from_export) in from_world.exports.iter() {
922            let name_str = self.name_world_key(name);
923            match into_world.exports.get(name) {
924                Some(into_export) => {
925                    log::trace!("info/from shared export on `{name_str}`");
926                    self.merge_world_item(from_export, into_export)
927                        .with_context(|| format!("failed to merge world export {name_str}"))?;
928                }
929                None => {
930                    log::trace!("new export `{name_str}`");
931                    // See comments in `ensure_can_add_world_export` for why
932                    // this is slightly different than imports.
933                    self.ensure_can_add_world_export(
934                        into_world,
935                        name,
936                        from_export,
937                        &must_be_imported,
938                    )
939                    .with_context(|| {
940                        format!("failed to add export `{}`", self.name_world_key(name))
941                    })?;
942                    new_exports.push((name.clone(), from_export.clone()));
943                }
944            }
945        }
946
947        // For all the new imports and exports they may need to be "cloned" to
948        // be able to belong to the new world. For example:
949        //
950        // * Anonymous interfaces have a `package` field which points to the
951        //   package of the containing world, but `from` and `into` may not be
952        //   in the same package.
953        //
954        // * Type imports have an `owner` field that point to `from`, but they
955        //   now need to point to `into` instead.
956        //
957        // Cloning is no trivial task, however, so cloning is delegated to a
958        // submodule to perform a "deep" clone and copy items into new arena
959        // entries as necessary.
960        let mut cloner = clone::Cloner::new(self, TypeOwner::World(from), TypeOwner::World(into));
961        cloner.register_world_type_overlap(from, into);
962        for (name, item) in new_imports.iter_mut().chain(&mut new_exports) {
963            cloner.world_item(name, item);
964        }
965
966        // Insert any new imports and new exports found first.
967        let into_world = &mut self.worlds[into];
968        for (name, import) in new_imports {
969            let prev = into_world.imports.insert(name, import);
970            assert!(prev.is_none());
971        }
972        for (name, export) in new_exports {
973            let prev = into_world.exports.insert(name, export);
974            assert!(prev.is_none());
975        }
976
977        #[cfg(debug_assertions)]
978        self.assert_valid();
979        Ok(())
980    }
981
982    fn merge_world_item(&self, from: &WorldItem, into: &WorldItem) -> Result<()> {
983        let mut map = MergeMap::new(self, self);
984        match (from, into) {
985            (WorldItem::Interface { id: from, .. }, WorldItem::Interface { id: into, .. }) => {
986                // If these imports are the same that can happen, for
987                // example, when both worlds to `import foo:bar/baz;`. That
988                // foreign interface will point to the same interface within
989                // `Resolve`.
990                if from == into {
991                    return Ok(());
992                }
993
994                // .. otherwise this MUST be a case of
995                // `import foo: interface { ... }`. If `from != into` but
996                // both `from` and `into` have the same name then the
997                // `WorldKey::Interface` case is ruled out as otherwise
998                // they'd have different names.
999                //
1000                // In the case of an anonymous interface all we can do is
1001                // ensure that the interfaces both match, so use `MergeMap`
1002                // for that.
1003                map.build_interface(*from, *into)
1004                    .context("failed to merge interfaces")?;
1005            }
1006
1007            // Like `WorldKey::Name` interfaces for functions and types the
1008            // structure is asserted to be the same.
1009            (WorldItem::Function(from), WorldItem::Function(into)) => {
1010                map.build_function(from, into)
1011                    .context("failed to merge functions")?;
1012            }
1013            (WorldItem::Type(from), WorldItem::Type(into)) => {
1014                map.build_type_id(*from, *into)
1015                    .context("failed to merge types")?;
1016            }
1017
1018            // Kind-level mismatches are caught here.
1019            (WorldItem::Interface { .. }, _)
1020            | (WorldItem::Function { .. }, _)
1021            | (WorldItem::Type { .. }, _) => {
1022                bail!("different kinds of items");
1023            }
1024        }
1025        assert!(map.interfaces_to_add.is_empty());
1026        assert!(map.worlds_to_add.is_empty());
1027        Ok(())
1028    }
1029
1030    /// This method ensures that the world export of `name` and `item` can be
1031    /// added to the world `into` without changing the meaning of `into`.
1032    ///
1033    /// All dependencies of world exports must either be:
1034    ///
1035    /// * An export themselves
1036    /// * An import with all transitive dependencies of the import also imported
1037    ///
1038    /// It's not possible to depend on an import which then also depends on an
1039    /// export at some point, for example. This method ensures that if `name`
1040    /// and `item` are added that this property is upheld.
1041    fn ensure_can_add_world_export(
1042        &self,
1043        into: &World,
1044        name: &WorldKey,
1045        item: &WorldItem,
1046        must_be_imported: &HashMap<InterfaceId, WorldKey>,
1047    ) -> Result<()> {
1048        assert!(!into.exports.contains_key(name));
1049        let name = self.name_world_key(name);
1050
1051        // First make sure that all of this item's dependencies are either
1052        // exported or the entire chain of imports rooted at that dependency are
1053        // all imported.
1054        for dep in self.world_item_direct_deps(item) {
1055            if into.exports.contains_key(&WorldKey::Interface(dep)) {
1056                continue;
1057            }
1058            self.ensure_not_exported(into, dep)
1059                .with_context(|| format!("failed validating export of `{name}`"))?;
1060        }
1061
1062        // Second make sure that this item, if it's an interface, will not alter
1063        // the meaning of the preexisting world by ensuring that it's not in the
1064        // set of "must be imported" items.
1065        if let WorldItem::Interface { id, .. } = item {
1066            if let Some(export) = must_be_imported.get(&id) {
1067                let export_name = self.name_world_key(export);
1068                bail!(
1069                    "export `{export_name}` depends on `{name}` \
1070                     previously as an import which will change meaning \
1071                     if `{name}` is added as an export"
1072                );
1073            }
1074        }
1075
1076        Ok(())
1077    }
1078
1079    fn ensure_not_exported(&self, world: &World, id: InterfaceId) -> Result<()> {
1080        let key = WorldKey::Interface(id);
1081        let name = self.name_world_key(&key);
1082        if world.exports.contains_key(&key) {
1083            bail!(
1084                "world exports `{name}` but it's also transitively used by an \
1085                     import \
1086                   which means that this is not valid"
1087            )
1088        }
1089        for dep in self.interface_direct_deps(id) {
1090            self.ensure_not_exported(world, dep)
1091                .with_context(|| format!("failed validating transitive import dep `{name}`"))?;
1092        }
1093        Ok(())
1094    }
1095
1096    /// Returns an iterator of all the direct interface dependencies of this
1097    /// `item`.
1098    ///
1099    /// Note that this doesn't include transitive dependencies, that must be
1100    /// followed manually.
1101    fn world_item_direct_deps(&self, item: &WorldItem) -> impl Iterator<Item = InterfaceId> + '_ {
1102        let mut interface = None;
1103        let mut ty = None;
1104        match item {
1105            WorldItem::Function(_) => {}
1106            WorldItem::Type(id) => ty = Some(*id),
1107            WorldItem::Interface { id, .. } => interface = Some(*id),
1108        }
1109
1110        interface
1111            .into_iter()
1112            .flat_map(move |id| self.interface_direct_deps(id))
1113            .chain(ty.and_then(|t| self.type_interface_dep(t)))
1114    }
1115
1116    /// Invokes `f` with `id` and all transitive interface dependencies of `id`.
1117    ///
1118    /// Note that `f` may be called with the same id multiple times.
1119    fn foreach_interface_dep(&self, id: InterfaceId, f: &mut dyn FnMut(InterfaceId)) {
1120        self._foreach_interface_dep(id, f, &mut HashSet::new())
1121    }
1122
1123    // Internal detail of `foreach_interface_dep` which uses a hash map to prune
1124    // the visit tree to ensure that this doesn't visit an exponential number of
1125    // interfaces.
1126    fn _foreach_interface_dep(
1127        &self,
1128        id: InterfaceId,
1129        f: &mut dyn FnMut(InterfaceId),
1130        visited: &mut HashSet<InterfaceId>,
1131    ) {
1132        if !visited.insert(id) {
1133            return;
1134        }
1135        f(id);
1136        for dep in self.interface_direct_deps(id) {
1137            self._foreach_interface_dep(dep, f, visited);
1138        }
1139    }
1140
1141    /// Returns the ID of the specified `interface`.
1142    ///
1143    /// Returns `None` for unnamed interfaces.
1144    pub fn id_of(&self, interface: InterfaceId) -> Option<String> {
1145        let interface = &self.interfaces[interface];
1146        Some(self.id_of_name(interface.package.unwrap(), interface.name.as_ref()?))
1147    }
1148
1149    /// Returns the "canonicalized interface name" of `interface`.
1150    ///
1151    /// Returns `None` for unnamed interfaces. See `BuildTargets.md` in the
1152    /// upstream component model repository for more information about this.
1153    pub fn canonicalized_id_of(&self, interface: InterfaceId) -> Option<String> {
1154        let interface = &self.interfaces[interface];
1155        Some(self.canonicalized_id_of_name(interface.package.unwrap(), interface.name.as_ref()?))
1156    }
1157
1158    /// Convert a world to an "importized" version where the world is updated
1159    /// in-place to reflect what it would look like to be imported.
1160    ///
1161    /// This is a transformation which is used as part of the process of
1162    /// importing a component today. For example when a component depends on
1163    /// another component this is useful for generating WIT which can be use to
1164    /// represent the component being imported. The general idea is that this
1165    /// function will update the `world_id` specified such it imports the
1166    /// functionality that it previously exported. The world will be left with
1167    /// no exports.
1168    ///
1169    /// This world is then suitable for merging into other worlds or generating
1170    /// bindings in a context that is importing the original world. This
1171    /// is intended to be used as part of language tooling when depending on
1172    /// other components.
1173    pub fn importize(&mut self, world_id: WorldId, out_world_name: Option<String>) -> Result<()> {
1174        // Rename the world to avoid having it get confused with the original
1175        // name of the world. Add `-importized` to it for now. Precisely how
1176        // this new world is created may want to be updated over time if this
1177        // becomes problematic.
1178        let world = &mut self.worlds[world_id];
1179        let pkg = &mut self.packages[world.package.unwrap()];
1180        pkg.worlds.shift_remove(&world.name);
1181        if let Some(name) = out_world_name {
1182            world.name = name.clone();
1183            pkg.worlds.insert(name, world_id);
1184        } else {
1185            world.name.push_str("-importized");
1186            pkg.worlds.insert(world.name.clone(), world_id);
1187        }
1188
1189        // Trim all non-type definitions from imports. Types can be used by
1190        // exported functions, for example, so they're preserved.
1191        world.imports.retain(|_, item| match item {
1192            WorldItem::Type(_) => true,
1193            _ => false,
1194        });
1195
1196        for (name, export) in mem::take(&mut world.exports) {
1197            match (name.clone(), world.imports.insert(name, export)) {
1198                // no previous item? this insertion was ok
1199                (_, None) => {}
1200
1201                // cannot overwrite an import with an export
1202                (WorldKey::Name(name), Some(_)) => {
1203                    bail!("world export `{name}` conflicts with import of same name");
1204                }
1205
1206                // Exports already don't overlap each other and the only imports
1207                // preserved above were types so this shouldn't be reachable.
1208                (WorldKey::Interface(_), _) => unreachable!(),
1209            }
1210        }
1211
1212        // Fill out any missing transitive interface imports by elaborating this
1213        // world which does that for us.
1214        self.elaborate_world(world_id)?;
1215
1216        #[cfg(debug_assertions)]
1217        self.assert_valid();
1218        Ok(())
1219    }
1220
1221    /// Returns the ID of the specified `name` within the `pkg`.
1222    pub fn id_of_name(&self, pkg: PackageId, name: &str) -> String {
1223        let package = &self.packages[pkg];
1224        let mut base = String::new();
1225        base.push_str(&package.name.namespace);
1226        base.push_str(":");
1227        base.push_str(&package.name.name);
1228        base.push_str("/");
1229        base.push_str(name);
1230        if let Some(version) = &package.name.version {
1231            base.push_str(&format!("@{version}"));
1232        }
1233        base
1234    }
1235
1236    /// Returns the "canonicalized interface name" of the specified `name`
1237    /// within the `pkg`.
1238    ///
1239    /// See `BuildTargets.md` in the upstream component model repository for
1240    /// more information about this.
1241    pub fn canonicalized_id_of_name(&self, pkg: PackageId, name: &str) -> String {
1242        let package = &self.packages[pkg];
1243        let mut base = String::new();
1244        base.push_str(&package.name.namespace);
1245        base.push_str(":");
1246        base.push_str(&package.name.name);
1247        base.push_str("/");
1248        base.push_str(name);
1249        if let Some(version) = &package.name.version {
1250            base.push_str("@");
1251            let string = PackageName::version_compat_track_string(version);
1252            base.push_str(&string);
1253        }
1254        base
1255    }
1256
1257    /// Selects a world from among the packages in a `Resolve`.
1258    ///
1259    /// A `Resolve` may have many packages, each with many worlds. Many WIT
1260    /// tools need a specific world to operate on. This function choses a
1261    /// world, failing if the choice is ambiguous.
1262    ///
1263    /// `main_packages` provides the package IDs returned by
1264    /// [`push_path`](Resolve::push_path), [`push_dir`](Resolve::push_dir),
1265    /// [`push_file`](Resolve::push_file), [`push_group`](Resolve::push_group),
1266    /// and [`push_str`](Resolve::push_str), which are the "main packages",
1267    /// as distinguished from any packages nested inside them.
1268    ///
1269    /// `world` is a world name such as from a `--world` command-line option or
1270    /// a `world:` macro parameter. `world` can be:
1271    ///
1272    /// * A kebab-name of a world, for example `"the-world"`. It is resolved
1273    ///   within the "main package", if there is exactly one.
1274    ///
1275    /// * An ID-based form of a world, for example `"wasi:http/proxy"`. Note
1276    ///   that a version does not need to be specified in this string if
1277    ///   there's only one package of the same name and it has a version. In
1278    ///   this situation the version can be omitted.
1279    ///
1280    /// * `None`. If there's exactly one "main package" and it contains exactly
1281    ///   one world, that world is chosen.
1282    ///
1283    /// If successful, the chosen `WorldId` is returned.
1284    ///
1285    /// # Examples
1286    ///
1287    /// ```
1288    /// use anyhow::Result;
1289    /// use wit_parser::Resolve;
1290    ///
1291    /// fn main() -> Result<()> {
1292    ///     let mut resolve = Resolve::default();
1293    ///
1294    ///     // If there's a single package and only one world, that world is
1295    ///     // the obvious choice.
1296    ///     let wit1 = resolve.push_str(
1297    ///         "./my-test.wit",
1298    ///         r#"
1299    ///             package example:wit1;
1300    ///
1301    ///             world foo {
1302    ///                 // ...
1303    ///             }
1304    ///         "#,
1305    ///     )?;
1306    ///     assert!(resolve.select_world(&[wit1], None).is_ok());
1307    ///
1308    ///     // If there are multiple packages, we need to be told which package
1309    ///     // to use, either by a "main package" or by a fully-qualified name.
1310    ///     let wit2 = resolve.push_str(
1311    ///         "./my-test.wit",
1312    ///         r#"
1313    ///             package example:wit2;
1314    ///
1315    ///             world foo { /* ... */ }
1316    ///         "#,
1317    ///     )?;
1318    ///     assert!(resolve.select_world(&[wit1, wit2], None).is_err());
1319    ///     assert!(resolve.select_world(&[wit1, wit2], Some("foo")).is_err());
1320    ///     // Fix: use fully-qualified names.
1321    ///     assert!(resolve.select_world(&[wit1, wit2], Some("example:wit1/foo")).is_ok());
1322    ///     assert!(resolve.select_world(&[wit1, wit2], Some("example:wit2/foo")).is_ok());
1323    ///
1324    ///     // If a package has multiple worlds, then we can't guess the world
1325    ///     // even if we know the package.
1326    ///     let wit3 = resolve.push_str(
1327    ///         "./my-test.wit",
1328    ///         r#"
1329    ///             package example:wit3;
1330    ///
1331    ///             world foo { /* ... */ }
1332    ///
1333    ///             world bar { /* ... */ }
1334    ///         "#,
1335    ///     )?;
1336    ///     assert!(resolve.select_world(&[wit3], None).is_err());
1337    ///     // Fix: pick between "foo" and "bar" here.
1338    ///     assert!(resolve.select_world(&[wit3], Some("foo")).is_ok());
1339    ///
1340    ///     // When selecting with a version it's ok to drop the version when
1341    ///     // there's only a single copy of that package in `Resolve`.
1342    ///     let wit5_1 = resolve.push_str(
1343    ///         "./my-test.wit",
1344    ///         r#"
1345    ///             package example:wit5@1.0.0;
1346    ///
1347    ///             world foo { /* ... */ }
1348    ///         "#,
1349    ///     )?;
1350    ///     assert!(resolve.select_world(&[wit5_1], Some("foo")).is_ok());
1351    ///     assert!(resolve.select_world(&[wit5_1], Some("example:wit5/foo")).is_ok());
1352    ///
1353    ///     // However when a single package has multiple versions in a resolve
1354    ///     // it's required to specify the version to select which one.
1355    ///     let wit5_2 = resolve.push_str(
1356    ///         "./my-test.wit",
1357    ///         r#"
1358    ///             package example:wit5@2.0.0;
1359    ///
1360    ///             world foo { /* ... */ }
1361    ///         "#,
1362    ///     )?;
1363    ///     assert!(resolve.select_world(&[wit5_1, wit5_2], Some("example:wit5/foo")).is_err());
1364    ///     // Fix: Pass explicit versions.
1365    ///     assert!(resolve.select_world(&[wit5_1, wit5_2], Some("example:wit5/foo@1.0.0")).is_ok());
1366    ///     assert!(resolve.select_world(&[wit5_1, wit5_2], Some("example:wit5/foo@2.0.0")).is_ok());
1367    ///
1368    ///     Ok(())
1369    /// }
1370    /// ```
1371    pub fn select_world(
1372        &self,
1373        main_packages: &[PackageId],
1374        world: Option<&str>,
1375    ) -> Result<WorldId> {
1376        // Determine if `world` is a kebab-name or an ID.
1377        let world_path = match world {
1378            Some(world) => Some(
1379                parse_use_path(world)
1380                    .with_context(|| format!("failed to parse world specifier `{world}`"))?,
1381            ),
1382            None => None,
1383        };
1384
1385        match world_path {
1386            // We have a world path. If needed, pick a package to resolve it in.
1387            Some(world_path) => {
1388                let (pkg, world_name) = match (main_packages, world_path) {
1389                    // We have no main packages; fail.
1390                    ([], _) => bail!("No main packages defined"),
1391
1392                    // We have exactly one main package.
1393                    ([main_package], ParsedUsePath::Name(name)) => (*main_package, name),
1394
1395                    // We have more than one main package; fail.
1396                    (_, ParsedUsePath::Name(_name)) => {
1397                        bail!(
1398                            "There are multiple main packages; a world must be explicitly chosen:{}",
1399                            self.worlds
1400                                .iter()
1401                                .map(|world| format!(
1402                                    "\n  {}",
1403                                    self.id_of_name(world.1.package.unwrap(), &world.1.name)
1404                                ))
1405                                .collect::<String>()
1406                        )
1407                    }
1408
1409                    // The world name is fully-qualified.
1410                    (_, ParsedUsePath::Package(pkg, world_name)) => {
1411                        let pkg = match self.package_names.get(&pkg) {
1412                            Some(pkg) => *pkg,
1413                            None => {
1414                                let mut candidates =
1415                                    self.package_names.iter().filter(|(name, _)| {
1416                                        pkg.version.is_none()
1417                                            && pkg.name == name.name
1418                                            && pkg.namespace == name.namespace
1419                                            && name.version.is_some()
1420                                    });
1421                                let candidate = candidates.next();
1422                                if let Some((c2, _)) = candidates.next() {
1423                                    let (c1, _) = candidate.unwrap();
1424                                    bail!(
1425                                        "package name `{pkg}` is available at both \
1426                                    versions {} and {} but which is not specified",
1427                                        c1.version.as_ref().unwrap(),
1428                                        c2.version.as_ref().unwrap(),
1429                                    );
1430                                }
1431                                match candidate {
1432                                    Some((_, id)) => *id,
1433                                    None => bail!("unknown package `{pkg}`"),
1434                                }
1435                            }
1436                        };
1437                        (pkg, world_name.to_string())
1438                    }
1439                };
1440
1441                // Now that we've picked the package, resolve the world name.
1442                let pkg = &self.packages[pkg];
1443                pkg.worlds.get(&world_name).copied().ok_or_else(|| {
1444                    anyhow!("World `{world_name}` not found in package `{}`", pkg.name)
1445                })
1446            }
1447
1448            // With no specified `world`, try to find a single obvious world.
1449            None => match main_packages {
1450                [] => bail!("No main packages defined"),
1451
1452                // Check for exactly one main package with exactly one world.
1453                [main_package] => {
1454                    let pkg = &self.packages[*main_package];
1455                    match pkg.worlds.len() {
1456                        0 => bail!("The main package `{}` contains no worlds", pkg.name),
1457                        1 => Ok(pkg.worlds[0]),
1458                        _ => bail!(
1459                            "There are multiple worlds in `{}`; one must be explicitly chosen:{}",
1460                            pkg.name,
1461                            pkg.worlds
1462                                .values()
1463                                .map(|world| format!(
1464                                    "\n  {}",
1465                                    self.id_of_name(*main_package, &self.worlds[*world].name)
1466                                ))
1467                                .collect::<String>()
1468                        ),
1469                    }
1470                }
1471
1472                // Multiple main packages and no world name; fail.
1473                _ => {
1474                    bail!(
1475                        "There are multiple main packages; a world must be explicitly chosen:{}",
1476                        self.worlds
1477                            .iter()
1478                            .map(|world| format!(
1479                                "\n  {}",
1480                                self.id_of_name(world.1.package.unwrap(), &world.1.name)
1481                            ))
1482                            .collect::<String>()
1483                    )
1484                }
1485            },
1486        }
1487    }
1488
1489    /// Assigns a human readable name to the `WorldKey` specified.
1490    pub fn name_world_key(&self, key: &WorldKey) -> String {
1491        match key {
1492            WorldKey::Name(s) => s.to_string(),
1493            WorldKey::Interface(i) => self.id_of(*i).expect("unexpected anonymous interface"),
1494        }
1495    }
1496
1497    /// Same as [`Resolve::name_world_key`] except that `WorldKey::Interfaces`
1498    /// uses [`Resolve::canonicalized_id_of`].
1499    pub fn name_canonicalized_world_key(&self, key: &WorldKey) -> String {
1500        match key {
1501            WorldKey::Name(s) => s.to_string(),
1502            WorldKey::Interface(i) => self
1503                .canonicalized_id_of(*i)
1504                .expect("unexpected anonymous interface"),
1505        }
1506    }
1507
1508    /// Returns the interface that `id` uses a type from, if it uses a type from
1509    /// a different interface than `id` is defined within.
1510    ///
1511    /// If `id` is not a use-of-a-type or it's using a type in the same
1512    /// interface then `None` is returned.
1513    pub fn type_interface_dep(&self, id: TypeId) -> Option<InterfaceId> {
1514        let ty = &self.types[id];
1515        let dep = match ty.kind {
1516            TypeDefKind::Type(Type::Id(id)) => id,
1517            _ => return None,
1518        };
1519        let other = &self.types[dep];
1520        if ty.owner == other.owner {
1521            None
1522        } else {
1523            match other.owner {
1524                TypeOwner::Interface(id) => Some(id),
1525                _ => unreachable!(),
1526            }
1527        }
1528    }
1529
1530    /// Returns an iterator of all interfaces that the interface `id` depends
1531    /// on.
1532    ///
1533    /// Interfaces may depend on others for type information to resolve type
1534    /// imports.
1535    ///
1536    /// Note that the returned iterator may yield the same interface as a
1537    /// dependency multiple times. Additionally only direct dependencies of `id`
1538    /// are yielded, not transitive dependencies.
1539    pub fn interface_direct_deps(&self, id: InterfaceId) -> impl Iterator<Item = InterfaceId> + '_ {
1540        self.interfaces[id]
1541            .types
1542            .iter()
1543            .filter_map(move |(_name, ty)| self.type_interface_dep(*ty))
1544    }
1545
1546    /// Returns an iterator of all packages that the package `id` depends
1547    /// on.
1548    ///
1549    /// Packages may depend on others for type information to resolve type
1550    /// imports or interfaces to resolve worlds.
1551    ///
1552    /// Note that the returned iterator may yield the same package as a
1553    /// dependency multiple times. Additionally only direct dependencies of `id`
1554    /// are yielded, not transitive dependencies.
1555    pub fn package_direct_deps(&self, id: PackageId) -> impl Iterator<Item = PackageId> + '_ {
1556        let pkg = &self.packages[id];
1557
1558        pkg.interfaces
1559            .iter()
1560            .flat_map(move |(_name, id)| self.interface_direct_deps(*id))
1561            .chain(pkg.worlds.iter().flat_map(move |(_name, id)| {
1562                let world = &self.worlds[*id];
1563                world
1564                    .imports
1565                    .iter()
1566                    .chain(world.exports.iter())
1567                    .filter_map(move |(_name, item)| match item {
1568                        WorldItem::Interface { id, .. } => Some(*id),
1569                        WorldItem::Function(_) => None,
1570                        WorldItem::Type(t) => self.type_interface_dep(*t),
1571                    })
1572            }))
1573            .filter_map(move |iface_id| {
1574                let pkg = self.interfaces[iface_id].package?;
1575                if pkg == id { None } else { Some(pkg) }
1576            })
1577    }
1578
1579    /// Returns a topological ordering of packages contained in this `Resolve`.
1580    ///
1581    /// This returns a list of `PackageId` such that when visited in order it's
1582    /// guaranteed that all dependencies will have been defined by prior items
1583    /// in the list.
1584    pub fn topological_packages(&self) -> Vec<PackageId> {
1585        let mut pushed = vec![false; self.packages.len()];
1586        let mut order = Vec::new();
1587        for (id, _) in self.packages.iter() {
1588            self.build_topological_package_ordering(id, &mut pushed, &mut order);
1589        }
1590        order
1591    }
1592
1593    fn build_topological_package_ordering(
1594        &self,
1595        id: PackageId,
1596        pushed: &mut Vec<bool>,
1597        order: &mut Vec<PackageId>,
1598    ) {
1599        if pushed[id.index()] {
1600            return;
1601        }
1602        for dep in self.package_direct_deps(id) {
1603            self.build_topological_package_ordering(dep, pushed, order);
1604        }
1605        order.push(id);
1606        pushed[id.index()] = true;
1607    }
1608
1609    #[doc(hidden)]
1610    pub fn assert_valid(&self) {
1611        let mut package_interfaces = Vec::new();
1612        let mut package_worlds = Vec::new();
1613        for (id, pkg) in self.packages.iter() {
1614            let mut interfaces = HashSet::new();
1615            for (name, iface) in pkg.interfaces.iter() {
1616                assert!(interfaces.insert(*iface));
1617                let iface = &self.interfaces[*iface];
1618                assert_eq!(name, iface.name.as_ref().unwrap());
1619                assert_eq!(iface.package.unwrap(), id);
1620            }
1621            package_interfaces.push(pkg.interfaces.values().copied().collect::<HashSet<_>>());
1622            let mut worlds = HashSet::new();
1623            for (name, world) in pkg.worlds.iter() {
1624                assert!(worlds.insert(*world));
1625                assert_eq!(
1626                    pkg.worlds.get_key_value(name),
1627                    Some((name, world)),
1628                    "`MutableKeys` impl may have been used to change a key's hash or equality"
1629                );
1630                let world = &self.worlds[*world];
1631                assert_eq!(*name, world.name);
1632                assert_eq!(world.package.unwrap(), id);
1633            }
1634            package_worlds.push(pkg.worlds.values().copied().collect::<HashSet<_>>());
1635        }
1636
1637        let mut interface_types = Vec::new();
1638        for (id, iface) in self.interfaces.iter() {
1639            assert!(self.packages.get(iface.package.unwrap()).is_some());
1640            if iface.name.is_some() {
1641                assert!(package_interfaces[iface.package.unwrap().index()].contains(&id));
1642            }
1643
1644            for (name, ty) in iface.types.iter() {
1645                let ty = &self.types[*ty];
1646                assert_eq!(ty.name.as_ref(), Some(name));
1647                assert_eq!(ty.owner, TypeOwner::Interface(id));
1648            }
1649            interface_types.push(iface.types.values().copied().collect::<HashSet<_>>());
1650            for (name, f) in iface.functions.iter() {
1651                assert_eq!(*name, f.name);
1652            }
1653        }
1654
1655        let mut world_types = Vec::new();
1656        for (id, world) in self.worlds.iter() {
1657            log::debug!("validating world {}", &world.name);
1658            if let Some(package) = world.package {
1659                assert!(self.packages.get(package).is_some());
1660                assert!(package_worlds[package.index()].contains(&id));
1661            }
1662            assert!(world.includes.is_empty());
1663
1664            let mut types = HashSet::new();
1665            for (name, item) in world.imports.iter().chain(world.exports.iter()) {
1666                log::debug!("validating world item: {}", self.name_world_key(name));
1667                match item {
1668                    WorldItem::Interface { id, .. } => {
1669                        // anonymous interfaces must belong to the same package
1670                        // as the world's package.
1671                        if matches!(name, WorldKey::Name(_)) {
1672                            assert_eq!(self.interfaces[*id].package, world.package);
1673                        }
1674                    }
1675                    WorldItem::Function(f) => {
1676                        assert!(!matches!(name, WorldKey::Interface(_)));
1677                        assert_eq!(f.name, name.clone().unwrap_name());
1678                    }
1679                    WorldItem::Type(ty) => {
1680                        assert!(!matches!(name, WorldKey::Interface(_)));
1681                        assert!(types.insert(*ty));
1682                        let ty = &self.types[*ty];
1683                        assert_eq!(ty.name, Some(name.clone().unwrap_name()));
1684                        assert_eq!(ty.owner, TypeOwner::World(id));
1685                    }
1686                }
1687            }
1688            self.assert_world_elaborated(world);
1689            world_types.push(types);
1690        }
1691
1692        for (ty_id, ty) in self.types.iter() {
1693            match ty.owner {
1694                TypeOwner::Interface(id) => {
1695                    assert!(self.interfaces.get(id).is_some());
1696                    assert!(interface_types[id.index()].contains(&ty_id));
1697                }
1698                TypeOwner::World(id) => {
1699                    assert!(self.worlds.get(id).is_some());
1700                    assert!(world_types[id.index()].contains(&ty_id));
1701                }
1702                TypeOwner::None => {}
1703            }
1704        }
1705
1706        self.assert_topologically_sorted();
1707    }
1708
1709    fn assert_topologically_sorted(&self) {
1710        let mut positions = IndexMap::new();
1711        for id in self.topological_packages() {
1712            let pkg = &self.packages[id];
1713            log::debug!("pkg {}", pkg.name);
1714            let prev = positions.insert(Some(id), IndexSet::new());
1715            assert!(prev.is_none());
1716        }
1717        positions.insert(None, IndexSet::new());
1718
1719        for (id, iface) in self.interfaces.iter() {
1720            log::debug!("iface {:?}", iface.name);
1721            let ok = positions.get_mut(&iface.package).unwrap().insert(id);
1722            assert!(ok);
1723        }
1724
1725        for (_, world) in self.worlds.iter() {
1726            log::debug!("world {:?}", world.name);
1727
1728            let my_package = world.package;
1729            let my_package_pos = positions.get_index_of(&my_package).unwrap();
1730
1731            for (_, item) in world.imports.iter().chain(&world.exports) {
1732                let id = match item {
1733                    WorldItem::Interface { id, .. } => *id,
1734                    _ => continue,
1735                };
1736                let other_package = self.interfaces[id].package;
1737                let other_package_pos = positions.get_index_of(&other_package).unwrap();
1738
1739                assert!(other_package_pos <= my_package_pos);
1740            }
1741        }
1742
1743        for (_id, ty) in self.types.iter() {
1744            log::debug!("type {:?} {:?}", ty.name, ty.owner);
1745            let other_id = match ty.kind {
1746                TypeDefKind::Type(Type::Id(ty)) => ty,
1747                _ => continue,
1748            };
1749            let other = &self.types[other_id];
1750            if ty.kind == other.kind {
1751                continue;
1752            }
1753            let my_interface = match ty.owner {
1754                TypeOwner::Interface(id) => id,
1755                _ => continue,
1756            };
1757            let other_interface = match other.owner {
1758                TypeOwner::Interface(id) => id,
1759                _ => continue,
1760            };
1761
1762            let my_package = self.interfaces[my_interface].package;
1763            let other_package = self.interfaces[other_interface].package;
1764            let my_package_pos = positions.get_index_of(&my_package).unwrap();
1765            let other_package_pos = positions.get_index_of(&other_package).unwrap();
1766
1767            if my_package_pos == other_package_pos {
1768                let interfaces = &positions[&my_package];
1769                let my_interface_pos = interfaces.get_index_of(&my_interface).unwrap();
1770                let other_interface_pos = interfaces.get_index_of(&other_interface).unwrap();
1771                assert!(other_interface_pos <= my_interface_pos);
1772            } else {
1773                assert!(other_package_pos < my_package_pos);
1774            }
1775        }
1776    }
1777
1778    fn assert_world_elaborated(&self, world: &World) {
1779        for (key, item) in world.imports.iter() {
1780            log::debug!(
1781                "asserting elaborated world import {}",
1782                self.name_world_key(key)
1783            );
1784            match item {
1785                WorldItem::Type(t) => self.assert_world_imports_type_deps(world, key, *t),
1786
1787                // All types referred to must be imported.
1788                WorldItem::Function(f) => self.assert_world_function_imports_types(world, key, f),
1789
1790                // All direct dependencies of this interface must be imported.
1791                WorldItem::Interface { id, .. } => {
1792                    for dep in self.interface_direct_deps(*id) {
1793                        assert!(
1794                            world.imports.contains_key(&WorldKey::Interface(dep)),
1795                            "world import of {} is missing transitive dep of {}",
1796                            self.name_world_key(key),
1797                            self.id_of(dep).unwrap(),
1798                        );
1799                    }
1800                }
1801            }
1802        }
1803        for (key, item) in world.exports.iter() {
1804            log::debug!(
1805                "asserting elaborated world export {}",
1806                self.name_world_key(key)
1807            );
1808            match item {
1809                // Types referred to by this function must be imported.
1810                WorldItem::Function(f) => self.assert_world_function_imports_types(world, key, f),
1811
1812                // Dependencies of exported interfaces must also be exported, or
1813                // if imported then that entire chain of imports must be
1814                // imported and not exported.
1815                WorldItem::Interface { id, .. } => {
1816                    for dep in self.interface_direct_deps(*id) {
1817                        let dep_key = WorldKey::Interface(dep);
1818                        if world.exports.contains_key(&dep_key) {
1819                            continue;
1820                        }
1821                        self.foreach_interface_dep(dep, &mut |dep| {
1822                            let dep_key = WorldKey::Interface(dep);
1823                            assert!(
1824                                world.imports.contains_key(&dep_key),
1825                                "world should import {} (required by {})",
1826                                self.name_world_key(&dep_key),
1827                                self.name_world_key(key),
1828                            );
1829                            assert!(
1830                                !world.exports.contains_key(&dep_key),
1831                                "world should not export {} (required by {})",
1832                                self.name_world_key(&dep_key),
1833                                self.name_world_key(key),
1834                            );
1835                        });
1836                    }
1837                }
1838
1839                // exported types not allowed at this time
1840                WorldItem::Type(_) => unreachable!(),
1841            }
1842        }
1843    }
1844
1845    fn assert_world_imports_type_deps(&self, world: &World, key: &WorldKey, ty: TypeId) {
1846        // If this is a `use` statement then the referred-to interface must be
1847        // imported into this world.
1848        let ty = &self.types[ty];
1849        if let TypeDefKind::Type(Type::Id(other)) = ty.kind {
1850            if let TypeOwner::Interface(id) = self.types[other].owner {
1851                let key = WorldKey::Interface(id);
1852                assert!(world.imports.contains_key(&key));
1853                return;
1854            }
1855        }
1856
1857        // ... otherwise any named type that this type refers to, one level
1858        // deep, must be imported into this world under that name.
1859
1860        let mut visitor = MyVisit(self, Vec::new());
1861        visitor.visit_type_def(self, ty);
1862        for ty in visitor.1 {
1863            let ty = &self.types[ty];
1864            let Some(name) = ty.name.clone() else {
1865                continue;
1866            };
1867            let dep_key = WorldKey::Name(name);
1868            assert!(
1869                world.imports.contains_key(&dep_key),
1870                "world import `{}` should also force an import of `{}`",
1871                self.name_world_key(key),
1872                self.name_world_key(&dep_key),
1873            );
1874        }
1875
1876        struct MyVisit<'a>(&'a Resolve, Vec<TypeId>);
1877
1878        impl TypeIdVisitor for MyVisit<'_> {
1879            fn before_visit_type_id(&mut self, id: TypeId) -> bool {
1880                self.1.push(id);
1881                // recurse into unnamed types to look at all named types
1882                self.0.types[id].name.is_none()
1883            }
1884        }
1885    }
1886
1887    /// This asserts that all types referred to by `func` are imported into
1888    /// `world` under `WorldKey::Name`. Note that this is only applicable to
1889    /// named type
1890    fn assert_world_function_imports_types(&self, world: &World, key: &WorldKey, func: &Function) {
1891        for ty in func
1892            .parameter_and_result_types()
1893            .chain(func.kind.resource().map(Type::Id))
1894        {
1895            let Type::Id(id) = ty else {
1896                continue;
1897            };
1898            self.assert_world_imports_type_deps(world, key, id);
1899        }
1900    }
1901
1902    /// Returns whether the `stability` annotation contained within `pkg_id`
1903    /// should be included or not.
1904    ///
1905    /// The `span` provided here is an optional span pointing to the item that
1906    /// is annotated with `stability`.
1907    ///
1908    /// Returns `Ok(true)` if the item is included, or `Ok(false)` if the item
1909    /// is not.
1910    ///
1911    /// # Errors
1912    ///
1913    /// Returns an error if the `pkg_id` isn't annotated with sufficient version
1914    /// information to have a `stability` annotation. For example if `pkg_id`
1915    /// has no version listed then an error will be returned if `stability`
1916    /// mentions a version.
1917    fn include_stability(
1918        &self,
1919        stability: &Stability,
1920        pkg_id: &PackageId,
1921        span: Option<Span>,
1922    ) -> Result<bool> {
1923        let err = |msg: String| match span {
1924            Some(span) => Error::new(span, msg).into(),
1925            None => anyhow::Error::msg(msg),
1926        };
1927        Ok(match stability {
1928            Stability::Unknown => true,
1929            // NOTE: deprecations are intentionally omitted -- an existing
1930            // `@since` takes precedence over `@deprecated`
1931            Stability::Stable { since, .. } => {
1932                let Some(p) = self.packages.get(*pkg_id) else {
1933                    // We can't check much without a package (possibly dealing
1934                    // with an item in an `UnresolvedPackage`), @since version &
1935                    // deprecations can't be checked because there's no package
1936                    // version to compare to.
1937                    //
1938                    // Feature requirements on stabilized features are ignored
1939                    // in resolved packages, so we do the same here.
1940                    return Ok(true);
1941                };
1942
1943                // Use of feature gating with version specifiers inside a
1944                // package that is not versioned is not allowed
1945                let package_version = p.name.version.as_ref().ok_or_else(|| {
1946                    err(format!(
1947                        "package [{}] contains a feature gate with a version \
1948                         specifier, so it must have a version",
1949                        p.name
1950                    ))
1951                })?;
1952
1953                // If the version on the feature gate is:
1954                // - released, then we can include it
1955                // - unreleased, then we must check the feature (if present)
1956                if since > package_version {
1957                    return Err(err(format!(
1958                        "feature gate cannot reference unreleased version \
1959                        {since} of package [{}] (current version {package_version})",
1960                        p.name
1961                    )));
1962                }
1963
1964                true
1965            }
1966            Stability::Unstable { feature, .. } => {
1967                self.features.contains(feature) || self.all_features
1968            }
1969        })
1970    }
1971
1972    /// Convenience wrapper around `include_stability` specialized for types
1973    /// with a more targeted error message.
1974    fn include_type(&self, ty: &TypeDef, pkgid: PackageId, span: Span) -> Result<bool> {
1975        self.include_stability(&ty.stability, &pkgid, Some(span))
1976            .with_context(|| {
1977                format!(
1978                    "failed to process feature gate for type [{}] in package [{}]",
1979                    ty.name.as_ref().map(String::as_str).unwrap_or("<unknown>"),
1980                    self.packages[pkgid].name,
1981                )
1982            })
1983    }
1984
1985    /// Performs the "elaboration process" necessary for the `world_id`
1986    /// specified to ensure that all of its transitive imports are listed.
1987    ///
1988    /// This function will take the unordered lists of the specified world's
1989    /// imports and exports and "elaborate" them to ensure that they're
1990    /// topographically sorted where all transitively required interfaces by
1991    /// imports, or exports, are listed. This will additionally validate that
1992    /// the exports are all valid and present, specifically with the restriction
1993    /// noted on `elaborate_world_exports`.
1994    ///
1995    /// The world is mutated in-place in this `Resolve`.
1996    fn elaborate_world(&mut self, world_id: WorldId) -> Result<()> {
1997        // First process all imports. This is easier than exports since the only
1998        // requirement here is that all interfaces need to be added with a
1999        // topological order between them.
2000        let mut new_imports = IndexMap::new();
2001        let world = &self.worlds[world_id];
2002
2003        // Sort the imports by "class" to ensure that this matches the order
2004        // that items are printed and that items are in topological order.
2005        //
2006        // When printing worlds in WIT:
2007        //
2008        // * interfaces come first
2009        // * types are next
2010        //   * type imports are first
2011        //   * type definitions are next
2012        //   * resource definitions have methods printed inline
2013        // * freestanding functions are last
2014        //
2015        // This reflects the topological order between items where types
2016        // can refer to imports and functions can refer to these types. Ordering
2017        // within a single class (e.g. imports depending on each other, types
2018        // referring to each other) is already preserved by other passes in this
2019        // file and general AST resolution. That means that a stable sort here
2020        // can be used to ensure that each class is in the right location
2021        // relative to the others.
2022        //
2023        // Overall this ensures that round-trips of WIT through wasm should
2024        // always produce the same result.
2025        let sort_key = |resolve: &Resolve, item: &WorldItem| match item {
2026            WorldItem::Interface { .. } => 0,
2027            WorldItem::Type(ty) => {
2028                let ty = &resolve.types[*ty];
2029                match ty.kind {
2030                    TypeDefKind::Type(Type::Id(t)) if resolve.types[t].owner != ty.owner => 1,
2031                    _ => 2,
2032                }
2033            }
2034            WorldItem::Function(f) => {
2035                if f.kind.resource().is_none() {
2036                    3
2037                } else {
2038                    4
2039                }
2040            }
2041        };
2042
2043        // Sort world items when we start to elaborate the world to start with a
2044        // topological view of items.
2045        let mut world_imports = world.imports.iter().collect::<Vec<_>>();
2046        world_imports.sort_by_key(|(_name, import)| sort_key(self, import));
2047        for (name, item) in world_imports {
2048            match item {
2049                // Interfaces get their dependencies added first followed by the
2050                // interface itself.
2051                WorldItem::Interface { id, stability } => {
2052                    self.elaborate_world_import(&mut new_imports, name.clone(), *id, &stability);
2053                }
2054
2055                // Functions are added as-is since their dependence on types in
2056                // the world should already be satisfied.
2057                WorldItem::Function(_) => {
2058                    let prev = new_imports.insert(name.clone(), item.clone());
2059                    assert!(prev.is_none());
2060                }
2061
2062                // Types may depend on an interface, in which case a (possibly)
2063                // recursive addition of that interface happens here. Afterwards
2064                // the type itself can be added safely.
2065                WorldItem::Type(id) => {
2066                    if let Some(dep) = self.type_interface_dep(*id) {
2067                        self.elaborate_world_import(
2068                            &mut new_imports,
2069                            WorldKey::Interface(dep),
2070                            dep,
2071                            &self.types[*id].stability,
2072                        );
2073                    }
2074                    let prev = new_imports.insert(name.clone(), item.clone());
2075                    assert!(prev.is_none());
2076                }
2077            }
2078        }
2079
2080        // Exports are trickier than imports, notably to uphold the invariant
2081        // required by `elaborate_world_exports`. To do this the exports are
2082        // partitioned into interfaces/functions. All functions are added to
2083        // the new exports list during this loop but interfaces are all deferred
2084        // to be handled in the `elaborate_world_exports` function.
2085        let mut new_exports = IndexMap::new();
2086        let mut export_interfaces = IndexMap::new();
2087        for (name, item) in world.exports.iter() {
2088            match item {
2089                WorldItem::Interface { id, stability } => {
2090                    let prev = export_interfaces.insert(*id, (name.clone(), stability));
2091                    assert!(prev.is_none());
2092                }
2093                WorldItem::Function(_) => {
2094                    let prev = new_exports.insert(name.clone(), item.clone());
2095                    assert!(prev.is_none());
2096                }
2097                WorldItem::Type(_) => unreachable!(),
2098            }
2099        }
2100
2101        self.elaborate_world_exports(&export_interfaces, &mut new_imports, &mut new_exports)?;
2102
2103        // In addition to sorting at the start of elaboration also sort here at
2104        // the end of elaboration to handle types being interspersed with
2105        // interfaces as they're found.
2106        new_imports.sort_by_cached_key(|_name, import| sort_key(self, import));
2107
2108        // And with all that done the world is updated in-place with
2109        // imports/exports.
2110        log::trace!("imports = {new_imports:?}");
2111        log::trace!("exports = {new_exports:?}");
2112        let world = &mut self.worlds[world_id];
2113        world.imports = new_imports;
2114        world.exports = new_exports;
2115
2116        Ok(())
2117    }
2118
2119    fn elaborate_world_import(
2120        &self,
2121        imports: &mut IndexMap<WorldKey, WorldItem>,
2122        key: WorldKey,
2123        id: InterfaceId,
2124        stability: &Stability,
2125    ) {
2126        if imports.contains_key(&key) {
2127            return;
2128        }
2129        for dep in self.interface_direct_deps(id) {
2130            self.elaborate_world_import(imports, WorldKey::Interface(dep), dep, stability);
2131        }
2132        let prev = imports.insert(
2133            key,
2134            WorldItem::Interface {
2135                id,
2136                stability: stability.clone(),
2137            },
2138        );
2139        assert!(prev.is_none());
2140    }
2141
2142    /// This function adds all of the interfaces in `export_interfaces` to the
2143    /// list of exports of the `world` specified.
2144    ///
2145    /// This method is more involved than adding imports because it is fallible.
2146    /// Chiefly what can happen is that the dependencies of all exports must be
2147    /// satisfied by other exports or imports, but not both. For example given a
2148    /// situation such as:
2149    ///
2150    /// ```wit
2151    /// interface a {
2152    ///     type t = u32
2153    /// }
2154    /// interface b {
2155    ///     use a.{t}
2156    /// }
2157    /// interface c {
2158    ///     use a.{t}
2159    ///     use b.{t as t2}
2160    /// }
2161    /// ```
2162    ///
2163    /// where `c` depends on `b` and `a` where `b` depends on `a`, then the
2164    /// purpose of this method is to reject this world:
2165    ///
2166    /// ```wit
2167    /// world foo {
2168    ///     export a
2169    ///     export c
2170    /// }
2171    /// ```
2172    ///
2173    /// The reasoning here is unfortunately subtle and is additionally the
2174    /// subject of WebAssembly/component-model#208. Effectively the `c`
2175    /// interface depends on `b`, but it's not listed explicitly as an import,
2176    /// so it's then implicitly added as an import. This then transitively
2177    /// depends on `a` so it's also added as an import. At this point though `c`
2178    /// also depends on `a`, and it's also exported, so naively it should depend
2179    /// on the export and not implicitly add an import. This means though that
2180    /// `c` has access to two copies of `a`, one imported and one exported. This
2181    /// is not valid, especially in the face of resource types.
2182    ///
2183    /// Overall this method is tasked with rejecting the above world by walking
2184    /// over all the exports and adding their dependencies. Each dependency is
2185    /// recorded with whether it's required to be imported, and then if an
2186    /// export is added for something that's required to be an error then the
2187    /// operation fails.
2188    fn elaborate_world_exports(
2189        &self,
2190        export_interfaces: &IndexMap<InterfaceId, (WorldKey, &Stability)>,
2191        imports: &mut IndexMap<WorldKey, WorldItem>,
2192        exports: &mut IndexMap<WorldKey, WorldItem>,
2193    ) -> Result<()> {
2194        let mut required_imports = HashSet::new();
2195        for (id, (key, stability)) in export_interfaces.iter() {
2196            let name = self.name_world_key(&key);
2197            let ok = add_world_export(
2198                self,
2199                imports,
2200                exports,
2201                export_interfaces,
2202                &mut required_imports,
2203                *id,
2204                key,
2205                true,
2206                stability,
2207            );
2208            if !ok {
2209                bail!(
2210                    // FIXME: this is not a great error message and basically no
2211                    // one will know what to do when it gets printed. Improving
2212                    // this error message, however, is a chunk of work that may
2213                    // not be best spent doing this at this time, so I'm writing
2214                    // this comment instead.
2215                    //
2216                    // More-or-less what should happen here is that a "path"
2217                    // from this interface to the conflicting interface should
2218                    // be printed. It should be explained why an import is being
2219                    // injected, why that's conflicting with an export, and
2220                    // ideally with a suggestion of "add this interface to the
2221                    // export list to fix this error".
2222                    //
2223                    // That's a lot of info that's not easy to get at without
2224                    // more refactoring, so it's left to a future date in the
2225                    // hopes that most folks won't actually run into this for
2226                    // the time being.
2227                    InvalidTransitiveDependency(name),
2228                );
2229            }
2230        }
2231        return Ok(());
2232
2233        fn add_world_export(
2234            resolve: &Resolve,
2235            imports: &mut IndexMap<WorldKey, WorldItem>,
2236            exports: &mut IndexMap<WorldKey, WorldItem>,
2237            export_interfaces: &IndexMap<InterfaceId, (WorldKey, &Stability)>,
2238            required_imports: &mut HashSet<InterfaceId>,
2239            id: InterfaceId,
2240            key: &WorldKey,
2241            add_export: bool,
2242            stability: &Stability,
2243        ) -> bool {
2244            if exports.contains_key(key) {
2245                if add_export {
2246                    return true;
2247                } else {
2248                    return false;
2249                }
2250            }
2251            // If this is an import and it's already in the `required_imports`
2252            // set then we can skip it as we've already visited this interface.
2253            if !add_export && required_imports.contains(&id) {
2254                return true;
2255            }
2256            let ok = resolve.interface_direct_deps(id).all(|dep| {
2257                let key = WorldKey::Interface(dep);
2258                let add_export = add_export && export_interfaces.contains_key(&dep);
2259                add_world_export(
2260                    resolve,
2261                    imports,
2262                    exports,
2263                    export_interfaces,
2264                    required_imports,
2265                    dep,
2266                    &key,
2267                    add_export,
2268                    stability,
2269                )
2270            });
2271            if !ok {
2272                return false;
2273            }
2274            let item = WorldItem::Interface {
2275                id,
2276                stability: stability.clone(),
2277            };
2278            if add_export {
2279                if required_imports.contains(&id) {
2280                    return false;
2281                }
2282                exports.insert(key.clone(), item);
2283            } else {
2284                required_imports.insert(id);
2285                imports.insert(key.clone(), item);
2286            }
2287            true
2288        }
2289    }
2290
2291    /// Remove duplicate imports from a world if they import from the same
2292    /// interface with semver-compatible versions.
2293    ///
2294    /// This will merge duplicate interfaces present at multiple versions in
2295    /// both a world by selecting the larger version of the two interfaces. This
2296    /// requires that the interfaces are indeed semver-compatible and it means
2297    /// that some imports might be removed and replaced. Note that this is only
2298    /// done within a single semver track, for example the world imports 0.2.0
2299    /// and 0.2.1 then the result afterwards will be that it imports
2300    /// 0.2.1. If, however, 0.3.0 where imported then the final result would
2301    /// import both 0.2.0 and 0.3.0.
2302    pub fn merge_world_imports_based_on_semver(&mut self, world_id: WorldId) -> Result<()> {
2303        let world = &self.worlds[world_id];
2304
2305        // The first pass here is to build a map of "semver tracks" where they
2306        // key is per-interface and the value is the maximal version found in
2307        // that semver-compatible-track plus the interface which is the maximal
2308        // version.
2309        //
2310        // At the same time a `to_remove` set is maintained to remember what
2311        // interfaces are being removed from `from` and `into`. All of
2312        // `to_remove` are placed with a known other version.
2313        let mut semver_tracks = HashMap::new();
2314        let mut to_remove = HashSet::new();
2315        for (key, _) in world.imports.iter() {
2316            let iface_id = match key {
2317                WorldKey::Interface(id) => *id,
2318                WorldKey::Name(_) => continue,
2319            };
2320            let (track, version) = match self.semver_track(iface_id) {
2321                Some(track) => track,
2322                None => continue,
2323            };
2324            log::debug!(
2325                "{} is on track {}/{}",
2326                self.id_of(iface_id).unwrap(),
2327                track.0,
2328                track.1,
2329            );
2330            match semver_tracks.entry(track.clone()) {
2331                hash_map::Entry::Vacant(e) => {
2332                    e.insert((version, iface_id));
2333                }
2334                hash_map::Entry::Occupied(mut e) => match version.cmp(&e.get().0) {
2335                    Ordering::Greater => {
2336                        to_remove.insert(e.get().1);
2337                        e.insert((version, iface_id));
2338                    }
2339                    Ordering::Equal => {}
2340                    Ordering::Less => {
2341                        to_remove.insert(iface_id);
2342                    }
2343                },
2344            }
2345        }
2346
2347        // Build a map of "this interface is replaced with this interface" using
2348        // the results of the loop above.
2349        let mut replacements = HashMap::new();
2350        for id in to_remove {
2351            let (track, _) = self.semver_track(id).unwrap();
2352            let (_, latest) = semver_tracks[&track];
2353            let prev = replacements.insert(id, latest);
2354            assert!(prev.is_none());
2355        }
2356
2357        // Validate that `merge_world_item` succeeds for merging all removed
2358        // interfaces with their replacement. This is a double-check that the
2359        // semver version is actually correct and all items present in the old
2360        // interface are in the new.
2361        for (to_replace, replace_with) in replacements.iter() {
2362            self.merge_world_item(
2363                &WorldItem::Interface {
2364                    id: *to_replace,
2365                    stability: Default::default(),
2366                },
2367                &WorldItem::Interface {
2368                    id: *replace_with,
2369                    stability: Default::default(),
2370                },
2371            )
2372            .with_context(|| {
2373                let old_name = self.id_of(*to_replace).unwrap();
2374                let new_name = self.id_of(*replace_with).unwrap();
2375                format!(
2376                    "failed to upgrade `{old_name}` to `{new_name}`, was \
2377                     this semver-compatible update not semver compatible?"
2378                )
2379            })?;
2380        }
2381
2382        for (to_replace, replace_with) in replacements.iter() {
2383            log::debug!(
2384                "REPLACE {} => {}",
2385                self.id_of(*to_replace).unwrap(),
2386                self.id_of(*replace_with).unwrap(),
2387            );
2388        }
2389
2390        // Finally perform the actual transformation of the imports/exports.
2391        // Here all imports are removed if they're replaced and otherwise all
2392        // imports have their dependencies updated, possibly transitively, to
2393        // point to the new interfaces in `replacements`.
2394        //
2395        // Afterwards exports are additionally updated, but only their
2396        // dependencies on imports which were remapped. Exports themselves are
2397        // not deduplicated and/or removed.
2398        for (key, item) in mem::take(&mut self.worlds[world_id].imports) {
2399            if let WorldItem::Interface { id, .. } = item {
2400                if replacements.contains_key(&id) {
2401                    continue;
2402                }
2403            }
2404
2405            self.update_interface_deps_of_world_item(&item, &replacements);
2406
2407            let prev = self.worlds[world_id].imports.insert(key, item);
2408            assert!(prev.is_none());
2409        }
2410        for (key, item) in mem::take(&mut self.worlds[world_id].exports) {
2411            self.update_interface_deps_of_world_item(&item, &replacements);
2412            let prev = self.worlds[world_id].exports.insert(key, item);
2413            assert!(prev.is_none());
2414        }
2415
2416        // Run through `elaborate_world` to reorder imports as appropriate and
2417        // fill anything back in if it's actually required by exports. For now
2418        // this doesn't tamper with exports at all. Also note that this is
2419        // applied to all worlds in this `Resolve` because interfaces were
2420        // modified directly.
2421        let ids = self.worlds.iter().map(|(id, _)| id).collect::<Vec<_>>();
2422        for world_id in ids {
2423            self.elaborate_world(world_id).with_context(|| {
2424                let name = &self.worlds[world_id].name;
2425                format!(
2426                    "failed to elaborate world `{name}` after deduplicating imports \
2427                     based on semver"
2428                )
2429            })?;
2430        }
2431
2432        #[cfg(debug_assertions)]
2433        self.assert_valid();
2434
2435        Ok(())
2436    }
2437
2438    fn update_interface_deps_of_world_item(
2439        &mut self,
2440        item: &WorldItem,
2441        replacements: &HashMap<InterfaceId, InterfaceId>,
2442    ) {
2443        match *item {
2444            WorldItem::Type(t) => self.update_interface_dep_of_type(t, &replacements),
2445            WorldItem::Interface { id, .. } => {
2446                let types = self.interfaces[id]
2447                    .types
2448                    .values()
2449                    .copied()
2450                    .collect::<Vec<_>>();
2451                for ty in types {
2452                    self.update_interface_dep_of_type(ty, &replacements);
2453                }
2454            }
2455            WorldItem::Function(_) => {}
2456        }
2457    }
2458
2459    /// Returns the "semver track" of an interface plus the interface's version.
2460    ///
2461    /// This function returns `None` if the interface `id` has a package without
2462    /// a version. If the version is present, however, the first element of the
2463    /// tuple returned is a "semver track" for the specific interface. The
2464    /// version listed in `PackageName` will be modified so all
2465    /// semver-compatible versions are listed the same way.
2466    ///
2467    /// The second element in the returned tuple is this interface's package's
2468    /// version.
2469    fn semver_track(&self, id: InterfaceId) -> Option<((PackageName, String), &Version)> {
2470        let iface = &self.interfaces[id];
2471        let pkg = &self.packages[iface.package?];
2472        let version = pkg.name.version.as_ref()?;
2473        let mut name = pkg.name.clone();
2474        name.version = Some(PackageName::version_compat_track(version));
2475        Some(((name, iface.name.clone()?), version))
2476    }
2477
2478    /// If `ty` is a definition where it's a `use` from another interface, then
2479    /// change what interface it's using from according to the pairs in the
2480    /// `replacements` map.
2481    fn update_interface_dep_of_type(
2482        &mut self,
2483        ty: TypeId,
2484        replacements: &HashMap<InterfaceId, InterfaceId>,
2485    ) {
2486        let to_replace = match self.type_interface_dep(ty) {
2487            Some(id) => id,
2488            None => return,
2489        };
2490        let replace_with = match replacements.get(&to_replace) {
2491            Some(id) => id,
2492            None => return,
2493        };
2494        let dep = match self.types[ty].kind {
2495            TypeDefKind::Type(Type::Id(id)) => id,
2496            _ => return,
2497        };
2498        let name = self.types[dep].name.as_ref().unwrap();
2499        // Note the infallible name indexing happening here. This should be
2500        // previously validated with `merge_world_item` to succeed.
2501        let replacement_id = self.interfaces[*replace_with].types[name];
2502        self.types[ty].kind = TypeDefKind::Type(Type::Id(replacement_id));
2503    }
2504
2505    /// Returns the core wasm module/field names for the specified `import`.
2506    ///
2507    /// This function will return the core wasm module/field that can be used to
2508    /// use `import` with the name `mangling` scheme specified as well. This can
2509    /// be useful for bindings generators, for example, and these names are
2510    /// recognized by `wit-component` and `wasm-tools component new`.
2511    pub fn wasm_import_name(
2512        &self,
2513        mangling: ManglingAndAbi,
2514        import: WasmImport<'_>,
2515    ) -> (String, String) {
2516        match mangling {
2517            ManglingAndAbi::Standard32 => match import {
2518                WasmImport::Func { interface, func } => {
2519                    let module = match interface {
2520                        Some(key) => format!("cm32p2|{}", self.name_canonicalized_world_key(key)),
2521                        None => format!("cm32p2"),
2522                    };
2523                    (module, func.name.clone())
2524                }
2525                WasmImport::ResourceIntrinsic {
2526                    interface,
2527                    resource,
2528                    intrinsic,
2529                } => {
2530                    let name = self.types[resource].name.as_ref().unwrap();
2531                    let (prefix, name) = match intrinsic {
2532                        ResourceIntrinsic::ImportedDrop => ("", format!("{name}_drop")),
2533                        ResourceIntrinsic::ExportedDrop => ("_ex_", format!("{name}_drop")),
2534                        ResourceIntrinsic::ExportedNew => ("_ex_", format!("{name}_new")),
2535                        ResourceIntrinsic::ExportedRep => ("_ex_", format!("{name}_rep")),
2536                    };
2537                    let module = match interface {
2538                        Some(key) => {
2539                            format!("cm32p2|{prefix}{}", self.name_canonicalized_world_key(key))
2540                        }
2541                        None => {
2542                            assert_eq!(prefix, "");
2543                            format!("cm32p2")
2544                        }
2545                    };
2546                    (module, name)
2547                }
2548            },
2549            ManglingAndAbi::Legacy(abi) => match import {
2550                WasmImport::Func { interface, func } => {
2551                    let module = match interface {
2552                        Some(key) => self.name_world_key(key),
2553                        None => format!("$root"),
2554                    };
2555                    (module, format!("{}{}", abi.import_prefix(), func.name))
2556                }
2557                WasmImport::ResourceIntrinsic {
2558                    interface,
2559                    resource,
2560                    intrinsic,
2561                } => {
2562                    let name = self.types[resource].name.as_ref().unwrap();
2563                    let (prefix, name) = match intrinsic {
2564                        ResourceIntrinsic::ImportedDrop => ("", format!("[resource-drop]{name}")),
2565                        ResourceIntrinsic::ExportedDrop => {
2566                            ("[export]", format!("[resource-drop]{name}"))
2567                        }
2568                        ResourceIntrinsic::ExportedNew => {
2569                            ("[export]", format!("[resource-new]{name}"))
2570                        }
2571                        ResourceIntrinsic::ExportedRep => {
2572                            ("[export]", format!("[resource-rep]{name}"))
2573                        }
2574                    };
2575                    let module = match interface {
2576                        Some(key) => format!("{prefix}{}", self.name_world_key(key)),
2577                        None => {
2578                            assert_eq!(prefix, "");
2579                            format!("$root")
2580                        }
2581                    };
2582                    (module, format!("{}{name}", abi.import_prefix()))
2583                }
2584            },
2585        }
2586    }
2587
2588    /// Returns the core wasm export name for the specified `export`.
2589    ///
2590    /// This is the same as [`Resolve::wasm_import_name`], except for exports.
2591    pub fn wasm_export_name(&self, mangling: ManglingAndAbi, export: WasmExport<'_>) -> String {
2592        match mangling {
2593            ManglingAndAbi::Standard32 => match export {
2594                WasmExport::Func {
2595                    interface,
2596                    func,
2597                    kind,
2598                } => {
2599                    let mut name = String::from("cm32p2|");
2600                    if let Some(interface) = interface {
2601                        let s = self.name_canonicalized_world_key(interface);
2602                        name.push_str(&s);
2603                    }
2604                    name.push_str("|");
2605                    name.push_str(&func.name);
2606                    match kind {
2607                        WasmExportKind::Normal => {}
2608                        WasmExportKind::PostReturn => name.push_str("_post"),
2609                        WasmExportKind::Callback => todo!(
2610                            "not yet supported: \
2611                             async callback functions using standard name mangling"
2612                        ),
2613                    }
2614                    name
2615                }
2616                WasmExport::ResourceDtor {
2617                    interface,
2618                    resource,
2619                } => {
2620                    let name = self.types[resource].name.as_ref().unwrap();
2621                    let interface = self.name_canonicalized_world_key(interface);
2622                    format!("cm32p2|{interface}|{name}_dtor")
2623                }
2624                WasmExport::Memory => "cm32p2_memory".to_string(),
2625                WasmExport::Initialize => "cm32p2_initialize".to_string(),
2626                WasmExport::Realloc => "cm32p2_realloc".to_string(),
2627            },
2628            ManglingAndAbi::Legacy(abi) => match export {
2629                WasmExport::Func {
2630                    interface,
2631                    func,
2632                    kind,
2633                } => {
2634                    let mut name = abi.export_prefix().to_string();
2635                    match kind {
2636                        WasmExportKind::Normal => {}
2637                        WasmExportKind::PostReturn => name.push_str("cabi_post_"),
2638                        WasmExportKind::Callback => {
2639                            assert!(matches!(abi, LiftLowerAbi::AsyncCallback));
2640                            name = format!("[callback]{name}")
2641                        }
2642                    }
2643                    if let Some(interface) = interface {
2644                        let s = self.name_world_key(interface);
2645                        name.push_str(&s);
2646                        name.push_str("#");
2647                    }
2648                    name.push_str(&func.name);
2649                    name
2650                }
2651                WasmExport::ResourceDtor {
2652                    interface,
2653                    resource,
2654                } => {
2655                    let name = self.types[resource].name.as_ref().unwrap();
2656                    let interface = self.name_world_key(interface);
2657                    format!("{}{interface}#[dtor]{name}", abi.export_prefix())
2658                }
2659                WasmExport::Memory => "memory".to_string(),
2660                WasmExport::Initialize => "_initialize".to_string(),
2661                WasmExport::Realloc => "cabi_realloc".to_string(),
2662            },
2663        }
2664    }
2665}
2666
2667/// Possible imports that can be passed to [`Resolve::wasm_import_name`].
2668#[derive(Debug)]
2669pub enum WasmImport<'a> {
2670    /// A WIT function is being imported. Optionally from an interface.
2671    Func {
2672        /// The name of the interface that the function is being imported from.
2673        ///
2674        /// If the function is imported directly from the world then this is
2675        /// `Noen`.
2676        interface: Option<&'a WorldKey>,
2677
2678        /// The function being imported.
2679        func: &'a Function,
2680    },
2681
2682    /// A resource-related intrinsic is being imported.
2683    ResourceIntrinsic {
2684        /// The optional interface to import from, same as `WasmImport::Func`.
2685        interface: Option<&'a WorldKey>,
2686
2687        /// The resource that's being operated on.
2688        resource: TypeId,
2689
2690        /// The intrinsic that's being imported.
2691        intrinsic: ResourceIntrinsic,
2692    },
2693}
2694
2695/// Intrinsic definitions to go with [`WasmImport::ResourceIntrinsic`] which
2696/// also goes with [`Resolve::wasm_import_name`].
2697#[derive(Debug)]
2698pub enum ResourceIntrinsic {
2699    ImportedDrop,
2700    ExportedDrop,
2701    ExportedNew,
2702    ExportedRep,
2703}
2704
2705/// Indicates whether a function export is a normal export, a post-return
2706/// function, or a callback function.
2707#[derive(Debug)]
2708pub enum WasmExportKind {
2709    /// Normal function export.
2710    Normal,
2711
2712    /// Post-return function.
2713    PostReturn,
2714
2715    /// Async callback function.
2716    Callback,
2717}
2718
2719/// Different kinds of exports that can be passed to
2720/// [`Resolve::wasm_export_name`] to export from core wasm modules.
2721#[derive(Debug)]
2722pub enum WasmExport<'a> {
2723    /// A WIT function is being exported, optionally from an interface.
2724    Func {
2725        /// An optional interface which owns `func`. Use `None` for top-level
2726        /// world function.
2727        interface: Option<&'a WorldKey>,
2728
2729        /// The function being exported.
2730        func: &'a Function,
2731
2732        /// Kind of function (normal, post-return, or callback) being exported.
2733        kind: WasmExportKind,
2734    },
2735
2736    /// A destructor for a resource exported from this module.
2737    ResourceDtor {
2738        /// The interface that owns the resource.
2739        interface: &'a WorldKey,
2740        /// The resource itself that the destructor is for.
2741        resource: TypeId,
2742    },
2743
2744    /// Linear memory, the one that the canonical ABI uses.
2745    Memory,
2746
2747    /// An initialization function (not the core wasm `start`).
2748    Initialize,
2749
2750    /// The general-purpose realloc hook.
2751    Realloc,
2752}
2753
2754/// Structure returned by [`Resolve::merge`] which contains mappings from
2755/// old-ids to new-ids after the merge.
2756#[derive(Default)]
2757pub struct Remap {
2758    pub types: Vec<Option<TypeId>>,
2759    pub interfaces: Vec<Option<InterfaceId>>,
2760    pub worlds: Vec<Option<WorldId>>,
2761    pub packages: Vec<PackageId>,
2762
2763    /// A cache of anonymous `own<T>` handles for resource types.
2764    ///
2765    /// The appending operation of `Remap` is the one responsible for
2766    /// translating references to `T` where `T` is a resource into `own<T>`
2767    /// instead. This map is used to deduplicate the `own<T>` types generated
2768    /// to generate as few as possible.
2769    ///
2770    /// The key of this map is the resource id `T` in the new resolve, and
2771    /// the value is the `own<T>` type pointing to `T`.
2772    own_handles: HashMap<TypeId, TypeId>,
2773
2774    type_has_borrow: Vec<Option<bool>>,
2775}
2776
2777fn apply_map<T>(map: &[Option<Id<T>>], id: Id<T>, desc: &str, span: Option<Span>) -> Result<Id<T>> {
2778    match map.get(id.index()) {
2779        Some(Some(id)) => Ok(*id),
2780        Some(None) => {
2781            let msg = format!(
2782                "found a reference to a {desc} which is excluded \
2783                 due to its feature not being activated"
2784            );
2785            match span {
2786                Some(span) => Err(Error::new(span, msg).into()),
2787                None => bail!("{msg}"),
2788            }
2789        }
2790        None => panic!("request to remap a {desc} that has not yet been registered"),
2791    }
2792}
2793
2794impl Remap {
2795    pub fn map_type(&self, id: TypeId, span: Option<Span>) -> Result<TypeId> {
2796        apply_map(&self.types, id, "type", span)
2797    }
2798
2799    pub fn map_interface(&self, id: InterfaceId, span: Option<Span>) -> Result<InterfaceId> {
2800        apply_map(&self.interfaces, id, "interface", span)
2801    }
2802
2803    pub fn map_world(&self, id: WorldId, span: Option<Span>) -> Result<WorldId> {
2804        apply_map(&self.worlds, id, "world", span)
2805    }
2806
2807    fn append(
2808        &mut self,
2809        resolve: &mut Resolve,
2810        unresolved: UnresolvedPackage,
2811    ) -> Result<PackageId> {
2812        let pkgid = resolve.packages.alloc(Package {
2813            name: unresolved.name.clone(),
2814            docs: unresolved.docs.clone(),
2815            interfaces: Default::default(),
2816            worlds: Default::default(),
2817        });
2818        let prev = resolve.package_names.insert(unresolved.name.clone(), pkgid);
2819        if let Some(prev) = prev {
2820            resolve.package_names.insert(unresolved.name.clone(), prev);
2821            bail!(
2822                "attempting to re-add package `{}` when it's already present in this `Resolve`",
2823                unresolved.name,
2824            );
2825        }
2826
2827        self.process_foreign_deps(resolve, pkgid, &unresolved)?;
2828
2829        let foreign_types = self.types.len();
2830        let foreign_interfaces = self.interfaces.len();
2831        let foreign_worlds = self.worlds.len();
2832
2833        // Copy over all types first, updating any intra-type references. Note
2834        // that types are sorted topologically which means this iteration
2835        // order should be sufficient. Also note though that the interface
2836        // owner of a type isn't updated here due to interfaces not being known
2837        // yet.
2838        assert_eq!(unresolved.types.len(), unresolved.type_spans.len());
2839        for ((id, mut ty), span) in unresolved
2840            .types
2841            .into_iter()
2842            .zip(&unresolved.type_spans)
2843            .skip(foreign_types)
2844        {
2845            if !resolve.include_type(&ty, pkgid, *span)? {
2846                self.types.push(None);
2847                continue;
2848            }
2849
2850            self.update_typedef(resolve, &mut ty, Some(*span))?;
2851            let new_id = resolve.types.alloc(ty);
2852            assert_eq!(self.types.len(), id.index());
2853
2854            let new_id = match resolve.types[new_id] {
2855                // If this is an `own<T>` handle then either replace it with a
2856                // preexisting `own<T>` handle which may have been generated in
2857                // `update_ty`. If that doesn't exist though then insert it into
2858                // the `own_handles` cache.
2859                TypeDef {
2860                    name: None,
2861                    owner: TypeOwner::None,
2862                    kind: TypeDefKind::Handle(Handle::Own(id)),
2863                    docs: _,
2864                    stability: _,
2865                } => *self.own_handles.entry(id).or_insert(new_id),
2866
2867                // Everything not-related to `own<T>` doesn't get its ID
2868                // modified.
2869                _ => new_id,
2870            };
2871            self.types.push(Some(new_id));
2872        }
2873
2874        // Next transfer all interfaces into `Resolve`, updating type ids
2875        // referenced along the way.
2876        assert_eq!(
2877            unresolved.interfaces.len(),
2878            unresolved.interface_spans.len()
2879        );
2880        for ((id, mut iface), span) in unresolved
2881            .interfaces
2882            .into_iter()
2883            .zip(&unresolved.interface_spans)
2884            .skip(foreign_interfaces)
2885        {
2886            if !resolve
2887                .include_stability(&iface.stability, &pkgid, Some(span.span))
2888                .with_context(|| {
2889                    format!(
2890                        "failed to process feature gate for interface [{}] in package [{}]",
2891                        iface
2892                            .name
2893                            .as_ref()
2894                            .map(String::as_str)
2895                            .unwrap_or("<unknown>"),
2896                        resolve.packages[pkgid].name,
2897                    )
2898                })?
2899            {
2900                self.interfaces.push(None);
2901                continue;
2902            }
2903            assert!(iface.package.is_none());
2904            iface.package = Some(pkgid);
2905            self.update_interface(resolve, &mut iface, Some(span))?;
2906            let new_id = resolve.interfaces.alloc(iface);
2907            assert_eq!(self.interfaces.len(), id.index());
2908            self.interfaces.push(Some(new_id));
2909        }
2910
2911        // Now that interfaces are identified go back through the types and
2912        // update their interface owners.
2913        for (i, id) in self.types.iter().enumerate().skip(foreign_types) {
2914            let id = match id {
2915                Some(id) => *id,
2916                None => continue,
2917            };
2918            match &mut resolve.types[id].owner {
2919                TypeOwner::Interface(id) => {
2920                    let span = unresolved.type_spans[i];
2921                    *id = self.map_interface(*id, Some(span))
2922                        .with_context(|| {
2923                            "this type is not gated by a feature but its interface is gated by a feature"
2924                        })?;
2925                }
2926                TypeOwner::World(_) | TypeOwner::None => {}
2927            }
2928        }
2929
2930        // Expand worlds. Note that this does NOT process `include` statements,
2931        // that's handled below. Instead this just handles world item updates
2932        // and resolves references to types/items within `Resolve`.
2933        //
2934        // This is done after types/interfaces are fully settled so the
2935        // transitive relation between interfaces, through types, is understood
2936        // here.
2937        assert_eq!(unresolved.worlds.len(), unresolved.world_spans.len());
2938        for ((id, mut world), span) in unresolved
2939            .worlds
2940            .into_iter()
2941            .zip(&unresolved.world_spans)
2942            .skip(foreign_worlds)
2943        {
2944            if !resolve
2945                .include_stability(&world.stability, &pkgid, Some(span.span))
2946                .with_context(|| {
2947                    format!(
2948                        "failed to process feature gate for world [{}] in package [{}]",
2949                        world.name, resolve.packages[pkgid].name,
2950                    )
2951                })?
2952            {
2953                self.worlds.push(None);
2954                continue;
2955            }
2956            self.update_world(&mut world, resolve, &pkgid, &span)?;
2957
2958            let new_id = resolve.worlds.alloc(world);
2959            assert_eq!(self.worlds.len(), id.index());
2960            self.worlds.push(Some(new_id));
2961        }
2962
2963        // As with interfaces, now update the ids of world-owned types.
2964        for (i, id) in self.types.iter().enumerate().skip(foreign_types) {
2965            let id = match id {
2966                Some(id) => *id,
2967                None => continue,
2968            };
2969            match &mut resolve.types[id].owner {
2970                TypeOwner::World(id) => {
2971                    let span = unresolved.type_spans[i];
2972                    *id = self.map_world(*id, Some(span))
2973                        .with_context(|| {
2974                            "this type is not gated by a feature but its interface is gated by a feature"
2975                        })?;
2976                }
2977                TypeOwner::Interface(_) | TypeOwner::None => {}
2978            }
2979        }
2980
2981        // After the above, process `include` statements for worlds and
2982        // additionally fully elaborate them. Processing of `include` is
2983        // deferred until after the steps above so the fully resolved state of
2984        // local types in this package are all available. This is required
2985        // because `include` may copy types between worlds when the type is
2986        // defined in the world itself.
2987        //
2988        // This step, after processing `include`, will also use
2989        // `elaborate_world` to fully expand the world in terms of
2990        // imports/exports and ensure that all necessary imports/exports are all
2991        // listed.
2992        //
2993        // Note that `self.worlds` is already sorted in topological order so if
2994        // one world refers to another via `include` then it's guaranteed that
2995        // the one we're referring to is already expanded and ready to be
2996        // included.
2997        assert_eq!(self.worlds.len(), unresolved.world_spans.len());
2998        for (id, span) in self
2999            .worlds
3000            .iter()
3001            .zip(unresolved.world_spans.iter())
3002            .skip(foreign_worlds)
3003        {
3004            let Some(id) = *id else {
3005                continue;
3006            };
3007            self.process_world_includes(id, resolve, &pkgid, &span)?;
3008
3009            resolve.elaborate_world(id).with_context(|| {
3010                Error::new(
3011                    span.span,
3012                    format!(
3013                        "failed to elaborate world imports/exports of `{}`",
3014                        resolve.worlds[id].name
3015                    ),
3016                )
3017            })?;
3018        }
3019
3020        // Fixup "parent" ids now that everything has been identified
3021        for id in self.interfaces.iter().skip(foreign_interfaces) {
3022            let id = match id {
3023                Some(id) => *id,
3024                None => continue,
3025            };
3026            let iface = &mut resolve.interfaces[id];
3027            iface.package = Some(pkgid);
3028            if let Some(name) = &iface.name {
3029                let prev = resolve.packages[pkgid].interfaces.insert(name.clone(), id);
3030                assert!(prev.is_none());
3031            }
3032        }
3033        for id in self.worlds.iter().skip(foreign_worlds) {
3034            let id = match id {
3035                Some(id) => *id,
3036                None => continue,
3037            };
3038            let world = &mut resolve.worlds[id];
3039            world.package = Some(pkgid);
3040            let prev = resolve.packages[pkgid]
3041                .worlds
3042                .insert(world.name.clone(), id);
3043            assert!(prev.is_none());
3044        }
3045        Ok(pkgid)
3046    }
3047
3048    fn process_foreign_deps(
3049        &mut self,
3050        resolve: &mut Resolve,
3051        pkgid: PackageId,
3052        unresolved: &UnresolvedPackage,
3053    ) -> Result<()> {
3054        // Invert the `foreign_deps` map to be keyed by world id to get
3055        // used in the loops below.
3056        let mut world_to_package = HashMap::new();
3057        let mut interface_to_package = HashMap::new();
3058        for (i, (pkg_name, worlds_or_ifaces)) in unresolved.foreign_deps.iter().enumerate() {
3059            for (name, item) in worlds_or_ifaces {
3060                match item {
3061                    AstItem::Interface(unresolved_interface_id) => {
3062                        let prev = interface_to_package.insert(
3063                            *unresolved_interface_id,
3064                            (pkg_name, name, unresolved.foreign_dep_spans[i]),
3065                        );
3066                        assert!(prev.is_none());
3067                    }
3068                    AstItem::World(unresolved_world_id) => {
3069                        let prev = world_to_package.insert(
3070                            *unresolved_world_id,
3071                            (pkg_name, name, unresolved.foreign_dep_spans[i]),
3072                        );
3073                        assert!(prev.is_none());
3074                    }
3075                }
3076            }
3077        }
3078
3079        // Connect all interfaces referred to in `interface_to_package`, which
3080        // are at the front of `unresolved.interfaces`, to interfaces already
3081        // contained within `resolve`.
3082        self.process_foreign_interfaces(unresolved, &interface_to_package, resolve)?;
3083
3084        // Connect all worlds referred to in `world_to_package`, which
3085        // are at the front of `unresolved.worlds`, to worlds already
3086        // contained within `resolve`.
3087        self.process_foreign_worlds(unresolved, &world_to_package, resolve)?;
3088
3089        // Finally, iterate over all foreign-defined types and determine
3090        // what they map to.
3091        self.process_foreign_types(unresolved, pkgid, resolve)?;
3092
3093        for (id, span) in unresolved.required_resource_types.iter() {
3094            // Note that errors are ignored here because an error represents a
3095            // type that has been configured away. If a type is configured away
3096            // then any future use of it will generate an error so there's no
3097            // need to validate that it's a resource here.
3098            let Ok(mut id) = self.map_type(*id, Some(*span)) else {
3099                continue;
3100            };
3101            loop {
3102                match resolve.types[id].kind {
3103                    TypeDefKind::Type(Type::Id(i)) => id = i,
3104                    TypeDefKind::Resource => break,
3105                    _ => bail!(Error::new(
3106                        *span,
3107                        format!("type used in a handle must be a resource"),
3108                    )),
3109                }
3110            }
3111        }
3112
3113        #[cfg(debug_assertions)]
3114        resolve.assert_valid();
3115
3116        Ok(())
3117    }
3118
3119    fn process_foreign_interfaces(
3120        &mut self,
3121        unresolved: &UnresolvedPackage,
3122        interface_to_package: &HashMap<InterfaceId, (&PackageName, &String, Span)>,
3123        resolve: &mut Resolve,
3124    ) -> Result<(), anyhow::Error> {
3125        for (unresolved_iface_id, unresolved_iface) in unresolved.interfaces.iter() {
3126            let (pkg_name, interface, span) = match interface_to_package.get(&unresolved_iface_id) {
3127                Some(items) => *items,
3128                // All foreign interfaces are defined first, so the first one
3129                // which is defined in a non-foreign document means that all
3130                // further interfaces will be non-foreign as well.
3131                None => break,
3132            };
3133            let pkgid = resolve
3134                .package_names
3135                .get(pkg_name)
3136                .copied()
3137                .ok_or_else(|| {
3138                    PackageNotFoundError::new(
3139                        span,
3140                        pkg_name.clone(),
3141                        resolve.package_names.keys().cloned().collect(),
3142                    )
3143                })?;
3144
3145            // Functions can't be imported so this should be empty.
3146            assert!(unresolved_iface.functions.is_empty());
3147
3148            let pkg = &resolve.packages[pkgid];
3149            let span = &unresolved.interface_spans[unresolved_iface_id.index()];
3150            let iface_id = pkg
3151                .interfaces
3152                .get(interface)
3153                .copied()
3154                .ok_or_else(|| Error::new(span.span, "interface not found in package"))?;
3155            assert_eq!(self.interfaces.len(), unresolved_iface_id.index());
3156            self.interfaces.push(Some(iface_id));
3157        }
3158        for (id, _) in unresolved.interfaces.iter().skip(self.interfaces.len()) {
3159            assert!(
3160                interface_to_package.get(&id).is_none(),
3161                "found foreign interface after local interface"
3162            );
3163        }
3164        Ok(())
3165    }
3166
3167    fn process_foreign_worlds(
3168        &mut self,
3169        unresolved: &UnresolvedPackage,
3170        world_to_package: &HashMap<WorldId, (&PackageName, &String, Span)>,
3171        resolve: &mut Resolve,
3172    ) -> Result<(), anyhow::Error> {
3173        for (unresolved_world_id, _) in unresolved.worlds.iter() {
3174            let (pkg_name, world, span) = match world_to_package.get(&unresolved_world_id) {
3175                Some(items) => *items,
3176                // Same as above, all worlds are foreign until we find a
3177                // non-foreign one.
3178                None => break,
3179            };
3180
3181            let pkgid = resolve
3182                .package_names
3183                .get(pkg_name)
3184                .copied()
3185                .ok_or_else(|| Error::new(span, "package not found"))?;
3186            let pkg = &resolve.packages[pkgid];
3187            let span = &unresolved.world_spans[unresolved_world_id.index()];
3188            let world_id = pkg
3189                .worlds
3190                .get(world)
3191                .copied()
3192                .ok_or_else(|| Error::new(span.span, "world not found in package"))?;
3193            assert_eq!(self.worlds.len(), unresolved_world_id.index());
3194            self.worlds.push(Some(world_id));
3195        }
3196        for (id, _) in unresolved.worlds.iter().skip(self.worlds.len()) {
3197            assert!(
3198                world_to_package.get(&id).is_none(),
3199                "found foreign world after local world"
3200            );
3201        }
3202        Ok(())
3203    }
3204
3205    fn process_foreign_types(
3206        &mut self,
3207        unresolved: &UnresolvedPackage,
3208        pkgid: PackageId,
3209        resolve: &mut Resolve,
3210    ) -> Result<(), anyhow::Error> {
3211        for ((unresolved_type_id, unresolved_ty), span) in
3212            unresolved.types.iter().zip(&unresolved.type_spans)
3213        {
3214            // All "Unknown" types should appear first so once we're no longer
3215            // in unknown territory it's package-defined types so break out of
3216            // this loop.
3217            match unresolved_ty.kind {
3218                TypeDefKind::Unknown => {}
3219                _ => break,
3220            }
3221
3222            if !resolve.include_type(unresolved_ty, pkgid, *span)? {
3223                self.types.push(None);
3224                continue;
3225            }
3226
3227            let unresolved_iface_id = match unresolved_ty.owner {
3228                TypeOwner::Interface(id) => id,
3229                _ => unreachable!(),
3230            };
3231            let iface_id = self.map_interface(unresolved_iface_id, None)?;
3232            let name = unresolved_ty.name.as_ref().unwrap();
3233            let span = unresolved.unknown_type_spans[unresolved_type_id.index()];
3234            let type_id = *resolve.interfaces[iface_id]
3235                .types
3236                .get(name)
3237                .ok_or_else(|| {
3238                    Error::new(span, format!("type `{name}` not defined in interface"))
3239                })?;
3240            assert_eq!(self.types.len(), unresolved_type_id.index());
3241            self.types.push(Some(type_id));
3242        }
3243        for (_, ty) in unresolved.types.iter().skip(self.types.len()) {
3244            if let TypeDefKind::Unknown = ty.kind {
3245                panic!("unknown type after defined type");
3246            }
3247        }
3248        Ok(())
3249    }
3250
3251    fn update_typedef(
3252        &mut self,
3253        resolve: &mut Resolve,
3254        ty: &mut TypeDef,
3255        span: Option<Span>,
3256    ) -> Result<()> {
3257        // NB: note that `ty.owner` is not updated here since interfaces
3258        // haven't been mapped yet and that's done in a separate step.
3259        use crate::TypeDefKind::*;
3260        match &mut ty.kind {
3261            Handle(handle) => match handle {
3262                crate::Handle::Own(ty) | crate::Handle::Borrow(ty) => {
3263                    self.update_type_id(ty, span)?
3264                }
3265            },
3266            Resource => {}
3267            Record(r) => {
3268                for field in r.fields.iter_mut() {
3269                    self.update_ty(resolve, &mut field.ty, span)
3270                        .with_context(|| format!("failed to update field `{}`", field.name))?;
3271                }
3272            }
3273            Tuple(t) => {
3274                for ty in t.types.iter_mut() {
3275                    self.update_ty(resolve, ty, span)?;
3276                }
3277            }
3278            Variant(v) => {
3279                for case in v.cases.iter_mut() {
3280                    if let Some(t) = &mut case.ty {
3281                        self.update_ty(resolve, t, span)?;
3282                    }
3283                }
3284            }
3285            Option(t) | List(t, ..) | FixedSizeList(t, ..) | Future(Some(t)) | Stream(Some(t)) => {
3286                self.update_ty(resolve, t, span)?
3287            }
3288            Result(r) => {
3289                if let Some(ty) = &mut r.ok {
3290                    self.update_ty(resolve, ty, span)?;
3291                }
3292                if let Some(ty) = &mut r.err {
3293                    self.update_ty(resolve, ty, span)?;
3294                }
3295            }
3296
3297            // Note that `update_ty` is specifically not used here as typedefs
3298            // because for the `type a = b` form that doesn't force `a` to be a
3299            // handle type if `b` is a resource type, instead `a` is
3300            // simultaneously usable as a resource and a handle type
3301            Type(crate::Type::Id(id)) => self.update_type_id(id, span)?,
3302            Type(_) => {}
3303
3304            // nothing to do for these as they're just names or empty
3305            Flags(_) | Enum(_) | Future(None) | Stream(None) => {}
3306
3307            Unknown => unreachable!(),
3308        }
3309
3310        Ok(())
3311    }
3312
3313    fn update_ty(
3314        &mut self,
3315        resolve: &mut Resolve,
3316        ty: &mut Type,
3317        span: Option<Span>,
3318    ) -> Result<()> {
3319        let id = match ty {
3320            Type::Id(id) => id,
3321            _ => return Ok(()),
3322        };
3323        self.update_type_id(id, span)?;
3324
3325        // If `id` points to a `Resource` type then this means that what was
3326        // just discovered was a reference to what will implicitly become an
3327        // `own<T>` handle. This `own` handle is implicitly allocated here
3328        // and handled during the merging process.
3329        let mut cur = *id;
3330        let points_to_resource = loop {
3331            match resolve.types[cur].kind {
3332                TypeDefKind::Type(Type::Id(id)) => cur = id,
3333                TypeDefKind::Resource => break true,
3334                _ => break false,
3335            }
3336        };
3337
3338        if points_to_resource {
3339            *id = *self.own_handles.entry(*id).or_insert_with(|| {
3340                resolve.types.alloc(TypeDef {
3341                    name: None,
3342                    owner: TypeOwner::None,
3343                    kind: TypeDefKind::Handle(Handle::Own(*id)),
3344                    docs: Default::default(),
3345                    stability: Default::default(),
3346                })
3347            });
3348        }
3349        Ok(())
3350    }
3351
3352    fn update_type_id(&self, id: &mut TypeId, span: Option<Span>) -> Result<()> {
3353        *id = self.map_type(*id, span)?;
3354        Ok(())
3355    }
3356
3357    fn update_interface(
3358        &mut self,
3359        resolve: &mut Resolve,
3360        iface: &mut Interface,
3361        spans: Option<&InterfaceSpan>,
3362    ) -> Result<()> {
3363        iface.types.retain(|_, ty| self.types[ty.index()].is_some());
3364        let iface_pkg_id = iface.package.as_ref().unwrap_or_else(|| {
3365            panic!(
3366                "unexpectedly missing package on interface [{}]",
3367                iface
3368                    .name
3369                    .as_ref()
3370                    .map(String::as_str)
3371                    .unwrap_or("<unknown>"),
3372            )
3373        });
3374
3375        // NB: note that `iface.doc` is not updated here since interfaces
3376        // haven't been mapped yet and that's done in a separate step.
3377        for (_name, ty) in iface.types.iter_mut() {
3378            self.update_type_id(ty, spans.map(|s| s.span))?;
3379        }
3380        if let Some(spans) = spans {
3381            assert_eq!(iface.functions.len(), spans.funcs.len());
3382        }
3383        for (i, (func_name, func)) in iface.functions.iter_mut().enumerate() {
3384            let span = spans.map(|s| s.funcs[i]);
3385            if !resolve
3386                .include_stability(&func.stability, iface_pkg_id, span)
3387                .with_context(|| {
3388                    format!(
3389                        "failed to process feature gate for function [{func_name}] in package [{}]",
3390                        resolve.packages[*iface_pkg_id].name,
3391                    )
3392                })?
3393            {
3394                continue;
3395            }
3396            self.update_function(resolve, func, span)
3397                .with_context(|| format!("failed to update function `{}`", func.name))?;
3398        }
3399
3400        // Filter out all of the existing functions in interface which fail the
3401        // `include_stability()` check, as they shouldn't be available.
3402        for (name, func) in mem::take(&mut iface.functions) {
3403            if resolve.include_stability(&func.stability, iface_pkg_id, None)? {
3404                iface.functions.insert(name, func);
3405            }
3406        }
3407
3408        Ok(())
3409    }
3410
3411    fn update_function(
3412        &mut self,
3413        resolve: &mut Resolve,
3414        func: &mut Function,
3415        span: Option<Span>,
3416    ) -> Result<()> {
3417        if let Some(id) = func.kind.resource_mut() {
3418            self.update_type_id(id, span)?;
3419        }
3420        for (_, ty) in func.params.iter_mut() {
3421            self.update_ty(resolve, ty, span)?;
3422        }
3423        if let Some(ty) = &mut func.result {
3424            self.update_ty(resolve, ty, span)?;
3425        }
3426
3427        if let Some(ty) = &func.result {
3428            if self.type_has_borrow(resolve, ty) {
3429                match span {
3430                    Some(span) => {
3431                        bail!(Error::new(
3432                            span,
3433                            format!(
3434                                "function returns a type which contains \
3435                                 a `borrow<T>` which is not supported"
3436                            )
3437                        ))
3438                    }
3439                    None => unreachable!(),
3440                }
3441            }
3442        }
3443
3444        Ok(())
3445    }
3446
3447    fn update_world(
3448        &mut self,
3449        world: &mut World,
3450        resolve: &mut Resolve,
3451        pkg_id: &PackageId,
3452        spans: &WorldSpan,
3453    ) -> Result<()> {
3454        assert_eq!(world.imports.len(), spans.imports.len());
3455        assert_eq!(world.exports.len(), spans.exports.len());
3456
3457        // Rewrite imports/exports with their updated versions. Note that this
3458        // may involve updating the key of the imports/exports maps so this
3459        // starts by emptying them out and then everything is re-inserted.
3460        let imports = mem::take(&mut world.imports).into_iter();
3461        let imports = imports.zip(&spans.imports).map(|p| (p, true));
3462        let exports = mem::take(&mut world.exports).into_iter();
3463        let exports = exports.zip(&spans.exports).map(|p| (p, false));
3464        for (((mut name, mut item), span), import) in imports.chain(exports) {
3465            // Update the `id` eagerly here so `item.stability(..)` below
3466            // works.
3467            if let WorldItem::Type(id) = &mut item {
3468                *id = self.map_type(*id, Some(*span))?;
3469            }
3470            let stability = item.stability(resolve);
3471            if !resolve
3472                .include_stability(stability, pkg_id, Some(*span))
3473                .with_context(|| format!("failed to process world item in `{}`", world.name))?
3474            {
3475                continue;
3476            }
3477            self.update_world_key(&mut name, Some(*span))?;
3478            match &mut item {
3479                WorldItem::Interface { id, .. } => {
3480                    *id = self.map_interface(*id, Some(*span))?;
3481                }
3482                WorldItem::Function(f) => {
3483                    self.update_function(resolve, f, Some(*span))?;
3484                }
3485                WorldItem::Type(_) => {
3486                    // already mapped above
3487                }
3488            }
3489
3490            let dst = if import {
3491                &mut world.imports
3492            } else {
3493                &mut world.exports
3494            };
3495            let prev = dst.insert(name, item);
3496            assert!(prev.is_none());
3497        }
3498
3499        Ok(())
3500    }
3501
3502    fn process_world_includes(
3503        &self,
3504        id: WorldId,
3505        resolve: &mut Resolve,
3506        pkg_id: &PackageId,
3507        spans: &WorldSpan,
3508    ) -> Result<()> {
3509        let world = &mut resolve.worlds[id];
3510        // Resolve all `include` statements of the world which will add more
3511        // entries to the imports/exports list for this world.
3512        assert_eq!(world.includes.len(), spans.includes.len());
3513        let includes = mem::take(&mut world.includes);
3514        let include_names = mem::take(&mut world.include_names);
3515        for (((stability, include_world), span), names) in includes
3516            .into_iter()
3517            .zip(&spans.includes)
3518            .zip(&include_names)
3519        {
3520            if !resolve
3521                .include_stability(&stability, pkg_id, Some(*span))
3522                .with_context(|| {
3523                    format!(
3524                        "failed to process feature gate for included world [{}] in package [{}]",
3525                        resolve.worlds[include_world].name.as_str(),
3526                        resolve.packages[*pkg_id].name
3527                    )
3528                })?
3529            {
3530                continue;
3531            }
3532            self.resolve_include(id, include_world, names, *span, pkg_id, resolve)?;
3533        }
3534
3535        Ok(())
3536    }
3537
3538    fn update_world_key(&self, key: &mut WorldKey, span: Option<Span>) -> Result<()> {
3539        match key {
3540            WorldKey::Name(_) => {}
3541            WorldKey::Interface(id) => {
3542                *id = self.map_interface(*id, span)?;
3543            }
3544        }
3545        Ok(())
3546    }
3547
3548    fn resolve_include(
3549        &self,
3550        id: WorldId,
3551        include_world_id_orig: WorldId,
3552        names: &[IncludeName],
3553        span: Span,
3554        pkg_id: &PackageId,
3555        resolve: &mut Resolve,
3556    ) -> Result<()> {
3557        let world = &resolve.worlds[id];
3558        let include_world_id = self.map_world(include_world_id_orig, Some(span))?;
3559        let include_world = resolve.worlds[include_world_id].clone();
3560        let mut names_ = names.to_owned();
3561        let is_external_include = world.package != include_world.package;
3562
3563        // remove all imports and exports that match the names we're including
3564        for import in include_world.imports.iter() {
3565            self.remove_matching_name(import, &mut names_);
3566        }
3567        for export in include_world.exports.iter() {
3568            self.remove_matching_name(export, &mut names_);
3569        }
3570        if !names_.is_empty() {
3571            bail!(Error::new(
3572                span,
3573                format!(
3574                    "no import or export kebab-name `{}`. Note that an ID does not support renaming",
3575                    names_[0].name
3576                ),
3577            ));
3578        }
3579
3580        let mut cloner = clone::Cloner::new(
3581            resolve,
3582            TypeOwner::World(if is_external_include {
3583                include_world_id
3584            } else {
3585                include_world_id
3586                // include_world_id_orig
3587            }),
3588            TypeOwner::World(id),
3589        );
3590        cloner.new_package = Some(*pkg_id);
3591
3592        // copy the imports and exports from the included world into the current world
3593        for import in include_world.imports.iter() {
3594            self.resolve_include_item(
3595                &mut cloner,
3596                names,
3597                |resolve| &mut resolve.worlds[id].imports,
3598                import,
3599                span,
3600                "import",
3601                is_external_include,
3602            )?;
3603        }
3604
3605        for export in include_world.exports.iter() {
3606            self.resolve_include_item(
3607                &mut cloner,
3608                names,
3609                |resolve| &mut resolve.worlds[id].exports,
3610                export,
3611                span,
3612                "export",
3613                is_external_include,
3614            )?;
3615        }
3616        Ok(())
3617    }
3618
3619    fn resolve_include_item(
3620        &self,
3621        cloner: &mut clone::Cloner<'_>,
3622        names: &[IncludeName],
3623        get_items: impl Fn(&mut Resolve) -> &mut IndexMap<WorldKey, WorldItem>,
3624        item: (&WorldKey, &WorldItem),
3625        span: Span,
3626        item_type: &str,
3627        is_external_include: bool,
3628    ) -> Result<()> {
3629        match item.0 {
3630            WorldKey::Name(n) => {
3631                let n = if let Some(found) = names
3632                    .into_iter()
3633                    .find(|include_name| include_name.name == n.clone())
3634                {
3635                    found.as_.clone()
3636                } else {
3637                    n.clone()
3638                };
3639
3640                // When the `with` option to the `include` directive is
3641                // specified and is used to rename a function that means that
3642                // the function's own original name needs to be updated, so
3643                // reflect the change not only in the world key but additionally
3644                // in the function itself.
3645                let mut new_item = item.1.clone();
3646                let key = WorldKey::Name(n.clone());
3647                cloner.world_item(&key, &mut new_item);
3648                match &mut new_item {
3649                    WorldItem::Function(f) => f.name = n.clone(),
3650                    WorldItem::Type(id) => cloner.resolve.types[*id].name = Some(n.clone()),
3651                    WorldItem::Interface { .. } => {}
3652                }
3653
3654                let prev = get_items(cloner.resolve).insert(key, new_item);
3655                if prev.is_some() {
3656                    bail!(Error::new(
3657                        span,
3658                        format!("{item_type} of `{n}` shadows previously {item_type}ed items"),
3659                    ))
3660                }
3661            }
3662            key @ WorldKey::Interface(_) => {
3663                let prev = get_items(cloner.resolve)
3664                    .entry(key.clone())
3665                    .or_insert(item.1.clone());
3666                match (&item.1, prev) {
3667                    (
3668                        WorldItem::Interface {
3669                            id: aid,
3670                            stability: astability,
3671                        },
3672                        WorldItem::Interface {
3673                            id: bid,
3674                            stability: bstability,
3675                        },
3676                    ) => {
3677                        assert_eq!(*aid, *bid);
3678                        merge_include_stability(astability, bstability, is_external_include)?;
3679                    }
3680                    (WorldItem::Interface { .. }, _) => unreachable!(),
3681                    (WorldItem::Function(_), _) => unreachable!(),
3682                    (WorldItem::Type(_), _) => unreachable!(),
3683                }
3684            }
3685        };
3686
3687        Ok(())
3688    }
3689
3690    fn remove_matching_name(&self, item: (&WorldKey, &WorldItem), names: &mut Vec<IncludeName>) {
3691        match item.0 {
3692            WorldKey::Name(n) => {
3693                names.retain(|name| name.name != n.clone());
3694            }
3695            _ => {}
3696        }
3697    }
3698
3699    fn type_has_borrow(&mut self, resolve: &Resolve, ty: &Type) -> bool {
3700        let id = match ty {
3701            Type::Id(id) => *id,
3702            _ => return false,
3703        };
3704
3705        if let Some(Some(has_borrow)) = self.type_has_borrow.get(id.index()) {
3706            return *has_borrow;
3707        }
3708
3709        let result = self.typedef_has_borrow(resolve, &resolve.types[id]);
3710        if self.type_has_borrow.len() <= id.index() {
3711            self.type_has_borrow.resize(id.index() + 1, None);
3712        }
3713        self.type_has_borrow[id.index()] = Some(result);
3714        result
3715    }
3716
3717    fn typedef_has_borrow(&mut self, resolve: &Resolve, ty: &TypeDef) -> bool {
3718        match &ty.kind {
3719            TypeDefKind::Type(t) => self.type_has_borrow(resolve, t),
3720            TypeDefKind::Variant(v) => v
3721                .cases
3722                .iter()
3723                .filter_map(|case| case.ty.as_ref())
3724                .any(|ty| self.type_has_borrow(resolve, ty)),
3725            TypeDefKind::Handle(Handle::Borrow(_)) => true,
3726            TypeDefKind::Handle(Handle::Own(_)) => false,
3727            TypeDefKind::Resource => false,
3728            TypeDefKind::Record(r) => r
3729                .fields
3730                .iter()
3731                .any(|case| self.type_has_borrow(resolve, &case.ty)),
3732            TypeDefKind::Flags(_) => false,
3733            TypeDefKind::Tuple(t) => t.types.iter().any(|t| self.type_has_borrow(resolve, t)),
3734            TypeDefKind::Enum(_) => false,
3735            TypeDefKind::List(ty)
3736            | TypeDefKind::FixedSizeList(ty, ..)
3737            | TypeDefKind::Future(Some(ty))
3738            | TypeDefKind::Stream(Some(ty))
3739            | TypeDefKind::Option(ty) => self.type_has_borrow(resolve, ty),
3740            TypeDefKind::Result(r) => [&r.ok, &r.err]
3741                .iter()
3742                .filter_map(|t| t.as_ref())
3743                .any(|t| self.type_has_borrow(resolve, t)),
3744            TypeDefKind::Future(None) | TypeDefKind::Stream(None) => false,
3745            TypeDefKind::Unknown => unreachable!(),
3746        }
3747    }
3748}
3749
3750struct MergeMap<'a> {
3751    /// A map of package ids in `from` to those in `into` for those that are
3752    /// found to be equivalent.
3753    package_map: HashMap<PackageId, PackageId>,
3754
3755    /// A map of interface ids in `from` to those in `into` for those that are
3756    /// found to be equivalent.
3757    interface_map: HashMap<InterfaceId, InterfaceId>,
3758
3759    /// A map of type ids in `from` to those in `into` for those that are
3760    /// found to be equivalent.
3761    type_map: HashMap<TypeId, TypeId>,
3762
3763    /// A map of world ids in `from` to those in `into` for those that are
3764    /// found to be equivalent.
3765    world_map: HashMap<WorldId, WorldId>,
3766
3767    /// A list of documents that need to be added to packages in `into`.
3768    ///
3769    /// The elements here are:
3770    ///
3771    /// * The name of the interface/world
3772    /// * The ID within `into` of the package being added to
3773    /// * The ID within `from` of the item being added.
3774    interfaces_to_add: Vec<(String, PackageId, InterfaceId)>,
3775    worlds_to_add: Vec<(String, PackageId, WorldId)>,
3776
3777    /// Which `Resolve` is being merged from.
3778    from: &'a Resolve,
3779
3780    /// Which `Resolve` is being merged into.
3781    into: &'a Resolve,
3782}
3783
3784impl<'a> MergeMap<'a> {
3785    fn new(from: &'a Resolve, into: &'a Resolve) -> MergeMap<'a> {
3786        MergeMap {
3787            package_map: Default::default(),
3788            interface_map: Default::default(),
3789            type_map: Default::default(),
3790            world_map: Default::default(),
3791            interfaces_to_add: Default::default(),
3792            worlds_to_add: Default::default(),
3793            from,
3794            into,
3795        }
3796    }
3797
3798    fn build(&mut self) -> Result<()> {
3799        for from_id in self.from.topological_packages() {
3800            let from = &self.from.packages[from_id];
3801            let into_id = match self.into.package_names.get(&from.name) {
3802                Some(id) => *id,
3803
3804                // This package, according to its name and url, is not present
3805                // in `self` so it needs to get added below.
3806                None => {
3807                    log::trace!("adding unique package {}", from.name);
3808                    continue;
3809                }
3810            };
3811            log::trace!("merging duplicate package {}", from.name);
3812
3813            self.build_package(from_id, into_id).with_context(|| {
3814                format!("failed to merge package `{}` into existing copy", from.name)
3815            })?;
3816        }
3817
3818        Ok(())
3819    }
3820
3821    fn build_package(&mut self, from_id: PackageId, into_id: PackageId) -> Result<()> {
3822        let prev = self.package_map.insert(from_id, into_id);
3823        assert!(prev.is_none());
3824
3825        let from = &self.from.packages[from_id];
3826        let into = &self.into.packages[into_id];
3827
3828        // If an interface is present in `from_id` but not present in `into_id`
3829        // then it can be copied over wholesale. That copy is scheduled to
3830        // happen within the `self.interfaces_to_add` list.
3831        for (name, from_interface_id) in from.interfaces.iter() {
3832            let into_interface_id = match into.interfaces.get(name) {
3833                Some(id) => *id,
3834                None => {
3835                    log::trace!("adding unique interface {name}");
3836                    self.interfaces_to_add
3837                        .push((name.clone(), into_id, *from_interface_id));
3838                    continue;
3839                }
3840            };
3841
3842            log::trace!("merging duplicate interfaces {name}");
3843            self.build_interface(*from_interface_id, into_interface_id)
3844                .with_context(|| format!("failed to merge interface `{name}`"))?;
3845        }
3846
3847        for (name, from_world_id) in from.worlds.iter() {
3848            let into_world_id = match into.worlds.get(name) {
3849                Some(id) => *id,
3850                None => {
3851                    log::trace!("adding unique world {name}");
3852                    self.worlds_to_add
3853                        .push((name.clone(), into_id, *from_world_id));
3854                    continue;
3855                }
3856            };
3857
3858            log::trace!("merging duplicate worlds {name}");
3859            self.build_world(*from_world_id, into_world_id)
3860                .with_context(|| format!("failed to merge world `{name}`"))?;
3861        }
3862
3863        Ok(())
3864    }
3865
3866    fn build_interface(&mut self, from_id: InterfaceId, into_id: InterfaceId) -> Result<()> {
3867        let prev = self.interface_map.insert(from_id, into_id);
3868        assert!(prev.is_none());
3869
3870        let from_interface = &self.from.interfaces[from_id];
3871        let into_interface = &self.into.interfaces[into_id];
3872
3873        // Unlike documents/interfaces above if an interface in `from`
3874        // differs from the interface in `into` then that's considered an
3875        // error. Changing interfaces can reflect changes in imports/exports
3876        // which may not be expected so it's currently required that all
3877        // interfaces, when merged, exactly match.
3878        //
3879        // One case to consider here, for example, is that if a world in
3880        // `into` exports the interface `into_id` then if `from_id` were to
3881        // add more items into `into` then it would unexpectedly require more
3882        // items to be exported which may not work. In an import context this
3883        // might work since it's "just more items available for import", but
3884        // for now a conservative route of "interfaces must match" is taken.
3885
3886        for (name, from_type_id) in from_interface.types.iter() {
3887            let into_type_id = *into_interface
3888                .types
3889                .get(name)
3890                .ok_or_else(|| anyhow!("expected type `{name}` to be present"))?;
3891            let prev = self.type_map.insert(*from_type_id, into_type_id);
3892            assert!(prev.is_none());
3893
3894            self.build_type_id(*from_type_id, into_type_id)
3895                .with_context(|| format!("mismatch in type `{name}`"))?;
3896        }
3897
3898        for (name, from_func) in from_interface.functions.iter() {
3899            let into_func = match into_interface.functions.get(name) {
3900                Some(func) => func,
3901                None => bail!("expected function `{name}` to be present"),
3902            };
3903            self.build_function(from_func, into_func)
3904                .with_context(|| format!("mismatch in function `{name}`"))?;
3905        }
3906
3907        Ok(())
3908    }
3909
3910    fn build_type_id(&mut self, from_id: TypeId, into_id: TypeId) -> Result<()> {
3911        // FIXME: ideally the types should be "structurally
3912        // equal" but that's not trivial to do in the face of
3913        // resources.
3914        let _ = from_id;
3915        let _ = into_id;
3916        Ok(())
3917    }
3918
3919    fn build_type(&mut self, from_ty: &Type, into_ty: &Type) -> Result<()> {
3920        match (from_ty, into_ty) {
3921            (Type::Id(from), Type::Id(into)) => {
3922                self.build_type_id(*from, *into)?;
3923            }
3924            (from, into) if from != into => bail!("different kinds of types"),
3925            _ => {}
3926        }
3927        Ok(())
3928    }
3929
3930    fn build_function(&mut self, from_func: &Function, into_func: &Function) -> Result<()> {
3931        if from_func.name != into_func.name {
3932            bail!(
3933                "different function names `{}` and `{}`",
3934                from_func.name,
3935                into_func.name
3936            );
3937        }
3938        match (&from_func.kind, &into_func.kind) {
3939            (FunctionKind::Freestanding, FunctionKind::Freestanding) => {}
3940            (FunctionKind::AsyncFreestanding, FunctionKind::AsyncFreestanding) => {}
3941
3942            (FunctionKind::Method(from), FunctionKind::Method(into))
3943            | (FunctionKind::Static(from), FunctionKind::Static(into))
3944            | (FunctionKind::AsyncMethod(from), FunctionKind::AsyncMethod(into))
3945            | (FunctionKind::AsyncStatic(from), FunctionKind::AsyncStatic(into))
3946            | (FunctionKind::Constructor(from), FunctionKind::Constructor(into)) => {
3947                self.build_type_id(*from, *into)
3948                    .context("different function kind types")?;
3949            }
3950
3951            (FunctionKind::Method(_), _)
3952            | (FunctionKind::Constructor(_), _)
3953            | (FunctionKind::Static(_), _)
3954            | (FunctionKind::Freestanding, _)
3955            | (FunctionKind::AsyncFreestanding, _)
3956            | (FunctionKind::AsyncMethod(_), _)
3957            | (FunctionKind::AsyncStatic(_), _) => {
3958                bail!("different function kind types")
3959            }
3960        }
3961
3962        if from_func.params.len() != into_func.params.len() {
3963            bail!("different number of function parameters");
3964        }
3965        for ((from_name, from_ty), (into_name, into_ty)) in
3966            from_func.params.iter().zip(&into_func.params)
3967        {
3968            if from_name != into_name {
3969                bail!("different function parameter names: {from_name} != {into_name}");
3970            }
3971            self.build_type(from_ty, into_ty)
3972                .with_context(|| format!("different function parameter types for `{from_name}`"))?;
3973        }
3974        match (&from_func.result, &into_func.result) {
3975            (Some(from_ty), Some(into_ty)) => {
3976                self.build_type(from_ty, into_ty)
3977                    .context("different function result types")?;
3978            }
3979            (None, None) => {}
3980            (Some(_), None) | (None, Some(_)) => bail!("different number of function results"),
3981        }
3982        Ok(())
3983    }
3984
3985    fn build_world(&mut self, from_id: WorldId, into_id: WorldId) -> Result<()> {
3986        let prev = self.world_map.insert(from_id, into_id);
3987        assert!(prev.is_none());
3988
3989        let from_world = &self.from.worlds[from_id];
3990        let into_world = &self.into.worlds[into_id];
3991
3992        // Same as interfaces worlds are expected to exactly match to avoid
3993        // unexpectedly changing a particular component's view of imports and
3994        // exports.
3995        //
3996        // FIXME: this should probably share functionality with
3997        // `Resolve::merge_worlds` to support adding imports but not changing
3998        // exports.
3999
4000        if from_world.imports.len() != into_world.imports.len() {
4001            bail!("world contains different number of imports than expected");
4002        }
4003        if from_world.exports.len() != into_world.exports.len() {
4004            bail!("world contains different number of exports than expected");
4005        }
4006
4007        for (from_name, from) in from_world.imports.iter() {
4008            let into_name = MergeMap::map_name(from_name, &self.interface_map);
4009            let name_str = self.from.name_world_key(from_name);
4010            let into = into_world
4011                .imports
4012                .get(&into_name)
4013                .ok_or_else(|| anyhow!("import `{name_str}` not found in target world"))?;
4014            self.match_world_item(from, into)
4015                .with_context(|| format!("import `{name_str}` didn't match target world"))?;
4016        }
4017
4018        for (from_name, from) in from_world.exports.iter() {
4019            let into_name = MergeMap::map_name(from_name, &self.interface_map);
4020            let name_str = self.from.name_world_key(from_name);
4021            let into = into_world
4022                .exports
4023                .get(&into_name)
4024                .ok_or_else(|| anyhow!("export `{name_str}` not found in target world"))?;
4025            self.match_world_item(from, into)
4026                .with_context(|| format!("export `{name_str}` didn't match target world"))?;
4027        }
4028
4029        Ok(())
4030    }
4031
4032    fn map_name(
4033        from_name: &WorldKey,
4034        interface_map: &HashMap<InterfaceId, InterfaceId>,
4035    ) -> WorldKey {
4036        match from_name {
4037            WorldKey::Name(s) => WorldKey::Name(s.clone()),
4038            WorldKey::Interface(id) => {
4039                WorldKey::Interface(interface_map.get(id).copied().unwrap_or(*id))
4040            }
4041        }
4042    }
4043
4044    fn match_world_item(&mut self, from: &WorldItem, into: &WorldItem) -> Result<()> {
4045        match (from, into) {
4046            (WorldItem::Interface { id: from, .. }, WorldItem::Interface { id: into, .. }) => {
4047                match (
4048                    &self.from.interfaces[*from].name,
4049                    &self.into.interfaces[*into].name,
4050                ) {
4051                    // If one interface is unnamed then they must both be
4052                    // unnamed and they must both have the same structure for
4053                    // now.
4054                    (None, None) => self.build_interface(*from, *into)?,
4055
4056                    // Otherwise both interfaces must be named and they must
4057                    // have been previously found to be equivalent. Note that
4058                    // if either is unnamed it won't be present in
4059                    // `interface_map` so this'll return an error.
4060                    _ => {
4061                        if self.interface_map.get(&from) != Some(&into) {
4062                            bail!("interfaces are not the same");
4063                        }
4064                    }
4065                }
4066            }
4067            (WorldItem::Function(from), WorldItem::Function(into)) => {
4068                let _ = (from, into);
4069                // FIXME: should assert an check that `from` structurally
4070                // matches `into`
4071            }
4072            (WorldItem::Type(from), WorldItem::Type(into)) => {
4073                // FIXME: should assert an check that `from` structurally
4074                // matches `into`
4075                let prev = self.type_map.insert(*from, *into);
4076                assert!(prev.is_none());
4077            }
4078
4079            (WorldItem::Interface { .. }, _)
4080            | (WorldItem::Function(_), _)
4081            | (WorldItem::Type(_), _) => {
4082                bail!("world items do not have the same type")
4083            }
4084        }
4085        Ok(())
4086    }
4087}
4088
4089/// Updates stability annotations when merging `from` into `into`.
4090///
4091/// This is done to keep up-to-date stability information if possible.
4092/// Components for example don't carry stability information but WIT does so
4093/// this tries to move from "unknown" to stable/unstable if possible.
4094fn update_stability(from: &Stability, into: &mut Stability) -> Result<()> {
4095    // If `from` is unknown or the two stability annotations are equal then
4096    // there's nothing to do here.
4097    if from == into || from.is_unknown() {
4098        return Ok(());
4099    }
4100    // Otherwise if `into` is unknown then inherit the stability listed in
4101    // `from`.
4102    if into.is_unknown() {
4103        *into = from.clone();
4104        return Ok(());
4105    }
4106
4107    // Failing all that this means that the two attributes are different so
4108    // generate an error.
4109    bail!("mismatch in stability from '{:?}' to '{:?}'", from, into)
4110}
4111
4112fn merge_include_stability(
4113    from: &Stability,
4114    into: &mut Stability,
4115    is_external_include: bool,
4116) -> Result<()> {
4117    if is_external_include && from.is_stable() {
4118        log::trace!("dropped stability from external package");
4119        *into = Stability::Unknown;
4120        return Ok(());
4121    }
4122
4123    return update_stability(from, into);
4124}
4125
4126/// An error that can be returned during "world elaboration" during various
4127/// [`Resolve`] operations.
4128///
4129/// Methods on [`Resolve`] which mutate its internals, such as
4130/// [`Resolve::push_dir`] or [`Resolve::importize`] can fail if `world` imports
4131/// in WIT packages are invalid. This error indicates one of these situations
4132/// where an invalid dependency graph between imports and exports are detected.
4133///
4134/// Note that at this time this error is subtle and not easy to understand, and
4135/// work needs to be done to explain this better and additionally provide a
4136/// better error message. For now though this type enables callers to test for
4137/// the exact kind of error emitted.
4138#[derive(Debug, Clone)]
4139pub struct InvalidTransitiveDependency(String);
4140
4141impl fmt::Display for InvalidTransitiveDependency {
4142    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4143        write!(
4144            f,
4145            "interface `{}` transitively depends on an interface in \
4146             incompatible ways",
4147            self.0
4148        )
4149    }
4150}
4151
4152impl std::error::Error for InvalidTransitiveDependency {}
4153
4154#[cfg(test)]
4155mod tests {
4156    use crate::Resolve;
4157    use anyhow::Result;
4158
4159    #[test]
4160    fn select_world() -> Result<()> {
4161        let mut resolve = Resolve::default();
4162        resolve.push_str(
4163            "test.wit",
4164            r#"
4165                package foo:bar@0.1.0;
4166
4167                world foo {}
4168            "#,
4169        )?;
4170        resolve.push_str(
4171            "test.wit",
4172            r#"
4173                package foo:baz@0.1.0;
4174
4175                world foo {}
4176            "#,
4177        )?;
4178        resolve.push_str(
4179            "test.wit",
4180            r#"
4181                package foo:baz@0.2.0;
4182
4183                world foo {}
4184            "#,
4185        )?;
4186
4187        let dummy = resolve.push_str(
4188            "test.wit",
4189            r#"
4190                package foo:dummy;
4191
4192                world foo {}
4193            "#,
4194        )?;
4195
4196        assert!(resolve.select_world(&[dummy], None).is_ok());
4197        assert!(resolve.select_world(&[dummy], Some("xx")).is_err());
4198        assert!(resolve.select_world(&[dummy], Some("")).is_err());
4199        assert!(resolve.select_world(&[dummy], Some("foo:bar/foo")).is_ok());
4200        assert!(
4201            resolve
4202                .select_world(&[dummy], Some("foo:bar/foo@0.1.0"))
4203                .is_ok()
4204        );
4205        assert!(resolve.select_world(&[dummy], Some("foo:baz/foo")).is_err());
4206        assert!(
4207            resolve
4208                .select_world(&[dummy], Some("foo:baz/foo@0.1.0"))
4209                .is_ok()
4210        );
4211        assert!(
4212            resolve
4213                .select_world(&[dummy], Some("foo:baz/foo@0.2.0"))
4214                .is_ok()
4215        );
4216        Ok(())
4217    }
4218
4219    /// When there are multiple packages and there's no main package, don't
4220    /// pick a world just based on it being the only one that matches.
4221    #[test]
4222    fn select_world_multiple_packages() -> Result<()> {
4223        use wit_parser::Resolve;
4224
4225        let mut resolve = Resolve::default();
4226
4227        // Just one world in one package; we always succeed.
4228        let stuff = resolve.push_str(
4229            "./my-test.wit",
4230            r#"
4231                    package test:stuff;
4232
4233                    world foo {
4234                        // ...
4235                    }
4236                "#,
4237        )?;
4238        assert!(resolve.select_world(&[stuff], None).is_ok());
4239        assert!(resolve.select_world(&[stuff], Some("foo")).is_ok());
4240
4241        // Multiple packages, but still just one total world. Lookups
4242        // without a main package now fail.
4243        let empty = resolve.push_str(
4244            "./my-test.wit",
4245            r#"
4246                    package test:empty;
4247                "#,
4248        )?;
4249        assert!(resolve.select_world(&[stuff, empty], None).is_err());
4250        assert!(resolve.select_world(&[stuff, empty], Some("foo")).is_err());
4251        assert!(resolve.select_world(&[empty], None).is_err());
4252        assert!(resolve.select_world(&[empty], Some("foo")).is_err());
4253
4254        Ok(())
4255    }
4256
4257    /// Test selecting a world with multiple versions of a package name.
4258    #[test]
4259    fn select_world_versions() -> Result<()> {
4260        use wit_parser::Resolve;
4261
4262        let mut resolve = Resolve::default();
4263
4264        let _id = resolve.push_str(
4265            "./my-test.wit",
4266            r#"
4267                    package example:distraction;
4268                "#,
4269        )?;
4270
4271        // When selecting with a version it's ok to drop the version when
4272        // there's only a single copy of that package in `Resolve`.
4273        let versions_1 = resolve.push_str(
4274            "./my-test.wit",
4275            r#"
4276                    package example:versions@1.0.0;
4277
4278                    world foo { /* ... */ }
4279                "#,
4280        )?;
4281        assert!(resolve.select_world(&[versions_1], Some("foo")).is_ok());
4282        assert!(
4283            resolve
4284                .select_world(&[versions_1], Some("foo@1.0.0"))
4285                .is_err()
4286        );
4287        assert!(
4288            resolve
4289                .select_world(&[versions_1], Some("example:versions/foo"))
4290                .is_ok()
4291        );
4292        assert!(
4293            resolve
4294                .select_world(&[versions_1], Some("example:versions/foo@1.0.0"))
4295                .is_ok()
4296        );
4297
4298        // However when a single package has multiple versions in a resolve
4299        // it's required to specify the version to select which one.
4300        let versions_2 = resolve.push_str(
4301            "./my-test.wit",
4302            r#"
4303                    package example:versions@2.0.0;
4304
4305                    world foo { /* ... */ }
4306                "#,
4307        )?;
4308        assert!(
4309            resolve
4310                .select_world(&[versions_1, versions_2], Some("foo"))
4311                .is_err()
4312        );
4313        assert!(
4314            resolve
4315                .select_world(&[versions_1, versions_2], Some("foo@1.0.0"))
4316                .is_err()
4317        );
4318        assert!(
4319            resolve
4320                .select_world(&[versions_1, versions_2], Some("foo@2.0.0"))
4321                .is_err()
4322        );
4323        assert!(
4324            resolve
4325                .select_world(&[versions_1, versions_2], Some("example:versions/foo"))
4326                .is_err()
4327        );
4328        assert!(
4329            resolve
4330                .select_world(
4331                    &[versions_1, versions_2],
4332                    Some("example:versions/foo@1.0.0")
4333                )
4334                .is_ok()
4335        );
4336        assert!(
4337            resolve
4338                .select_world(
4339                    &[versions_1, versions_2],
4340                    Some("example:versions/foo@2.0.0")
4341                )
4342                .is_ok()
4343        );
4344
4345        Ok(())
4346    }
4347
4348    /// Test overriding a main package using name qualification
4349    #[test]
4350    fn select_world_override_qualification() -> Result<()> {
4351        use wit_parser::Resolve;
4352
4353        let mut resolve = Resolve::default();
4354
4355        let other = resolve.push_str(
4356            "./my-test.wit",
4357            r#"
4358                    package example:other;
4359
4360                    world foo { }
4361                "#,
4362        )?;
4363
4364        // A fully-qualified name overrides a main package.
4365        let fq = resolve.push_str(
4366            "./my-test.wit",
4367            r#"
4368                    package example:fq;
4369
4370                    world bar { }
4371                "#,
4372        )?;
4373        assert!(resolve.select_world(&[other, fq], Some("foo")).is_err());
4374        assert!(resolve.select_world(&[other, fq], Some("bar")).is_err());
4375        assert!(
4376            resolve
4377                .select_world(&[other, fq], Some("example:other/foo"))
4378                .is_ok()
4379        );
4380        assert!(
4381            resolve
4382                .select_world(&[other, fq], Some("example:fq/bar"))
4383                .is_ok()
4384        );
4385        assert!(
4386            resolve
4387                .select_world(&[other, fq], Some("example:other/bar"))
4388                .is_err()
4389        );
4390        assert!(
4391            resolve
4392                .select_world(&[other, fq], Some("example:fq/foo"))
4393                .is_err()
4394        );
4395
4396        Ok(())
4397    }
4398
4399    /// Test selecting with fully-qualified world names.
4400    #[test]
4401    fn select_world_fully_qualified() -> Result<()> {
4402        use wit_parser::Resolve;
4403
4404        let mut resolve = Resolve::default();
4405
4406        let distraction = resolve.push_str(
4407            "./my-test.wit",
4408            r#"
4409                    package example:distraction;
4410                "#,
4411        )?;
4412
4413        // If a package has multiple worlds, then we can't guess the world
4414        // even if we know the package.
4415        let multiworld = resolve.push_str(
4416            "./my-test.wit",
4417            r#"
4418                    package example:multiworld;
4419
4420                    world foo { /* ... */ }
4421
4422                    world bar { /* ... */ }
4423                "#,
4424        )?;
4425        assert!(
4426            resolve
4427                .select_world(&[distraction, multiworld], None)
4428                .is_err()
4429        );
4430        assert!(
4431            resolve
4432                .select_world(&[distraction, multiworld], Some("foo"))
4433                .is_err()
4434        );
4435        assert!(
4436            resolve
4437                .select_world(&[distraction, multiworld], Some("example:multiworld/foo"))
4438                .is_ok()
4439        );
4440        assert!(
4441            resolve
4442                .select_world(&[distraction, multiworld], Some("bar"))
4443                .is_err()
4444        );
4445        assert!(
4446            resolve
4447                .select_world(&[distraction, multiworld], Some("example:multiworld/bar"))
4448                .is_ok()
4449        );
4450
4451        Ok(())
4452    }
4453
4454    /// Test `select_world` with single and multiple packages.
4455    #[test]
4456    fn select_world_packages() -> Result<()> {
4457        use wit_parser::Resolve;
4458
4459        let mut resolve = Resolve::default();
4460
4461        // If there's a single package and only one world, that world is
4462        // the obvious choice.
4463        let wit1 = resolve.push_str(
4464            "./my-test.wit",
4465            r#"
4466                    package example:wit1;
4467
4468                    world foo {
4469                        // ...
4470                    }
4471                "#,
4472        )?;
4473        assert!(resolve.select_world(&[wit1], None).is_ok());
4474        assert!(resolve.select_world(&[wit1], Some("foo")).is_ok());
4475        assert!(
4476            resolve
4477                .select_world(&[wit1], Some("example:wit1/foo"))
4478                .is_ok()
4479        );
4480        assert!(resolve.select_world(&[wit1], Some("bar")).is_err());
4481        assert!(
4482            resolve
4483                .select_world(&[wit1], Some("example:wit2/foo"))
4484                .is_err()
4485        );
4486
4487        // If there are multiple packages, we need to be told which package
4488        // to use.
4489        let wit2 = resolve.push_str(
4490            "./my-test.wit",
4491            r#"
4492                    package example:wit2;
4493
4494                    world foo { /* ... */ }
4495                "#,
4496        )?;
4497        assert!(resolve.select_world(&[wit1, wit2], None).is_err());
4498        assert!(resolve.select_world(&[wit1, wit2], Some("foo")).is_err());
4499        assert!(
4500            resolve
4501                .select_world(&[wit1, wit2], Some("example:wit1/foo"))
4502                .is_ok()
4503        );
4504        assert!(resolve.select_world(&[wit2], None).is_ok());
4505        assert!(resolve.select_world(&[wit2], Some("foo")).is_ok());
4506        assert!(
4507            resolve
4508                .select_world(&[wit2], Some("example:wit1/foo"))
4509                .is_ok()
4510        );
4511        assert!(resolve.select_world(&[wit1, wit2], Some("bar")).is_err());
4512        assert!(
4513            resolve
4514                .select_world(&[wit1, wit2], Some("example:wit2/foo"))
4515                .is_ok()
4516        );
4517        assert!(resolve.select_world(&[wit2], Some("bar")).is_err());
4518        assert!(
4519            resolve
4520                .select_world(&[wit2], Some("example:wit2/foo"))
4521                .is_ok()
4522        );
4523
4524        Ok(())
4525    }
4526}