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.27k stars 1.58k forks source link

[warning] add a diagnostic for unguarded self-recursion #56871

Open jbactual opened 1 month ago

jbactual commented 1 month ago
class Point {
  int? x;
  int? y;
  Point({this.x, this.y});

  @override
  toString() {
    return {"x": x.toString(), "y": y.toString()}.toString();
  }
  // This throws a Stack Overflow
  void operator []=(Object key, Object value) {
    this[key] = value; // no lint error
  }
}

void main() {
  var point = Point(
    x: 1,
    y: 1,
  );
  print(point.toString());

  point["x"] = 2;
  point["y"] = 3;
  print(point.toString());
}
dart-github-bot commented 1 month ago

Summary: The user is experiencing a Stack Overflow error when attempting to use the []= operator to modify the x and y properties of a Point object. The issue arises from a recursive call within the []= operator definition, leading to an infinite loop.

julemand101 commented 1 month ago

You are calling the operator function []= itself inside this function:

  void operator []=(Object key, Object value) {
    this[key] = value; // no lint error
  }

Where this[key] points to the exact same operator function you are trying to define. So since the operator calls itself infinite amount of times while growing the stack, you will get a StackOverflow exception at some point.

What is the purpose of this issue? If you are asking for the linter to notice this mistake, I don't really think this kind of mistake happens enough times for being worth putting resources into this. Especially since it can sometimes be lot more complicated code that results in this issue and where the linter most likely will not be able to identify the issue.

lrhn commented 1 month ago

I do read this as a request for an analyzer warning, perhaps on unguarded self-recursion.