@@ -965,15 +965,44 @@ RequirementRequest::evaluate(Evaluator &evaluator,
965
965
}
966
966
967
967
llvm::Expected<Type>
968
- swift::StructuralTypeRequest::evaluate (Evaluator &evaluator,
969
- TypeAliasDecl *D) const {
970
- TypeResolutionOptions options (TypeResolverContext::TypeAliasDecl);
971
- if (!D->getDeclContext ()->isCascadingContextForLookup (
972
- /* functionsAreNonCascading*/ true )) {
968
+ StructuralTypeRequest::evaluate (Evaluator &evaluator,
969
+ TypeAliasDecl *typeAlias) const {
970
+ // Fast path: If the interface type is already resolved, there's no need
971
+ // to compute the structural type. This also prevents us from writing
972
+ // ErrorTypes into otherwise valid ASTs with generated typealiases.
973
+ if (typeAlias->hasInterfaceType ()) {
974
+ return typeAlias->getInterfaceType ()->getMetatypeInstanceType ();
975
+ }
976
+
977
+ TypeResolutionOptions options ((typeAlias->getGenericParams ()
978
+ ? TypeResolverContext::GenericTypeAliasDecl
979
+ : TypeResolverContext::TypeAliasDecl));
980
+
981
+ if (!typeAlias->getDeclContext ()->isCascadingContextForLookup (
982
+ /* functionsAreNonCascading*/ true )) {
973
983
options |= TypeResolutionFlags::KnownNonCascadingDependency;
974
984
}
985
+
986
+ // This can happen when code completion is attempted inside
987
+ // of typealias underlying type e.g. `typealias F = () -> Int#^TOK^#`
988
+ auto &ctx = typeAlias->getASTContext ();
989
+ auto underlyingTypeRepr = typeAlias->getUnderlyingTypeRepr ();
990
+ if (!underlyingTypeRepr) {
991
+ typeAlias->setInvalid ();
992
+ return ErrorType::get (ctx);
993
+ }
975
994
976
- auto typeRepr = D->getUnderlyingTypeLoc ().getTypeRepr ();
977
- auto resolution = TypeResolution::forStructural (D);
978
- return resolution.resolveType (typeRepr, options);
995
+ auto resolution = TypeResolution::forStructural (typeAlias);
996
+ auto type = resolution.resolveType (underlyingTypeRepr, options);
997
+
998
+ auto *genericSig = typeAlias->getGenericSignature ();
999
+ SubstitutionMap subs;
1000
+ if (genericSig)
1001
+ subs = genericSig->getIdentitySubstitutionMap ();
1002
+
1003
+ Type parent;
1004
+ auto parentDC = typeAlias->getDeclContext ();
1005
+ if (parentDC->isTypeContext ())
1006
+ parent = parentDC->getDeclaredInterfaceType ();
1007
+ return TypeAliasType::get (typeAlias, parent, subs, type);
979
1008
}
0 commit comments