@@ -466,6 +466,29 @@ extension SwiftType {
466466 )
467467 } else if let genericParamDecl = typeDecl as? SwiftGenericParameterDeclaration {
468468 self = . genericParameter( genericParamDecl)
469+ } else if let aliasDecl = typeDecl as? SwiftTypeAliasDeclaration {
470+ let aliasGenericParams =
471+ aliasDecl. syntax. genericParameterClause? . parameters. map { $0. name. text } ?? [ ]
472+ let useSiteArgs = genericArguments ?? [ ]
473+
474+ // The alias's generic parameter count must match the use-site argument
475+ // count. Treat any mismatch (including use-site args on a non-generic
476+ // alias, or missing args on a generic alias) as unimplemented to fall
477+ // through to silent drop.
478+ guard aliasGenericParams. count == useSiteArgs. count else {
479+ throw TypeTranslationError . unimplementedType ( originalType)
480+ }
481+
482+ let resolved = try lookupContext. resolve ( typeAlias: aliasDecl)
483+
484+ if aliasGenericParams. isEmpty {
485+ self = resolved
486+ } else {
487+ let substitutions = Dictionary (
488+ uniqueKeysWithValues: zip ( aliasGenericParams, useSiteArgs)
489+ )
490+ self = resolved. substituting ( genericParameters: substitutions)
491+ }
469492 } else {
470493 fatalError ( " unknown SwiftTypeDeclaration: \( type ( of: typeDecl) ) " )
471494 }
@@ -494,6 +517,55 @@ extension SwiftType {
494517 )
495518 }
496519
520+ /// Substitute generic parameters *by name*.
521+ ///
522+ /// This is used e.g. by typealiases like `typealias Ano<T> = Array<T>`,
523+ /// so usages like `Ano<Int>` become `Array<Int>`.
524+ func substituting( genericParameters substitutions: [ String : SwiftType ] ) -> SwiftType {
525+ guard !substitutions. isEmpty else { return self }
526+
527+ switch self {
528+ case . nominal( let nominal) :
529+ return . nominal(
530+ SwiftNominalType (
531+ parent: nominal. parent,
532+ sugarName: nominal. sugarName,
533+ nominalTypeDecl: nominal. nominalTypeDecl,
534+ genericArguments: nominal. genericArguments? . map {
535+ $0. substituting ( genericParameters: substitutions)
536+ }
537+ )
538+ )
539+ case . genericParameter( let decl) :
540+ return substitutions [ decl. name] ?? self
541+ case . function( var fn) :
542+ fn. parameters = fn. parameters. map { p in
543+ var p = p
544+ p. type = p. type. substituting ( genericParameters: substitutions)
545+ return p
546+ }
547+ fn. resultType = fn. resultType. substituting ( genericParameters: substitutions)
548+ return . function( fn)
549+ case . metatype( let inner) :
550+ return . metatype( inner. substituting ( genericParameters: substitutions) )
551+ case . tuple( let elements) :
552+ return . tuple(
553+ elements. map {
554+ SwiftTupleElement (
555+ label: $0. label,
556+ type: $0. type. substituting ( genericParameters: substitutions)
557+ )
558+ }
559+ )
560+ case . existential( let inner) :
561+ return . existential( inner. substituting ( genericParameters: substitutions) )
562+ case . opaque( let inner) :
563+ return . opaque( inner. substituting ( genericParameters: substitutions) )
564+ case . composite( let types) :
565+ return . composite( types. map { $0. substituting ( genericParameters: substitutions) } )
566+ }
567+ }
568+
497569 /// Produce an expression that creates the metatype for this type in
498570 /// Swift source code.
499571 var metatypeReferenceExprSyntax : ExprSyntax {
0 commit comments