sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Expression evaluation returns an error when trying to use extension inside aliased import

Open FMorschel opened this issue 1 year ago • 5 comments
trafficstars

Repro:

Files

main.dart:

import 'ext.dart' as ext;
import 'ext2.dart';

void main() {
  final i = 0;
  print(i.foo());
  print(i.bar());
  print(i.baz());
}

extension E2 on int {
  int bar() => this;
}

ext.dart

extension E on int {
  int foo() => this;
}

ext2.dart

extension E3 on int {
  int baz() => this;
}

Place a breakpoint under main.dart at any line and add all of these to watch:

  • i.foo()
  • i.bar()
  • i.baz()

image

Even with the code running and the analyzer saying nothing, the expression evaluation for i.foo() which is under import 'ext.dart' as ext; throws an error.

Related: https://github.com/dart-lang/sdk/issues/56555


CC: @DanTup

FMorschel avatar Aug 22 '24 17:08 FMorschel

Summary: Expression evaluation fails when attempting to use an extension method imported with an alias, even though the analyzer does not report any errors. This issue occurs when the extension method is defined in a separate file and imported with an alias.

dart-github-bot avatar Aug 22 '24 17:08 dart-github-bot

I believe this issue is in the same area as https://github.com/dart-lang/sdk/issues/56555, marking this as a duplicate of that issue.

a-siva avatar Aug 24 '24 00:08 a-siva

My point in making them separate was so that this one focuses on solving the error and the other one focuses on the error message. But if you believe that they should be handled together thats alright by me.

FMorschel avatar Aug 24 '24 01:08 FMorschel

Unless the reason #56555 shows _Smi as the type name is because of the extension/aliased import, these seem like different issues to me. My understanding is:

  • this issue is about not being able to evaluate expressions using extension members if they imported with an alias
  • that issue is about the message saying "isn't defined for the class _Smi" but it's not clear what _Smi is one might have expected to see int here

DanTup avatar Aug 24 '24 12:08 DanTup

@johnniwinther, It seems like frontend changes are needed to fix this. I inspected the arguments passed to KernelIsolate::CompileExpressionToKernel here once when running the reproduction example as written, and again after changing the import 'ext.dart' as ext; line to just import 'ext.dart';. The arguments passed were the same in both cases, the interesting ones being: expression: "i.foo()", params: ["i"], param_types: ["dart:core", "_Smi", "1", "0"], but compilation fails when the prefix is used to import 'ext.dart' and succeeds when the prefix isn't used. Please let me know if any VM changes are required.

derekxu16 avatar Aug 26 '24 20:08 derekxu16