Version 2.10.0-80.0.dev
Merge commit 'f5a0e185d20b46dbfe112070bb2128a99e93a400' into 'dev'
diff --git a/pkg/analyzer/lib/error/error.dart b/pkg/analyzer/lib/error/error.dart
index aba9b3e..5d9f60a 100644
--- a/pkg/analyzer/lib/error/error.dart
+++ b/pkg/analyzer/lib/error/error.dart
@@ -465,6 +465,7 @@
FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_EXTENDS,
FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_IMPLEMENTS,
FfiCode.SUBTYPE_OF_STRUCT_CLASS_IN_WITH,
+ HintCode.ASSIGNMENT_OF_DO_NOT_STORE,
HintCode.CAN_BE_NULL_AFTER_NULL_AWARE,
HintCode.DEAD_CODE,
HintCode.DEAD_CODE_CATCH_FOLLOWING_CATCH,
diff --git a/pkg/analyzer/lib/src/dart/element/extensions.dart b/pkg/analyzer/lib/src/dart/element/extensions.dart
index ff2a475..bd4bd24 100644
--- a/pkg/analyzer/lib/src/dart/element/extensions.dart
+++ b/pkg/analyzer/lib/src/dart/element/extensions.dart
@@ -7,6 +7,26 @@
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/generated/utilities_dart.dart';
+extension ElementExtension on Element {
+ /// Return `true` if this element, the enclosing class (if there is one), or
+ /// the enclosing library, has been annotated with the `@doNotStore`
+ /// annotation.
+ bool get hasOrInheritsDoNotStore {
+ if (hasDoNotStore) {
+ return true;
+ }
+ var ancestor = enclosingElement;
+ if (ancestor is ClassElement || ancestor is ExtensionElement) {
+ if (ancestor.hasDoNotStore) {
+ return true;
+ }
+ ancestor = ancestor.enclosingElement;
+ }
+ return ancestor is CompilationUnitElement &&
+ ancestor.enclosingElement.hasDoNotStore;
+ }
+}
+
extension ParameterElementExtensions on ParameterElement {
/// Return [ParameterElement] with the specified properties replaced.
ParameterElement copyWith({DartType type, ParameterKind kind}) {
diff --git a/pkg/analyzer/lib/src/dart/error/hint_codes.dart b/pkg/analyzer/lib/src/dart/error/hint_codes.dart
index f413987..7aef562 100644
--- a/pkg/analyzer/lib/src/dart/error/hint_codes.dart
+++ b/pkg/analyzer/lib/src/dart/error/hint_codes.dart
@@ -15,6 +15,14 @@
*/
class HintCode extends AnalyzerErrorCode {
/**
+ * Users should not assign values marked `@doNotStore`.
+ */
+ static const HintCode ASSIGNMENT_OF_DO_NOT_STORE = HintCode(
+ 'ASSIGNMENT_OF_DO_NOT_STORE',
+ "'{0}' is marked 'doNotStore' and shouldn't be assigned to a field.",
+ correction: "Try removing the assignment.");
+
+ /**
* When the target expression uses '?.' operator, it can be `null`, so all the
* subsequent invocations should also use '?.' operator.
*/
diff --git a/pkg/analyzer/lib/src/error/best_practices_verifier.dart b/pkg/analyzer/lib/src/error/best_practices_verifier.dart
index aef2cc9..8cfc83d 100644
--- a/pkg/analyzer/lib/src/error/best_practices_verifier.dart
+++ b/pkg/analyzer/lib/src/error/best_practices_verifier.dart
@@ -14,6 +14,7 @@
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/element/element.dart';
+import 'package:analyzer/src/dart/element/extensions.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer/src/dart/element/member.dart' show ExecutableMember;
import 'package:analyzer/src/dart/element/type.dart';
@@ -359,6 +360,37 @@
field.name,
[field.name, overriddenElement.enclosingElement.name]);
}
+
+ var expression = field.initializer;
+
+ Element element;
+ if (expression is PropertyAccess) {
+ element = expression.propertyName.staticElement;
+ // Tear-off.
+ if (element is FunctionElement || element is MethodElement) {
+ element = null;
+ }
+ } else if (expression is MethodInvocation) {
+ element = expression.methodName.staticElement;
+ } else if (expression is Identifier) {
+ element = expression.staticElement;
+ // Tear-off.
+ if (element is FunctionElement || element is MethodElement) {
+ element = null;
+ }
+ }
+ if (element != null) {
+ if (element is PropertyAccessorElement && element.isSynthetic) {
+ element = (element as PropertyAccessorElement).variable;
+ }
+ if (element.hasOrInheritsDoNotStore) {
+ _errorReporter.reportErrorForNode(
+ HintCode.ASSIGNMENT_OF_DO_NOT_STORE,
+ expression,
+ [element.name],
+ );
+ }
+ }
}
} finally {
_inDeprecatedMember = wasInDeprecatedMember;
diff --git a/pkg/analyzer/lib/src/test_utilities/mock_packages.dart b/pkg/analyzer/lib/src/test_utilities/mock_packages.dart
index edc7bd4..2f054b5 100644
--- a/pkg/analyzer/lib/src/test_utilities/mock_packages.dart
+++ b/pkg/analyzer/lib/src/test_utilities/mock_packages.dart
@@ -25,6 +25,7 @@
library meta;
const _AlwaysThrows alwaysThrows = const _AlwaysThrows();
+const _DoNotStore doNotStore = _DoNotStore();
const _Factory factory = const _Factory();
const Immutable immutable = const Immutable();
const _Literal literal = const _Literal();
@@ -36,16 +37,19 @@
const _Sealed sealed = const _Sealed();
const _VisibleForTesting visibleForTesting = const _VisibleForTesting();
-class Immutable {
- final String reason;
- const Immutable([this.reason]);
-}
class _AlwaysThrows {
const _AlwaysThrows();
}
+class _DoNotStore {
+ const _DoNotStore();
+}
class _Factory {
const _Factory();
}
+class Immutable {
+ final String reason;
+ const Immutable([this.reason]);
+}
class _Literal {
const _Literal();
}
diff --git a/pkg/analyzer/test/src/diagnostics/assignment_of_do_not_store_test.dart b/pkg/analyzer/test/src/diagnostics/assignment_of_do_not_store_test.dart
new file mode 100644
index 0000000..ef2c8c2
--- /dev/null
+++ b/pkg/analyzer/test/src/diagnostics/assignment_of_do_not_store_test.dart
@@ -0,0 +1,179 @@
+// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import 'package:analyzer/src/error/codes.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+import '../dart/resolution/context_collection_resolution.dart';
+
+main() {
+ defineReflectiveSuite(() {
+ defineReflectiveTests(AssignmentOfDoNotStoreTest);
+ });
+}
+
+@reflectiveTest
+class AssignmentOfDoNotStoreTest extends PubPackageResolutionTest {
+ @override
+ void setUp() {
+ super.setUp();
+ writeTestPackageConfigWithMeta();
+ }
+
+ test_classMemberGetter() async {
+ await assertErrorsInCode('''
+import 'package:meta/meta.dart';
+
+class A {
+ @doNotStore
+ String get v => '';
+}
+
+class B {
+ String f = A().v;
+}
+''', [
+ error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 106, 5),
+ ]);
+ }
+
+ test_classMemberVariable() async {
+ await assertErrorsInCode('''
+import 'package:meta/meta.dart';
+
+class A{
+ @doNotStore
+ final f = '';
+}
+
+class B {
+ String f = A().f;
+}
+''', [
+ error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 99, 5),
+ ]);
+ }
+
+ test_classStaticGetter() async {
+ await assertErrorsInCode('''
+import 'package:meta/meta.dart';
+
+class A {
+ @doNotStore
+ static String get v => '';
+}
+
+class B {
+ String f = A.v;
+}
+''', [
+ error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 113, 3),
+ ]);
+ }
+
+ test_classStaticVariable() async {
+ await assertErrorsInCode('''
+import 'package:meta/meta.dart';
+
+class A{
+ @doNotStore
+ static final f = '';
+}
+
+class B {
+ String f = A.f;
+}
+''', [
+ error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 106, 3),
+ ]);
+ }
+
+ test_functionAssignment() async {
+ await assertNoErrorsInCode('''
+import 'package:meta/meta.dart';
+
+@doNotStore
+String g(int i) => '';
+
+class C {
+ String Function(int) f = g;
+}
+''');
+ }
+
+ test_functionReturnValue() async {
+ await assertErrorsInCode('''
+import 'package:meta/meta.dart';
+
+@doNotStore
+String getV() => '';
+
+class A {
+ final f = getV();
+}
+''', [
+ error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 90, 6),
+ ]);
+ }
+
+ test_methodReturnValue() async {
+ await assertErrorsInCode('''
+import 'package:meta/meta.dart';
+
+class A {
+ @doNotStore
+ String getV() => '';
+}
+
+class B {
+ final f = A().getV();
+}
+''', [
+ error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 106, 10),
+ ]);
+ }
+
+ test_tearOff() async {
+ await assertNoErrorsInCode('''
+import 'package:meta/meta.dart';
+
+@doNotStore
+String getV() => '';
+
+class A {
+ final f = getV;
+}
+''');
+ }
+
+ test_topLevelGetter() async {
+ await assertErrorsInCode('''
+import 'package:meta/meta.dart';
+
+@doNotStore
+String get v => '';
+
+class A {
+ final f = v;
+}
+''', [
+ error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 89, 1),
+ ]);
+ }
+
+ test_topLevelVariable() async {
+ await assertErrorsInCode('''
+import 'package:meta/meta.dart';
+
+@doNotStore
+final v = '';
+
+class A {
+ final f = v;
+}
+''', [
+ error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 83, 1),
+ ]);
+ }
+}
diff --git a/pkg/analyzer/test/src/diagnostics/test_all.dart b/pkg/analyzer/test/src/diagnostics/test_all.dart
index 0341386..e23b641 100644
--- a/pkg/analyzer/test/src/diagnostics/test_all.dart
+++ b/pkg/analyzer/test/src/diagnostics/test_all.dart
@@ -22,6 +22,7 @@
import 'argument_type_not_assignable_test.dart' as argument_type_not_assignable;
import 'assert_in_redirecting_constructor_test.dart'
as assert_in_redirecting_constructor;
+import 'assignment_of_do_not_store_test.dart' as assignment_of_do_not_store;
import 'assignment_to_const_test.dart' as assignment_to_const;
import 'assignment_to_final_local_test.dart' as assignment_to_final_local;
import 'assignment_to_final_no_setter_test.dart'
@@ -653,6 +654,7 @@
annotation_with_non_class.main();
argument_type_not_assignable.main();
assert_in_redirecting_constructor.main();
+ assignment_of_do_not_store.main();
assignment_to_const.main();
assignment_to_final_local.main();
assignment_to_final_no_setter.main();
diff --git a/pkg/dartdev/README.md b/pkg/dartdev/README.md
index 7b9d987..ef1c6c9 100644
--- a/pkg/dartdev/README.md
+++ b/pkg/dartdev/README.md
@@ -6,23 +6,23 @@
Usage: dart [<vm-flags>] <command|dart-file> [<arguments>]
Global options:
--h, --help Print this usage information.
--v, --verbose Show additional command output.
- --version Print the Dart SDK version.
- --enable-analytics Enable anonymous analytics.
- --disable-analytics Disable anonymous analytics.
- --enable-experiment=<experiment> Enable one or more experimental features (see dart.dev/go/experiments).
+-h, --help Print this usage information.
+-v, --verbose Show additional command output.
+ --version Print the Dart SDK version.
+ --enable-analytics Enable anonymous analytics.
+ --disable-analytics Disable anonymous analytics.
Available commands:
analyze Analyze the project's Dart code.
compile Compile Dart to various formats.
create Create a new project.
- format Idiomatically formats Dart source code.
+ format Idiomatically format Dart source code.
pub Work with packages.
run Run a Dart program.
test Run tests in this package.
Run "dart help <command>" for more information about a command.
+See https://dart.dev/tools/dart-tool for detailed documentation.
```
## Contributing
diff --git a/pkg/dartdev/lib/dartdev.dart b/pkg/dartdev/lib/dartdev.dart
index b63580a..1e0af75 100644
--- a/pkg/dartdev/lib/dartdev.dart
+++ b/pkg/dartdev/lib/dartdev.dart
@@ -206,10 +206,12 @@
// A hidden flag to disable analytics on this run, this constructor can be
// called with this flag, but should be removed before run() is called as
// the flag has not been added to all sub-commands.
- argParser.addFlag('disable-dartdev-analytics',
- negatable: false,
- help: 'Disable anonymous analytics for this `dart *` run',
- hide: true);
+ argParser.addFlag(
+ 'disable-dartdev-analytics',
+ negatable: false,
+ help: 'Disable anonymous analytics for this `dart *` run',
+ hide: true,
+ );
// Another hidden flag used by the VM to indicate that DDS should be
// launched. Should be removed for all commands other than `run`.
@@ -220,7 +222,7 @@
addCommand(CreateCommand(verbose: verbose));
addCommand(CompileCommand());
addCommand(FixCommand());
- addCommand(FormatCommand());
+ addCommand(FormatCommand(verbose: verbose));
addCommand(MigrateCommand(
verbose: verbose,
hidden: Runtime.runtime.stableChannel,
@@ -254,6 +256,7 @@
allowedHelp: verbose ? allowedHelp : null,
help: 'Enable one or more experimental features '
'(see dart.dev/go/experiments).',
+ hide: !verbose,
);
}
diff --git a/pkg/dartdev/lib/src/commands/compile.dart b/pkg/dartdev/lib/src/commands/compile.dart
index 669d4be..5da7168 100644
--- a/pkg/dartdev/lib/src/commands/compile.dart
+++ b/pkg/dartdev/lib/src/commands/compile.dart
@@ -175,7 +175,7 @@
)
..addMultiOption('define', abbr: 'D', valueHelp: 'key=value', help: '''
Define an environment declaration. To specify multiple declarations, use multiple options or use commas to separate key-value pairs.
-For example: dart compile $commandName -Da=1,b=2 main.dart.''')
+For example: dart compile $commandName -Da=1,b=2 main.dart''')
..addFlag('enable-asserts',
negatable: false, help: 'Enable assert statements.')
..addOption('packages',
@@ -184,7 +184,7 @@
help:
'''Get package locations from the specified file instead of .packages.
<path> can be relative or absolute.
-For example: dart compile $commandName --packages=/tmp/pkgs main.dart.''')
+For example: dart compile $commandName --packages=/tmp/pkgs main.dart''')
..addOption('save-debugging-info', abbr: 'S', valueHelp: 'path', help: '''
Remove debugging information from the output and save it separately to the specified file.
<path> can be relative or absolute.''');
diff --git a/pkg/dartdev/test/commands/format_test.dart b/pkg/dartdev/test/commands/format_test.dart
index 93d417d..cded95b 100644
--- a/pkg/dartdev/test/commands/format_test.dart
+++ b/pkg/dartdev/test/commands/format_test.dart
@@ -25,6 +25,22 @@
expect(result.stdout, contains('Idiomatically format Dart source code.'));
expect(result.stdout,
contains('Usage: dart format [options...] <files or directories...>'));
+
+ // Does not show verbose help.
+ expect(result.stdout.contains('--stdin-name'), isFalse);
+ });
+
+ test('--help --verbose', () {
+ p = project();
+ var result = p.runSync('format', ['--help', '--verbose']);
+ expect(result.exitCode, 0);
+ expect(result.stderr, isEmpty);
+ expect(result.stdout, contains('Idiomatically format Dart source code.'));
+ expect(result.stdout,
+ contains('Usage: dart format [options...] <files or directories...>'));
+
+ // Shows verbose help.
+ expect(result.stdout, contains('--stdin-name'));
});
test('unchanged', () {
diff --git a/tools/VERSION b/tools/VERSION
index 2492ead..10ccd8d 100644
--- a/tools/VERSION
+++ b/tools/VERSION
@@ -27,5 +27,5 @@
MAJOR 2
MINOR 10
PATCH 0
-PRERELEASE 79
+PRERELEASE 80
PRERELEASE_PATCH 0
\ No newline at end of file