Skip to content

[QoI] diagnose operator fixity attrs together; improve messages #4661

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 22, 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
7 changes: 5 additions & 2 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1139,9 +1139,12 @@ ERROR(duplicate_attribute,none,
"duplicate %select{attribute|modifier}0", (bool))
NOTE(previous_attribute,none,
"%select{attribute|modifier}0 already specified here", (bool))
ERROR(mutually_exclusive_attrs,none,
"'%0' contradicts previous %select{attribute|modifier}2 '%1'", (StringRef, StringRef, bool))

ERROR(invalid_infix_on_func,none,
"'infix' modifier is not required or allowed on func declarations", ())

ERROR(cannot_combine_attribute,none,
"attribute '%0' cannot be combined with this attribute", (StringRef))
ERROR(expected_in_attribute_list,none,
"expected ']' or ',' in attribute list", ())

Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,6 @@ ERROR(operator_not_func,none,
"operators must be declared with 'func'", ())
ERROR(redefining_builtin_operator,none,
"cannot declare a custom %0 '%1' operator", (StringRef, StringRef))
ERROR(invalid_infix_on_func,none,
"'infix' modifier is not required or allowed on func declarations", ())
ERROR(attribute_requires_operator_identifier,none,
"'%0' requires a function with an operator identifier", (StringRef))
ERROR(attribute_requires_single_argument,none,
Expand Down
63 changes: 50 additions & 13 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,6 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
// Delay issuing the diagnostic until we parse the attribute.
DiscardAttribute = true;
}

if ((DK == DAK_Prefix || DK == DAK_Postfix) && !DiscardAttribute &&
Attributes.getUnaryOperatorKind() != UnaryOperatorKind::None) {
diagnose(Loc, diag::cannot_combine_attribute,
DK == DAK_Prefix ? "postfix" : "prefix");
DiscardAttribute = true;
}

// If this is a SIL-only attribute, reject it.
if ((DeclAttribute::getOptions(DK) & DeclAttribute::SILOnly) != 0 &&
Expand Down Expand Up @@ -1597,6 +1590,51 @@ static bool isStartOfOperatorDecl(const Token &Tok, const Token &Tok2) {
Tok2.isContextualKeyword("infix"));
}

/// \brief Diagnose issues with fixity attributes, if any.
static void diagnoseOperatorFixityAttributes(Parser &P,
DeclAttributes &Attrs,
const Decl *D) {
auto isFixityAttr = [](DeclAttribute *attr){
DeclAttrKind kind = attr->getKind();
return attr->isValid() && (kind == DAK_Prefix ||
kind == DAK_Infix ||
kind == DAK_Postfix);
};

SmallVector<DeclAttribute *, 3> fixityAttrs;
std::copy_if(Attrs.begin(), Attrs.end(),
std::back_inserter(fixityAttrs), isFixityAttr);
std::reverse(fixityAttrs.begin(), fixityAttrs.end());

for (auto it = fixityAttrs.begin(); it != fixityAttrs.end(); ++it) {
if (it != fixityAttrs.begin()) {
auto *attr = *it;
P.diagnose(attr->getLocation(), diag::mutually_exclusive_attrs,
attr->getAttrName(), fixityAttrs.front()->getAttrName(),
attr->isDeclModifier())
.fixItRemove(attr->getRange());
attr->setInvalid();
}
}

// Operator declarations must specify a fixity.
if (auto *OD = dyn_cast<OperatorDecl>(D)) {
if (fixityAttrs.empty()) {
P.diagnose(OD->getOperatorLoc(), diag::operator_decl_no_fixity);
}
}
// Infix operator is only allowed on operator declarations, not on func.
else if (isa<FuncDecl>(D)) {
if (auto *attr = Attrs.getAttribute<InfixAttr>()) {
P.diagnose(attr->getLocation(), diag::invalid_infix_on_func)
.fixItRemove(attr->getLocation());
attr->setInvalid();
}
} else {
llvm_unreachable("unexpected decl kind?");
}
}

static bool isKeywordPossibleDeclStart(const Token &Tok) {
switch (Tok.getKind()) {
case tok::at_sign:
Expand Down Expand Up @@ -4479,6 +4517,8 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
GenericParams, BodyParams, Type(), FuncRetTy,
CurDeclContext);

diagnoseOperatorFixityAttributes(*this, Attributes, FD);

// Add the attributes here so if we need them while parsing the body
// they are available.
FD->getAttrs() = Attributes;
Expand Down Expand Up @@ -5512,23 +5552,20 @@ Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
(void) consumeIf(tok::r_brace);
}


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);

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

diagnoseOperatorFixityAttributes(*this, Attributes, res);

res->getAttrs() = Attributes;
return makeParserResult(res);
Expand Down
8 changes: 0 additions & 8 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,14 +1085,6 @@ void AttributeChecker::checkOperatorAttribute(DeclAttribute *attr) {
return;
}

// Infix operator is only allowed on operator declarations, not on func.
if (isa<InfixAttr>(attr)) {
TC.diagnose(attr->getLocation(), diag::invalid_infix_on_func)
.fixItRemove(attr->getLocation());
attr->setInvalid();
return;
}

// Otherwise, must be unary.
if (!FD->isUnaryOperator()) {
TC.diagnose(attr->getLocation(), diag::attribute_requires_single_argument,
Expand Down
8 changes: 5 additions & 3 deletions test/decl/func/operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ precedencegroup ReallyHighPrecedence {
associativity: left
}

infix func fn_binary(_ lhs: Int, rhs: Int) {} // expected-error {{'infix' requires a function with an operator identifier}}
infix func fn_binary(_ lhs: Int, rhs: Int) {} // expected-error {{'infix' modifier is not required or allowed on func declarations}}


func ++++(lhs: X, rhs: X) -> X {}
Expand Down Expand Up @@ -142,8 +142,10 @@ func test_14705150() {

}

prefix postfix func ++(x: Int) {} // expected-error {{attribute 'prefix' cannot be combined with this attribute}}
postfix prefix func ++(x: Int) {} // expected-error {{attribute 'postfix' cannot be combined with this attribute}}
prefix postfix func ++(x: Int) {} // expected-error {{'postfix' contradicts previous modifier 'prefix'}} {{8-16=}}
postfix prefix func ++(x: Float) {} // expected-error {{'prefix' contradicts previous modifier 'postfix'}} {{9-16=}}
postfix prefix infix func ++(x: Double) {} // expected-error {{'prefix' contradicts previous modifier 'postfix'}} {{9-16=}} expected-error {{'infix' contradicts previous modifier 'postfix'}} {{16-22=}}
infix prefix func +-+(x: Int, y: Int) {} // expected-error {{'infix' modifier is not required or allowed on func declarations}} {{1-7=}} expected-error{{'prefix' contradicts previous modifier 'infix'}} {{7-14=}}

// Don't allow one to define a postfix '!'; it's built into the
// language.
Expand Down