Skip to content

[QoI] improve diagnostics for operator declarations; unify parsing code #4628

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
Sep 6, 2016
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ ERROR(operator_decl_no_fixity,none,
"operator must be declared as 'prefix', 'postfix', or 'infix'", ())

// PrecedenceGroup
ERROR(precedencegroup_not_infix,none,
"only infix operators may declare a precedence", ())
ERROR(expected_precedencegroup_name,none,
"expected identifier after 'precedencegroup'", ())
ERROR(expected_precedencegroup_lbrace,none,
Expand Down
16 changes: 4 additions & 12 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -825,18 +825,10 @@ class Parser {

ParserResult<OperatorDecl> parseDeclOperator(ParseDeclOptions Flags,
DeclAttributes &Attributes);
ParserResult<OperatorDecl> parseDeclPrefixOperator(SourceLoc OperatorLoc,
Identifier Name,
SourceLoc NameLoc,
DeclAttributes &Attrs);
ParserResult<OperatorDecl> parseDeclPostfixOperator(SourceLoc OperatorLoc,
Identifier Name,
SourceLoc NameLoc,
DeclAttributes &Attrs);
ParserResult<OperatorDecl> parseDeclInfixOperator(SourceLoc OperatorLoc,
Identifier Name,
SourceLoc NameLoc,
DeclAttributes &Attrs);
ParserResult<OperatorDecl> parseDeclOperatorImpl(SourceLoc OperatorLoc,
Identifier Name,
SourceLoc NameLoc,
DeclAttributes &Attrs);

ParserResult<PrecedenceGroupDecl>
parseDeclPrecedenceGroup(ParseDeclOptions flags, DeclAttributes &attributes);
Expand Down
120 changes: 45 additions & 75 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5466,17 +5466,8 @@ Parser::parseDeclOperator(ParseDeclOptions Flags, DeclAttributes &Attributes) {

Identifier Name = Context.getIdentifier(Tok.getText());
SourceLoc NameLoc = consumeToken();

ParserResult<OperatorDecl> Result;
if (Attributes.hasAttribute<PrefixAttr>())
Result = parseDeclPrefixOperator(OperatorLoc, Name, NameLoc, Attributes);
else if (Attributes.hasAttribute<PostfixAttr>())
Result = parseDeclPostfixOperator(OperatorLoc, Name, NameLoc, Attributes);
else {
if (!Attributes.hasAttribute<InfixAttr>())
diagnose(OperatorLoc, diag::operator_decl_no_fixity);
Result = parseDeclInfixOperator(OperatorLoc, Name, NameLoc, Attributes);
}

auto Result = parseDeclOperatorImpl(OperatorLoc, Name, NameLoc, Attributes);

if (!DCC.movedToTopLevel() && !AllowTopLevel) {
diagnose(OperatorLoc, diag::operator_decl_inner_scope);
Expand All @@ -5487,89 +5478,68 @@ Parser::parseDeclOperator(ParseDeclOptions Flags, DeclAttributes &Attributes) {
}

ParserResult<OperatorDecl>
Parser::parseDeclPrefixOperator(SourceLoc OperatorLoc, Identifier Name,
Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
SourceLoc NameLoc, DeclAttributes &Attributes) {
SourceLoc lBraceLoc;
if (consumeIf(tok::l_brace, lBraceLoc)) {
auto Diag = diagnose(lBraceLoc, diag::deprecated_operator_body);
if (Tok.is(tok::r_brace)) {
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
NameLoc);
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
Diag.fixItRemoveChars(lastGoodLocEnd, rBraceEnd);
}

skipUntilDeclRBrace();
(void) consumeIf(tok::r_brace);
}
bool isPrefix = Attributes.hasAttribute<PrefixAttr>();
bool isInfix = Attributes.hasAttribute<InfixAttr>();
bool isPostfix = Attributes.hasAttribute<PostfixAttr>();
Copy link
Member

@rintaro rintaro Sep 5, 2016

Choose a reason for hiding this comment

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

In this PR or later, I suggest to diagnose multiple fixture here.
Currently, postfix infix operator ++++ is accepted.

Although prefix postfix combinations are diagnosed in parseNewDeclAttribute,
we should not simply add DAK_Infix there, because that will emit superfluous diagnostic for func decl:

error: attribute 'infix' cannot be combined with this attribute
infix prefix func ++(x: Int, y: Int) {}
      ^
error: 'infix' modifier is not required or allowed on func declarations
infix prefix func ++(x: Int, y: Int) {}
^~~~~~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you could probably handle DAK_Infix there, if you made it so that infix is forgotten, rather than kept, after emitting the 'infix' modifier is not required or allowed on func declarations error.

Copy link
Member

Choose a reason for hiding this comment

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

If 'infix' modifier is not required or allowed on func declarations' is emitted early enough, marking it invalid will prevent later checks from seeing it. It's fine for that to be a follow-up PR.


auto *Res = new (Context) PrefixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc);
Res->getAttrs() = Attributes;
return makeParserResult(Res);
}

ParserResult<OperatorDecl>
Parser::parseDeclPostfixOperator(SourceLoc OperatorLoc,
Identifier Name, SourceLoc NameLoc,
DeclAttributes &Attributes) {
SourceLoc lBraceLoc;
if (consumeIf(tok::l_brace, lBraceLoc)) {
auto Diag = diagnose(lBraceLoc, diag::deprecated_operator_body);
if (Tok.is(tok::r_brace)) {
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
NameLoc);
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
Diag.fixItRemoveChars(lastGoodLocEnd, rBraceEnd);
}

skipUntilDeclRBrace();
(void) consumeIf(tok::r_brace);
}

auto Res = new (Context) PostfixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc);
Res->getAttrs() = Attributes;
return makeParserResult(Res);
}

ParserResult<OperatorDecl>
Parser::parseDeclInfixOperator(SourceLoc operatorLoc, Identifier name,
SourceLoc nameLoc, DeclAttributes &attributes) {
// Parse (or diagnose) a specified precedence group.
SourceLoc colonLoc;
Identifier precedenceGroupName;
SourceLoc precedenceGroupNameLoc;
if (consumeIf(tok::colon, colonLoc)) {
if (Tok.is(tok::identifier)) {
precedenceGroupName = Context.getIdentifier(Tok.getText());
precedenceGroupNameLoc = consumeToken(tok::identifier);

if (isPrefix || isPostfix)
diagnose(colonLoc, diag::precedencegroup_not_infix)
.fixItRemove({colonLoc, precedenceGroupNameLoc});
}
}


// Diagnose deprecated operator body syntax `operator + { ... }`.
SourceLoc lBraceLoc;
if (consumeIf(tok::l_brace, lBraceLoc)) {
if (Tok.is(tok::r_brace)) {
SourceLoc lastGoodLoc = precedenceGroupNameLoc;
if (lastGoodLoc.isInvalid())
lastGoodLoc = nameLoc;
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
lastGoodLoc);
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
diagnose(lBraceLoc, diag::deprecated_operator_body)
.fixItRemoveChars(lastGoodLocEnd, rBraceEnd);
} else {
if (isInfix && !Tok.is(tok::r_brace)) {
diagnose(lBraceLoc, diag::deprecated_operator_body_use_group);
} else {
auto Diag = diagnose(lBraceLoc, diag::deprecated_operator_body);
if (Tok.is(tok::r_brace)) {
SourceLoc lastGoodLoc = precedenceGroupNameLoc;
if (lastGoodLoc.isInvalid())
lastGoodLoc = NameLoc;
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
lastGoodLoc);
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
Diag.fixItRemoveChars(lastGoodLocEnd, rBraceEnd);
}
}

skipUntilDeclRBrace();
(void) consumeIf(tok::r_brace);
}

auto res = new (Context) InfixOperatorDecl(CurDeclContext, operatorLoc,
name, nameLoc, colonLoc,
precedenceGroupName,
precedenceGroupNameLoc);
res->getAttrs() = attributes;


OperatorDecl *res;
if (Attributes.hasAttribute<PrefixAttr>())
res = new (Context) PrefixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc);
else if (Attributes.hasAttribute<PostfixAttr>())
res = new (Context) PostfixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc);
else {
if (!Attributes.hasAttribute<InfixAttr>())
diagnose(OperatorLoc, diag::operator_decl_no_fixity);

res = new (Context) InfixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc, colonLoc,
precedenceGroupName,
precedenceGroupNameLoc);
}

res->getAttrs() = Attributes;
return makeParserResult(res);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ namespace {
Expr *Guard = nullptr;
};

/// Contexts in which a guarded pattern can appears.
/// Contexts in which a guarded pattern can appear.
enum class GuardedPatternContext {
Case,
Catch,
Expand Down
28 changes: 27 additions & 1 deletion test/Parse/operator_decl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,33 @@ infix operator +++ {} // expected-warning {{operator should no longer be declare
infix operator +++* { // expected-warning {{operator should no longer be declared with body; use a precedence group instead}} {{none}}
associativity right
}
infix operator +++** : A { } // expected-warning {{operator should no longer be declared with body}} {{25-29=}}
infix operator +++*+ : A { } // expected-warning {{operator should no longer be declared with body}} {{25-29=}}


prefix operator +++** : A { }
// expected-error@-1 {{only infix operators may declare a precedence}} {{23-27=}}
// expected-warning@-2 {{operator should no longer be declared with body}} {{26-30=}}

prefix operator ++*++ : A
// expected-error@-1 {{only infix operators may declare a precedence}} {{23-26=}}

postfix operator ++*+* : A { }
// expected-error@-1 {{only infix operators may declare a precedence}} {{24-28=}}
// expected-warning@-2 {{operator should no longer be declared with body}} {{27-31=}}

postfix operator ++**+ : A
// expected-error@-1 {{only infix operators may declare a precedence}} {{24-27=}}

operator ++*** : A
// expected-error@-1 {{operator must be declared as 'prefix', 'postfix', or 'infix'}}

operator +*+++ { }
// expected-error@-1 {{operator must be declared as 'prefix', 'postfix', or 'infix'}}
// expected-warning@-2 {{operator should no longer be declared with body}} {{15-19=}}

operator +*++* : A { }
// expected-error@-1 {{operator must be declared as 'prefix', 'postfix', or 'infix'}}
// expected-warning@-2 {{operator should no longer be declared with body}} {{19-23=}}

prefix operator // expected-error {{expected operator name in operator declaration}}

Expand Down