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

analyzer: Using a const instance as the "bitwise or expression" in a relational pattern reports a weird error #51679

Closed
srawlins opened this issue Mar 9, 2023 · 5 comments
Assignees
Labels
analyzer-ux area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on

Comments

@srawlins
Copy link
Member

srawlins commented Mar 9, 2023

Take this slight modification to the Relational pattern example from the spec:

class MyInt {
  const MyInt(int i);
  
  bool operator <(MyInt other) => false;
}

String asciiCharType(int char) {
  const space = MyInt(32);
  const zero = 48;
  const nine = 57;

  return switch (char) {
    < space => 'control', // error
    == space => 'space',
    > space && < zero => 'punctuation', // error
    >= zero && <= nine => 'digit'
    // Etc...
  };
}

This code (today on DartPad) results in an odd error on the // error lines:

The argument type 'MyInt' can't be assigned to the parameter type 'num'.

I don't understand this message:

  • I don't think the "bitwise or expression" has to be a num, so I don't know where 'num' is coming from.
  • I don't think there is any 'MyInt' argument, or any 'num' parameter, in a pattern.

CC @scheglov @munificent

@srawlins srawlins added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-ux P2 A bug or feature request we're likely to work on labels Mar 9, 2023
@scheglov
Copy link
Contributor

scheglov commented Mar 9, 2023

Here we match char against space.
This means matching int < MyInt.
This means invocation of int.<(num).
And we try to pass MyInt where num is expected, so the error.

I don't understand the portion about "bitwise or expression", there are no || anywhere.

@srawlins
Copy link
Member Author

srawlins commented Mar 9, 2023

This means invocation of int.<(num).

Ah I see. This makes sense. If I change the parameter to MyInt char, then the errors go away.

I still hope we can separate into a different error message, other than "The argument type 'MyInt' can't be assigned to the parameter type 'num'." Perhaps something like "The constant expression type 'MyInt' can't be used in this relational pattern against a 'num'"?

I don't know all of the terms we're using for patterns and errors.

I don't understand the portion about "bitwise or expression", there are no || anywhere.

I don't understand it either, I'm just using the name from the spec. Maybe a typo.

@bwilkerson
Copy link
Member

Not a typo. That's a grammar production, so it means you can use any expression whose precedence is less than or equal to the precedence of the bitwise-or operator.

@scheglov scheglov self-assigned this Mar 9, 2023
@srawlins
Copy link
Member Author

srawlins commented Mar 9, 2023

Ah I see. Sounds good.

@scheglov
Copy link
Contributor

scheglov commented Mar 9, 2023

copybara-service bot pushed a commit that referenced this issue Mar 10, 2023
Bug: #51679
Change-Id: Ia0655afa328dbc20ee7ee0fb62675a0f26f9be19
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/287940
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-ux area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

3 participants