Skip to content

Commit 02ba114

Browse files
committed
Prepare for change in handling of NoSuchMethod.
In a future release of Dart, if a class containing private members is *implemented* (rather than *extendeed*), any attempt to invoke one of the private members will result in a runtime error (see dart-lang/sdk#49687). This is necessary in order to soundly support field promotion (dart-lang/language#2020). The runtime error will most likely be a new exception type, not `NoSuchMethodError`. So, to avoid breaking the `SharedPreferencesStorePlatform.instance` setter, we need to generalize it to consider any exception thrown by `_verifyProvidesDefaultImplementations` as an indication that the `SharedPreferencesStorePlatform` has been illegally implemented. In the non-error case, `_verifyProvidesDefaultImplementations` does nothing, so this generalization should be safe.
1 parent 0e8d831 commit 02ba114

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/shared_preferences/shared_preferences_platform_interface/lib/shared_preferences_platform_interface.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ abstract class SharedPreferencesStorePlatform {
2727
if (!value.isMock) {
2828
try {
2929
value._verifyProvidesDefaultImplementations();
30-
} on NoSuchMethodError catch (_) {
30+
} catch (_) {
3131
throw AssertionError(
3232
'Platform interfaces must not be implemented with `implements`');
3333
}

packages/shared_preferences/shared_preferences_platform_interface/test/shared_preferences_platform_interface_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ void main() {
1717
throwsAssertionError,
1818
);
1919
});
20+
21+
test('disallows implementing interface, unusual exception', () {
22+
expect(
23+
() {
24+
SharedPreferencesStorePlatform.instance = IllegalImplementationWithUnusualException();
25+
},
26+
throwsAssertionError,
27+
);
28+
});
2029
});
2130
}
2231

@@ -46,3 +55,12 @@ class IllegalImplementation implements SharedPreferencesStorePlatform {
4655
throw UnimplementedError();
4756
}
4857
}
58+
59+
class IllegalImplementationWithUnusualException extends IllegalImplementation {
60+
@override
61+
dynamic noSuchMethod(Invocation invocation) {
62+
throw _UnusualException();
63+
}
64+
}
65+
66+
class _UnusualException {}

0 commit comments

Comments
 (0)