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

[Extension types] Analyzer reports incorrect "const eval throws" for extension constructor. #53935

Closed
Tracked by #49732
lrhn opened this issue Nov 2, 2023 · 6 comments
Assignees
Labels
analyzer-constants area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. feature-extension-types Implementation of the extension type feature P1 A high priority bug; for example, a single project is unusable or has many test failures P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@lrhn
Copy link
Member

lrhn commented Nov 2, 2023

Example:

extension type const Num(num value) {
  const Num.add(Num v1, Num v2) : this((v1 as num) + (v2 as num));
  const Num.sub(Num v1, Num v2) : this((v1 as num) - (v2 as num));
  const Num.mul(Num v1, Num v2) : this((v1 as num) * (v2 as num));
  //                                    ^^^^^^^^^
}

void main() {
  const n = Num.sub(Num.mul(Num(7), Num(7)), Num(7));
  print(n.value);
}

The CFE (VM) prints 42 as expected, with nothing throwing.

The analyzer reports:

  error - const-eval.dart:4:41 - Evaluation of this constant expression
          throws an exception. - const_eval_throws_exception

The marked position is what is shown in dartpad.dev, which is at the v1 as num cast of mul.

That is the first cast to be reached in the computation, so most likely the analyzer's constant evaluator doesn't recognize that an expression with runtime value 7 and static type Num can be safely cast to num.

(I use as num instead of ._ because the former is a potentially constant expression, and the latter is not. Either should be no-ops at runtime, but only one is allowed in a const constructor.

@lrhn lrhn added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Nov 2, 2023
@bwilkerson
Copy link
Member

@kallentu

@bwilkerson bwilkerson added the feature-extension-types Implementation of the extension type feature label Nov 2, 2023
@pq pq added P1 A high priority bug; for example, a single project is unusable or has many test failures P2 A bug or feature request we're likely to work on analyzer-constants labels Nov 2, 2023
@kallentu
Copy link
Member

kallentu commented Nov 2, 2023

I don't think any additional support has been added to the const evaluator to handle extension types.
@scheglov Are you going to be working on this at some point? I'm not sure where the feature last ended off.

@scheglov
Copy link
Contributor

scheglov commented Nov 2, 2023

Honestly, I hoped that you will implement this portion, given your new experience with constant evaluation.

@scheglov
Copy link
Contributor

scheglov commented Nov 7, 2023

Clarification for #53935 (comment).
I did not try to be rude or something.

Here is the translation of what I wanted to say through an LLM.

I'm currently occupied with other commitments, but I'm eager to get this task completed. I understand that @kallentu has experience with constant evaluation, and I would greatly appreciate their assistance if they are available. If @kallentu is unable to contribute, I'm fully prepared to take on the responsibility. Additionally, if anyone else is interested in helping, I'm open to their involvement and would be grateful for their support.

@kallentu
Copy link
Member

kallentu commented Nov 8, 2023

@scheglov No problem! I don't think I have the capacity to complete the fix for this, unfortunately. If you or someone else could take this, that would be great.

I did have a quick glance at what the issue might be though and it's in DartObjectImpl.castToType which calls typeSystem.isSubtypeOf and the subtyping algorithm doesn't handle the difference between the extension type Num and num in this case. Perhaps an update is needed there.

@scheglov scheglov self-assigned this Nov 8, 2023
@scheglov
Copy link
Contributor

scheglov commented Nov 8, 2023

@scheglov scheglov closed this as completed Nov 9, 2023
copybara-service bot pushed a commit that referenced this issue Nov 9, 2023
Bug: #53935
Bug: #53751
Change-Id: I514c79c79c9229b7fed313712b8f1d7a3ed91ca7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/334862
Reviewed-by: Kallen Tu <kallentu@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-constants area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. feature-extension-types Implementation of the extension type feature P1 A high priority bug; for example, a single project is unusable or has many test failures P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants