dart-lang / dart-pad

An online Dart editor with support for console, web, and Flutter apps
https://dartpad.dev
BSD 3-Clause "New" or "Revised" License
1.71k stars 555 forks source link

Dart pad - No fault detection #1551

Closed bshlomo closed 4 years ago

bshlomo commented 4 years ago

This code in the dartPad not working well, it should report an error

class TagTable {
  static  Map<String, List<String>> records = const {
    'RCRD_ALL': ['key1', 'key2'],
  };
}

void main() {
  print('1-${TagTable.records}');

  TagTable.records['RCRD_ALL'] = ['key5', 'key6'];

  print('2-${TagTable.records}');
}

--

Win-7 - Microsoft Windows [Version 6.1.7601] Dart VM version: 2.7.2 (Mon Mar 23 22:11:27 2020 +0100) on "windows_x64"

rahadur commented 4 years ago

Ok i think the problem is that you are trying to update const value of records Map. Because const value is modifiable and your are trying to update value of a Map['RCRD_ALL'] that's why your are not getting any compile time error.

Here is the code that works. Read more about default value

class TagTable {

  static Map<String, List> records = const {
    'RCRD_ALL': ['key1', 'key2'],
  };
}

void main() {
  print('1-${TagTable.records}');

  TagTable.records = {'RCRD_ALL': ['key5', 'key6']};  // reinitialize records value

  print('2-${TagTable.records}');
}
mraleph commented 4 years ago

@rahadur I understand you are trying to help - but you should try to understand the reported issue first. @bshlomo is not asking how to make their code work, they are reporting a bug - the code in question should report a runtime error, but it does not, at least in DartPad.

mraleph commented 4 years ago

/cc @sigmundch

mraleph commented 4 years ago

I have tested it with dart2js on the master and it seems to produce correct code that throws an error.

I guess dartpad might have a buggy version of dart2js.

@mit-mit Michael, do you know how often we update dartpad?

mit-mit commented 4 years ago

Moving this to the dartpad repo

mit-mit commented 4 years ago

cc @RedBrogdon @domesticmouse

RedBrogdon commented 4 years ago

I'm seeing an error when this is run:

image

There is no analyzer error when running this code, which is perhaps the source of confusion. I'm going to go ahead and close this, since DartPad is matching the behavior of a command line dartanalyzer call.

I can't for the life of me remember why this isn't caught as a static analysis issue, but I know there's a reason for it. If someone from the Dart team can provide more info, I'd be much obliged. 😄

bshlomo commented 4 years ago

I checked it with Dart 2.8.1, and I got the same result as noted by @RedBrogdon
1551 Thank you

eernstg commented 4 years ago

@RedBrogdon wrote:

I can't for the life of me remember why this isn't caught as a static analysis issue

There are several proposals about adding improved support for immutable entities in Dart (e.g., several of the ideas presented in dart-lang/language#314), but it was initially a property that has no representation in the types of objects.

For example, an unmodifiable list can be created using the constructor List.unmodifiable (so the new object has static type List<...>, just like the modifiable ones). Similarly, const <SomeType>[some, elements] has static type List<...>, and the type system has no idea which lists are mutable and which ones are not.

It's not hard to declare a class whose instances are immutable, say,

class Point {
  final int x,y;
  Point(this.x, this.y);
}

but, for historical reasons, standard data structures in Dart don't do this. Also, we'd need some additional convenience features (like auto-generated deep equality and hashCode) in order to make immutable classes work well.

So I guess the reason why there is no compile-time error for modifying a constant list/map/set is best described as "history". ;)