Skip to content

[CodeCompletion] Stop using temporary Lexer in the second pass #28433

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 3 commits into from
Dec 4, 2019
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
9 changes: 8 additions & 1 deletion lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ void typeCheckContextImpl(DeclContext *DC, SourceLoc Loc) {

case DeclContextKind::AbstractFunctionDecl: {
auto *AFD = cast<AbstractFunctionDecl>(DC);
swift::typeCheckAbstractFunctionBodyUntil(AFD, Loc);
auto &SM = DC->getASTContext().SourceMgr;
auto bodyRange = AFD->getBodySourceRange();
if (SM.rangeContainsTokenLoc(bodyRange, Loc)) {
swift::typeCheckAbstractFunctionBodyUntil(AFD, Loc);
} else {
assert(bodyRange.isInvalid() && "The body should not be parsed if the "
"completion happens in the signature");
}
break;
}

Expand Down
100 changes: 50 additions & 50 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5646,6 +5646,7 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
}
}

ParserStatus Status;
SourceLoc FuncLoc =
HasFuncKeyword ? consumeToken(tok::kw_func) : Tok.getLoc();

Expand Down Expand Up @@ -5702,27 +5703,25 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
Optional<Scope> GenericsScope;
GenericsScope.emplace(this, ScopeKind::Generics);
GenericParamList *GenericParams;
bool SignatureHasCodeCompletion = false;
auto GenericParamResult = maybeParseGenericParams();
GenericParams = GenericParamResult.getPtrOrNull();
SignatureHasCodeCompletion |= GenericParamResult.hasCodeCompletion();
if (SignatureHasCodeCompletion && !CodeCompletion)
return makeParserCodeCompletionStatus();
if (GenericParamResult.hasCodeCompletion()) {
Status.setHasCodeCompletion();
if (!CodeCompletion)
return Status;
}

DefaultArgumentInfo DefaultArgs;
TypeRepr *FuncRetTy = nullptr;
DeclName FullName;
ParameterList *BodyParams;
SourceLoc throwsLoc;
bool rethrows;
ParserStatus SignatureStatus =
parseFunctionSignature(SimpleName, FullName, BodyParams, DefaultArgs,
throwsLoc, rethrows, FuncRetTy);

SignatureHasCodeCompletion |= SignatureStatus.hasCodeCompletion();
if (SignatureStatus.hasCodeCompletion() && !CodeCompletion) {
Status |= parseFunctionSignature(SimpleName, FullName, BodyParams,
DefaultArgs, throwsLoc, rethrows, FuncRetTy);
if (Status.hasCodeCompletion() && !CodeCompletion) {
// Trigger delayed parsing, no need to continue.
return SignatureStatus;
return Status;
}

diagnoseWhereClauseInGenericParamList(GenericParams);
Expand All @@ -5747,11 +5746,10 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
if (Tok.is(tok::kw_where)) {
ContextChange CC(*this, FD);

auto whereStatus = parseFreestandingGenericWhereClause(GenericParams);
SignatureHasCodeCompletion |= whereStatus.hasCodeCompletion();
if (whereStatus.hasCodeCompletion() && !CodeCompletion) {
Status |= parseFreestandingGenericWhereClause(GenericParams);
if (Status.hasCodeCompletion() && !CodeCompletion) {
// Trigger delayed parsing, no need to continue.
return whereStatus;
return Status;
}
}

Expand All @@ -5772,8 +5770,10 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
FD->getAttrs() = Attributes;

// Pass the function signature to code completion.
if (SignatureHasCodeCompletion)
if (Status.hasCodeCompletion()) {
assert(CodeCompletion && "must be code completion second pass");
CodeCompletion->setParsedDecl(FD);
}

DefaultArgs.setFunctionContext(FD, FD->getParameters());
setLocalDiscriminator(FD);
Expand All @@ -5783,7 +5783,7 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
diagnose(Tok, diag::protocol_method_with_body);
skipSingle();
}
} else {
} else if (!Status.hasCodeCompletion()) {
parseAbstractFunctionBody(FD);
}

Expand Down Expand Up @@ -6545,14 +6545,14 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
Optional<Scope> GenericsScope;
GenericsScope.emplace(this, ScopeKind::Generics);
GenericParamList *GenericParams;
bool SignatureHasCodeCompletion = false;

auto Result = maybeParseGenericParams();
GenericParams = Result.getPtrOrNull();
SignatureHasCodeCompletion |= Result.hasCodeCompletion();

if (SignatureHasCodeCompletion && !CodeCompletion)
return makeParserCodeCompletionStatus();
if (Result.hasCodeCompletion()) {
Status.setHasCodeCompletion();
if (!CodeCompletion)
return Status;
}

// Parse the parameter list.
DefaultArgumentInfo DefaultArgs;
Expand All @@ -6561,10 +6561,8 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
= parseSingleParameterClause(ParameterContextKind::Subscript,
&argumentNames, &DefaultArgs);
Status |= Indices;

SignatureHasCodeCompletion |= Indices.hasCodeCompletion();
if (SignatureHasCodeCompletion && !CodeCompletion)
return makeParserCodeCompletionStatus();
if (Status.hasCodeCompletion() && !CodeCompletion)
return Status;

SourceLoc ArrowLoc;
ParserResult<TypeRepr> ElementTy;
Expand All @@ -6588,10 +6586,9 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
// type
ElementTy = parseDeclResultType(diag::expected_type_subscript);
Status |= ElementTy;
SignatureHasCodeCompletion |= ElementTy.hasCodeCompletion();
if (SignatureHasCodeCompletion && !CodeCompletion) {
return makeParserCodeCompletionStatus();
}
if (Status.hasCodeCompletion() && !CodeCompletion)
return Status;

if (ElementTy.isNull()) {
// Always set an element type.
ElementTy = makeParserResult(ElementTy, new (Context) ErrorTypeRepr());
Expand Down Expand Up @@ -6625,16 +6622,16 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
if (Tok.is(tok::kw_where)) {
ContextChange CC(*this, Subscript);

auto whereStatus = parseFreestandingGenericWhereClause(GenericParams);
SignatureHasCodeCompletion |= whereStatus.hasCodeCompletion();
if (whereStatus.hasCodeCompletion() && !CodeCompletion) {
Status |= parseFreestandingGenericWhereClause(GenericParams);
if (Status.hasCodeCompletion() && !CodeCompletion) {
// Trigger delayed parsing, no need to continue.
return whereStatus;
return Status;
}
}

// Pass the function signature to code completion.
if (SignatureHasCodeCompletion && CodeCompletion) {
if (Status.hasCodeCompletion()) {
assert(CodeCompletion && "must be code completion second pass");
CodeCompletion->setParsedDecl(Subscript);
}

Expand All @@ -6655,7 +6652,7 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
}
Status.setIsParseError();
}
} else {
} else if (!Status.hasCodeCompletion()) {
Status |= parseGetSet(Flags, GenericParams, Indices.get(),
accessors, Subscript, StaticLoc);
}
Expand All @@ -6680,6 +6677,7 @@ Parser::parseDeclSubscript(SourceLoc StaticLoc,
ParserResult<ConstructorDecl>
Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
assert(Tok.is(tok::kw_init));
ParserStatus Status;
SourceLoc ConstructorLoc = consumeToken();
bool Failable = false, IUO = false;
SourceLoc FailabilityLoc;
Expand Down Expand Up @@ -6714,21 +6712,22 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
Scope S(this, ScopeKind::Generics);
auto GPResult = maybeParseGenericParams();
GenericParamList *GenericParams = GPResult.getPtrOrNull();
if (GPResult.hasCodeCompletion())
return makeParserCodeCompletionStatus();
if (GPResult.hasCodeCompletion()) {
Status.setHasCodeCompletion();
if (!CodeCompletion)
return Status;
}

// Parse the parameters.
DefaultArgumentInfo DefaultArgs;
llvm::SmallVector<Identifier, 4> namePieces;
bool SignatureHasCodeCompletion = false;
ParserResult<ParameterList> Params
= parseSingleParameterClause(ParameterContextKind::Initializer,
&namePieces, &DefaultArgs);

SignatureHasCodeCompletion |= Params.hasCodeCompletion();
if (Params.hasCodeCompletion() && !CodeCompletion) {
Status |= Params;
if (Status.hasCodeCompletion() && !CodeCompletion) {
// Trigger delayed parsing, no need to continue.
return makeParserCodeCompletionStatus();
return Status;
}

// Protocol initializer arguments may not have default values.
Expand Down Expand Up @@ -6760,11 +6759,10 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
if (Tok.is(tok::kw_where)) {
ContextChange(*this, CD);

auto whereStatus = parseFreestandingGenericWhereClause(GenericParams);
SignatureHasCodeCompletion |= whereStatus.hasCodeCompletion();
if (whereStatus.hasCodeCompletion() && !CodeCompletion) {
Status |= parseFreestandingGenericWhereClause(GenericParams);
if (Status.hasCodeCompletion() && !CodeCompletion) {
// Trigger delayed parsing, no need to continue.
return whereStatus;
return Status;
}
}

Expand All @@ -6773,8 +6771,10 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
DefaultArgs.setFunctionContext(CD, CD->getParameters());

// Pass the function signature to code completion.
if (SignatureHasCodeCompletion)
if (Status.hasCodeCompletion()) {
assert(CodeCompletion && "must be code completion second pass");
CodeCompletion->setParsedDecl(CD);
}

if (ConstructorsNotAllowed || Params.isParseError()) {
// Tell the type checker not to touch this constructor.
Expand All @@ -6786,11 +6786,11 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
diagnose(Tok, diag::protocol_init_with_body);
skipSingle();
}
} else {
} else if(!Status.hasCodeCompletion()) {
parseAbstractFunctionBody(CD);
}

return makeParserResult(CD);
return makeParserResult(Status, CD);
}

ParserResult<DestructorDecl> Parser::
Expand Down
20 changes: 2 additions & 18 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,8 @@ void Parser::performCodeCompletionSecondPassImpl(
// Disable libSyntax creation in the delayed parsing.
SyntaxContext->disable();

auto BeginParserPosition = getParserPosition(info.BodyPos);
auto EndLexerState = L->getStateForEndOfTokenLoc(info.BodyEnd);

// ParserPositionRAII needs a primed parser to restore to.
if (Tok.is(tok::NUM_TOKENS))
consumeTokenWithoutFeedingReceiver();

// Ensure that we restore the parser state at exit.
ParserPositionRAII PPR(*this);

// Create a lexer that cannot go past the end state.
Lexer LocalLex(*L, BeginParserPosition.LS, EndLexerState);

// Temporarily swap out the parser's current lexer with our new one.
llvm::SaveAndRestore<Lexer *> T(L, &LocalLex);

// Rewind to the beginning of the top-level code.
restoreParserPosition(BeginParserPosition);
// Set the parser position to the start of the delayed decl or the body.
restoreParserPosition(getParserPosition(info.BodyPos));

// Do not delay parsing in the second pass.
llvm::SaveAndRestore<bool> DisableDelayedBody(DelayBodyParsing, false);
Expand Down
14 changes: 5 additions & 9 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2121,15 +2121,11 @@ TypeCheckFunctionBodyUntilRequest::evaluate(Evaluator &evaluator,
builderType);
if (!body)
return true;
} else if (func->hasSingleExpressionBody()) {
auto resultTypeLoc = func->getBodyResultTypeLoc();
auto expr = func->getSingleExpressionBody();

if (resultTypeLoc.isNull() || resultTypeLoc.getType()->isVoid()) {
// The function returns void. We don't need an explicit return, no matter
// what the type of the expression is. Take the inserted return back out.
body->setFirstElement(expr);
}
} else if (func->hasSingleExpressionBody() &&
func->getResultInterfaceType()->isVoid()) {
// The function returns void. We don't need an explicit return, no matter
// what the type of the expression is. Take the inserted return back out.
body->setFirstElement(func->getSingleExpressionBody());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DougGregor Do you have any concern about this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this seems like a clear improvement.

}
} else if (isa<ConstructorDecl>(AFD) &&
(body->empty() ||
Expand Down
3 changes: 0 additions & 3 deletions test/Parse/recovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,6 @@ struct InitializerWithNameAndParam {
struct InitializerWithLabels {
init c d: Int {}
// expected-error @-1 {{expected '(' for initializer parameters}}
// expected-error @-2 {{expected declaration}}
// expected-error @-3 {{consecutive declarations on a line must be separated by ';'}}
// expected-note @-5 {{in declaration of 'InitializerWithLabels'}}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because parseDeclInit now returns an error state. So the parser property skips to the next decl or '}'

}

// rdar://20337695
Expand Down