Skip to content

Commit a315b22

Browse files
committed
Remove non-essential uses of SimpleIdentTypeRepr & GenericIdentTypeRepr
1 parent 7e7f6d8 commit a315b22

11 files changed

+100
-51
lines changed

lib/AST/Attr.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,8 +2757,9 @@ CustomAttr::destructureMacroRef() {
27572757
if (auto *unqualIdentType = dyn_cast<UnqualifiedIdentTypeRepr>(typeRepr))
27582758
return {nullptr, unqualIdentType};
27592759
if (auto *memType = dyn_cast<MemberTypeRepr>(typeRepr)) {
2760-
if (auto *base = dyn_cast<SimpleIdentTypeRepr>(memType->getBase())) {
2761-
return {base, memType};
2760+
if (auto *base = dyn_cast<UnqualifiedIdentTypeRepr>(memType->getBase())) {
2761+
if (!base->hasGenericArgList())
2762+
return {base, memType};
27622763
}
27632764
}
27642765
return {nullptr, nullptr};

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,9 +2294,9 @@ static bool isDefaultInitializable(const TypeRepr *typeRepr, ASTContext &ctx) {
22942294
return true;
22952295
}
22962296

2297-
if (auto *identRepr = dyn_cast<GenericIdentTypeRepr>(typeRepr)) {
2298-
if (identRepr->getNameRef().getBaseIdentifier() == ctx.Id_Optional &&
2299-
identRepr->getNumGenericArgs() == 1)
2297+
if (auto *unqualIdentRepr = dyn_cast<UnqualifiedIdentTypeRepr>(typeRepr)) {
2298+
if (unqualIdentRepr->getNumGenericArgs() == 1 &&
2299+
unqualIdentRepr->getNameRef().getBaseIdentifier() == ctx.Id_Optional)
23002300
return true;
23012301
}
23022302
}

lib/AST/NameLookup.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,15 +3734,19 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
37343734
// If we found declarations that are associated types, look outside of
37353735
// the current context to see if we can recover.
37363736
if (declsAreAssociatedTypes(decls.first)) {
3737-
if (auto typeRepr = attr->getTypeRepr()) {
3738-
if (auto identTypeRepr = dyn_cast<SimpleIdentTypeRepr>(typeRepr)) {
3737+
if (auto *unqualIdentRepr =
3738+
dyn_cast_or_null<UnqualifiedIdentTypeRepr>(attr->getTypeRepr())) {
3739+
if (!unqualIdentRepr->hasGenericArgList()) {
37393740
auto assocType = cast<AssociatedTypeDecl>(decls.first.front());
37403741

3742+
const auto name = unqualIdentRepr->getNameRef();
3743+
const auto nameLoc = unqualIdentRepr->getNameLoc();
3744+
const auto loc = unqualIdentRepr->getLoc();
3745+
37413746
modulesFound.clear();
37423747
anyObject = false;
37433748
decls = directReferencesForUnqualifiedTypeLookup(
3744-
identTypeRepr->getNameRef(), identTypeRepr->getLoc(), dc,
3745-
LookupOuterResults::Included);
3749+
name, loc, dc, LookupOuterResults::Included);
37463750
nominals = resolveTypeDeclsToNominal(evaluator, ctx, decls.first,
37473751
ResolveToNominalOptions(),
37483752
modulesFound, anyObject);
@@ -3751,22 +3755,19 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
37513755
if (nominal->getDeclContext()->isModuleScopeContext()) {
37523756
// Complain, producing module qualification in a Fix-It.
37533757
auto moduleName = nominal->getParentModule()->getName();
3754-
ctx.Diags.diagnose(typeRepr->getLoc(),
3755-
diag::warn_property_wrapper_module_scope,
3756-
identTypeRepr->getNameRef(),
3757-
moduleName)
3758-
.fixItInsert(typeRepr->getLoc(),
3759-
moduleName.str().str() + ".");
3758+
ctx.Diags
3759+
.diagnose(loc, diag::warn_property_wrapper_module_scope, name,
3760+
moduleName)
3761+
.fixItInsert(loc, moduleName.str().str() + ".");
37603762
ctx.Diags.diagnose(assocType, diag::kind_declname_declared_here,
37613763
assocType->getDescriptiveKind(),
37623764
assocType->getName());
37633765

37643766
auto *baseTR = UnqualifiedIdentTypeRepr::create(
3765-
ctx, identTypeRepr->getNameLoc(), DeclNameRef(moduleName));
3767+
ctx, nameLoc, DeclNameRef(moduleName));
37663768

3767-
auto *newTE = new (ctx) TypeExpr(
3768-
MemberTypeRepr::create(ctx, baseTR, identTypeRepr->getNameLoc(),
3769-
identTypeRepr->getNameRef()));
3769+
auto *newTE = new (ctx)
3770+
TypeExpr(MemberTypeRepr::create(ctx, baseTR, nameLoc, name));
37703771
attr->resetTypeInformation(newTE);
37713772
return nominal;
37723773
}

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9886,9 +9886,8 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
98869886

98879887
if (auto *lifetimeTyR =
98889888
dyn_cast_or_null<LifetimeDependentReturnTypeRepr>(FuncRetTy)) {
9889-
auto *identTyR = dyn_cast<SimpleIdentTypeRepr>(lifetimeTyR->getBase());
9890-
if (!identTyR ||
9891-
identTyR->getNameRef().getBaseIdentifier() != Context.Id_Self) {
9889+
if (!lifetimeTyR->getBase()->isSimpleUnqualifiedIdentifier(
9890+
Context.Id_Self)) {
98929891
diagnose(FuncRetTy->getStartLoc(),
98939892
diag::lifetime_dependence_invalid_init_return);
98949893
return nullptr;

lib/Parse/ParseType.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,11 @@ ParserResult<TypeRepr> Parser::parseTypeScalar(
562562

563563
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
564564
// Only unqualified identifiers can reference generic parameters.
565-
if (auto *simpleIdentTR = dyn_cast<SimpleIdentTypeRepr>(T)) {
565+
auto *unqualIdentTR = dyn_cast<UnqualifiedIdentTypeRepr>(T);
566+
if (unqualIdentTR && !unqualIdentTR->hasGenericArgList()) {
566567
if (auto *genericParam = dyn_cast_or_null<GenericTypeParamDecl>(
567-
simpleIdentTR->getBoundDecl())) {
568-
simpleIdentTR->overwriteNameRef(genericParam->createNameRef());
568+
unqualIdentTR->getBoundDecl())) {
569+
unqualIdentTR->overwriteNameRef(genericParam->createNameRef());
569570
}
570571
}
571572
return Action::Continue();

lib/Sema/AssociatedTypeInference.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,15 +1234,23 @@ class TypeReprCycleCheckWalker : private ASTWalker {
12341234

12351235
private:
12361236
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
1237+
auto *declRefTyR = dyn_cast<DeclRefTypeRepr>(T);
1238+
if (!declRefTyR || declRefTyR->hasGenericArgList()) {
1239+
return Action::Continue();
1240+
}
1241+
1242+
auto *memberTyR = dyn_cast<MemberTypeRepr>(T);
1243+
12371244
// If we're inferring `Foo`, don't look at a witness mentioning `Foo`.
1238-
if (auto *identTyR = dyn_cast<SimpleIdentTypeRepr>(T)) {
1239-
if (circularNames.count(identTyR->getNameRef().getBaseIdentifier()) > 0) {
1245+
if (!memberTyR) {
1246+
if (circularNames.count(declRefTyR->getNameRef().getBaseIdentifier()) >
1247+
0) {
12401248
// If unqualified lookup can find a type with this name without looking
12411249
// into protocol members, don't skip the witness, since this type might
12421250
// be a candidate witness.
12431251
auto desc = UnqualifiedLookupDescriptor(
1244-
identTyR->getNameRef(), witness->getDeclContext(),
1245-
identTyR->getLoc(), UnqualifiedLookupOptions());
1252+
declRefTyR->getNameRef(), witness->getDeclContext(),
1253+
declRefTyR->getLoc(), UnqualifiedLookupOptions());
12461254

12471255
auto results =
12481256
evaluateOrDefault(ctx.evaluator, UnqualifiedLookupRequest{desc}, {});
@@ -1259,23 +1267,18 @@ class TypeReprCycleCheckWalker : private ASTWalker {
12591267
}
12601268

12611269
// If we're inferring `Foo`, don't look at a witness mentioning `Self.Foo`.
1262-
auto *memberTyR = dyn_cast<MemberTypeRepr>(T);
1263-
if (!memberTyR || memberTyR->hasGenericArgList()) {
1264-
return Action::Continue();
1265-
}
1266-
12671270
if (!memberTyR->getBase()->isSimpleUnqualifiedIdentifier(ctx.Id_Self)) {
12681271
return Action::Continue();
12691272
}
12701273

1271-
if (circularNames.count(memberTyR->getNameRef().getBaseIdentifier()) > 0) {
1274+
if (circularNames.count(declRefTyR->getNameRef().getBaseIdentifier()) > 0) {
12721275
// But if qualified lookup can find a type with this name without looking
12731276
// into protocol members, don't skip the witness, since this type might
12741277
// be a candidate witness.
12751278
SmallVector<ValueDecl *, 2> results;
12761279
witness->getInnermostDeclContext()->lookupQualified(
12771280
witness->getDeclContext()->getSelfTypeInContext(),
1278-
memberTyR->getNameRef(), SourceLoc(), NLOptions(), results);
1281+
declRefTyR->getNameRef(), SourceLoc(), NLOptions(), results);
12791282

12801283
// Ok, resolving this member type would trigger associated type
12811284
// inference recursively. We're going to skip this witness.

lib/Sema/CSDiagnostics.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4696,7 +4696,8 @@ bool InvalidMemberRefOnExistential::diagnoseAsError() {
46964696
while (auto *metatype = dyn_cast<MetatypeTypeRepr>(constraintRepr)) {
46974697
// The generic equivalent of 'any P.Type' is '(some P).Type'
46984698
constraintRepr = metatype->getBase()->getWithoutParens();
4699-
if (isa<SimpleIdentTypeRepr>(constraintRepr))
4699+
if (isa<UnqualifiedIdentTypeRepr>(constraintRepr) &&
4700+
!cast<UnqualifiedIdentTypeRepr>(constraintRepr)->hasGenericArgList())
47004701
needsParens = !isa<TupleTypeRepr>(metatype->getBase());
47014702
}
47024703

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,11 +4055,11 @@ class TypeReprAvailabilityWalker : public ASTWalker {
40554055

40564056
bool foundAnyIssues = false;
40574057

4058-
if (auto *GTR = dyn_cast<GenericIdentTypeRepr>(declRefTR)) {
4058+
if (declRefTR->hasGenericArgList()) {
40594059
auto genericFlags = flags;
40604060
genericFlags -= DeclAvailabilityFlag::AllowPotentiallyUnavailableProtocol;
40614061

4062-
for (auto *genericArg : GTR->getGenericArgs()) {
4062+
for (auto *genericArg : declRefTR->getGenericArgs()) {
40634063
if (diagnoseTypeReprAvailability(genericArg, where, genericFlags))
40644064
foundAnyIssues = true;
40654065

lib/Sema/TypeCheckType.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2754,9 +2754,10 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
27542754
// FIXME: Temporary hack to resolve an identifier type to one of the
27552755
// generic parameters of the opaque return type. This should be subsumed
27562756
// be proper name lookup, OF COURSE.
2757-
if (auto simpleIdent = dyn_cast<SimpleIdentTypeRepr>(
2758-
opaqueRepr->getBase())) {
2759-
Identifier name = simpleIdent->getNameRef().getBaseIdentifier();
2757+
auto *unqualIdentRepr =
2758+
dyn_cast<UnqualifiedIdentTypeRepr>(opaqueRepr->getBase());
2759+
if (unqualIdentRepr && !unqualIdentRepr->hasGenericArgList()) {
2760+
Identifier name = unqualIdentRepr->getNameRef().getBaseIdentifier();
27602761
if (auto gpDecl = opaqueRepr->getGenericParams()
27612762
->lookUpGenericParam(name)) {
27622763
auto outerGenericSignature = opaqueDecl->getNamingDecl()
@@ -3833,12 +3834,12 @@ NeverNullType
38333834
TypeResolver::resolveOpaqueReturnType(TypeRepr *repr, StringRef mangledName,
38343835
unsigned ordinal,
38353836
TypeResolutionOptions options) {
3836-
// The type repr should be a generic identifier type. We don't really use
3837-
// the identifier for anything, but we do resolve the generic arguments
3838-
// to instantiate the possibly-generic opaque type.
3837+
// The type representation should be an unqualified identifier. We don't
3838+
// really use the identifier for anything, but we do resolve any generic
3839+
// arguments to instantiate the possibly-generic opaque type.
38393840
SmallVector<Type, 4> TypeArgsBuf;
3840-
if (auto generic = dyn_cast<GenericIdentTypeRepr>(repr)) {
3841-
for (auto argRepr : generic->getGenericArgs()) {
3841+
if (auto *unqualIdentRepr = dyn_cast<UnqualifiedIdentTypeRepr>(repr)) {
3842+
for (auto argRepr : unqualIdentRepr->getGenericArgs()) {
38423843
auto argTy = resolveType(argRepr, options);
38433844
// If we cannot resolve the generic parameter, propagate the error out.
38443845
if (argTy->hasError()) {
@@ -3847,7 +3848,7 @@ TypeResolver::resolveOpaqueReturnType(TypeRepr *repr, StringRef mangledName,
38473848
TypeArgsBuf.push_back(argTy);
38483849
}
38493850
}
3850-
3851+
38513852
// Use type reconstruction to summon the opaque type decl.
38523853
Demangler demangle;
38533854
auto definingDeclNode = demangle.demangleSymbol(mangledName);

lib/Sema/TypeChecker.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,11 @@ namespace {
490490

491491
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
492492
// Only unqualified identifiers can reference generic parameters.
493-
if (auto *simpleIdentTR = dyn_cast<SimpleIdentTypeRepr>(T)) {
494-
auto name = simpleIdentTR->getNameRef().getBaseIdentifier();
493+
auto *unqualIdentTR = dyn_cast<UnqualifiedIdentTypeRepr>(T);
494+
if (unqualIdentTR && !unqualIdentTR->hasGenericArgList()) {
495+
auto name = unqualIdentTR->getNameRef().getBaseIdentifier();
495496
if (auto *paramDecl = params->lookUpGenericParam(name)) {
496-
simpleIdentTR->setValue(paramDecl, dc);
497+
unqualIdentTR->setValue(paramDecl, dc);
497498
}
498499
}
499500

0 commit comments

Comments
 (0)