Skip to content

Commit 8724a01

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-rebranch
2 parents 54e8e97 + 6364a99 commit 8724a01

28 files changed

+335
-480
lines changed

include/swift/AST/ASTScope.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,10 @@ class GenericTypeOrExtensionWherePortion final
715715
GenericTypeOrExtensionWherePortion()
716716
: GenericTypeOrExtensionWhereOrBodyPortion("Where") {}
717717

718+
bool lookupMembersOf(const GenericTypeOrExtensionScope *scope,
719+
ArrayRef<const ASTScopeImpl *>,
720+
ASTScopeImpl::DeclConsumer consumer) const override;
721+
718722
ASTScopeImpl *expandScope(GenericTypeOrExtensionScope *,
719723
ScopeCreator &) const override;
720724

@@ -815,6 +819,8 @@ class GenericTypeOrExtensionScope : public ASTScopeImpl {
815819
return nullptr;
816820
}
817821

822+
bool areMembersVisibleFromWhereClause() const;
823+
818824
virtual void createBodyScope(ASTScopeImpl *leaf, ScopeCreator &) {}
819825

820826
protected:

include/swift/AST/Decl.h

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,9 +1190,6 @@ class GenericParamList final :
11901190

11911191
GenericParamList *OuterParameters;
11921192

1193-
SourceLoc TrailingWhereLoc;
1194-
unsigned FirstTrailingWhereArg;
1195-
11961193
GenericParamList(SourceLoc LAngleLoc,
11971194
ArrayRef<GenericTypeParamDecl *> Params,
11981195
SourceLoc WhereLoc,
@@ -1272,31 +1269,6 @@ class GenericParamList final :
12721269
/// implicitly-generated requirements, and may be non-empty even if no
12731270
/// 'where' keyword is present.
12741271
ArrayRef<RequirementRepr> getRequirements() const { return Requirements; }
1275-
1276-
/// Retrieve only those requirements that are written within the brackets,
1277-
/// which does not include any requirements written in a trailing where
1278-
/// clause.
1279-
ArrayRef<RequirementRepr> getNonTrailingRequirements() const {
1280-
return Requirements.slice(0, FirstTrailingWhereArg);
1281-
}
1282-
1283-
/// Retrieve only those requirements written in a trailing where
1284-
/// clause.
1285-
ArrayRef<RequirementRepr> getTrailingRequirements() const {
1286-
return Requirements.slice(FirstTrailingWhereArg);
1287-
}
1288-
1289-
/// Determine whether the generic parameters have a trailing where clause.
1290-
bool hasTrailingWhereClause() const {
1291-
return FirstTrailingWhereArg < Requirements.size();
1292-
}
1293-
1294-
/// Add a trailing 'where' clause to the list of requirements.
1295-
///
1296-
/// Trailing where clauses are written outside the angle brackets, after the
1297-
/// main part of a declaration's signature.
1298-
void addTrailingWhereClause(ASTContext &ctx, SourceLoc trailingWhereLoc,
1299-
ArrayRef<RequirementRepr> trailingRequirements);
13001272

13011273
/// Retrieve the outer generic parameter list.
13021274
///
@@ -1309,6 +1281,8 @@ class GenericParamList final :
13091281
/// for more information.
13101282
void setOuterParameters(GenericParamList *Outer) { OuterParameters = Outer; }
13111283

1284+
void setDeclContext(DeclContext *dc);
1285+
13121286
SourceLoc getLAngleLoc() const { return Brackets.Start; }
13131287
SourceLoc getRAngleLoc() const { return Brackets.End; }
13141288

@@ -1319,19 +1293,10 @@ class GenericParamList final :
13191293
if (WhereLoc.isInvalid())
13201294
return SourceRange();
13211295

1322-
auto endLoc = Requirements[FirstTrailingWhereArg-1].getSourceRange().End;
1296+
auto endLoc = Requirements.back().getSourceRange().End;
13231297
return SourceRange(WhereLoc, endLoc);
13241298
}
13251299

1326-
/// Retrieve the source range covering the trailing where clause.
1327-
SourceRange getTrailingWhereClauseSourceRange() const {
1328-
if (!hasTrailingWhereClause())
1329-
return SourceRange();
1330-
1331-
return SourceRange(TrailingWhereLoc,
1332-
Requirements.back().getSourceRange().End);
1333-
}
1334-
13351300
/// Configure the depth of the generic parameters in this list.
13361301
void setDepth(unsigned depth);
13371302

@@ -1413,6 +1378,11 @@ class GenericContext : private _GenericContext, public DeclContext {
14131378
/// this context is not generic.
14141379
GenericParamList *getGenericParams() const;
14151380

1381+
/// Retrieve the generic parameters as written in source. Unlike
1382+
/// getGenericParams() this will not synthesize generic parameters for
1383+
/// extensions, protocols and certain type aliases.
1384+
GenericParamList *getParsedGenericParams() const;
1385+
14161386
/// Determine whether this context has generic parameters
14171387
/// of its own.
14181388
///

include/swift/AST/TypeCheckRequests.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ struct WhereClauseOwner {
396396
WhereClauseOwner(DeclContext *dc, GenericParamList *genericParams)
397397
: dc(dc), source(genericParams) {}
398398

399+
WhereClauseOwner(DeclContext *dc, TrailingWhereClause *where)
400+
: dc(dc), source(where) {}
401+
399402
WhereClauseOwner(DeclContext *dc, SpecializeAttr *attr)
400403
: dc(dc), source(attr) {}
401404

include/swift/Parse/Parser.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,9 +1647,7 @@ class Parser {
16471647
diagnoseWhereClauseInGenericParamList(const GenericParamList *GenericParams);
16481648

16491649
ParserStatus
1650-
parseFreestandingGenericWhereClause(GenericContext *genCtx,
1651-
GenericParamList *&GPList,
1652-
ParseDeclOptions flags);
1650+
parseFreestandingGenericWhereClause(GenericContext *genCtx);
16531651

16541652
ParserStatus parseGenericWhereClause(
16551653
SourceLoc &WhereLoc, SmallVectorImpl<RequirementRepr> &Requirements,

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,7 @@ namespace {
561561
};
562562

563563
if (const auto GC = Owner.dyn_cast<const GenericContext *>()) {
564-
if (!GC->isGeneric() || isa<ProtocolDecl>(GC))
565-
printWhere(GC->getTrailingWhereClause());
564+
printWhere(GC->getTrailingWhereClause());
566565
} else {
567566
const auto ATD = Owner.get<const AssociatedTypeDecl *>();
568567
printWhere(ATD->getTrailingWhereClause());
@@ -730,9 +729,9 @@ namespace {
730729
OS << ' ';
731730
printDeclName(VD);
732731
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(VD))
733-
printGenericParameters(OS, AFD->getGenericParams());
732+
printGenericParameters(OS, AFD->getParsedGenericParams());
734733
if (auto *GTD = dyn_cast<GenericTypeDecl>(VD))
735-
printGenericParameters(OS, GTD->getGenericParams());
734+
printGenericParameters(OS, GTD->getParsedGenericParams());
736735

737736
if (auto *var = dyn_cast<VarDecl>(VD)) {
738737
PrintWithColorRAII(OS, TypeColor) << " type='";

lib/AST/ASTScopeCreation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,9 +1599,13 @@ ASTScopeImpl *GenericTypeOrExtensionWholePortion::expandScope(
15991599
if (scope->shouldHaveABody() && !scope->doesDeclHaveABody())
16001600
return ip;
16011601

1602+
auto *context = scope->getGenericContext();
1603+
auto *genericParams = (isa<TypeAliasDecl>(context)
1604+
? context->getParsedGenericParams()
1605+
: context->getGenericParams());
16021606
auto *deepestScope = scopeCreator.addNestedGenericParamScopesToTree(
1603-
scope->getDecl(), scope->getGenericContext()->getGenericParams(), scope);
1604-
if (scope->getGenericContext()->getTrailingWhereClause())
1607+
scope->getDecl(), genericParams, scope);
1608+
if (context->getTrailingWhereClause())
16051609
scope->createTrailingWhereClauseScope(deepestScope, scopeCreator);
16061610
scope->createBodyScope(deepestScope, scopeCreator);
16071611
return ip;

lib/AST/ASTScopeLookup.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ NullablePtr<const GenericParamList> GenericTypeScope::genericParams() const {
292292
// Sigh... These must be here so that from body, we search generics before
293293
// members. But they also must be on the Decl scope for lookups starting from
294294
// generic parameters, where clauses, etc.
295-
return getGenericContext()->getGenericParams();
295+
auto *context = getGenericContext();
296+
if (isa<TypeAliasDecl>(context))
297+
return context->getParsedGenericParams();
298+
return context->getGenericParams();
296299
}
297300
NullablePtr<const GenericParamList> ExtensionScope::genericParams() const {
298301
return decl->getGenericParams();
@@ -367,6 +370,22 @@ bool GenericTypeOrExtensionWhereOrBodyPortion::lookupMembersOf(
367370
});
368371
}
369372

373+
bool GenericTypeOrExtensionWherePortion::lookupMembersOf(
374+
const GenericTypeOrExtensionScope *scope,
375+
ArrayRef<const ASTScopeImpl *> history,
376+
ASTScopeImpl::DeclConsumer consumer) const {
377+
if (!scope->areMembersVisibleFromWhereClause())
378+
return false;
379+
380+
return GenericTypeOrExtensionWhereOrBodyPortion::lookupMembersOf(
381+
scope, history, consumer);
382+
}
383+
384+
bool GenericTypeOrExtensionScope::areMembersVisibleFromWhereClause() const {
385+
auto *decl = getDecl();
386+
return isa<ProtocolDecl>(decl) || isa<ExtensionDecl>(decl);
387+
}
388+
370389
#pragma mark looking in locals or members - locals
371390

372391
bool GenericParamScope::lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,

lib/AST/ASTWalker.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
129129
bool visitGenericParamListIfNeeded(GenericContext *GC) {
130130
// Must check this first in case extensions have not been bound yet
131131
if (Walker.shouldWalkIntoGenericParams()) {
132-
if (auto *params = GC->getGenericParams()) {
132+
if (auto *params = GC->getParsedGenericParams()) {
133133
visitGenericParamList(params);
134134
}
135135
return true;
@@ -142,11 +142,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
142142
for (auto &Req: Where->getRequirements())
143143
if (doIt(Req))
144144
return true;
145-
} else if (!isa<ExtensionDecl>(GC)) {
146-
if (const auto GP = GC->getGenericParams())
147-
for (auto Req: GP->getTrailingRequirements())
148-
if (doIt(Req))
149-
return true;
150145
}
151146
return false;
152147
}
@@ -446,7 +441,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
446441
}
447442

448443
// Visit param conformance
449-
for (auto Req : GPL->getNonTrailingRequirements()) {
444+
for (auto Req : GPL->getRequirements()) {
450445
if (doIt(Req))
451446
return true;
452447
}

0 commit comments

Comments
 (0)