Skip to content
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

[SR-7131] You cannot assign an operator without parens the way you can use an operator in map #49679

Open
swift-ci opened this issue Mar 6, 2018 · 3 comments
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

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 6, 2018

Previous ID SR-7131
Radar None
Original Reporter erica (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI, Parser
Assignee None
Priority Medium

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.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 6, 2018

Comment by Jared Sinclair (JIRA)

This bug also affects default arguments, to wit:

```

// error: expected expression after unary operator
// error: expected initial value after ‘=’
func foo(_ comparison: @escaping Comparison = <)

```

@belkadan
Copy link
Contributor

belkadan commented Mar 8, 2018

It definitely seems like we should be able to parse operators in array literal contexts the way we do in function arguments. The assignment case is trickier because let minus = - looks a lot like let negative = - x, but that can probably be improved. cc @rintaro

(Also, operators are not sugar; they are static methods. But methods don't show up in mirrors, do they? If they do, having operators not show up is a separate bug and should be filed separately.)

@rintaro
Copy link
Mannequin

rintaro mannequin commented Mar 15, 2018

Operators in parentheses are specially handled in `Parser::parseExprList()` . We can factor out this, and use it from parsing array/dictionary literal.

As for diagnostics for bare operator (e.g. let c: Fooquatable = ==), we definitely should improve it. Maybe, if the next token is not on the same line or not starts with non-right bondable characters (e.g. ')' '}' ','), we can treat it as a bare operator.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

2 participants