Skip to content

Commit 8e97111

Browse files
author
John Messerly
authored
Docs for no-implicit-casts and no-implicit-dynamic
As part of fixing #26782, I wanted to be able to link to some documentation for the Analyzer flags.
1 parent 409fbcf commit 8e97111

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

pkg/dev_compiler/doc/STATIC_SAFETY.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,105 @@ code where a field definition in a subclass shadows the field
416416
allocated. Users should prefer explicit getters and setters in such
417417
cases. See [issue 52](https://github.com/dart-lang/dev_compiler/issues/52).
418418

419+
## Optional Features
420+
421+
### Disable implicit casts
422+
423+
This is an optional feature of strong mode. It disables implicit down casts. For example:
424+
425+
```dart
426+
main() {
427+
num n = 0.5;
428+
int x = n; // error: invalid assignment
429+
int y = n as int; // ok at compile time, might fail when run
430+
}
431+
```
432+
433+
Casts from `dynamic` must be explicit as well:
434+
435+
```dart
436+
main() {
437+
dynamic d = 'hi';
438+
int x = d; // error: invalid assignment
439+
int y = d as int; // ok at compile time, might fail when run
440+
}
441+
```
442+
443+
Configure this for your project by editing .analysis_options:
444+
445+
```yaml
446+
analyzer:
447+
strong-mode:
448+
implicit-casts: False
449+
```
450+
451+
Or pass `--no-implicit-casts` to Dart Analyzer:
452+
453+
```
454+
dartanalyzer --strong --no-implicit-casts my_app.dart
455+
```
456+
457+
### Disable implicit dynamic
458+
459+
This is an optional feature of analyzer, intended primarily for use with strong mode's inference.
460+
It rejects implicit use of `dynamic`, requiring it to be written explicitly. For example:
461+
462+
```dart
463+
main() {
464+
var x; // error: implicit dynamic
465+
var i = 123; // okay, inferred to be `int x`
466+
dynamic y; // okay, declared as dynamic
467+
}
468+
```
469+
470+
This also affects: parameters, return types, fields, creating objects with generic type, generic functions/methods, and
471+
supertypes:
472+
473+
```dart
474+
// error: parameters and return types are implicit dynamic
475+
f(x) => x + 42;
476+
dynamic f(dynamic x) => x + 42; // okay
477+
int f(int x) => x + 42; // okay
478+
479+
class C {
480+
var f; // error: implicit dynamic field
481+
dynamic f; // okay
482+
}
483+
484+
main() {
485+
var x = []; // error: implicit List<dynamic>
486+
var y = [42]; // okay: List<int>
487+
var z = <dynamic>[]; // okay: List<dynamic>
488+
489+
T genericFn<T>() => null;
490+
genericFn(); // error: implicit genericFn<dynamic>
491+
genericFn<dynamic>(); // okay
492+
int x = genericFn(); // okay, inferred genericFn<int>
493+
}
494+
495+
// error: implicit supertype Iterable<dynamic>
496+
class C extends Iterable { /* ... */ }
497+
// okay
498+
class C extends Iterable<dynamic> { /* ... */ }
499+
```
500+
501+
It is designed to prevent accidental use of `dynamic` in code that does not intend to use it. It will lead to
502+
more verbose dynamic code, so it may not be advisable for your code depending on its goals.
503+
504+
Configure this for your project by editing .analysis_options:
505+
506+
```yaml
507+
analyzer:
508+
strong-mode:
509+
implicit-dynamic: False
510+
```
511+
512+
Or pass `--no-implicit-dynamic` to Dart Analyzer:
513+
514+
```
515+
dartanalyzer --strong --no-implicit-dynamic my_app.dart
516+
```
517+
419518
### Open Items
420519
421520
- Is / As restrictions: Dart's `is` and `as` checks are unsound for certain types

0 commit comments

Comments
 (0)