[SR-7131] You cannot assign an operator without parens the way you can use an operator in map #49679
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
diagnostics QoI
Bug: Diagnostics Quality of Implementation
parser
Area → compiler: The legacy C++ parser
Additional Detail from JIRA
md5: d4fef6b82184726aebddbce044526757
Issue Description:
This is legal:
let (x, y) = ("Hello.", "There.")
zip(x, y).map(==)
zip(x, y).map(((==)))
This is not:
public typealias Fooquatable = (Foo, Foo) -> Bool
let c: Fooquatable = == // unary operator cannot be separated from its operand
To compile you must surround the == with parens
let c: Fooquatable = (==)
The fixit is inappropriate (it just deletes the new line and any trailing comment), and the reason appears to be the way assignment tokens expect unary operators to operate, even though this is an infix operator:
if (OperEndLoc == Tok.getLoc())
diagnose(PreviousLoc, diag::expected_expr_after_unary_operator);
else
diagnose(PreviousLoc, diag::expected_prefix_operator)
.fixItRemoveChars(OperEndLoc, Tok.getLoc());
It is not overly burdensome to use parens, but it should be made clear that this is the necessary use, not just in direct assignment but when creating collections of operators, for example:
let j: [Fooquatable] = [<=, ==, >=] // expected expression after unary operator
whereas parenthesizing each operator works properly.
I'd also like to see documentation that explains that operator implementations are sugar and are not static members. They do not appear in a mirror reflecting SomeType.self.
The text was updated successfully, but these errors were encountered: