wirm 4.0.0-rc2

A lightweight WebAssembly Transformation Library for the Component Model
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//! Fully-resolved, index-free representations of WebAssembly component types.
//!
//! These types are produced by [`Component::concretize_import`] and
//! [`Component::concretize_export`], which follow the entire reference chain
//! (outer aliases, imports, nested scopes) and return concrete Rust types with
//! no remaining index references.
//!
//! # WIT interface focus
//!
//! The current implementation is scoped to the needs of [WIT]-defined interfaces,
//! where component imports and exports are either:
//!
//! - **Instance types** whose exports are all **functions** (`ComponentType::Instance`
//!   with `ComponentTypeRef::Func` exports), or
//! - **Function types** (`ComponentType::Func`).
//!
//! Non-function instance exports (nested instances, type exports, value exports) are
//! intentionally skipped.  This covers the full surface of every WIT interface today.
//!
//! [WIT]: https://component-model.bytecodealliance.org/design/wit.html
//!
//! # TODO: extend beyond WIT interfaces
//!
//! A future release should generalise [`ConcreteType::Instance`] to carry all export
//! kinds, not just functions.  Candidates include:
//!
//! - Type exports (resource declarations, defined types)
//! - Nested instance exports
//! - Value exports
//!
//! Until then, non-function exports silently produce no entry in the `Instance` vec.

use crate::ir::component::idx_spaces::Space;
use crate::ir::component::refs::{Depth, GetCompRefs, GetItemRef, GetTypeRefs, IndexedRef};
use crate::ir::component::visitor::utils::{TypeBodyDecls, VisitCtxInner};
use crate::ir::component::visitor::{ResolvedItem, VisitCtx};
use crate::Component;
use wasmparser::{
    ComponentAlias, ComponentDefinedType, ComponentExport, ComponentExternalKind,
    ComponentFuncType, ComponentInstance, ComponentType, ComponentValType, InstanceTypeDeclaration,
    PrimitiveValType,
};
// ============================================================
// Public output types
// ============================================================

/// A fully-resolved component type with no remaining index references.
///
/// Produced by [`Component::concretize_import`] and [`Component::concretize_export`].
#[derive(Debug, Clone)]
pub enum ConcreteType<'a> {
    /// A WIT instance interface — a named set of exported functions.
    ///
    /// Each entry is `(function_name, signature)`.  Only `ComponentTypeRef::Func`
    /// exports are included; this matches the WIT interface model where every
    /// instance export is a function.  See the [module-level TODO](self) for plans
    /// to extend this to other export kinds in a future release.
    Instance(Vec<(&'a str, ConcreteFuncType<'a>)>),
    /// A single function type.
    Func(ConcreteFuncType<'a>),
    /// A resource (own or borrow handle).
    Resource,
}

/// A fully-resolved function signature with no index references.
#[derive(Debug, Clone)]
pub struct ConcreteFuncType<'a> {
    /// Named parameters.
    pub params: Vec<(&'a str, ConcreteValType<'a>)>,
    /// Return type, if any.
    pub result: Option<ConcreteValType<'a>>,
}

/// A fully-resolved value type with no index references.
#[derive(Debug, Clone)]
pub enum ConcreteValType<'a> {
    Primitive(PrimitiveValType),
    Record(Vec<(&'a str, Box<ConcreteValType<'a>>)>),
    Variant(Vec<(&'a str, Option<Box<ConcreteValType<'a>>>)>),
    List(Box<ConcreteValType<'a>>),
    Tuple(Vec<ConcreteValType<'a>>),
    Option(Box<ConcreteValType<'a>>),
    Result {
        ok: Option<Box<ConcreteValType<'a>>>,
        err: Option<Box<ConcreteValType<'a>>>,
    },
    Flags(Vec<&'a str>),
    Enum(Vec<&'a str>),
    Map(Box<ConcreteValType<'a>>, Box<ConcreteValType<'a>>),
    FixedSizeList(Box<ConcreteValType<'a>>, u32),
    /// A resource handle (`own<T>` or `borrow<T>`).
    Resource,
    /// An async handle (`future<T>` or `stream<T>`).
    AsyncHandle,
}

// ============================================================
// Public API and helpers on Component
// ============================================================

impl<'a> Component<'a> {
    /// Resolve an import by name to its fully-concrete type.
    ///
    /// Follows all alias chains, outer references, and index lookups so that
    /// the returned [`ConcreteType`] contains no remaining index references.
    ///
    /// Returns `None` if no import with the given name exists, or if its type
    /// is not one wirm currently concretizes (e.g. a raw module import).
    pub fn concretize_import(&'a self, name: &str) -> Option<ConcreteType<'a>> {
        match self.resolve_named_import(name)? {
            ResolvedItem::CompType(_, ty) => concretize_comp_type(self, ty),
            _ => None,
        }
    }

    /// Resolve an export by name to its fully-concrete type.
    ///
    /// Follows all alias chains, outer references, and index lookups so that
    /// the returned [`ConcreteType`] contains no remaining index references.
    ///
    /// Returns `None` if no export with the given name exists, or if its type
    /// is not one wirm currently concretizes.
    pub fn concretize_export(&'a self, name: &str) -> Option<ConcreteType<'a>> {
        match self.resolve_named_export(name)? {
            ResolvedItem::CompType(_, ty) => concretize_comp_type(self, ty),
            ResolvedItem::CompInst(_, ComponentInstance::FromExports(exports)) => {
                concretize_from_exports_instance(self, exports)
            }
            // The export resolves to a real instantiated component (e.g. a wit-component shim
            // that re-exports individual functions under short names like "handle").
            //
            // Strategy: if the outer component imports an interface under the *same name*
            // (the common wit-component shim pattern where the middleware imports and then
            // re-exports the same WIT interface via a shim), use the import's declared type.
            // This gives a fingerprint consistent with other components that declare the same
            // WIT import.  Fall back to reconstructing the type from the nested component's
            // function exports when no matching import exists.
            ResolvedItem::CompInst(_, inst @ ComponentInstance::Instantiate { .. }) => {
                self.concretize_import(name).or_else(|| {
                    let comp_ref = inst.get_comp_refs().into_iter().next()?;
                    match self.resolve(&comp_ref.ref_) {
                        ResolvedItem::Component(_, nested) => concretize_comp_func_exports(nested),
                        _ => None,
                    }
                })
            }
            // The export directly re-exposes an imported instance (pass-through middleware).
            // Concretize by following the import's declared instance type.
            ResolvedItem::Import(_, imp) => {
                let type_ref = imp.get_type_refs().into_iter().next()?;
                let ty = match self.resolve(&type_ref.ref_) {
                    ResolvedItem::CompType(_, ty) => ty,
                    _ => return None,
                };
                concretize_comp_type(self, ty)
            }
            _ => None,
        }
    }
    /// Create a [`VisitCtx`] rooted at this component for resolving refs inside a
    /// component-type body that **belongs to this component**.
    ///
    /// Used internally by [`Component::concretize_import`] and [`Component::concretize_export`]
    /// to ensure outer-alias refs (e.g. `alias outer 1 …`) inside a type body resolve against
    /// this component's own index space rather than a walk-time context.
    fn enter_type_scope(&'a self, ty: &'a ComponentType<'a>) -> VisitCtx<'a> {
        let mut inner = VisitCtxInner::new(self);
        inner.push_component(self);
        inner.maybe_enter_scope(ty);
        // Mirror what the visitor driver does: push the type body's decl slice so
        // that `resolve()` dispatches body-relative refs into the right namespace
        // rather than falling through to the component's main type index space.
        match ty {
            ComponentType::Instance(decls) => inner.push_type_body(TypeBodyDecls::Inst(decls)),
            ComponentType::Component(decls) => inner.push_type_body(TypeBodyDecls::Comp(decls)),
            _ => {}
        }
        VisitCtx { inner }
    }
}

// ============================================================
// Internal concretization logic
// ============================================================

fn concretize_comp_type<'a>(
    comp: &'a Component<'a>,
    ty: &'a ComponentType<'a>,
) -> Option<ConcreteType<'a>> {
    match ty {
        ComponentType::Instance(decls) => {
            let cx = comp.enter_type_scope(ty);
            Some(ConcreteType::Instance(concretize_instance_decls(
                comp, decls, &cx,
            )))
        }
        ComponentType::Func(ft) => {
            let cx = comp.enter_type_scope(ty);
            Some(ConcreteType::Func(concretize_func_ty(ft, comp, &cx)))
        }
        ComponentType::Resource { .. } => Some(ConcreteType::Resource),
        _ => None,
    }
}

fn concretize_instance_decls<'a>(
    comp: &'a Component<'a>,
    decls: &'a [InstanceTypeDeclaration<'a>],
    cx: &VisitCtx<'a>,
) -> Vec<(&'a str, ConcreteFuncType<'a>)> {
    let mut funcs = vec![];
    for decl in decls {
        if let InstanceTypeDeclaration::Export { name, .. } = decl {
            if let Some(type_ref) = decl.get_type_refs().first() {
                let resolved = cx.resolve(&type_ref.ref_);
                if let Some(ft) = resolve_and_concretize_func(resolved, comp, cx) {
                    funcs.push((name.0, ft));
                }
            }
        }
    }
    funcs
}

/// Follow aliases until we reach a function type, then concretize it.
///
/// Only `ComponentType::Func` is considered a match; all other resolved types
/// return `None`.  This intentionally limits instance-export concretization to
/// WIT function exports — see the [module-level TODO](self) for the plan to
/// extend beyond WIT interfaces in a future release.
///
/// Returns an owned [`ConcreteFuncType`] (rather than a borrowed
/// `&ComponentFuncType`) so that cross-scope resolution via `InstanceExport`
/// aliases — where the func type lives in a different component — can be
/// returned without lifetime issues.
fn resolve_and_concretize_func<'a>(
    resolved: ResolvedItem<'a, 'a>,
    comp: &'a Component<'a>,
    cx: &VisitCtx<'a>,
) -> Option<ConcreteFuncType<'a>> {
    match resolved {
        ResolvedItem::CompType(_, ComponentType::Func(ft)) => {
            Some(concretize_func_ty(ft, comp, cx))
        }
        ResolvedItem::Alias(_, alias @ ComponentAlias::Outer { .. }) => {
            resolve_and_concretize_func(cx.resolve(&alias.get_item_ref().ref_), comp, cx)
        }
        // `InstanceExport` aliases carry the instance index relative to the owning component's
        // instance namespace.  Resolve through the instantiated component's export instead of
        // calling `cx.resolve()`, which would incorrectly dispatch depth=0 into the type body.
        ResolvedItem::Alias(
            _,
            ComponentAlias::InstanceExport {
                instance_index,
                name,
                ..
            },
        ) => {
            if let Some(nested_comp) = resolve_instantiated_comp(comp, *instance_index) {
                // Instance is a locally-instantiated component — look up the export type.
                match nested_comp.concretize_export(name) {
                    Some(ConcreteType::Func(ft)) => Some(ft),
                    _ => None,
                }
            } else {
                // Instance is an import (not a local instantiation) — extract the function
                // type from the import's declared instance type.
                resolve_func_from_import_instance(comp, *instance_index, name)
            }
        }
        // The function is a direct import (e.g. `(import "f" (func (type $sig)))`).
        // This arises in shim components that take individual function imports rather
        // than a whole instance import.  Follow the import's declared type.
        ResolvedItem::Import(_, imp) => {
            let type_ref = imp.get_type_refs().into_iter().next()?;
            match comp.resolve(&type_ref.ref_) {
                ResolvedItem::CompType(_, ComponentType::Func(ft)) => {
                    Some(concretize_func_ty(ft, comp, cx))
                }
                _ => None,
            }
        }
        _ => None,
    }
}

fn concretize_func_ty<'a>(
    ft: &'a ComponentFuncType<'a>,
    comp: &'a Component<'a>,
    cx: &VisitCtx<'a>,
) -> ConcreteFuncType<'a> {
    ConcreteFuncType {
        params: ft
            .params
            .iter()
            .map(|(name, ty)| (*name, concretize_val_type(ty, comp, cx)))
            .collect(),
        result: ft
            .result
            .as_ref()
            .map(|ty| concretize_val_type(ty, comp, cx)),
    }
}

fn concretize_val_type<'a>(
    ty: &'a ComponentValType,
    comp: &'a Component<'a>,
    cx: &VisitCtx<'a>,
) -> ConcreteValType<'a> {
    match ty {
        ComponentValType::Primitive(p) => ConcreteValType::Primitive(*p),
        ComponentValType::Type(_) => {
            if let Some(type_ref) = ty.get_type_refs().first() {
                concretize_from_resolved(cx.resolve(&type_ref.ref_), comp, cx)
            } else {
                unreachable!("`ComponentValType::Type(idx)` always carries exactly one type ref in a valid binary")
            }
        }
    }
}

fn concretize_from_resolved<'a>(
    resolved: ResolvedItem<'a, 'a>,
    comp: &'a Component<'a>,
    cx: &VisitCtx<'a>,
) -> ConcreteValType<'a> {
    match resolved {
        ResolvedItem::CompType(_, ty) => concretize_comp_type_to_val(ty, comp, cx),
        ResolvedItem::Alias(_, alias @ ComponentAlias::Outer { .. }) => {
            concretize_from_resolved(cx.resolve(&alias.get_item_ref().ref_), comp, cx)
        }
        // Same fix as in `resolve_and_concretize_func`: bypass `cx.resolve()` for InstanceExport
        // and look up the type directly through the instantiated component's export chain.
        ResolvedItem::Alias(
            _,
            ComponentAlias::InstanceExport {
                instance_index,
                name,
                ..
            },
        ) => {
            let Some(nested_comp) = resolve_instantiated_comp(comp, *instance_index) else {
                // The instance is an import (e.g. `wasi:http/types@...`) rather than a
                // locally-instantiated component.  Look up the named type export from
                // the import's declared instance type — the same approach used by
                // `resolve_func_from_import_instance` for function aliases.
                return resolve_type_from_import_instance(comp, *instance_index, name);
            };
            match nested_comp.concretize_export(name) {
                Some(ConcreteType::Resource) | None => ConcreteValType::Resource,
                // TODO(beyond-wit): An `InstanceExport` alias used as a *value type*
                // should only ever resolve to a resource or defined type in the WIT
                // subset — functions and instances are not value types. If you hit
                // this with a non-WIT component, extend `ConcreteValType` and add a
                // proper case here.
                Some(ConcreteType::Instance(_) | ConcreteType::Func(_)) => {
                    ConcreteValType::Resource
                }
            }
        }
        ResolvedItem::Import(_, import) => {
            if let Some(type_ref) = import.get_type_refs().into_iter().next() {
                concretize_from_resolved(cx.resolve(&type_ref.ref_), comp, cx)
            } else {
                // TODO(beyond-wit): In WIT, an import used as a val type always carries
                // a type ref. Module imports have no type refs but can't appear as val
                // types. Audit this if concretizing non-WIT component imports.
                ConcreteValType::Resource
            }
        }
        ResolvedItem::InstTyDeclExport(_, decl) => {
            if let Some(type_ref) = decl.get_type_refs().into_iter().next() {
                concretize_from_resolved(cx.resolve(&type_ref.ref_), comp, cx)
            } else {
                // TODO(beyond-wit): Same as the Import case above — no type ref on a
                // decl used as a val type shouldn't arise in WIT. Audit for non-WIT use.
                ConcreteValType::Resource
            }
        }
        // TODO(beyond-wit): All other `ResolvedItem` variants (`CompInst`, `Component`,
        // `Module`, `CoreType`, etc.) cannot appear as val types in a valid WIT binary.
        // If you extend concretization beyond WIT, audit every variant of `ResolvedItem`
        // and add explicit arms for any that can legitimately carry a val type.
        _ => ConcreteValType::Resource,
    }
}

fn concretize_comp_type_to_val<'a>(
    ty: &'a ComponentType<'a>,
    comp: &'a Component<'a>,
    cx: &VisitCtx<'a>,
) -> ConcreteValType<'a> {
    match ty {
        ComponentType::Defined(def) => concretize_defined_type(def, comp, cx),
        // `ComponentType::Resource` legitimately maps to `ConcreteValType::Resource`.
        // TODO(beyond-wit): `Func`, `Instance`, and `Component` variants here indicate
        // a type reference that resolved to a non-value type, which shouldn't happen in
        // a valid WIT binary. Add explicit handling if concretizing non-WIT components.
        _ => ConcreteValType::Resource,
    }
}

fn concretize_defined_type<'a>(
    ty: &'a ComponentDefinedType,
    comp: &'a Component<'a>,
    cx: &VisitCtx<'a>,
) -> ConcreteValType<'a> {
    match ty {
        ComponentDefinedType::Primitive(p) => ConcreteValType::Primitive(*p),
        ComponentDefinedType::Record(fields) => ConcreteValType::Record(
            fields
                .iter()
                .map(|(name, ty)| (*name, Box::new(concretize_val_type(ty, comp, cx))))
                .collect(),
        ),
        ComponentDefinedType::Variant(cases) => ConcreteValType::Variant(
            cases
                .iter()
                .map(|c| {
                    (
                        c.name,
                        c.ty.as_ref()
                            .map(|t| Box::new(concretize_val_type(t, comp, cx))),
                    )
                })
                .collect(),
        ),
        ComponentDefinedType::List(ty) => {
            ConcreteValType::List(Box::new(concretize_val_type(ty, comp, cx)))
        }
        ComponentDefinedType::Tuple(types) => ConcreteValType::Tuple(
            types
                .iter()
                .map(|t| concretize_val_type(t, comp, cx))
                .collect(),
        ),
        ComponentDefinedType::Option(ty) => {
            ConcreteValType::Option(Box::new(concretize_val_type(ty, comp, cx)))
        }
        ComponentDefinedType::Result { ok, err } => ConcreteValType::Result {
            ok: ok
                .as_ref()
                .map(|t| Box::new(concretize_val_type(t, comp, cx))),
            err: err
                .as_ref()
                .map(|t| Box::new(concretize_val_type(t, comp, cx))),
        },
        ComponentDefinedType::Flags(names) => ConcreteValType::Flags(names.to_vec()),
        ComponentDefinedType::Enum(names) => ConcreteValType::Enum(names.to_vec()),
        ComponentDefinedType::Map(key, val) => ConcreteValType::Map(
            Box::new(concretize_val_type(key, comp, cx)),
            Box::new(concretize_val_type(val, comp, cx)),
        ),
        ComponentDefinedType::FixedSizeList(elem, size) => {
            ConcreteValType::FixedSizeList(Box::new(concretize_val_type(elem, comp, cx)), *size)
        }
        ComponentDefinedType::Own(_) | ComponentDefinedType::Borrow(_) => ConcreteValType::Resource,
        ComponentDefinedType::Future(_) | ComponentDefinedType::Stream(_) => {
            ConcreteValType::AsyncHandle
        }
    }
}

/// Concretize a `FromExports` synthetic instance into a [`ConcreteType::Instance`].
///
/// Handles the case where a component export resolves to an instance built with
/// `(instance $out (export "name" (func $f)) ...)` rather than a typed instance import.
/// Each `Func` export in the instance is resolved to its concrete signature by following
/// the alias chain to the underlying function type declaration.
fn concretize_from_exports_instance<'a>(
    comp: &'a Component<'a>,
    exports: &'a [ComponentExport<'a>],
) -> Option<ConcreteType<'a>> {
    // Build a root-level context for resolving aliases in the component's own namespace.
    let cx = {
        let mut inner = VisitCtxInner::new(comp);
        inner.push_component(comp);
        VisitCtx { inner }
    };

    let mut funcs = vec![];
    for export in exports.iter() {
        if export.kind != ComponentExternalKind::Func {
            continue; // Skip non-function exports (nested instances, types, etc.)
        }
        let resolved = comp.resolve(&export.get_item_ref().ref_);
        if let Some(ft) = resolve_and_concretize_func(resolved, comp, &cx) {
            funcs.push((export.name.0, ft));
        }
    }

    Some(ConcreteType::Instance(funcs))
}

/// Concretize the "interface" of a real instantiated component by collecting all of its
/// function exports into a [`ConcreteType::Instance`].
///
/// This handles the pattern produced by `wit-component` where a shim component re-exports
/// individual functions (`"handle"`, etc.) rather than bundling them under a WIT interface
/// name.  When the outer component exports the whole shim instance under an interface name
/// (e.g. `"wasi:http/handler@..."`), the type of that interface is implicitly defined by the
/// shim's function exports.
fn concretize_comp_func_exports<'a>(comp: &'a Component<'a>) -> Option<ConcreteType<'a>> {
    let cx = {
        let mut inner = VisitCtxInner::new(comp);
        inner.push_component(comp);
        VisitCtx { inner }
    };

    let mut funcs = vec![];
    for export in comp.exports.iter() {
        if export.kind != ComponentExternalKind::Func {
            continue; // Only collect function exports; skip types, instances, etc.
        }
        let resolved = comp.resolve(&export.get_item_ref().ref_);
        if let Some(ft) = resolve_and_concretize_func(resolved, comp, &cx) {
            funcs.push((export.name.0, ft));
        }
    }

    Some(ConcreteType::Instance(funcs))
}

/// Resolve a **type** exported by an **import** instance (not a locally-instantiated component).
///
/// When `(alias export $import-inst "type-name" (type $t))` appears inside a component and
/// `$import-inst` is an import (e.g. `wasi:http/types@...`), [`resolve_instantiated_comp`]
/// returns `None`.  This function looks up the import's declared instance type, enters its
/// type-body scope, and concretizes the named type export from the declarations.
///
/// This is the value-type counterpart of [`resolve_func_from_import_instance`].
fn resolve_type_from_import_instance<'a>(
    comp: &'a Component<'a>,
    instance_index: u32,
    type_name: &str,
) -> ConcreteValType<'a> {
    let inst_ref = IndexedRef {
        depth: Depth::default(),
        space: Space::CompInst,
        index: instance_index,
    };
    let import = match comp.resolve(&inst_ref) {
        ResolvedItem::Import(_, imp) => imp,
        _ => return ConcreteValType::Resource,
    };
    let type_ref = match import.get_type_refs().into_iter().next() {
        Some(tr) => tr,
        None => return ConcreteValType::Resource,
    };
    let ty = match comp.resolve(&type_ref.ref_) {
        ResolvedItem::CompType(_, ty) => ty,
        _ => return ConcreteValType::Resource,
    };
    let decls = match ty {
        ComponentType::Instance(decls) => decls,
        _ => return ConcreteValType::Resource,
    };
    // Build a type-body scope so that outer-alias refs inside the decls resolve
    // against the component's own type space (same as `enter_type_scope`).
    let inner_cx = comp.enter_type_scope(ty);
    for decl in decls {
        if let InstanceTypeDeclaration::Export { name, .. } = decl {
            if name.0 != type_name {
                continue;
            }
            if let Some(tr) = decl.get_type_refs().first() {
                let resolved = inner_cx.resolve(&tr.ref_);
                return concretize_from_resolved(resolved, comp, &inner_cx);
            }
        }
    }
    ConcreteValType::Resource
}

/// Follow a function alias that points into an **import** instance (not a locally-instantiated
/// component).
///
/// When `(alias export $import-inst "func-name" (func $f))` appears inside a component and
/// `$import-inst` was provided as an import (rather than instantiated locally), we cannot
/// reach its type via [`resolve_instantiated_comp`].  Instead, we look up the import's
/// declared instance type and extract the named function signature from it.
fn resolve_func_from_import_instance<'a>(
    comp: &'a Component<'a>,
    instance_index: u32,
    func_name: &str,
) -> Option<ConcreteFuncType<'a>> {
    let inst_ref = IndexedRef {
        depth: Depth::default(),
        space: Space::CompInst,
        index: instance_index,
    };
    let import = match comp.resolve(&inst_ref) {
        ResolvedItem::Import(_, imp) => imp,
        _ => return None,
    };

    // The import's type must be ComponentTypeRef::Instance.
    let type_ref = import.get_type_refs().into_iter().next()?;
    let ty = match comp.resolve(&type_ref.ref_) {
        ResolvedItem::CompType(_, ty) => ty,
        _ => return None,
    };

    // Concretize the full instance type and find the named function.
    match concretize_comp_type(comp, ty)? {
        ConcreteType::Instance(funcs) => funcs
            .into_iter()
            .find(|(name, _)| *name == func_name)
            .map(|(_, ft)| ft),
        _ => None,
    }
}

/// Given an `instance_index` in `comp`'s instance namespace, resolve the component being
/// instantiated and return a reference to it.
///
/// Returns `None` if the instance index is out of range, the instance is a `FromExports`
/// synthetic instance, or the component ref cannot be resolved.
fn resolve_instantiated_comp<'a>(
    comp: &'a Component<'a>,
    instance_index: u32,
) -> Option<&'a Component<'a>> {
    let inst_ref = IndexedRef {
        depth: Depth::default(),
        space: Space::CompInst,
        index: instance_index,
    };
    let inst = match comp.resolve(&inst_ref) {
        ResolvedItem::CompInst(_, inst) => inst,
        _ => return None,
    };
    let comp_ref = inst.get_comp_refs().into_iter().next()?;
    match comp.resolve(&comp_ref.ref_) {
        ResolvedItem::Component(_, nested_comp) => Some(nested_comp),
        _ => None,
    }
}