Skip to content

Commit 9560a32

Browse files
author
Dart CI
committed
Version 2.11.0-186.0.dev
Merge commit '78e30b039e07fb0a16e5972caf6f5543fd13e9c8' into 'dev'
2 parents 1a039e5 + 78e30b0 commit 9560a32

File tree

145 files changed

+1334
-2353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+1334
-2353
lines changed

DEPS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ vars = {
4444
# co19 is a cipd package. Use update.sh in tests/co19[_2] to update these
4545
# hashes. It requires access to the dart-build-access group, which EngProd
4646
# has.
47-
"co19_rev": "53dd55a31d345320a6aa2b6eea0467f868b72377",
47+
"co19_rev": "01adc3471b81d3bceb9bbe775ed1262b1b89cc8a",
4848
"co19_2_rev": "e48b3090826cf40b8037648f19d211e8eab1b4b6",
4949

5050
# The internal benchmarks to use. See go/dart-benchmarks-internal
@@ -529,7 +529,7 @@ deps = {
529529
"packages": [
530530
{
531531
"package": "dart/cfe/dart2js_dills",
532-
"version": "binary_version:45",
532+
"version": "binary_version:46",
533533
}
534534
],
535535
"dep_type": "cipd",

pkg/analyzer/lib/error/error.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ const List<ErrorCode> errorCodeValues = [
260260
CompileTimeErrorCode.LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR,
261261
CompileTimeErrorCode.LATE_FINAL_LOCAL_ALREADY_ASSIGNED,
262262
CompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE,
263+
CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION,
263264
CompileTimeErrorCode.MAP_ENTRY_NOT_IN_MAP,
264265
CompileTimeErrorCode.MAP_KEY_TYPE_NOT_ASSIGNABLE,
265266
CompileTimeErrorCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE,

pkg/analyzer/lib/src/error/best_practices_verifier.dart

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -413,20 +413,14 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
413413
[field.name, overriddenElement.enclosingElement.name]);
414414
}
415415

416-
var expression = field.initializer;
417-
418-
var element = _getElement(expression);
419-
if (element != null) {
420-
if (element is PropertyAccessorElement && element.isSynthetic) {
421-
element = (element as PropertyAccessorElement).variable;
422-
}
423-
if (element.hasOrInheritsDoNotStore) {
424-
_errorReporter.reportErrorForNode(
425-
HintCode.ASSIGNMENT_OF_DO_NOT_STORE,
426-
expression,
427-
[element.name],
428-
);
429-
}
416+
var expressionMap =
417+
_getSubExpressionsMarkedDoNotStore(field.initializer);
418+
for (var entry in expressionMap.entries) {
419+
_errorReporter.reportErrorForNode(
420+
HintCode.ASSIGNMENT_OF_DO_NOT_STORE,
421+
entry.key,
422+
[entry.value.name],
423+
);
430424
}
431425
}
432426
} finally {
@@ -1382,15 +1376,17 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
13821376
if (_inDoNotStoreMember) {
13831377
return;
13841378
}
1385-
var element = _getElement(expression);
1386-
if (element != null && element.hasOrInheritsDoNotStore) {
1379+
var expressionMap = _getSubExpressionsMarkedDoNotStore(expression);
1380+
if (expressionMap.isNotEmpty) {
13871381
Declaration parent = expression.thisOrAncestorMatching(
13881382
(e) => e is FunctionDeclaration || e is MethodDeclaration);
1389-
_errorReporter.reportErrorForNode(
1390-
HintCode.RETURN_OF_DO_NOT_STORE,
1391-
expression,
1392-
[element.name, parent.declaredElement.displayName],
1393-
);
1383+
for (var entry in expressionMap.entries) {
1384+
_errorReporter.reportErrorForNode(
1385+
HintCode.RETURN_OF_DO_NOT_STORE,
1386+
entry.key,
1387+
[entry.value.name, parent.declaredElement.displayName],
1388+
);
1389+
}
13941390
}
13951391
}
13961392

@@ -1537,7 +1533,13 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
15371533
}
15381534
}
15391535

1540-
Element _getElement(Expression expression) {
1536+
/// Return subexpressions that are marked `@doNotStore`, as a map so that
1537+
/// corresponding elements can be used in the diagnostic message.
1538+
Map<Expression, Element> _getSubExpressionsMarkedDoNotStore(
1539+
Expression expression,
1540+
{Map<Expression, Element> addTo}) {
1541+
var expressions = addTo ?? <Expression, Element>{};
1542+
15411543
Element element;
15421544
if (expression is PropertyAccess) {
15431545
element = expression.propertyName.staticElement;
@@ -1553,13 +1555,26 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
15531555
if (element is FunctionElement || element is MethodElement) {
15541556
element = null;
15551557
}
1558+
} else if (expression is ConditionalExpression) {
1559+
_getSubExpressionsMarkedDoNotStore(expression.elseExpression,
1560+
addTo: expressions);
1561+
_getSubExpressionsMarkedDoNotStore(expression.thenExpression,
1562+
addTo: expressions);
1563+
} else if (expression is BinaryExpression) {
1564+
_getSubExpressionsMarkedDoNotStore(expression.leftOperand,
1565+
addTo: expressions);
1566+
_getSubExpressionsMarkedDoNotStore(expression.rightOperand,
1567+
addTo: expressions);
15561568
}
1557-
if (element != null) {
1558-
if (element is PropertyAccessorElement && element.isSynthetic) {
1559-
element = (element as PropertyAccessorElement).variable;
1560-
}
1569+
if (element is PropertyAccessorElement && element.isSynthetic) {
1570+
element = (element as PropertyAccessorElement).variable;
15611571
}
1562-
return element;
1572+
1573+
if (element != null && element.hasOrInheritsDoNotStore) {
1574+
expressions[expression] = element;
1575+
}
1576+
1577+
return expressions;
15631578
}
15641579

15651580
bool _isLibraryInWorkspacePackage(LibraryElement library) {

pkg/analyzer/lib/src/error/codes.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5460,6 +5460,12 @@ class CompileTimeErrorCode extends AnalyzerErrorCode {
54605460
"The element type '{0}' can't be assigned to the list type '{1}'.",
54615461
hasPublishedDocs: true);
54625462

5463+
static const CompileTimeErrorCode MAIN_IS_NOT_FUNCTION = CompileTimeErrorCode(
5464+
'MAIN_IS_NOT_FUNCTION',
5465+
"The declaration named 'main' must be a function.",
5466+
correction: "Try using a different name for this declaration.",
5467+
);
5468+
54635469
/**
54645470
* No parameters.
54655471
*/

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
449449
_checkForFinalNotInitializedInClass(members);
450450
_checkForBadFunctionUse(node);
451451
_checkForWrongTypeParameterVarianceInSuperinterfaces();
452+
_checkForMainFunction(node.name);
452453
super.visitClassDeclaration(node);
453454
} finally {
454455
_isInNativeClass = false;
@@ -466,6 +467,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
466467
_enclosingClass = node.declaredElement;
467468
_checkClassInheritance(
468469
node, node.superclass, node.withClause, node.implementsClause);
470+
_checkForMainFunction(node.name);
469471
_checkForWrongTypeParameterVarianceInSuperinterfaces();
470472
} finally {
471473
_enclosingClass = outerClassElement;
@@ -724,6 +726,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
724726
_checkForTypeAnnotationDeferredClass(returnType);
725727
_returnTypeVerifier.verifyReturnType(returnType);
726728
_checkForImplicitDynamicReturn(node.name, node.declaredElement);
729+
_checkForMainFunction(node.name);
727730
super.visitFunctionDeclaration(node);
728731
});
729732
}
@@ -770,6 +773,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
770773
void visitFunctionTypeAlias(FunctionTypeAlias node) {
771774
_checkForBuiltInIdentifierAsName(
772775
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME);
776+
_checkForMainFunction(node.name);
773777
_checkForTypeAliasCannotReferenceItself(node, node.declaredElement);
774778
super.visitFunctionTypeAlias(node);
775779
}
@@ -805,6 +809,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
805809
void visitGenericTypeAlias(GenericTypeAlias node) {
806810
_checkForBuiltInIdentifierAsName(
807811
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME);
812+
_checkForMainFunction(node.name);
808813
_checkForTypeAliasCannotReferenceItself(node, node.declaredElement);
809814
super.visitGenericTypeAlias(node);
810815
}
@@ -970,6 +975,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
970975

971976
_checkForConflictingClassMembers();
972977
_checkForFinalNotInitializedInClass(members);
978+
_checkForMainFunction(node.name);
973979
_checkForWrongTypeParameterVarianceInSuperinterfaces();
974980
// _checkForBadFunctionUse(node);
975981
super.visitMixinDeclaration(node);
@@ -1193,6 +1199,11 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
11931199
void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
11941200
_checkForFinalNotInitialized(node.variables);
11951201
_checkForNotInitializedNonNullableVariable(node.variables);
1202+
1203+
for (var declaration in node.variables.variables) {
1204+
_checkForMainFunction(declaration.name);
1205+
}
1206+
11961207
super.visitTopLevelVariableDeclaration(node);
11971208
}
11981209

@@ -3094,6 +3105,31 @@ class ErrorVerifier extends RecursiveAstVisitor<void> {
30943105
}
30953106
}
30963107

3108+
void _checkForMainFunction(SimpleIdentifier nameNode) {
3109+
if (!_currentLibrary.isNonNullableByDefault) {
3110+
return;
3111+
}
3112+
3113+
var element = nameNode.staticElement;
3114+
3115+
// We should only check exported declarations, i.e. top-level.
3116+
if (element.enclosingElement is! CompilationUnitElement) {
3117+
return;
3118+
}
3119+
3120+
if (element.displayName != 'main') {
3121+
return;
3122+
}
3123+
3124+
if (element is! FunctionElement) {
3125+
_errorReporter.reportErrorForNode(
3126+
CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION,
3127+
nameNode,
3128+
);
3129+
return;
3130+
}
3131+
}
3132+
30973133
void _checkForMapTypeNotAssignable(SetOrMapLiteral literal) {
30983134
// Determine the map's key and value types. We base this on the static type
30993135
// and not the literal's type arguments because in strong mode, the type

pkg/analyzer/test/src/diagnostics/assignment_of_do_not_store_test.dart

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class B {
3434
String f = A().v;
3535
}
3636
''', [
37-
error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 106, 5),
37+
error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 106, 5,
38+
messageContains: "'v'"),
3839
]);
3940
}
4041

@@ -176,4 +177,59 @@ class A {
176177
error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 83, 1),
177178
]);
178179
}
180+
181+
test_topLevelVariable_binaryExpression() async {
182+
await assertErrorsInCode('''
183+
import 'package:meta/meta.dart';
184+
185+
@doNotStore
186+
final v = '';
187+
188+
class A {
189+
final f = v ?? v;
190+
}
191+
''', [
192+
error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 83, 1),
193+
error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 88, 1),
194+
]);
195+
}
196+
197+
test_topLevelVariable_libraryAnnotation() async {
198+
testFilePath;
199+
newFile('$testPackageLibPath/library.dart', content: '''
200+
@doNotStore
201+
library lib;
202+
203+
import 'package:meta/meta.dart';
204+
205+
final v = '';
206+
''');
207+
208+
await assertErrorsInCode('''
209+
import 'library.dart';
210+
211+
class A {
212+
final f = v;
213+
}
214+
''', [
215+
error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 46, 1),
216+
]);
217+
}
218+
219+
test_topLevelVariable_ternary() async {
220+
await assertErrorsInCode('''
221+
import 'package:meta/meta.dart';
222+
223+
@doNotStore
224+
final v = '';
225+
226+
class A {
227+
static bool c;
228+
final f = c ? v : v;
229+
}
230+
''', [
231+
error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 104, 1),
232+
error(HintCode.ASSIGNMENT_OF_DO_NOT_STORE, 108, 1),
233+
]);
234+
}
179235
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/context_collection_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(MainIsNotFunctionTest);
13+
defineReflectiveTests(MainIsNotFunctionWithNullSafetyTest);
14+
});
15+
}
16+
17+
@reflectiveTest
18+
class MainIsNotFunctionTest extends PubPackageResolutionTest
19+
with MainIsNotFunctionTestCases {}
20+
21+
mixin MainIsNotFunctionTestCases on PubPackageResolutionTest {
22+
test_class() async {
23+
await resolveTestCode('''
24+
class main {}
25+
''');
26+
assertErrorsInResult(expectedErrorsByNullability(nullable: [
27+
error(CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION, 6, 4),
28+
], legacy: []));
29+
}
30+
31+
test_classAlias() async {
32+
await resolveTestCode('''
33+
class A {}
34+
mixin M {}
35+
class main = A with M;
36+
''');
37+
assertErrorsInResult(expectedErrorsByNullability(nullable: [
38+
error(CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION, 28, 4),
39+
], legacy: []));
40+
}
41+
42+
test_function() async {
43+
await assertNoErrorsInCode('''
44+
void main() {}
45+
''');
46+
}
47+
48+
test_getter() async {
49+
await resolveTestCode('''
50+
int get main => 0;
51+
''');
52+
assertErrorsInResult(expectedErrorsByNullability(nullable: [
53+
error(CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION, 8, 4),
54+
], legacy: []));
55+
}
56+
57+
test_mixin() async {
58+
await resolveTestCode('''
59+
class A {}
60+
mixin main on A {}
61+
''');
62+
assertErrorsInResult(expectedErrorsByNullability(nullable: [
63+
error(CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION, 17, 4),
64+
], legacy: []));
65+
}
66+
67+
test_typedef() async {
68+
await resolveTestCode('''
69+
typedef main = void Function();
70+
''');
71+
assertErrorsInResult(expectedErrorsByNullability(nullable: [
72+
error(CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION, 8, 4),
73+
], legacy: []));
74+
}
75+
76+
test_typedef_legacy() async {
77+
await resolveTestCode('''
78+
typedef void main();
79+
''');
80+
assertErrorsInResult(expectedErrorsByNullability(nullable: [
81+
error(CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION, 13, 4),
82+
], legacy: []));
83+
}
84+
85+
test_variable() async {
86+
await resolveTestCode('''
87+
var main = 0;
88+
''');
89+
assertErrorsInResult(expectedErrorsByNullability(nullable: [
90+
error(CompileTimeErrorCode.MAIN_IS_NOT_FUNCTION, 4, 4),
91+
], legacy: []));
92+
}
93+
}
94+
95+
@reflectiveTest
96+
class MainIsNotFunctionWithNullSafetyTest extends PubPackageResolutionTest
97+
with WithNullSafetyMixin, MainIsNotFunctionTestCases {}

0 commit comments

Comments
 (0)