Skip to content

Commit f4f2101

Browse files
committed
[NFC] Sema: Internalize the use of UnboundGenericType in applyUnboundGenericArguments
1 parent e1ceab0 commit f4f2101

File tree

5 files changed

+51
-65
lines changed

5 files changed

+51
-65
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,11 +1039,9 @@ bool TypeVarBindingProducer::computeNext() {
10391039
// types, that's going to ensure that subtype relationship is
10401040
// always preserved.
10411041
auto *BGT = type->castTo<BoundGenericType>();
1042-
auto UGT = UnboundGenericType::get(BGT->getDecl(), BGT->getParent(),
1043-
BGT->getASTContext());
1044-
10451042
auto dstLocator = TypeVar->getImpl().getLocator();
1046-
auto newType = CS.openUnboundGenericType(UGT, dstLocator)
1043+
auto newType = CS.openUnboundGenericType(BGT->getDecl(), BGT->getParent(),
1044+
dstLocator)
10471045
->reconstituteSugar(/*recursive=*/false);
10481046
addNewBinding(binding.withType(newType));
10491047
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -655,25 +655,19 @@ Optional<std::pair<unsigned, Expr *>> ConstraintSystem::getExprDepthAndParent(
655655
return None;
656656
}
657657

658-
Type
659-
ConstraintSystem::openUnboundGenericType(UnboundGenericType *unbound,
660-
ConstraintLocatorBuilder locator) {
661-
auto unboundDecl = unbound->getDecl();
662-
auto parentTy = unbound->getParent();
658+
Type ConstraintSystem::openUnboundGenericType(
659+
GenericTypeDecl *decl, Type parentTy, ConstraintLocatorBuilder locator) {
663660
if (parentTy) {
664661
parentTy = openUnboundGenericTypes(parentTy, locator);
665-
unbound = UnboundGenericType::get(unboundDecl, parentTy,
666-
getASTContext());
667662
}
668663

669664
// Open up the generic type.
670665
OpenedTypeMap replacements;
671-
openGeneric(unboundDecl->getDeclContext(), unboundDecl->getGenericSignature(),
672-
locator, replacements);
666+
openGeneric(decl->getDeclContext(), decl->getGenericSignature(), locator,
667+
replacements);
673668

674669
if (parentTy) {
675-
auto subs = parentTy->getContextSubstitutions(
676-
unboundDecl->getDeclContext());
670+
auto subs = parentTy->getContextSubstitutions(decl->getDeclContext());
677671
for (auto pair : subs) {
678672
auto found = replacements.find(
679673
cast<GenericTypeParamType>(pair.first));
@@ -686,7 +680,7 @@ ConstraintSystem::openUnboundGenericType(UnboundGenericType *unbound,
686680

687681
// Map the generic parameters to their corresponding type variables.
688682
llvm::SmallVector<Type, 2> arguments;
689-
for (auto gp : unboundDecl->getInnermostGenericParamTypes()) {
683+
for (auto gp : decl->getInnermostGenericParamTypes()) {
690684
auto found = replacements.find(
691685
cast<GenericTypeParamType>(gp->getCanonicalType()));
692686
assert(found != replacements.end() &&
@@ -699,8 +693,8 @@ ConstraintSystem::openUnboundGenericType(UnboundGenericType *unbound,
699693
// handle generic TypeAliases elsewhere, this can just become a
700694
// call to BoundGenericType::get().
701695
return TypeChecker::applyUnboundGenericArguments(
702-
unbound, unboundDecl, SourceLoc(),
703-
TypeResolution::forContextual(DC, None), arguments);
696+
decl, parentTy, SourceLoc(), TypeResolution::forContextual(DC, None),
697+
arguments);
704698
}
705699

706700
static void checkNestedTypeConstraints(ConstraintSystem &cs, Type type,
@@ -791,7 +785,8 @@ Type ConstraintSystem::openUnboundGenericTypes(
791785

792786
type = type.transform([&](Type type) -> Type {
793787
if (auto unbound = type->getAs<UnboundGenericType>()) {
794-
return openUnboundGenericType(unbound, locator);
788+
return openUnboundGenericType(unbound->getDecl(), unbound->getParent(),
789+
locator);
795790
}
796791

797792
return type;

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3403,14 +3403,12 @@ class ConstraintSystem {
34033403
/// Add implicit "load" expressions to the given expression.
34043404
Expr *addImplicitLoadExpr(Expr *expr);
34053405

3406-
/// "Open" the given unbound type by introducing fresh type
3407-
/// variables for generic parameters and constructing a bound generic
3408-
/// type from these type variables.
3409-
///
3410-
/// \param unbound The type to open.
3406+
/// "Open" the unbound generic type represented by the given declaration and
3407+
/// parent type by introducing fresh type variables for generic parameters
3408+
/// and constructing a bound generic type from these type variables.
34113409
///
34123410
/// \returns The opened type.
3413-
Type openUnboundGenericType(UnboundGenericType *unbound,
3411+
Type openUnboundGenericType(GenericTypeDecl *decl, Type parentTy,
34143412
ConstraintLocatorBuilder locator);
34153413

34163414
/// "Open" the given type by replacing any occurrences of unbound

lib/Sema/TypeCheckType.cpp

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -725,9 +725,8 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
725725
// Make sure we have the right number of generic arguments.
726726
// FIXME: If we have fewer arguments than we need, that might be okay, if
727727
// we're allowed to deduce the remaining arguments from context.
728-
auto genericDecl = cast<GenericTypeDecl>(decl);
729728
auto genericArgs = generic->getGenericArgs();
730-
auto genericParams = genericDecl->getGenericParams();
729+
auto genericParams = decl->getGenericParams();
731730
if (genericParams->size() != genericArgs.size()) {
732731
if (!options.contains(TypeResolutionFlags::SilenceErrors)) {
733732
diags.diagnose(loc, diag::type_parameter_count_mismatch, decl->getName(),
@@ -756,10 +755,10 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
756755
}
757756

758757
// FIXME: More principled handling of circularity.
759-
if (!genericDecl->getGenericSignature()) {
758+
if (!decl->getGenericSignature()) {
760759
diags.diagnose(loc, diag::recursive_decl_reference,
761-
genericDecl->getDescriptiveKind(), genericDecl->getName());
762-
genericDecl->diagnose(diag::kind_declared_here, DescriptiveDeclKind::Type);
760+
decl->getDescriptiveKind(), decl->getName());
761+
decl->diagnose(diag::kind_declared_here, DescriptiveDeclKind::Type);
763762
return ErrorType::get(ctx);
764763
}
765764

@@ -777,9 +776,8 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
777776
args.push_back(substTy);
778777
}
779778

780-
auto result = TypeChecker::applyUnboundGenericArguments(
781-
unboundType, genericDecl, loc,
782-
resolution, args);
779+
const auto result = TypeChecker::applyUnboundGenericArguments(
780+
decl, unboundType->getParent(), loc, resolution, args);
783781

784782
const auto genericOptions = genericResolution.getOptions();
785783
if (!genericOptions.contains(TypeResolutionFlags::AllowUnavailable)) {
@@ -802,10 +800,10 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
802800
}
803801

804802
/// Apply generic arguments to the given type.
805-
Type TypeChecker::applyUnboundGenericArguments(
806-
UnboundGenericType *unboundType, GenericTypeDecl *decl,
807-
SourceLoc loc, TypeResolution resolution,
808-
ArrayRef<Type> genericArgs) {
803+
Type TypeChecker::applyUnboundGenericArguments(GenericTypeDecl *decl,
804+
Type parentTy, SourceLoc loc,
805+
TypeResolution resolution,
806+
ArrayRef<Type> genericArgs) {
809807
assert(genericArgs.size() == decl->getGenericParams()->size() &&
810808
"invalid arguments, use applyGenericArguments for diagnostic emitting");
811809

@@ -826,20 +824,20 @@ Type TypeChecker::applyUnboundGenericArguments(
826824

827825
// Get the substitutions for outer generic parameters from the parent
828826
// type.
829-
if (auto parentType = unboundType->getParent()) {
830-
if (parentType->hasUnboundGenericType()) {
827+
if (parentTy) {
828+
if (parentTy->hasUnboundGenericType()) {
831829
// If we're working with a nominal type declaration, just construct
832830
// a bound generic type without checking the generic arguments.
833831
if (auto *nominalDecl = dyn_cast<NominalTypeDecl>(decl)) {
834-
return BoundGenericType::get(nominalDecl, parentType, genericArgs);
832+
return BoundGenericType::get(nominalDecl, parentTy, genericArgs);
835833
}
836834

837835
assert(!resultType->hasTypeParameter());
838836
return resultType;
839837
}
840838

841-
subs = parentType->getContextSubstitutions(decl->getDeclContext());
842-
skipRequirementsCheck |= parentType->hasTypeVariable();
839+
subs = parentTy->getContextSubstitutions(decl->getDeclContext());
840+
skipRequirementsCheck |= parentTy->hasTypeVariable();
843841
} else if (auto genericEnv =
844842
decl->getDeclContext()->getGenericEnvironmentOfContext()) {
845843
auto genericSig = genericEnv->getGenericSignature();
@@ -876,11 +874,11 @@ Type TypeChecker::applyUnboundGenericArguments(
876874

877875
if (!skipRequirementsCheck &&
878876
resolution.getStage() > TypeResolutionStage::Structural) {
879-
auto result =
880-
checkGenericArguments(dc, loc, noteLoc, unboundType,
881-
genericSig->getGenericParams(),
882-
genericSig->getRequirements(),
883-
QueryTypeSubstitutionMap{subs});
877+
auto result = checkGenericArguments(
878+
dc, loc, noteLoc,
879+
UnboundGenericType::get(decl, parentTy, dc->getASTContext()),
880+
genericSig->getGenericParams(), genericSig->getRequirements(),
881+
QueryTypeSubstitutionMap{subs});
884882

885883
switch (result) {
886884
case RequirementCheckResult::Failure:
@@ -903,14 +901,12 @@ Type TypeChecker::applyUnboundGenericArguments(
903901
LookUpConformanceInModule(module));
904902

905903
// Form a sugared typealias reference.
906-
Type parentType = unboundType->getParent();
907-
if (typealias && (!parentType || !parentType->isAnyExistentialType())) {
904+
if (typealias && (!parentTy || !parentTy->isAnyExistentialType())) {
908905
auto genericSig = typealias->getGenericSignature();
909906
auto subMap = SubstitutionMap::get(genericSig,
910907
QueryTypeSubstitutionMap{subs},
911908
LookUpConformanceInModule(module));
912-
resultType = TypeAliasType::get(typealias, parentType,
913-
subMap, resultType);
909+
resultType = TypeAliasType::get(typealias, parentTy, subMap, resultType);
914910
}
915911

916912
return resultType;
@@ -3311,17 +3307,16 @@ Type TypeResolver::resolveDictionaryType(DictionaryTypeRepr *repr,
33113307
return ErrorType::get(Context);
33123308
}
33133309

3314-
auto dictDecl = Context.getDictionaryDecl();
3310+
auto *const dictDecl = Context.getDictionaryDecl();
33153311
if (!dictDecl) {
33163312
Context.Diags.diagnose(repr->getBrackets().Start,
33173313
diag::sugar_type_not_found, 3);
33183314
return ErrorType::get(Context);
33193315
}
33203316

3321-
auto unboundTy = dictDecl->getDeclaredType()->castTo<UnboundGenericType>();
3322-
Type args[] = {keyTy, valueTy};
33233317
if (!TypeChecker::applyUnboundGenericArguments(
3324-
unboundTy, dictDecl, repr->getStartLoc(), resolution, args)) {
3318+
dictDecl, nullptr, repr->getStartLoc(), resolution,
3319+
{keyTy, valueTy})) {
33253320
assert(Context.Diags.hadAnyError());
33263321
return ErrorType::get(Context);
33273322
}

lib/Sema/TypeChecker.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,25 +383,25 @@ void checkUnsupportedProtocolType(ASTContext &ctx,
383383
Type resolveTypeInContext(TypeDecl *typeDecl, DeclContext *foundDC,
384384
TypeResolution resolution, bool isSpecialized);
385385

386-
/// Apply generic arguments to the given type.
386+
/// Apply generic arguments to the unbound generic type represented by the
387+
/// given declaration and parent type.
387388
///
388-
/// This function requires a valid unbound generic type with the correct
389-
/// number of generic arguments given, whereas applyGenericArguments emits
390-
/// diagnostics in those cases.
389+
/// This function requires the correct number of generic arguments,
390+
/// whereas applyGenericArguments emits diagnostics in those cases.
391391
///
392-
/// \param unboundType The unbound generic type to which to apply arguments.
393-
/// \param decl The declaration of the type.
392+
/// \param decl The declaration that the resulting bound generic type
393+
/// shall reference.
394+
/// \param parentTy The parent type.
394395
/// \param loc The source location for diagnostic reporting.
395396
/// \param resolution The type resolution.
396-
/// \param genericArgs The list of generic arguments to apply to the type.
397+
/// \param genericArgs The list of generic arguments to apply.
397398
///
398399
/// \returns A BoundGenericType bound to the given arguments, or null on
399400
/// error.
400401
///
401402
/// \see applyGenericArguments
402-
Type applyUnboundGenericArguments(UnboundGenericType *unboundType,
403-
GenericTypeDecl *decl, SourceLoc loc,
404-
TypeResolution resolution,
403+
Type applyUnboundGenericArguments(GenericTypeDecl *decl, Type parentTy,
404+
SourceLoc loc, TypeResolution resolution,
405405
ArrayRef<Type> genericArgs);
406406

407407
/// Substitute the given base type into the type of the given nested type,

0 commit comments

Comments
 (0)