dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

Confusing error message for explicit extension invocation #39254

Open sgrekhov opened 5 years ago

sgrekhov commented 5 years ago
class C {
  int value;
  C() : value = 0 {}
  init() {
    value = 0;
  }
  int get id => value;
  void set id(int v) {
    this.value = v;
  }
}

extension Ext on C {
  int get id => this.value + 1;
}

main() {
  C c = C();
  Ext(c).id++;
}

Analyzer produces

dartanalyzer.bat --enable-experiment=extension-methods explicit_extension_member_invocation_A17_t02.dart
Analyzing explicit_extension_member_invocation_A17_t02.dart...
  error - The setter 'id' isn't defined for the extension 'Ext'. - explicit_extension_member_invocation_A17_t02.dart:34:10 - undefined_extension_setter
1 error found.

That is correct. But if to run the same code in VM the an error message became confusing

dart --enable-experiment=extension-methods explicit_extension_member_invocation_A17_t02.dart
explicit_extension_member_invocation_A17_t02.dart:34:10: Error: The setter 'id' isn't defined for the class 'int'.
Try correcting the name to the name of an existing setter, or defining a setter or field named 'id'.
  Ext(c).id++;
         ^^

There must be the same error message as in Analyzer " error - The setter 'id' isn't defined for the extension 'Ext'. ", not "... in class 'int'"

dartanalyzer version 2.7.0-dev.0.0 Dart VM version: 2.7.0-dev.0.0 (Tue Nov 5 12:57:33 2019 +0100) on "windows_x64"

bwilkerson commented 5 years ago

I'm not able to reproduce this on master. When I create a file containing exactly the code above I get an error that reads

error • The setter 'id' isn't defined for the extension 'Ext'.

It's possible that the bug has already been fixed.

sgrekhov commented 5 years ago

@bwilkerson, I'm sorry, the description was wrong. There is no this issue in analyzer, but there is an issue in VM. I've updated the issue.

sgrekhov commented 5 years ago

One more example of the wrong error message

class C {
  int value;
  C() : value = 0 {}
  C operator +(int val) {
    this.value += val;
    return this;
  }
}

extension Ext1 on C {
  C operator [](int index) => this;
}

extension Ext2 on C {
  void operator []=(int index, C other) {
    this.value += other.value + index;
  }
}

main() {
  C c = C();
  Ext1(c)[42]++; // Error: The method '[]=' isn't defined for the class 'int'
  Ext2(c)[42]++; // Error: The method '[]' isn't defined for the class 'dynamic'.
}

Error messages in an analyzer are correct, but wrong in VM