Skip to content

[CodeCompletion] Propagate completion status of parseGenericArguments() #20960

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 1 commit into from
Dec 4, 2018
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: 3 additions & 3 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -972,9 +972,9 @@ class Parser {
/// \brief Parse layout constraint.
LayoutConstraint parseLayoutConstraint(Identifier LayoutConstraintID);

bool parseGenericArguments(SmallVectorImpl<TypeRepr*> &Args,
SourceLoc &LAngleLoc,
SourceLoc &RAngleLoc);
ParserStatus parseGenericArguments(SmallVectorImpl<TypeRepr *> &Args,
SourceLoc &LAngleLoc,
SourceLoc &RAngleLoc);

ParserResult<TypeRepr> parseTypeIdentifier();
ParserResult<TypeRepr> parseOldStyleProtocolComposition();
Expand Down
8 changes: 4 additions & 4 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,9 +1207,9 @@ Parser::parseExprPostfixSuffix(ParserResult<Expr> Result, bool isExprBasic,
if (canParseAsGenericArgumentList()) {
SmallVector<TypeRepr *, 8> args;
SourceLoc LAngleLoc, RAngleLoc;
if (parseGenericArguments(args, LAngleLoc, RAngleLoc)) {
auto argStat = parseGenericArguments(args, LAngleLoc, RAngleLoc);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this status be incorporated into the ParserResult about to be returned?

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, that's unreachable because canParseAsGenericArgumentList() fails if CC token is in it.

if (argStat.isError())
diagnose(LAngleLoc, diag::while_parsing_as_left_angle_bracket);
}

SmallVector<TypeLoc, 8> locArgs;
for (auto ty : args)
Expand Down Expand Up @@ -2259,9 +2259,9 @@ Expr *Parser::parseExprIdentifier() {
if (canParseAsGenericArgumentList()) {
SyntaxContext->createNodeInPlace(SyntaxKind::IdentifierExpr);
SyntaxContext->setCreateSyntax(SyntaxKind::SpecializeExpr);
if (parseGenericArguments(args, LAngleLoc, RAngleLoc)) {
auto argStat = parseGenericArguments(args, LAngleLoc, RAngleLoc);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

if (argStat.isError())
diagnose(LAngleLoc, diag::while_parsing_as_left_angle_bracket);
}

// The result can be empty in error cases.
hasGenericArgumentList = !args.empty();
Expand Down
17 changes: 9 additions & 8 deletions lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
specifierLoc));
}

bool Parser::parseGenericArguments(SmallVectorImpl<TypeRepr*> &Args,
SourceLoc &LAngleLoc,
SourceLoc &RAngleLoc) {
ParserStatus Parser::parseGenericArguments(SmallVectorImpl<TypeRepr *> &Args,
SourceLoc &LAngleLoc,
SourceLoc &RAngleLoc) {
SyntaxParsingContext GenericArgumentsContext(
SyntaxContext, SyntaxKind::GenericArgumentClause);

Expand All @@ -523,7 +523,7 @@ bool Parser::parseGenericArguments(SmallVectorImpl<TypeRepr*> &Args,
if (Ty.isNull() || Ty.hasCodeCompletion()) {
// Skip until we hit the '>'.
RAngleLoc = skipUntilGreaterInTypeList();
return true;
return ParserStatus(Ty);
}

Args.push_back(Ty.get());
Expand All @@ -540,12 +540,12 @@ bool Parser::parseGenericArguments(SmallVectorImpl<TypeRepr*> &Args,

// Skip until we hit the '>'.
RAngleLoc = skipUntilGreaterInTypeList();
return true;
return makeParserError();
} else {
RAngleLoc = consumeStartingGreater();
}

return false;
return makeParserSuccess();
}

/// parseTypeIdentifier
Expand Down Expand Up @@ -597,8 +597,9 @@ ParserResult<TypeRepr> Parser::parseTypeIdentifier() {
SourceLoc LAngle, RAngle;
SmallVector<TypeRepr*, 8> GenericArgs;
if (startsWithLess(Tok)) {
if (parseGenericArguments(GenericArgs, LAngle, RAngle))
return nullptr;
auto genericArgsStatus = parseGenericArguments(GenericArgs, LAngle, RAngle);
if (genericArgsStatus.isError())
return genericArgsStatus;
}
EndLoc = Loc;

Expand Down
40 changes: 32 additions & 8 deletions test/IDE/complete_type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@
// RUN: %FileCheck %s -check-prefix=GLOBAL_NEGATIVE < %t.types.txt


// FIXME: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TYPE_IDENTIFIER_GENERIC_1 > %t.types.txt
// FIXME: %FileCheck %s -check-prefix=TYPE_IDENTIFIER_GENERIC_1 < %t.types.txt
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TYPE_IDENTIFIER_GENERIC_1 > %t.types.txt
// RUN: %FileCheck %s -check-prefix=TYPE_IDENTIFIER_GENERIC_1 < %t.types.txt
// RUN: %FileCheck %s -check-prefix=WITHOUT_GLOBAL_TYPES < %t.types.txt
// RUN: %FileCheck %s -check-prefix=GLOBAL_NEGATIVE < %t.types.txt

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TYPE_IDENTIFIER_GENERIC_2 > %t.types.txt
// FIXME: %FileCheck %s -check-prefix=TYPE_IDENTIFIER_GENERIC_2 < %t.types.txt
// RUN: %FileCheck %s -check-prefix=TYPE_IDENTIFIER_GENERIC_2 < %t.types.txt
// RUN: %FileCheck %s -check-prefix=WITHOUT_GLOBAL_TYPES < %t.types.txt
// RUN: %FileCheck %s -check-prefix=GLOBAL_NEGATIVE < %t.types.txt

Expand All @@ -386,6 +386,16 @@
// RUN: %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES < %t.gentypealias.txt
// RUN: %FileCheck %s -check-prefix=GLOBAL_NEGATIVE < %t.gentypealias.txt

// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_TOPLEVEL_VAR | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_TOPLEVEL_PARAM | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_TOPLEVEL_RETURN | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_MEMBER_VAR | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_MEMBER_PARAM | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_MEMBER_RETURN | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_LOCAL_VAR | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_LOCAL_PARAM | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_ARGS_LOCAL_RETURN | %FileCheck %s -check-prefix=WITH_GLOBAL_TYPES

//===--- Helper types that are used in this test

struct FooStruct {
Expand Down Expand Up @@ -1003,20 +1013,18 @@ func testTypeIdentifierGeneric1<
>(a: GenericFoo.#^TYPE_IDENTIFIER_GENERIC_1^#

// TYPE_IDENTIFIER_GENERIC_1: Begin completions
// TYPE_IDENTIFIER_GENERIC_1-NEXT: Decl[TypeAlias]/Super: FooTypeAlias1{{; name=.+$}}
// TYPE_IDENTIFIER_GENERIC_1-NEXT: Decl[AssociatedType]/Super: FooTypeAlias1{{; name=.+$}}
// TYPE_IDENTIFIER_GENERIC_1-NEXT: Keyword/None: Type[#GenericFoo.Type#]
// TYPE_IDENTIFIER_GENERIC_1-NEXT: Keyword/None: self[#GenericFoo#]
// TYPE_IDENTIFIER_GENERIC_1-NEXT: End completions

func testTypeIdentifierGeneric2<
GenericFoo : FooProtocol & BarProtocol
>(a: GenericFoo.#^TYPE_IDENTIFIER_GENERIC_2^#

// TYPE_IDENTIFIER_GENERIC_2: Begin completions
// TYPE_IDENTIFIER_GENERIC_2-NEXT: Decl[TypeAlias]/Super: BarTypeAlias1{{; name=.+$}}
// TYPE_IDENTIFIER_GENERIC_2-NEXT: Decl[TypeAlias]/Super: FooTypeAlias1{{; name=.+$}}
// TYPE_IDENTIFIER_GENERIC_2-NEXT: Decl[AssociatedType]/Super: BarTypeAlias1{{; name=.+$}}
// TYPE_IDENTIFIER_GENERIC_2-NEXT: Decl[AssociatedType]/Super: FooTypeAlias1{{; name=.+$}}
// TYPE_IDENTIFIER_GENERIC_2-NEXT: Keyword/None: Type[#GenericFoo.Type#]
// TYPE_IDENTIFIER_GENERIC_2-NEXT: Keyword/None: self[#GenericFoo#]
// TYPE_IDENTIFIER_GENERIC_2-NEXT: End completions

func testTypeIdentifierGeneric3<
Expand Down Expand Up @@ -1058,3 +1066,19 @@ func testGenericTypealias2() {
typealias MyPair<T> = (T, T)
let x: MyPair<#^GENERIC_TYPEALIAS_2^#>
}

// In generic argument
struct GenStruct<T> { }
let a : GenStruct<#^GENERIC_ARGS_TOPLEVEL_VAR^#
func foo1(x: GenStruct<#^GENERIC_ARGS_TOPLEVEL_PARAM^#
func foo2() -> GenStruct<#^GENERIC_ARGS_TOPLEVEL_RETURN^#
class _TestForGenericArg_ {
let a : GenStruct<#^GENERIC_ARGS_MEMBER_VAR^#
func foo1(x: GenStruct<#^GENERIC_ARGS_MEMBER_PARAM^#
func foo2() -> GenStruct<#^GENERIC_ARGS_MEMBER_RETURN^#
}
func _testForGenericArg_() {
let a : GenStruct<#^GENERIC_ARGS_LOCAL_VAR^#
func foo1(x: GenStruct<#^GENERIC_ARGS_LOCAL_PARAM^#
func foo2() -> GenStruct<#^GENERIC_ARGS_LOCAL_RETURN^#
}