Skip to content

SILParser: Fix for removal of parse-time name lookup #33922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,12 @@ class GenericParamList final :

void print(raw_ostream &OS) const;
SWIFT_DEBUG_DUMP;

bool walk(ASTWalker &walker);

/// Finds a generic parameter declaration by name. This should only
/// be used from the SIL parser.
GenericTypeParamDecl *lookUpGenericParam(Identifier name) const;
};

/// A trailing where clause.
Expand Down
5 changes: 3 additions & 2 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -2501,7 +2501,8 @@ class HasImplementationOnlyImportsRequest

class ResolveTypeRequest
: public SimpleRequest<ResolveTypeRequest,
Type(const TypeResolution *, TypeRepr *),
Type(const TypeResolution *, TypeRepr *,
GenericParamList *),
RequestFlags::Uncached> {
public:
using SimpleRequest::SimpleRequest;
Expand All @@ -2515,7 +2516,7 @@ class ResolveTypeRequest

// Evaluation.
Type evaluate(Evaluator &evaluator, const TypeResolution *resolution,
TypeRepr *repr) const;
TypeRepr *repr, GenericParamList *silParams) const;
};

void simple_display(llvm::raw_ostream &out, const TypeResolution *resolution);
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ SWIFT_REQUEST(TypeChecker, ResolveTypeEraserTypeRequest,
Type(ProtocolDecl *, TypeEraserAttr *),
SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, ResolveTypeRequest,
Type (const TypeResolution *, TypeRepr *),
Type (const TypeResolution *, TypeRepr *, GenericParamList *),
Uncached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, SPIGroupsRequest,
llvm::ArrayRef<Identifier>(Decl *),
Expand Down
4 changes: 3 additions & 1 deletion include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ namespace swift {
///
/// \returns A well-formed type on success, or an \c ErrorType.
Type performTypeResolution(TypeRepr *TyR, ASTContext &Ctx, bool isSILMode,
bool isSILType, GenericEnvironment *GenericEnv,
bool isSILType,
GenericEnvironment *GenericEnv,
GenericParamList *GenericParams,
DeclContext *DC, bool ProduceDiagnostics = true);

/// Expose TypeChecker's handling of GenericParamList to SIL parsing.
Expand Down
42 changes: 23 additions & 19 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
// Must check this first in case extensions have not been bound yet
if (Walker.shouldWalkIntoGenericParams()) {
if (auto *params = GC->getParsedGenericParams()) {
visitGenericParamList(params);
doIt(params);
}
return true;
}
Expand Down Expand Up @@ -252,7 +252,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,

bool visitOpaqueTypeDecl(OpaqueTypeDecl *OTD) {
if (Walker.shouldWalkIntoGenericParams() && OTD->getGenericParams()) {
if (visitGenericParamList(OTD->getGenericParams()))
if (doIt(OTD->getGenericParams()))
return true;
}
return false;
Expand Down Expand Up @@ -439,22 +439,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
return false;
}

bool visitGenericParamList(GenericParamList *GPL) {
// Visit generic params
for (auto &P : GPL->getParams()) {
if (doIt(P))
return true;
}

// Visit param conformance
for (auto Req : GPL->getRequirements()) {
if (doIt(Req))
return true;
}

return false;
}

//===--------------------------------------------------------------------===//
// Exprs
//===--------------------------------------------------------------------===//
Expand Down Expand Up @@ -1342,12 +1326,28 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
return true;
break;
case RequirementReprKind::LayoutConstraint:
if (doIt(Req.getFirstTypeRepr()))
if (doIt(Req.getSubjectRepr()))
return true;
break;
}
return false;
}

bool doIt(GenericParamList *GPL) {
// Visit generic params
for (auto &P : GPL->getParams()) {
if (doIt(P))
return true;
}

// Visit param conformance
for (auto Req : GPL->getRequirements()) {
if (doIt(Req))
return true;
}

return false;
}
};

} // end anonymous namespace
Expand Down Expand Up @@ -1877,3 +1877,7 @@ StmtConditionElement *StmtConditionElement::walk(ASTWalker &walker) {
bool Decl::walk(ASTWalker &walker) {
return Traversal(walker).doIt(this);
}

bool GenericParamList::walk(ASTWalker &walker) {
return Traversal(walker).doIt(this);
}
15 changes: 15 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,21 @@ void GenericParamList::setDeclContext(DeclContext *dc) {
param->setDeclContext(dc);
}

GenericTypeParamDecl *GenericParamList::lookUpGenericParam(
Identifier name) const {
for (const auto *innerParams = this;
innerParams != nullptr;
innerParams = innerParams->getOuterParameters()) {
for (auto *paramDecl : *innerParams) {
if (name == paramDecl->getName()) {
return const_cast<GenericTypeParamDecl *>(paramDecl);
}
}
}

return nullptr;
}

TrailingWhereClause::TrailingWhereClause(
SourceLoc whereLoc,
ArrayRef<RequirementRepr> requirements)
Expand Down
4 changes: 3 additions & 1 deletion lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,9 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
const auto ty = swift::performTypeResolution(
ParsedTypeLoc.getTypeRepr(), P.Context,
/*isSILMode=*/false,
/*isSILType=*/false, CurDeclContext->getGenericEnvironmentOfContext(),
/*isSILType=*/false,
CurDeclContext->getGenericEnvironmentOfContext(),
/*GenericParams=*/nullptr,
CurDeclContext,
/*ProduceDiagnostics=*/false);
ParsedTypeLoc.setType(ty);
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ void swift::ide::collectPossibleReturnTypesFromContext(
const auto type = swift::performTypeResolution(
CE->getExplicitResultTypeRepr(), DC->getASTContext(),
/*isSILMode=*/false, /*isSILType=*/false,
DC->getGenericEnvironmentOfContext(),
DC->getGenericEnvironmentOfContext(), /*GenericParams=*/nullptr,
const_cast<DeclContext *>(DC), /*diagnostics=*/false);

if (!type->hasError()) {
Expand Down
Loading