Skip to content

Commit 09d7f47

Browse files
committed
Fix code completion for custom attributes.
1 parent f7800fd commit 09d7f47

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

include/swift/Parse/Parser.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ class Parser {
656656
/// Read tokens until we get to one of the specified tokens, then
657657
/// return without consuming it. Because we cannot guarantee that the token
658658
/// will ever occur, this skips to some likely good stopping point.
659-
void skipUntil(tok T1, tok T2 = tok::NUM_TOKENS);
659+
ParserStatus skipUntil(tok T1, tok T2 = tok::NUM_TOKENS);
660660
void skipUntilAnyOperator();
661661

662662
/// Skip until a token that starts with '>', and consume it if found.
@@ -680,7 +680,10 @@ class Parser {
680680
/// Note: this does \em not match angle brackets ("<" and ">")! These are
681681
/// matched in the source when they refer to a generic type,
682682
/// but not when used as comparison operators.
683-
void skipSingle();
683+
///
684+
/// Returns a parser status that can capture whether a code completion token
685+
/// was returned.
686+
ParserStatus skipSingle();
684687

685688
/// Skip until the next '#else', '#endif' or until eof.
686689
void skipUntilConditionalBlockClose();

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2821,7 +2821,8 @@ static PatternBindingInitializer *findAttributeInitContent(
28212821

28222822
bool Parser::isCustomAttributeArgument() {
28232823
BacktrackingScope backtrack(*this);
2824-
skipSingle();
2824+
if (skipSingle().hasCodeCompletion())
2825+
return true;
28252826

28262827
// If we have any keyword, identifier, or token that follows a function
28272828
// type's parameter list, this is a parameter list and not an attribute.

lib/Parse/Parser.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -647,49 +647,58 @@ SourceLoc Parser::consumeStartingGreater() {
647647
return consumeStartingCharacterOfCurrentToken(tok::r_angle);
648648
}
649649

650-
void Parser::skipSingle() {
650+
ParserStatus Parser::skipSingle() {
651+
ParserStatus status;
651652
switch (Tok.getKind()) {
652653
case tok::l_paren:
653654
consumeToken();
654-
skipUntil(tok::r_paren, tok::r_brace);
655+
status |= skipUntil(tok::r_paren, tok::r_brace);
655656
consumeIf(tok::r_paren);
656657
break;
657658
case tok::l_brace:
658659
consumeToken();
659-
skipUntil(tok::r_brace);
660+
status |= skipUntil(tok::r_brace);
660661
consumeIf(tok::r_brace);
661662
break;
662663
case tok::l_square:
663664
consumeToken();
664-
skipUntil(tok::r_square, tok::r_brace);
665+
status |= skipUntil(tok::r_square, tok::r_brace);
665666
consumeIf(tok::r_square);
666667
break;
667668
case tok::pound_if:
668669
case tok::pound_else:
669670
case tok::pound_elseif:
670671
consumeToken();
671672
// skipUntil also implicitly stops at tok::pound_endif.
672-
skipUntil(tok::pound_else, tok::pound_elseif);
673+
status |= skipUntil(tok::pound_else, tok::pound_elseif);
673674

674675
if (Tok.isAny(tok::pound_else, tok::pound_elseif))
675-
skipSingle();
676+
status |= skipSingle();
676677
else
677678
consumeIf(tok::pound_endif);
678679
break;
679680

680681
default:
682+
if (Tok.is(tok::code_complete))
683+
status.setHasCodeCompletionAndIsError();
681684
consumeToken();
682685
break;
683686
}
687+
688+
return status;
684689
}
685690

686-
void Parser::skipUntil(tok T1, tok T2) {
691+
ParserStatus Parser::skipUntil(tok T1, tok T2) {
692+
ParserStatus status;
693+
687694
// tok::NUM_TOKENS is a sentinel that means "don't skip".
688-
if (T1 == tok::NUM_TOKENS && T2 == tok::NUM_TOKENS) return;
695+
if (T1 == tok::NUM_TOKENS && T2 == tok::NUM_TOKENS) return status;
689696

690697
while (Tok.isNot(T1, T2, tok::eof, tok::pound_endif, tok::pound_else,
691698
tok::pound_elseif))
692-
skipSingle();
699+
status |= skipSingle();
700+
701+
return status;
693702
}
694703

695704
void Parser::skipUntilAnyOperator() {

test/Syntax/Outputs/round_trip_parse_gen.swift.withkinds

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ typealias G <TypeInitializerClause>= <FunctionType>(<TupleTypeElement>a x: <Simp
145145
typealias H <TypeInitializerClause>= <FunctionType>() rethrows -> <TupleType>()</TupleType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
146146
typealias I <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><CompositionType><CompositionTypeElement><SimpleTypeIdentifier>A </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>B<GenericArgumentClause><<GenericArgument><SimpleTypeIdentifier>C</SimpleTypeIdentifier></GenericArgument>></GenericArgumentClause></SimpleTypeIdentifier></CompositionTypeElement></CompositionType></TupleTypeElement>) -> <CompositionType><CompositionTypeElement><SimpleTypeIdentifier>C </SimpleTypeIdentifier>& </CompositionTypeElement><CompositionTypeElement><SimpleTypeIdentifier>D</SimpleTypeIdentifier></CompositionTypeElement></CompositionType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
147147
typealias J <TypeInitializerClause>= <AttributedType>inout <Attribute>@autoclosure </Attribute><FunctionType>() -> <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></FunctionType></AttributedType></TypeInitializerClause></TypealiasDecl><TypealiasDecl>
148-
typealias K <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><Attribute>@invalidAttr </Attribute><SimpleTypeIdentifier>Int</SimpleTypeIdentifier>, </TupleTypeElement><TupleTypeElement><AttributedType>inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__shared <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__owned <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType></TupleTypeElement>) -> <TupleType>()</TupleType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
148+
typealias K <TypeInitializerClause>= <FunctionType>(<TupleTypeElement><AttributedType><CustomAttribute>@<SimpleTypeIdentifier>invalidAttr </SimpleTypeIdentifier></CustomAttribute><SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>inout <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__shared <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType>, </TupleTypeElement><TupleTypeElement><AttributedType>__owned <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></AttributedType></TupleTypeElement>) -> <TupleType>()</TupleType></FunctionType></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
149149

150150
@objc </Attribute><DeclModifier>private </DeclModifier>typealias T<GenericParameterClause><<GenericParameter>a,</GenericParameter><GenericParameter>b</GenericParameter>> </GenericParameterClause><TypeInitializerClause>= <SimpleTypeIdentifier>Int</SimpleTypeIdentifier></TypeInitializerClause></TypealiasDecl><TypealiasDecl><Attribute>
151151
@objc </Attribute><DeclModifier>private </DeclModifier>typealias T<GenericParameterClause><<GenericParameter>a,</GenericParameter><GenericParameter>b</GenericParameter>></GenericParameterClause></TypealiasDecl><ClassDecl>

0 commit comments

Comments
 (0)