Add missing method to UnmodifiableMapMixin.

Fixes 34256
BUG= http://dartbug.com/34256

Change-Id: I780368176b0111130bee1b5aae1f5228eabfa16f
Reviewed-on: https://dart-review.googlesource.com/71460
Reviewed-by: Nate Bosch <nbosch@google.com>
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
This commit is contained in:
Lasse Reichstein Holst Nielsen 2018-08-28 05:11:12 +00:00 committed by commit-bot@chromium.org
parent 4b8d3e4cf1
commit edd6b75a05
4 changed files with 52 additions and 6 deletions

View file

@ -13,11 +13,18 @@
#### Other Tools
### Core library changes
* `dart:async`
* Update `Stream.fromIterable` to send a done event after the the error when
#### `dart:async`
* Update `Stream.fromIterable` to send a done event after the the error when
the iterator's `moveNext` throws, and handle if the `current` getter throws.
Issue [33431](http://dartbug.com/33431).
#### `dart:core`
* Added missing methods to `UnmodifiableMapMixin`. Some maps intended to
be unmodifiable incorrectly allowed new methods added in Dart 2 to
succeed.
## 2.1.0-dev.2.0
### Tool Changes

View file

@ -282,6 +282,11 @@ abstract class _UnmodifiableMapMixin<K, V> implements Map<K, V> {
throw new UnsupportedError("Cannot modify unmodifiable map");
}
/** This operation is not supported by an unmodifiable map. */
void addEntries(Iterable<MapEntry<K, V>> entries) {
throw new UnsupportedError("Cannot modify unmodifiable map");
}
/** This operation is not supported by an unmodifiable map. */
void clear() {
throw new UnsupportedError("Cannot modify unmodifiable map");
@ -292,10 +297,25 @@ abstract class _UnmodifiableMapMixin<K, V> implements Map<K, V> {
throw new UnsupportedError("Cannot modify unmodifiable map");
}
/** This operation is not supported by an unmodifiable map. */
void removeWhere(bool test(K key, V value)) {
throw new UnsupportedError("Cannot modify unmodifiable map");
}
/** This operation is not supported by an unmodifiable map. */
V putIfAbsent(K key, V ifAbsent()) {
throw new UnsupportedError("Cannot modify unmodifiable map");
}
/** This operation is not supported by an unmodifiable map. */
V update(K key, V update(V value), {V ifAbsent()}) {
throw new UnsupportedError("Cannot modify unmodifiable map");
}
/** This operation is not supported by an unmodifiable map. */
void updateAll(V update(K key, V value)) {
throw new UnsupportedError("Cannot modify unmodifiable map");
}
}
/**
@ -341,7 +361,7 @@ class MapView<K, V> implements Map<K, V> {
Iterable<MapEntry<K, V>> get entries => _map.entries;
void addEntries(Iterable<Object> entries) {
void addEntries(Iterable<MapEntry<K, V>> entries) {
_map.addEntries(entries);
}

View file

@ -89,6 +89,7 @@ symbol_test/03: MissingCompileTimeError
[ !$strong ]
cast_test: SkipByDesign # Uses generic method parameters.
iterable_where_type_test: SkipByDesign
map_test: SkipByDesign
regress_33166_test: SkipByDesign # Not a Dart 1 test
[ $arch == simarmv5te && ($runtime == dart_precompiled || $runtime == vm) ]

View file

@ -27,6 +27,7 @@ void main() {
testMapLiteral();
testNullValue();
testTypes();
testUnmodifiableMaps();
testWeirdStringKeys(new Map());
testWeirdStringKeys(new Map<String, String>());
@ -703,8 +704,7 @@ class Equalizer {
int id;
Equalizer(this.id);
int get hashCode => id;
bool operator ==(Object other) =>
other is Equalizer && id == (other as Equalizer).id;
bool operator ==(Object other) => other is Equalizer && id == other.id;
}
/**
@ -721,7 +721,7 @@ class Vampire {
// The double-fang operator falsely claims that a vampire is equal to
// any of its sire's generation.
bool operator ==(Object other) =>
other is Vampire && generation - 1 == (other as Vampire).generation;
other is Vampire && generation - 1 == other.generation;
}
void testCustomMap<K, V>(Map<K, V> typedMap) {
@ -1026,3 +1026,21 @@ void testTypeAnnotations(Map<int, int> map) {
Expect.equals(103, map.remove(0x20000000000000));
testLength(1, map);
}
void testUnmodifiableMaps() {
void checkUnmodifiable(Map<int, int> map) {
Expect.throws(() => map[0] = 0);
Expect.throws(() => map.addAll({0: 0}));
Expect.throws(() => map.addEntries({0: 0}.entries));
Expect.throws(() => map.clear());
Expect.throws(() => map.putIfAbsent(0, () => 0));
Expect.throws(() => map.remove(0));
Expect.throws(() => map.removeWhere((k, v) => true));
Expect.throws(() => map.update(0, (v) => v, ifAbsent: () => 0));
Expect.throws(() => map.updateAll((k, v) => v));
}
checkUnmodifiable(const {1: 1});
checkUnmodifiable(Map.unmodifiable({1: 1}));
checkUnmodifiable(UnmodifiableMapView({1: 1}));
}