Skip to content
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

[Parse] Avoid delayed member parsing for type decl with missing brace #32191

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
5 changes: 2 additions & 3 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,8 @@ class Parser {
std::pair<std::vector<Decl *>, Optional<std::string>>
parseDeclListDelayed(IterableDeclContext *IDC);

bool parseMemberDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
SourceLoc PosBeforeLB,
Diag<> ErrorDiag,
bool parseMemberDeclList(SourceLoc &LBLoc, SourceLoc &RBLoc,
Diag<> LBraceDiag, Diag<> RBraceDiag,
IterableDeclContext *IDC);

bool canDelayMemberDeclParsing(bool &HasOperatorDeclarations,
Expand Down
66 changes: 27 additions & 39 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4422,10 +4422,18 @@ ParserStatus Parser::parseDeclItem(bool &PreviousHadSemi,
return Result;
}

bool Parser::parseMemberDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
SourceLoc PosBeforeLB,
Diag<> ErrorDiag,
bool Parser::parseMemberDeclList(SourceLoc &LBLoc, SourceLoc &RBLoc,
Diag<> LBraceDiag, Diag<> RBraceDiag,
IterableDeclContext *IDC) {
if (parseToken(tok::l_brace, LBLoc, LBraceDiag)) {
LBLoc = RBLoc = PreviousLoc;

// Cache the empty result to prevent delayed parsing.
Context.evaluator.cacheOutput(
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we change the evaluator to handle this rather than poking the result into the cache? This currently depends on the cache not really being a cache, but instead global mutable state.

The refactoring of parseMemberDeclList still seems like a readability win to me though.

Copy link
Member Author

@rintaro rintaro Jun 4, 2020

Choose a reason for hiding this comment

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

Although we can use getBrace().Start == getBrace().End as the indicator of "missing member brace", I thought it's a little brittle.

I implement it like this because "eager member parsing" is doing cacheOutput() as well.
https://github.com/apple/swift/blob/09ea5fd1f7fd61ed2ce8be380f1efc3c120a5ba6/lib/Parse/ParseDecl.cpp#L4441-L4458

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. I guess this doesn't make it worse in that case.

ParseMembersRequest{IDC}, FingerprintAndMembers{None, {}});
return true;
}

bool HasOperatorDeclarations;
bool HasNestedClassDeclarations;

Expand All @@ -4444,7 +4452,7 @@ bool Parser::parseMemberDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
bool hadError = false;
ParseDeclOptions Options = getMemberParseDeclOptions(IDC);
auto membersAndHash =
parseDeclList(LBLoc, RBLoc, ErrorDiag, Options, IDC, hadError);
parseDeclList(LBLoc, RBLoc, RBraceDiag, Options, IDC, hadError);
IDC->setMaybeHasOperatorDeclarations();
IDC->setMaybeHasNestedClassDeclarations();
Context.evaluator.cacheOutput(
Expand Down Expand Up @@ -4614,16 +4622,12 @@ Parser::parseDeclExtension(ParseDeclOptions Flags, DeclAttributes &Attributes) {
SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock);
SourceLoc LBLoc, RBLoc;

auto PosBeforeLB = Tok.getLoc();
if (parseToken(tok::l_brace, LBLoc, diag::expected_lbrace_extension)) {
LBLoc = PreviousLoc;
RBLoc = LBLoc;
status.setIsParseError();
} else {
{
ContextChange CC(*this, ext);
Scope S(this, ScopeKind::Extension);

if (parseMemberDeclList(LBLoc, RBLoc, PosBeforeLB,
if (parseMemberDeclList(LBLoc, RBLoc,
diag::expected_lbrace_extension,
diag::expected_rbrace_extension,
ext))
status.setIsParseError();
Expand Down Expand Up @@ -6576,15 +6580,11 @@ ParserResult<EnumDecl> Parser::parseDeclEnum(ParseDeclOptions Flags,

SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock);
SourceLoc LBLoc, RBLoc;
SourceLoc PosBeforeLB = Tok.getLoc();
if (parseToken(tok::l_brace, LBLoc, diag::expected_lbrace_enum)) {
LBLoc = PreviousLoc;
RBLoc = LBLoc;
Status.setIsParseError();
} else {
{
Scope S(this, ScopeKind::EnumBody);

if (parseMemberDeclList(LBLoc, RBLoc, PosBeforeLB,
if (parseMemberDeclList(LBLoc, RBLoc,
diag::expected_lbrace_enum,
diag::expected_rbrace_enum,
ED))
Status.setIsParseError();
Expand Down Expand Up @@ -6862,16 +6862,12 @@ ParserResult<StructDecl> Parser::parseDeclStruct(ParseDeclOptions Flags,
// Make the entities of the struct as a code block.
SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock);
SourceLoc LBLoc, RBLoc;
SourceLoc PosBeforeLB = Tok.getLoc();
if (parseToken(tok::l_brace, LBLoc, diag::expected_lbrace_struct)) {
LBLoc = PreviousLoc;
RBLoc = LBLoc;
Status.setIsParseError();
} else {
{
// Parse the body.
Scope S(this, ScopeKind::StructBody);

if (parseMemberDeclList(LBLoc, RBLoc, PosBeforeLB,
if (parseMemberDeclList(LBLoc, RBLoc,
diag::expected_lbrace_struct,
diag::expected_rbrace_struct,
SD))
Status.setIsParseError();
Expand Down Expand Up @@ -6978,16 +6974,12 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,

SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock);
SourceLoc LBLoc, RBLoc;
auto PosBeforeLB = Tok.getLoc();
if (parseToken(tok::l_brace, LBLoc, diag::expected_lbrace_class)) {
LBLoc = PreviousLoc;
RBLoc = LBLoc;
Status.setIsParseError();
} else {
{
// Parse the body.
Scope S(this, ScopeKind::ClassBody);

if (parseMemberDeclList(LBLoc, RBLoc, PosBeforeLB,
if (parseMemberDeclList(LBLoc, RBLoc,
diag::expected_lbrace_class,
diag::expected_rbrace_class,
CD))
Status.setIsParseError();
Expand Down Expand Up @@ -7079,14 +7071,10 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) {
SyntaxParsingContext BlockContext(SyntaxContext, SyntaxKind::MemberDeclBlock);
SourceLoc LBraceLoc;
SourceLoc RBraceLoc;
SourceLoc PosBeforeLB = Tok.getLoc();
if (parseToken(tok::l_brace, LBraceLoc, diag::expected_lbrace_protocol)) {
LBraceLoc = PreviousLoc;
RBraceLoc = LBraceLoc;
Status.setIsParseError();
} else {
{
// Parse the members.
if (parseMemberDeclList(LBraceLoc, RBraceLoc, PosBeforeLB,
if (parseMemberDeclList(LBraceLoc, RBraceLoc,
diag::expected_lbrace_protocol,
diag::expected_rbrace_protocol,
Proto))
Status.setIsParseError();
Expand Down
10 changes: 10 additions & 0 deletions test/SourceKit/CodeComplete/complete_sequence_innertype.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func test() {
class C:
}

// RUN: %sourcekitd-test \
// RUN: -req=complete -pos=2:11 -repeat-request=2 %s -- %s -parse-as-library \
// RUN: | %FileCheck %s

// CHECK: key.results: [
// CHECK: description: "Int",