Skip to content

Commit 2d5d8e3

Browse files
committed
Sema: Clean up applyGenericArguments() and friends
1 parent 6b8d54d commit 2d5d8e3

File tree

2 files changed

+54
-84
lines changed

2 files changed

+54
-84
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 54 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -626,11 +626,43 @@ static bool isPointerToVoid(ASTContext &Ctx, Type Ty, bool &IsMutable) {
626626
return BGT->getGenericArgs().front()->isVoid();
627627
}
628628

629-
Type TypeChecker::applyGenericArguments(Type type,
630-
SourceLoc loc,
631-
TypeResolution resolution,
632-
GenericIdentTypeRepr *generic,
633-
TypeResolutionOptions options) {
629+
static void diagnoseUnboundGenericType(Type ty, SourceLoc loc);
630+
631+
/// Apply generic arguments to the given type.
632+
///
633+
/// If the type is itself not generic, this does nothing.
634+
///
635+
/// This function emits diagnostics about an invalid type or the wrong number
636+
/// of generic arguments, whereas applyUnboundGenericArguments requires this
637+
/// to be in a correct and valid form.
638+
///
639+
/// \param type The generic type to which to apply arguments.
640+
/// \param resolution The type resolution to perform.
641+
/// \param comp The arguments to apply with the angle bracket range for
642+
/// diagnostics.
643+
/// \param options The type resolution context.
644+
///
645+
/// \returns A BoundGenericType bound to the given arguments, or null on
646+
/// error.
647+
///
648+
/// \see applyUnboundGenericArguments
649+
static Type applyGenericArguments(Type type,
650+
TypeResolution resolution,
651+
ComponentIdentTypeRepr *comp,
652+
TypeResolutionOptions options) {
653+
auto loc = comp->getIdLoc();
654+
auto *generic = dyn_cast<GenericIdentTypeRepr>(comp);
655+
if (!generic) {
656+
if (type->is<UnboundGenericType>() &&
657+
!options.is(TypeResolverContext::TypeAliasDecl) &&
658+
!options.contains(TypeResolutionFlags::AllowUnboundGenerics)) {
659+
diagnoseUnboundGenericType(type, loc);
660+
return ErrorType::get(type->getASTContext());
661+
}
662+
663+
return type;
664+
}
665+
634666
if (type->hasError()) {
635667
generic->setInvalid();
636668
return type;
@@ -716,15 +748,14 @@ Type TypeChecker::applyGenericArguments(Type type,
716748
args.push_back(substTy);
717749
}
718750

719-
auto result = applyUnboundGenericArguments(unboundType, genericDecl, loc,
720-
resolution, args);
721-
if (!result)
722-
return result;
751+
auto result = TypeChecker::applyUnboundGenericArguments(
752+
unboundType, genericDecl, loc,
753+
resolution, args);
723754

724755
if (!options.contains(TypeResolutionFlags::AllowUnavailable)) {
725756
if (options.isAnyExpr() || dc->getParent()->isLocalContext())
726757
if (dc->getResilienceExpansion() == ResilienceExpansion::Minimal)
727-
diagnoseGenericTypeExportability(loc, result, dc);
758+
TypeChecker::diagnoseGenericTypeExportability(loc, result, dc);
728759
}
729760

730761
// Migration hack.
@@ -908,41 +939,25 @@ static void maybeDiagnoseBadConformanceRef(DeclContext *dc,
908939
}
909940

910941
/// Returns a valid type or ErrorType in case of an error.
911-
static Type resolveTypeDecl(TypeDecl *typeDecl, SourceLoc loc,
942+
static Type resolveTypeDecl(TypeDecl *typeDecl,
912943
DeclContext *foundDC, TypeResolution resolution,
913-
GenericIdentTypeRepr *generic,
944+
ComponentIdentTypeRepr *comp,
914945
TypeResolutionOptions options) {
915-
916946
// Resolve the type declaration to a specific type. How this occurs
917947
// depends on the current context and where the type was found.
918-
Type type = TypeChecker::resolveTypeInContext(typeDecl, foundDC, resolution,
919-
options, generic);
920-
921-
if (type->is<UnboundGenericType>() && !generic &&
922-
!options.is(TypeResolverContext::TypeAliasDecl) &&
923-
!options.contains(TypeResolutionFlags::AllowUnboundGenerics)) {
924-
diagnoseUnboundGenericType(type, loc);
925-
return ErrorType::get(typeDecl->getASTContext());
926-
}
948+
Type type = TypeChecker::resolveTypeInContext(
949+
typeDecl, foundDC, resolution, options,
950+
isa<GenericIdentTypeRepr>(comp));
927951

928952
if (type->hasError() && foundDC &&
929953
(isa<AssociatedTypeDecl>(typeDecl) || isa<TypeAliasDecl>(typeDecl))) {
930954
auto fromDC = resolution.getDeclContext();
931955
assert(fromDC && "No declaration context for type resolution?");
932956
maybeDiagnoseBadConformanceRef(fromDC, foundDC->getDeclaredInterfaceType(),
933-
loc, typeDecl);
934-
}
935-
936-
if (generic) {
937-
// Apply the generic arguments to the type.
938-
type = TypeChecker::applyGenericArguments(type, loc, resolution, generic,
939-
options);
940-
if (!type)
941-
return nullptr;
957+
comp->getIdLoc(), typeDecl);
942958
}
943959

944-
assert(type);
945-
return type;
960+
return applyGenericArguments(type, resolution, comp, options);
946961
}
947962

948963
static std::string getDeclNameFromContext(DeclContext *dc,
@@ -1217,9 +1232,8 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
12171232
// that now.
12181233
if (auto *typeDecl = comp->getBoundDecl()) {
12191234
// Resolve the type declaration within this context.
1220-
return resolveTypeDecl(typeDecl, comp->getIdLoc(),
1221-
comp->getDeclContext(), resolution,
1222-
dyn_cast<GenericIdentTypeRepr>(comp), options);
1235+
return resolveTypeDecl(typeDecl, comp->getDeclContext(), resolution,
1236+
comp, options);
12231237
}
12241238

12251239
// Resolve the first component, which is the only one that requires
@@ -1265,13 +1279,8 @@ resolveTopLevelIdentTypeComponent(TypeResolution resolution,
12651279
auto *foundDC = entry.getDeclContext();
12661280
auto *typeDecl = cast<TypeDecl>(entry.getValueDecl());
12671281

1268-
Type type = resolveTypeDecl(typeDecl, comp->getIdLoc(),
1269-
foundDC, resolution,
1270-
dyn_cast<GenericIdentTypeRepr>(comp), options);
1271-
1272-
if (!type)
1273-
return type;
1274-
1282+
Type type = resolveTypeDecl(typeDecl, foundDC, resolution,
1283+
comp, options);
12751284
if (type->is<ErrorType>())
12761285
return type;
12771286

@@ -1355,23 +1364,6 @@ static Type resolveNestedIdentTypeComponent(
13551364
auto &ctx = DC->getASTContext();
13561365
auto &diags = ctx.Diags;
13571366

1358-
auto maybeApplyGenericArgs = [&](Type memberType) {
1359-
// If there are generic arguments, apply them now.
1360-
if (auto genComp = dyn_cast<GenericIdentTypeRepr>(comp)) {
1361-
return TypeChecker::applyGenericArguments(memberType, comp->getIdLoc(),
1362-
resolution, genComp, options);
1363-
}
1364-
1365-
if (memberType->is<UnboundGenericType>() &&
1366-
!options.is(TypeResolverContext::TypeAliasDecl) &&
1367-
!options.contains(TypeResolutionFlags::AllowUnboundGenerics)) {
1368-
diagnoseUnboundGenericType(memberType, comp->getLoc());
1369-
return ErrorType::get(ctx);
1370-
}
1371-
1372-
return memberType;
1373-
};
1374-
13751367
auto maybeDiagnoseBadMemberType = [&](TypeDecl *member, Type memberType,
13761368
AssociatedTypeDecl *inferredAssocType) {
13771369
// Diagnose invalid cases.
@@ -1416,7 +1408,7 @@ static Type resolveNestedIdentTypeComponent(
14161408
return memberType;
14171409

14181410
// If there are generic arguments, apply them now.
1419-
return maybeApplyGenericArgs(memberType);
1411+
return applyGenericArguments(memberType, resolution, comp, options);
14201412
};
14211413

14221414
// Short-circuiting.
@@ -1433,7 +1425,7 @@ static Type resolveNestedIdentTypeComponent(
14331425
// type later on.
14341426
if (!memberType->is<DependentMemberType>() ||
14351427
memberType->castTo<DependentMemberType>()->getAssocType()) {
1436-
return maybeApplyGenericArgs(memberType);
1428+
return applyGenericArguments(memberType, resolution, comp, options);
14371429
}
14381430

14391431
return memberType;

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -630,28 +630,6 @@ class TypeChecker final {
630630
TypeResolutionOptions options,
631631
bool isSpecialized);
632632

633-
/// Apply generic arguments to the given type.
634-
///
635-
/// This function emits diagnostics about an invalid type or the wrong number
636-
/// of generic arguments, whereas applyUnboundGenericArguments requires this
637-
/// to be in a correct and valid form.
638-
///
639-
/// \param type The generic type to which to apply arguments.
640-
/// \param loc The source location for diagnostic reporting.
641-
/// \param resolution The type resolution to perform.
642-
/// \param generic The arguments to apply with the angle bracket range for
643-
/// diagnostics.
644-
/// \param options The type resolution context.
645-
///
646-
/// \returns A BoundGenericType bound to the given arguments, or null on
647-
/// error.
648-
///
649-
/// \see applyUnboundGenericArguments
650-
static Type applyGenericArguments(Type type, SourceLoc loc,
651-
TypeResolution resolution,
652-
GenericIdentTypeRepr *generic,
653-
TypeResolutionOptions options);
654-
655633
/// Apply generic arguments to the given type.
656634
///
657635
/// This function requires a valid unbound generic type with the correct

0 commit comments

Comments
 (0)