Skip to content

Commit 3ad9c58

Browse files
committed
Disallow (with a nice fixit) the infix modifier on func decls, it is just validated
and ignored anyway. It is still required on operator decls though. Swift SVN r19978
1 parent 666e198 commit 3ad9c58

File tree

4 files changed

+18
-23
lines changed

4 files changed

+18
-23
lines changed

include/swift/AST/DiagnosticsSema.def

+2-6
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,13 @@ ERROR(operator_not_func,sema_tcd,none,
292292
"operators must be declared with 'func'", ())
293293
ERROR(redefining_builtin_operator,sema_tcd,none,
294294
"cannot declare a custom %0 '%1' operator", (StringRef, StringRef))
295-
296-
ERROR(invalid_infix_input,sema_tcd,none,
297-
"function with 'infix' specified must take a two element tuple as input",
298-
())
295+
ERROR(invalid_infix_on_func,sema_tcd,none,
296+
"'infix' modifier is not required or allowed on func declarations", ())
299297
ERROR(attribute_requires_operator_identifier,sema_tcd,none,
300298
"'%0' requires a function with an operator identifier", (StringRef))
301299
ERROR(attribute_requires_single_argument,sema_tcd,none,
302300
"'%0' requires a function with one argument", (StringRef))
303301

304-
ERROR(assignment_without_inout,sema_tcd,none,
305-
"assignment operator must have an initial inout argument", ())
306302
ERROR(inout_cant_be_variadic,sema_tce,none,
307303
"inout arguments cannot be variadic", ())
308304
ERROR(inout_only_parameter,sema_tce,none,

lib/Sema/TypeCheckAttr.cpp

+13-14
Original file line numberDiff line numberDiff line change
@@ -649,21 +649,20 @@ void AttributeChecker::checkOperatorAttribute(DeclAttribute *attr) {
649649
return;
650650
}
651651

652-
// Infix operator must be binary.
652+
// Infix operator is only allowed on operator declarations, not on func.
653653
if (isa<InfixAttr>(attr)) {
654-
if (!FD->isBinaryOperator()) {
655-
TC.diagnose(attr->getLocation(), diag::invalid_infix_input);
656-
attr->setInvalid();
657-
return;
658-
}
659-
} else {
660-
// Otherwise, must be unary.
661-
if (!FD->isUnaryOperator()) {
662-
TC.diagnose(attr->getLocation(), diag::attribute_requires_single_argument,
663-
attr->getAttrName());
664-
attr->setInvalid();
665-
return;
666-
}
654+
TC.diagnose(attr->getLocation(), diag::invalid_infix_on_func)
655+
.fixItRemove(attr->getLocation());
656+
attr->setInvalid();
657+
return;
658+
}
659+
660+
// Otherwise, must be unary.
661+
if (!FD->isUnaryOperator()) {
662+
TC.diagnose(attr->getLocation(), diag::attribute_requires_single_argument,
663+
attr->getAttrName());
664+
attr->setInvalid();
665+
return;
667666
}
668667
}
669668

stdlib/core/IntegerArithmetic.swift.gyb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func ${op}= <T: _IntegerArithmeticType>(inout lhs: T, rhs: T) {
6060
// protocol should be presented to users as part of SignedNumberType.
6161
public protocol _SignedNumberType : Comparable, IntegerLiteralConvertible {
6262
// Subtraction is a requirement for SignedNumberType
63-
infix func - (lhs: Self, rhs: Self) -> Self
63+
func - (lhs: Self, rhs: Self) -> Self
6464
}
6565

6666
// SignedNumberType itself contains only operator requirements having

test/decl/operator/operators.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ postfix operator -+- {}
8383

8484
infix operator +-+= {}
8585

86-
infix func +-+ (x: Int, y: Int) -> Int {}
86+
infix func +-+ (x: Int, y: Int) -> Int {} // expected-error {{'infix' modifier is not required or allowed on func declarations}}
8787
prefix func +-+ (x: Int) -> Int {}
8888

8989
prefix func -+- (inout y: Int) -> Int {} // expected-note 2{{found this candidate}}
9090
postfix func -+- (inout x: Int) -> Int {} // expected-note 2{{found this candidate}}
9191

92-
infix func +-+= (inout x: Int, y: Int) -> Int {}
92+
infix func +-+= (inout x: Int, y: Int) -> Int {} // expected-error {{'infix' modifier is not required or allowed on func declarations}}
9393

9494
var n = 0
9595

0 commit comments

Comments
 (0)