diff --git a/pkg/analyzer/test/dart/element/fragment_offset_test.dart b/pkg/analyzer/test/dart/element/fragment_offset_test.dart index ebefa4b0e6c..cd115078d63 100644 --- a/pkg/analyzer/test/dart/element/fragment_offset_test.dart +++ b/pkg/analyzer/test/dart/element/fragment_offset_test.dart @@ -4,15 +4,16 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/element/element.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../../src/dart/resolution/context_collection_resolution.dart'; +import '../../src/dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(FragmentOffsetTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -84,13 +85,12 @@ class C = Object with M; } test_classFragment_classTypeAlias_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin M {} class = Object with M; -''', - [error(diag.missingIdentifier, 17, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var classTypeAlias = findNode.classTypeAlias('Object with M'); checkOffsetInRange( classTypeAlias, @@ -99,14 +99,13 @@ class = Object with M; } test_classFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library; // Ensures that the class declaration isn't at offset 0 class {} -''', - [error(diag.missingIdentifier, 72, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var classDeclaration = findNode.classDeclaration('class {}'); checkOffsetInRange( classDeclaration, @@ -131,14 +130,13 @@ class C {} } test_constructorFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { C.(); +// ^ +// [diag.missingIdentifier] Expected an identifier. } -''', - [error(diag.missingIdentifier, 14, 1)], - ); +'''); var constructorDeclaration = findNode.constructor('C.()'); checkOffsetInRange( constructorDeclaration, @@ -199,14 +197,13 @@ enum E { e1 } } test_enumFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library; // Ensures that the enum declaration isn't at offset 0 enum { e1 } -''', - [error(diag.missingIdentifier, 70, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var enumDeclaration = findNode.enumDeclaration('enum { e1 }'); checkOffsetInRange( enumDeclaration, @@ -254,14 +251,13 @@ extension type E(int i) {} } test_extensionTypeFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library; // Ensures that the extension type declaration isn't at offset 0 extension type(int i) {} -''', - [error(diag.missingIdentifier, 89, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var extensionTypeDeclaration = findNode.extensionTypeDeclaration( 'extension type(int i)', ); @@ -287,18 +283,16 @@ class C { } test_fieldFormalParameterFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { int? x; C(this.); +// ^^^^^ +// [diag.initializingFormalForNonExistentField] '' isn't a field in the enclosing class. +// ^ +// [diag.missingIdentifier] Expected an identifier. } -''', - [ - error(diag.initializingFormalForNonExistentField, 24, 5), - error(diag.missingIdentifier, 29, 1), - ], - ); +'''); var parameter = findNode.fieldFormalParameter('this.'); checkOffsetInRange( parameter, @@ -388,12 +382,11 @@ void f(int x) {} } test_formalParameterFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f((int x)) {} -''', - [error(diag.missingIdentifier, 7, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var function = findNode.functionDeclaration('f('); var parameter = function.functionExpression.parameters!.parameters[0] @@ -406,12 +399,11 @@ void f((int x)) {} } test_formalParameterFragment_missingName2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(void (int x)) {} -''', - [error(diag.missingIdentifier, 12, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var function = findNode.functionDeclaration('f('); var parameter = function.functionExpression.parameters!.parameters[0] @@ -745,14 +737,13 @@ mixin M {} } test_mixinFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library; // Ensures that the mixin declaration isn't at offset 0 mixin {} -''', - [error(diag.missingIdentifier, 72, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var mixinDeclaration = findNode.mixinDeclaration('mixin {}'); checkOffsetInRange( mixinDeclaration, @@ -801,13 +792,12 @@ import 'dart:math' as a; // second } test_prefixFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // ignore: unused_import import 'dart:async' as; -''', - [error(diag.missingIdentifier, 47, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var importDirective = findNode.import('as;'); checkOffsetInRange( importDirective, @@ -895,18 +885,17 @@ class C extends B { } test_superFormalParameterFragment_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class B { B([int? i]); } class C extends B { C(super.); +// ^ +// [diag.missingIdentifier] Expected an identifier. } -''', - [error(diag.missingIdentifier, 58, 1)], - ); +'''); var parameter = findNode.superFormalParameter('super.'); checkOffsetInRange( parameter, @@ -987,14 +976,14 @@ typedef void F(); } test_typeAliasFragment_functionTypeAlias_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library; // Ensures that the function type alias declaration isn't at offset 0 +// [diag.unusedElement][column 1][length 0] The declaration '' isn't referenced. typedef void(); -''', - [error(diag.unusedElement, 0, 0), error(diag.missingIdentifier, 92, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var functionTypeAlias = findNode.functionTypeAlias('void()'); checkOffsetInRange( functionTypeAlias, @@ -1015,14 +1004,14 @@ typedef T = int; } test_typeAliasFragment_genericTypeAlias_missingName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library; // Ensures that the generic type alias declaration isn't at offset 0 +// [diag.unusedElement][column 1][length 0] The declaration '' isn't referenced. typedef = int; -''', - [error(diag.unusedElement, 0, 0), error(diag.missingIdentifier, 87, 1)], - ); +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var genericTypeAlias = findNode.genericTypeAlias('= int'); checkOffsetInRange( genericTypeAlias, diff --git a/pkg/analyzer/test/generated/non_error_resolver_test.dart b/pkg/analyzer/test/generated/non_error_resolver_test.dart index 8e3b8d8b46f..66d0e304e65 100644 --- a/pkg/analyzer/test/generated/non_error_resolver_test.dart +++ b/pkg/analyzer/test/generated/non_error_resolver_test.dart @@ -10,10 +10,12 @@ import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/context_collection_resolution.dart'; +import '../src/dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(NonErrorResolverTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -76,14 +78,13 @@ export 'lib2.dart' show C; library lib; class N {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library L; export 'lib.dart'; export 'lib.dart'; -''', - [error(diag.duplicateExport, 37, 10)], - ); +// ^^^^^^^^^^ +// [diag.duplicateExport] Duplicate export. +'''); } test_ambiguousImport_dart_implicitHide() async { @@ -139,17 +140,16 @@ library lib2; class N {} class N2 {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'lib1.dart'; import 'lib2.dart' show N, N2; +// ^ +// [diag.unusedShownName] The name N is shown, but isn't used. main() { new N1(); new N2(); } -''', - [error(diag.unusedShownName, 44, 1)], - ); +'''); } test_annotated_partOfDeclaration() async { @@ -342,15 +342,14 @@ main() { } test_assignmentToFinal_prefixNegate() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f() { final x = 0; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. -x; } -''', - [error(diag.unusedLocalVariable, 14, 1)], - ); +'''); } test_assignmentToFinalNoSetter_prefixedIdentifier() async { @@ -428,15 +427,14 @@ dynamic f() async {} } test_async_expression_function_type() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' typedef Future F(int i); main() { F f = (int i) async => i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'f' isn't used. } -''', - [error(diag.unusedLocalVariable, 43, 1)], - ); +'''); } test_async_flattened() async { @@ -556,27 +554,25 @@ f() async {} } test_asyncForInWrongContext_async() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(list) async { await for (var e in list) { +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'e' isn't used. } } -''', - [error(diag.unusedLocalVariable, 33, 1)], - ); +'''); } test_asyncForInWrongContext_asyncStar() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(list) async* { await for (var e in list) { +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'e' isn't used. } } -''', - [error(diag.unusedLocalVariable, 34, 1)], - ); +'''); } test_await_flattened() async { @@ -663,14 +659,13 @@ typedef Foo(param); } test_builtInIdentifierAsType_dynamic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f() { dynamic x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [error(diag.unusedLocalVariable, 16, 1)], - ); +'''); } test_castFrom() async { @@ -678,18 +673,17 @@ f() { // substitution in the `newSet` parameter of `Set.castFrom`, we wind up with // a synthetic `ParameterMember` that belongs to no library. We need to // make sure this doesn't lead to a crash. - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C {} void testNewSet(Set setEls) { var customNewSet; +// ^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'customNewSet' isn't used. Set.castFrom(setEls, newSet: () => customNewSet = new Set()); } -''', - [error(diag.unusedLocalVariable, 51, 12)], - ); +'''); } test_class_type_alias_documentationComment() async { @@ -802,17 +796,16 @@ class C extends A { } test_constConstructorWithNonConstSuper_unresolved() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.a(); } class B extends A { const B(): super(); +// ^^^^^^^ +// [diag.undefinedConstructorInInitializerDefault] The class 'A' doesn't have an unnamed constructor. } -''', - [error(diag.undefinedConstructorInInitializerDefault, 54, 7)], - ); +'''); } test_constConstructorWithNonFinalField_finalInstanceVar() async { @@ -1081,14 +1074,13 @@ A a = new A(); } test_dynamicIdentifier() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = dynamic; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); } test_empty_generator_async() async { @@ -1516,32 +1508,31 @@ test() { test_importDuplicatedLibraryName() async { newFile("$testPackageLibPath/lib.dart", "library lib;"); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library test; import 'lib.dart'; +// ^^^^^^^^^^ +// [diag.unusedImport] Unused import: 'lib.dart'. import 'lib.dart'; -''', - [ - error(diag.unusedImport, 21, 10), - error(diag.unusedImport, 40, 10), - error(diag.duplicateImport, 40, 10), - ], - ); +// ^^^^^^^^^^ +// [diag.duplicateImport] Duplicate import. +// [diag.unusedImport] Unused import: 'lib.dart'. +'''); } test_importDuplicatedLibraryUnnamed() async { newFile("$testPackageLibPath/lib1.dart", ''); newFile("$testPackageLibPath/lib2.dart", ''); // No warning on duplicate import (https://github.com/dart-lang/sdk/issues/24156) - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library test; import 'lib1.dart'; +// ^^^^^^^^^^^ +// [diag.unusedImport] Unused import: 'lib1.dart'. import 'lib2.dart'; -''', - [error(diag.unusedImport, 21, 11), error(diag.unusedImport, 41, 11)], - ); +// ^^^^^^^^^^^ +// [diag.unusedImport] Unused import: 'lib2.dart'. +'''); } test_importOfNonLibrary_libraryDeclared() async { @@ -1724,15 +1715,14 @@ class A { static var _m; } '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'lib.dart'; class B extends A { _m() {} +//^^ +// [diag.unusedElement] The declaration '_m' isn't referenced. } -''', - [error(diag.unusedElement, 41, 2)], - ); +'''); } test_instanceMethodNameCollidesWithSuperclassStatic_method() async { @@ -1742,15 +1732,14 @@ class A { static _m() {} } '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'lib.dart'; class B extends A { _m() {} +//^^ +// [diag.unusedElement] The declaration '_m' isn't referenced. } -''', - [error(diag.unusedElement, 41, 2)], - ); +'''); } test_instantiateGenericFunctionWithNamedParameterAsGenericArg() async { @@ -1892,22 +1881,21 @@ main() { } test_invalidIdentifierInAsync() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { m() { int async; +// ^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'async' isn't used. int await; +// ^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'await' isn't used. int yield; +// ^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'yield' isn't used. } } -''', - [ - error(diag.unusedLocalVariable, 26, 5), - error(diag.unusedLocalVariable, 41, 5), - error(diag.unusedLocalVariable, 56, 5), - ], - ); +'''); } test_invalidMethodOverrideNamedParamType() async { @@ -2091,21 +2079,19 @@ f(S s) async { } test_issue_32394() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' var x = y.map((a) => a.toString()); var y = [3]; var z = x.toList(); void main() { String p = z; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'p' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'String'. } -''', - [ - error(diag.unusedLocalVariable, 93, 1), - error(diag.invalidAssignment, 97, 1), - ], - ); +'''); var z = result.unit.declaredFragment!.element.topLevelVariables .where((e) => e.name == 'z') .single; @@ -2153,16 +2139,15 @@ int f(v) { } test_librarySource_of_type_substituted_synthetic_parameter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Map f(T t) => throw ''; Map g(T t) => throw ''; h(bool b) { Map m = (b ? f : g)('x'); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'm' isn't used. } -''', - [error(diag.unusedLocalVariable, 104, 1)], - ); +'''); var parameter = findNode.stringLiteral("'x'").correspondingParameter; expect(parameter!.library, isNull); expect(parameter.library?.firstFragment.source, isNull); @@ -2408,18 +2393,17 @@ f(bool pb, pd) { } test_nonBoolNegationExpression_dynamic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f1(bool dynamic) { !dynamic; } f2() { bool dynamic = true; +// ^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'dynamic' isn't used. !dynamic; } -''', - [error(diag.unusedLocalVariable, 47, 7)], - ); +'''); } test_nonBoolOperand_and_bool() async { @@ -2545,14 +2529,13 @@ f() { } test_nonConstMapAsExpressionStatement_notExpressionStatement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f() { var m = {'a' : 0, 'b' : 1}; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'm' isn't used. } -''', - [error(diag.unusedLocalVariable, 12, 1)], - ); +'''); } test_nonConstMapAsExpressionStatement_typeArguments() async { @@ -2572,18 +2555,17 @@ main() { } test_nonConstValueInInitializer_binary_bool() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { final v; const A.a1(bool p) : v = p && true; const A.a2(bool p) : v = true && p; const A.b1(bool p) : v = p || true; const A.b2(bool p) : v = true || p; +// ^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.deadCode, 167, 4)], - ); +'''); } test_nonConstValueInInitializer_binary_dynamic() async { @@ -2823,17 +2805,16 @@ main() {} test_parameterScope_local() async { // Parameter names shouldn't conflict with the name of the function they // are enclosed in. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f() { g(g) { +//^ +// [diag.unusedElement] The declaration 'g' isn't referenced. h(g); } } h(x) {} -''', - [error(diag.unusedElement, 8, 1)], - ); +'''); } test_parameterScope_method() async { @@ -2896,45 +2877,42 @@ class B extends A { } test_referenceToDeclaredVariableInInitializer_constructorName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A.x() {} } f() { var x = new A.x(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 1)], - ); +'''); } test_referenceToDeclaredVariableInInitializer_methodName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { x() {} } f(A a) { var x = a.x(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [error(diag.unusedLocalVariable, 36, 1)], - ); +'''); } test_referenceToDeclaredVariableInInitializer_propertyName() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { var x; } f(A a) { var x = a.x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [error(diag.unusedLocalVariable, 36, 1)], - ); +'''); } test_regress34906() async { @@ -3141,19 +3119,18 @@ void f(A p) { } test_typePromotion_if_inClosure_assignedAfter_inSameFunction() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { f(Object p) { +//^ +// [diag.unusedElement] The declaration 'f' isn't referenced. if (p is String) { p.length; } p = 0; }; } -''', - [error(diag.unusedElement, 11, 1)], - ); +'''); } test_typePromotion_if_is_and_left() async { diff --git a/pkg/analyzer/test/generated/non_hint_code_test.dart b/pkg/analyzer/test/generated/non_hint_code_test.dart index 936d982b611..779877b0e5b 100644 --- a/pkg/analyzer/test/generated/non_hint_code_test.dart +++ b/pkg/analyzer/test/generated/non_hint_code_test.dart @@ -6,11 +6,13 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/context_collection_resolution.dart'; +import '../src/dart/resolution/node_text_expectations.dart'; import 'test_support.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(NonHintCodeTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -18,23 +20,21 @@ main() { class NonHintCodeTest extends PubPackageResolutionTest { test_issue20904BuggyTypePromotionAtIfJoin_1() async { // https://code.google.com/p/dart/issues/detail?id=20904 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(message, dynamic_) { if (message is Function) { message = dynamic_; } int s = message; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. } -''', - [error(diag.unusedLocalVariable, 86, 1)], - ); +'''); } test_issue20904BuggyTypePromotionAtIfJoin_3() async { // https://code.google.com/p/dart/issues/detail?id=20904 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(message) { var dynamic_; if (message is Function) { @@ -43,16 +43,15 @@ f(message) { return; } int s = message; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. } -''', - [error(diag.unusedLocalVariable, 115, 1)], - ); +'''); } test_issue20904BuggyTypePromotionAtIfJoin_4() async { // https://code.google.com/p/dart/issues/detail?id=20904 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(message) { if (message is Function) { message = ''; @@ -60,10 +59,10 @@ f(message) { return; } String s = message; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. } -''', - [error(diag.unusedLocalVariable, 96, 1)], - ); +'''); } test_propagatedFieldType() async { diff --git a/pkg/analyzer/test/generated/resolver_test.dart b/pkg/analyzer/test/generated/resolver_test.dart index 21f859931ac..fb1afef6eee 100644 --- a/pkg/analyzer/test/generated/resolver_test.dart +++ b/pkg/analyzer/test/generated/resolver_test.dart @@ -2,15 +2,16 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/context_collection_resolution.dart'; +import '../src/dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(StrictModeTest); defineReflectiveTests(TypePropagationTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -19,14 +20,13 @@ main() { @reflectiveTest class StrictModeTest extends PubPackageResolutionTest { test_assert_is() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int f(num n) { assert (n is int); return n & 0x0F; -}''', - [error(diag.undefinedOperator, 47, 1)], - ); +// ^ +// [diag.undefinedOperator] The operator '&' isn't defined for the type 'num'. +}'''); } test_conditional_and_is() async { @@ -71,17 +71,16 @@ void f(List list) { } test_forEach() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(List list) { num sum = 0; // ignore: unused_local_variable for (num n in list) { sum += n & 0x0F; +// ^ +// [diag.undefinedOperator] The operator '&' isn't defined for the type 'num'. } } -''', - [error(diag.undefinedOperator, 110, 1)], - ); +'''); } test_if_and_is() async { @@ -140,14 +139,13 @@ int f(num n) { } test_localVar() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int f() { num n = 1234; return n & 0x0F; -}''', - [error(diag.undefinedOperator, 37, 1)], - ); +// ^ +// [diag.undefinedOperator] The operator '&' isn't defined for the type 'num'. +}'''); } } diff --git a/pkg/analyzer/test/generated/simple_resolver_test.dart b/pkg/analyzer/test/generated/simple_resolver_test.dart index acf22e0845f..87de3c9af4b 100644 --- a/pkg/analyzer/test/generated/simple_resolver_test.dart +++ b/pkg/analyzer/test/generated/simple_resolver_test.dart @@ -4,15 +4,16 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/element/element.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/context_collection_resolution.dart'; +import '../src/dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SimpleResolverTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -522,19 +523,17 @@ SimpleIdentifier } test_forEachLoops_nonConflicting() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f() { List list = [1,2,3]; for (int x in list) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. for (int x in list) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 40, 1), - error(diag.unusedLocalVariable, 65, 1), - ], - ); +'''); } test_forLoops_nonConflicting() async { @@ -585,8 +584,7 @@ SimpleIdentifier } test_getter_fromMixins_property_access() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class B {} mixin M1 { get x => null; @@ -597,10 +595,10 @@ mixin M2 { class C extends B with M1, M2 {} void main() { var y = new C().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [error(diag.unusedLocalVariable, 124, 1)], - ); +'''); // Verify that the getter for "x" in "new C().x" refers to the getter // defined in M2. @@ -649,9 +647,10 @@ main() { // single error generated when the only problem is that an imported file // does not exist. // - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'missing.dart' as p; +// ^^^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'missing.dart'. int a = p.q + p.r.s; String b = p.t(a) + p.u(v: 0); p.T c = new p.T(); @@ -666,9 +665,7 @@ class G extends Object with p.V {} class H extends D { H(int i) : super(i); } -''', - [error(diag.uriDoesNotExist, 7, 14)], - ); +'''); } test_import_show_doesNotExist() async { @@ -677,9 +674,10 @@ class H extends D { // single error generated when the only problem is that an imported file // does not exist. // - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'missing.dart' show q, r, t, u, T, U, V, W; +// ^^^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'missing.dart'. int a = q + r.s; String b = t(a) + u(v: 0); T c = new T(); @@ -694,9 +692,7 @@ class G extends Object with V {} class H extends D { H(int i) : super(i); } -''', - [error(diag.uriDoesNotExist, 7, 14)], - ); +'''); } test_import_spaceInUri() async { @@ -724,14 +720,13 @@ f() { } test_indexExpression_typeParameters_invalidAssignmentWarning() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f() { List> b = []; b[0][0] = 'hi'; -}''', - [error(diag.invalidAssignment, 44, 4)], - ); +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. +}'''); } test_indirectOperatorThroughCall() async { @@ -764,12 +759,13 @@ class A { } test_isValidMixin_badSuperclass() async { - await assertErrorsInCode( + await resolveTestCodeWithDiagnostics( r''' class A extends B {} class B {} -class C = Object with A;''', - [error(diag.classUsedAsMixin, 54, 1)], +class C = Object with A; +// ^ +// [diag.classUsedAsMixin] The class 'A' can't be used as a mixin because it's neither a mixin class nor a mixin.''', ); var a = findElement2.class_('A'); @@ -777,13 +773,14 @@ class C = Object with A;''', } test_isValidMixin_constructor() async { - await assertErrorsInCode( + await resolveTestCodeWithDiagnostics( r''' class A { A() {} } -class C = Object with A;''', - [error(diag.classUsedAsMixin, 43, 1)], +class C = Object with A; +// ^ +// [diag.classUsedAsMixin] The class 'A' can't be used as a mixin because it's neither a mixin class nor a mixin.''', ); var a = findElement2.class_('A'); diff --git a/pkg/analyzer/test/generated/static_type_analyzer_test.dart b/pkg/analyzer/test/generated/static_type_analyzer_test.dart index 9c5dd9338b1..1352a838c5c 100644 --- a/pkg/analyzer/test/generated/static_type_analyzer_test.dart +++ b/pkg/analyzer/test/generated/static_type_analyzer_test.dart @@ -4,15 +4,16 @@ import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/src/dart/element/type.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/context_collection_resolution.dart'; +import '../src/dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(StaticTypeAnalyzerTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -43,18 +44,16 @@ late Derived> derivedDerivedInt; } test_flatten_inhibit_recursion() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, A. class B extends A {} +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, A. late A a; late B b; -''', - [ - error(diag.recursiveInterfaceInheritance, 6, 1), - error(diag.recursiveInterfaceInheritance, 27, 1), - ], - ); +'''); var aType = findElement2.topVar('a').type; var bType = findElement2.topVar('b').type; // flatten(A) = A and flatten(B) = B, since neither class contains Future @@ -65,22 +64,21 @@ late B b; } test_flatten_related_derived_types() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' abstract class Derived implements Future {} abstract class A extends Derived implements Derived {} +// ^ +// [diag.conflictingGenericInterfaces] The class 'A' can't implement both 'Derived' and 'Derived' because the type arguments are different. +// [diag.conflictingGenericInterfaces] The class 'A' can't implement both 'Future' and 'Future' because the type arguments are different. +// ^^^^^^^^^^^^ +// [diag.implementsSuperClass] 'abstract class Derived implements Future' can't be used in both the 'extends' and 'implements' clauses. abstract class B1 implements Future {} abstract class B2 extends B1 implements Future {} +// ^^ +// [diag.conflictingGenericInterfaces] The class 'B2' can't implement both 'Future' and 'Future' because the type arguments are different. late A a; late B2 b; -''', - [ - error(diag.conflictingGenericInterfaces, 65, 1), - error(diag.conflictingGenericInterfaces, 65, 1), - error(diag.implementsSuperClass, 99, 12), - error(diag.conflictingGenericInterfaces, 174, 2), - ], - ); +'''); InterfaceType intType = typeProvider.intType; InterfaceType numType = typeProvider.numType; var aType = findElement2.topVar('a').type; @@ -92,20 +90,18 @@ late B2 b; } test_flatten_related_types() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' abstract class A1 implements Future {} abstract class A2 extends A1 implements Future {} +// ^^ +// [diag.conflictingGenericInterfaces] The class 'A2' can't implement both 'Future' and 'Future' because the type arguments are different. abstract class B1 implements Future {} abstract class B2 extends B1 implements Future {} +// ^^ +// [diag.conflictingGenericInterfaces] The class 'B2' can't implement both 'Future' and 'Future' because the type arguments are different. late A2 a; late B2 b; -''', - [ - error(diag.conflictingGenericInterfaces, 59, 2), - error(diag.conflictingGenericInterfaces, 158, 2), - ], - ); +'''); InterfaceType intType = typeProvider.intType; InterfaceType numType = typeProvider.numType; var aType = findElement2.topVar('a').type; @@ -139,26 +135,24 @@ late B2 b; } test_flatten_unrelated_types() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' abstract class A1 implements Future {} abstract class A2 extends A1 implements Future {} +// ^^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'catchError': Future.catchError (Future Function(Function, {bool Function(Object)? test})), Future.catchError (Future Function(Function, {bool Function(Object)? test})). +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'then': Future.then (Future Function(FutureOr Function(int), {Function? onError})), Future.then (Future Function(FutureOr Function(String), {Function? onError})). +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'whenComplete': Future.whenComplete (Future Function(FutureOr Function())), Future.whenComplete (Future Function(FutureOr Function())). +// [diag.conflictingGenericInterfaces] The class 'A2' can't implement both 'Future' and 'Future' because the type arguments are different. abstract class B1 implements Future {} abstract class B2 extends B1 implements Future {} +// ^^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'catchError': Future.catchError (Future Function(Function, {bool Function(Object)? test})), Future.catchError (Future Function(Function, {bool Function(Object)? test})). +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'then': Future.then (Future Function(FutureOr Function(String), {Function? onError})), Future.then (Future Function(FutureOr Function(int), {Function? onError})). +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'whenComplete': Future.whenComplete (Future Function(FutureOr Function())), Future.whenComplete (Future Function(FutureOr Function())). +// [diag.conflictingGenericInterfaces] The class 'B2' can't implement both 'Future' and 'Future' because the type arguments are different. late A2 a; late B2 b; -''', - [ - error(diag.inconsistentInheritance, 59, 2), - error(diag.inconsistentInheritance, 59, 2), - error(diag.inconsistentInheritance, 59, 2), - error(diag.conflictingGenericInterfaces, 59, 2), - error(diag.inconsistentInheritance, 164, 2), - error(diag.inconsistentInheritance, 164, 2), - error(diag.inconsistentInheritance, 164, 2), - error(diag.conflictingGenericInterfaces, 164, 2), - ], - ); +'''); var aType = findElement2.topVar('a').type; var bType = findElement2.topVar('b').type; // flatten(A) = A and flatten(B) = B, since neither string nor int is more diff --git a/pkg/analyzer/test/generated/static_type_warning_code_test.dart b/pkg/analyzer/test/generated/static_type_warning_code_test.dart index 36de42ef1e7..bc6343f2b6c 100644 --- a/pkg/analyzer/test/generated/static_type_warning_code_test.dart +++ b/pkg/analyzer/test/generated/static_type_warning_code_test.dart @@ -2,15 +2,16 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/context_collection_resolution.dart'; +import '../src/dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(StaticTypeWarningCodeTest); defineReflectiveTests(StrongModeStaticTypeWarningCodeTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -20,134 +21,119 @@ main() { @reflectiveTest class StaticTypeWarningCodeTest extends PubPackageResolutionTest { test_await_flattened() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' external Future> ffi(); f() async { Future b = await ffi(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. } -''', - [error(diag.unusedLocalVariable, 62, 1)], - ); +'''); } test_await_simple() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Future fi() => Future.value(0); f() async { String a = await fi(); // Warning: int not assignable to String +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. +// ^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. } -''', - [ - error(diag.unusedLocalVariable, 58, 1), - error(diag.invalidAssignment, 62, 10), - ], - ); +'''); } test_awaitForIn_declaredVariableRightType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(Stream stream) async { await for (int i in stream) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 47, 1)], - ); +'''); } test_awaitForIn_declaredVariableWrongType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(Stream stream) async { await for (int i in stream) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. +// ^^^^^^ +// [diag.forInOfInvalidElementType] The type 'Stream' used in the 'for' loop must implement 'Stream' with a type argument that can be assigned to 'int'. } -''', - [ - error(diag.unusedLocalVariable, 50, 1), - error(diag.forInOfInvalidElementType, 55, 6), - ], - ); +'''); } test_awaitForIn_downcast() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(Stream stream) async { await for (int i in stream) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. +// ^^^^^^ +// [diag.forInOfInvalidElementType] The type 'Stream' used in the 'for' loop must implement 'Stream' with a type argument that can be assigned to 'int'. } -''', - [ - error(diag.unusedLocalVariable, 47, 1), - error(diag.forInOfInvalidElementType, 52, 6), - ], - ); +'''); } test_awaitForIn_dynamicVariable() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(Stream stream) async { await for (var i in stream) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 47, 1)], - ); +'''); } test_awaitForIn_existingVariableRightType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(Stream stream) async { late int i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. await for (i in stream) {} } -''', - [error(diag.unusedLocalVariable, 41, 1)], - ); +'''); } test_awaitForIn_existingVariableWrongType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(Stream stream) async { late int i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. await for (i in stream) {} +// ^^^^^^ +// [diag.forInOfInvalidElementType] The type 'Stream' used in the 'for' loop must implement 'Stream' with a type argument that can be assigned to 'int'. } -''', - [ - error(diag.unusedLocalVariable, 44, 1), - error(diag.forInOfInvalidElementType, 65, 6), - ], - ); +'''); } test_awaitForIn_streamOfDynamic() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(Stream stream) async { await for (int i in stream) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 42, 1)], - ); +'''); } test_awaitForIn_upcast() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(Stream stream) async { await for (num i in stream) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 47, 1)], - ); +'''); } test_bug21912() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A {} class B extends A {} @@ -161,250 +147,228 @@ void f( ) { { Function2, Function2> left; +// ^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'left' isn't used. left = t1; +// ^^ +// [diag.invalidAssignment] A value of type 'Function2, Function2>' can't be assigned to a variable of type 'Function2, Function2>'. left = t2; +// ^^ +// [diag.invalidAssignment] A value of type 'Function2' can't be assigned to a variable of type 'Function2, Function2>'. } } -''', - [ - error(diag.unusedLocalVariable, 263, 4), - error(diag.invalidAssignment, 281, 2), - error(diag.invalidAssignment, 296, 2), - ], - ); +'''); } test_forIn_declaredVariableRightType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { for (int i in []) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 17, 1)], - ); +'''); } test_forIn_declaredVariableWrongType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { for (int i in []) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. +// ^^^^^^^^^^ +// [diag.forInOfInvalidElementType] The type 'List' used in the 'for' loop must implement 'Iterable' with a type argument that can be assigned to 'int'. } -''', - [ - error(diag.unusedLocalVariable, 17, 1), - error(diag.forInOfInvalidElementType, 22, 10), - ], - ); +'''); } test_forIn_dynamic() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { dynamic d; // Could be []. for (var i in d) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 46, 1)], - ); +'''); } test_forIn_dynamicIterable() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { dynamic iterable; for (int i in iterable) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 37, 1)], - ); +'''); } test_forIn_dynamicVariable() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { for (var i in []) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 17, 1)], - ); +'''); } test_forIn_existingVariableRightType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { int i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. for (i in []) {} } -''', - [error(diag.unusedLocalVariable, 12, 1)], - ); +'''); } test_forIn_existingVariableWrongType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { int i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. for (i in []) {} +// ^^^^^^^^^^ +// [diag.forInOfInvalidElementType] The type 'List' used in the 'for' loop must implement 'Iterable' with a type argument that can be assigned to 'int'. } -''', - [ - error(diag.unusedLocalVariable, 12, 1), - error(diag.forInOfInvalidElementType, 27, 10), - ], - ); +'''); } test_forIn_iterableOfDynamic() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { for (int i in []) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 17, 1)], - ); +'''); } test_forIn_object() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f(List o) { // Could be []. for (var i in o) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 39, 1)], - ); +'''); } test_forIn_typeBoundBad() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class Foo> { void method(T iterable) { for (String i in iterable) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. +// ^^^^^^^^ +// [diag.forInOfInvalidElementType] The type 'Iterable' used in the 'for' loop must implement 'Iterable' with a type argument that can be assigned to 'String'. } } -''', - [ - error(diag.unusedLocalVariable, 81, 1), - error(diag.forInOfInvalidElementType, 86, 8), - ], - ); +'''); } test_forIn_typeBoundGood() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class Foo> { void method(T iterable) { for (var i in iterable) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } } -''', - [error(diag.unusedLocalVariable, 78, 1)], - ); +'''); } test_forIn_upcast() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' f() { for (num i in []) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. } -''', - [error(diag.unusedLocalVariable, 17, 1)], - ); +'''); } test_typePromotion_booleanAnd_useInRight_accessedInClosureRight_mutated() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' callMe(f()) { f(); } f(Object p) { (p is String) && callMe(() { p.length; }); +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. p = 0; } -''', - [error(diag.undefinedGetter, 68, 6)], - ); +'''); } test_typePromotion_booleanAnd_useInRight_mutatedInLeft() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(Object p) { ((p is String) && ((p = 42) == 42)) && p.length != 0; +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. } -''', - [error(diag.undefinedGetter, 57, 6)], - ); +'''); } test_typePromotion_booleanAnd_useInRight_mutatedInRight() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(Object p) { (p is String) && (((p = 42) == 42) && p.length != 0); +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. } -''', - [error(diag.undefinedGetter, 56, 6)], - ); +'''); } test_typePromotion_conditional_useInThen_accessedInClosure_hasAssignment_after() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' callMe(f()) { f(); } g(Object p) { p is String ? callMe(() { p.length; }) : 0; +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. p = 42; } -''', - [error(diag.undefinedGetter, 65, 6)], - ); +'''); } test_typePromotion_conditional_useInThen_accessedInClosure_hasAssignment_before() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' callMe(f()) { f(); } g(Object p) { p = 42; p is String ? callMe(() { p.length; }) : 0; +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. } -''', - [error(diag.undefinedGetter, 75, 6)], - ); +'''); } test_typePromotion_if_accessedInClosure_hasAssignment() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' callMe(f()) { f(); } f(Object p) { if (p is String) { callMe(() { p.length; +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. }); } p = 0; } -''', - [error(diag.undefinedGetter, 80, 6)], - ); +'''); } test_typePromotion_if_extends_notMoreSpecific_dynamic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class V {} class A {} class B extends A { @@ -414,16 +378,15 @@ class B extends A { f(A p) { if (p is B) { p.b; +// ^ +// [diag.undefinedGetter] The getter 'b' isn't defined for the type 'A'. } } -''', - [error(diag.undefinedGetter, 97, 1)], - ); +'''); } test_typePromotion_if_extends_notMoreSpecific_notMoreSpecificTypeArg() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class V {} class A {} class B extends A { @@ -433,58 +396,56 @@ class B extends A { f(A p) { if (p is B) { p.b; +// ^ +// [diag.undefinedGetter] The getter 'b' isn't defined for the type 'A'. } } -''', - [error(diag.undefinedGetter, 102, 1)], - ); +'''); } test_typePromotion_if_hasAssignment_before() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(Object p) { if (p is String) { p = 0; p.length; +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. } } -''', - [error(diag.undefinedGetter, 52, 6)], - ); +'''); } test_typePromotion_if_hasAssignment_inClosure_anonymous_before() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(Object p) { () {p = 0;}; if (p is String) { p.length; +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. } } -''', - [error(diag.undefinedGetter, 56, 6)], - ); +'''); } test_typePromotion_if_hasAssignment_inClosure_function_before() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' g(Object p) { f() {p = 0;}; +//^ +// [diag.unusedElement] The declaration 'f' isn't referenced. if (p is String) { p.length; +// ^^^^^^ +// [diag.undefinedGetter] The getter 'length' isn't defined for the type 'Object'. } } -''', - [error(diag.unusedElement, 16, 1), error(diag.undefinedGetter, 57, 6)], - ); +'''); } test_typePromotion_if_implements_notMoreSpecific_dynamic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class V {} class A {} class B implements A { @@ -494,16 +455,15 @@ class B implements A { f(A p) { if (p is B) { p.b; +// ^ +// [diag.undefinedGetter] The getter 'b' isn't defined for the type 'A'. } } -''', - [error(diag.undefinedGetter, 100, 1)], - ); +'''); } test_typePromotion_if_with_notMoreSpecific_dynamic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class V {} mixin A {} class B extends Object with A { @@ -513,25 +473,24 @@ class B extends Object with A { f(A p) { if (p is B) { p.b; +// ^ +// [diag.undefinedGetter] The getter 'b' isn't defined for the type 'A'. } } -''', - [error(diag.undefinedGetter, 109, 1)], - ); +'''); } test_wrongNumberOfTypeArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { late E element; } g(A a) { +// ^^^^^^^^^^ +// [diag.nonTypeAsTypeArgument] The name 'NoSuchType' isn't a type, so it can't be used as a type argument. a.element.anyGetterExistsInDynamic; } -''', - [error(diag.nonTypeAsTypeArgument, 37, 10)], - ); +'''); } } diff --git a/pkg/analyzer/test/generated/static_warning_code_test.dart b/pkg/analyzer/test/generated/static_warning_code_test.dart index f818aadb162..0ababaa63fa 100644 --- a/pkg/analyzer/test/generated/static_warning_code_test.dart +++ b/pkg/analyzer/test/generated/static_warning_code_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/context_collection_resolution.dart'; +import '../src/dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(StaticWarningCodeTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -20,7 +21,7 @@ class StaticWarningCodeTest extends PubPackageResolutionTest { // they're testing. test_nonAbstractClassInheritsAbstractMemberOne_ensureCorrectFunctionSubtypeIsUsedInImplementation() async { // 15028 - await assertErrorsInCode( + await resolveTestCodeWithDiagnostics( ''' class C { foo(int x) => x; @@ -28,8 +29,9 @@ class C { abstract class D { foo(x, [y]); } -class E extends C implements D {}''', - [error(diag.invalidImplementationOverride, 73, 1)], +class E extends C implements D {} +// ^ +// [diag.invalidImplementationOverride] 'C.foo' ('dynamic Function(int)') isn't a valid concrete implementation of 'D.foo' ('dynamic Function(dynamic, [dynamic])').''', ); } diff --git a/pkg/analyzer/test/generated/strong_mode_test.dart b/pkg/analyzer/test/generated/strong_mode_test.dart index 1e40ffc7c26..b7b094d424c 100644 --- a/pkg/analyzer/test/generated/strong_mode_test.dart +++ b/pkg/analyzer/test/generated/strong_mode_test.dart @@ -13,6 +13,7 @@ import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/context_collection_resolution.dart'; +import '../src/dart/resolution/node_text_expectations.dart'; import '../utils.dart'; import 'resolver_test_case.dart'; @@ -21,6 +22,7 @@ main() { defineReflectiveTests(StrongModeLocalInferenceTest); defineReflectiveTests(StrongModeStaticTypeAnalyzer2Test); defineReflectiveTests(StrongModeTypePropagationTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -250,14 +252,14 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { // Test that upwards inference with two type variables correctly // propagates from the constrained variable to the unconstrained // variable if they are ordered left to right. - String code = r''' + await resolveTestCodeWithDiagnostics(r''' T f(S x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. void test() { var x = f(3); } - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 32, 4), - error(diag.unusedLocalVariable, 60, 1), - ]); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -273,14 +275,14 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { // Test that upwards inference with two type variables does // propagate from the constrained variable to the unconstrained // variable if they are ordered right to left. - String code = r''' + await resolveTestCodeWithDiagnostics(r''' T f(S x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. void test() { var x = f(3); } - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 32, 4), - error(diag.unusedLocalVariable, 60, 1), - ]); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -293,14 +295,14 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_constrainedByBounds3() async { - var code = r''' + await resolveTestCodeWithDiagnostics(r''' T f(S x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. void test() { var x = f(3); } - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 46, 4), - error(diag.unusedLocalVariable, 76, 1), - ]); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -317,15 +319,15 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { // propagates from the constrained variable to the unconstrained // variable if they are ordered left to right, when the variable // appears co and contra variantly - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef To Func1(From x); T f>(S x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. void test() { var x = f(3)(4); } - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 82, 4), - error(diag.unusedLocalVariable, 110, 1), - ]); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -384,15 +386,14 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_factoryConstructor_propagation() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { factory A() { return new B(); } } class B extends A {} - '''; - await assertErrorsInCode(code, [ - error(diag.noGenerativeConstructorsInSuperclass, 92, 4), - ]); +// ^^^^ +// [diag.noGenerativeConstructorsInSuperclass] The class 'B' can't extend 'A' because 'A' only has factory constructors (no generative constructors), and 'B' has at least one generative constructor. + '''); ConstructorDeclaration constructor = AstFinder.getConstructorInClass( unit, @@ -435,19 +436,20 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionDeclaration_body_propagation() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef T Function2(S x); List test1() => []; Function2 test2 (int x) { Function2 inner() { +// ^^^^^ +// [diag.unusedElement] The declaration 'inner' isn't referenced. return (x) => x.length; } return (x) => x; } - '''; - await assertErrorsInCode(code, [error(diag.unusedElement, 144, 5)]); + '''); Asserter assertListOfInt = _isListOf(_isInt); @@ -477,28 +479,35 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_assignment_typedArguments() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef T Function2(S x); void main () { Function2 l0 = (int x) => null; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. Function2 l1 = (int x) => "hello"; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Function2 l2 = (String x) => "hello"; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'String Function(String)' can't be assigned to a variable of type 'Function2'. Function2 l3 = (int x) => 3; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. Function2 l4 = (int x) {return 3;}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 91, 2), - error(diag.returnOfInvalidTypeFromClosure, 107, 4), - error(diag.unusedLocalVariable, 144, 2), - error(diag.unusedLocalVariable, 200, 2), - error(diag.invalidAssignment, 205, 21), - error(diag.unusedLocalVariable, 259, 2), - error(diag.returnOfInvalidTypeFromClosure, 275, 1), - error(diag.unusedLocalVariable, 309, 2), - error(diag.returnOfInvalidTypeFromClosure, 330, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -519,27 +528,33 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_assignment_unTypedArguments() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef T Function2(S x); void main () { Function2 l0 = (x) => null; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. Function2 l1 = (x) => "hello"; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Function2 l2 = (x) => "hello"; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. Function2 l3 = (x) => 3; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. Function2 l4 = (x) {return 3;}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 91, 2), - error(diag.returnOfInvalidTypeFromClosure, 103, 4), - error(diag.unusedLocalVariable, 140, 2), - error(diag.unusedLocalVariable, 192, 2), - error(diag.unusedLocalVariable, 244, 2), - error(diag.returnOfInvalidTypeFromClosure, 256, 1), - error(diag.unusedLocalVariable, 290, 2), - error(diag.returnOfInvalidTypeFromClosure, 307, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -560,25 +575,30 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_body_propagation() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef T Function2(S x); void main () { Function2> l0 = (int x) => ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Function2> l1 = (String x) => ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List Function(String)' can't be assigned to a variable of type 'Function2>'. Function2> l2 = (int x) => [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. Function2> l3 = (int x) {return [3];}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 97, 2), - error(diag.unusedLocalVariable, 161, 2), - error(diag.invalidAssignment, 166, 23), - error(diag.unusedLocalVariable, 228, 2), - error(diag.listElementTypeNotAssignable, 245, 1), - error(diag.unusedLocalVariable, 286, 2), - error(diag.listElementTypeNotAssignable, 308, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -605,26 +625,29 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_functionExpressionInvocation_typedArguments() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class Mapper { T map(T mapper(F x)) => mapper(null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'F'. } void main () { (new Mapper().map)((int x) => null); +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. (new Mapper().map)((int x) => "hello"); (new Mapper().map)((String x) => "hello"); +// ^^^^^^^^^^^^^^^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String Function(String)' can't be assigned to the parameter type 'String Function(int)'. (new Mapper().map)((int x) => 3); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. (new Mapper().map)((int x) {return 3;}); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } - '''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 66, 4), - error(diag.returnOfInvalidTypeFromClosure, 154, 4), - error(diag.argumentTypeNotAssignable, 262, 21), - error(diag.returnOfInvalidTypeFromClosure, 337, 1), - error(diag.returnOfInvalidTypeFromClosure, 397, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -645,25 +668,27 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_functionExpressionInvocation_unTypedArguments() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class Mapper { T map(T mapper(F x)) => mapper(null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'F'. } void main () { (new Mapper().map)((x) => null); +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. (new Mapper().map)((x) => "hello"); (new Mapper().map)((x) => "hello"); (new Mapper().map)((x) => 3); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. (new Mapper().map)((x) {return 3;}); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } - '''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 66, 4), - error(diag.returnOfInvalidTypeFromClosure, 150, 4), - error(diag.returnOfInvalidTypeFromClosure, 318, 1), - error(diag.returnOfInvalidTypeFromClosure, 374, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -684,24 +709,27 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_functionInvocation_typedArguments() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' String map(String mapper(int x)) => mapper(null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'int'. void main () { map((int x) => null); +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. map((int x) => "hello"); map((String x) => "hello"); +// ^^^^^^^^^^^^^^^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String Function(String)' can't be assigned to the parameter type 'String Function(int)'. map((int x) => 3); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. map((int x) {return 3;}); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } - '''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 49, 4), - error(diag.returnOfInvalidTypeFromClosure, 101, 4), - error(diag.argumentTypeNotAssignable, 153, 21), - error(diag.returnOfInvalidTypeFromClosure, 200, 1), - error(diag.returnOfInvalidTypeFromClosure, 232, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -722,23 +750,25 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_functionInvocation_unTypedArguments() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' String map(String mapper(int x)) => mapper(null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'int'. void main () { map((x) => null); +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. map((x) => "hello"); map((x) => "hello"); map((x) => 3); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. map((x) {return 3;}); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } - '''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 49, 4), - error(diag.returnOfInvalidTypeFromClosure, 97, 4), - error(diag.returnOfInvalidTypeFromClosure, 181, 1), - error(diag.returnOfInvalidTypeFromClosure, 209, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -759,26 +789,29 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_methodInvocation_typedArguments() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class Mapper { T map(T mapper(F x)) => mapper(null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'F'. } void main () { new Mapper().map((int x) => null); +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. new Mapper().map((int x) => "hello"); new Mapper().map((String x) => "hello"); +// ^^^^^^^^^^^^^^^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String Function(String)' can't be assigned to the parameter type 'String Function(int)'. new Mapper().map((int x) => 3); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. new Mapper().map((int x) {return 3;}); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } - '''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 66, 4), - error(diag.returnOfInvalidTypeFromClosure, 152, 4), - error(diag.argumentTypeNotAssignable, 256, 21), - error(diag.returnOfInvalidTypeFromClosure, 329, 1), - error(diag.returnOfInvalidTypeFromClosure, 387, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -799,25 +832,27 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_methodInvocation_unTypedArguments() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class Mapper { T map(T mapper(F x)) => mapper(null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'F'. } void main () { new Mapper().map((x) => null); +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. new Mapper().map((x) => "hello"); new Mapper().map((x) => "hello"); new Mapper().map((x) => 3); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. new Mapper().map((x) {return 3;}); +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } - '''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 66, 4), - error(diag.returnOfInvalidTypeFromClosure, 148, 4), - error(diag.returnOfInvalidTypeFromClosure, 310, 1), - error(diag.returnOfInvalidTypeFromClosure, 364, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -838,26 +873,31 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_functionLiteral_unTypedArgument_propagation() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef T Function2(S x); void main () { Function2 l0 = (x) => x; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Function2 l1 = (x) => x+1; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Function2 l2 = (x) => x; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. Function2 l3 = (x) => x.toLowerCase(); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^^^^^ +// [diag.undefinedMethod] The method 'toLowerCase' isn't defined for the type 'int'. Function2 l4 = (x) => x.toLowerCase(); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 88, 2), - error(diag.unusedLocalVariable, 131, 2), - error(diag.unusedLocalVariable, 179, 2), - error(diag.returnOfInvalidTypeFromClosure, 191, 1), - error(diag.unusedLocalVariable, 225, 2), - error(diag.undefinedMethod, 239, 11), - error(diag.unusedLocalVariable, 288, 2), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -1248,7 +1288,7 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { // type schemas correctly. Downwards inference in a partial context // (e.g. Map) should still allow upwards inference to fill // in the missing information. - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(T x); A.fromA(A a) {} @@ -1264,19 +1304,22 @@ class B { void test() { var a0 = new A.fromA(new A(3)); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. var a1 = new A.fromMap({'hello' : 3}); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. var a2 = new A.fromList([3]); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. var a3 = new A.fromT(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. var a4 = new A.fromB(new B(3)); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 205, 2), - error(diag.unusedLocalVariable, 241, 2), - error(diag.unusedLocalVariable, 284, 2), - error(diag.unusedLocalVariable, 318, 2), - error(diag.unusedLocalVariable, 347, 2), - ]); + '''); Element elementA = AstFinder.getClass(unit, "A").declaredFragment!.element; List statements = AstFinder.getStatementsInTopLevelFunction( @@ -1296,7 +1339,7 @@ void test() { } test_inferConstructor_unknownTypeLowerBound() async { - var code = r''' + await resolveTestCodeWithDiagnostics(r''' class C { C(void callback(List a)); } @@ -1304,9 +1347,10 @@ void test() { // downwards inference pushes List and in parameter position this // becomes inferred as List. var c = new C((items) {}); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'c' isn't used. } - '''; - await assertErrorsInCode(code, [error(diag.unusedLocalVariable, 225, 1)]); + '''); DartType cType = findElement2.localVar('c').type; Element elementC = AstFinder.getClass(unit, "C").declaredFragment!.element; @@ -1373,18 +1417,19 @@ Consider passing explicit type argument(s) to the generic. test_inference_error_extendsFromReturn() async { // This is not an inference error because we successfully infer Null. - var code = r''' + await resolveTestCodeWithDiagnostics(r''' T max(T x, T y) => x; test() { String hello = max(1, 2); +// ^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'hello' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'Never'. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'Never'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 56, 5), - error(diag.argumentTypeNotAssignable, 68, 1), - error(diag.argumentTypeNotAssignable, 71, 1), - ]); + '''); var node = findNode.singleMethodInvocation; assertResolvedNodeText(node, r''' @@ -1417,22 +1462,22 @@ MethodInvocation } test_inference_error_extendsFromReturn2() async { - var code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef R F(T t); F g() => (y) => y; test() { F hello = g(); +// ^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'hello' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'F' can't be assigned to a variable of type 'F'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 94, 5), - error(diag.invalidAssignment, 102, 3), - ]); + '''); } test_inference_error_genericFunction() async { - var code = r''' + await resolveTestCodeWithDiagnostics(r''' T max(T x, T y) => x < y ? y : x; abstract class Iterable { T get first; @@ -1440,12 +1485,12 @@ abstract class Iterable { } test(Iterable values) { num n = values.fold(values.first as num, max); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'n' isn't used. +// ^^^ +// [diag.argumentTypeNotAssignable] The argument type 'num Function(num, num)' can't be assigned to the parameter type 'num Function(num, dynamic)'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 158, 1), - error(diag.argumentTypeNotAssignable, 195, 3), - ]); + '''); } test_inference_error_returnContext() async { @@ -1476,31 +1521,32 @@ Consider passing explicit type argument(s) to the generic. } test_inference_hints() async { - var code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { var x = 3; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. List l0 = []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 33, 1), - error(diag.unusedLocalVariable, 58, 2), - ]); + '''); } test_inference_simplePolymorphicRecursion_function() async { // Regression test for https://github.com/dart-lang/sdk/issues/30980 // Check that inference works properly when inferring the type argument // for a self-recursive call with a function type - var code = r''' + await resolveTestCodeWithDiagnostics(r''' void _mergeSort(T Function(T) list, int compare(T a, T b), T Function(T) target) { +// ^^^^^^^^^^ +// [diag.unusedElement] The declaration '_mergeSort' isn't referenced. _mergeSort(list, compare, target); _mergeSort(list, compare, list); _mergeSort(target, compare, target); _mergeSort(target, compare, list); } - '''; - await assertErrorsInCode(code, [error(diag.unusedElement, 5, 10)]); + '''); var node = findNode.singleBlock; assertResolvedNodeText(node, r''' @@ -1659,15 +1705,16 @@ Block // Regression test for https://github.com/dart-lang/sdk/issues/30980 // Check that inference works properly when inferring the type argument // for a self-recursive call with an interface type - var code = r''' + await resolveTestCodeWithDiagnostics(r''' void _mergeSort(List list, int compare(T a, T b), List target) { +// ^^^^^^^^^^ +// [diag.unusedElement] The declaration '_mergeSort' isn't referenced. _mergeSort(list, compare, target); _mergeSort(list, compare, list); _mergeSort(target, compare, target); _mergeSort(target, compare, list); } - '''; - await assertErrorsInCode(code, [error(diag.unusedElement, 5, 10)]); + '''); var node = findNode.singleBlock; assertResolvedNodeText(node, r''' @@ -1826,15 +1873,16 @@ Block // Regression test for https://github.com/dart-lang/sdk/issues/30980 // Check that inference works properly when inferring the type argument // for a self-recursive call with a simple type parameter - var code = r''' + await resolveTestCodeWithDiagnostics(r''' void _mergeSort(T list, int compare(T a, T b), T target) { +// ^^^^^^^^^^ +// [diag.unusedElement] The declaration '_mergeSort' isn't referenced. _mergeSort(list, compare, target); _mergeSort(list, compare, list); _mergeSort(target, compare, target); _mergeSort(target, compare, list); } - '''; - await assertErrorsInCode(code, [error(diag.unusedElement, 5, 10)]); + '''); var node = findNode.singleBlock; assertResolvedNodeText(node, r''' @@ -1991,17 +2039,17 @@ Block test_inferGenericInstantiation() async { // Verify that we don't infer '?` when we instantiate a generic function. - var code = r''' + await resolveTestCodeWithDiagnostics(r''' T f(T x(T t)) => x(null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'T'. S g(S s) => s; test() { var h = f(g); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'h' isn't used. } - '''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 22, 4), - error(diag.unusedLocalVariable, 61, 1), - ]); + '''); var node = findNode.methodInvocation('f(g)'); assertResolvedNodeText(node, r''' @@ -2035,17 +2083,16 @@ MethodInvocation test_inferGenericInstantiation2() async { // Verify the behavior when we cannot infer an instantiation due to invalid // constraints from an outer generic method. - var code = r''' + await resolveTestCodeWithDiagnostics(r''' T max(T x, T y) => x < y ? y : x; abstract class Iterable { T get first; S fold(S s, S f(S s, T t)); } num test(Iterable values) => values.fold(values.first as num, max); - '''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 190, 3), - ]); +// ^^^ +// [diag.argumentTypeNotAssignable] The argument type 'num Function(num, num)' can't be assigned to the parameter type 'num Function(num, dynamic)'. + '''); var node = findNode.methodInvocation('values.fold'); assertResolvedNodeText(node, r''' @@ -2154,7 +2201,7 @@ MethodInvocation } test_instanceCreation() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S x; T y; @@ -2179,143 +2226,210 @@ MethodInvocation class E extends A, T> { E(T a) : super(null, a); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'C'. } class F extends A { F(S x, T y, {List a, List b}) : super(x, y); +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'b' can't have a value of 'null' because of its type, but the implicit default value is 'null'. F.named(S x, T y, [S a, T b]) : super(a, b); +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'b' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } void test0() { A a0 = new A(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new A.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. A a2 = new A(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new A.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new A(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'A' can't be assigned to a variable of type 'A'. A a5 = new A.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'A' can't be assigned to a variable of type 'A'. } void test1() { A a0 = new A("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. A a1 = new A.named("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. } void test2() { A a0 = new B("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new B.named("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. A a2 = new B("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new B.named("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new B("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'B' can't be assigned to a variable of type 'A'. A a5 = new B.named("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'B' can't be assigned to a variable of type 'A'. } void test3() { A a0 = new B(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. A a1 = new B.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. } void test4() { A a0 = new C(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new C.named(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. A a2 = new C(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new C.named(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new C(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'C' can't be assigned to a variable of type 'A'. A a5 = new C.named(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'C' can't be assigned to a variable of type 'A'. } void test5() { A a0 = new C("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. A a1 = new C.named("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. } void test6() { A a0 = new D("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new D.named("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. A a2 = new D("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new D.named("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new D("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'D' can't be assigned to a variable of type 'A'. A a5 = new D.named("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'D' can't be assigned to a variable of type 'A'. } void test7() { A a0 = new D(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. A a1 = new D.named(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. } void test8() { A, String> a0 = new E("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. } void test9() { // Check named and optional arguments A a0 = new F(3, "hello", a: [3], b: ["hello"]); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new F(3, "hello", a: ["hello"], b:[3]); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. A a2 = new F.named(3, "hello", 3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new F.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new F.named(3, "hello", "hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. A a5 = new F.named(3, "hello", "hello"); - }'''; - await assertErrorsInCode(code, [ - error(diag.argumentTypeNotAssignable, 547, 4), - error(diag.missingDefaultValueForParameter, 633, 1), - error(diag.missingDefaultValueForParameter, 644, 1), - error(diag.missingDefaultValueForParameterPositional, 692, 1), - error(diag.missingDefaultValueForParameterPositional, 697, 1), - error(diag.unusedLocalVariable, 769, 2), - error(diag.unusedLocalVariable, 816, 2), - error(diag.unusedLocalVariable, 869, 2), - error(diag.unusedLocalVariable, 929, 2), - error(diag.unusedLocalVariable, 995, 2), - error(diag.invalidAssignment, 1000, 31), - error(diag.unusedLocalVariable, 1056, 2), - error(diag.invalidAssignment, 1061, 41), - error(diag.unusedLocalVariable, 1157, 2), - error(diag.argumentTypeNotAssignable, 1168, 7), - error(diag.argumentTypeNotAssignable, 1177, 1), - error(diag.unusedLocalVariable, 1204, 2), - error(diag.argumentTypeNotAssignable, 1221, 7), - error(diag.argumentTypeNotAssignable, 1230, 1), - error(diag.unusedLocalVariable, 1286, 2), - error(diag.unusedLocalVariable, 1333, 2), - error(diag.unusedLocalVariable, 1386, 2), - error(diag.unusedLocalVariable, 1446, 2), - error(diag.unusedLocalVariable, 1512, 2), - error(diag.invalidAssignment, 1517, 34), - error(diag.unusedLocalVariable, 1576, 2), - error(diag.invalidAssignment, 1581, 41), - error(diag.unusedLocalVariable, 1676, 2), - error(diag.argumentTypeNotAssignable, 1687, 1), - error(diag.argumentTypeNotAssignable, 1690, 7), - error(diag.unusedLocalVariable, 1723, 2), - error(diag.argumentTypeNotAssignable, 1740, 1), - error(diag.argumentTypeNotAssignable, 1743, 7), - error(diag.unusedLocalVariable, 1802, 2), - error(diag.unusedLocalVariable, 1837, 2), - error(diag.unusedLocalVariable, 1878, 2), - error(diag.unusedLocalVariable, 1918, 2), - error(diag.unusedLocalVariable, 1964, 2), - error(diag.invalidAssignment, 1969, 17), - error(diag.unusedLocalVariable, 2008, 2), - error(diag.invalidAssignment, 2013, 23), - error(diag.unusedLocalVariable, 2087, 2), - error(diag.argumentTypeNotAssignable, 2098, 7), - error(diag.unusedLocalVariable, 2128, 2), - error(diag.argumentTypeNotAssignable, 2145, 7), - error(diag.unusedLocalVariable, 2208, 2), - error(diag.unusedLocalVariable, 2252, 2), - error(diag.unusedLocalVariable, 2302, 2), - error(diag.unusedLocalVariable, 2359, 2), - error(diag.unusedLocalVariable, 2425, 2), - error(diag.invalidAssignment, 2430, 28), - error(diag.unusedLocalVariable, 2483, 2), - error(diag.invalidAssignment, 2488, 38), - error(diag.unusedLocalVariable, 2580, 2), - error(diag.argumentTypeNotAssignable, 2591, 1), - error(diag.unusedLocalVariable, 2618, 2), - error(diag.argumentTypeNotAssignable, 2635, 1), - error(diag.unusedLocalVariable, 2694, 2), - error(diag.unusedLocalVariable, 2805, 2), - error(diag.unusedLocalVariable, 2874, 2), - error(diag.listElementTypeNotAssignable, 2901, 7), - error(diag.listElementTypeNotAssignable, 2914, 1), - error(diag.unusedLocalVariable, 2942, 2), - error(diag.unusedLocalVariable, 3007, 2), - error(diag.unusedLocalVariable, 3060, 2), - error(diag.argumentTypeNotAssignable, 3089, 7), - error(diag.argumentTypeNotAssignable, 3098, 1), - error(diag.unusedLocalVariable, 3125, 2), - error(diag.argumentTypeNotAssignable, 3154, 7), - ]); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. + }'''); Expression rhs(AstNode stmt) { stmt as VariableDeclarationStatement; @@ -2463,21 +2577,24 @@ MethodInvocation } test_listLiteral_nested() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { List> l0 = [[]]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Iterable> l1 = [[3]]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Iterable> l2 = [[3], [4]]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. List> l3 = [["hello", 3], []]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 45, 2), - error(diag.unusedLocalVariable, 84, 2), - error(diag.unusedLocalVariable, 124, 2), - error(diag.unusedLocalVariable, 165, 2), - error(diag.listElementTypeNotAssignable, 172, 7), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -2512,22 +2629,26 @@ MethodInvocation } test_listLiteral_simple() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { List l0 = []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. List l1 = [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. List l2 = ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. List l3 = ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 39, 2), - error(diag.unusedLocalVariable, 66, 2), - error(diag.unusedLocalVariable, 94, 2), - error(diag.listElementTypeNotAssignable, 100, 7), - error(diag.unusedLocalVariable, 128, 2), - error(diag.listElementTypeNotAssignable, 134, 7), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -2549,22 +2670,26 @@ MethodInvocation } test_listLiteral_simple_const() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { const List c0 = const []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c0' isn't used. const List c1 = const [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c1' isn't used. const List c2 = const ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c2' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. const List c3 = const ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c3' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 45, 2), - error(diag.unusedLocalVariable, 84, 2), - error(diag.unusedLocalVariable, 124, 2), - error(diag.listElementTypeNotAssignable, 136, 7), - error(diag.unusedLocalVariable, 170, 2), - error(diag.listElementTypeNotAssignable, 182, 7), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -2586,24 +2711,30 @@ MethodInvocation } test_listLiteral_simple_disabled() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { List l0 = []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^^^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. List l1 = [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. +// ^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. List l2 = ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. List l3 = ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 39, 2), - error(diag.invalidAssignment, 44, 7), - error(diag.unusedLocalVariable, 71, 2), - error(diag.invalidAssignment, 76, 8), - error(diag.unusedLocalVariable, 104, 2), - error(diag.invalidAssignment, 109, 17), - error(diag.unusedLocalVariable, 146, 2), - error(diag.invalidAssignment, 151, 21), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -2623,22 +2754,26 @@ MethodInvocation } test_listLiteral_simple_subtype() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { Iterable l0 = []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Iterable l1 = [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Iterable l2 = ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. Iterable l3 = ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 43, 2), - error(diag.unusedLocalVariable, 74, 2), - error(diag.unusedLocalVariable, 106, 2), - error(diag.listElementTypeNotAssignable, 112, 7), - error(diag.unusedLocalVariable, 144, 2), - error(diag.listElementTypeNotAssignable, 150, 7), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -2660,26 +2795,33 @@ MethodInvocation } test_mapLiteral_nested() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { Map> l0 = {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Map> l1 = {3: ["hello"]}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Map> l2 = {"hello": ["hello"]}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. Map> l3 = {3: [3]}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. Map> l4 = {3:["hello"], "hello": [3]}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 52, 2), - error(diag.unusedLocalVariable, 92, 2), - error(diag.unusedLocalVariable, 144, 2), - error(diag.mapKeyTypeNotAssignable, 150, 7), - error(diag.unusedLocalVariable, 202, 2), - error(diag.listElementTypeNotAssignable, 212, 1), - error(diag.unusedLocalVariable, 248, 2), - error(diag.mapKeyTypeNotAssignable, 267, 7), - error(diag.listElementTypeNotAssignable, 277, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -2723,26 +2865,33 @@ MethodInvocation } test_mapLiteral_simple() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { Map l0 = {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Map l1 = {3: "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Map l2 = {"hello": "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. Map l3 = {3: 3}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.mapValueTypeNotAssignable] The element type 'int' can't be assigned to the map value type 'String'. Map l4 = {3:"hello", "hello": 3}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. +// ^ +// [diag.mapValueTypeNotAssignable] The element type 'int' can't be assigned to the map value type 'String'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 46, 2), - error(diag.unusedLocalVariable, 80, 2), - error(diag.unusedLocalVariable, 124, 2), - error(diag.mapKeyTypeNotAssignable, 130, 7), - error(diag.unusedLocalVariable, 174, 2), - error(diag.mapValueTypeNotAssignable, 183, 1), - error(diag.unusedLocalVariable, 212, 2), - error(diag.mapKeyTypeNotAssignable, 229, 7), - error(diag.mapValueTypeNotAssignable, 238, 1), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -2767,25 +2916,32 @@ MethodInvocation } test_mapLiteral_simple_disabled() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' void main () { Map l0 = {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. Map l1 = {3: "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. Map l2 = {"hello": "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. Map l3 = {3: 3}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. } - '''; - await assertErrorsInCode(code, [ - error(diag.unusedLocalVariable, 46, 2), - error(diag.invalidAssignment, 51, 16), - error(diag.unusedLocalVariable, 94, 2), - error(diag.invalidAssignment, 99, 26), - error(diag.unusedLocalVariable, 152, 2), - error(diag.invalidAssignment, 157, 32), - error(diag.mapKeyTypeNotAssignable, 172, 7), - error(diag.unusedLocalVariable, 216, 2), - error(diag.invalidAssignment, 221, 20), - ]); + '''); List statements = AstFinder.getStatementsInTopLevelFunction( unit, @@ -2810,15 +2966,14 @@ MethodInvocation } test_methodDeclaration_body_propagation() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { List m0(int x) => ["hello"]; List m1(int x) {return [3];} +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. } - '''; - await assertErrorsInCode(code, [ - error(diag.listElementTypeNotAssignable, 101, 1), - ]); + '''); Expression methodReturnValue(String methodName) { MethodDeclaration method = AstFinder.getMethodInClass( @@ -2844,14 +2999,13 @@ MethodInvocation // Test that downwards inference with a partial type // correctly uses the partial information to fill in subterm // types - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef To Func1(From x); S f(Func1 g) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'S'. String test() => f((l) => l.length); - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 72, 4), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -2867,19 +3021,20 @@ MethodInvocation // Test that downwards inference with two different downwards covariant // constraints on the same parameter correctly fails to infer when // the types do not share a common subtype - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S s; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 's' must be initialized. T t; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 't' must be initialized. } class B extends A { B(S s); } A test() => new B(3); - '''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableInstanceField, 28, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - error(diag.argumentTypeNotAssignable, 126, 1), - ]); +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'Never'. + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -2893,18 +3048,18 @@ MethodInvocation test_pinning_multipleConstraints2() async { // Test that downwards inference with two identical downwards covariant // constraints on the same parameter correctly infers and pins the type - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S s; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 's' must be initialized. T t; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 't' must be initialized. } class B extends A { B(S s); } A test() => new B(3); - '''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableInstanceField, 28, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -2919,19 +3074,20 @@ MethodInvocation // Test that downwards inference with two different downwards covariant // constraints on the same parameter correctly fails to infer when // the types do not share a common subtype, but do share a common supertype - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S s; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 's' must be initialized. T t; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 't' must be initialized. } class B extends A { B(S s); } A test() => new B(3); - '''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableInstanceField, 28, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - error(diag.argumentTypeNotAssignable, 126, 1), - ]); +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'Never'. + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -2946,18 +3102,18 @@ MethodInvocation // Test that downwards inference with two subtype related downwards // covariant constraints on the same parameter correctly infers and pins // the type - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S s; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 's' must be initialized. T t; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 't' must be initialized. } class B extends A {} A test() => new B(); - '''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableInstanceField, 28, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -2972,20 +3128,20 @@ MethodInvocation // Test that downwards inference with two different downwards contravariant // constraints on the same parameter chooses the upper bound // when the only supertype is Object - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S s; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 's' must be initialized. T t; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 't' must be initialized. } class B extends A {} typedef void Contra1(T x); Contra1> mkA() => (A x) {}; Contra1> test() => mkA(); - '''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableInstanceField, 28, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3000,20 +3156,20 @@ MethodInvocation test_pinning_multipleConstraints_contravariant2() async { // Test that downwards inference with two identical downwards contravariant // constraints on the same parameter correctly pins the type - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S s; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 's' must be initialized. T t; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 't' must be initialized. } class B extends A {} typedef void Contra1(T x); Contra1> mkA() => (A x) {}; Contra1> test() => mkA(); - '''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableInstanceField, 28, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3029,20 +3185,20 @@ MethodInvocation // Test that downwards inference with two different downwards contravariant // constraints on the same parameter correctly choose the least upper bound // when they share a common supertype - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S s; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 's' must be initialized. T t; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 't' must be initialized. } class B extends A {} typedef void Contra1(T x); Contra1> mkA() => (A x) {}; Contra1> test() => mkA(); - '''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableInstanceField, 28, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3058,20 +3214,20 @@ MethodInvocation // Test that downwards inference with two different downwards contravariant // constraints on the same parameter correctly choose the least upper bound // when one is a subtype of the other - String code = r''' + await resolveTestCodeWithDiagnostics(r''' class A { S s; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 's' must be initialized. T t; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 't' must be initialized. } class B extends A {} typedef void Contra1(T x); Contra1> mkA() => (A x) {}; Contra1> test() => mkA(); - '''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableInstanceField, 28, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3165,14 +3321,13 @@ class B { test_returnType_variance1() async { // Check that downwards inference correctly pins a type parameter // when the parameter is constrained in a contravariant position - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef To Func1(From x); Func1 f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'Func1'. Func1 test() => f(42); - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 74, 4), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3185,14 +3340,13 @@ class B { test_returnType_variance2() async { // Check that downwards inference correctly pins a type parameter // when the parameter is constrained in a covariant position - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef To Func1(From x); Func1 f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'Func1'. Func1 test() => f(42); - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 74, 4), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3206,14 +3360,13 @@ class B { // Check that the variance heuristic chooses the most precise type // when the return type uses the variable in a contravariant position // and there is no downwards constraint. - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef To Func1(From x); Func1 f(T x, g(T x)) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'Func1'. dynamic test() => f(42, (num x) => x); - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 82, 4), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3226,14 +3379,13 @@ class B { // Check that the variance heuristic chooses the more precise type // when the return type uses the variable in a covariant position // and there is no downwards constraint - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef To Func1(From x); Func1 f(T x, g(T x)) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'Func1'. dynamic test() => f(42, (num x) => x); - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 82, 4), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3245,16 +3397,16 @@ class B { test_returnType_variance5() async { // Check that pinning works correctly with a partial type // when the return type uses the variable in a contravariant position - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef To Func1(From x); Func1 f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'Func1'. T g(Func1 f) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'g' because it has a return type of 'T'. num test() => g(f(3)); - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 74, 4), - error(diag.returnOfInvalidTypeFromFunction, 112, 4), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3268,16 +3420,16 @@ class B { test_returnType_variance6() async { // Check that pinning works correctly with a partial type // when the return type uses the variable in a covariant position - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef To Func1(From x); Func1 f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'Func1'. T g(Func1 f) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'g' because it has a return type of 'T'. num test() => g(f(3)); - '''; - await assertErrorsInCode(code, [ - error(diag.returnOfInvalidTypeFromFunction, 74, 4), - error(diag.returnOfInvalidTypeFromFunction, 112, 4), - ]); + '''); FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, "test"); var body = test.functionExpression.body as ExpressionFunctionBody; @@ -3351,16 +3503,15 @@ $code @reflectiveTest class StrongModeStaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared { test_dynamicObjectGetter_hashCode() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { dynamic a = null; var foo = a.hashCode; +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 3)], - ); - expectInitializerType('foo', 'int'); +'''); + expectInitializerType('foo =', 'int'); } test_futureOr_promotion1() async { @@ -3375,14 +3526,13 @@ main() { test_futureOr_promotion2() async { // Test that promotion from FutureOr to Future works for concrete // types - String code = r''' + await resolveTestCodeWithDiagnostics(r''' import "dart:async"; dynamic test(FutureOr x) => (x is Future) && (x.then((x) => x) == null); - '''; - await assertErrorsInCode(code, [ - error(diag.unnecessaryNullComparisonNeverNullFalse, 139, 7), - ]); +// ^^^^^^^ +// [diag.unnecessaryNullComparisonNeverNullFalse] The operand can't be 'null', so the condition is always 'false'. + '''); } test_futureOr_promotion3() async { @@ -3399,32 +3549,32 @@ main() { test_futureOr_promotion4() async { // Test that promotion from FutureOr to Future works for type // parameters T - String code = r''' + await resolveTestCodeWithDiagnostics(r''' import "dart:async"; dynamic test(FutureOr x) => (x is Future) && (x.then((x) => x) == null); - '''; - await assertErrorsInCode(code, [ - error(diag.unnecessaryNullComparisonNeverNullFalse, 163, 7), - ]); +// ^^^^^^^ +// [diag.unnecessaryNullComparisonNeverNullFalse] The operand can't be 'null', so the condition is always 'false'. + '''); } test_generalizedVoid_assignToVoidOk() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void main() { void x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. x = 42; } -''', - [error(diag.unusedLocalVariable, 21, 1)], - ); +'''); } test_genericFunction() async { - await assertErrorsInCode(r'T f(T x) => null;', [ - error(diag.returnOfInvalidTypeFromFunction, 15, 4), - ]); + await resolveTestCodeWithDiagnostics(r''' +T f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. +'''); var node = findNode.functionDeclaration('f'); assertResolvedNodeText(node, r''' @@ -3472,9 +3622,11 @@ FunctionDeclaration } test_genericFunction_bounds() async { - await assertErrorsInCode(r'T f(T x) => null;', [ - error(diag.returnOfInvalidTypeFromFunction, 27, 4), - ]); + await resolveTestCodeWithDiagnostics(r''' +T f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. +'''); var node = findNode.functionDeclaration('f(T x)) {} } test_genericFunction_static() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static T f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } -''', - [error(diag.returnOfInvalidTypeFromMethod, 37, 4)], - ); +'''); var node = findNode.methodDeclaration('f'); assertResolvedNodeText(node, r''' @@ -3588,13 +3739,19 @@ MethodDeclaration } test_genericFunction_typedef() async { - String code = r''' + await resolveTestCodeWithDiagnostics(r''' typedef T F(T x); F f0; +//^^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'f0' must be initialized. class C { static F f1; +// ^^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'f1' must be initialized. F f2; +// ^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'f2' must be initialized. void g(F f3) { // C F f4; f0(3); @@ -3602,12 +3759,18 @@ class C { f2(3); f3(3); f4(3); +// ^^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'f4' must be assigned before it can be used. } } class D { static F f1; +// ^^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'f1' must be initialized. F f2; +// ^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'f2' must be initialized. void g(F f3) { // D F f4; f0(3); @@ -3615,18 +3778,11 @@ class D { f2(3); f3(3); f4(3); +// ^^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'f4' must be assigned before it can be used. } } -'''; - await assertErrorsInCode(code, [ - error(diag.notInitializedNonNullableVariable, 23, 2), - error(diag.notInitializedNonNullableVariable, 49, 2), - error(diag.notInitializedNonNullableInstanceField, 57, 2), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 141, 2), - error(diag.notInitializedNonNullableVariable, 179, 2), - error(diag.notInitializedNonNullableInstanceField, 187, 2), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 271, 2), - ]); +'''); checkBody(String className) { var statements = findNode.block('{ // $className').statements; @@ -3644,43 +3800,40 @@ class D { test_genericFunction_upwardsAndDownwards() async { // Regression tests for https://github.com/dart-lang/sdk/issues/27586. await resolveTestCodeWithDiagnostics(r'List x = [1, 2];'); - expectInitializerType('x', 'List'); + expectInitializerType('x =', 'List'); } test_genericFunction_upwardsAndDownwards_Object() async { // Regression tests for https://github.com/dart-lang/sdk/issues/27625. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' List aaa = []; List bbb = [1, 2, 3]; List ccc = [null]; +// ^^^^ +// [diag.listElementTypeNotAssignableNullability] The element type 'Null' can't be assigned to the list type 'Object'. List ddd = [1 as dynamic]; List eee = [new Object()]; -''', - [error(diag.listElementTypeNotAssignableNullability, 73, 4)], - ); - expectInitializerType('aaa', 'List'); - expectInitializerType('bbb', 'List'); - expectInitializerType('ccc', 'List'); - expectInitializerType('ddd', 'List'); - expectInitializerType('eee', 'List'); +'''); + expectInitializerType('aaa =', 'List'); + expectInitializerType('bbb =', 'List'); + expectInitializerType('ccc =', 'List'); + expectInitializerType('ddd =', 'List'); + expectInitializerType('eee =', 'List'); } test_genericMethod() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { List f(E e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'List'. } main() { C cOfString; +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'cOfString' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 36, 4), - error(diag.unusedLocalVariable, 65, 9), - ], - ); +'''); assertType(findElement2.method('f').type, 'List Function(E)'); var cOfString = findElement2.localVar('cOfString'); @@ -3695,22 +3848,21 @@ main() { } test_genericMethod_explicitTypeParams() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { List f(E e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'List'. } main() { C cOfString; var x = cOfString.f('hi'); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. +// ^^^^^^^^^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'cOfString' must be assigned before it can be used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 36, 4), - error(diag.unusedLocalVariable, 82, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 86, 9), - ], - ); +'''); var f = findNode.simple('f').parent as MethodInvocation; var ft = f.staticInvokeType as FunctionType; assertType(ft, 'List Function(String)'); @@ -3720,46 +3872,55 @@ main() { } test_genericMethod_functionExpressionInvocation_explicit() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. static T g(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'g' because it has a return type of 'T'. static T Function(T) h = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T Function(T)'. } T topF(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'topF' because it has a return type of 'T'. var topG = topF; void test(T Function(T) pf) { var c = new C(); T lf(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'lf' because it has a return type of 'T'. var lambdaCall = ((E e) => e)(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'lambdaCall' isn't used. var methodCall = (c.f)(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'methodCall' isn't used. var staticCall = (C.g)(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticCall' isn't used. var staticFieldCall = (C.h)(3); +// ^^^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticFieldCall' isn't used. var topFunCall = (topF)(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFunCall' isn't used. var topFieldCall = (topG)(3); +// ^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFieldCall' isn't used. var localCall = (lf)(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'localCall' isn't used. var paramCall = (pf)(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramCall' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 30, 4), - error(diag.returnOfInvalidTypeFromMethod, 60, 4), - error(diag.invalidAssignment, 96, 4), - error(diag.returnOfInvalidTypeFromFunction, 123, 4), - error(diag.returnOfInvalidTypeFromFunction, 224, 4), - error(diag.unusedLocalVariable, 237, 10), - error(diag.unusedLocalVariable, 281, 10), - error(diag.unusedLocalVariable, 315, 10), - error(diag.unusedLocalVariable, 349, 15), - error(diag.unusedLocalVariable, 388, 10), - error(diag.unusedLocalVariable, 423, 12), - error(diag.unusedLocalVariable, 460, 9), - error(diag.unusedLocalVariable, 492, 9), - ], - ); +'''); _assertLocalVarType('lambdaCall', "int"); _assertLocalVarType('methodCall', "int"); _assertLocalVarType('staticCall', "int"); @@ -3771,70 +3932,77 @@ void test(T Function(T) pf) { } test_genericMethod_functionExpressionInvocation_functionTypedParameter_explicit() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void test(T pf(T e)) { var paramCall = (pf)(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramCall' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 9)], - ); +'''); _assertLocalVarType('paramCall', "int"); } test_genericMethod_functionExpressionInvocation_functionTypedParameter_inferred() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void test(T pf(T e)) { var paramCall = (pf)(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramCall' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 9)], - ); +'''); _assertLocalVarType('paramCall', "int"); } test_genericMethod_functionExpressionInvocation_inferred() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. static T g(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'g' because it has a return type of 'T'. static T Function(T) h = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T Function(T)'. } T topF(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'topF' because it has a return type of 'T'. var topG = topF; void test(T Function(T) pf) { var c = new C(); T lf(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'lf' because it has a return type of 'T'. var lambdaCall = ((E e) => e)(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'lambdaCall' isn't used. var methodCall = (c.f)(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'methodCall' isn't used. var staticCall = (C.g)(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticCall' isn't used. var staticFieldCall = (C.h)(3); +// ^^^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticFieldCall' isn't used. var topFunCall = (topF)(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFunCall' isn't used. var topFieldCall = (topG)(3); +// ^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFieldCall' isn't used. var localCall = (lf)(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'localCall' isn't used. var paramCall = (pf)(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramCall' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 30, 4), - error(diag.returnOfInvalidTypeFromMethod, 60, 4), - error(diag.invalidAssignment, 96, 4), - error(diag.returnOfInvalidTypeFromFunction, 123, 4), - error(diag.returnOfInvalidTypeFromFunction, 224, 4), - error(diag.unusedLocalVariable, 237, 10), - error(diag.unusedLocalVariable, 276, 10), - error(diag.unusedLocalVariable, 305, 10), - error(diag.unusedLocalVariable, 334, 15), - error(diag.unusedLocalVariable, 368, 10), - error(diag.unusedLocalVariable, 398, 12), - error(diag.unusedLocalVariable, 430, 9), - error(diag.unusedLocalVariable, 457, 9), - ], - ); +'''); _assertLocalVarType('lambdaCall', "int"); _assertLocalVarType('methodCall', "int"); _assertLocalVarType('staticCall', "int"); @@ -3846,43 +4014,51 @@ void test(T Function(T) pf) { } test_genericMethod_functionInvocation_explicit() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. static T g(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'g' because it has a return type of 'T'. static T Function(T) h = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T Function(T)'. } T topF(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'topF' because it has a return type of 'T'. var topG = topF; void test(T Function(T) pf) { var c = new C(); T lf(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'lf' because it has a return type of 'T'. var methodCall = c.f(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'methodCall' isn't used. var staticCall = C.g(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticCall' isn't used. var staticFieldCall = C.h(3); +// ^^^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticFieldCall' isn't used. var topFunCall = topF(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFunCall' isn't used. var topFieldCall = topG(3); +// ^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFieldCall' isn't used. var localCall = lf(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'localCall' isn't used. var paramCall = pf(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramCall' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 30, 4), - error(diag.returnOfInvalidTypeFromMethod, 60, 4), - error(diag.invalidAssignment, 96, 4), - error(diag.returnOfInvalidTypeFromFunction, 123, 4), - error(diag.returnOfInvalidTypeFromFunction, 224, 4), - error(diag.unusedLocalVariable, 236, 10), - error(diag.unusedLocalVariable, 268, 10), - error(diag.unusedLocalVariable, 300, 15), - error(diag.unusedLocalVariable, 337, 10), - error(diag.unusedLocalVariable, 370, 12), - error(diag.unusedLocalVariable, 405, 9), - error(diag.unusedLocalVariable, 435, 9), - ], - ); +'''); _assertLocalVarType('methodCall', "int"); _assertLocalVarType('staticCall', "int"); _assertLocalVarType('staticFieldCall', "int"); @@ -3893,67 +4069,73 @@ void test(T Function(T) pf) { } test_genericMethod_functionInvocation_functionTypedParameter_explicit() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void test(T pf(T e)) { var paramCall = pf(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramCall' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 9)], - ); +'''); _assertLocalVarType('paramCall', "int"); } test_genericMethod_functionInvocation_functionTypedParameter_inferred() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void test(T pf(T e)) { var paramCall = pf(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramCall' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 9)], - ); +'''); _assertLocalVarType('paramCall', "int"); } test_genericMethod_functionInvocation_inferred() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. static T g(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'g' because it has a return type of 'T'. static T Function(T) h = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T Function(T)'. } T topF(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'topF' because it has a return type of 'T'. var topG = topF; void test(T Function(T) pf) { var c = new C(); T lf(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'lf' because it has a return type of 'T'. var methodCall = c.f(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'methodCall' isn't used. var staticCall = C.g(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticCall' isn't used. var staticFieldCall = C.h(3); +// ^^^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticFieldCall' isn't used. var topFunCall = topF(3); +// ^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFunCall' isn't used. var topFieldCall = topG(3); +// ^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFieldCall' isn't used. var localCall = lf(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'localCall' isn't used. var paramCall = pf(3); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramCall' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 30, 4), - error(diag.returnOfInvalidTypeFromMethod, 60, 4), - error(diag.invalidAssignment, 96, 4), - error(diag.returnOfInvalidTypeFromFunction, 123, 4), - error(diag.returnOfInvalidTypeFromFunction, 224, 4), - error(diag.unusedLocalVariable, 236, 10), - error(diag.unusedLocalVariable, 263, 10), - error(diag.unusedLocalVariable, 290, 15), - error(diag.unusedLocalVariable, 322, 10), - error(diag.unusedLocalVariable, 350, 12), - error(diag.unusedLocalVariable, 380, 9), - error(diag.unusedLocalVariable, 405, 9), - ], - ); +'''); _assertLocalVarType('methodCall', "int"); _assertLocalVarType('staticCall', "int"); _assertLocalVarType('staticFieldCall', "int"); @@ -3964,20 +4146,18 @@ void test(T Function(T) pf) { } test_genericMethod_functionTypedParameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { List f(T f(E e)) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'List'. } main() { C cOfString; +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'cOfString' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 41, 4), - error(diag.unusedLocalVariable, 70, 9), - ], - ); +'''); assertType( findElement2.method('f').type, 'List Function(T Function(E))', @@ -3995,14 +4175,13 @@ main() { } test_genericMethod_functionTypedParameter_tearoff() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void test(T pf(T e)) { var paramTearOff = pf; +// ^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramTearOff' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 12)], - ); +'''); _assertLocalVarType('paramTearOff', "T Function(T)"); } @@ -4010,28 +4189,26 @@ void test(T pf(T e)) { // Regression test for: // https://github.com/dart-lang/sdk/issues/25100#issuecomment-162047588 // These should not cause any hints or warnings. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class List { T map(T f(E e)) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'map' because it has a return type of 'T'. } void foo() { List list = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'List'. list.map((e) => e); list.map((e) => 3); -}''', - [ - error(diag.returnOfInvalidTypeFromMethod, 40, 4), - error(diag.invalidAssignment, 75, 4), - ], - ); +}'''); var node1 = findNode.methodInvocation('map((e) => e);'); assertResolvedNodeText(node1, r''' MethodInvocation target: SimpleIdentifier token: list - element: list@68 + element: list@237 staticType: List operator: . methodName: SimpleIdentifier @@ -4048,7 +4225,7 @@ MethodInvocation leftParenthesis: ( parameter: RegularFormalParameter name: e - declaredFragment: e@93 + declaredFragment: e@389 element: hasImplicitType isPublic type: dynamic rightParenthesis: ) @@ -4056,7 +4233,7 @@ MethodInvocation functionDefinition: => expression: SimpleIdentifier token: e - element: e@93 + element: e@389 staticType: dynamic declaredFragment: null@null element: null@null @@ -4077,7 +4254,7 @@ MethodInvocation MethodInvocation target: SimpleIdentifier token: list - element: list@68 + element: list@237 staticType: List operator: . methodName: SimpleIdentifier @@ -4094,7 +4271,7 @@ MethodInvocation leftParenthesis: ( parameter: RegularFormalParameter name: e - declaredFragment: e@115 + declaredFragment: e@411 element: hasImplicitType isPublic type: dynamic rightParenthesis: ) @@ -4119,68 +4296,63 @@ MethodInvocation } test_genericMethod_max_doubleDouble() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math'; main() { var foo = max(1.0, 2.0); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 3)], - ); - expectInitializerType('foo', 'double'); +'''); + expectInitializerType('foo =', 'double'); } test_genericMethod_max_doubleDouble_prefixed() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math' as math; main() { var foo = math.max(1.0, 2.0); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [error(diag.unusedLocalVariable, 43, 3)], - ); - expectInitializerType('foo', 'double'); +'''); + expectInitializerType('foo =', 'double'); } test_genericMethod_max_doubleInt() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math'; main() { var foo = max(1.0, 2); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 3)], - ); - expectInitializerType('foo', 'num'); +'''); + expectInitializerType('foo =', 'num'); } test_genericMethod_max_intDouble() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math'; main() { var foo = max(1, 2.0); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 3)], - ); - expectInitializerType('foo', 'num'); +'''); + expectInitializerType('foo =', 'num'); } test_genericMethod_max_intInt() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math'; main() { var foo = max(1, 2); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [error(diag.unusedLocalVariable, 35, 3)], - ); - expectInitializerType('foo', 'int'); +'''); + expectInitializerType('foo =', 'int'); } test_genericMethod_nestedBound() async { @@ -4195,18 +4367,17 @@ class Foo { } test_genericMethod_nestedCapture() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(S x) { new C().f(3); new C().f; // tear-off return null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } } -''', - [error(diag.returnOfInvalidTypeFromMethod, 96, 4)], - ); +'''); var node1 = findNode.methodInvocation('f(3);'); assertResolvedNodeText(node1, r''' @@ -4266,21 +4437,19 @@ MethodInvocation } test_genericMethod_nestedCaptureBounds() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(S x) { new C().f(3); +// ^^^ +// [diag.typeArgumentNotMatchingBounds] 'int' doesn't conform to the bound 'S' of the type parameter 'S'. new C().f; // tear-off return null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } } -''', - [ - error(diag.typeArgumentNotMatchingBounds, 56, 3), - error(diag.returnOfInvalidTypeFromMethod, 106, 4), - ], - ); +'''); var node1 = findNode.methodInvocation('f(3);'); assertResolvedNodeText(node1, r''' @@ -4350,18 +4519,16 @@ SimpleIdentifier } test_genericMethod_nestedFunctions() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' S f(S x) { g(S x) => f; +//^ +// [diag.unusedElement] The declaration 'g' isn't referenced. return null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'S'. } -''', - [ - error(diag.unusedElement, 16, 1), - error(diag.returnOfInvalidTypeFromFunction, 41, 4), - ], - ); +'''); assertType(findElement2.topFunction('f').type, 'S Function(S)'); assertType( findElement2.localFunction('g').type, @@ -4370,20 +4537,18 @@ S f(S x) { } test_genericMethod_override() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } class D extends C { T f(T y) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 27, 4), - error(diag.returnOfInvalidTypeFromMethod, 72, 4), - ], - ); +'''); var node = findNode.methodDeclaration('f(T y)'); assertResolvedNodeText(node, r''' @@ -4398,7 +4563,7 @@ MethodDeclaration typeParameters TypeParameter name: T - declaredFragment: T@61 + declaredFragment: T@221 defaultType: dynamic rightBracket: > parameters: FormalParameterList @@ -4409,7 +4574,7 @@ MethodDeclaration element: #E0 T type: T name: y - declaredFragment: y@66 + declaredFragment: y@226 element: isPublic type: T rightParenthesis: ) @@ -4419,39 +4584,37 @@ MethodDeclaration literal: null staticType: Null semicolon: ; - declaredFragment: f@59 + declaredFragment: f@219 element: ::@class::D::@method::f type: T Function(T) '''); } test_genericMethod_override_bounds() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B { T f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } // override with the same bound is OK class C extends B { T f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } // override with new name and the same bound is OK class D extends B { Q f(Q x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'Q'. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 48, 4), - error(diag.returnOfInvalidTypeFromMethod, 141, 4), - error(diag.returnOfInvalidTypeFromMethod, 247, 4), - ], - ); +'''); } test_genericMethod_override_covariant_field() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class A { num get x; set x(covariant num _); @@ -4459,10 +4622,10 @@ abstract class A { class B extends A { int x; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'x' must be initialized. } -''', - [error(diag.notInitializedNonNullableInstanceField, 87, 1)], - ); +'''); } test_genericMethod_override_differentContextsSameBounds() async { @@ -4483,95 +4646,79 @@ class GenericMethodBoundsDerived extends GenericMethodBounds { } test_genericMethod_override_invalidContravariantTypeParamBounds() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A {} class C { T f(T x) => null; +// ^ +// [context 1] The member being overridden. +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } class D extends C { T f(T x) => null; -}''', - [ - error(diag.returnOfInvalidTypeFromMethod, 69, 4), - error( - diag.invalidOverride, - 101, - 1, - contextMessages: [message(testFile, 46, 1)], - ), - error(diag.returnOfInvalidTypeFromMethod, 124, 4), - ], - ); +// ^ +// [diag.invalidOverride][context 1] 'D.f' ('T Function(T)') isn't a valid override of 'C.f' ('T Function(T)'). +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. +}'''); } test_genericMethod_override_invalidCovariantTypeParamBounds() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A {} class C { T f(T x) => null; +// ^ +// [context 1] The member being overridden. +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } class D extends C { T f(T x) => null; -}''', - [ - error(diag.returnOfInvalidTypeFromMethod, 69, 4), - error( - diag.invalidOverride, - 101, - 1, - contextMessages: [message(testFile, 46, 1)], - ), - error(diag.returnOfInvalidTypeFromMethod, 124, 4), - ], - ); +// ^ +// [diag.invalidOverride][context 1] 'D.f' ('T Function(T)') isn't a valid override of 'C.f' ('T Function(T)'). +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. +}'''); } test_genericMethod_override_invalidReturnType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { Iterable f(T x) => null; +// ^ +// [context 1] The member being overridden. +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'Iterable'. } class D extends C { String f(S x) => null; -}''', - [ - error(diag.returnOfInvalidTypeFromMethod, 37, 4), - error( - diag.invalidOverride, - 74, - 1, - contextMessages: [message(testFile, 24, 1)], - ), - error(diag.returnOfInvalidTypeFromMethod, 87, 4), - ], - ); +// ^ +// [diag.invalidOverride][context 1] 'D.f' ('String Function(S)') isn't a valid override of 'C.f' ('Iterable Function(T)'). +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'String'. +}'''); } test_genericMethod_override_invalidTypeParamCount() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(T x) => null; +// ^ +// [context 1] The member being overridden. +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. } class D extends C { S f(T x) => null; -}''', - [ - error(diag.returnOfInvalidTypeFromMethod, 27, 4), - error( - diag.invalidOverride, - 59, - 1, - contextMessages: [message(testFile, 14, 1)], - ), - error(diag.returnOfInvalidTypeFromMethod, 75, 4), - ], - ); +// ^ +// [diag.invalidOverride][context 1] 'D.f' ('S Function(T)') isn't a valid override of 'C.f' ('T Function(T)'). +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'S'. +}'''); } test_genericMethod_propagatedType_promotion() async { @@ -4582,8 +4729,7 @@ class D extends C { // example won't work, as we now compute a static type and therefore discard // the propagated type. So a new test was created that doesn't run under // strong mode. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class Iter { List map(S f(x)); } @@ -4591,56 +4737,63 @@ class C {} C toSpan(dynamic element) { if (element is Iter) { var y = element.map(toSpan); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } return null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'toSpan' because it has a return type of 'C'. } -''', - [ - error(diag.unusedLocalVariable, 122, 1), - error(diag.returnOfInvalidTypeFromFunction, 160, 4), - ], - ); +'''); _assertLocalVarType('y', 'List'); } test_genericMethod_tearoff() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { T f(E e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'T'. static T g(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'g' because it has a return type of 'T'. static T Function(T) h = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T Function(T)'. } T topF(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'topF' because it has a return type of 'T'. var topG = topF; void test(T Function(T) pf) { var c = new C(); T lf(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'lf' because it has a return type of 'T'. var methodTearOff = c.f; +// ^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'methodTearOff' isn't used. var staticTearOff = C.g; +// ^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticTearOff' isn't used. var staticFieldTearOff = C.h; +// ^^^^^^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'staticFieldTearOff' isn't used. var topFunTearOff = topF; +// ^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFunTearOff' isn't used. var topFieldTearOff = topG; +// ^^^^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'topFieldTearOff' isn't used. var localTearOff = lf; +// ^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'localTearOff' isn't used. var paramTearOff = pf; +// ^^^^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'paramTearOff' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 30, 4), - error(diag.returnOfInvalidTypeFromMethod, 60, 4), - error(diag.invalidAssignment, 96, 4), - error(diag.returnOfInvalidTypeFromFunction, 123, 4), - error(diag.returnOfInvalidTypeFromFunction, 224, 4), - error(diag.unusedLocalVariable, 236, 13), - error(diag.unusedLocalVariable, 263, 13), - error(diag.unusedLocalVariable, 290, 18), - error(diag.unusedLocalVariable, 322, 13), - error(diag.unusedLocalVariable, 350, 15), - error(diag.unusedLocalVariable, 380, 12), - error(diag.unusedLocalVariable, 405, 12), - ], - ); +'''); _assertLocalVarType('methodTearOff', "T Function(int)"); _assertLocalVarType('staticTearOff', "T Function(T)"); _assertLocalVarType('staticFieldTearOff', "T Function(T)"); @@ -4683,87 +4836,80 @@ void test(T pf(T e)) { } test_genericMethod_then() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' String toString(int x) => x.toString(); main() { Future bar = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'Future'. var foo = bar.then(toString); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [ - error(diag.invalidAssignment, 69, 4), - error(diag.unusedLocalVariable, 81, 3), - ], - ); +'''); - expectInitializerType('foo', 'Future'); + expectInitializerType('foo =', 'Future'); } test_genericMethod_then_prefixed() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:async' as async; String toString(int x) => x.toString(); main() { async.Future bar = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'Future'. var foo = bar.then(toString); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [ - error(diag.invalidAssignment, 105, 4), - error(diag.unusedLocalVariable, 117, 3), - ], - ); - expectInitializerType('foo', 'Future'); +'''); + expectInitializerType('foo =', 'Future'); } test_genericMethod_then_propagatedType() async { // Regression test for https://github.com/dart-lang/sdk/issues/25482. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void main() { Future p; var foo = p.then((r) => new Future.value(3)); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'p' must be assigned before it can be used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'FutureOr?'. } -''', - [ - error(diag.unusedLocalVariable, 40, 3), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 46, 1), - error(diag.argumentTypeNotAssignable, 85, 1), - ], - ); +'''); // Note: this correctly reports the error // CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE when run with the driver; // when run without the driver, it reports no errors. So we don't bother // checking whether the correct errors were reported. - expectInitializerType('foo', 'Future'); + expectInitializerType('foo =', 'Future'); } test_genericMethod_toplevel_field_staticTearoff() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static T g(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'g' because it has a return type of 'T'. static T Function(T) h = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T Function(T)'. } void test() { var fieldRead = C.h; +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'fieldRead' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 37, 4), - error(diag.invalidAssignment, 73, 4), - error(diag.unusedLocalVariable, 102, 9), - ], - ); +'''); _assertLocalVarType('fieldRead', "T Function(T)"); } test_implicitBounds() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} @@ -4772,22 +4918,25 @@ class C, U extends A> {} void test() { A ai; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'ai' isn't used. B bi; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'bi' isn't used. C ci; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'ci' isn't used. var aa = new A(); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'aa' isn't used. var bb = new B(); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'bb' isn't used. var cc = new C(); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'cc' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 116, 2), - error(diag.unusedLocalVariable, 124, 2), - error(diag.unusedLocalVariable, 132, 2), - error(diag.unusedLocalVariable, 142, 2), - error(diag.unusedLocalVariable, 162, 2), - error(diag.unusedLocalVariable, 182, 2), - ], - ); +'''); _assertLocalVarType('ai', "A"); _assertLocalVarType('bi', "B"); _assertLocalVarType('ci', "C, A>"); @@ -4799,26 +4948,15 @@ void test() { test_instantiateToBounds_class_error_extension_malbounded() async { // Test that superclasses are strictly checked for malbounded default // types - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C, T1 extends List> {} class D extends C {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 69, - 1, - contextMessages: [message(testFile, 69, 1)], - ), - error( - diag.typeArgumentNotMatchingBounds, - 69, - 1, - contextMessages: [message(testFile, 69, 1)], - ), - ], - ); +// ^ +// [context 1] The raw type was instantiated as 'C, List>', and is not regular-bounded. +// [context 2] The raw type was instantiated as 'C, List>', and is not regular-bounded. +// [diag.typeArgumentNotMatchingBounds][context 1] 'List' doesn't conform to the bound 'List>' of the type parameter 'T0'. +// [diag.typeArgumentNotMatchingBounds][context 2] 'List' doesn't conform to the bound 'List>' of the type parameter 'T1'. +'''); } test_instantiateToBounds_class_error_instantiation_malbounded() async { @@ -4846,135 +4984,121 @@ void test() { } test_instantiateToBounds_class_error_recursion() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C, T1 extends List> {} C c; -''', - [error(diag.notInitializedNonNullableVariable, 55, 1)], - ); +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C, List>'); } test_instantiateToBounds_class_error_recursion_self() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C> {} C c; -''', - [error(diag.notInitializedNonNullableVariable, 29, 1)], - ); +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C>'); } test_instantiateToBounds_class_error_recursion_self2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class C> {} C c; -''', - [error(diag.notInitializedNonNullableVariable, 43, 1)], - ); +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C>'); } test_instantiateToBounds_class_error_typedef() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' typedef T F(T x); class C> {} C c; -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 48, - 1, - contextMessages: [message(testFile, 48, 1), message(testFile, 48, 1)], - ), - error(diag.notInitializedNonNullableVariable, 50, 1), - ], - ); +// [context 1][column 1][length 1] The raw type was instantiated as 'C', and is not regular-bounded. +// [context 2][column 1][length 1] The inverted type 'C' is also not regular-bounded, so the type is not well-bounded. +// [diag.typeArgumentNotMatchingBounds][column 1][length 1][context 1][context 2] 'F' doesn't conform to the bound 'F>' of the type parameter 'T'. +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C'); } test_instantiateToBounds_class_ok_implicitDynamic_multi() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C, T1 extends List, T2 extends int> {} C c; -''', - [error(diag.notInitializedNonNullableVariable, 70, 1)], - ); +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C, int>, List, int>'); } test_instantiateToBounds_class_ok_referenceOther_after() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C {} C c; -''', - [error(diag.notInitializedNonNullableVariable, 44, 1)], - ); +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C'); } test_instantiateToBounds_class_ok_referenceOther_after2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C, T1 extends int> {} C c; -''', - [error(diag.notInitializedNonNullableVariable, 53, 1)], - ); +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C, int>'); } test_instantiateToBounds_class_ok_referenceOther_before() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C {} C c; -''', - [error(diag.notInitializedNonNullableVariable, 44, 1)], - ); +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C'); } test_instantiateToBounds_class_ok_referenceOther_multi() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C, T1 extends List, T2 extends int> {} C c; -''', - [error(diag.notInitializedNonNullableVariable, 74, 1)], - ); +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. +'''); _assertTopVarType('c', 'C, int>, List, int>'); } test_instantiateToBounds_class_ok_simpleBounds() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class C> {} class D {} void main() { A a; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. B b; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. C c; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'c' isn't used. D d; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'd' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 114, 1), - error(diag.unusedLocalVariable, 121, 1), - error(diag.unusedLocalVariable, 128, 1), - error(diag.unusedLocalVariable, 135, 1), - ], - ); +'''); _assertLocalVarType('a', 'A'); _assertLocalVarType('b', 'B'); _assertLocalVarType('c', 'C>'); @@ -5002,21 +5126,19 @@ void g() { } test_instantiateToBounds_method_ok_referenceOther_before() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { void m>(S0 p0, S1 p1) {} void main() { m(null, null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'T'. +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'List'. } } -''', - [ - error(diag.argumentTypeNotAssignable, 97, 4), - error(diag.argumentTypeNotAssignable, 103, 4), - ], - ); +'''); var node = findNode.singleMethodInvocation; assertResolvedNodeText(node, r''' @@ -5052,18 +5174,17 @@ MethodInvocation } test_instantiateToBounds_method_ok_referenceOther_before2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { Map m>() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'm' because it has a return type of 'Map'. void main() { m(); } } -''', - [error(diag.returnOfInvalidTypeFromMethod, 69, 4)], - ); +'''); var node = findNode.singleMethodInvocation; assertResolvedNodeText(node, r''' @@ -5086,18 +5207,17 @@ MethodInvocation } test_instantiateToBounds_method_ok_simpleBounds() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { void m(S p0) {} void main() { m(null); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'T'. } } -''', - [error(diag.argumentTypeNotAssignable, 67, 4)], - ); +'''); var node = findNode.singleMethodInvocation; assertResolvedNodeText(node, r''' @@ -5126,18 +5246,17 @@ MethodInvocation } test_instantiateToBounds_method_ok_simpleBounds2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { S m() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'm' because it has a return type of 'S'. void main() { m(); } } -''', - [error(diag.returnOfInvalidTypeFromMethod, 37, 4)], - ); +'''); var node = findNode.singleMethodInvocation; assertResolvedNodeText(node, r''' @@ -5159,15 +5278,14 @@ MethodInvocation } test_issue32396() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { static T g(T e) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'g' because it has a return type of 'T'. static final h = g; } -''', - [error(diag.returnOfInvalidTypeFromMethod, 37, 4)], - ); +'''); } test_objectMethodOnFunctions_Anonymous() async { @@ -5346,25 +5464,23 @@ void main() { } test_returnOfInvalidType_object_void() async { - await assertErrorsInCode( - "Object f() { void voidFn() => null; return voidFn(); }", - [error(diag.returnOfInvalidTypeFromFunction, 43, 8)], - ); + await resolveTestCodeWithDiagnostics(r''' +Object f() { void voidFn() => null; return voidFn(); } +// ^^^^^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'void' can't be returned from the function 'f' because it has a return type of 'Object'. +'''); } test_setterWithDynamicTypeIsError() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { dynamic set f(String s) => null; +//^^^^^^^ +// [diag.nonVoidReturnForSetter] The return type of the setter must be 'void' or absent. } dynamic set g(int x) => null; -''', - [ - error(diag.nonVoidReturnForSetter, 12, 7), - error(diag.nonVoidReturnForSetter, 47, 7), - ], - ); +// [diag.nonVoidReturnForSetter][column 1][length 7] The return type of the setter must be 'void' or absent. +'''); } test_setterWithExplicitVoidType_returningVoid() async { @@ -5378,17 +5494,16 @@ void set g(int x) => returnsVoid(); } test_setterWithNoVoidType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { set f(String s) { return '42'; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'String' can't be returned from the function 'f' because it has a return type of 'void'. } } set g(int x) => 42; -''', - [error(diag.returnOfInvalidTypeFromFunction, 41, 4)], - ); +'''); } test_setterWithNoVoidType_returningVoid() async { @@ -5402,44 +5517,45 @@ set g(int x) => returnsVoid(); } test_setterWithOtherTypeIsError() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { String set f(String s) => null; +//^^^^^^ +// [diag.nonVoidReturnForSetter] The return type of the setter must be 'void' or absent. +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'String'. } Object set g(x) => null; -''', - [ - error(diag.nonVoidReturnForSetter, 12, 6), - error(diag.returnOfInvalidTypeFromFunction, 38, 4), - error(diag.nonVoidReturnForSetter, 46, 6), - error(diag.returnOfInvalidTypeFromFunction, 65, 4), - ], - ); +// [diag.nonVoidReturnForSetter][column 1][length 6] The return type of the setter must be 'void' or absent. +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'g' because it has a return type of 'Object'. +'''); } test_ternaryOperator_null_left() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { var foo = (true) ? null : 3; +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. +// ^ +// [diag.deadCode] Dead code. } -''', - [error(diag.unusedLocalVariable, 15, 3), error(diag.deadCode, 37, 1)], - ); - expectInitializerType('foo', 'int?'); +'''); + expectInitializerType('foo =', 'int?'); } test_ternaryOperator_null_right() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { var foo = (true) ? 3 : null; +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. +// ^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.unusedLocalVariable, 15, 3), error(diag.deadCode, 34, 4)], - ); - expectInitializerType('foo', 'int?'); +'''); + expectInitializerType('foo =', 'int?'); } void _assertLocalVarType(String name, String expectedType) { diff --git a/pkg/analyzer/test/src/dart/analysis/driver_test.dart b/pkg/analyzer/test/src/dart/analysis/driver_test.dart index 484a3ed2790..4e605ec4bc3 100644 --- a/pkg/analyzer/test/src/dart/analysis/driver_test.dart +++ b/pkg/analyzer/test/src/dart/analysis/driver_test.dart @@ -20,7 +20,6 @@ import 'package:analyzer/src/dart/analysis/file_state.dart'; import 'package:analyzer/src/dart/analysis/status.dart'; import 'package:analyzer/src/dart/ast/ast.dart'; import 'package:analyzer/src/dart/element/element.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart'; import 'package:analyzer/src/utilities/extensions/async.dart'; import 'package:analyzer_testing/package_config_file_builder.dart'; @@ -3069,13 +3068,12 @@ linter: - omit_local_variable_types '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library my.lib; part 'a.dart'; -''', - [error(diag.uriDoesNotExist, 21, 8)], - ); +// ^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'package:test/a.dart'. +'''); } test_getResolvedUnit_part_empty_lints() async { @@ -3087,13 +3085,12 @@ linter: newFile('$testPackageLibPath/a.dart', ''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library my.lib; part 'a.dart'; -''', - [error(diag.partOfNonPart, 21, 8)], - ); +// ^^^^^^^^ +// [diag.partOfNonPart] The included part 'package:test/a.dart' must have a part-of directive. +'''); } test_getResolvedUnit_part_hasPartOfName_notThisLibrary_lints() async { @@ -3107,13 +3104,12 @@ linter: part of other.lib; '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library my.lib; part 'a.dart'; -''', - [error(diag.partOfDifferentLibrary, 21, 8)], - ); +// ^^^^^^^^ +// [diag.partOfDifferentLibrary] Expected this library to be part of 'my.lib', not 'other.lib'. +'''); } test_getResolvedUnit_part_hasPartOfUri_notThisLibrary_lints() async { @@ -3127,13 +3123,12 @@ linter: part of 'not_test.dart'; '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' library my.lib; part 'a.dart'; -''', - [error(diag.partOfDifferentLibrary, 21, 8)], - ); +// ^^^^^^^^ +// [diag.partOfDifferentLibrary] Expected this library to be part of 'package:test/test.dart', not 'package:test/a.dart'. +'''); } test_getResolvedUnit_part_library() async { diff --git a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart index 84ecd458471..f4a2a342bc3 100644 --- a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart +++ b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart @@ -98,12 +98,11 @@ int 6 } test_declaration_staticError_notAssignable() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' const int x = 'foo'; -''', - [error(diag.invalidAssignment, 14, 5)], - ); +// ^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. +'''); } test_dotShorthand_enum_simple() async { @@ -146,19 +145,17 @@ bool true } test_dotShorthand_equalEqual_constructor_lhsShorthand() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); } const v = .new() == A(); -''', - [ - error(diag.constInitializedWithNonConstantValue, 36, 6), - error(diag.dotShorthandUndefinedInvocation, 37, 3), - ], - ); +// ^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'new' isn't defined for the context type '_'. +'''); } test_dotShorthand_equalEqual_field() async { @@ -178,55 +175,46 @@ bool true } test_dotShorthand_equalEqual_method_error() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { static A method() => A(); } const v = A() == .method(); -''', - [ - error(diag.constWithNonConst, 51, 3), - error(diag.constInitializedWithNonConstantValue, 51, 3), - ], - ); +// ^^^ +// [diag.constWithNonConst] The constructor being called isn't a const constructor. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_dotShorthand_method_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { static A method() => A(); } const A a = .method(); -''', - [error(diag.constEvalMethodInvocation, 52, 9)], - ); +// ^^^^^^^^^ +// [diag.constEvalMethodInvocation] Methods can't be invoked in constant expressions. +'''); } test_dotShorthand_missingContext_invocation() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' const a = .new(); -''', - [ - error(diag.constInitializedWithNonConstantValue, 10, 6), - error(diag.dotShorthandUndefinedInvocation, 11, 3), - ], - ); +// ^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^^^ +// [diag.dotShorthandUndefinedInvocation] The static method or constructor 'new' isn't defined for the context type '_'. +'''); } test_dotShorthand_missingContext_propertyAccess() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' const a = .id; -''', - [ - error(diag.dotShorthandMissingContext, 10, 3), - error(diag.constInitializedWithNonConstantValue, 10, 3), - ], - ); +// ^^^ +// [diag.dotShorthandMissingContext] A dot shorthand can't be used where there is no context type. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_dotShorthand_propertyAccess() async { @@ -264,17 +252,16 @@ E } test_enum_argument_methodInvocation() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' enum E { enumValue(["text"].map((x) => x)); +// ^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constEvalMethodInvocation] Methods can't be invoked in constant expressions. const E(this.strings); final Iterable strings; } -''', - [error(diag.constEvalMethodInvocation, 21, 22)], - ); +'''); } /// Enum constants can reference other constants. @@ -528,27 +515,21 @@ bool false } test_equalEqual_invalidLeft() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' const v = a == 1; -''', - [ - error(diag.undefinedIdentifier, 10, 1), - error(diag.constInitializedWithNonConstantValue, 10, 1), - ], - ); +// ^ +// [diag.undefinedIdentifier] Undefined name 'a'. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_equalEqual_invalidRight() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' const v = 1 == a; -''', - [ - error(diag.undefinedIdentifier, 15, 1), - error(diag.constInitializedWithNonConstantValue, 15, 1), - ], - ); +// ^ +// [diag.undefinedIdentifier] Undefined name 'a'. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_equalEqual_list_matchingTypeArgs_explicit() async { @@ -674,33 +655,31 @@ bool false } test_equalEqual_userClass_hasEqEq() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); bool operator ==(other) => false; } const v = A() == 0; -''', - [error(diag.constEvalPrimitiveEquality, 72, 8)], - ); +// ^^^^^^^^ +// [diag.constEvalPrimitiveEquality] In constant expressions, operands of the equality operator must have primitive equality. +'''); var result = _topLevelVar('v'); _assertNull(result); } test_equalEqual_userClass_hasHashCode() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); int get hashCode => 0; } const v = A() == 0; -''', - [error(diag.constEvalPrimitiveEquality, 61, 8)], - ); +// ^^^^^^^^ +// [diag.constEvalPrimitiveEquality] In constant expressions, operands of the equality operator must have primitive equality. +'''); var result = _topLevelVar('v'); _assertNull(result); } @@ -722,17 +701,16 @@ bool false } test_equalEqual_userClass_hasPrimitiveEquality_language219() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // @dart = 2.19 class A { const A(); } const v = A() == 0; -''', - [error(diag.constEvalTypeBoolNumString, 52, 8)], - ); +// ^^^^^^^^ +// [diag.constEvalTypeBoolNumString] In constant expressions, operands of this operator must be of type 'bool', 'num', 'String' or 'null'. +'''); var result = _topLevelVar('v'); _assertNull(result); } @@ -754,17 +732,16 @@ bool true } test_equalEqual_userClass_noPrimitiveEquality() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); bool operator ==(other) => false; } const v = A() == A(); -''', - [error(diag.constEvalPrimitiveEquality, 72, 10)], - ); +// ^^^^^^^^^^ +// [diag.constEvalPrimitiveEquality] In constant expressions, operands of the equality operator must have primitive equality. +'''); } test_hasPrimitiveEquality_bool() async { @@ -1436,52 +1413,48 @@ bool false } test_typeParameter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(); void m() { const x = X; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. +// ^ +// [diag.constTypeParameter] Type parameters can't be used in a constant expression. } } -''', - [ - error(diag.unusedLocalVariable, 49, 1), - error(diag.constTypeParameter, 53, 1), - ], - ); +'''); var result = _localVar('x'); _assertNull(result); } test_visitBinaryExpression_extensionMethod() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension on Object { int operator +(Object other) => 0; } const Object v1 = 0; const v2 = v1 + v1; -''', - [error(diag.constEvalExtensionMethod, 94, 7)], - ); +// ^^^^^^^ +// [diag.constEvalExtensionMethod] Extension methods can't be used in constant expressions. +'''); var result = _topLevelVar('v2'); _assertNull(result); } test_visitBinaryExpression_extensionType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension type const A(int it) { int operator +(Object other) => 0; } const v1 = A(1); const v2 = v1 + 2; -''', - [error(diag.constEvalExtensionTypeMethod, 101, 6)], - ); +// ^^^^^^ +// [diag.constEvalExtensionTypeMethod] Extension type methods can't be used in constant expressions. +'''); var result = _topLevelVar('v2'); _assertNull(result); } @@ -1557,12 +1530,11 @@ int 0 } test_visitBinaryExpression_gtGtGt_negative_negativeBits() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' const c = 0xFFFFFFFF >>> -2; -''', - [error(diag.constEvalThrowsException, 10, 17)], - ); +// ^^^^^^^^^^^^^^^^^ +// [diag.constEvalThrowsException] Evaluation of this constant expression throws an exception. +'''); var result = _topLevelVar('c'); _assertNull(result); } @@ -1614,12 +1586,11 @@ int 0 } test_visitBinaryExpression_gtGtGt_positive_negativeBits() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' const c = 0xFF >>> -2; -''', - [error(diag.constEvalThrowsException, 10, 11)], - ); +// ^^^^^^^^^^^ +// [diag.constEvalThrowsException] Evaluation of this constant expression throws an exception. +'''); var result = _topLevelVar('c'); _assertNull(result); } @@ -1659,43 +1630,37 @@ bool true } test_visitBinaryExpression_questionQuestion_invalid_notNull() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' final x = 0; const c = x ?? 1; -''', - [ - error(diag.constInitializedWithNonConstantValue, 23, 1), - error(diag.deadCode, 25, 4), - error(diag.deadNullAwareExpression, 28, 1), - ], - ); +// ^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^^^^ +// [diag.deadCode] Dead code. +// ^ +// [diag.deadNullAwareExpression] The left operand can't be null, so the right operand is never executed. +'''); } test_visitBinaryExpression_questionQuestion_notNull_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' final x = 1; const c = 0 ?? x; -''', - [ - error(diag.deadCode, 25, 4), - error(diag.constInitializedWithNonConstantValue, 28, 1), - error(diag.deadNullAwareExpression, 28, 1), - ], - ); +// ^^^^ +// [diag.deadCode] Dead code. +// ^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// [diag.deadNullAwareExpression] The left operand can't be null, so the right operand is never executed. +'''); } test_visitConditionalExpression_eager_invalid_int_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' const c = null ? 1 : 0; -''', - [ - error(diag.nonBoolCondition, 10, 4), - error(diag.constEvalTypeBool, 10, 4), - ], - ); +// ^^^^ +// [diag.nonBoolCondition] Conditions must have a static type of 'bool'. +// [diag.constEvalTypeBool] In constant expressions, operands of this operator must be of type 'bool'. +'''); } test_visitConditionalExpression_instantiatedFunctionType_variable() async { @@ -1730,58 +1695,51 @@ const x = kIsWeb ? 0 : 1; } test_visitConditionalExpression_unknownCondition_errorInConstructor() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const bool kIsWeb = identical(0, 0.0); var a = 2; const x = A(kIsWeb ? 0 : a); +// ^ +// [diag.invalidConstant] Invalid constant value. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. class A { const A(int _); } -''', - [ - error(diag.invalidConstant, 76, 1), - error(diag.constInitializedWithNonConstantValue, 76, 1), - ], - ); +'''); var result = _topLevelVar('x'); _assertNull(result); } test_visitConditionalExpression_unknownCondition_undefinedIdentifier() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const bool kIsWeb = identical(0, 0.0); const x = kIsWeb ? a : b; -''', - [ - error(diag.undefinedIdentifier, 58, 1), - error(diag.constInitializedWithNonConstantValue, 58, 1), - error(diag.undefinedIdentifier, 62, 1), - ], - ); +// ^ +// [diag.undefinedIdentifier] Undefined name 'a'. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^ +// [diag.undefinedIdentifier] Undefined name 'b'. +'''); var result = _topLevelVar('x'); _assertNull(result); } test_visitConstructorDeclaration_cycle() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { final A a; const A() : a = const A(); +// ^ +// [diag.recursiveConstantConstructor] The constant constructor depends on itself. } -''', - [error(diag.recursiveConstantConstructor, 31, 1)], - ); +'''); } test_visitConstructorDeclaration_cycle_subclass_issue46735() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void main() { const EmptyInjector(); } @@ -1790,6 +1748,8 @@ abstract class BaseInjector { final BaseInjector parent; const BaseInjector([BaseInjector? parent]) +// ^^^^^^^^^^^^ +// [diag.recursiveConstantConstructor] The constant constructor depends on itself. : parent = parent ?? const EmptyInjector(); } @@ -1799,26 +1759,22 @@ abstract class Injector implements BaseInjector { class EmptyInjector extends BaseInjector implements Injector { const EmptyInjector(); +// ^^^^^^^^^^^^^ +// [diag.recursiveConstantConstructor] The constant constructor depends on itself. } -''', - [ - error(diag.recursiveConstantConstructor, 110, 12), - error(diag.recursiveConstantConstructor, 344, 13), - ], - ); +'''); } test_visitConstructorDeclaration_field_asExpression_nonConst() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' dynamic y = 2; class A { const A(); +//^^^^^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'x' is initialized with a non-constant value. final x = y as num; } -''', - [error(diag.constConstructorWithFieldInitializedByNonConst, 27, 5)], - ); +'''); } test_visitConstructorReference_generic_named() async { @@ -2165,17 +2121,16 @@ C Function() } test_visitFunctionReference_defaultConstructorValue() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(T t) => t; class C { final void Function(T) p; const C({this.p = f}); +// ^ +// [diag.constWithTypeParametersFunctionTearoff] A constant function tearoff can't use a type parameter as a type argument. } -''', - [error(diag.constWithTypeParametersFunctionTearoff, 83, 1)], - ); +'''); } test_visitFunctionReference_explicitTypeArgs_complexExpression() async { @@ -2228,13 +2183,12 @@ void Function(int) } test_visitFunctionReference_explicitTypeArgs_functionName_notMatchingBound() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(T a) {} const g = f; -''', - [error(diag.typeArgumentNotMatchingBounds, 42, 6)], - ); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. +'''); var result = _topLevelVar('g'); assertDartObjectText(result, r''' void Function(String) @@ -2246,62 +2200,58 @@ void Function(String) } test_visitFunctionReference_explicitTypeArgs_functionName_notType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void foo(T a) {} const g = foo; -''', - [ - error(diag.constEvalTypeNum, 30, 8), - error(diag.undefinedOperator, 33, 1), - error(diag.equalityCannotBeEqualityOperand, 38, 1), - error(diag.missingIdentifier, 39, 1), - ], - ); +// ^^^^^^^^ +// [diag.constEvalTypeNum] In constant expressions, operands of this operator must be of type 'num'. +// ^ +// [diag.undefinedOperator] The operator '<' isn't defined for the type 'void Function(T)'. +// ^ +// [diag.equalityCannotBeEqualityOperand] A comparison expression can't be an operand of another comparison expression. +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); var result = _topLevelVar('g'); _assertNull(result); } test_visitFunctionReference_explicitTypeArgs_functionName_tooFew() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void foo(T a, U b) {} const g = foo; -''', - [error(diag.wrongNumberOfTypeArgumentsElement, 41, 5)], - ); +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsElement] The function 'foo' is declared with 2 type parameters, but 1 type arguments are given. +'''); var result = _topLevelVar('g'); _assertNull(result); } test_visitFunctionReference_explicitTypeArgs_functionName_tooMany() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void foo(T a) {} const g = foo; -''', - [error(diag.wrongNumberOfTypeArgumentsElement, 33, 13)], - ); +// ^^^^^^^^^^^^^ +// [diag.wrongNumberOfTypeArgumentsElement] The function 'foo' is declared with 1 type parameters, but 2 type arguments are given. +'''); var result = _topLevelVar('g'); _assertNull(result); } test_visitFunctionReference_explicitTypeArgs_functionName_typeParameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(T a) {} class C { void m() { const g = f; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'g' isn't used. +// ^ +// [diag.constWithTypeParametersFunctionTearoff] A constant function tearoff can't use a type parameter as a type argument. } } -''', - [ - error(diag.unusedLocalVariable, 55, 1), - error(diag.constWithTypeParametersFunctionTearoff, 61, 1), - ], - ); +'''); } test_visitFunctionReference_explicitTypeArgs_identical_differentElements() async { @@ -2537,20 +2487,18 @@ bool true } test_visitFunctionReference_wildcard_local() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' test() { void _() {} +//^^^^^^^^^^^ +// [diag.deadCode] Dead code. const c = _; +// ^ +// [diag.undefinedIdentifier] Undefined name '_'. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. print(c); } -''', - [ - error(diag.deadCode, 11, 11), - error(diag.undefinedIdentifier, 35, 1), - error(diag.constInitializedWithNonConstantValue, 35, 1), - ], - ); +'''); } test_visitFunctionReference_wildcard_top() async { @@ -2561,23 +2509,20 @@ const c = _; } test_visitInstanceCreationExpression_invalidNamedArg() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A({ required int x }); } const a = A(x: false); -''', - [ - error(diag.argumentTypeNotAssignable, 58, 5), - error(diag.constConstructorParamTypeMismatch, 55, 8), - ], - ); +// ^^^^^^^^ +// [diag.constConstructorParamTypeMismatch] A value of type 'bool' can't be assigned to a parameter of type 'int' in a const constructor. +// ^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'bool' can't be assigned to the parameter type 'int'. +'''); } test_visitInstanceCreationExpression_invalidNamedArg_superParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A({ required int x }); } @@ -2585,32 +2530,27 @@ class B extends A { const B({ required super.x }); } const a = B(x: false); -''', - [ - error(diag.argumentTypeNotAssignable, 113, 5), - error(diag.constConstructorParamTypeMismatch, 110, 8), - ], - ); +// ^^^^^^^^ +// [diag.constConstructorParamTypeMismatch] A value of type 'bool' can't be assigned to a parameter of type 'int' in a const constructor. +// ^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'bool' can't be assigned to the parameter type 'int'. +'''); } test_visitInstanceCreationExpression_invalidPositionalArg() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(int x); } const a = A(false); -''', - [ - error(diag.argumentTypeNotAssignable, 42, 5), - error(diag.constConstructorParamTypeMismatch, 42, 5), - ], - ); +// ^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'bool' can't be assigned to the parameter type 'int'. +// [diag.constConstructorParamTypeMismatch] A value of type 'bool' can't be assigned to a parameter of type 'int' in a const constructor. +'''); } test_visitInstanceCreationExpression_invalidPositionalArg_superParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(int x); } @@ -2618,29 +2558,25 @@ class B extends A { const B(super.x); } const a = B(false); -''', - [ - error(diag.argumentTypeNotAssignable, 84, 5), - error(diag.constConstructorParamTypeMismatch, 84, 5), - ], - ); +// ^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'bool' can't be assigned to the parameter type 'int'. +// [diag.constConstructorParamTypeMismatch] A value of type 'bool' can't be assigned to a parameter of type 'int' in a const constructor. +'''); } test_visitInstanceCreationExpression_missingNamedArg() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A({required int x }); } const a = A(); -''', - [error(diag.missingRequiredArgument, 52, 1)], - ); +// ^ +// [diag.missingRequiredArgument] The named parameter 'x' is required, but there's no corresponding argument. +'''); } test_visitInstanceCreationExpression_missingNamedArg_superParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A({required int x }); } @@ -2648,26 +2584,24 @@ class B extends A { const B({required super.x }); } const a = B(); -''', - [error(diag.missingRequiredArgument, 106, 1)], - ); +// ^ +// [diag.missingRequiredArgument] The named parameter 'x' is required, but there's no corresponding argument. +'''); } test_visitInstanceCreationExpression_missingPositionalArg() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(int x); } const a = A(); -''', - [error(diag.notEnoughPositionalArgumentsNameSingular, 42, 1)], - ); +// ^ +// [diag.notEnoughPositionalArgumentsNameSingular] 1 positional argument expected by 'A.new', but 0 found. +'''); } test_visitInstanceCreationExpression_missingPositionalArg_superParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(int x); } @@ -2675,9 +2609,9 @@ class B extends A { const B(super.x); } const a = B(); -''', - [error(diag.notEnoughPositionalArgumentsNameSingular, 84, 1)], - ); +// ^ +// [diag.notEnoughPositionalArgumentsNameSingular] 1 positional argument expected by 'B.new', but 0 found. +'''); } test_visitInstanceCreationExpression_noArgs() async { @@ -2697,16 +2631,13 @@ A } test_visitInstanceCreationExpression_noConstConstructor() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A {} const a = A(); -''', - [ - error(diag.constWithNonConst, 21, 3), - error(diag.constInitializedWithNonConstantValue, 21, 3), - ], - ); +// ^^^ +// [diag.constWithNonConst] The constructor being called isn't a const constructor. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitInstanceCreationExpression_simpleArgs() async { @@ -2728,41 +2659,37 @@ A } test_visitInstanceCreationExpression_unknown() async { - await assertErrorsInCode( - ''' + // TODO(kallentu): This should not be reported. + // https://github.com/dart-lang/sdk/issues/50441 + await resolveTestCodeWithDiagnostics(r''' class C { const C.named(); } const x = C.(); -''', - [ - // TODO(kallentu): This should not be reported. - // https://github.com/dart-lang/sdk/issues/50441 - error(diag.classInstantiationAccessToUnknownMember, 45, 8), - error(diag.constInitializedWithNonConstantValue, 45, 8), - error(diag.missingIdentifier, 52, 1), - ], - ); +// ^^^^^^^^ +// [diag.classInstantiationAccessToUnknownMember] The class 'C' doesn't have a constructor named '('. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^ +// [diag.missingIdentifier] Expected an identifier. +'''); } test_visitInterpolationExpression_list() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const x = '${const [2]}'; -''', - [error(diag.constEvalTypeBoolNumString, 11, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.constEvalTypeBoolNumString] In constant expressions, operands of this operator must be of type 'bool', 'num', 'String' or 'null'. +'''); } test_visitIsExpression_is_functionType_correctTypes() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' void foo(int a) {} const c = foo is void Function(int); -''', - [error(diag.unnecessaryTypeCheckTrue, 29, 25)], - ); +// ^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'. +'''); var result = _topLevelVar('c'); assertDartObjectText(result, r''' bool true @@ -2771,16 +2698,15 @@ bool true } test_visitIsExpression_is_instanceOfSameClass() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = const A(); const b = a is A; +// ^^^^^^ +// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'. class A { const A(); } -''', - [error(diag.unnecessaryTypeCheckTrue, 31, 6)], - ); +'''); var result = _topLevelVar('b'); assertDartObjectText(result, r''' bool true @@ -2789,19 +2715,18 @@ bool true } test_visitIsExpression_is_instanceOfSubclass() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = const B(); const b = a is A; +// ^^^^^^ +// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'. class A { const A(); } class B extends A { const B(); } -''', - [error(diag.unnecessaryTypeCheckTrue, 31, 6)], - ); +'''); var result = _topLevelVar('b'); assertDartObjectText(result, r''' bool true @@ -2848,16 +2773,15 @@ bool false } test_visitIsExpression_isNot_instanceOfSameClass() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = const A(); const b = a is! A; +// ^^^^^^^ +// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'. class A { const A(); } -''', - [error(diag.unnecessaryTypeCheckFalse, 31, 7)], - ); +'''); var result = _topLevelVar('b'); assertDartObjectText(result, r''' bool false @@ -2866,19 +2790,18 @@ bool false } test_visitIsExpression_isNot_instanceOfSubclass() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = const B(); const b = a is! A; +// ^^^^^^^ +// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'. class A { const A(); } class B extends A { const B(); } -''', - [error(diag.unnecessaryTypeCheckFalse, 31, 7)], - ); +'''); var result = _topLevelVar('b'); assertDartObjectText(result, r''' bool false @@ -2900,38 +2823,34 @@ bool true } test_visitListLiteral_forElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const x = [for (int i = 0; i < 3; i++) i]; -''', - [ - error(diag.constInitializedWithNonConstantValue, 10, 31), - error(diag.constEvalForElement, 11, 29), - ], - ); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constEvalForElement] Constant expressions don't support 'for' elements. +'''); var result = _topLevelVar('x'); _assertNull(result); } test_visitListLiteral_ifElement_nonBoolCondition() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const dynamic c = 2; const x = [1, if (c) 2 else 3, 4]; -''', - [error(diag.nonBoolCondition, 39, 1)], - ); +// ^ +// [diag.nonBoolCondition] Conditions must have a static type of 'bool'. +'''); var result = _topLevelVar('x'); _assertNull(result); } test_visitListLiteral_ifElement_nonBoolCondition_static() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const x = [1, if (1) 2 else 3, 4]; -''', - [error(diag.nonBoolCondition, 18, 1)], - ); +// ^ +// [diag.nonBoolCondition] Conditions must have a static type of 'bool'. +'''); var result = _topLevelVar('x'); _assertNull(result); } @@ -2965,16 +2884,15 @@ List } test_visitListLiteral_listElement_field_final() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { final String bar = ''; const A(); List foo() => const [bar]; +// ^^^ +// [diag.nonConstantListElement] The values in a const list literal must be constants. } -''', - [error(diag.nonConstantListElement, 79, 3)], - ); +'''); } test_visitListLiteral_listElement_field_static() async { @@ -3024,13 +2942,12 @@ List } test_visitListLiteral_spreadElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const dynamic a = 5; const x = [...a]; -''', - [error(diag.constSpreadExpectedListOrSet, 40, 1)], - ); +// ^ +// [diag.constSpreadExpectedListOrSet] A list or a set is expected in this spread. +'''); var result = _topLevelVar('x'); _assertNull(result); } @@ -3073,37 +2990,34 @@ List } test_visitMethodInvocation_notIdentical() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int f() { return 3; } const a = f(); -''', - [error(diag.constEvalMethodInvocation, 34, 3)], - ); +// ^^^ +// [diag.constEvalMethodInvocation] Methods can't be invoked in constant expressions. +'''); } test_visitNamedType_typeLiteral_typeParameter_nested() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case const (T)) {} +// ^ +// [diag.constTypeParameter] Type parameters can't be used in a constant expression. } -''', - [error(diag.constTypeParameter, 43, 1)], - ); +'''); } test_visitNamedType_typeLiteral_typeParameter_nested2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case const (List)) {} +// ^^^^^^^ +// [diag.constTypeParameter] Type parameters can't be used in a constant expression. } -''', - [error(diag.constTypeParameter, 43, 7)], - ); +'''); } test_visitPrefixedIdentifier_function() async { @@ -3187,34 +3101,19 @@ void Function(T) } test_visitPrefixedIdentifier_length_invalidTarget() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void main() { const RequiresNonEmptyList([1]); +//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constEvalPropertyAccess][context 1] The property 'length' can't be accessed on the type 'List' in a constant expression. } class RequiresNonEmptyList { const RequiresNonEmptyList(List numbers) : assert(numbers.length > 0); +// ^^^^^^^^^^^^^^ +// [context 1] The error is in the assert initializer of 'RequiresNonEmptyList', and occurs here. } -''', - [ - error( - diag.constEvalPropertyAccess, - 16, - 31, - contextMessages: [ - contextMessage( - testFile, - 138, - 14, - textContains: [ - "The error is in the assert initializer of 'RequiresNonEmptyList', and occurs here.", - ], - ), - ], - ), - ], - ); +'''); } test_visitPrefixExpression_bitNot() async { @@ -3229,33 +3128,31 @@ int -43 } test_visitPrefixExpression_extensionMethod() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' extension on Object { int operator -() => 0; } const Object v1 = 1; const v2 = -v1; -''', - [error(diag.constEvalExtensionMethod, 82, 3)], - ); +// ^^^ +// [diag.constEvalExtensionMethod] Extension methods can't be used in constant expressions. +'''); var result = _topLevelVar('v2'); _assertNull(result); } test_visitPrefixExpression_extensionType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' extension type const A(int it) { int operator -() => 0; } const v1 = A(1); const v2 = -v1; -''', - [error(diag.constEvalExtensionTypeMethod, 89, 3)], - ); +// ^^^ +// [diag.constEvalExtensionTypeMethod] Extension type methods can't be used in constant expressions. +'''); var result = _topLevelVar('v2'); _assertNull(result); } @@ -3286,15 +3183,13 @@ bool false } test_visitPrefixExpression_negated_bool() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = -true; -''', - [ - error(diag.undefinedOperator, 10, 1), - error(diag.constEvalTypeNum, 10, 5), - ], - ); +// ^ +// [diag.undefinedOperator] The operator 'unary-' isn't defined for the type 'bool'. +// ^^^^^ +// [diag.constEvalTypeNum] In constant expressions, operands of this operator must be of type 'num'. +'''); } test_visitPrefixExpression_negated_double() async { @@ -3447,13 +3342,12 @@ Record({int f1, int f2}) } test_visitRecordLiteral_namedField_final() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final bar = ''; ({String bar, }) foo() => const (bar: bar, ); -''', - [error(diag.nonConstantRecordField, 54, 3)], - ); +// ^^^ +// [diag.nonConstantRecordField] The fields in a const record literal must be constants. +'''); } test_visitRecordLiteral_objectField_generic() async { @@ -3498,13 +3392,12 @@ Record(int, int, int) } test_visitRecordLiteral_positionalField_final() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final bar = ''; (String, ) foo() => const (bar, ); -''', - [error(diag.nonConstantRecordField, 43, 3)], - ); +// ^^^ +// [diag.nonConstantRecordField] The fields in a const record literal must be constants. +'''); } test_visitRecordLiteral_withoutEnvironment() async { @@ -3524,44 +3417,40 @@ Record(int, String, {bool c}) } test_visitSetOrMapLiteral_ambiguous() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const l = []; const ambiguous = {...l, 1: 2}; -''', - [error(diag.ambiguousSetOrMapLiteralBoth, 32, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.ambiguousSetOrMapLiteralBoth] The literal can't be either a map or a set because it contains at least one literal map entry or a spread operator spreading a 'Map', and at least one element which is neither of these. +'''); } test_visitSetOrMapLiteral_ambiguous_either() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const int? i = 1; const res = {...?i}; -''', - [error(diag.ambiguousSetOrMapLiteralEither, 31, 7)], - ); +// ^^^^^^^ +// [diag.ambiguousSetOrMapLiteralEither] This literal must be either a map or a set, but the elements don't have enough information for type inference to work. +'''); } test_visitSetOrMapLiteral_ambiguous_expression() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const m = {1: 1}; const res = {...m, 2}; -''', - [error(diag.ambiguousSetOrMapLiteralBoth, 30, 9)], - ); +// ^^^^^^^^^ +// [diag.ambiguousSetOrMapLiteralBoth] The literal can't be either a map or a set because it contains at least one literal map entry or a spread operator spreading a 'Map', and at least one element which is neither of these. +'''); } test_visitSetOrMapLiteral_ambiguous_inList() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const l = []; const ambiguous = {...l, 1: 2}; +// ^^^^^^^^^^^^ +// [diag.ambiguousSetOrMapLiteralBoth] The literal can't be either a map or a set because it contains at least one literal map entry or a spread operator spreading a 'Map', and at least one element which is neither of these. const anotherList = [...ambiguous]; -''', - [error(diag.ambiguousSetOrMapLiteralBoth, 32, 12)], - ); +'''); } test_visitSetOrMapLiteral_map_complexKey() async { @@ -3594,41 +3483,36 @@ Map } test_visitSetOrMapLiteral_map_forElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const x = {1: null, for (final i in const []) i: null}; -''', - [ - error(diag.constInitializedWithNonConstantValue, 10, 44), - error(diag.constEvalForElement, 20, 33), - ], - ); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constEvalForElement] Constant expressions don't support 'for' elements. +'''); var result = _topLevelVar('x'); _assertNull(result); } test_visitSetOrMapLiteral_map_forElement_nested() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const x = {1: null, if (true) for (final i in const []) i: null}; -''', - [ - error(diag.constInitializedWithNonConstantValue, 10, 54), - error(diag.constEvalForElement, 30, 33), - ], - ); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constEvalForElement] Constant expressions don't support 'for' elements. +'''); var result = _topLevelVar('x'); _assertNull(result); } test_visitSetOrMapLiteral_map_ifElement_nonBoolCondition() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const dynamic nonBool = null; const c = const {if (nonBool) 'a' : 1}; -''', - [error(diag.nonBoolCondition, 51, 7)], - ); +// ^^^^^^^ +// [diag.nonBoolCondition] Conditions must have a static type of 'bool'. +'''); var result = _topLevelVar('c'); _assertNull(result); } @@ -3674,19 +3558,16 @@ Map } test_visitSetOrMapLiteral_map_spread_notMap() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const x = ['string']; const Map alwaysInclude = { 'anotherString': 0, ...x, +// ^ +// [diag.constSpreadExpectedMap] A map is expected in this spread. +// [diag.notMapSpread] Spread elements in map literals must implement 'Map'. }; -''', - [ - error(diag.constSpreadExpectedMap, 90, 1), - error(diag.notMapSpread, 90, 1), - ], - ); +'''); } test_visitSetOrMapLiteral_map_spread_null() async { @@ -3743,28 +3624,25 @@ Set } test_visitSetOrMapLiteral_set_forElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const Set set = {}; const x = {for (final i in set) i}; -''', - [ - error(diag.constInitializedWithNonConstantValue, 30, 24), - error(diag.constEvalForElement, 31, 22), - ], - ); +// ^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^^^^^^^^^^^^^^^^^^^^^^ +// [diag.constEvalForElement] Constant expressions don't support 'for' elements. +'''); var result = _topLevelVar('x'); _assertNull(result); } test_visitSetOrMapLiteral_set_ifElement_nonBoolCondition() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const dynamic nonBool = 'a'; const c = const {if (nonBool) 3}; -''', - [error(diag.nonBoolCondition, 50, 7)], - ); +// ^^^^^^^ +// [diag.nonBoolCondition] Conditions must have a static type of 'bool'. +'''); var result = _topLevelVar('c'); _assertNull(result); } @@ -3981,17 +3859,16 @@ void Function(int, {int? b}) } test_visitUnaryExpression_extensionType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' extension type const A(int it) { int operator -() => 0; } const v1 = A(1); const v2 = -v1; -''', - [error(diag.constEvalExtensionTypeMethod, 89, 3)], - ); +// ^^^ +// [diag.constEvalExtensionTypeMethod] Extension type methods can't be used in constant expressions. +'''); var result = _topLevelVar('v2'); _assertNull(result); } @@ -4271,37 +4148,35 @@ class B extends A { } test_visitAsExpression_instanceOfSuperclass() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = const A(); const b = a as B; +// ^^^^^^ +// [diag.constEvalThrowsException] Evaluation of this constant expression throws an exception. class A { const A(); } class B extends A { const B(); } -''', - [error(diag.constEvalThrowsException, 31, 6)], - ); +'''); var result = _topLevelVar('b'); _assertNull(result); } test_visitAsExpression_instanceOfUnrelatedClass() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = const A(); const b = a as B; +// ^^^^^^ +// [diag.constEvalThrowsException] Evaluation of this constant expression throws an exception. class A { const A(); } class B { const B(); } -''', - [error(diag.constEvalThrowsException, 31, 6)], - ); +'''); var result = _topLevelVar('b'); _assertNull(result); } @@ -4331,20 +4206,18 @@ double 5.5 } test_visitBinaryExpression_add_instance_String() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { const C(); String operator +(String other) => other; } const c = C() + 1; -''', - [ - error(diag.constEvalTypeNumString, 80, 7), - error(diag.argumentTypeNotAssignable, 86, 1), - ], - ); +// ^^^^^^^ +// [diag.constEvalTypeNumString] In constant expressions, operands of this operator must be of type 'num' or 'String'. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. +'''); } test_visitBinaryExpression_add_int_int() async { @@ -4381,36 +4254,32 @@ bool false } test_visitBinaryExpression_and_bool_false_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' final a = false; const c = false && a; -''', - [ - error(diag.deadCode, 33, 4), - error(diag.constInitializedWithNonConstantValue, 36, 1), - ], - ); +// ^^^^ +// [diag.deadCode] Dead code. +// ^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_and_bool_invalid_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' final a = false; const c = a && false; -''', - [error(diag.constInitializedWithNonConstantValue, 27, 1)], - ); +// ^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_and_bool_invalid_true() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' final a = false; const c = a && true; -''', - [error(diag.constInitializedWithNonConstantValue, 27, 1)], - ); +// ^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_and_bool_known_known() async { @@ -4431,13 +4300,12 @@ const c = false & b; } test_visitBinaryExpression_and_bool_true_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' final a = false; const c = true && a; -''', - [error(diag.constInitializedWithNonConstantValue, 27, 9)], - ); +// ^^^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_and_bool_unknown_known() async { @@ -4471,15 +4339,13 @@ int 10 } test_visitBinaryExpression_and_mixed() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = 3 & false; -''', - [ - error(diag.constEvalTypeBoolInt, 10, 9), - error(diag.argumentTypeNotAssignable, 14, 5), - ], - ); +// ^^^^^^^^^ +// [diag.constEvalTypeBoolInt] In constant expressions, operands of this operator must be of type 'bool' or 'int'. +// ^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'bool' can't be assigned to the parameter type 'int'. +'''); } test_visitBinaryExpression_divide_double_double() async { @@ -4527,12 +4393,11 @@ double Infinity } test_visitBinaryExpression_eqeq_double_double_nan_left() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = double.nan == 2.3; -''', - [error(diag.unnecessaryNanComparisonFalse, 10, 13)], - ); +// ^^^^^^^^^^^^^ +// [diag.unnecessaryNanComparisonFalse] A double can't equal 'double.nan', so the condition is always 'false'. +'''); // This test case produces a warning, but the value of the constant should // be `false`. var result = _topLevelVar('c'); @@ -4543,12 +4408,11 @@ bool false } test_visitBinaryExpression_eqeq_double_double_nan_right() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = 2.3 == double.nan; -''', - [error(diag.unnecessaryNanComparisonFalse, 14, 13)], - ); +// ^^^^^^^^^^^^^ +// [diag.unnecessaryNanComparisonFalse] A double can't equal 'double.nan', so the condition is always 'false'. +'''); // This test case produces a warning, but the value of the constant should // be `false`. var result = _topLevelVar('c'); @@ -4603,27 +4467,21 @@ bool true } test_visitBinaryExpression_notEqual_invalidLeft() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = a != 3; -''', - [ - error(diag.undefinedIdentifier, 10, 1), - error(diag.constInitializedWithNonConstantValue, 10, 1), - ], - ); +// ^ +// [diag.undefinedIdentifier] Undefined name 'a'. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_notEqual_invalidRight() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = 2 != a; -''', - [ - error(diag.undefinedIdentifier, 15, 1), - error(diag.constInitializedWithNonConstantValue, 15, 1), - ], - ); +// ^ +// [diag.undefinedIdentifier] Undefined name 'a'. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_notEqual_string_string() async { @@ -4638,33 +4496,30 @@ bool true } test_visitBinaryExpression_or_bool_false_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' final a = false; const c = false || a; -''', - [error(diag.constInitializedWithNonConstantValue, 27, 10)], - ); +// ^^^^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_or_bool_invalid_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' final a = false; const c = a || false; -''', - [error(diag.constInitializedWithNonConstantValue, 27, 1)], - ); +// ^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_or_bool_invalid_true() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' final a = false; const c = a || true; -''', - [error(diag.constInitializedWithNonConstantValue, 27, 1)], - ); +// ^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_or_bool_known_known() async { @@ -4685,16 +4540,14 @@ const c = false | b; } test_visitBinaryExpression_or_bool_true_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' final a = false; const c = true || a; -''', - [ - error(diag.deadCode, 32, 4), - error(diag.constInitializedWithNonConstantValue, 35, 1), - ], - ); +// ^^^^ +// [diag.deadCode] Dead code. +// ^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitBinaryExpression_or_bool_unknown_known() async { @@ -4725,12 +4578,11 @@ const c = 3 | 5; } test_visitBinaryExpression_or_known_known() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = true || false; -''', - [error(diag.deadCode, 15, 8)], - ); +// ^^^^^^^^ +// [diag.deadCode] Dead code. +'''); var result = _topLevelVar('c'); assertDartObjectText(result, r''' bool true @@ -4739,15 +4591,13 @@ bool true } test_visitBinaryExpression_or_mixed() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = 3 | false; -''', - [ - error(diag.constEvalTypeBoolInt, 10, 9), - error(diag.argumentTypeNotAssignable, 14, 5), - ], - ); +// ^^^^^^^^^ +// [diag.constEvalTypeBoolInt] In constant expressions, operands of this operator must be of type 'bool' or 'int'. +// ^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'bool' can't be assigned to the parameter type 'int'. +'''); } test_visitBinaryExpression_questionQuestion_notNull_notNull() async { @@ -4760,13 +4610,12 @@ const c = 'a' ?? 'b'; } test_visitBinaryExpression_questionQuestion_null_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = null ?? new C(); +// ^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. class C {} -''', - [error(diag.constInitializedWithNonConstantValue, 18, 7)], - ); +'''); } test_visitBinaryExpression_questionQuestion_null_notNull() async { @@ -4831,15 +4680,13 @@ const c = 3 ^ 5; } test_visitBinaryExpression_xor_mixed() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = 3 ^ false; -''', - [ - error(diag.constEvalTypeBoolInt, 10, 9), - error(diag.argumentTypeNotAssignable, 14, 5), - ], - ); +// ^^^^^^^^^ +// [diag.constEvalTypeBoolInt] In constant expressions, operands of this operator must be of type 'bool' or 'int'. +// ^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'bool' can't be assigned to the parameter type 'int'. +'''); } test_visitBoolLiteral_false() async { @@ -4867,12 +4714,11 @@ bool true } test_visitConditionalExpression_eager_false_int_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = false ? 1 : 0; -''', - [error(diag.deadCode, 18, 1)], - ); +// ^ +// [diag.deadCode] Dead code. +'''); var result = _topLevelVar('c'); assertDartObjectText(result, r''' int 0 @@ -4881,12 +4727,11 @@ int 0 } test_visitConditionalExpression_eager_true_int_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = true ? 1 : 0; -''', - [error(diag.deadCode, 21, 1)], - ); +// ^ +// [diag.deadCode] Dead code. +'''); var result = _topLevelVar('c'); assertDartObjectText(result, r''' int 1 @@ -4895,38 +4740,32 @@ int 1 } test_visitConditionalExpression_eager_true_int_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = true ? 1 : x; -''', - [ - error(diag.undefinedIdentifier, 21, 1), - error(diag.deadCode, 21, 1), - error(diag.constInitializedWithNonConstantValue, 21, 1), - ], - ); +// ^ +// [diag.undefinedIdentifier] Undefined name 'x'. +// [diag.deadCode] Dead code. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitConditionalExpression_eager_true_invalid_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = true ? x : 0; -''', - [ - error(diag.undefinedIdentifier, 17, 1), - error(diag.constInitializedWithNonConstantValue, 17, 1), - error(diag.deadCode, 21, 1), - ], - ); +// ^ +// [diag.undefinedIdentifier] Undefined name 'x'. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^ +// [diag.deadCode] Dead code. +'''); } test_visitConditionalExpression_lazy_false_int_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = false ? 1 : 0; -''', - [error(diag.deadCode, 18, 1)], - ); +// ^ +// [diag.deadCode] Dead code. +'''); var result = _topLevelVar('c'); assertDartObjectText(result, r''' int 0 @@ -4935,50 +4774,43 @@ int 0 } test_visitConditionalExpression_lazy_false_int_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = false ? 1 : new C(); -''', - [ - error(diag.deadCode, 18, 1), - error(diag.constInitializedWithNonConstantValue, 22, 7), - error(diag.newWithNonType, 26, 1), - ], - ); +// ^ +// [diag.deadCode] Dead code. +// ^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^ +// [diag.newWithNonType] The name 'C' isn't a class. +'''); } test_visitConditionalExpression_lazy_false_invalid_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = false ? new C() : 0; -''', - [ - error(diag.deadCode, 18, 7), - error(diag.constInitializedWithNonConstantValue, 18, 7), - error(diag.newWithNonType, 22, 1), - ], - ); +// ^^^^^^^ +// [diag.deadCode] Dead code. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^ +// [diag.newWithNonType] The name 'C' isn't a class. +'''); } test_visitConditionalExpression_lazy_invalid_int_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = 3 ? 1 : 0; -''', - [ - error(diag.nonBoolCondition, 10, 1), - error(diag.constEvalTypeBool, 10, 1), - ], - ); +// ^ +// [diag.nonBoolCondition] Conditions must have a static type of 'bool'. +// [diag.constEvalTypeBool] In constant expressions, operands of this operator must be of type 'bool'. +'''); } test_visitConditionalExpression_lazy_true_int_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = true ? 1 : 0; -''', - [error(diag.deadCode, 21, 1)], - ); +// ^ +// [diag.deadCode] Dead code. +'''); var result = _topLevelVar('c'); assertDartObjectText(result, r''' int 1 @@ -4987,47 +4819,41 @@ int 1 } test_visitConditionalExpression_lazy_true_int_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = true ? 1: new C(); -''', - [ - error(diag.deadCode, 20, 7), - error(diag.constInitializedWithNonConstantValue, 20, 7), - error(diag.newWithNonType, 24, 1), - ], - ); +// ^^^^^^^ +// [diag.deadCode] Dead code. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^ +// [diag.newWithNonType] The name 'C' isn't a class. +'''); } test_visitConditionalExpression_lazy_true_invalid_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = true ? new C() : 0; +// ^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// ^ +// [diag.deadCode] Dead code. class C {} -''', - [ - error(diag.constInitializedWithNonConstantValue, 17, 7), - error(diag.deadCode, 27, 1), - ], - ); +'''); } test_visitConditionalExpression_lazy_unknown_int_invalid() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = identical(0, 0.0) ? 1 : new Object(); -''', - [error(diag.constInitializedWithNonConstantValue, 34, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitConditionalExpression_lazy_unknown_invalid_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const c = identical(0, 0.0) ? 1 : new Object(); -''', - [error(diag.constInitializedWithNonConstantValue, 34, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitDoubleLiteral() async { @@ -5121,14 +4947,13 @@ bool false } test_visitIsExpression_is_null_dynamic() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = null; const b = a is dynamic; +// ^^^^^^^^^^^^ +// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'. class A {} -''', - [error(diag.unnecessaryTypeCheckTrue, 26, 12)], - ); +'''); var result = _topLevelVar('b'); assertDartObjectText(result, r''' bool true @@ -5137,14 +4962,13 @@ bool true } test_visitIsExpression_is_null_null() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = null; const b = a is Null; +// ^^^^^^^^^ +// [diag.typeCheckIsNull] Tests for null should be done with '== null'. class A {} -''', - [error(diag.typeCheckIsNull, 26, 9)], - ); +'''); var result = _topLevelVar('b'); assertDartObjectText(result, r''' bool true @@ -5231,8 +5055,7 @@ int 42 } test_visitPropertyAccess_length_extension() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension ExtObject on Object { int get length => 4; } @@ -5240,42 +5063,27 @@ extension ExtObject on Object { class B { final l; const B(Object o) : l = o.length; +// ^^^^^^^^ +// [context 1] The error is in the field initializer of 'B', and occurs here. } const b = B(''); -''', - [ - error( - diag.constEvalExtensionMethod, - 128, - 5, - contextMessages: [ - contextMessage( - testFile, - 105, - 8, - textContains: [ - "The error is in the field initializer of 'B', and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^ +// [diag.constEvalExtensionMethod][context 1] Extension methods can't be used in constant expressions. +'''); } test_visitPropertyAccess_length_extensionType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' extension type const A(String it) { int get length => 0; } const v1 = A(''); const v2 = v1.length; -''', - [error(diag.constEvalExtensionTypeMethod, 91, 9)], - ); +// ^^^^^^^^^ +// [diag.constEvalExtensionTypeMethod] Extension type methods can't be used in constant expressions. +'''); var result = _topLevelVar('v2'); _assertNull(result); } @@ -5295,35 +5103,21 @@ int 3 } test_visitPropertyAccess_length_unresolvedType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class B { final l; const B(String o) : l = o.length; +// ^^^^^^^^ +// [context 1] The error is in the field initializer of 'B', and occurs here. } const y = B(x); -''', - [ - error( - diag.constEvalTypeString, - 70, - 4, - contextMessages: [ - contextMessage( - testFile, - 47, - 8, - textContains: [ - "The error is in the field initializer of 'B', and occurs here.", - ], - ), - ], - ), - error(diag.undefinedIdentifier, 72, 1), - error(diag.constWithNonConstantArgument, 72, 1), - ], - ); +// ^^^^ +// [diag.constEvalTypeString][context 1] In constant expressions, operands of this operator must be of type 'String'. +// ^ +// [diag.undefinedIdentifier] Undefined name 'x'. +// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions. +'''); } test_visitSimpleIdentifier_dynamic() async { @@ -5348,19 +5142,17 @@ int 42 } test_visitSimpleIdentifier_wildcard_local() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' test() { const _ = true; const c = _; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'c' isn't used. +// ^ +// [diag.undefinedIdentifier] Undefined name '_'. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. } -''', - [ - error(diag.unusedLocalVariable, 35, 1, messageContainsAll: ["'c'"]), - error(diag.undefinedIdentifier, 39, 1), - error(diag.constInitializedWithNonConstantValue, 39, 1), - ], - ); +'''); } test_visitSimpleIdentifier_wildcard_top() async { @@ -5393,15 +5185,13 @@ String abc } test_visitStringInterpolation_invalid() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' const c = 'a${f()}c'; -''', - [ - error(diag.undefinedFunction, 14, 1), - error(diag.constInitializedWithNonConstantValue, 14, 3), - ], - ); +// ^ +// [diag.undefinedFunction] The function 'f' isn't defined. +// ^^^ +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_visitStringInterpolation_valid() async { @@ -5497,63 +5287,35 @@ class ConstantVisitorTestSupport extends PubPackageResolutionTest { @reflectiveTest class InstanceCreationEvaluatorTest extends ConstantVisitorTestSupport { test_assertInitializer_assertIsNot_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A() : assert(0 is! int); +// ^^^^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. +// ^^^^^^^^^ +// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'. } const a = const A(null); -''', - [ - error(diag.unnecessaryTypeCheckFalse, 31, 9), - error( - diag.constEvalThrowsException, - 56, - 13, - contextMessages: [ - contextMessage( - testFile, - 24, - 17, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - error(diag.extraPositionalArguments, 64, 4), - ], - ); +// ^^^^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +// ^^^^ +// [diag.extraPositionalArguments] Too many positional arguments: 0 expected, but 1 found. +'''); } test_assertInitializer_assertIsNot_null_nullableType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A() : assert(null is! T); +// ^^^^^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. } const a = const A(); -''', - [ - error( - diag.constEvalThrowsException, - 60, - 15, - contextMessages: [ - contextMessage( - testFile, - 27, - 18, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_assertInitializer_assertIsNot_true() async { @@ -5574,24 +5336,19 @@ A } test_assertInitializer_class_privateNamedParameters_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { final int _x; +// ^^ +// [diag.unusedField] The value of the field '_x' isn't used. const A({required this._x}) : assert(_x > 0); +// ^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. } const a = A(x: 0); -''', - [ - error(diag.unusedField, 22, 2), - error( - diag.constEvalThrowsException, - 86, - 7, - contextMessages: [message(testFile, 58, 14)], - ), - ], - ); +// ^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); var result = _topLevelVar('a'); assertDartObjectText(result, ''' @@ -5599,17 +5356,18 @@ const a = A(x: 0); } test_assertInitializer_class_privateNamedParameters_multiple() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { final int _x; +// ^^ +// [diag.unusedField] The value of the field '_x' isn't used. final int _y; +// ^^ +// [diag.unusedField] The value of the field '_y' isn't used. const A({required this._x, required this._y}) : assert(_x < _y); } const a = A(x: 1, y: 2); -''', - [error(diag.unusedField, 22, 2), error(diag.unusedField, 38, 2)], - ); +'''); var result = _topLevelVar('a'); assertDartObjectText(result, ''' A @@ -5625,16 +5383,15 @@ A } test_assertInitializer_class_privateNamedParameters_true() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { final int _x; +// ^^ +// [diag.unusedField] The value of the field '_x' isn't used. const A({required this._x}) : assert(_x > 0); } const a = A(x: 1); -''', - [error(diag.unusedField, 22, 2)], - ); +'''); var result = _topLevelVar('a'); assertDartObjectText(result, ''' A @@ -5648,32 +5405,17 @@ A } test_assertInitializer_enum_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' enum E { a, b } class A { const A(E e) : assert(e != E.a); +// ^^^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. } const c = const A(E.a); -''', - [ - error( - diag.constEvalThrowsException, - 73, - 12, - contextMessages: [ - contextMessage( - testFile, - 43, - 16, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_assertInitializer_enum_true() async { @@ -5701,57 +5443,35 @@ A } test_assertInitializer_indirect() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(int i) : assert(i == 1); // (2) +// ^^^^^^^^^^^^^^ +// [context 2] The exception is 'The assertion in this constant expression failed.' and occurs here. } class B extends A { const B(int i) : super(i); +// ^ +// [context 1] The evaluated constructor 'A' is called by 'B' and 'B' is defined here. } main() { print(const B(2)); // (1) +// ^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1][context 2] Evaluation of this constant expression throws an exception. } -''', - [ - error( - diag.constEvalThrowsException, - 124, - 10, - contextMessages: [ - contextMessage( - testFile, - 84, - 1, - textContains: [ - "The evaluated constructor 'A' is called by 'B' and 'B' is defined here.", - ], - ), - contextMessage( - testFile, - 31, - 14, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +'''); } test_assertInitializer_intInDoubleContext_assertIsDouble_true() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(double x): assert(x is double); +// ^^^^^^^^^^^ +// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'. } const a = const A(0); -''', - [error(diag.unnecessaryTypeCheckTrue, 38, 11)], - ); +'''); var result = _topLevelVar('a'); assertDartObjectText(result, ''' A @@ -5764,31 +5484,16 @@ A } test_assertInitializer_intInDoubleContext_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(double x): assert((x + 3) / 2 == 1.5); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. } const a = const A(1); -''', - [ - error( - diag.constEvalThrowsException, - 71, - 10, - contextMessages: [ - contextMessage( - testFile, - 31, - 26, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_assertInitializer_intInDoubleContext_true() async { @@ -5810,43 +5515,27 @@ A } test_assertInitializer_simple_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(): assert(1 is String); +// ^^^^^^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. } const a = const A(); -''', - [ - error( - diag.constEvalThrowsException, - 56, - 9, - contextMessages: [ - contextMessage( - testFile, - 23, - 19, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_assertInitializer_simple_true() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(): assert(1 is int); +// ^^^^^^^^ +// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'. } const a = const A(); -''', - [error(diag.unnecessaryTypeCheckTrue, 30, 8)], - ); +'''); var result = _topLevelVar('a'); assertDartObjectText(result, ''' A @@ -5857,57 +5546,35 @@ A } test_assertInitializer_simpleInSuperInitializer_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(): assert(1 is String); +// ^^^^^^^^^^^^^^^^^^^ +// [context 2] The exception is 'The assertion in this constant expression failed.' and occurs here. } class B extends A { const B() : super(); +// ^ +// [context 1] The evaluated constructor 'A' is called by 'B' and 'B' is defined here. } const b = const B(); -''', - [ - error( - diag.constEvalThrowsException, - 101, - 9, - contextMessages: [ - contextMessage( - testFile, - 74, - 1, - textContains: [ - "The evaluated constructor 'A' is called by 'B' and 'B' is defined here.", - ], - ), - contextMessage( - testFile, - 23, - 19, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^ +// [diag.constEvalThrowsException][context 1][context 2] Evaluation of this constant expression throws an exception. +'''); } test_assertInitializer_simpleInSuperInitializer_true() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(): assert(1 is int); +// ^^^^^^^^ +// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'. } class B extends A { const B() : super(); } const b = const B(); -''', - [error(diag.unnecessaryTypeCheckTrue, 30, 8)], - ); +'''); var result = _topLevelVar('b'); assertDartObjectText(result, ''' B @@ -5921,90 +5588,47 @@ B } test_assertInitializer_usingArgument_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A(int x): assert(x > 0); +// ^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. } const a = const A(0); -''', - [ - error( - diag.constEvalThrowsException, - 55, - 10, - contextMessages: [ - contextMessage( - testFile, - 28, - 13, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_assertInitializer_usingArgument_false_withMessage() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(int x): assert(x > 0, '$x must be greater than 0'); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [context 1] The exception is 'An assertion failed with message '0 must be greater than 0'.' and occurs here. } const a = const A(0); -''', - [ - error( - diag.constEvalThrowsException, - 84, - 10, - contextMessages: [ - contextMessage( - testFile, - 28, - 42, - textContains: [ - "The exception is 'An assertion failed with message '0 must be greater than 0'.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_assertInitializer_usingArgument_false_withMessage_cannotCompute() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(int x): assert(x > 0, '${throw ''}'); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. +// ^^^^^^^^ +// [diag.invalidConstant] Invalid constant value. +// [diag.constConstructorThrowsException] Const constructors can't throw exceptions. +// ^^^ +// [diag.deadCode] Dead code. } const a = const A(0); -''', - [ - error(diag.invalidConstant, 45, 8), - error(diag.constConstructorThrowsException, 45, 8), - error(diag.deadCode, 54, 3), - error( - diag.constEvalThrowsException, - 70, - 10, - contextMessages: [ - contextMessage( - testFile, - 28, - 28, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_assertInitializer_usingArgument_true() async { @@ -6160,18 +5784,17 @@ const right = b == {3:'3', if (a) 1:'1' else 2:'2', 4:'4'}; } test_bool_fromEnvironment_dartLibraryJsUtil_ifElement_nonConstant() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = bool.fromEnvironment('dart.library.js_util'); var b = 7; var x = const A([if (a) b]); +// ^ +// [diag.nonConstantListElement] The values in a const list literal must be constants. class A { const A(List p); } -''', - [error(diag.nonConstantListElement, 91, 1)], - ); +'''); } test_bool_fromEnvironment_dartLibraryJsUtil_ifElement_set() async { @@ -6225,18 +5848,17 @@ const right = b == {3, if (a) ...[1] else ...[1, 2], 4}; } test_bool_fromEnvironment_dartLibraryJsUtil_ifElementElse_nonConstant() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = bool.fromEnvironment('dart.library.js_util'); var b = 7; var x = const A([if (a) 3 else b]); +// ^ +// [diag.nonConstantListElement] The values in a const list literal must be constants. class A { const A(List p); } -''', - [error(diag.nonConstantListElement, 98, 1)], - ); +'''); } test_bool_fromEnvironment_dartLibraryJsUtil_ifStatement_list() async { @@ -6252,18 +5874,17 @@ const x = [3, if (a) ...[1] else ...[1, 2], 4]; } test_bool_fromEnvironment_dartLibraryJsUtil_recordField_nonConstant() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const a = bool.fromEnvironment('dart.library.js_util'); var b = 7; var x = const A((b, )); +// ^ +// [diag.invalidConstant] Invalid constant value. class A { const A((int, ) p); } -''', - [error(diag.invalidConstant, 84, 1)], - ); +'''); } test_bool_hasEnvironment() async { @@ -6283,17 +5904,16 @@ bool true } test_class_constructor_duplicateInitialization_fieldInitializer_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int x = 1; const A() : x = 2; +// ^ +// [diag.fieldInitializedInInitializerAndDeclaration] Fields can't be initialized in the constructor if they are final and were already initialized at their declaration. } const a = A(); -''', - [error(diag.fieldInitializedInInitializerAndDeclaration, 43, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 2 @@ -6304,17 +5924,16 @@ A } test_class_constructor_duplicateInitialization_fieldInitializer_initializingFormal() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int x = 1; const A(this.x); +// ^ +// [diag.finalInitializedInDeclarationAndConstructor] 'x' is final and was given a value when it was declared, so it can't be set to a new value. } const a = A(2); -''', - [error(diag.finalInitializedInDeclarationAndConstructor, 44, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 2 @@ -6327,17 +5946,16 @@ A } test_class_constructor_duplicateInitialization_initializer_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int x; const A() : x = 1, x = 2; +// ^ +// [diag.fieldInitializedByMultipleInitializers] The field 'x' can't be initialized twice in the same constructor. } const a = A(); -''', - [error(diag.fieldInitializedByMultipleInitializers, 46, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 2 @@ -6348,17 +5966,16 @@ A } test_class_constructor_duplicateInitialization_initializingFormal_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int x; const A(this.x) : x = 1; +// ^ +// [diag.fieldInitializedInParameterAndInitializer] Fields can't be initialized in both the parameter list and the initializers. } const a = A(2); -''', - [error(diag.fieldInitializedInParameterAndInitializer, 45, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 1 @@ -6371,80 +5988,75 @@ A } test_class_field_declarationInitializer_nonConstant() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int x = 0; class A { final int f = x; +// ^ +// [diag.invalidConstant] Invalid constant value. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. const A(); +//^^^^^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'f' is initialized with a non-constant value. } const a = A(); -''', - [ - error(diag.constInitializedWithNonConstantValue, 37, 1), - error(diag.invalidConstant, 37, 1), - error(diag.constConstructorWithFieldInitializedByNonConst, 42, 5), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' '''); } test_class_field_declarationInitializer_thisReference_field() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int x = 0; final int f = x; +// ^ +// [diag.invalidConstant] Invalid constant value. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// [diag.implicitThisReferenceInInitializer] The instance member 'x' can't be accessed in an initializer. const A(); +//^^^^^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'f' is initialized with a non-constant value. } const a = A(); -''', - [ - error(diag.constInitializedWithNonConstantValue, 45, 1), - error(diag.invalidConstant, 45, 1), - error(diag.implicitThisReferenceInInitializer, 45, 1), - error(diag.constConstructorWithFieldInitializedByNonConst, 50, 5), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' '''); } test_class_field_declarationInitializer_thisReference_getter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { int get x => 0; final int f = x; +// ^ +// [diag.invalidConstant] Invalid constant value. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +// [diag.implicitThisReferenceInInitializer] The instance member 'x' can't be accessed in an initializer. const A(); +//^^^^^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'f' is initialized with a non-constant value. } const a = A(); -''', - [ - error(diag.constInitializedWithNonConstantValue, 44, 1), - error(diag.invalidConstant, 44, 1), - error(diag.implicitThisReferenceInInitializer, 44, 1), - error(diag.constConstructorWithFieldInitializedByNonConst, 49, 5), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' '''); } test_class_primaryConstructor_assert_false() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class const A(int x) { this : assert(x > 0); +// ^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. } const a = A(0); -''', - [error(diag.constEvalThrowsException, 59, 4)], - ); +// ^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_class_primaryConstructor_assert_true() async { @@ -6484,23 +6096,17 @@ A } test_class_primaryConstructor_duplicateDefinition_field() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class const A(final int x) { +// ^ +// [context 1] The first definition of this name. final int x = 1; +// ^ +// [diag.duplicateDefinition][context 1] The name 'x' is already defined. } const a = A(2); -''', - [ - error( - diag.duplicateDefinition, - 41, - 1, - contextMessages: [message(testFile, 24, 1)], - ), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 2 @@ -6513,23 +6119,16 @@ A } test_class_primaryConstructor_duplicateInitialization_fieldInitializer_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class const A() { final int x = 1; this : x = 2; +// ^ +// [diag.fieldInitializedInDeclarationAndInitializerOfPrimaryConstructor] Fields can't be initialized in both the primary constructor and at their declaration. } const a = A(); -''', - [ - error( - diag.fieldInitializedInDeclarationAndInitializerOfPrimaryConstructor, - 46, - 1, - ), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 2 @@ -6540,22 +6139,15 @@ A } test_class_primaryConstructor_duplicateInitialization_fieldInitializer_initializingFormal() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class const A(this.x) { +// ^ +// [diag.fieldInitializedInDeclarationAndParameterOfPrimaryConstructor] Fields can't be initialized in both the primary constructor parameter list and at their declaration. final int x = 1; } const a = A(2); -''', - [ - error( - diag.fieldInitializedInDeclarationAndParameterOfPrimaryConstructor, - 19, - 1, - ), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 2 @@ -6568,17 +6160,16 @@ A } test_class_primaryConstructor_duplicateInitialization_initializer_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class const A() { final int x; this : x = 1, x = 2; +// ^ +// [diag.fieldInitializedByMultipleInitializers] The field 'x' can't be initialized twice in the same constructor. } const a = A(); -''', - [error(diag.fieldInitializedByMultipleInitializers, 49, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 2 @@ -6589,19 +6180,17 @@ A } test_class_primaryConstructor_duplicateInitialization_initializingFormal_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class const A(this.x) { +// ^^^^^^ +// [diag.initializingFormalForNonExistentField] 'x' isn't a field in the enclosing class. this : x = 2; +// ^^^^^ +// [diag.initializerForNonExistentField] 'x' isn't a field in the enclosing class. } const a = A(1); -''', - [ - error(diag.initializingFormalForNonExistentField, 14, 6), - error(diag.initializerForNonExistentField, 33, 5), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A x: int 2 @@ -6632,14 +6221,13 @@ A } test_class_primaryConstructor_fieldInitializer_functionExpression() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class const A(int x) { +// ^^^^^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'foo' is initialized with a non-constant value. final int Function() foo = () => x; } -''', - [error(diag.constConstructorWithFieldInitializedByNonConst, 6, 5)], - ); +'''); } test_class_primaryConstructor_fieldInitializer_multipleInvocations() async { @@ -6688,32 +6276,29 @@ A } test_class_primaryConstructor_fieldInitializer_topLevel_final() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final x = 1; class const A() { +// ^^^^^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'y' is initialized with a non-constant value. final int y = x + 2; +// ^ +// [diag.invalidConstant] Invalid constant value. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. } const a = A(); -''', - [ - error(diag.constConstructorWithFieldInitializedByNonConst, 19, 5), - error(diag.invalidConstant, 47, 1), - error(diag.constInitializedWithNonConstantValue, 47, 1), - ], - ); +'''); } test_class_primaryConstructor_fieldInitializer_topLevel_final_noInvocation() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final x = 1; class const A(int p) { +// ^^^^^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'y' is initialized with a non-constant value. final int y = x + p; } -''', - [error(diag.constConstructorWithFieldInitializedByNonConst, 19, 5)], - ); +'''); } test_class_primaryConstructor_formalParameter_declaring_optionalNamed_noArgument() async { @@ -7128,43 +6713,40 @@ B } test_class_primaryConstructor_initializer_assert_nonConstant() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int x = 1; class const A() { this : assert(x > 0); +// ^ +// [diag.invalidConstant] Invalid constant value. } -''', - [error(diag.invalidConstant, 45, 1)], - ); +'''); } test_class_primaryConstructor_initializer_field_nonConstant() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int x = 1; class const A() { final int f; this : f = x; +// ^ +// [diag.invalidConstant] Invalid constant value. } -''', - [error(diag.invalidConstant, 57, 1)], - ); +'''); } test_class_primaryConstructor_initializer_super_nonConstantArgument() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' int x = 1; class A { const A(int a); } class const B() extends A { this : super(x); +// ^ +// [diag.invalidConstant] Invalid constant value. } -''', - [error(diag.invalidConstant, 84, 1)], - ); +'''); } test_class_primaryConstructor_redirect_fieldInitializer() async { @@ -7187,20 +6769,18 @@ A } test_class_primaryConstructor_redirect_fieldInitializer_nonRedirectingSecondary() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class const A(int x) { final int y = x + 1; const A.named(int z) : this(z * 2); const A.other() {} +// ^^^^^^^ +// [diag.nonRedirectingGenerativeConstructorWithPrimary] Classes with primary constructors can't have non-redirecting generative constructors. +// ^ +// [diag.constConstructorWithBody] Const constructors can't have a body. } const a = A.named(10); -''', - [ - error(diag.nonRedirectingGenerativeConstructorWithPrimary, 92, 7), - error(diag.constConstructorWithBody, 102, 1), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' A y: int 21 @@ -7238,33 +6818,19 @@ B } test_dotShorthand_assertInitializer_assertIsNot_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { const A() : assert(0 is! int); +// ^^^^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. +// ^^^^^^^^^ +// [diag.unnecessaryTypeCheckFalse] Unnecessary type check; the result is always 'false'. } const A a = .new(); -''', - [ - error(diag.unnecessaryTypeCheckFalse, 31, 9), - error( - diag.constEvalThrowsException, - 58, - 6, - contextMessages: [ - contextMessage( - testFile, - 24, - 17, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_dotShorthand_assertInitializer_assertIsNot_true() async { @@ -7285,32 +6851,17 @@ A } test_dotShorthand_assertInitializer_enum_false() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' enum E { a, b } class A { const A(E e) : assert(e != .a); +// ^^^^^^^^^^^^^^^ +// [context 1] The exception is 'The assertion in this constant expression failed.' and occurs here. } const A a = .new(.a); -''', - [ - error( - diag.constEvalThrowsException, - 74, - 8, - contextMessages: [ - contextMessage( - testFile, - 43, - 15, - textContains: [ - "The exception is 'The assertion in this constant expression failed.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^ +// [diag.constEvalThrowsException][context 1] Evaluation of this constant expression throws an exception. +'''); } test_dotShorthand_assertInitializer_enum_true() async { @@ -7338,20 +6889,19 @@ A } test_dotShorthand_assertInitializer_simpleInSuperInitializer_true() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(): assert(1 is int); +// ^^^^^^^^ +// [diag.unnecessaryTypeCheckTrue] Unnecessary type check; the result is always 'true'. } class B extends A { const B() : super(); } const B b = .new(); -''', - [error(diag.unnecessaryTypeCheckTrue, 30, 8)], - ); +'''); var result = _topLevelVar('b'); - assertDartObjectText(result, ''' + assertDartObjectText(result, r''' B (super): A constructorInvocation @@ -7472,18 +7022,16 @@ class C { const C({required this.one}); } '''); - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; const C c = .new(); -''', - [error(diag.missingRequiredArgument, 30, 3)], - ); +// ^^^ +// [diag.missingRequiredArgument] The named parameter 'one' is required, but there's no corresponding argument. +'''); } test_dotShorthand_nonConstantArgument_issue60963() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { int cannotBeConst; A(): cannotBeConst = 0; @@ -7491,27 +7039,24 @@ class A { extension type const B(A a) {} const B b = .new(A()); -''', - [ - error(diag.constWithNonConst, 108, 3), - error(diag.constInitializedWithNonConstantValue, 108, 3), - ], - ); +// ^^^ +// [diag.constWithNonConst] The constructor being called isn't a const constructor. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_enum_constructor_duplicateInitialization_fieldInitializer_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v; final int x = 1; const E() : x = 2; +// ^ +// [diag.fieldInitializedInInitializerAndDeclaration] Fields can't be initialized in the constructor if they are final and were already initialized at their declaration. } const a = E.v; -''', - [error(diag.fieldInitializedInInitializerAndDeclaration, 47, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' E _name: String v @@ -7524,18 +7069,17 @@ E } test_enum_constructor_duplicateInitialization_fieldInitializer_initializingFormal() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v(2); final int x = 1; const E(this.x); +// ^ +// [diag.finalInitializedInDeclarationAndConstructor] 'x' is final and was given a value when it was declared, so it can't be set to a new value. } const a = E.v; -''', - [error(diag.finalInitializedInDeclarationAndConstructor, 51, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' E _name: String v @@ -7550,18 +7094,17 @@ E } test_enum_constructor_duplicateInitialization_initializer_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v; final int x; const E() : x = 1, x = 2; +// ^ +// [diag.fieldInitializedByMultipleInitializers] The field 'x' can't be initialized twice in the same constructor. } const a = E.v; -''', - [error(diag.fieldInitializedByMultipleInitializers, 50, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' E _name: String v @@ -7574,18 +7117,17 @@ E } test_enum_constructor_duplicateInitialization_initializingFormal_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v(1); final int x; const E(this.x) : x = 2; +// ^ +// [diag.fieldInitializedInParameterAndInitializer] Fields can't be initialized in both the parameter list and the initializers. } const a = E.v; -''', - [error(diag.fieldInitializedInParameterAndInitializer, 52, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' E _name: String v @@ -7600,25 +7142,19 @@ E } test_enum_primaryConstructor_duplicateDefinition_field() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E(final int x) { +// ^ +// [context 1] The first definition of this name. v(2); final int x = 1; +// ^ +// [diag.duplicateDefinition][context 1] The name 'x' is already defined. } const a = E.v; -''', - [ - error( - diag.duplicateDefinition, - 43, - 1, - contextMessages: [message(testFile, 17, 1)], - ), - ], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' E _name: String v @@ -7633,18 +7169,17 @@ E } test_enum_primaryConstructor_duplicateInitialization_initializer_initializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E() { v; final int x; this : x = 1, x = 2; +// ^ +// [diag.fieldInitializedByMultipleInitializers] The field 'x' can't be initialized twice in the same constructor. } const a = E.v; -''', - [error(diag.fieldInitializedByMultipleInitializers, 47, 1)], - ); +'''); assertDartObjectText(_topLevelVar('a'), r''' E _name: String v @@ -7657,29 +7192,27 @@ E } test_enum_primaryConstructor_fieldInitializer_functionExpression_explicitConst() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum const E(int x) { +// ^^^^^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'foo' is initialized with a non-constant value. v(0); final int Function() foo = () => x; } -''', - [error(diag.constConstructorWithFieldInitializedByNonConst, 5, 5)], - ); +'''); } test_enum_primaryConstructor_fieldInitializer_functionExpression_implicitConst() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E(int x) { +// ^ +// [diag.constConstructorWithFieldInitializedByNonConst] Can't define the 'const' constructor because the field 'foo' is initialized with a non-constant value. v(0); final int Function() foo = () => x; } -''', - [error(diag.constConstructorWithFieldInitializedByNonConst, 5, 1)], - ); +'''); } test_field_deferred_issue48991() async { @@ -7691,8 +7224,7 @@ class A { const aa = A(); '''); - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart' deferred as a; class B { @@ -7701,10 +7233,10 @@ class B { main() { print(const B(a.aa)); +// ^^ +// [diag.constConstructorConstantFromDeferredLibrary] Constant values from a deferred library can't be used as values in a 'const' constructor. } -''', - [error(diag.constConstructorConstantFromDeferredLibrary, 93, 2)], - ); +'''); } test_field_imported_staticConst() async { @@ -7820,34 +7352,19 @@ A } test_fieldInitializer_typeParameter_withoutConstructorTearoffs() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // @dart=2.12 class A { final Object f; const A(): f = T; +// ^ +// [context 1] The error is in the field initializer of 'A', and occurs here. +// [diag.invalidConstant] Invalid constant value. } const a = const A(); -''', - [ - error(diag.invalidConstant, 62, 1), - error( - diag.constTypeParameter, - 77, - 14, - contextMessages: [ - contextMessage( - testFile, - 62, - 1, - textContains: [ - "The error is in the field initializer of 'A', and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^^^^^^ +// [diag.constTypeParameter][context 1] Type parameters can't be used in a constant expression. +'''); var result = _topLevelVar('a'); _assertNull(result); } @@ -7891,8 +7408,7 @@ int 42 } test_issue47351() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class Foo { final int bar; const Foo(this.bar); @@ -7900,17 +7416,14 @@ class Foo { int bar = 2; const a = const Foo(bar); -''', - [ - error(diag.constWithNonConstantArgument, 88, 3), - error(diag.constInitializedWithNonConstantValue, 88, 3), - ], - ); +// ^^^ +// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions. +// [diag.constInitializedWithNonConstantValue] Const variables must be initialized with a constant value. +'''); } test_issue47603() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { final void Function() c; const C(this.c); @@ -7918,15 +7431,16 @@ class C { void main() { const C(() {}); +// ^^^^^ +// [diag.constWithNonConstantArgument] Arguments of a constant creation must be constant expressions. } -''', - [error(diag.constWithNonConstantArgument, 83, 5)], - ); +'''); } test_issue49389() async { - await assertErrorsInCode( - ''' + // TODO(kallentu): Fix [InvalidConstant.genericError] to handle + // NamedExpressions. + await resolveTestCodeWithDiagnostics(r''' class Foo { const Foo({required this.bar}); final Map bar; @@ -7935,14 +7449,10 @@ class Foo { void main() { final data = {}; const Foo(bar: data); +// ^^^^ +// [diag.invalidConstant] Invalid constant value. } -''', - [ - // TODO(kallentu): Fix [InvalidConstant.genericError] to handle - // NamedExpressions. - error(diag.invalidConstant, 148, 4), - ], - ); +'''); } @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/55467') @@ -8392,54 +7902,27 @@ B } test_superInitializer_paramTypeMismatch_indirect() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { final double d; const C(this.d); } class D extends C { const D(d) : super(d); +// ^ +// [context 1] The evaluated constructor 'C' is called by 'D' and 'D' is defined here. +// ^ +// [context 3] The exception is 'A value of type 'String' can't be assigned to a parameter of type 'double' in a const constructor.' and occurs here. } class E extends D { const E(e) : super(e); +// ^ +// [context 2] The evaluated constructor 'D' is called by 'E' and 'E' is defined here. } const f = const E('0.0'); -''', - [ - error( - diag.constEvalThrowsException, - 153, - 14, - contextMessages: [ - contextMessage( - testFile, - 77, - 1, - textContains: [ - "The evaluated constructor 'C' is called by 'D' and 'D' is defined here.", - ], - ), - contextMessage( - testFile, - 124, - 1, - textContains: [ - "The evaluated constructor 'D' is called by 'E' and 'E' is defined here.", - ], - ), - contextMessage( - testFile, - 90, - 1, - textContains: [ - "The exception is 'A value of type 'String' can't be assigned to a parameter of type 'double' in a const constructor.' and occurs here.", - ], - ), - ], - ), - ], - ); +// ^^^^^^^^^^^^^^ +// [diag.constEvalThrowsException][context 1][context 2][context 3] Evaluation of this constant expression throws an exception. +'''); } test_superInitializer_typeParameter() async { @@ -8530,19 +8013,16 @@ A } test_wildcard_regularInitializer_initializerList() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { final int _; final int y; const A(this._): y = _; +// ^ +// [diag.invalidConstant] Invalid constant value. +// [diag.implicitThisReferenceInInitializer] The instance member '_' can't be accessed in an initializer. } -''', - [ - error(diag.invalidConstant, 63, 1), - error(diag.implicitThisReferenceInInitializer, 63, 1), - ], - ); +'''); } test_wildcard_superInitializer() async { @@ -8575,10 +8055,11 @@ B } test_wildcard_superInitializer_multiple_optionalPositional() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { final int _; +// ^ +// [diag.unusedField] The value of the field '_' isn't used. final int y; const A([this._ = 1, this.y = 2]); } @@ -8586,9 +8067,7 @@ class B extends A { const B([super._ = 3, super._ = 4]); } const a = const B(10); -''', - [error(diag.unusedField, 22, 1)], - ); +'''); var result = _topLevelVar('a'); assertDartObjectText(result, ''' B diff --git a/pkg/analyzer/test/src/dart/element/class_element_test.dart b/pkg/analyzer/test/src/dart/element/class_element_test.dart index ff8487c4d3d..75b890ae29a 100644 --- a/pkg/analyzer/test/src/dart/element/class_element_test.dart +++ b/pkg/analyzer/test/src/dart/element/class_element_test.dart @@ -4,14 +4,15 @@ import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/src/dart/element/element.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../resolution/context_collection_resolution.dart'; +import '../resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(ClassElementTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -286,16 +287,14 @@ abstract class B implements A {} } test_lookUpInheritedConcreteGetter_recursive() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, A. class B extends A {} -''', - [ - error(diag.recursiveInterfaceInheritance, 6, 1), - error(diag.recursiveInterfaceInheritance, 27, 1), - ], - ); +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, A. +'''); var B = findElement2.class_('B'); assertElementNull(B._lookUpInheritedConcreteGetter('foo')); } @@ -577,16 +576,14 @@ abstract class B implements A {} } test_lookUpInheritedConcreteMethod_recursive() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, A. class B extends A {} -''', - [ - error(diag.recursiveInterfaceInheritance, 6, 1), - error(diag.recursiveInterfaceInheritance, 27, 1), - ], - ); +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, A. +'''); var B = findElement2.class_('B'); assertElementNull(B._lookUpInheritedConcreteMethod('foo')); } @@ -868,16 +865,14 @@ abstract class B implements A {} } test_lookUpInheritedConcreteSetter_recursive() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, A. class B extends A {} -''', - [ - error(diag.recursiveInterfaceInheritance, 6, 1), - error(diag.recursiveInterfaceInheritance, 27, 1), - ], - ); +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, A. +'''); var B = findElement2.class_('B'); assertElementNull(B._lookUpInheritedConcreteSetter('foo')); } @@ -1129,16 +1124,14 @@ abstract class B implements A {} } test_lookUpInheritedMethod_recursive() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, A. class B extends A {} -''', - [ - error(diag.recursiveInterfaceInheritance, 6, 1), - error(diag.recursiveInterfaceInheritance, 27, 1), - ], - ); +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, A. +'''); var B = findElement2.class_('B'); assertElementNull(B._lookUpInheritedMethod('foo')); } diff --git a/pkg/analyzer/test/src/dart/element/display_string_test.dart b/pkg/analyzer/test/src/dart/element/display_string_test.dart index 2680d27a1ff..5f98d5ef7bd 100644 --- a/pkg/analyzer/test/src/dart/element/display_string_test.dart +++ b/pkg/analyzer/test/src/dart/element/display_string_test.dart @@ -3,15 +3,16 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/src/dart/element/element.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../resolution/context_collection_resolution.dart'; +import '../resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(ElementDisplayStringTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -326,12 +327,11 @@ final class A {} } test_writeDirectiveUri() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'src/f.dart'; -''', - [error(diag.uriDoesNotExist, 7, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'src/f.dart'. +'''); var import = findElement2.libraryFragment.libraryImports[0] as LibraryImportImpl; expect(import.displayString(), "import package:test/src/f.dart"); @@ -427,14 +427,13 @@ nonexistentType a; } test_writeLabelElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { f: 0; +//^^ +// [diag.unusedLabel] The label 'f' isn't used. } -''', - [error(diag.unusedLabel, 13, 2)], - ); +'''); var element = findElement2.label('f'); expect(element.displayString(), 'f'); } @@ -448,64 +447,59 @@ library f; } test_writeLibraryExport() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' export 'src/f.dart'; -''', - [error(diag.uriDoesNotExist, 7, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'src/f.dart'. +'''); var export = findElement2.libraryFragment.libraryExports.single as LibraryExportImpl; expect(export.displayString(), "export package:test/src/f.dart"); } test_writeLibraryImport() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'src/f.dart'; -''', - [error(diag.uriDoesNotExist, 7, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'src/f.dart'. +'''); var import = findElement2.libraryFragment.libraryImports[0] as LibraryImportImpl; expect(import.displayString(), "import package:test/src/f.dart"); } test_writeLocalFunctionElement() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { void g() {} +// ^ +// [diag.unusedElement] The declaration 'g' isn't referenced. } -''', - [error(diag.unusedElement, 18, 1)], - ); +'''); var element = findElement2.localFunction('g'); expect(element.displayString(), "void g()"); } test_writeLocalFunctionElement_formalParameters() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { void g(int a, bool b, {String? c}) {} +// ^ +// [diag.unusedElement] The declaration 'g' isn't referenced. } -''', - [error(diag.unusedElement, 18, 1)], - ); +'''); var element = findElement2.localFunction('g'); expect(element.displayString(), "void g(int a, bool b, {String? c})"); } test_writeLocalFunctionElement_typeParameters() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { void g() {} +// ^ +// [diag.unusedElement] The declaration 'g' isn't referenced. } -''', - [error(diag.unusedElement, 18, 1)], - ); +'''); var element = findElement2.localFunction('g'); expect(element.displayString(), "void g()"); } @@ -549,36 +543,35 @@ mixin M {} } test_writeNeverType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' Never a; -''', - [error(diag.notInitializedNonNullableVariable, 6, 1)], - ); +// ^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'a' must be initialized. +'''); var element = findElement2.topVar('a'); expect(element.displayString(), "Never a"); } test_writePartInclude() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' part 'src/f.dart'; -''', - [error(diag.uriDoesNotExist, 5, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'package:test/src/f.dart'. +'''); var element = findElement2.libraryFragment.partIncludes.single as PartIncludeImpl; expect(element.displayString(), 'part package:test/src/f.dart'); } test_writePrefixElement_multipleImports() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'src/f.dart' as a; +// ^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'src/f.dart'. import 'src/bar.dart' as a; -''', - [error(diag.uriDoesNotExist, 7, 12), error(diag.uriDoesNotExist, 33, 14)], - ); +// ^^^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'src/bar.dart'. +'''); var prefix = findElement2.prefix('a'); expect( prefix.displayString(), @@ -587,12 +580,11 @@ import 'src/bar.dart' as a; } test_writePrefixElement_singleImport() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'src/f.dart' as a; -''', - [error(diag.uriDoesNotExist, 7, 12)], - ); +// ^^^^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'src/f.dart'. +'''); var prefix = findElement2.prefix('a'); expect(prefix.displayString(), "import 'src/f.dart' as a;"); } diff --git a/pkg/analyzer/test/src/dart/micro/simple_file_resolver_test.dart b/pkg/analyzer/test/src/dart/micro/simple_file_resolver_test.dart index a6774f00cdf..295509fac16 100644 --- a/pkg/analyzer/test/src/dart/micro/simple_file_resolver_test.dart +++ b/pkg/analyzer/test/src/dart/micro/simple_file_resolver_test.dart @@ -9,7 +9,6 @@ import 'package:analyzer/src/dart/analysis/file_state.dart'; import 'package:analyzer/src/dart/micro/resolve_file.dart'; import 'package:analyzer/src/dart/micro/utils.dart'; import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; -import 'package:linter/src/diagnostic.dart' as diag; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -846,13 +845,12 @@ analyzer: implicit-casts: false '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' num a = 0; int b = a; -''', - [error(diag.invalidAssignment, 19, 1)], - ); +// ^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'int'. +'''); } test_analysisOptions_file_inPackage() async { @@ -862,13 +860,12 @@ analyzer: implicit-casts: false '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' num a = 0; int b = a; -''', - [error(diag.invalidAssignment, 19, 1)], - ); +// ^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'int'. +'''); } test_analysisOptions_file_inThirdParty() async { @@ -926,15 +923,14 @@ linter: - omit_local_variable_types '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { int a = 0; +//^^^ +// [diag.omitLocalVariableTypes] Unnecessary type annotation on a local variable. a; } -''', - [error(diag.omitLocalVariableTypes, 11, 3)], - ); +'''); } test_basic() async { @@ -1032,21 +1028,13 @@ p.dynamic f() {} } Future test_errors_hasNullSuffix() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' String f(Map a) { return a[0]; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'String?' can't be returned from the function 'f' because it has a return type of 'String'. } -''', - [ - error( - diag.returnOfInvalidTypeFromFunction, - 40, - 4, - messageContains: ["'String'", 'String?'], - ), - ], - ); +'''); } test_findReferences_class() async { @@ -2572,21 +2560,19 @@ void f(MyEnum myEnum) { } test_unknown_uri() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'foo:bar'; -''', - [error(diag.uriDoesNotExist, 7, 9)], - ); +// ^^^^^^^^^ +// [diag.uriDoesNotExist] Target of URI doesn't exist: 'foo:bar'. +'''); } test_warning() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math'; -''', - [error(diag.unusedImport, 7, 11)], - ); +// ^^^^^^^^^^^ +// [diag.unusedImport] Unused import: 'dart:math'. +'''); } void _assertResolvedFiles(List expected, {bool andClear = true}) { diff --git a/pkg/analyzer/test/src/dart/resolution/assignment_test.dart b/pkg/analyzer/test/src/dart/resolution/assignment_test.dart index 134c172a791..43b1a24f362 100644 --- a/pkg/analyzer/test/src/dart/resolution/assignment_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/assignment_test.dart @@ -4843,25 +4843,23 @@ AssignmentExpression } test_simpleIdentifier_staticMethod_superSetter_simple() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { set x(num _) {} } class B extends A { static void x() {} +// ^ +// [diag.conflictingStaticAndInstance] Class 'B' can't define static member 'x' and have instance member 'A.x' with the same name. void f() { x = 2; +// ^ +// [diag.assignmentToMethod] Methods can't be assigned a value. } } -''', - [ - error(diag.conflictingStaticAndInstance, 65, 1), - error(diag.assignmentToMethod, 90, 1), - ], - ); +'''); var assignment = findNode.assignment('x = 2'); @@ -4921,14 +4919,13 @@ AssignmentExpression } test_simpleIdentifier_synthetic_simple() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void f(int y) { = y; +//^ +// [diag.missingIdentifier] Expected an identifier. } -''', - [error(diag.missingIdentifier, 18, 1)], - ); +'''); var assignment = findNode.assignment('= y'); @@ -5100,8 +5097,7 @@ AssignmentExpression } test_simpleIdentifier_topGetter_superSetter_simple() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { set x(num _) {} } @@ -5112,11 +5108,11 @@ class B extends A { void f() { x = 2; +// ^ +// [diag.assignmentToFinal] 'x' can't be used as a setter because it's final. } } -''', - [error(diag.assignmentToFinal, 86, 1)], - ); +'''); var assignment = findNode.assignment('x = 2'); diff --git a/pkg/analyzer/test/src/dart/resolution/binary_expression_test.dart b/pkg/analyzer/test/src/dart/resolution/binary_expression_test.dart index a6ce4f8f771..b1af6f05dcf 100644 --- a/pkg/analyzer/test/src/dart/resolution/binary_expression_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/binary_expression_test.dart @@ -2,7 +2,6 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; @@ -717,16 +716,15 @@ BinaryExpression } test_bangEq_extensionOverride_left() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' extension E on int {} void f(int a) { E(a) != 0; +// ^^ +// [diag.undefinedExtensionOperator] The operator '==' isn't defined for the extension 'E'. } -''', - [error(diag.undefinedExtensionOperator, 46, 2)], - ); +'''); assertResolvedNodeText(findNode.binary('!= 0'), r''' BinaryExpression @@ -756,14 +754,13 @@ BinaryExpression } test_bangEqEq() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(int a, int b) { a !== b; +// ^ +// [diag.unsupportedOperator] The '!==' operator is not supported. } -''', - [error(diag.unsupportedOperator, 22, 1)], - ); +'''); assertResolvedNodeText(findNode.binary('a !== b'), r''' BinaryExpression @@ -809,16 +806,15 @@ BinaryExpression } test_eqEq_extensionOverride_left() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' extension E on int {} void f(int a) { E(a) == 0; +// ^^ +// [diag.undefinedExtensionOperator] The operator '==' isn't defined for the extension 'E'. } -''', - [error(diag.undefinedExtensionOperator, 46, 2)], - ); +'''); assertResolvedNodeText(findNode.binary('== 0'), r''' BinaryExpression @@ -874,14 +870,13 @@ BinaryExpression } test_eqEq_invalidType_int() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(A a) { +// ^ +// [diag.undefinedClass] Undefined class 'A'. a == 0; } -''', - [error(diag.undefinedClass, 7, 1)], - ); +'''); var node = findNode.binary('a == 0'); assertResolvedNodeText(node, r''' @@ -902,14 +897,13 @@ BinaryExpression } test_eqEqEq() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f(int a, int b) { a === b; +// ^ +// [diag.unsupportedOperator] The '===' operator is not supported. } -''', - [error(diag.unsupportedOperator, 22, 1)], - ); +'''); assertResolvedNodeText(findNode.binary('a === b'), r''' BinaryExpression diff --git a/pkg/analyzer/test/src/diagnostics/augmentation_extends_clause_already_present_test.dart b/pkg/analyzer/test/src/diagnostics/augmentation_extends_clause_already_present_test.dart index 0f293904cd4..747243b95c9 100644 --- a/pkg/analyzer/test/src/diagnostics/augmentation_extends_clause_already_present_test.dart +++ b/pkg/analyzer/test/src/diagnostics/augmentation_extends_clause_already_present_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(AugmentationExtendsClauseAlreadyPresentTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -17,42 +18,30 @@ main() { class AugmentationExtendsClauseAlreadyPresentTest extends PubPackageResolutionTest { test_alreadyPresent() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A {} +// ^ +// [context 1] The declaration being augmented. augment class B extends A {} -''', - [ - error( - diag.augmentationExtendsClauseAlreadyPresent, - 49, - 7, - contextMessages: [message(testFile, 18, 1)], - ), - ], - ); +// ^^^^^^^ +// [diag.augmentationExtendsClauseAlreadyPresent][context 1] The augmentation has an 'extends' clause, but an augmentation target already includes an 'extends' clause and it isn't allowed to be repeated or changed. +'''); } test_alreadyPresent2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A {} +// ^ +// [context 1] The declaration being augmented. augment class B {} augment class B extends A {} -''', - [ - error( - diag.augmentationExtendsClauseAlreadyPresent, - 68, - 7, - contextMessages: [message(testFile, 18, 1)], - ), - ], - ); +// ^^^^^^^ +// [diag.augmentationExtendsClauseAlreadyPresent][context 1] The augmentation has an 'extends' clause, but an augmentation target already includes an 'extends' clause and it isn't allowed to be repeated or changed. +'''); } test_notPresent() async { diff --git a/pkg/analyzer/test/src/diagnostics/augmentation_modifier_extra_test.dart b/pkg/analyzer/test/src/diagnostics/augmentation_modifier_extra_test.dart index 1025b7428c3..f3a88627c28 100644 --- a/pkg/analyzer/test/src/diagnostics/augmentation_modifier_extra_test.dart +++ b/pkg/analyzer/test/src/diagnostics/augmentation_modifier_extra_test.dart @@ -2,27 +2,27 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(AugmentationModifierExtraTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class AugmentationModifierExtraTest extends PubPackageResolutionTest { test_class_abstract_abstractBase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class A {} augment abstract base class A {} -''', - [error(diag.augmentationModifierExtra, 37, 4)], - ); +// ^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'base' modifier that the declaration doesn't have. +'''); } test_class_abstractBase_abstractBase() async { @@ -33,97 +33,87 @@ augment abstract base class A {} } test_class_base_abstractBase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} augment abstract base class A {} -''', - [error(diag.augmentationModifierExtra, 24, 8)], - ); +// ^^^^^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'abstract' modifier that the declaration doesn't have. +'''); } test_class_nothing_abstract() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} augment abstract class A {} -''', - [error(diag.augmentationModifierExtra, 19, 8)], - ); +// ^^^^^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'abstract' modifier that the declaration doesn't have. +'''); } test_class_nothing_abstractBase() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} augment abstract base class A {} -''', - [ - error(diag.augmentationModifierExtra, 19, 8), - error(diag.augmentationModifierExtra, 28, 4), - ], - ); +// ^^^^^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'abstract' modifier that the declaration doesn't have. +// ^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'base' modifier that the declaration doesn't have. +'''); } test_class_nothing_base() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} augment base class A {} -''', - [error(diag.augmentationModifierExtra, 19, 4)], - ); +// ^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'base' modifier that the declaration doesn't have. +'''); } test_class_nothing_final() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} augment final class A {} -''', - [error(diag.augmentationModifierExtra, 19, 5)], - ); +// ^^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'final' modifier that the declaration doesn't have. +'''); } test_class_nothing_interface() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} augment interface class A {} -''', - [error(diag.augmentationModifierExtra, 19, 9)], - ); +// ^^^^^^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'interface' modifier that the declaration doesn't have. +'''); } test_class_nothing_mixin() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} augment mixin class A {} -''', - [error(diag.augmentationModifierExtra, 19, 5)], - ); +// ^^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'mixin' modifier that the declaration doesn't have. +'''); } test_class_nothing_nothing_abstract() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} augment class A {} augment abstract class A {} -''', - [error(diag.augmentationModifierExtra, 38, 8)], - ); +// ^^^^^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'abstract' modifier that the declaration doesn't have. +'''); } test_class_nothing_sealed() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} augment sealed class A {} -''', - [error(diag.augmentationModifierExtra, 19, 6)], - ); +// ^^^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'sealed' modifier that the declaration doesn't have. +'''); } test_mixin_base_base() async { @@ -134,23 +124,21 @@ augment base mixin A {} } test_mixin_nothing_base() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin A {} augment base mixin A {} -''', - [error(diag.augmentationModifierExtra, 19, 4)], - ); +// ^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'base' modifier that the declaration doesn't have. +'''); } test_mixin_nothing_nothing_base() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin A {} augment mixin A {} augment base mixin A {} -''', - [error(diag.augmentationModifierExtra, 38, 4)], - ); +// ^^^^ +// [diag.augmentationModifierExtra] The augmentation has the 'base' modifier that the declaration doesn't have. +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/augmentation_modifier_missing_test.dart b/pkg/analyzer/test/src/diagnostics/augmentation_modifier_missing_test.dart index 22720b7c6a3..7213472716c 100644 --- a/pkg/analyzer/test/src/diagnostics/augmentation_modifier_missing_test.dart +++ b/pkg/analyzer/test/src/diagnostics/augmentation_modifier_missing_test.dart @@ -2,48 +2,43 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(AugmentationModifierMissingTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class AugmentationModifierMissingTest extends PubPackageResolutionTest { test_class_abstract_abstract_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class A {} augment abstract class A {} augment class A {} -''', - [error(diag.augmentationModifierMissing, 48, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'abstract' modifier that the declaration has. +'''); } test_class_abstract_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class A {} augment class A {} -''', - [error(diag.augmentationModifierMissing, 20, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'abstract' modifier that the declaration has. +'''); } test_class_abstractBase_abstract() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract base class A {} augment abstract class A {} -''', - [error(diag.augmentationModifierMissing, 25, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'base' modifier that the declaration has. +'''); } test_class_abstractBase_abstractBase() async { @@ -54,76 +49,60 @@ augment abstract base class A {} } test_class_abstractBase_base() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract base class A {} augment base class A {} -''', - [error(diag.augmentationModifierMissing, 25, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'abstract' modifier that the declaration has. +'''); } test_class_abstractBase_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract base class A {} augment class A {} -''', - [ - error(diag.augmentationModifierMissing, 25, 7), - error(diag.augmentationModifierMissing, 25, 7), - ], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'abstract' modifier that the declaration has. +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'base' modifier that the declaration has. +'''); } test_class_base_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} augment class A {} -''', - [error(diag.augmentationModifierMissing, 16, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'base' modifier that the declaration has. +'''); } test_class_final_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} augment class A {} -''', - [error(diag.augmentationModifierMissing, 17, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'final' modifier that the declaration has. +'''); } test_class_interface_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' interface class A {} augment class A {} -''', - [error(diag.augmentationModifierMissing, 21, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'interface' modifier that the declaration has. +'''); } test_class_mixin_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin class A {} augment class A {} -''', - [error(diag.augmentationModifierMissing, 17, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'mixin' modifier that the declaration has. +'''); } test_class_sealed_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' sealed class A {} augment class A {} -''', - [error(diag.augmentationModifierMissing, 18, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'sealed' modifier that the declaration has. +'''); } test_mixin_base_base() async { @@ -134,23 +113,19 @@ augment base mixin A {} } test_mixin_base_base_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base mixin A {} augment base mixin A {} augment mixin A {} -''', - [error(diag.augmentationModifierMissing, 40, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'base' modifier that the declaration has. +'''); } test_mixin_base_nothing() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base mixin A {} augment mixin A {} -''', - [error(diag.augmentationModifierMissing, 16, 7)], - ); +// [diag.augmentationModifierMissing][column 1][length 7] The augmentation is missing the 'base' modifier that the declaration has. +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/class_used_as_mixin_test.dart b/pkg/analyzer/test/src/diagnostics/class_used_as_mixin_test.dart index 81f45ba43c1..84f99d467d1 100644 --- a/pkg/analyzer/test/src/diagnostics/class_used_as_mixin_test.dart +++ b/pkg/analyzer/test/src/diagnostics/class_used_as_mixin_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(ClassUsedAsMixinTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -26,16 +27,14 @@ class Bar with Comparable { } test_coreLib_dartCoreEnum() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' abstract class A with Enum {} +// ^^^^ +// [diag.classUsedAsMixin] The class 'Enum' can't be used as a mixin because it's neither a mixin class nor a mixin. abstract class B = Object with Enum; -''', - [ - error(diag.classUsedAsMixin, 22, 4), - error(diag.classUsedAsMixin, 61, 4), - ], - ); +// ^^^^ +// [diag.classUsedAsMixin] The class 'Enum' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_coreLib_dartCoreEnum_language219() async { diff --git a/pkg/analyzer/test/src/diagnostics/constructor_body_test.dart b/pkg/analyzer/test/src/diagnostics/constructor_body_test.dart index 9d64ab64404..fe0deec8f90 100644 --- a/pkg/analyzer/test/src/diagnostics/constructor_body_test.dart +++ b/pkg/analyzer/test/src/diagnostics/constructor_body_test.dart @@ -2,7 +2,6 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; @@ -126,15 +125,16 @@ class C { } test_class_secondaryConstructor_constFactory_emptyBody_language305() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // @dart = 3.5 class C { const factory C(); +//^^^^^ +// [diag.constFactory] Only redirecting factory constructors can be declared to be 'const'. +// ^ +// [diag.missingFunctionBody] A function body must be provided. } -''', - [error(diag.constFactory, 27, 5), error(diag.missingFunctionBody, 44, 1)], - ); +'''); } test_class_secondaryConstructor_constFactory_expressionBody() async { @@ -772,17 +772,18 @@ enum E { } test_enum_secondaryConstructor_constFactory_emptyBody_language305() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // @dart = 3.5 enum E { v; const E(); const factory E.named(); +//^^^^^ +// [diag.constFactory] Only redirecting factory constructors can be declared to be 'const'. +// ^ +// [diag.missingFunctionBody] A function body must be provided. } -''', - [error(diag.constFactory, 44, 5), error(diag.missingFunctionBody, 67, 1)], - ); +'''); } test_enum_secondaryConstructor_constFactory_expressionBody() async { @@ -1119,15 +1120,16 @@ extension type const E(int it) { } test_extensionType_secondaryConstructor_constFactory_emptyBody_language305() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // @dart = 3.5 extension type const E(int it) { const factory E.named(); +//^^^^^ +// [diag.constFactory] Only redirecting factory constructors can be declared to be 'const'. +// ^ +// [diag.missingFunctionBody] A function body must be provided. } -''', - [error(diag.constFactory, 50, 5), error(diag.missingFunctionBody, 73, 1)], - ); +'''); } test_extensionType_secondaryConstructor_constFactory_expressionBody() async { diff --git a/pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart b/pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart index 31b02a134d3..ffd7ba75988 100644 --- a/pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart +++ b/pkg/analyzer/test/src/diagnostics/deprecated_member_use_test.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/file_system/file_system.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:analyzer_testing/package_config_file_builder.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -1408,19 +1407,18 @@ class B = Object with A; test_namedParameterMissingName() async { // This is a regression test; previously this code would cause an analyzer // crash in DeprecatedMemberUseVerifier. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { const C({this.}); +// ^^^^^ +// [diag.initializingFormalForNonExistentField] '' isn't a field in the enclosing class. +// ^ +// [diag.missingIdentifier] Expected an identifier. } var z = C(x: ''); -''', - [ - error(diag.initializingFormalForNonExistentField, 21, 5), - error(diag.missingIdentifier, 26, 1), - error(diag.undefinedNamedParameter, 42, 1), - ], - ); +// ^ +// [diag.undefinedNamedParameter] The named parameter 'x' isn't defined. +'''); } test_operator() async { diff --git a/pkg/analyzer/test/src/diagnostics/experimental_member_use_test.dart b/pkg/analyzer/test/src/diagnostics/experimental_member_use_test.dart index cede7a1502c..6359f1047d5 100644 --- a/pkg/analyzer/test/src/diagnostics/experimental_member_use_test.dart +++ b/pkg/analyzer/test/src/diagnostics/experimental_member_use_test.dart @@ -7,6 +7,7 @@ import 'package:analyzer_testing/package_config_file_builder.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { @@ -15,6 +16,7 @@ main() { defineReflectiveTests(ExperimentalInstantiateTest); defineReflectiveTests(ExperimentalMemberUseTest); defineReflectiveTests(ExperimentalMixinTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -779,16 +781,15 @@ class A { } '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:aaa/a.dart'; void f(A a) { a.foo; +// ^^^ +// [diag.experimentalMemberUse] 'foo' is experimental and could be removed or changed at any time. } -''', - [error(diag.experimentalMemberUse, 48, 3)], - ); +'''); } test_field_implicitSetter() async { @@ -1377,16 +1378,15 @@ class A { } '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:aaa/a.dart'; void f() { A(); +//^ +// [diag.experimentalMemberUse] 'A' is experimental and could be removed or changed at any time. } -''', - [error(diag.experimentalMemberUse, 43, 1)], - ); +'''); } test_instanceCreation_unnamedConstructor() async { diff --git a/pkg/analyzer/test/src/diagnostics/field_initializer_factory_constructor_test.dart b/pkg/analyzer/test/src/diagnostics/field_initializer_factory_constructor_test.dart index 5eb1438bcd0..1853a4f4707 100644 --- a/pkg/analyzer/test/src/diagnostics/field_initializer_factory_constructor_test.dart +++ b/pkg/analyzer/test/src/diagnostics/field_initializer_factory_constructor_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(FieldInitializerFactoryConstructorTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -38,21 +39,19 @@ class A { } test_class_fieldFormalParameter_functionTyped_language305() async { - await assertErrorsInCode( - r''' + // TODO(srawlins): Only report one error. Theoretically change Fasta to + // report "Field initializer in factory constructor" as a parse error. + await resolveTestCodeWithDiagnostics(r''' // @dart = 3.5 class A { int Function()? x; factory A(int this.x()); +// ^^^^^^^^^^^^ +// [diag.fieldInitializerFactoryConstructor] Initializing formal parameters can't be used in factory constructors. +// ^ +// [diag.missingFunctionBody] A function body must be provided. } -''', - [ - // TODO(srawlins): Only report one error. Theoretically change Fasta to - // report "Field initializer in factory constructor" as a parse error. - error(diag.fieldInitializerFactoryConstructor, 58, 12), - error(diag.missingFunctionBody, 71, 1), - ], - ); +'''); } test_enum_fieldFormalParameter() async { diff --git a/pkg/analyzer/test/src/diagnostics/invalid_assignment_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_assignment_test.dart index 60becf2e5f6..c02692019a9 100644 --- a/pkg/analyzer/test/src/diagnostics/invalid_assignment_test.dart +++ b/pkg/analyzer/test/src/diagnostics/invalid_assignment_test.dart @@ -6,12 +6,14 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(InvalidAssignment_ImplicitCallReferenceTest); defineReflectiveTests(InvalidAssignmentTest); defineReflectiveTests(InvalidAssignmentWithStrictCastsTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -886,8 +888,7 @@ f(C c) { } test_promotedTypeParameter_regress35306() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A {} class C extends D {} @@ -896,31 +897,31 @@ class D {} void f(X x) { if (x is Y) { A a = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. B b = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. X x2 = x; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'x2' isn't used. Y y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } } -''', - [ - error(diag.unusedLocalVariable, 127, 1), - error(diag.unusedLocalVariable, 140, 1), - error(diag.unusedLocalVariable, 153, 2), - error(diag.unusedLocalVariable, 167, 1), - ], - ); +'''); } void test_recordType_localVariable_initializer() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void f() { (int, int) r = (a: 1, b: 2); +// ^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type '({int a, int b})' can't be assigned to a variable of type '(int, int)'. print(r); } -''', - [error(diag.invalidAssignment, 28, 12)], - ); +'''); } void test_recordType_parameter() async { diff --git a/pkg/analyzer/test/src/diagnostics/invalid_override_of_non_virtual_member_test.dart b/pkg/analyzer/test/src/diagnostics/invalid_override_of_non_virtual_member_test.dart index cf3a55bee9f..dafad5fae7a 100644 --- a/pkg/analyzer/test/src/diagnostics/invalid_override_of_non_virtual_member_test.dart +++ b/pkg/analyzer/test/src/diagnostics/invalid_override_of_non_virtual_member_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(InvalidOverrideOfNonVirtualMemberTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -22,8 +23,7 @@ class InvalidOverrideOfNonVirtualMemberTest extends PubPackageResolutionTest { } test_class_field() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -34,22 +34,14 @@ class C { class B extends C { @override int g = 0; +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [ - error( - diag.invalidOverrideOfNonVirtualMember, - 113, - 1, - messageContains: ["member 'g'", "in 'C'"], - ), - ], - ); +'''); } test_class_field_2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -59,15 +51,14 @@ class C { class B extends C { int g = 0, h = 1; +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [error(diag.invalidOverrideOfNonVirtualMember, 101, 1)], - ); +'''); } test_class_field_overriddenByGetter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -78,22 +69,14 @@ class C { class B extends C { @override int get g => 0; +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [ - error( - diag.invalidOverrideOfNonVirtualMember, - 117, - 1, - messageContains: ["member 'g'", "in 'C'"], - ), - ], - ); +'''); } test_class_field_overriddenBySetter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -104,15 +87,14 @@ class C { class B extends C { @override set g(int v) {} +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [error(diag.invalidOverrideOfNonVirtualMember, 113, 1)], - ); +'''); } test_class_getter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -123,15 +105,14 @@ class C { class B extends C { @override int get g => 0; +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [error(diag.invalidOverrideOfNonVirtualMember, 122, 1)], - ); +'''); } test_class_getter_overriddenByField() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -142,10 +123,10 @@ class C { class B extends C { @override int g = 0; +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [error(diag.invalidOverrideOfNonVirtualMember, 118, 1)], - ); +'''); } test_class_implements_getter() async { @@ -181,8 +162,7 @@ class B implements C { } test_class_method() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -193,15 +173,14 @@ class C { class B extends C { @override void f() {} +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'f' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [error(diag.invalidOverrideOfNonVirtualMember, 115, 1)], - ); +'''); } test_class_setter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -212,15 +191,14 @@ class C { class B extends C { @override set g(int v) {} +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [error(diag.invalidOverrideOfNonVirtualMember, 118, 1)], - ); +'''); } test_class_setter_overriddenByField() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; class C { @@ -231,15 +209,14 @@ class C { class B extends C { @override int g = 0; +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'C' and can't be overridden in subclasses. } -''', - [error(diag.invalidOverrideOfNonVirtualMember, 118, 1)], - ); +'''); } test_mixin_method() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; mixin M { @@ -250,22 +227,14 @@ mixin M { class B with M { @override void f() {} +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'f' is declared non-virtual in 'M' and can't be overridden in subclasses. } -''', - [ - error( - diag.invalidOverrideOfNonVirtualMember, - 111, - 1, - messageContains: ["member 'f'", "in 'M'"], - ), - ], - ); +'''); } test_mixin_setter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:meta/meta.dart'; mixin M { @@ -276,9 +245,9 @@ mixin M { class B with M { @override set g(int v) {} +// ^ +// [diag.invalidOverrideOfNonVirtualMember] The member 'g' is declared non-virtual in 'M' and can't be overridden in subclasses. } -''', - [error(diag.invalidOverrideOfNonVirtualMember, 114, 1)], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/receiver_of_type_never_test.dart b/pkg/analyzer/test/src/diagnostics/receiver_of_type_never_test.dart index 4679045e414..6d3fdde05e5 100644 --- a/pkg/analyzer/test/src/diagnostics/receiver_of_type_never_test.dart +++ b/pkg/analyzer/test/src/diagnostics/receiver_of_type_never_test.dart @@ -2,28 +2,30 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(InvalidUseOfNeverTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class InvalidUseOfNeverTest extends PubPackageResolutionTest { test_binaryExpression_eqEq() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { (throw '') == 1 + 2; +//^^^^^^^^^^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 13, 10), error(diag.deadCode, 24, 8)], - ); +'''); assertResolvedNodeText(findNode.binary('=='), r''' BinaryExpression @@ -57,14 +59,15 @@ BinaryExpression } test_binaryExpression_never_eqEq() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x == 1 + 2; +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 20, 1), error(diag.deadCode, 22, 8)], - ); +'''); assertResolvedNodeText(findNode.binary('x =='), r''' BinaryExpression @@ -93,14 +96,15 @@ BinaryExpression } test_binaryExpression_never_plus() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x + (1 + 2); +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 20, 1), error(diag.deadCode, 22, 9)], - ); +'''); assertResolvedNodeText(findNode.binary('x +'), r''' BinaryExpression @@ -166,14 +170,13 @@ BinaryExpression } test_binaryExpression_neverQ_plus() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { x + (1 + 2); +// ^ +// [diag.uncheckedOperatorInvocationOfNullableValue] The operator '+' can't be unconditionally invoked because the receiver can be 'null'. } -''', - [error(diag.uncheckedOperatorInvocationOfNullableValue, 23, 1)], - ); +'''); assertResolvedNodeText(findNode.binary('x +'), r''' BinaryExpression @@ -206,14 +209,15 @@ BinaryExpression } test_binaryExpression_plus() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { (throw '') + (1 + 2); +//^^^^^^^^^^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 13, 10), error(diag.deadCode, 24, 9)], - ); +'''); assertResolvedNodeText(findNode.binary('+ ('), r''' BinaryExpression @@ -269,36 +273,37 @@ void f(bool c, Never x) { } test_functionExpressionInvocation_never() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x(); +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 20, 1), error(diag.deadCode, 21, 3)], - ); +'''); } test_functionExpressionInvocation_neverQ() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { x(); +//^ +// [diag.uncheckedInvocationOfNullableValue] The function can't be unconditionally invoked because it can be 'null'. } -''', - [error(diag.uncheckedInvocationOfNullableValue, 21, 1)], - ); +'''); } test_indexExpression_never_read() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x[0]; +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 20, 1), error(diag.deadCode, 22, 3)], - ); +'''); assertResolvedNodeText(findNode.index('x[0]'), r''' IndexExpression @@ -318,14 +323,15 @@ IndexExpression } test_indexExpression_never_readWrite() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x[0] += 1 + 2; +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^^^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 20, 1), error(diag.deadCode, 22, 12)], - ); +'''); var assignment = findNode.assignment('[0] +='); assertResolvedNodeText(assignment, r''' @@ -367,14 +373,15 @@ AssignmentExpression } test_indexExpression_never_write() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x[0] = 1 + 2; +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 20, 1), error(diag.deadCode, 22, 11)], - ); +'''); assertResolvedNodeText(findNode.assignment('x[0]'), r''' AssignmentExpression @@ -415,14 +422,13 @@ AssignmentExpression } test_indexExpression_neverQ_read() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { x[0]; +// ^ +// [diag.uncheckedMethodInvocationOfNullableValue] The method '[]' can't be unconditionally invoked because the receiver can be 'null'. } -''', - [error(diag.uncheckedMethodInvocationOfNullableValue, 22, 1)], - ); +'''); assertResolvedNodeText(findNode.index('x[0]'), r''' IndexExpression @@ -442,14 +448,13 @@ IndexExpression } test_indexExpression_neverQ_readWrite() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { x[0] += 1 + 2; +// ^ +// [diag.uncheckedMethodInvocationOfNullableValue] The method '[]' can't be unconditionally invoked because the receiver can be 'null'. } -''', - [error(diag.uncheckedMethodInvocationOfNullableValue, 22, 1)], - ); +'''); var assignment = findNode.assignment('[0] +='); assertResolvedNodeText(assignment, r''' @@ -491,14 +496,13 @@ AssignmentExpression } test_indexExpression_neverQ_write() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { x[0] = 1 + 2; +// ^ +// [diag.uncheckedMethodInvocationOfNullableValue] The method '[]' can't be unconditionally invoked because the receiver can be 'null'. } -''', - [error(diag.uncheckedMethodInvocationOfNullableValue, 22, 1)], - ); +'''); assertResolvedNodeText(findNode.assignment('x[0]'), r''' AssignmentExpression @@ -547,14 +551,15 @@ void f(g, Never x) { } test_methodInvocation_never() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x.foo(1 + 2); +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 20, 1), error(diag.deadCode, 25, 8)], - ); +'''); var node = findNode.methodInvocation('.foo(1 + 2)'); assertResolvedNodeText(node, r''' @@ -591,14 +596,15 @@ MethodInvocation } test_methodInvocation_never_toString() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x.toString(1 + 2); +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 20, 1), error(diag.deadCode, 30, 8)], - ); +'''); var node = findNode.methodInvocation('.toString(1 + 2)'); assertResolvedNodeText(node, r''' @@ -635,14 +641,13 @@ MethodInvocation } test_methodInvocation_neverQ_toString() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { x.toString(1 + 2); +// ^^^^^ +// [diag.extraPositionalArguments] Too many positional arguments: 0 expected, but 1 found. } -''', - [error(diag.extraPositionalArguments, 32, 5)], - ); +'''); var node = findNode.methodInvocation('.toString(1 + 2)'); assertResolvedNodeText(node, r''' @@ -679,14 +684,15 @@ MethodInvocation } test_methodInvocation_toString() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { (throw '').toString(); +//^^^^^^^^^^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. +// ^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.receiverOfTypeNever, 13, 10), error(diag.deadCode, 32, 3)], - ); +'''); var node = findNode.methodInvocation('toString()'); assertResolvedNodeText(node, r''' @@ -714,14 +720,13 @@ MethodInvocation } test_postfixExpression_never_plusPlus() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x++; +//^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. } -''', - [error(diag.receiverOfTypeNever, 20, 1)], - ); +'''); assertResolvedNodeText(findNode.postfix('x++'), r''' PostfixExpression @@ -740,14 +745,13 @@ PostfixExpression } test_postfixExpression_neverQ_plusPlus() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { x++; +// ^^ +// [diag.uncheckedMethodInvocationOfNullableValue] The method '+' can't be unconditionally invoked because the receiver can be 'null'. } -''', - [error(diag.uncheckedMethodInvocationOfNullableValue, 22, 2)], - ); +'''); assertResolvedNodeText(findNode.postfix('x++'), r''' PostfixExpression @@ -767,14 +771,13 @@ PostfixExpression test_prefixExpression_never_plusPlus() async { // Reports 'undefined operator' - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { ++x; +// ^ +// [diag.receiverOfTypeNever] The receiver is of type 'Never', and will never complete with a value. } -''', - [error(diag.receiverOfTypeNever, 22, 1)], - ); +'''); assertResolvedNodeText(findNode.prefix('++x'), r''' PrefixExpression @@ -793,14 +796,13 @@ PrefixExpression } test_prefixExpression_neverQ_plusPlus() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { ++x; +//^^ +// [diag.uncheckedMethodInvocationOfNullableValue] The method '+' can't be unconditionally invoked because the receiver can be 'null'. } -''', - [error(diag.uncheckedMethodInvocationOfNullableValue, 21, 2)], - ); +'''); assertResolvedNodeText(findNode.prefix('++x'), r''' PrefixExpression @@ -819,14 +821,13 @@ PrefixExpression } test_propertyAccess_never_read() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x.foo; +// ^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.deadCode, 22, 4)], - ); +'''); var node = findNode.singlePrefixedIdentifier; assertResolvedNodeText(node, r''' @@ -846,14 +847,13 @@ PrefixedIdentifier } test_propertyAccess_never_read_hashCode() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x.hashCode; +// ^^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.deadCode, 22, 9)], - ); +'''); var node = findNode.singlePrefixedIdentifier; assertResolvedNodeText(node, r''' @@ -873,14 +873,13 @@ PrefixedIdentifier } test_propertyAccess_never_readWrite() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x.foo += 0; +// ^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.deadCode, 29, 2)], - ); +'''); var assignment = findNode.assignment('foo += 0'); assertResolvedNodeText(assignment, r''' @@ -912,14 +911,13 @@ AssignmentExpression } test_propertyAccess_never_tearOff_toString() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x.toString; +// ^^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.deadCode, 22, 9)], - ); +'''); var node = findNode.singlePrefixedIdentifier; assertResolvedNodeText(node, r''' @@ -939,14 +937,13 @@ PrefixedIdentifier } test_propertyAccess_never_write() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never x) { x.foo = 0; +// ^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.deadCode, 28, 2)], - ); +'''); var assignment = findNode.assignment('foo = 0'); assertResolvedNodeText(assignment, r''' @@ -978,14 +975,13 @@ AssignmentExpression } test_propertyAccess_neverQ_read() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f(Never? x) { x.foo; +// ^^^ +// [diag.uncheckedPropertyAccessOfNullableValue] The property 'foo' can't be unconditionally accessed because the receiver can be 'null'. } -''', - [error(diag.uncheckedPropertyAccessOfNullableValue, 23, 3)], - ); +'''); var node = findNode.singlePrefixedIdentifier; assertResolvedNodeText(node, r''' @@ -1053,14 +1049,13 @@ PrefixedIdentifier } test_propertyAccess_toString() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { (throw '').toString; +// ^^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.deadCode, 24, 9)], - ); +'''); var node = findNode.singlePropertyAccess; assertResolvedNodeText(node, r''' @@ -1084,14 +1079,13 @@ PropertyAccess } test_throw_getter_hashCode() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { (throw '').hashCode; +// ^^^^^^^^^ +// [diag.deadCode] Dead code. } -''', - [error(diag.deadCode, 24, 9)], - ); +'''); var node = findNode.singlePropertyAccess; assertResolvedNodeText(node, r''' diff --git a/pkg/analyzer/test/src/diagnostics/recursive_interface_inheritance_implements_test.dart b/pkg/analyzer/test/src/diagnostics/recursive_interface_inheritance_implements_test.dart index 2477fa76ee2..0c9cf5a7720 100644 --- a/pkg/analyzer/test/src/diagnostics/recursive_interface_inheritance_implements_test.dart +++ b/pkg/analyzer/test/src/diagnostics/recursive_interface_inheritance_implements_test.dart @@ -2,7 +2,6 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; @@ -54,15 +53,13 @@ class B = A with M implements B; } test_mixin() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin A implements B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, A. mixin B implements A {} -''', - [ - error(diag.recursiveInterfaceInheritance, 6, 1), - error(diag.recursiveInterfaceInheritance, 30, 1), - ], - ); +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, A. +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/subtype_of_base_is_not_base_final_or_sealed_test.dart b/pkg/analyzer/test/src/diagnostics/subtype_of_base_is_not_base_final_or_sealed_test.dart index bd980c1a8b6..51ea3580d97 100644 --- a/pkg/analyzer/test/src/diagnostics/subtype_of_base_is_not_base_final_or_sealed_test.dart +++ b/pkg/analyzer/test/src/diagnostics/subtype_of_base_is_not_base_final_or_sealed_test.dart @@ -7,50 +7,34 @@ import 'package:analyzer_testing/analysis_rule/analysis_rule.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SubtypeOfBaseIsNotBaseFinalOrSealedTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SubtypeOfBaseIsNotBaseFinalOrSealedTest extends PubPackageResolutionTest { test_class_extends() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} class B extends A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 22, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_class_extends_multiple() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} base class B extends A {} class C extends A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 48, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_class_extends_outside() async { @@ -58,21 +42,12 @@ class C extends A {} base class A {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; class B extends A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 23, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_class_extends_outside_viaLanguage219AndCore() async { @@ -85,39 +60,21 @@ abstract class A implements LinkedListEntry {} await resolveFile2(a); assertNoErrorsInResult(); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; abstract class B extends A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 32, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'LinkedListEntry' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'LinkedListEntry' is 'base'. +'''); } test_class_implements() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} class B implements A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 22, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_class_implements_outside() async { @@ -125,22 +82,14 @@ class B implements A {} base class A {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; class B implements A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 23, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - this.error(diag.baseClassImplementedOutsideOfLibrary, 36, 1), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +// ^ +// [diag.baseClassImplementedOutsideOfLibrary] The class 'A' can't be implemented outside of its library because it's a base class. +'''); } test_class_implements_outside_viaLanguage219AndCore() async { @@ -153,141 +102,65 @@ abstract class A implements LinkedListEntry {} await resolveFile2(a); assertNoErrorsInResult(); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; abstract class B implements A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 32, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'LinkedListEntry' is 'base'.", - ), - this.error(diag.baseClassImplementedOutsideOfLibrary, 45, 1), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'LinkedListEntry' is 'base'. +// ^ +// [diag.baseClassImplementedOutsideOfLibrary] The class 'LinkedListEntry' can't be implemented outside of its library because it's a base class. +'''); } test_class_sealed_extends() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} +// ^ +// [context 1] The type 'B' is a subtype of 'A', and 'A' is defined here. sealed class B extends A {} class C extends B {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 50, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 11, - 1, - textContains: [ - "The type 'B' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_class_sealed_extends_interface_implements_base() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} +// ^ +// [context 1] The type 'C' is a subtype of 'A', and 'A' is defined here. interface class B {} sealed class C extends B implements A {} class D extends C {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 84, - 1, - text: - "The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 11, - 1, - textContains: [ - "The type 'C' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_class_sealed_extends_interface_with_base() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base mixin A {} +// ^ +// [context 1] The type 'C' is a subtype of 'A', and 'A' is defined here. interface class B {} sealed class C extends B with A {} class D extends C {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 78, - 1, - text: - "The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 11, - 1, - textContains: [ - "The type 'C' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_class_sealed_extends_multiple() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} +// ^ +// [context 1] The type 'C' is a subtype of 'A', and 'A' is defined here. sealed class B extends A {} sealed class C extends B {} class D extends C {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 78, - 1, - text: - "The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 11, - 1, - textContains: [ - "The type 'C' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_class_sealed_extends_outside() async { @@ -324,188 +197,85 @@ class C extends B {} } test_class_sealed_extends_unordered() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C extends B {} +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. sealed class B extends A {} base class A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 6, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 60, - 1, - textContains: [ - "The type 'B' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [context 1] The type 'B' is a subtype of 'A', and 'A' is defined here. +'''); } test_class_sealed_implements() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} +// ^ +// [context 1] The type 'B' is a subtype of 'A', and 'A' is defined here. sealed class B implements A {} class C implements B {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 53, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 11, - 1, - textContains: [ - "The type 'B' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_classTypeAlias() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} mixin B {} class C = Object with B implements A; -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 33, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_classTypeAlias_interface() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} mixin B {} interface class C = Object with B implements A; -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 43, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_classTypeAlias_sealed() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} +// ^ +// [context 1] The type 'AA' is a subtype of 'A', and 'A' is defined here. sealed class AA extends A {} mixin B {} class C = Object with B implements AA; -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 62, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 11, - 1, - textContains: [ - "The type 'AA' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_classTypeAlias_sealed_interface() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base class A {} +// ^ +// [context 1] The type 'AA' is a subtype of 'A', and 'A' is defined here. sealed class AA extends A {} mixin B {} interface class C = Object with B implements AA; -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 72, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 11, - 1, - textContains: [ - "The type 'AA' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_mixinClass_sealed() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base mixin class A {} +// ^ +// [context 1] The type 'B' is a subtype of 'A', and 'A' is defined here. sealed class B with A {} class C extends B {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 53, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - contextMessages: [ - contextMessage( - testFile, - 17, - 1, - textContains: [ - "The type 'B' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_mixinClass_sealed_outside() async { @@ -542,21 +312,12 @@ class C extends B {} } test_mixinClass_with() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' base mixin class A {} class B with A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 28, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } test_mixinClass_with_outside() async { @@ -564,20 +325,11 @@ class B with A {} base mixin class A {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; class B with A {} -''', - [ - this.error( - diag.subtypeOfBaseIsNotBaseFinalOrSealed, - 23, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'.", - ), - ], - ); +// ^ +// [diag.subtypeOfBaseIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'base'. +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart b/pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart index e893ec68252..e15df8301d4 100644 --- a/pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart +++ b/pkg/analyzer/test/src/diagnostics/subtype_of_ffi_class_test.dart @@ -2,112 +2,104 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SubtypeOfFfiClassInExtendsTest); defineReflectiveTests(SubtypeOfFfiClassInImplementsTest); defineReflectiveTests(SubtypeOfFfiClassInWithTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SubtypeOfFfiClassInExtendsTest extends PubPackageResolutionTest { test_Double() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class C extends Double {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 41, 6)], - ); +// ^^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Double' can't be extended outside of its library because it's a final class. +'''); } test_Double_language219() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // @dart=2.19 import 'dart:ffi'; class C extends Double {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 49, 6)], - ); +// ^^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Double' can't be extended outside of its library because it's a final class. +'''); } test_Finalizable() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Finalizable {} -''', - [error(diag.noGenerativeConstructorsInSuperclass, 35, 11)], - ); +// ^^^^^^^^^^^ +// [diag.noGenerativeConstructorsInSuperclass] The class 'C' can't extend 'Finalizable' because 'Finalizable' only has factory constructors (no generative constructors), and 'C' has at least one generative constructor. +'''); } test_Float() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Float {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 5)], - ); +// ^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Float' can't be extended outside of its library because it's a final class. +'''); } test_Int16() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Int16 {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 5)], - ); +// ^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Int16' can't be extended outside of its library because it's a final class. +'''); } test_Int32() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Int32 {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 5)], - ); +// ^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Int32' can't be extended outside of its library because it's a final class. +'''); } test_Int64() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Int64 {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 5)], - ); +// ^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Int64' can't be extended outside of its library because it's a final class. +'''); } test_Int8() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Int8 {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 4)], - ); +// ^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Int8' can't be extended outside of its library because it's a final class. +'''); } test_Pointer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Pointer { +// ^^^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Pointer' can't be extended outside of its library because it's a final class. external factory C(); } -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 7)], - ); +'''); } test_Struct() async { @@ -120,43 +112,39 @@ final class C extends Struct { } test_Uint16() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Uint16 {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 6)], - ); +// ^^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Uint16' can't be extended outside of its library because it's a final class. +'''); } test_Uint32() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Uint32 {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 6)], - ); +// ^^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Uint32' can't be extended outside of its library because it's a final class. +'''); } test_Uint64() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Uint64 {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 6)], - ); +// ^^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Uint64' can't be extended outside of its library because it's a final class. +'''); } test_Uint8() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Uint8 {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 5)], - ); +// ^^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Uint8' can't be extended outside of its library because it's a final class. +'''); } test_Union() async { @@ -169,47 +157,43 @@ final class C extends Union { } test_Void() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C extends Void {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 35, 4)], - ); +// ^^^^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'Void' can't be extended outside of its library because it's a final class. +'''); } } @reflectiveTest class SubtypeOfFfiClassInImplementsTest extends PubPackageResolutionTest { test_Double() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Double {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 6)], - ); +// ^^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Double' can't be implemented outside of its library because it's a final class. +'''); } test_Double_language219() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // @dart=2.19 import 'dart:ffi'; class C implements Double {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 52, 6)], - ); +// ^^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Double' can't be implemented outside of its library because it's a final class. +'''); } test_Double_prefixed() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi' as ffi; class C implements ffi.Double {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 45, 10)], - ); +// ^^^^^^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Double' can't be implemented outside of its library because it's a final class. +'''); } test_Finalizable() async { @@ -220,299 +204,269 @@ class C implements Finalizable {} } test_Float() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Float {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 5)], - ); +// ^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Float' can't be implemented outside of its library because it's a final class. +'''); } test_Int16() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Int16 {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 5)], - ); +// ^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Int16' can't be implemented outside of its library because it's a final class. +'''); } test_Int32() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Int32 {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 5)], - ); +// ^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Int32' can't be implemented outside of its library because it's a final class. +'''); } test_Int64() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Int64 {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 5)], - ); +// ^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Int64' can't be implemented outside of its library because it's a final class. +'''); } test_Int8() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Int8 {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 4)], - ); +// ^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Int8' can't be implemented outside of its library because it's a final class. +'''); } test_Pointer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Pointer {} -''', - [ - error(diag.finalClassImplementedOutsideOfLibrary, 38, 7), - error(diag.nonAbstractClassInheritsAbstractMemberOne, 25, 1), - ], - ); +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'Pointer.cast'. +// ^^^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Pointer' can't be implemented outside of its library because it's a final class. +'''); } test_Struct() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class C implements Struct {} -''', - [error(diag.baseClassImplementedOutsideOfLibrary, 44, 6)], - ); +// ^^^^^^ +// [diag.baseClassImplementedOutsideOfLibrary] The class 'Struct' can't be implemented outside of its library because it's a base class. +'''); } test_Uint16() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Uint16 {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 6)], - ); +// ^^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Uint16' can't be implemented outside of its library because it's a final class. +'''); } test_Uint32() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Uint32 {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 6)], - ); +// ^^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Uint32' can't be implemented outside of its library because it's a final class. +'''); } test_Uint64() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Uint64 {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 6)], - ); +// ^^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Uint64' can't be implemented outside of its library because it's a final class. +'''); } test_Uint8() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Uint8 {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 5)], - ); +// ^^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Uint8' can't be implemented outside of its library because it's a final class. +'''); } test_Union() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class C implements Union {} -''', - [error(diag.baseClassImplementedOutsideOfLibrary, 44, 5)], - ); +// ^^^^^ +// [diag.baseClassImplementedOutsideOfLibrary] The class 'Union' can't be implemented outside of its library because it's a base class. +'''); } test_Void() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C implements Void {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 38, 4)], - ); +// ^^^^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'Void' can't be implemented outside of its library because it's a final class. +'''); } } @reflectiveTest class SubtypeOfFfiClassInWithTest extends PubPackageResolutionTest { test_Double() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Double {} -''', - [error(diag.classUsedAsMixin, 32, 6)], - ); +// ^^^^^^ +// [diag.classUsedAsMixin] The class 'Double' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Double_language219() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // @dart=2.19 import 'dart:ffi'; class C with Double {} -''', - [error(diag.classUsedAsMixin, 46, 6)], - ); +// ^^^^^^ +// [diag.classUsedAsMixin] The class 'Double' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Double_prefixed() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi' as ffi; class C with ffi.Double {} -''', - [error(diag.classUsedAsMixin, 39, 10)], - ); +// ^^^^^^^^^^ +// [diag.classUsedAsMixin] The class 'Double' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Float() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Float {} -''', - [error(diag.classUsedAsMixin, 32, 5)], - ); +// ^^^^^ +// [diag.classUsedAsMixin] The class 'Float' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Int16() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Int16 {} -''', - [error(diag.classUsedAsMixin, 32, 5)], - ); +// ^^^^^ +// [diag.classUsedAsMixin] The class 'Int16' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Int32() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Int32 {} -''', - [error(diag.classUsedAsMixin, 32, 5)], - ); +// ^^^^^ +// [diag.classUsedAsMixin] The class 'Int32' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Int64() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Int64 {} -''', - [error(diag.classUsedAsMixin, 32, 5)], - ); +// ^^^^^ +// [diag.classUsedAsMixin] The class 'Int64' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Int8() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Int8 {} -''', - [error(diag.classUsedAsMixin, 32, 4)], - ); +// ^^^^ +// [diag.classUsedAsMixin] The class 'Int8' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Pointer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Pointer {} -''', - [error(diag.classUsedAsMixin, 32, 7)], - ); +// ^^^^^^^ +// [diag.classUsedAsMixin] The class 'Pointer' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Struct() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class C with Struct {} -''', - [error(diag.classUsedAsMixin, 38, 6)], - ); +// ^^^^^^ +// [diag.classUsedAsMixin] The class 'Struct' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Uint16() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Uint16 {} -''', - [error(diag.classUsedAsMixin, 32, 6)], - ); +// ^^^^^^ +// [diag.classUsedAsMixin] The class 'Uint16' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Uint32() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Uint32 {} -''', - [error(diag.classUsedAsMixin, 32, 6)], - ); +// ^^^^^^ +// [diag.classUsedAsMixin] The class 'Uint32' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Uint64() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Uint64 {} -''', - [error(diag.classUsedAsMixin, 32, 6)], - ); +// ^^^^^^ +// [diag.classUsedAsMixin] The class 'Uint64' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Uint8() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Uint8 {} -''', - [error(diag.classUsedAsMixin, 32, 5)], - ); +// ^^^^^ +// [diag.classUsedAsMixin] The class 'Uint8' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Union() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class C with Union {} -''', - [error(diag.classUsedAsMixin, 38, 5)], - ); +// ^^^^^ +// [diag.classUsedAsMixin] The class 'Union' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } test_Void() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; class C with Void {} -''', - [error(diag.classUsedAsMixin, 32, 4)], - ); +// ^^^^ +// [diag.classUsedAsMixin] The class 'Void' can't be used as a mixin because it's neither a mixin class nor a mixin. +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/subtype_of_final_is_not_base_final_or_sealed_test.dart b/pkg/analyzer/test/src/diagnostics/subtype_of_final_is_not_base_final_or_sealed_test.dart index 8515d3b3818..9217541e099 100644 --- a/pkg/analyzer/test/src/diagnostics/subtype_of_final_is_not_base_final_or_sealed_test.dart +++ b/pkg/analyzer/test/src/diagnostics/subtype_of_final_is_not_base_final_or_sealed_test.dart @@ -3,14 +3,15 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; -import 'package:analyzer_testing/analysis_rule/analysis_rule.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SubtypeOfFinalIsNotBaseFinalOrSealedTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -18,21 +19,12 @@ main() { class SubtypeOfFinalIsNotBaseFinalOrSealedTest extends PubPackageResolutionTest { test_class_extends() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} class B extends A {} -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 23, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } @SkippedTest() // TODO(scheglov): implement augmentation @@ -49,7 +41,7 @@ final class A {} class B {} ''', [ - this.error( + error( diag.subtypeOfFinalIsNotBaseFinalOrSealed, 38, 1, @@ -67,13 +59,12 @@ class B {} final class A {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; class B extends A {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 33, 1)], - ); +// ^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'A' can't be extended outside of its library because it's a final class. +'''); } test_class_extends_outside_viaLanguage219AndCore() async { @@ -89,42 +80,24 @@ class A implements MapEntry { await resolveFile2(a); assertNoErrorsInResult(); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; class B extends A { +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'MapEntry' is 'final'. int get key => 0; int get value => 1; } -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 23, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'MapEntry' is 'final'.", - ), - ], - ); +'''); } test_class_implements() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} class B implements A {} -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 23, - 1, - text: - "The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed] The type 'B' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } test_class_implements_outside() async { @@ -134,13 +107,12 @@ class B implements A {} final class A {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; class B implements A {} -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 36, 1)], - ); +// ^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'A' can't be implemented outside of its library because it's a final class. +'''); } test_class_implements_outside_viaLanguage219AndCore() async { @@ -159,16 +131,15 @@ class A implements MapEntry { await resolveFile2(a); assertNoErrorsInResult(); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; class B implements A { +// ^ +// [diag.finalClassImplementedOutsideOfLibrary] The class 'MapEntry' can't be implemented outside of its library because it's a final class. int get key => 0; int get value => 1; } -''', - [error(diag.finalClassImplementedOutsideOfLibrary, 36, 1)], - ); +'''); } test_class_on_outside() async { @@ -178,72 +149,37 @@ class B implements A { final class A {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; mixin B on A {} -''', - [error(diag.finalClassUsedAsMixinConstraintOutsideOfLibrary, 28, 1)], - ); +// ^ +// [diag.finalClassUsedAsMixinConstraintOutsideOfLibrary] The class 'A' can't be used as a mixin superclass constraint outside of its library because it's a final class. +'''); } test_class_sealed_extends() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} +// ^ +// [context 1] The type 'B' is a subtype of 'A', and 'A' is defined here. sealed class B extends A {} class C extends B {} -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 51, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - contextMessages: [ - contextMessage( - testFile, - 12, - 1, - textContains: [ - "The type 'B' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } test_class_sealed_extends_multiple() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} +// ^ +// [context 1] The type 'C' is a subtype of 'A', and 'A' is defined here. sealed class B extends A {} sealed class C extends B {} class D extends C {} -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 79, - 1, - text: - "The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - contextMessages: [ - contextMessage( - testFile, - 12, - 1, - textContains: [ - "The type 'C' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed][context 1] The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } test_class_sealed_extends_outside() async { @@ -253,199 +189,95 @@ class D extends C {} final class A {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; sealed class B extends A {} +// ^ +// [diag.finalClassExtendedOutsideOfLibrary] The class 'A' can't be extended outside of its library because it's a final class. class C extends B {} -''', - [error(diag.finalClassExtendedOutsideOfLibrary, 40, 1)], - ); +'''); } test_class_sealed_extends_unordered() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C extends B {} +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. sealed class B extends A {} final class A {} -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 6, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - contextMessages: [ - contextMessage( - testFile, - 61, - 1, - textContains: [ - "The type 'B' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [context 1] The type 'B' is a subtype of 'A', and 'A' is defined here. +'''); } test_class_sealed_implements() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} +// ^ +// [context 1] The type 'B' is a subtype of 'A', and 'A' is defined here. sealed class B implements A {} class C implements B {} -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 54, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - contextMessages: [ - contextMessage( - testFile, - 12, - 1, - textContains: [ - "The type 'B' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } test_class_sealed_with_extends() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' mixin A {} final class B {} +// ^ +// [context 1] The type 'C' is a subtype of 'B', and 'B' is defined here. sealed class C extends B with A {} class D extends C {} -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 69, - 1, - text: - "The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'B' is 'final'.", - contextMessages: [ - contextMessage( - testFile, - 23, - 1, - textContains: [ - "The type 'C' is a subtype of 'B', and 'B' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed][context 1] The type 'D' must be 'base', 'final' or 'sealed' because the supertype 'B' is 'final'. +'''); } test_classTypeAlias() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} mixin B {} class C = Object with B implements A; -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 34, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } test_classTypeAlias_interface() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} mixin B {} interface class C = Object with B implements A; -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 44, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } test_classTypeAlias_sealed() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} +// ^ +// [context 1] The type 'AA' is a subtype of 'A', and 'A' is defined here. sealed class AA extends A {} mixin B {} class C = Object with B implements AA; -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 63, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - contextMessages: [ - contextMessage( - testFile, - 12, - 1, - textContains: [ - "The type 'AA' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } test_classTypeAlias_sealed_interface() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' final class A {} +// ^ +// [context 1] The type 'AA' is a subtype of 'A', and 'A' is defined here. sealed class AA extends A {} mixin B {} interface class C = Object with B implements AA; -''', - [ - this.error( - diag.subtypeOfFinalIsNotBaseFinalOrSealed, - 73, - 1, - text: - "The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'.", - contextMessages: [ - contextMessage( - testFile, - 12, - 1, - textContains: [ - "The type 'AA' is a subtype of 'A', and 'A' is defined here.", - ], - ), - ], - ), - ], - ); +// ^ +// [diag.subtypeOfFinalIsNotBaseFinalOrSealed][context 1] The type 'C' must be 'base', 'final' or 'sealed' because the supertype 'A' is 'final'. +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/subtype_of_sealed_class_test.dart b/pkg/analyzer/test/src/diagnostics/subtype_of_sealed_class_test.dart index d5550dc2f87..094a4dade4d 100644 --- a/pkg/analyzer/test/src/diagnostics/subtype_of_sealed_class_test.dart +++ b/pkg/analyzer/test/src/diagnostics/subtype_of_sealed_class_test.dart @@ -2,15 +2,16 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:analyzer_testing/package_config_file_builder.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; void main() { defineReflectiveSuite(() { defineReflectiveTests(SubtypeOfSealedClassTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -34,13 +35,11 @@ import 'package:meta/meta.dart'; @sealed class Foo {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:foo/foo.dart'; class Bar extends Foo {} -''', - [error(diag.subtypeOfSealedClass, 31, 24)], - ); +// [diag.subtypeOfSealedClass][column 1][length 24] The class 'Foo' shouldn't be extended, mixed in, or implemented because it's sealed. +'''); } test_implementingSealedClass() async { @@ -55,13 +54,11 @@ import 'package:meta/meta.dart'; @sealed class Foo {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:foo/foo.dart'; class Bar implements Foo {} -''', - [error(diag.subtypeOfSealedClass, 31, 27)], - ); +// [diag.subtypeOfSealedClass][column 1][length 27] The class 'Foo' shouldn't be extended, mixed in, or implemented because it's sealed. +'''); } test_mixinApplicationOfSealedClass() async { @@ -77,14 +74,12 @@ import 'package:meta/meta.dart'; @sealed class Foo {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:foo/foo.dart'; class Bar1 {} class Bar2 = Bar1 with Foo; -''', - [error(diag.subtypeOfSealedClass, 45, 27)], - ); +// [diag.subtypeOfSealedClass][column 1][length 27] The class 'Foo' shouldn't be extended, mixed in, or implemented because it's sealed. +'''); } test_mixinApplicationOfSealedMixin() async { @@ -99,14 +94,12 @@ import 'package:meta/meta.dart'; @sealed mixin Foo {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:foo/foo.dart'; class Bar1 {} class Bar2 = Bar1 with Foo; -''', - [error(diag.subtypeOfSealedClass, 45, 27)], - ); +// [diag.subtypeOfSealedClass][column 1][length 27] The class 'Foo' shouldn't be extended, mixed in, or implemented because it's sealed. +'''); } test_mixingInWithSealedMixin() async { @@ -121,13 +114,11 @@ import 'package:meta/meta.dart'; @sealed mixin Foo {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:foo/foo.dart'; class Bar extends Object with Foo {} -''', - [error(diag.subtypeOfSealedClass, 31, 36)], - ); +// [diag.subtypeOfSealedClass][column 1][length 36] The class 'Foo' shouldn't be extended, mixed in, or implemented because it's sealed. +'''); } test_mixinImplementsSealedClass() async { @@ -142,13 +133,11 @@ import 'package:meta/meta.dart'; @sealed class Foo {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'package:foo/foo.dart'; mixin Bar implements Foo {} -''', - [error(diag.subtypeOfSealedClass, 31, 27)], - ); +// [diag.subtypeOfSealedClass][column 1][length 27] The class 'Foo' shouldn't be extended, mixed in, or implemented because it's sealed. +'''); } test_withinLibrary_language219() async { diff --git a/pkg/analyzer/test/src/diagnostics/subtype_of_struct_class_test.dart b/pkg/analyzer/test/src/diagnostics/subtype_of_struct_class_test.dart index f0fb19dbc1d..0357b7e945d 100644 --- a/pkg/analyzer/test/src/diagnostics/subtype_of_struct_class_test.dart +++ b/pkg/analyzer/test/src/diagnostics/subtype_of_struct_class_test.dart @@ -2,53 +2,51 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SubtypeOfStructClassInExtendsTest); defineReflectiveTests(SubtypeOfStructClassInImplementsTest); defineReflectiveTests(SubtypeOfStructClassInWithTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SubtypeOfStructClassInExtendsTest extends PubPackageResolutionTest { test_extends_struct() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class S extends Struct { external Pointer notEmpty; } final class C extends S {} -''', - [error(diag.subtypeOfStructClassInExtends, 103, 1)], - ); +// ^ +// [diag.subtypeOfStructClassInExtends] The class 'C' can't extend 'S' because 'S' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. +'''); } test_extends_union() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class S extends Union { external Pointer notEmpty; } final class C extends S {} -''', - [error(diag.subtypeOfStructClassInExtends, 102, 1)], - ); +// ^ +// [diag.subtypeOfStructClassInExtends] The class 'C' can't extend 'S' because 'S' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. +'''); } } @reflectiveTest class SubtypeOfStructClassInImplementsTest extends PubPackageResolutionTest { test_implements_abi_specific_int() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; @AbiSpecificIntegerMapping({ Abi.androidArm: Uint32(), @@ -57,29 +55,25 @@ final class AbiSpecificInteger1 extends AbiSpecificInteger { const AbiSpecificInteger1(); } final class AbiSpecificInteger4 implements AbiSpecificInteger1 { +// ^^^^^^^^^^^^^^^^^^^ +// [diag.baseClassImplementedOutsideOfLibrary] The class 'AbiSpecificInteger' can't be implemented outside of its library because it's a base class. +// [diag.subtypeOfStructClassInImplements] The class 'AbiSpecificInteger4' can't implement 'AbiSpecificInteger1' because 'AbiSpecificInteger1' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. const AbiSpecificInteger4(); } -''', - [ - error(diag.baseClassImplementedOutsideOfLibrary, 216, 19), - error(diag.subtypeOfStructClassInImplements, 216, 19), - ], - ); +'''); } test_implements_struct() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class S extends Struct {} +// ^ +// [diag.emptyStruct] The class 'S' can't be empty because it's a subclass of 'Struct'. final class C implements S {} -''', - [ - error(diag.emptyStruct, 31, 1), - error(diag.baseClassImplementedOutsideOfLibrary, 76, 1), - error(diag.subtypeOfStructClassInImplements, 76, 1), - ], - ); +// ^ +// [diag.baseClassImplementedOutsideOfLibrary] The class 'Struct' can't be implemented outside of its library because it's a base class. +// [diag.subtypeOfStructClassInImplements] The class 'C' can't implement 'S' because 'S' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. +'''); } test_implements_struct_prefixed() async { @@ -87,55 +81,43 @@ final class C implements S {} import 'dart:ffi'; final class S extends Struct {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'lib1.dart' as lib1; class C implements lib1.S {} -''', - [ - error(diag.baseClassImplementedOutsideOfLibrary, 47, 6), - error(diag.finalClassImplementedOutsideOfLibrary, 47, 6), - error(diag.subtypeOfStructClassInImplements, 47, 6), - ], - ); +// ^^^^^^ +// [diag.baseClassImplementedOutsideOfLibrary] The class 'Struct' can't be implemented outside of its library because it's a base class. +// [diag.finalClassImplementedOutsideOfLibrary] The class 'S' can't be implemented outside of its library because it's a final class. +// [diag.subtypeOfStructClassInImplements] The class 'C' can't implement 'S' because 'S' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. +'''); } test_implements_union() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class S extends Union {} +// ^ +// [diag.emptyStruct] The class 'S' can't be empty because it's a subclass of 'Union'. final class C implements S {} -''', - [ - error(diag.emptyStruct, 31, 1), - error(diag.baseClassImplementedOutsideOfLibrary, 75, 1), - error(diag.subtypeOfStructClassInImplements, 75, 1), - ], - ); +// ^ +// [diag.baseClassImplementedOutsideOfLibrary] The class 'Union' can't be implemented outside of its library because it's a base class. +// [diag.subtypeOfStructClassInImplements] The class 'C' can't implement 'S' because 'S' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. +'''); } } @reflectiveTest class SubtypeOfStructClassInWithTest extends PubPackageResolutionTest { test_with_struct() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class S extends Struct {} +// ^ +// [diag.emptyStruct] The class 'S' can't be empty because it's a subclass of 'Struct'. final class C with S {} -''', - [ - error(diag.emptyStruct, 31, 1), - error(diag.classUsedAsMixin, 70, 1), - error( - diag.subtypeOfStructClassInWith, - 70, - 1, - messageContains: ["class 'C'", "mix in 'S'"], - ), - ], - ); +// ^ +// [diag.classUsedAsMixin] The class 'S' can't be used as a mixin because it's neither a mixin class nor a mixin. +// [diag.subtypeOfStructClassInWith] The class 'C' can't mix in 'S' because 'S' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. +'''); } test_with_struct_prefixed() async { @@ -143,36 +125,26 @@ final class C with S {} import 'dart:ffi'; final class S extends Struct {} '''); - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'lib1.dart' as lib1; class C with lib1.S {} -''', - [ - error(diag.classUsedAsMixin, 42, 6), - error( - diag.subtypeOfStructClassInWith, - 42, - 6, - messageContains: ["class 'C'", "mix in 'S'"], - ), - ], - ); +// ^^^^^^ +// [diag.classUsedAsMixin] The class 'S' can't be used as a mixin because it's neither a mixin class nor a mixin. +// [diag.subtypeOfStructClassInWith] The class 'C' can't mix in 'S' because 'S' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. +'''); } test_with_union() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:ffi'; final class S extends Union {} +// ^ +// [diag.emptyStruct] The class 'S' can't be empty because it's a subclass of 'Union'. final class C with S {} -''', - [ - error(diag.emptyStruct, 31, 1), - error(diag.classUsedAsMixin, 69, 1), - error(diag.subtypeOfStructClassInWith, 69, 1), - ], - ); +// ^ +// [diag.classUsedAsMixin] The class 'S' can't be used as a mixin because it's neither a mixin class nor a mixin. +// [diag.subtypeOfStructClassInWith] The class 'C' can't mix in 'S' because 'S' is a subtype of 'Struct', 'Union', or 'AbiSpecificInteger'. +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/super_formal_parameter_type_is_not_subtype_of_associated_test.dart b/pkg/analyzer/test/src/diagnostics/super_formal_parameter_type_is_not_subtype_of_associated_test.dart index d64245ce5fd..bf2e16247c4 100644 --- a/pkg/analyzer/test/src/diagnostics/super_formal_parameter_type_is_not_subtype_of_associated_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_formal_parameter_type_is_not_subtype_of_associated_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperFormalParameterTypeIsNotSubtypeOfAssociatedTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -17,18 +18,17 @@ main() { class SuperFormalParameterTypeIsNotSubtypeOfAssociatedTest extends PubPackageResolutionTest { test_generic_requiredPositional_explicit_notSubtype() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(T a); } class B extends A { B(num super.a); +// ^ +// [diag.superFormalParameterTypeIsNotSubtypeOfAssociated] The type 'num' of this parameter isn't a subtype of the type 'int' of the associated super constructor parameter. } -''', - [error(diag.superFormalParameterTypeIsNotSubtypeOfAssociated, 65, 1)], - ); +'''); } test_generic_requiredPositional_explicit_same() async { @@ -56,18 +56,17 @@ class B extends A { } test_requiredNamed_explicit_notSubtype() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A({required int a}); } class B extends A { B({required num super.a}); +// ^ +// [diag.superFormalParameterTypeIsNotSubtypeOfAssociated] The type 'num' of this parameter isn't a subtype of the type 'int' of the associated super constructor parameter. } -''', - [error(diag.superFormalParameterTypeIsNotSubtypeOfAssociated, 80, 1)], - ); +'''); } test_requiredNamed_explicit_same() async { @@ -107,34 +106,32 @@ class B extends A { } test_requiredPositional_explicit_notSubtype() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int a); } class B extends A { B(num super.a); +// ^ +// [diag.superFormalParameterTypeIsNotSubtypeOfAssociated] The type 'num' of this parameter isn't a subtype of the type 'int' of the associated super constructor parameter. } -''', - [error(diag.superFormalParameterTypeIsNotSubtypeOfAssociated, 59, 1)], - ); +'''); } /// No implicit coercions, like downcast from `dynamic`. test_requiredPositional_explicit_notSubtype_dynamic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int a); } class B extends A { B(dynamic super.a); +// ^ +// [diag.superFormalParameterTypeIsNotSubtypeOfAssociated] The type 'dynamic' of this parameter isn't a subtype of the type 'int' of the associated super constructor parameter. } -''', - [error(diag.superFormalParameterTypeIsNotSubtypeOfAssociated, 63, 1)], - ); +'''); } test_requiredPositional_explicit_same() async { diff --git a/pkg/analyzer/test/src/diagnostics/super_formal_parameter_without_associated_named_test.dart b/pkg/analyzer/test/src/diagnostics/super_formal_parameter_without_associated_named_test.dart index 8945dfc40fb..0a2f685b0f8 100644 --- a/pkg/analyzer/test/src/diagnostics/super_formal_parameter_without_associated_named_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_formal_parameter_without_associated_named_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperFormalParameterWithoutAssociatedNamedTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -17,54 +18,50 @@ main() { class SuperFormalParameterWithoutAssociatedNamedTest extends PubPackageResolutionTest { test_explicit_optional() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A { B({super.a}) : super(); +// ^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedNamed, 43, 1)], - ); +'''); } test_explicit_required() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A { B({required super.a}) : super(); +// ^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedNamed, 52, 1)], - ); +'''); } test_implicit_optional() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A { B({super.a}); +// ^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedNamed, 43, 1)], - ); +'''); } test_implicit_required() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A { B({required super.a}); +// ^ +// [diag.superFormalParameterWithoutAssociatedNamed] No associated named super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedNamed, 52, 1)], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/super_formal_parameter_without_associated_positional_test.dart b/pkg/analyzer/test/src/diagnostics/super_formal_parameter_without_associated_positional_test.dart index 928b01960f1..ba99cd2716a 100644 --- a/pkg/analyzer/test/src/diagnostics/super_formal_parameter_without_associated_positional_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_formal_parameter_without_associated_positional_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperFormalParameterWithoutAssociatedPositionalTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -17,146 +18,133 @@ main() { class SuperFormalParameterWithoutAssociatedPositionalTest extends PubPackageResolutionTest { test_class_primary_optionalPositional_01() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B([super.a]) extends A {} -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 27, 1)], - ); +// ^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. +'''); } test_class_primary_requiredPositional_01() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B(super.a) extends A {} -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 26, 1)], - ); +// ^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. +'''); } test_class_secondary_optionalPositional_01() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A { B([super.a]); +// ^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 43, 1)], - ); +'''); } test_class_secondary_requiredPositional_01() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A { B(super.a); +// ^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 42, 1)], - ); +'''); } test_class_secondary_requiredPositional_12() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int a); } class B extends A { B(super.a, super.b); +// ^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 64, 1)], - ); +'''); } test_enum_primary_requiredPositional_01() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E(super.a) { +// ^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. v(0); } -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 13, 1)], - ); +'''); } test_enum_secondary_requiredPositional_01() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v(0); const E(super.x); +// ^ +// [diag.superFormalParameterWithoutAssociatedPositional] No associated positional super constructor parameter. } -''', - [error(diag.superFormalParameterWithoutAssociatedPositional, 33, 1)], - ); +'''); } test_recovery_hasSuperClass_noSuperConstructor_primary() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int x); } class B(super.a) extends A { this : super.named(); +// ^^^^^^^^^^^^^ +// [diag.undefinedConstructorInInitializer] The class 'A' doesn't have a constructor named 'named'. } -''', - [error(diag.undefinedConstructorInInitializer, 63, 13)], - ); +'''); } test_recovery_hasSuperClass_noSuperConstructor_secondary() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int x); } class B extends A { B(super.x) : super.named(); +// ^^^^^^^^^^^^^ +// [diag.undefinedConstructorInInitializer] The class 'A' doesn't have a constructor named 'named'. } -''', - [error(diag.undefinedConstructorInInitializer, 60, 13)], - ); +'''); } test_recovery_noSuperClass_primary() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class B(super.a) extends A { +// ^ +// [diag.extendsNonClass] Classes can only extend other classes. this : super.named(); +// ^^^^^^^^^^^^^ +// [diag.undefinedConstructorInInitializer] The class 'Object' doesn't have a constructor named 'named'. } -''', - [ - error(diag.extendsNonClass, 25, 1), - error(diag.undefinedConstructorInInitializer, 38, 13), - ], - ); +'''); } test_recovery_noSuperClass_secondary() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class B extends A { +// ^ +// [diag.extendsNonClass] Classes can only extend other classes. B(super.x) : super.named(); +// ^^^^^^^^^^^^^ +// [diag.undefinedConstructorInInitializer] The class 'Object' doesn't have a constructor named 'named'. } -''', - [ - error(diag.extendsNonClass, 16, 1), - error(diag.undefinedConstructorInInitializer, 35, 13), - ], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/super_in_enum_constructor_test.dart b/pkg/analyzer/test/src/diagnostics/super_in_enum_constructor_test.dart index 0c6baddfee1..31943bb76e4 100644 --- a/pkg/analyzer/test/src/diagnostics/super_in_enum_constructor_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_in_enum_constructor_test.dart @@ -2,68 +2,64 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperInEnumConstructorTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SuperInEnumConstructorTest extends PubPackageResolutionTest { test_primary_one() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E() { v; this : super(); +// ^^^^^ +// [diag.superInEnumConstructor] The enum constructor can't have a 'super' initializer. } -''', - [error(diag.superInEnumConstructor, 25, 5)], - ); +'''); } test_typeName_hasRedirect() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v; const E.named(); const E() : this.named(), super(); +// ^^^^^ +// [diag.superInEnumConstructor] The enum constructor can't have a 'super' initializer. } -''', - [error(diag.superInEnumConstructor, 61, 5)], - ); +'''); } test_typeName_one() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v; const E() : super(); +// ^^^^^ +// [diag.superInEnumConstructor] The enum constructor can't have a 'super' initializer. } -''', - [error(diag.superInEnumConstructor, 28, 5)], - ); +'''); } test_typeName_two() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' enum E { v; const E() : super(), super(); +// ^^^^^ +// [diag.superInEnumConstructor] The enum constructor can't have a 'super' initializer. +// ^^^^^ +// [diag.superInEnumConstructor] The enum constructor can't have a 'super' initializer. } -''', - [ - error(diag.superInEnumConstructor, 28, 5), - error(diag.superInEnumConstructor, 37, 5), - ], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/super_in_extension_test.dart b/pkg/analyzer/test/src/diagnostics/super_in_extension_test.dart index 0d9f40a081c..c3c485a70d7 100644 --- a/pkg/analyzer/test/src/diagnostics/super_in_extension_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_in_extension_test.dart @@ -2,46 +2,46 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperInExtensionTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SuperInExtensionTest extends PubPackageResolutionTest { test_binaryOperator_inMethod() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension E on int { int plusOne() => super + 1; +// ^^^^^ +// [diag.superInExtension] The 'super' keyword can't be used in an extension because an extension doesn't have a superclass. } -''', - [error(diag.superInExtension, 40, 5)], - ); +'''); } test_binaryOperator_withGenericExtendedType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension on T { f() { +//^ +// [diag.unusedElement] The declaration 'f' isn't referenced. super + 1; +// ^^^^^ +// [diag.superInExtension] The 'super' keyword can't be used in an extension because an extension doesn't have a superclass. } } -''', - [error(diag.unusedElement, 23, 1), error(diag.superInExtension, 33, 5)], - ); +'''); } test_getter_inSetter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { int get value => 0; set value(int newValue) {} @@ -49,76 +49,70 @@ class C { extension E on C { set sign(int sign) { value = super.value * sign; +// ^^^^^ +// [diag.superInExtension] The 'super' keyword can't be used in an extension because an extension doesn't have a superclass. } } -''', - [error(diag.superInExtension, 117, 5)], - ); +'''); } test_indexOperator_inMethod() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { int operator[](int i) => 0; } extension E on C { int at(int i) => super[i]; +// ^^^^^ +// [diag.superInExtension] The 'super' keyword can't be used in an extension because an extension doesn't have a superclass. } -''', - [error(diag.superInExtension, 80, 5)], - ); +'''); } test_method_inGetter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension E on int { String get displayText => super.toString(); +// ^^^^^ +// [diag.superInExtension] The 'super' keyword can't be used in an extension because an extension doesn't have a superclass. } -''', - [error(diag.superInExtension, 49, 5)], - ); +'''); } test_methodInvocation_field_instance_late() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension E on int { late final v = super.foo(); +// ^ +// [diag.extensionDeclaresInstanceField] Extensions can't declare instance fields. +// ^^^^^ +// [diag.superInExtension] The 'super' keyword can't be used in an extension because an extension doesn't have a superclass. } -''', - [ - error(diag.extensionDeclaresInstanceField, 34, 1), - error(diag.superInExtension, 38, 5), - ], - ); +'''); } test_methodInvocation_method_instance() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension E on int { void foo() { super.foo(); +// ^^^^^ +// [diag.superInExtension] The 'super' keyword can't be used in an extension because an extension doesn't have a superclass. } } -''', - [error(diag.superInExtension, 40, 5)], - ); +'''); } test_prefixOperator_inGetter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { C operator-() => this; } extension E on C { C get negated => -super; +// ^^^^^ +// [diag.superInExtension] The 'super' keyword can't be used in an extension because an extension doesn't have a superclass. } -''', - [error(diag.superInExtension, 76, 5)], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/super_in_extension_type_test.dart b/pkg/analyzer/test/src/diagnostics/super_in_extension_type_test.dart index ea6c6e6266f..8bf40b73416 100644 --- a/pkg/analyzer/test/src/diagnostics/super_in_extension_type_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_in_extension_type_test.dart @@ -2,30 +2,30 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperInExtensionTypeTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SuperInExtensionTypeTest extends PubPackageResolutionTest { test_binaryOperator() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension type A(int it) { void f() { super + 0; +// ^^^^^ +// [diag.superInExtensionType] The 'super' keyword can't be used in an extension type because an extension type doesn't have a superclass. } } -''', - [error(diag.superInExtensionType, 44, 5)], - ); +'''); var node = findNode.singleBinaryExpression; assertResolvedNodeText(node, r''' @@ -45,16 +45,15 @@ BinaryExpression } test_methodInvocation() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension type A(int it) { void f() { super.foo(); +// ^^^^^ +// [diag.superInExtensionType] The 'super' keyword can't be used in an extension type because an extension type doesn't have a superclass. } } -''', - [error(diag.superInExtensionType, 44, 5)], - ); +'''); var node = findNode.singleMethodInvocation; assertResolvedNodeText(node, r''' @@ -76,16 +75,15 @@ MethodInvocation } test_propertyAccess() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension type A(int it) { void f() { super.foo; +// ^^^^^ +// [diag.superInExtensionType] The 'super' keyword can't be used in an extension type because an extension type doesn't have a superclass. } } -''', - [error(diag.superInExtensionType, 44, 5)], - ); +'''); var node = findNode.singlePropertyAccess; assertResolvedNodeText(node, r''' diff --git a/pkg/analyzer/test/src/diagnostics/super_in_invalid_context_test.dart b/pkg/analyzer/test/src/diagnostics/super_in_invalid_context_test.dart index d867c7e1fe4..6392fc8b570 100644 --- a/pkg/analyzer/test/src/diagnostics/super_in_invalid_context_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_in_invalid_context_test.dart @@ -2,41 +2,40 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperInInvalidContextTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SuperInInvalidContextTest extends PubPackageResolutionTest { test_binaryExpression() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' var v = super + 0; -''', - [error(diag.superInInvalidContext, 8, 5)], - ); +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. +'''); } test_class_field_instance() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } class B extends A { var f = super.foo; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 63, 5)], - ); +'''); } test_class_field_instance_late() async { @@ -52,53 +51,49 @@ class B extends A { } test_class_field_static() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } class B extends A { static var f = super.foo; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 70, 5)], - ); +'''); } test_class_field_static_late() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } class B extends A { static late var f = super.foo; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 75, 5)], - ); +'''); } test_constructorInitializer_field() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { m() {} } class B extends A { var f; B() : f = super.m(); +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 62, 5)], - ); +'''); } test_constructorInitializer_super() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class S { final int f; S(this.f); @@ -106,15 +101,14 @@ class S { class C extends S { C() : super(super.f); +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 75, 5)], - ); +'''); } test_constructorInitializer_this() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class S { final int f; S(this.f); @@ -122,82 +116,77 @@ class S { class C extends S { C() : this.other(super.f); +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. C.other(int a) : super(a); } -''', - [error(diag.superInInvalidContext, 80, 5)], - ); +'''); } test_factoryConstructor() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { m() {} } class B extends A { factory B() { super.m(); +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. return B._(); } B._(); } -''', - [error(diag.superInInvalidContext, 61, 5)], - ); +'''); } test_instanceVariableInitializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { var a; } class B extends A { var b = super.a; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 50, 5)], - ); +'''); } test_methodInvocation_extension_field_static() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension E on int { static final v = super.foo(); +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 40, 5)], - ); +'''); } test_methodInvocation_extension_method_static() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension E on int { static void foo() { super.foo(); +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } } -''', - [error(diag.superInInvalidContext, 47, 5)], - ); +'''); } test_mixin_field_instance() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } mixin M on A { var f = super.foo; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 58, 5)], - ); +'''); } test_mixin_field_instance_late() async { @@ -213,77 +202,72 @@ mixin M on A { } test_mixin_field_static() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } mixin M on A { static var f = super.foo; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 65, 5)], - ); +'''); } test_mixin_field_static_late() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } mixin M on A { static late var f = super.foo; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 70, 5)], - ); +'''); } test_propertyAccess_extension_field_static() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } extension E on int { static var f = super.foo; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 71, 5)], - ); +'''); } test_propertyAccess_extension_field_static_late() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { int get foo => 0; } extension E on int { static late var f = super.foo; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 76, 5)], - ); +'''); } test_staticMethod() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { static m() {} } class B extends A { static n() { return super.m(); } +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 70, 5)], - ); +'''); var node = findNode.methodInvocation('super.m()'); assertResolvedNodeText(node, r''' @@ -305,17 +289,16 @@ MethodInvocation } test_staticVariableInitializer() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { static int a = 0; } class B extends A { static int b = super.a; +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 69, 5)], - ); +'''); var node = findNode.singlePropertyAccess; assertResolvedNodeText(node, r''' @@ -333,44 +316,40 @@ PropertyAccess } test_topLevelFunction() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' f() { super.f(); +//^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. } -''', - [error(diag.superInInvalidContext, 8, 5)], - ); +'''); } test_topLevelVariableInitializer() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' var v = super.y; -''', - [error(diag.superInInvalidContext, 8, 5)], - ); +// ^^^^^ +// [diag.superInInvalidContext] Invalid context for 'super' invocation. +'''); } test_valid() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { m() {} } class B extends A { B() { var v = super.m(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } n() { var v = super.m(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } } -''', - [ - error(diag.unusedLocalVariable, 57, 1), - error(diag.unusedLocalVariable, 92, 1), - ], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/super_in_redirecting_constructor_test.dart b/pkg/analyzer/test/src/diagnostics/super_in_redirecting_constructor_test.dart index 9f5b243fbde..3a473e7c6ad 100644 --- a/pkg/analyzer/test/src/diagnostics/super_in_redirecting_constructor_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_in_redirecting_constructor_test.dart @@ -2,64 +2,61 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperInRedirectingConstructorTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SuperInRedirectingConstructorTest extends PubPackageResolutionTest { test_class_primary_redirectBeforeSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A() { A.named() : this(); this : this.named(), super(); +// ^^^^ +// [diag.primaryConstructorCannotRedirect] A primary constructor can't be a redirecting constructor. } -''', - [error(diag.primaryConstructorCannotRedirect, 43, 4)], - ); +'''); } test_class_primary_superBeforeRedirect() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A() { A.named() : this(); this : super(), this.named(); +// ^^^^ +// [diag.primaryConstructorCannotRedirect] A primary constructor can't be a redirecting constructor. } -''', - [error(diag.primaryConstructorCannotRedirect, 52, 4)], - ); +'''); } test_typeName_redirectionSuper() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A() : this.name(), super(); +// ^^^^^^^ +// [diag.superInRedirectingConstructor] The redirecting constructor can't have a 'super' initializer. A.name() {} } -''', - [error(diag.superInRedirectingConstructor, 31, 7)], - ); +'''); } test_typeName_superRedirection() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A() : super(), this.name(); +// ^^^^^^^ +// [diag.superInRedirectingConstructor] The redirecting constructor can't have a 'super' initializer. A.name() {} } -''', - [error(diag.superInRedirectingConstructor, 18, 7)], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/super_invocation_not_last_test.dart b/pkg/analyzer/test/src/diagnostics/super_invocation_not_last_test.dart index 263f6f2e560..8d25f1f4e73 100644 --- a/pkg/analyzer/test/src/diagnostics/super_invocation_not_last_test.dart +++ b/pkg/analyzer/test/src/diagnostics/super_invocation_not_last_test.dart @@ -2,40 +2,39 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(SuperInvocationNotLastTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class SuperInvocationNotLastTest extends PubPackageResolutionTest { test_primary_superBeforeAssert() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A(int x) { this : super(), assert(x > 0); +// ^^^^^ +// [diag.superInvocationNotLast] The superconstructor call must be last in an initializer list. } -''', - [error(diag.superInvocationNotLast, 26, 5)], - ); +'''); } test_primary_superBeforeField() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A() { int x; this : super(), x = 0; +// ^^^^^ +// [diag.superInvocationNotLast] The superconstructor call must be last in an initializer list. } -''', - [error(diag.superInvocationNotLast, 30, 5)], - ); +'''); } test_primary_superIsLast() async { @@ -48,26 +47,24 @@ class A() { } test_typeName_superBeforeAssert() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A(int? x) : super(), assert(x != null); +// ^^^^^ +// [diag.superInvocationNotLast] The superconstructor call must be last in an initializer list. } -''', - [error(diag.superInvocationNotLast, 24, 5)], - ); +'''); } test_typeName_superBeforeField() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { final int x; A() : super(), x = 1; +// ^^^^^ +// [diag.superInvocationNotLast] The superconstructor call must be last in an initializer list. } -''', - [error(diag.superInvocationNotLast, 33, 5)], - ); +'''); } test_typeName_superIsLast() async { diff --git a/pkg/analyzer/test/src/diagnostics/todo_test.dart b/pkg/analyzer/test/src/diagnostics/todo_test.dart index 22d2451f317..468aac75c77 100644 --- a/pkg/analyzer/test/src/diagnostics/todo_test.dart +++ b/pkg/analyzer/test/src/diagnostics/todo_test.dart @@ -6,77 +6,70 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(TodoTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class TodoTest extends PubPackageResolutionTest { test_eof() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() {} // TODO: Implement something else -''', - [error(diag.todo, 13, 30, text: 'TODO: Implement something else')], - ); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.todo] TODO: Implement something else +'''); } test_fixme() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { // FIXME: Implement +// ^^^^^^^^^^^^^^^^ +// [diag.fixme] FIXME: Implement } -''', - [error(diag.fixme, 14, 16, text: 'FIXME: Implement')], - ); +'''); } test_hack() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { // HACK: This is a hack +// ^^^^^^^^^^^^^^^^^^^^ +// [diag.hack] HACK: This is a hack } -''', - [error(diag.hack, 14, 20, text: 'HACK: This is a hack')], - ); +'''); } test_todo_multiLineComment() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { /* TODO: Implement */ +// ^^^^^^^^^^^^^^^ +// [diag.todo] TODO: Implement /* TODO: Implement*/ +// ^^^^^^^^^^^^^^^ +// [diag.todo] TODO: Implement } -''', - [ - error(diag.todo, 14, 15, text: 'TODO: Implement'), - error(diag.todo, 38, 15, text: 'TODO: Implement'), - ], - ); +'''); } test_todo_multiLineComment2() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { /* TODO: Implement1 +// [diag.todo][column 1][length 16] TODO: Implement1 TODO: Implement2 +// [diag.todo][column 1][length 16] TODO: Implement2 */ } -''', - [ - error(diag.todo, 12, 16, text: 'TODO: Implement1'), - error(diag.todo, 29, 16, text: 'TODO: Implement2'), - ], - ); +'''); } test_todo_multiLineCommentWrapped() async { @@ -188,14 +181,13 @@ main() { } test_todo_singleLineComment() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { // TODO: Implement +// ^^^^^^^^^^^^^^^ +// [diag.todo] TODO: Implement } -''', - [error(diag.todo, 14, 15, text: 'TODO: Implement')], - ); +'''); } test_todo_singleLineCommentDoubleCommented() async { @@ -230,14 +222,13 @@ main() { } test_todo_singleLineCommentFollowedByDartdoc() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' // TODO: Implement something +// ^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.todo] TODO: Implement something /// This is the function documentation void f() {} -''', - [error(diag.todo, 3, 25, text: 'TODO: Implement something')], - ); +'''); } test_todo_singleLineCommentLessIndentedContinuation() async { @@ -349,13 +340,12 @@ main() { } test_undone() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { // UNDONE: This was undone +// ^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.undone] UNDONE: This was undone } -''', - [error(diag.undone, 14, 23, text: 'UNDONE: This was undone')], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/type_argument_not_matching_bounds_test.dart b/pkg/analyzer/test/src/diagnostics/type_argument_not_matching_bounds_test.dart index 4b96c1199a0..1776faad129 100644 --- a/pkg/analyzer/test/src/diagnostics/type_argument_not_matching_bounds_test.dart +++ b/pkg/analyzer/test/src/diagnostics/type_argument_not_matching_bounds_test.dart @@ -2,44 +2,43 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(TypeArgumentNotMatchingBoundsTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class TypeArgumentNotMatchingBoundsTest extends PubPackageResolutionTest { test_classTypeAlias() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} mixin C {} class G {} class D = G with C; -''', - [error(diag.typeArgumentNotMatchingBounds, 69, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_const() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G { const G(); } f() { return const G(); } -''', - [error(diag.typeArgumentNotMatchingBounds, 81, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_const_matching() async { @@ -54,18 +53,15 @@ f() { return const G(); } } test_enum_inferred() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' enum E { v(''); +// ^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. +// [diag.constConstructorParamTypeMismatch] A value of type 'String' can't be assigned to a parameter of type 'int' in a const constructor. const E(T t); } -''', - [ - error(diag.argumentTypeNotAssignable, 28, 2), - error(diag.constConstructorParamTypeMismatch, 28, 2), - ], - ); +'''); } test_enum_superBounded() async { @@ -77,156 +73,134 @@ enum E> { } test_enum_withTypeArguments() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' enum E { v() +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'int' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 28, 6)], - ); +'''); } test_extends() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} class C extends G{} -''', - [error(diag.typeArgumentNotMatchingBounds, 64, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_extends_regressionInIssue18468Fix() async { // https://code.google.com/p/dart/issues/detail?id=18628 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class X {} class Y extends X {} -''', - [error(diag.typeArgumentNotMatchingBounds, 48, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'U' doesn't conform to the bound 'Type' of the type parameter 'T'. +'''); } test_extensionOverride_hasTypeArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' extension E on int { void foo() {} } void f() { E(0).foo(); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 70, 6)], - ); +'''); } test_extensionOverride_hasTypeArguments_call() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' extension E on int { void call() {} } void f() { E(0)(); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 71, 6)], - ); +'''); } test_extensionType_superBounded() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension type A>(int it) {} void f(A a) {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 52, - 1, - contextMessages: [message(testFile, 52, 1)], - ), - ], - ); +// ^ +// [context 1] The raw type was instantiated as 'A>', and is not regular-bounded. +// [diag.typeArgumentNotMatchingBounds][context 1] 'A' doesn't conform to the bound 'A>' of the type parameter 'T'. +'''); } test_extensionType_withTypeArguments() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' extension type A(int it) {} void f(A a) {} -''', - [error(diag.typeArgumentNotMatchingBounds, 53, 6)], - ); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. +'''); } test_fieldFormalParameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} class C { var f; C(G this.f) {} +// ^^^^ +// [context 1] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +// ^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. } -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 71, - 1, - contextMessages: [message(testFile, 69, 4)], - ), - ], - ); +'''); } test_functionExpressionInvocation() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void f() { (() {})(); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 36, 6)], - ); +'''); } test_functionExpressionInvocation_implicitCall() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class C { void call() {} } void f(C c) { c(); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 63, 6)], - ); +'''); } test_functionReference() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void foo(T a) {} void bar() { foo; +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 51, 6)], - ); +'''); } test_functionReference_matching() async { @@ -248,79 +222,54 @@ void bar() { } test_functionReturnType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} G f() => throw 0; -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 48, - 1, - contextMessages: [message(testFile, 46, 4)], - ), - ], - ); +// [context 1][column 1][length 4] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +//^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_functionTypeAlias() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} typedef G f(); -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 56, - 1, - contextMessages: [message(testFile, 54, 4)], - ), - ], - ); +// ^^^^ +// [context 1] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +// ^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_functionTypedFormalParameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} f(G h()) {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 50, - 1, - contextMessages: [message(testFile, 48, 4)], - ), - ], - ); +//^^^^ +// [context 1] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +// ^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_genericFunctionTypeArgument_invariant() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' typedef F = T Function(T); typedef FB = S Function(S); class CB {} void f(CB> a) {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 119, - 5, - contextMessages: [message(testFile, 116, 9)], - ), - ], - ); +// ^^^^^^^^^ +// [context 1] The inverted type 'CB(T)>(S)>' is also not regular-bounded, so the type is not well-bounded. +// ^^^^^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'FB' doesn't conform to the bound 'F' of the type parameter 'T'. +'''); } test_genericFunctionTypeArgument_regularBounded() async { @@ -333,34 +282,27 @@ void f(CB a) {} } test_implements() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} class C implements G{} -''', - [error(diag.typeArgumentNotMatchingBounds, 67, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_is() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} var b = 1 is G; -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 61, - 1, - contextMessages: [message(testFile, 59, 4)], - ), - ], - ); +// ^^^^ +// [context 1] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +// ^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_metadata_matching() async { @@ -375,22 +317,20 @@ void f() {} } test_metadata_notMatching() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); } @A() +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. void f() {} -''', - [error(diag.typeArgumentNotMatchingBounds, 44, 6)], - ); +'''); } test_metadata_notMatching_viaTypeAlias() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { const A(); } @@ -398,10 +338,10 @@ class A { typedef B = A; @B() +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. void f() {} -''', - [error(diag.typeArgumentNotMatchingBounds, 66, 6)], - ); +'''); } test_methodInvocation_genericFunctionTypeArgument_match() async { @@ -415,23 +355,21 @@ void g() { } test_methodInvocation_genericFunctionTypeArgument_mismatch() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} typedef F = void Function(); void f()>() {} void g() { f(); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'F' doesn't conform to the bound 'void Function()' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 131, 1)], - ); +'''); } test_methodInvocation_localFunction() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class Point { Point(T x, T y); } @@ -441,15 +379,14 @@ main() { return new Point(x, y); } print(f('hello', 'world')); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 145, 6)], - ); +'''); } test_methodInvocation_method() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class Point { Point(T x, T y); } @@ -462,15 +399,14 @@ class PointFactory { f(PointFactory factory) { print(factory.point('hello', 'world')); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 202, 6)], - ); +'''); } test_methodInvocation_topLevelFunction() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class Point { Point(T x, T y); } @@ -481,43 +417,36 @@ Point f(T x, T y) { main() { print(f('hello', 'world')); +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds] 'String' doesn't conform to the bound 'num' of the type parameter 'T'. } -''', - [error(diag.typeArgumentNotMatchingBounds, 140, 6)], - ); +'''); } test_methodReturnType() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} class C { G m() => throw 0; +//^^^^ +// [context 1] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +// ^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. } -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 60, - 1, - contextMessages: [message(testFile, 58, 4)], - ), - ], - ); +'''); } test_new() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} f() { return new G(); } -''', - [error(diag.typeArgumentNotMatchingBounds, 65, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_new_matching() async { @@ -530,28 +459,26 @@ f() { return new G(); } } test_new_superTypeOfUpperBound() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A {} class C extends B {} class G {} f() { return new G(); } -''', - [error(diag.typeArgumentNotMatchingBounds, 96, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'A' doesn't conform to the bound 'B' of the type parameter 'E'. +'''); } test_nonFunctionTypeAlias_body_typeArgument_mismatch() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} typedef X = G; -''', - [error(diag.typeArgumentNotMatchingBounds, 60, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'T'. +'''); } test_nonFunctionTypeAlias_body_typeArgument_regularBounded() async { @@ -571,15 +498,14 @@ typedef X = List; } test_nonFunctionTypeAlias_interfaceType_body_mismatch() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} typedef X = G; -''', - [error(diag.typeArgumentNotMatchingBounds, 60, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'T'. +'''); } test_nonFunctionTypeAlias_interfaceType_body_regularBounded() async { @@ -590,38 +516,25 @@ typedef X = A; } test_nonFunctionTypeAlias_interfaceType_body_superBounded() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A> {} typedef X = A; -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 42, - 1, - contextMessages: [message(testFile, 42, 1)], - ), - ], - ); +// ^ +// [context 1] The raw type was instantiated as 'A>', and is not regular-bounded. +// [diag.typeArgumentNotMatchingBounds][context 1] 'A' doesn't conform to the bound 'A>' of the type parameter 'T'. +'''); } test_nonFunctionTypeAlias_interfaceType_parameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} typedef X = Map; void f(X a) {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 58, - 6, - contextMessages: [message(testFile, 56, 9)], - ), - ], - ); +// ^^^^^^^^^ +// [context 1] The inverted type 'X' is also not regular-bounded, so the type is not well-bounded. +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'String' doesn't conform to the bound 'A' of the type parameter 'T'. +'''); } test_nonFunctionTypeAlias_interfaceType_parameter_regularBounded() async { @@ -634,81 +547,59 @@ void f(X a) {} } Future test_nonFunctionTypeAlias_parameter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A {} class B extends A {} class D {} typedef Alias = D; main() { D d = Alias(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'd' isn't used. +// ^ +// [diag.typeArgumentNotMatchingBounds] 'A' doesn't conform to the bound 'B' of the type parameter 'T'. } -''', - [ - error(diag.unusedLocalVariable, 94, 1), - error(diag.typeArgumentNotMatchingBounds, 104, 1), - ], - ); +'''); } test_not_matching_bounds() async { // There should be an error, because Bar's type argument T is Foo, which // doesn't extends Foo. - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class Foo {} class Bar> {} class Baz extends Bar {} +// ^^^ +// [context 1] The raw type was instantiated as 'Bar>', and is not regular-bounded. +// [diag.typeArgumentNotMatchingBounds][context 1] 'Foo' doesn't conform to the bound 'Foo>' of the type parameter 'T'. void main() {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 65, - 3, - contextMessages: [message(testFile, 65, 3)], - ), - ], - ); +'''); // Instantiate-to-bounds should have instantiated "Bar" to "Bar". assertType(findElement2.class_('Baz').supertype, 'Bar>'); } test_notRegularBounded_notSuperBounded_parameter_invariant() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' typedef A = X Function(X); typedef G> = void Function(); foo(G g) {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 92, - 1, - contextMessages: [message(testFile, 92, 1), message(testFile, 92, 1)], - ), - ], - ); +// ^ +// [context 1] The raw type was instantiated as 'G', and is not regular-bounded. +// [context 2] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +// [diag.typeArgumentNotMatchingBounds][context 1][context 2] 'A' doesn't conform to the bound 'A>' of the type parameter 'X'. +'''); } test_ofFunctionTypeAlias() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} typedef F(); F fff = (throw 42); -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 50, - 1, - contextMessages: [message(testFile, 48, 4)], - ), - ], - ); +// [context 1][column 1][length 4] The inverted type 'F' is also not regular-bounded, so the type is not well-bounded. +//^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'T'. +'''); } test_ofFunctionTypeAlias_hasBound2_matching() async { @@ -740,39 +631,31 @@ F f2 = (throw 0); } test_parameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} f(G g) {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 50, - 1, - contextMessages: [message(testFile, 48, 4)], - ), - ], - ); +//^^^^ +// [context 1] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +// ^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_redirectingConstructor() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class X { X(int x, int y) {} factory X.name(int x, int y) = X; +// ^^^^ +// [diag.redirectToInvalidReturnType] The return type 'X' of the redirected constructor isn't a subtype of 'X'. +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'T'. } -''', - [ - error(diag.redirectToInvalidReturnType, 99, 4), - error(diag.typeArgumentNotMatchingBounds, 101, 1), - ], - ); +'''); } test_regression_42196() async { @@ -823,125 +706,87 @@ A get foo => throw 0; } test_typeArgumentList() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class C {} class D {} C> c = (throw 0); -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 64, - 1, - contextMessages: [message(testFile, 62, 4)], - ), - ], - ); +//^^^^ +// [context 1] The inverted type 'D' is also not regular-bounded, so the type is not well-bounded. +// ^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_typeLiteral_class() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C {} var t = C; -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 36, - 6, - contextMessages: [message(testFile, 34, 9)], - ), - ], - ); +// ^^^^^^^^^ +// [context 1] The inverted type 'C' is also not regular-bounded, so the type is not well-bounded. +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'String' doesn't conform to the bound 'int' of the type parameter 'T'. +'''); } test_typeLiteral_functionTypeAlias() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' typedef Cb = void Function(); var t = Cb; -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 56, - 6, - contextMessages: [message(testFile, 53, 10)], - ), - ], - ); +// ^^^^^^^^^^ +// [context 1] The inverted type 'Cb' is also not regular-bounded, so the type is not well-bounded. +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'String' doesn't conform to the bound 'int' of the type parameter 'T'. +'''); } test_typeLiteral_typeAlias() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C {} typedef D = C; var t = D; -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 51, - 6, - contextMessages: [message(testFile, 49, 9)], - ), - ], - ); +// ^^^^^^^^^ +// [context 1] The inverted type 'D' is also not regular-bounded, so the type is not well-bounded. +// ^^^^^^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'String' doesn't conform to the bound 'int' of the type parameter 'T'. +'''); } test_typeParameter() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class C {} class G {} class D> {} -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 77, - 1, - contextMessages: [message(testFile, 75, 4)], - ), - ], - ); +// ^^^^ +// [context 1] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +// ^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_variableDeclaration() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} class G {} G g = (throw 0); -''', - [ - error( - diag.typeArgumentNotMatchingBounds, - 48, - 1, - contextMessages: [message(testFile, 46, 4)], - ), - ], - ); +// [context 1][column 1][length 4] The inverted type 'G' is also not regular-bounded, so the type is not well-bounded. +//^ +// [diag.typeArgumentNotMatchingBounds][context 1] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } test_with() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B {} mixin G {} class C extends Object with G{} -''', - [error(diag.typeArgumentNotMatchingBounds, 76, 1)], - ); +// ^ +// [diag.typeArgumentNotMatchingBounds] 'B' doesn't conform to the bound 'A' of the type parameter 'E'. +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/unawaited_return_in_try_block_test.dart b/pkg/analyzer/test/src/diagnostics/unawaited_return_in_try_block_test.dart index cf0236c0083..6c26e5bbac8 100644 --- a/pkg/analyzer/test/src/diagnostics/unawaited_return_in_try_block_test.dart +++ b/pkg/analyzer/test/src/diagnostics/unawaited_return_in_try_block_test.dart @@ -2,14 +2,15 @@ // 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/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; +import '../dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(UnawaitedReturnInTryBlockTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -33,17 +34,16 @@ Future foo(dynamic v) async { } Future test_dynamic_returnType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' foo() async { try { return Future.value(42); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } catch (_) {} return Future.value(42); } -''', - [error(diag.unawaitedReturnInTryBlock, 26, 6)], - ); +'''); } Future test_finallyBlock() async { @@ -58,43 +58,42 @@ Future foo() async { } Future test_futureOr() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'dart:async'; Future foo(FutureOr v) async { try { return v; +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } catch (_) {} return v; } -''', - [error(diag.unawaitedReturnInTryBlock, 75, 6)], - ); +'''); } Future test_futureOr_returnType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'dart:async'; FutureOr foo() async { try { return Future.value(42); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } catch (_) {} return Future.value(42); } -''', - [error(diag.unawaitedReturnInTryBlock, 62, 6)], - ); +'''); } Future test_futureSubtype() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Future foo() async { try { return MyFuture(); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } catch (_) {} return MyFuture(); } @@ -103,24 +102,21 @@ class MyFuture implements Future { @override noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); } -''', - [error(diag.unawaitedReturnInTryBlock, 38, 6)], - ); +'''); } Future test_insideBlock() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Future foo() async { try { { return Future.value(null); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } } catch (_) {} } -''', - [error(diag.unawaitedReturnInTryBlock, 47, 6)], - ); +'''); } Future test_insideClosure() async { @@ -136,97 +132,90 @@ void foo() { } Future test_insideClosure_functionExpression() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void foo() { try { var x = () async => return Future.value(null); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. +// ^^^^^^ +// [diag.unexpectedToken] Unexpected text 'return'. } catch (_) {} } -''', - [ - error(diag.unusedLocalVariable, 29, 1), - error(diag.unexpectedToken, 45, 6), - ], - ); +'''); } Future test_invalidType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' foo() async { return unknown; +// ^^^^^^^ +// [diag.undefinedIdentifier] Undefined name 'unknown'. } -''', - [error(diag.undefinedIdentifier, 23, 7)], - ); +'''); } Future test_localFunction() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void f() { Future foo() async { try { return Future.value(0); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } catch (_) {} return 0; } foo(); } -''', - [error(diag.unawaitedReturnInTryBlock, 55, 6)], - ); +'''); } Future test_localFunctionExpression_blockBody() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' void f() { var foo = () async { try { return Future.value(0); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } catch (_) {} return 0; }; foo(); } -''', - [error(diag.unawaitedReturnInTryBlock, 50, 6)], - ); +'''); } Future test_method() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { Future foo() async { try { return Future.value(0); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } catch (_) {} return 0; } } -''', - [error(diag.unawaitedReturnInTryBlock, 54, 6)], - ); +'''); } Future test_nestedTry() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Future foo() async { try { try { } catch (_) { return Future.value(42); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } } catch (_) {} return -1; } -''', - [error(diag.unawaitedReturnInTryBlock, 68, 6)], - ); +'''); } Future test_nonFuture() async { @@ -252,33 +241,31 @@ Future foo() { } Future test_stream() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'dart:async'; Stream foo() async* { try { yield 42; return Future.value(42); +// ^^^^^^ +// [diag.returnInGenerator] Can't return a value from a generator function that uses the 'async*' or 'sync*' modifier. } catch (_) {} } -''', - [error(diag.returnInGenerator, 75, 6)], - ); +'''); } Future test_typeParameter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Future foo>(T v) async { try { return v; +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } catch (_) {} return v; } -''', - [error(diag.unawaitedReturnInTryBlock, 64, 6)], - ); +'''); } Future test_withinTryCatch() async { @@ -293,40 +280,37 @@ Future foo() async { } Future test_withinTryCatch_withinTryBlock() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Future foo() async { try { try {} catch (_) { return Future.value(42); +// ^^^^^^ +// [diag.unawaitedReturnInTryBlock] Returning a 'Future' without 'await' inside a try block. } } catch (_) {} return -1; } -''', - [error(diag.unawaitedReturnInTryBlock, 63, 6)], - ); +'''); } Future test_wrongType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Future foo() async { return ''; +// ^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'String' can't be returned from the function 'foo' because it has a return type of 'Future'. } -''', - [error(diag.returnOfInvalidTypeFromFunction, 35, 2)], - ); +'''); } Future test_wrongType2() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' Future? foo() async { return Future.value(null); +// ^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Future' can't be returned from the function 'foo' because it has a return type of 'Future?'. } -''', - [error(diag.returnOfInvalidTypeFromFunction, 36, 24)], - ); +'''); } } diff --git a/pkg/analyzer/test/src/diagnostics/use_of_nullable_value_test.dart b/pkg/analyzer/test/src/diagnostics/use_of_nullable_value_test.dart index 695a34094c0..c9c91b2e9d3 100644 --- a/pkg/analyzer/test/src/diagnostics/use_of_nullable_value_test.dart +++ b/pkg/analyzer/test/src/diagnostics/use_of_nullable_value_test.dart @@ -3,7 +3,6 @@ // BSD-style license that can be found in the LICENSE file. import 'package:analyzer/dart/ast/ast.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/context_collection_resolution.dart'; @@ -1058,17 +1057,16 @@ m() { } test_member_nullable() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' m() { int? x; x.isEven; +// ^^^^^^ +// [diag.uncheckedPropertyAccessOfNullableValue] The property 'isEven' can't be unconditionally accessed because the receiver can be 'null'. } -''', - [error(diag.uncheckedPropertyAccessOfNullableValue, 20, 6)], - ); +'''); - var node = findNode.simple('isEven'); + var node = findNode.simple('isEven;'); assertResolvedNodeText(node, r''' SimpleIdentifier token: isEven diff --git a/pkg/analyzer/test/src/ignore_comments/diagnostic_suppression_test.dart b/pkg/analyzer/test/src/ignore_comments/diagnostic_suppression_test.dart index de3b8a2afd5..c00f8c09553 100644 --- a/pkg/analyzer/test/src/ignore_comments/diagnostic_suppression_test.dart +++ b/pkg/analyzer/test/src/ignore_comments/diagnostic_suppression_test.dart @@ -4,14 +4,15 @@ import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:analyzer_testing/utilities/utilities.dart'; -import 'package:linter/src/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../../src/dart/resolution/context_collection_resolution.dart'; +import '../../src/dart/resolution/node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(ErrorSuppressionTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -31,48 +32,46 @@ class ErrorSuppressionTest extends PubPackageResolutionTest { } test_codeMismatch() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // ignore: $ignoredCode int x = ''; +// ^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. int _y = 0; //INVALID_ASSIGNMENT -''', - [error(diag.invalidAssignment, 34, 2), error(diag.unusedElement, 42, 2)], - ); +// ^^ +// [diag.unusedElement] The declaration '_y' isn't referenced. +'''); } test_ignoreFirstOfMultiple() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // ignore: unnecessary_cast int x = (0 as int); // ... but no ignore here ... var y = x + ''; //ARGUMENT_TYPE_NOT_ASSIGNABLE -''', - [error(diag.argumentTypeNotAssignable, 90, 2)], - ); +// ^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. +'''); } test_ignoreFirstOfMultipleWithTrailingComment() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' int x = (0 as int); // ignore: unnecessary_cast // ... but no ignore here ... var y = x + ''; //ARGUMENT_TYPE_NOT_ASSIGNABLE -''', - [error(diag.argumentTypeNotAssignable, 90, 2)], - ); +// ^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. +'''); } test_ignoreForFile() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' int x = (0 as int); //UNNECESSARY_CAST var y = x + ''; //ARGUMENT_TYPE_NOT_ASSIGNABLE +// ^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. // ignore_for_file: unnecessary_cast -''', - [error(diag.argumentTypeNotAssignable, 51, 2)], - ); +'''); } test_ignoreForFileWithMuchWhitespace() async { @@ -107,14 +106,13 @@ void f() { } test_ignoreForFileWithTypeMismatchLintVsWarning() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // ignore_for_file: type=lint int a = 0; int _x = 1; -''', - [error(diag.unusedElement, 45, 2)], - ); +// ^^ +// [diag.unusedElement] The declaration '_x' isn't referenced. +'''); } test_ignoreOnlyDiagnosticWithTrailingComment() async { @@ -124,26 +122,24 @@ int x = (0 as int); // ignore: unnecessary_cast } test_ignoreSecondDiagnostic() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' //UNNECESSARY_CAST int x = (0 as int); +// ^^^^^^^^ +// [diag.unnecessaryCast] Unnecessary cast. // ignore: unused_element String _foo = ''; //UNUSED_ELEMENT -''', - [error(diag.unnecessaryCast, 28, 8)], - ); +'''); } test_ignoreSecondDiagnosticWithTrailingComment() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' //UNNECESSARY_CAST int x = (0 as int); +// ^^^^^^^^ +// [diag.unnecessaryCast] Unnecessary cast. String _foo = ''; // ignore: $ignoredCode -''', - [error(diag.unnecessaryCast, 28, 8)], - ); +'''); } test_ignoreTypeMatches() async { @@ -154,23 +150,21 @@ void f(arg1(int)) {} // AVOID_TYPES_AS_PARAMETER_NAMES } test_ignoreTypeMismatchLintVsWarning() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // ignore: type=lint int _x = 1; -''', - [error(diag.unusedElement, 25, 2)], - ); +// ^^ +// [diag.unusedElement] The declaration '_x' isn't referenced. +'''); } test_ignoreTypeWithBadType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // ignore: type=wrong void f(arg1(int)) {} // AVOID_TYPES_AS_PARAMETER_NAMES -''', - [error(diag.avoidTypesAsParameterNamesFormalParameter, 34, 3)], - ); +// ^^^ +// [diag.avoidTypesAsParameterNamesFormalParameter] The parameter name 'int' matches a visible type name. +'''); } test_ignoreUniqueName() async { @@ -193,41 +187,36 @@ int x = (0 as int); // ignore: UNNECESSARY_CAST } test_invalidCode() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // ignore: right_format_wrong_code int x = ''; +// ^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. var y = x + ''; //ARGUMENT_TYPE_NOT_ASSIGNABLE -''', - [ - error(diag.invalidAssignment, 43, 2), - error(diag.argumentTypeNotAssignable, 59, 2), - ], - ); +// ^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. +'''); } test_missingCodes() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' int x = 3; // ignore: String y = x + ''; //INVALID_ASSIGNMENT, ARGUMENT_TYPE_NOT_ASSIGNABLE -''', - [ - error(diag.invalidAssignment, 33, 6), - error(diag.argumentTypeNotAssignable, 37, 2), - ], - ); +// ^^^^^^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'String'. +// ^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. +'''); } test_missingMetadataSuffix() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' // ignore invalid_assignment String y = 3; //INVALID_ASSIGNMENT -''', - [error(diag.invalidAssignment, 40, 1)], - ); +// ^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. +'''); } test_multipleCodesInIgnore() async { @@ -254,37 +243,33 @@ int _y = x as int; // ignore: unnecessary_cast, $ignoredCode } test_multipleCommentsPreceding() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' int x = (0 as int); //This is the first comment... +// ^^^^^^^^ +// [diag.unnecessaryCast] Unnecessary cast. // ignore: $ignoredCode String _foo = ''; //UNUSED_ELEMENT -''', - [error(diag.unnecessaryCast, 9, 8)], - ); +'''); } test_noIgnores() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' int x = ''; //INVALID_ASSIGNMENT +// ^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. var y = x + ''; //ARGUMENT_TYPE_NOT_ASSIGNABLE -''', - [ - error(diag.invalidAssignment, 8, 2), - error(diag.argumentTypeNotAssignable, 45, 2), - ], - ); +// ^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. +'''); } test_trailingCommentDoesNotCountAsAbove() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' int x = (0 as int); // ignore: unnecessary_cast int y = (0 as int); -''', - [error(diag.unnecessaryCast, 57, 8)], - ); +// ^^^^^^^^ +// [diag.unnecessaryCast] Unnecessary cast. +'''); } test_undefinedFunctionWithinFlutterCanBeIgnored() async { diff --git a/pkg/analyzer/test/src/summary/top_level_inference_test.dart b/pkg/analyzer/test/src/summary/top_level_inference_test.dart index ffbb5f7d4a1..1302037913d 100644 --- a/pkg/analyzer/test/src/summary/top_level_inference_test.dart +++ b/pkg/analyzer/test/src/summary/top_level_inference_test.dart @@ -4,7 +4,6 @@ import 'package:analyzer/dart/analysis/results.dart'; import 'package:analyzer/src/dart/element/element.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../dart/resolution/node_text_expectations.dart'; @@ -93,13 +92,14 @@ var t = b } test_initializer_dependencyCycle() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' var a = b; +// ^ +// [diag.topLevelCycle] The type of 'a' can't be inferred because it depends on itself through the cycle: a, b. var b = a; -''', - [error(diag.topLevelCycle, 4, 1), error(diag.topLevelCycle, 15, 1)], - ); +// ^ +// [diag.topLevelCycle] The type of 'b' can't be inferred because it depends on itself through the cycle: a, b. +'''); } test_initializer_equality() async { @@ -297,38 +297,28 @@ var t = { } test_override_conflictFieldType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' abstract class A { int aaa = 0; +// ^^^ +// [context 1] The member being overridden. } abstract class B { String aaa = '0'; +// ^^^ +// [context 2] The member being overridden. } class C implements A, B { var aaa; +// ^^^ +// [diag.invalidOverride][context 1] 'C.aaa' ('dynamic Function()') isn't a valid override of 'A.aaa' ('int Function()'). +// [diag.invalidOverride][context 2] 'C.aaa' ('dynamic Function()') isn't a valid override of 'B.aaa' ('String Function()'). } -''', - [ - error( - diag.invalidOverride, - 109, - 3, - contextMessages: [message(testFile, 64, 3)], - ), - error( - diag.invalidOverride, - 109, - 3, - contextMessages: [message(testFile, 25, 3)], - ), - ], - ); +'''); } test_override_conflictParameterType_method() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' abstract class A { void mmm(int a); } @@ -337,10 +327,10 @@ abstract class B { } class C implements A, B { void mmm(a) {} +// ^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'C' from overridden methods: A.mmm (void Function(int)), B.mmm (void Function(String)). } -''', - [error(diag.noCombinedSuperSignature, 116, 3)], - ); +'''); } Future _assertErrorOnlyLeft(List operators) async { diff --git a/pkg/analyzer/test/src/task/strong/dart2_inference_test.dart b/pkg/analyzer/test/src/task/strong/dart2_inference_test.dart index eb3da259c64..778159609ed 100644 --- a/pkg/analyzer/test/src/task/strong/dart2_inference_test.dart +++ b/pkg/analyzer/test/src/task/strong/dart2_inference_test.dart @@ -5,7 +5,6 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/src/dart/element/type.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:analyzer/src/test_utilities/function_ast_visitor.dart'; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -167,8 +166,7 @@ void main() { } test_compoundAssignment_simpleIdentifier_topLevel() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A { @@ -181,10 +179,10 @@ void set topLevel(A value) {} main() { var /*@type=B*/ v = topLevel += 1; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 152, 1)], - ); +'''); _assertTypeAnnotations(); } diff --git a/pkg/analyzer/test/src/task/strong/inferred_type_test.dart b/pkg/analyzer/test/src/task/strong/inferred_type_test.dart index eb9469c5d0e..0bcfac51aa9 100644 --- a/pkg/analyzer/test/src/task/strong/inferred_type_test.dart +++ b/pkg/analyzer/test/src/task/strong/inferred_type_test.dart @@ -12,10 +12,12 @@ import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../../dart/resolution/context_collection_resolution.dart'; +import '../../dart/resolution/node_text_expectations.dart'; void main() { defineReflectiveSuite(() { defineReflectiveTests(InferredTypeTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -30,14 +32,13 @@ class InferredTypeTest extends PubPackageResolutionTest { } test_asyncClosureReturnType_flatten() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' Future futureInt = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'Future'. var f = () => futureInt; var g = () async => futureInt; -''', - [error(diag.invalidAssignment, 24, 4)], - ); +'''); var futureInt = _resultLibraryElement.topLevelVariables[0]; expect(futureInt.name, 'futureInt'); _assertTypeStr(futureInt.type, 'Future'); @@ -59,15 +60,14 @@ var f = () async => 0; } test_asyncClosureReturnType_futureOr() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:async'; FutureOr futureOrInt = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'FutureOr'. var f = () => futureOrInt; var g = () async => futureOrInt; -''', - [error(diag.invalidAssignment, 49, 4)], - ); +'''); var futureOrInt = _resultLibraryElement.topLevelVariables[0]; expect(futureOrInt.name, 'futureOrInt'); _assertTypeStr(futureOrInt.type, 'FutureOr'); @@ -80,8 +80,7 @@ var g = () async => futureOrInt; } test_blockBodiedLambdas_async_allReturnsAreFutures() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math' show Random; main() { var f = () async { @@ -92,23 +91,22 @@ main() { } }; Future g = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'g' isn't used. Future h = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'h' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'Future' can't be assigned to a variable of type 'Future'. } -''', - [ - error(diag.unusedLocalVariable, 218, 1), - error(diag.unusedLocalVariable, 241, 1), - error(diag.invalidAssignment, 245, 3), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Future Function()'); } test_blockBodiedLambdas_async_allReturnsAreValues() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math' show Random; main() { var f = () async { @@ -119,23 +117,22 @@ main() { } }; Future g = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'g' isn't used. Future h = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'h' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'Future' can't be assigned to a variable of type 'Future'. } -''', - [ - error(diag.unusedLocalVariable, 169, 1), - error(diag.unusedLocalVariable, 192, 1), - error(diag.invalidAssignment, 196, 3), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Future Function()'); } test_blockBodiedLambdas_async_mixOfValuesAndFutures() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math' show Random; main() { var f = () async { @@ -146,197 +143,197 @@ main() { } }; Future g = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'g' isn't used. Future h = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'h' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'Future' can't be assigned to a variable of type 'Future'. } -''', - [ - error(diag.unusedLocalVariable, 192, 1), - error(diag.unusedLocalVariable, 215, 1), - error(diag.invalidAssignment, 219, 3), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Future Function()'); } test_blockBodiedLambdas_asyncStar() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { var f = () async* { yield 1; Stream s; yield* s; +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 's' must be assigned before it can be used. }; Stream g = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'g' isn't used. Stream h = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'h' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'Stream' can't be assigned to a variable of type 'Stream'. } -''', - [ - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 77, 1), - error(diag.unusedLocalVariable, 99, 1), - error(diag.unusedLocalVariable, 122, 1), - error(diag.invalidAssignment, 126, 3), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Stream Function()'); } test_blockBodiedLambdas_basic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' test1() { List o; var y = o.map((x) { return x + 1; }); +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'o' must be assigned before it can be used. Iterable z = y; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 35, 1), - error(diag.unusedLocalVariable, 81, 1), - ], - ); +'''); } test_blockBodiedLambdas_downwardsIncompatibleWithUpwardsInference() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { String f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'String'. var g = f; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'g' isn't used. g = () { return 1; }; +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 25, 4), - error(diag.unusedLocalVariable, 37, 1), - error(diag.returnOfInvalidTypeFromClosure, 62, 1), - ], - ); +'''); var g = findElement2.localVar('g'); _assertTypeStr(g.type, 'String Function()'); } test_blockBodiedLambdas_downwardsIncompatibleWithUpwardsInference_topLevel() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' String f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'String'. var g = f; -''', - [error(diag.returnOfInvalidTypeFromFunction, 14, 4)], - ); +'''); var g = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(g.type, 'String Function()'); } test_blockBodiedLambdas_inferBottom_async() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() async { var f = () async { return null; }; Future y = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. Future z = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'Future' can't be assigned to a variable of type 'Future'. String s = await f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'String'. } -''', - [ - error(diag.unusedLocalVariable, 61, 1), - error(diag.unusedLocalVariable, 87, 1), - error(diag.invalidAssignment, 91, 3), - error(diag.unusedLocalVariable, 105, 1), - error(diag.invalidAssignment, 109, 9), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Future Function()'); } test_blockBodiedLambdas_inferBottom_asyncStar() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() async { var f = () async* { yield null; }; Stream y = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. Stream z = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'Stream' can't be assigned to a variable of type 'Stream'. String s = await f().first; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. +// ^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'String'. } -''', - [ - error(diag.unusedLocalVariable, 61, 1), - error(diag.unusedLocalVariable, 87, 1), - error(diag.invalidAssignment, 91, 3), - error(diag.unusedLocalVariable, 105, 1), - error(diag.invalidAssignment, 109, 15), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Stream Function()'); } test_blockBodiedLambdas_inferBottom_sync() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' var h = null; void foo(int g(Object _)) {} main() { var f = (Object x) { return null; }; String y = f(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'String'. f = (x) => 'hello'; +// ^^^^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'String' isn't returnable from a 'Null' function, as required by the closure's context. foo((x) { return null; }); +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'int' function, as required by the closure's context. foo((x) { throw "not implemented"; }); } -''', - [ - error(diag.unusedLocalVariable, 101, 1), - error(diag.invalidAssignment, 105, 5), - error(diag.returnOfInvalidTypeFromClosure, 126, 7), - error(diag.returnOfInvalidTypeFromClosure, 155, 4), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Null Function(Object)'); } test_blockBodiedLambdas_inferBottom_syncStar() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { var f = () sync* { yield null; }; Iterable y = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. Iterable z = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'Iterable' can't be assigned to a variable of type 'Iterable'. String s = f().first; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'String'. } -''', - [ - error(diag.unusedLocalVariable, 56, 1), - error(diag.unusedLocalVariable, 84, 1), - error(diag.invalidAssignment, 88, 3), - error(diag.unusedLocalVariable, 102, 1), - error(diag.invalidAssignment, 106, 9), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Iterable Function()'); } test_blockBodiedLambdas_LUB() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math' show Random; test2() { List o; var y = o.map((x) { +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'o' must be assigned before it can be used. if (new Random().nextBool()) { return x.toInt() + 1; } else { @@ -344,73 +341,69 @@ test2() { } }); Iterable w = y; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'w' isn't used. Iterable z = y; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'Iterable' can't be assigned to a variable of type 'Iterable'. } -''', - [ - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 67, 1), - error(diag.unusedLocalVariable, 210, 1), - error(diag.unusedLocalVariable, 233, 1), - error(diag.invalidAssignment, 237, 1), - ], - ); +'''); } test_blockBodiedLambdas_nestedLambdas() async { // Original feature request: https://github.com/dart-lang/sdk/issues/25487 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { var f = () { +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'f' isn't used. return (int x) { return 2.0 * x; }; }; } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'double Function(int) Function()'); } test_blockBodiedLambdas_noReturn() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' test1() { List o; var y = o.map((x) { }); +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'o' must be assigned before it can be used. Iterable z = y; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'Iterable' can't be assigned to a variable of type 'Iterable'. } -''', - [ - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 35, 1), - error(diag.unusedLocalVariable, 67, 1), - error(diag.invalidAssignment, 71, 1), - ], - ); +'''); var y = findElement2.localVar('y'); _assertTypeStr(y.type, 'Iterable'); } test_blockBodiedLambdas_syncStar() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { var f = () sync* { yield 1; yield* [3, 4.0]; }; Iterable g = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'g' isn't used. Iterable h = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'h' isn't used. +// ^^^ +// [diag.invalidAssignment] A value of type 'Iterable' can't be assigned to a variable of type 'Iterable'. } -''', - [ - error(diag.unusedLocalVariable, 85, 1), - error(diag.unusedLocalVariable, 110, 1), - error(diag.invalidAssignment, 114, 3), - ], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Iterable Function()'); @@ -433,13 +426,14 @@ var v = () => null; } test_circularReference_viaClosures() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' var x = () => y; +// ^ +// [diag.topLevelCycle] The type of 'x' can't be inferred because it depends on itself through the cycle: x, y. var y = () => x; -''', - [error(diag.topLevelCycle, 4, 1), error(diag.topLevelCycle, 21, 1)], - ); +// ^ +// [diag.topLevelCycle] The type of 'y' can't be inferred because it depends on itself through the cycle: x, y. +'''); var x = _resultLibraryElement.topLevelVariables[0]; var y = _resultLibraryElement.topLevelVariables[1]; @@ -469,78 +463,74 @@ var y = () => x; } test_conflictsCanHappen2() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class I1 { int x; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'x' must be initialized. } class I2 { int y; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'y' must be initialized. } class I3 implements I1, I2 { int x; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'x' must be initialized. int y; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'y' must be initialized. } class A { final I1 a = null; +// ^ +// [context 1] The member being overridden. +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'I1'. } class B { final I2 a = null; +// ^ +// [context 2] The member being overridden. +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'I2'. } class C1 implements A, B { I3 get a => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'a' because it has a return type of 'I3'. } class C2 implements A, B { get a => null; +// ^ +// [diag.invalidOverride][context 1] 'C2.a' ('dynamic Function()') isn't a valid override of 'A.a' ('I1 Function()'). +// [diag.invalidOverride][context 2] 'C2.a' ('dynamic Function()') isn't a valid override of 'B.a' ('I2 Function()'). } -''', - [ - error(diag.notInitializedNonNullableInstanceField, 17, 1), - error(diag.notInitializedNonNullableInstanceField, 39, 1), - error(diag.notInitializedNonNullableInstanceField, 80, 1), - error(diag.notInitializedNonNullableInstanceField, 89, 1), - error(diag.invalidAssignment, 120, 4), - error(diag.invalidAssignment, 154, 4), - error(diag.returnOfInvalidTypeFromFunction, 204, 4), - error( - diag.invalidOverride, - 246, - 1, - contextMessages: [message(testFile, 116, 1)], - ), - error( - diag.invalidOverride, - 246, - 1, - contextMessages: [message(testFile, 150, 1)], - ), - ], - ); +'''); } test_constructors_downwardsWithConstraint() async { // Regression test for https://github.com/dart-lang/sdk/issues/26431 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A {} class Foo {} void main() { Foo foo = new Foo(); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } -''', - [error(diag.unusedLocalVariable, 81, 3)], - ); +'''); } test_constructors_inferFromArguments() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T t; C(this.t); @@ -551,27 +541,30 @@ main() { num y; C c_int = new C(y); +// ^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'c_int' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'y' must be assigned before it can be used. +// [diag.argumentTypeNotAssignable] The argument type 'num' can't be assigned to the parameter type 'int'. // These hints are not reported because we resolve with a null error listener. C c_num = new C(123); +// ^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'c_num' isn't used. C c_num2 = (new C(456)) +// ^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'c_num2' isn't used. ..t = 1.0; // Don't infer from explicit dynamic. var c_dynamic = new C(42); +// ^^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'c_dynamic' isn't used. x.t = 'hello'; +// ^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.unusedLocalVariable, 85, 5), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 99, 1), - error(diag.argumentTypeNotAssignable, 99, 1), - error(diag.unusedLocalVariable, 194, 5), - error(diag.unusedLocalVariable, 223, 6), - error(diag.unusedLocalVariable, 309, 9), - error(diag.invalidAssignment, 349, 7), - ], - ); +'''); _assertTypeStr(findElement2.localVar('x').type, 'C'); _assertTypeStr(findElement2.localVar('c_int').type, 'C'); @@ -580,8 +573,7 @@ main() { } test_constructors_inferFromArguments_const() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { final T t; const C(this.t); @@ -589,10 +581,10 @@ class C { main() { var x = const C(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [error(diag.unusedLocalVariable, 63, 1)], - ); +'''); var x = findElement2.localVar('x'); _assertTypeStr(x.type, 'C'); @@ -600,8 +592,7 @@ main() { test_constructors_inferFromArguments_constWithUpperBound() async { // Regression for https://github.com/dart-lang/sdk/issues/26993 - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { final T x; const C(this.x); @@ -612,45 +603,44 @@ class D { void f() { const c = const C(0); C c2 = c; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c2' isn't used. const D d = const D(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'd' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 143, 2), - error(diag.unusedLocalVariable, 166, 1), - ], - ); +'''); } - test_constructors_inferFromArguments_downwardsFromConstructor() { - return assertErrorsInCode( - r''' + test_constructors_inferFromArguments_downwardsFromConstructor() async { + await resolveTestCodeWithDiagnostics(r''' class C { C(List list); } main() { var x = new C([123]); C y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. var a = new C([123]); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. // This one however works. var b = new C([123]); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 75, 1), - error(diag.unusedLocalVariable, 89, 1), - error(diag.unusedLocalVariable, 151, 1), - ], - ); +'''); } test_constructors_inferFromArguments_factory() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T t; C._(); +//^^^ +// [diag.notInitializedNonNullableInstanceFieldConstructor] Non-nullable instance field 't' must be initialized. factory C(T t) { var c = new C._(); @@ -663,13 +653,10 @@ class C { main() { var x = new C(42); x.t = 'hello'; +// ^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.notInitializedNonNullableInstanceFieldConstructor, 23, 3), - error(diag.invalidAssignment, 149, 7), - ], - ); +'''); var x = findElement2.localVar('x'); _assertTypeStr(x.type, 'C'); @@ -687,35 +674,34 @@ class A { } test_constructors_inferFromArguments_named() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T t; C.named(List t); +//^^^^^^^ +// [diag.notInitializedNonNullableInstanceFieldConstructor] Non-nullable instance field 't' must be initialized. } main() { var x = new C.named([]); x.t = 'hello'; +// ^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.notInitializedNonNullableInstanceFieldConstructor, 22, 7), - error(diag.invalidAssignment, 95, 7), - ], - ); +'''); var x = findElement2.localVar('x'); _assertTypeStr(x.type, 'C'); } test_constructors_inferFromArguments_namedFactory() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T t; C(); +//^ +// [diag.notInitializedNonNullableInstanceFieldConstructor] Non-nullable instance field 't' must be initialized. factory C.named(T t) { var c = new C(); @@ -728,21 +714,17 @@ class C { main() { var x = new C.named(42); x.t = 'hello'; +// ^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.notInitializedNonNullableInstanceFieldConstructor, 22, 1), - error(diag.invalidAssignment, 156, 7), - ], - ); +'''); var x = findElement2.localVar('x'); _assertTypeStr(x.type, 'C'); } test_constructors_inferFromArguments_redirecting() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T t; C(this.t); @@ -753,18 +735,17 @@ class C { main() { var x = new C.named([42]); x.t = 'hello'; +// ^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [error(diag.invalidAssignment, 123, 7)], - ); +'''); var x = findElement2.localVar('x'); _assertTypeStr(x.type, 'C'); } test_constructors_inferFromArguments_redirectingFactory() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' abstract class C { T get t; void set t(T t); @@ -780,10 +761,10 @@ class CImpl implements C { main() { var x = new C(42); x.t = 'hello'; +// ^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [error(diag.invalidAssignment, 183, 7)], - ); +'''); var x = findElement2.localVar('x'); _assertTypeStr(x.type, 'C'); @@ -802,55 +783,48 @@ class Pair { } test_constructors_tooManyPositionalArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A {} main() { var a = new A(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. +// ^^ +// [diag.extraPositionalArguments] Too many positional arguments: 0 expected, but 1 found. } -''', - [ - error(diag.unusedLocalVariable, 29, 1), - error(diag.extraPositionalArguments, 39, 2), - ], - ); +'''); var a = findElement2.localVar('a'); _assertTypeStr(a.type, 'A'); } test_doNotInferOverriddenFieldsThatExplicitlySayDynamic_infer() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class A { final int x = 2; +// ^ +// [context 1] The member being overridden. } class B implements A { dynamic get x => 3; +// ^ +// [diag.invalidOverride][context 1] 'B.x' ('dynamic Function()') isn't a valid override of 'A.x' ('int Function()'). } foo() { String y = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. int z = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error( - diag.invalidOverride, - 69, - 1, - contextMessages: [message(testFile, 22, 1)], - ), - error(diag.unusedLocalVariable, 97, 1), - error(diag.unusedLocalVariable, 118, 1), - ], - ); +'''); } test_dontInferFieldTypeWhenInitializerIsNull() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' var x = null; var y = 3; class A { @@ -864,48 +838,45 @@ class A { test() { x = "hi"; y = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. A.x = "hi"; A.y = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. new A().x2 = "hi"; new A().y2 = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.invalidAssignment, 140, 4), - error(diag.invalidAssignment, 168, 4), - error(diag.invalidAssignment, 210, 4), - ], - ); +'''); } test_dontInferTypeOnDynamic() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' test() { dynamic x = 3; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. x = "hi"; } -''', - [error(diag.unusedLocalVariable, 19, 1)], - ); +'''); } test_dontInferTypeWhenInitializerIsNull() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' test() { var x = null; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. x = "hi"; x = 3; } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); } test_downwardInference_miscellaneous() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' typedef T Function2(S x); class A { Function2 x; @@ -921,18 +892,19 @@ void main() { { int f(int x) => 0; A a = new A(f); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. } } -''', - [error(diag.unusedLocalVariable, 266, 1)], - ); +'''); } test_downwardsInference_insideTopLevel() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { B b; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'b' must be initialized. } class B { @@ -944,9 +916,7 @@ var t2 = >[new B(2)]; var t3 = [ new B(3) ]; -''', - [error(diag.notInitializedNonNullableInstanceField, 19, 1)], - ); +'''); } test_downwardsInferenceAnnotations() async { @@ -963,81 +933,75 @@ class Baz {} } test_downwardsInferenceAssignmentStatements() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' void main() { List l; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'l' isn't used. l = ["hello"]; +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. l = (l = [1]); } -''', - [ - error(diag.unusedLocalVariable, 26, 1), - error(diag.listElementTypeNotAssignable, 36, 7), - ], - ); +'''); } test_downwardsInferenceAsyncAwait() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' Future test() async { dynamic d; List l0 = await [d]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. List l1 = await new Future.value([d]); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 47, 2), - error(diag.unusedLocalVariable, 75, 2), - ], - ); +'''); } test_downwardsInferenceForEach() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' abstract class MyStream extends Stream { factory MyStream() => throw 0; } Future main() async { for(int x in [1, 2, 3]) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. await for(int x in new MyStream()) {} +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 115, 1), - error(diag.unusedLocalVariable, 150, 1), - ], - ); +'''); } test_downwardsInferenceInitializingFormalDefaultFormal() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' typedef T Function2([S x]); class Foo { List x; Foo([this.x = const [1]]); Foo.named([List x = const [1]]); +//^^^^^^^^^ +// [diag.notInitializedNonNullableInstanceFieldConstructor] Non-nullable instance field 'x' must be initialized. } void f([List l = const [1]]) {} // We do this inference in an early task but don't preserve the infos. Function2, String> g = ([llll = const [1]]) => "hello"; -''', - [error(diag.notInitializedNonNullableInstanceFieldConstructor, 92, 9)], - ); +'''); } test_downwardsInferenceOnConstructorArguments_inferDownwards() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class F0 { F0(List a) {} } class F1 { F1({List a}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class F2 { F2(Iterable a) {} @@ -1047,238 +1011,287 @@ class F3 { } class F4 { F4({Iterable> a}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } void main() { new F0([]); new F0([3]); new F0(["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F0(["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F1(a: []); new F1(a: [3]); new F1(a: ["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F1(a: ["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F2([]); new F2([3]); new F2(["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F2(["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F3([]); new F3([[3]]); new F3([["hello"]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F3([["hello"], [3]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F4(a: []); new F4(a: [[3]]); new F4(a: [["hello"]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F4(a: [["hello"], [3]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } -''', - [ - error(diag.missingDefaultValueForParameter, 61, 1), - error(diag.missingDefaultValueForParameter, 197, 1), - error(diag.listElementTypeNotAssignable, 259, 7), - error(diag.listElementTypeNotAssignable, 280, 7), - error(diag.listElementTypeNotAssignable, 343, 7), - error(diag.listElementTypeNotAssignable, 367, 7), - error(diag.listElementTypeNotAssignable, 421, 7), - error(diag.listElementTypeNotAssignable, 442, 7), - error(diag.listElementTypeNotAssignable, 499, 7), - error(diag.listElementTypeNotAssignable, 522, 7), - error(diag.listElementTypeNotAssignable, 591, 7), - error(diag.listElementTypeNotAssignable, 617, 7), - ], - ); +'''); } test_downwardsInferenceOnFunctionArguments_inferDownwards() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' void f0(List a) {} void f1({List a}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. void f2(Iterable a) {} void f3(Iterable> a) {} void f4({Iterable> a}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. void main() { f0([]); f0([3]); f0(["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f0(["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f1(a: []); f1(a: [3]); f1(a: ["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f1(a: ["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f2([]); f2([3]); f2(["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f2(["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f3([]); f3([[3]]); f3([["hello"]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f3([["hello"], [3]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f4(a: []); f4(a: [[3]]); f4(a: [["hello"]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. f4(a: [["hello"], [3]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } -''', - [ - error(diag.missingDefaultValueForParameter, 43, 1), - error(diag.missingDefaultValueForParameter, 149, 1), - error(diag.listElementTypeNotAssignable, 197, 7), - error(diag.listElementTypeNotAssignable, 214, 7), - error(diag.listElementTypeNotAssignable, 265, 7), - error(diag.listElementTypeNotAssignable, 285, 7), - error(diag.listElementTypeNotAssignable, 327, 7), - error(diag.listElementTypeNotAssignable, 344, 7), - error(diag.listElementTypeNotAssignable, 389, 7), - error(diag.listElementTypeNotAssignable, 408, 7), - error(diag.listElementTypeNotAssignable, 465, 7), - error(diag.listElementTypeNotAssignable, 487, 7), - ], - ); +'''); } test_downwardsInferenceOnFunctionExpressions() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' typedef T Function2(S x); void main () { { Function2 l0 = (int x) => null; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. Function2 l1 = (int x) => "hello"; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Function2 l2 = (String x) => "hello"; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'String Function(String)' can't be assigned to a variable of type 'Function2'. Function2 l3 = (int x) => 3; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. Function2 l4 = (int x) {return 3;}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } { Function2 l0 = (x) => null; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. Function2 l1 = (x) => "hello"; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Function2 l2 = (x) => 3; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. Function2 l3 = (x) {return 3;}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. Function2 l4 = (x) {return x;}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } { Function2> l0 = (int x) => null; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'List' function, as required by the closure's context. Function2> l1 = (int x) => ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Function2> l2 = (String x) => ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List Function(String)' can't be assigned to a variable of type 'Function2>'. Function2> l3 = (int x) => [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. Function2> l4 = (int x) {return [3];}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. } { Function2 l0 = (x) => x; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Function2 l1 = (x) => x+1; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Function2 l2 = (x) => x; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. Function2 l3 = (x) => x.substring(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^^^ +// [diag.undefinedMethod] The method 'substring' isn't defined for the type 'int'. Function2 l4 = (x) => x.substring(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. } } -''', - [ - error(diag.unusedLocalVariable, 79, 2), - error(diag.returnOfInvalidTypeFromClosure, 95, 4), - error(diag.unusedLocalVariable, 128, 2), - error(diag.unusedLocalVariable, 180, 2), - error(diag.invalidAssignment, 185, 21), - error(diag.unusedLocalVariable, 235, 2), - error(diag.returnOfInvalidTypeFromClosure, 251, 1), - error(diag.unusedLocalVariable, 281, 2), - error(diag.returnOfInvalidTypeFromClosure, 302, 1), - error(diag.unusedLocalVariable, 342, 2), - error(diag.returnOfInvalidTypeFromClosure, 354, 4), - error(diag.unusedLocalVariable, 387, 2), - error(diag.unusedLocalVariable, 435, 2), - error(diag.returnOfInvalidTypeFromClosure, 447, 1), - error(diag.unusedLocalVariable, 477, 2), - error(diag.returnOfInvalidTypeFromClosure, 494, 1), - error(diag.unusedLocalVariable, 526, 2), - error(diag.returnOfInvalidTypeFromClosure, 543, 1), - error(diag.unusedLocalVariable, 589, 2), - error(diag.returnOfInvalidTypeFromClosure, 605, 4), - error(diag.unusedLocalVariable, 644, 2), - error(diag.unusedLocalVariable, 704, 2), - error(diag.invalidAssignment, 709, 23), - error(diag.unusedLocalVariable, 767, 2), - error(diag.listElementTypeNotAssignable, 784, 1), - error(diag.unusedLocalVariable, 821, 2), - error(diag.listElementTypeNotAssignable, 843, 1), - error(diag.unusedLocalVariable, 881, 2), - error(diag.unusedLocalVariable, 920, 2), - error(diag.unusedLocalVariable, 964, 2), - error(diag.returnOfInvalidTypeFromClosure, 976, 1), - error(diag.unusedLocalVariable, 1006, 2), - error(diag.undefinedMethod, 1020, 9), - error(diag.unusedLocalVariable, 1064, 2), - ], - ); +'''); } test_downwardsInferenceOnFunctionOfTUsingTheT() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' void main () { { T f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. var v1 = f; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'v1' isn't used. v1 = (x) => x; } { List f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'List'. var v2 = f; v2 = (x) => [x]; Iterable r = v2(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'r' isn't used. Iterable s = v2('hello'); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. Iterable> t = v2([]); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 't' isn't used. Iterable u = v2(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'u' isn't used. Iterable v = v2(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 38, 4), - error(diag.unusedLocalVariable, 52, 2), - error(diag.returnOfInvalidTypeFromFunction, 115, 4), - error(diag.unusedLocalVariable, 179, 1), - error(diag.unusedLocalVariable, 212, 1), - error(diag.unusedLocalVariable, 253, 1), - error(diag.unusedLocalVariable, 288, 1), - error(diag.unusedLocalVariable, 318, 1), - ], - ); +'''); } test_downwardsInferenceOnGenericConstructorArguments_emptyList() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class F3 { F3(Iterable> a) {} } class F4 { F4({Iterable> a}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } void main() { new F3([]); new F4(a: []); } -''', - [error(diag.missingDefaultValueForParameter, 91, 1)], - ); +'''); } test_downwardsInferenceOnGenericConstructorArguments_inferDownwards() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class F0 { F0(List a) {} } class F1 { F1({List a}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class F2 { F2(Iterable a) {} @@ -1288,6 +1301,8 @@ class F3 { } class F4 { F4({Iterable> a}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class F5 { F5(Iterable>> a) {} @@ -1296,32 +1311,58 @@ void main() { new F0([]); new F0([3]); new F0(["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F0(["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F1(a: []); new F1(a: [3]); new F1(a: ["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F1(a: ["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F2([]); new F2([3]); new F2(["hello"]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F2(["hello", 3]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F3([]); new F3([[3]]); new F3([["hello"]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F3([["hello"], [3]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F4(a: []); new F4(a: [[3]]); new F4(a: [["hello"]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F4(a: [["hello"], [3]]); +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. new F3([]); var f31 = new F3([[3]]); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'f31' isn't used. var f32 = new F3([["hello"]]); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'f32' isn't used. var f33 = new F3([["hello"], [3]]); +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'f33' isn't used. new F4(a: []); new F4(a: [[3]]); @@ -1330,107 +1371,110 @@ void main() { new F5([[[3]]]); } -''', - [ - error(diag.missingDefaultValueForParameter, 63, 1), - error(diag.missingDefaultValueForParameter, 202, 1), - error(diag.listElementTypeNotAssignable, 338, 7), - error(diag.listElementTypeNotAssignable, 364, 7), - error(diag.listElementTypeNotAssignable, 442, 7), - error(diag.listElementTypeNotAssignable, 471, 7), - error(diag.listElementTypeNotAssignable, 540, 7), - error(diag.listElementTypeNotAssignable, 566, 7), - error(diag.listElementTypeNotAssignable, 638, 7), - error(diag.listElementTypeNotAssignable, 666, 7), - error(diag.listElementTypeNotAssignable, 750, 7), - error(diag.listElementTypeNotAssignable, 781, 7), - error(diag.unusedLocalVariable, 819, 3), - error(diag.unusedLocalVariable, 846, 3), - error(diag.unusedLocalVariable, 879, 3), - ], - ); +'''); } test_downwardsInferenceOnGenericFunctionExpressions() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' void main () { { String f(int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'String'. var v = f; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. v = (int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. v = (int x) => "hello"; v = (String x) => "hello"; +// ^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'String Function(String)' can't be assigned to a variable of type 'String Function(int)'. v = (int x) => 3; +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. v = (int x) {return 3;}; +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } { String f(int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'String'. var v = f; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. v = (x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'String' function, as required by the closure's context. v = (x) => "hello"; v = (x) => 3; +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. v = (x) {return 3;}; +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. v = (x) {return x;}; +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. } { List f(int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'List'. var v = f; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. v = (int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'List' function, as required by the closure's context. v = (int x) => ["hello"]; v = (String x) => ["hello"]; +// ^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List Function(String)' can't be assigned to a variable of type 'List Function(int)'. v = (int x) => [3]; +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. v = (int x) {return [3];}; +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. } { int int2int(int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'int2int' because it has a return type of 'int'. String int2String(int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'int2String' because it has a return type of 'String'. String string2String(String x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'string2String' because it has a return type of 'String'. var x = int2int; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. x = (x) => x; x = (x) => x+1; var y = int2String; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. y = (x) => x; +// ^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'String' function, as required by the closure's context. y = (x) => x.substring(3); +// ^^^^^^^^^ +// [diag.undefinedMethod] The method 'substring' isn't defined for the type 'int'. var z = string2String; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. z = (x) => x.substring(3); } } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 45, 4), - error(diag.unusedLocalVariable, 59, 1), - error(diag.returnOfInvalidTypeFromClosure, 88, 4), - error(diag.invalidAssignment, 133, 24), - error(diag.returnOfInvalidTypeFromClosure, 181, 1), - error(diag.returnOfInvalidTypeFromClosure, 211, 1), - error(diag.returnOfInvalidTypeFromFunction, 250, 4), - error(diag.unusedLocalVariable, 264, 1), - error(diag.returnOfInvalidTypeFromClosure, 289, 4), - error(diag.returnOfInvalidTypeFromClosure, 340, 1), - error(diag.returnOfInvalidTypeFromClosure, 366, 1), - error(diag.returnOfInvalidTypeFromClosure, 394, 1), - error(diag.returnOfInvalidTypeFromFunction, 439, 4), - error(diag.unusedLocalVariable, 453, 1), - error(diag.returnOfInvalidTypeFromClosure, 482, 4), - error(diag.invalidAssignment, 529, 26), - error(diag.listElementTypeNotAssignable, 580, 1), - error(diag.listElementTypeNotAssignable, 612, 1), - error(diag.returnOfInvalidTypeFromFunction, 655, 4), - error(diag.returnOfInvalidTypeFromFunction, 696, 4), - error(diag.returnOfInvalidTypeFromFunction, 743, 4), - error(diag.unusedLocalVariable, 757, 1), - error(diag.unusedLocalVariable, 822, 1), - error(diag.returnOfInvalidTypeFromClosure, 856, 1), - error(diag.undefinedMethod, 879, 9), - error(diag.unusedLocalVariable, 901, 1), - ], - ); +'''); } test_downwardsInferenceOnInstanceCreations_inferDownwards() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { S x; T y; @@ -1455,246 +1499,350 @@ class D extends B { class E extends A, T> { E(T a) : super(null, a); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'C'. } class F extends A { F(S x, T y, {List a, List b}) : super(x, y); +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'b' can't have a value of 'null' because of its type, but the implicit default value is 'null'. F.named(S x, T y, [S a, T b]) : super(a, b); +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'a' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'b' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } void main() { { A a0 = new A(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new A.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. A a2 = new A(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new A.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new A(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'A' can't be assigned to a variable of type 'A'. A a5 = new A.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'A' can't be assigned to a variable of type 'A'. } { A a0 = new A("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. A a1 = new A.named("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. } { A a0 = new B("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new B.named("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. A a2 = new B("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new B.named("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new B("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'B' can't be assigned to a variable of type 'A'. A a5 = new B.named("hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'B' can't be assigned to a variable of type 'A'. } { A a0 = new B(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. A a1 = new B.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. } { A a0 = new C(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new C.named(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. A a2 = new C(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new C.named(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new C(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'C' can't be assigned to a variable of type 'A'. A a5 = new C.named(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'C' can't be assigned to a variable of type 'A'. } { A a0 = new C("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. A a1 = new C.named("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. } { A a0 = new D("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. A a1 = new D.named("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. A a2 = new D("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new D.named("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new D("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'D' can't be assigned to a variable of type 'A'. A a5 = new D.named("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'D' can't be assigned to a variable of type 'A'. } { A a0 = new D(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. A a1 = new D.named(3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. } { A, String> a0 = new E("hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. } { // Check named and optional arguments A a0 = new F(3, "hello", +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a0' isn't used. a: [3], b: ["hello"]); A a1 = new F(3, "hello", +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. a: ["hello"], +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. b: [3]); +// ^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. A a2 = new F.named(3, "hello", 3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. A a3 = new F.named(3, "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a3' isn't used. A a4 = new F.named(3, "hello", "hello", 3); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a4' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'String'. A a5 = new F.named(3, "hello", "hello"); +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a5' isn't used. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. } } -''', - [ - error(diag.argumentTypeNotAssignable, 427, 4), - error(diag.missingDefaultValueForParameter, 495, 1), - error(diag.missingDefaultValueForParameter, 506, 1), - error(diag.missingDefaultValueForParameterPositional, 548, 1), - error(diag.missingDefaultValueForParameterPositional, 553, 1), - error(diag.unusedLocalVariable, 612, 2), - error(diag.unusedLocalVariable, 655, 2), - error(diag.unusedLocalVariable, 704, 2), - error(diag.unusedLocalVariable, 760, 2), - error(diag.unusedLocalVariable, 822, 2), - error(diag.invalidAssignment, 827, 31), - error(diag.unusedLocalVariable, 879, 2), - error(diag.invalidAssignment, 884, 41), - error(diag.unusedLocalVariable, 954, 2), - error(diag.argumentTypeNotAssignable, 965, 7), - error(diag.argumentTypeNotAssignable, 974, 1), - error(diag.unusedLocalVariable, 997, 2), - error(diag.argumentTypeNotAssignable, 1014, 7), - error(diag.argumentTypeNotAssignable, 1023, 1), - error(diag.unusedLocalVariable, 1054, 2), - error(diag.unusedLocalVariable, 1097, 2), - error(diag.unusedLocalVariable, 1146, 2), - error(diag.unusedLocalVariable, 1202, 2), - error(diag.unusedLocalVariable, 1264, 2), - error(diag.invalidAssignment, 1269, 34), - error(diag.unusedLocalVariable, 1324, 2), - error(diag.invalidAssignment, 1329, 41), - error(diag.unusedLocalVariable, 1399, 2), - error(diag.argumentTypeNotAssignable, 1410, 1), - error(diag.argumentTypeNotAssignable, 1413, 7), - error(diag.unusedLocalVariable, 1442, 2), - error(diag.argumentTypeNotAssignable, 1459, 1), - error(diag.argumentTypeNotAssignable, 1462, 7), - error(diag.unusedLocalVariable, 1496, 2), - error(diag.unusedLocalVariable, 1527, 2), - error(diag.unusedLocalVariable, 1564, 2), - error(diag.unusedLocalVariable, 1600, 2), - error(diag.unusedLocalVariable, 1642, 2), - error(diag.invalidAssignment, 1647, 17), - error(diag.unusedLocalVariable, 1682, 2), - error(diag.invalidAssignment, 1687, 23), - error(diag.unusedLocalVariable, 1736, 2), - error(diag.argumentTypeNotAssignable, 1747, 7), - error(diag.unusedLocalVariable, 1773, 2), - error(diag.argumentTypeNotAssignable, 1790, 7), - error(diag.unusedLocalVariable, 1827, 2), - error(diag.unusedLocalVariable, 1867, 2), - error(diag.unusedLocalVariable, 1913, 2), - error(diag.unusedLocalVariable, 1966, 2), - error(diag.unusedLocalVariable, 2028, 2), - error(diag.invalidAssignment, 2033, 28), - error(diag.unusedLocalVariable, 2082, 2), - error(diag.invalidAssignment, 2087, 38), - error(diag.unusedLocalVariable, 2154, 2), - error(diag.argumentTypeNotAssignable, 2165, 1), - error(diag.unusedLocalVariable, 2188, 2), - error(diag.argumentTypeNotAssignable, 2205, 1), - error(diag.unusedLocalVariable, 2239, 2), - error(diag.unusedLocalVariable, 2325, 2), - error(diag.unusedLocalVariable, 2406, 2), - error(diag.listElementTypeNotAssignable, 2441, 7), - error(diag.listElementTypeNotAssignable, 2463, 1), - error(diag.unusedLocalVariable, 2487, 2), - error(diag.unusedLocalVariable, 2548, 2), - error(diag.unusedLocalVariable, 2597, 2), - error(diag.argumentTypeNotAssignable, 2626, 7), - error(diag.argumentTypeNotAssignable, 2635, 1), - error(diag.unusedLocalVariable, 2658, 2), - error(diag.argumentTypeNotAssignable, 2687, 7), - ], - ); +'''); } test_downwardsInferenceOnListLiterals_inferDownwards() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' void foo([List list1 = const [], List list2 = const [42]]) { +// ^^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. } void main() { { List l0 = []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. List l1 = [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. List l2 = ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. List l3 = ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } { List l0 = []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. List l1 = [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. List l2 = ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. List l3 = ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. } { List l0 = []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^^^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. List l1 = [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. +// ^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. List l2 = ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. +// ^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'num'. List l3 = ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'num'. } { Iterable i0 = []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'i0' isn't used. Iterable i1 = [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'i1' isn't used. Iterable i2 = ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'i2' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. Iterable i3 = ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'i3' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } { const List c0 = const []; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c0' isn't used. const List c1 = const [3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c1' isn't used. const List c2 = const ["hello"]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c2' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. const List c3 = const ["hello", 3]; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'c3' isn't used. +// ^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'String' can't be assigned to the list type 'int'. } } -''', - [ - error(diag.listElementTypeNotAssignable, 79, 2), - error(diag.unusedLocalVariable, 122, 2), - error(diag.unusedLocalVariable, 145, 2), - error(diag.unusedLocalVariable, 169, 2), - error(diag.listElementTypeNotAssignable, 175, 7), - error(diag.unusedLocalVariable, 199, 2), - error(diag.listElementTypeNotAssignable, 205, 7), - error(diag.unusedLocalVariable, 244, 2), - error(diag.unusedLocalVariable, 271, 2), - error(diag.unusedLocalVariable, 299, 2), - error(diag.unusedLocalVariable, 333, 2), - error(diag.unusedLocalVariable, 374, 2), - error(diag.invalidAssignment, 379, 7), - error(diag.unusedLocalVariable, 402, 2), - error(diag.invalidAssignment, 407, 8), - error(diag.unusedLocalVariable, 431, 2), - error(diag.invalidAssignment, 436, 14), - error(diag.listElementTypeNotAssignable, 442, 7), - error(diag.unusedLocalVariable, 466, 2), - error(diag.invalidAssignment, 471, 17), - error(diag.listElementTypeNotAssignable, 477, 7), - error(diag.unusedLocalVariable, 516, 2), - error(diag.unusedLocalVariable, 543, 2), - error(diag.unusedLocalVariable, 571, 2), - error(diag.listElementTypeNotAssignable, 577, 7), - error(diag.unusedLocalVariable, 605, 2), - error(diag.listElementTypeNotAssignable, 611, 7), - error(diag.unusedLocalVariable, 652, 2), - error(diag.unusedLocalVariable, 687, 2), - error(diag.unusedLocalVariable, 723, 2), - error(diag.listElementTypeNotAssignable, 735, 7), - error(diag.unusedLocalVariable, 765, 2), - error(diag.listElementTypeNotAssignable, 777, 7), - ], - ); +'''); } test_downwardsInferenceOnListLiterals_inferIfValueTypesMatchContext() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class DartType {} typedef void Asserter(T type); typedef Asserter AsserterBuilder(S arg); Asserter _isInt; +// ^^^^^^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable '_isInt' must be initialized. Asserter _isString; +// ^^^^^^^^^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable '_isString' must be initialized. abstract class C { static AsserterBuilder>, DartType> assertBOf; +// ^^^^^^^^^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'assertBOf' must be initialized. static AsserterBuilder>, DartType> get assertCOf => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'assertCOf' because it has a return type of 'AsserterBuilder>, DartType>'. AsserterBuilder>, DartType> assertAOf; +// ^^^^^^^^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'assertAOf' must be initialized. AsserterBuilder>, DartType> get assertDOf; method(AsserterBuilder>, DartType> assertEOf) { @@ -1708,6 +1856,8 @@ abstract class C { abstract class G { AsserterBuilder>, DartType> assertAOf; +// ^^^^^^^^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'assertAOf' must be initialized. AsserterBuilder>, DartType> get assertDOf; method(AsserterBuilder>, DartType> assertEOf) { @@ -1719,11 +1869,17 @@ abstract class C { } AsserterBuilder>, DartType> assertBOf; +// ^^^^^^^^^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'assertBOf' must be initialized. AsserterBuilder>, DartType> get assertCOf => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'assertCOf' because it has a return type of 'AsserterBuilder>, DartType>'. main() { AsserterBuilder>, DartType> assertAOf; assertAOf([_isInt, _isString]); +//^^^^^^^^^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'assertAOf' must be assigned before it can be used. assertBOf([_isInt, _isString]); assertCOf([_isInt, _isString]); C.assertBOf([_isInt, _isString]); @@ -1731,200 +1887,230 @@ main() { C c; c.assertAOf([_isInt, _isString]); +//^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'c' must be assigned before it can be used. c.assertDOf([_isInt, _isString]); +//^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'c' must be assigned before it can be used. G g; g.assertAOf([_isInt, _isString]); +//^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'g' must be assigned before it can be used. g.assertDOf([_isInt, _isString]); +//^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'g' must be assigned before it can be used. } -''', - [ - error(diag.notInitializedNonNullableVariable, 122, 6), - error(diag.notInitializedNonNullableVariable, 149, 9), - error(diag.notInitializedNonNullableVariable, 241, 9), - error(diag.returnOfInvalidTypeFromFunction, 330, 4), - error(diag.notInitializedNonNullableInstanceField, 391, 9), - error(diag.notInitializedNonNullableInstanceField, 813, 9), - error(diag.notInitializedNonNullableVariable, 1181, 9), - error(diag.returnOfInvalidTypeFromFunction, 1261, 4), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 1344, 9), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 1526, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 1562, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 1611, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 1647, 1), - ], - ); +'''); } test_downwardsInferenceOnMapLiterals() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' void foo([Map m1 = const {1: "hello"}, Map m2 = const { // One error is from type checking and the other is from const evaluation. "hello": "world" +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. }]) { } void main() { { Map l0 = {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Map l1 = {3: "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Map l2 = { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. "hello": "hello" +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. }; Map l3 = { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. 3: 3 +// ^ +// [diag.mapValueTypeNotAssignable] The element type 'int' can't be assigned to the map value type 'String'. }; Map l4 = { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. 3: "hello", "hello": 3 +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. +// ^ +// [diag.mapValueTypeNotAssignable] The element type 'int' can't be assigned to the map value type 'String'. }; } { Map l0 = {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Map l1 = {3: "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Map l2 = {"hello": "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. Map l3 = {3: 3}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. Map l4 = {3:"hello", "hello": 3}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. } { Map l0 = {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Map l1 = {3: "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Map l2 = {"hello": "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. Map l3 = {3: 3}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^ +// [diag.mapValueTypeNotAssignable] The element type 'int' can't be assigned to the map value type 'String'. Map l4 = { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. 3: "hello", "hello": 3 +// ^ +// [diag.mapValueTypeNotAssignable] The element type 'int' can't be assigned to the map value type 'String'. }; } { Map l0 = {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. Map l1 = {3: "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. Map l2 = { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. "hello": "hello" +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. }; Map l3 = {3: 3}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. Map l4 = { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. 3:"hello", "hello": 3 +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. }; } { Map l0 = {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. +// ^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. Map l1 = {3: "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. Map l3 = {3: 3}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. +// ^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. } { const Map l0 = const {}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l0' isn't used. const Map l1 = const {3: "hello"}; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l1' isn't used. const Map l2 = const { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l2' isn't used. "hello": "hello" +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. }; const Map l3 = const { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l3' isn't used. 3: 3 +// ^ +// [diag.mapValueTypeNotAssignable] The element type 'int' can't be assigned to the map value type 'String'. }; const Map l4 = const { +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'l4' isn't used. 3:"hello", "hello": 3 +// ^^^^^^^ +// [diag.mapKeyTypeNotAssignable] The element type 'String' can't be assigned to the map key type 'int'. +// ^ +// [diag.mapValueTypeNotAssignable] The element type 'int' can't be assigned to the map value type 'String'. }; } } -''', - [ - error(diag.mapKeyTypeNotAssignable, 173, 7), - error(diag.unusedLocalVariable, 241, 2), - error(diag.unusedLocalVariable, 271, 2), - error(diag.unusedLocalVariable, 311, 2), - error(diag.mapKeyTypeNotAssignable, 324, 7), - error(diag.unusedLocalVariable, 369, 2), - error(diag.mapValueTypeNotAssignable, 385, 1), - error(diag.unusedLocalVariable, 415, 2), - error(diag.mapKeyTypeNotAssignable, 446, 7), - error(diag.mapValueTypeNotAssignable, 455, 1), - error(diag.unusedLocalVariable, 498, 2), - error(diag.unusedLocalVariable, 533, 2), - error(diag.unusedLocalVariable, 578, 2), - error(diag.unusedLocalVariable, 629, 2), - error(diag.unusedLocalVariable, 668, 2), - error(diag.unusedLocalVariable, 731, 2), - error(diag.unusedLocalVariable, 765, 2), - error(diag.unusedLocalVariable, 809, 2), - error(diag.unusedLocalVariable, 859, 2), - error(diag.mapValueTypeNotAssignable, 868, 1), - error(diag.unusedLocalVariable, 897, 2), - error(diag.mapValueTypeNotAssignable, 937, 1), - error(diag.unusedLocalVariable, 976, 2), - error(diag.unusedLocalVariable, 1007, 2), - error(diag.unusedLocalVariable, 1048, 2), - error(diag.mapKeyTypeNotAssignable, 1061, 7), - error(diag.unusedLocalVariable, 1107, 2), - error(diag.unusedLocalVariable, 1142, 2), - error(diag.mapKeyTypeNotAssignable, 1172, 7), - error(diag.unusedLocalVariable, 1219, 2), - error(diag.invalidAssignment, 1224, 16), - error(diag.unusedLocalVariable, 1263, 2), - error(diag.invalidAssignment, 1268, 26), - error(diag.unusedLocalVariable, 1317, 2), - error(diag.invalidAssignment, 1322, 20), - error(diag.unusedLocalVariable, 1379, 2), - error(diag.unusedLocalVariable, 1421, 2), - error(diag.unusedLocalVariable, 1473, 2), - error(diag.mapKeyTypeNotAssignable, 1492, 7), - error(diag.unusedLocalVariable, 1543, 2), - error(diag.mapValueTypeNotAssignable, 1565, 1), - error(diag.unusedLocalVariable, 1601, 2), - error(diag.mapKeyTypeNotAssignable, 1637, 7), - error(diag.mapValueTypeNotAssignable, 1646, 1), - ], - ); +'''); } test_fieldRefersToStaticGetter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { final x = _x; static int get _x => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function '_x' because it has a return type of 'int'. } -''', - [error(diag.returnOfInvalidTypeFromFunction, 49, 4)], - ); +'''); var x = _resultLibraryElement.classes[0].fields[0]; _assertTypeStr(x.type, 'int'); } test_fieldRefersToTopLevelGetter() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { final x = y; } int get y => null; -''', - [error(diag.returnOfInvalidTypeFromFunction, 40, 4)], - ); +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'y' because it has a return type of 'int'. +'''); var x = _resultLibraryElement.classes[0].fields[0]; _assertTypeStr(x.type, 'int'); } test_futureOr_subtyping() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void add(int x) {} add2(int y) {} main() { Future f; var a = f.then(add); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'f' must be assigned before it can be used. var b = f.then(add2); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'f' must be assigned before it can be used. } -''', - [ - error(diag.unusedLocalVariable, 66, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 70, 1), - error(diag.unusedLocalVariable, 89, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 93, 1), - ], - ); +'''); } test_futureThen() async { @@ -2167,28 +2353,30 @@ main() { } test_futureThen_explicitFuture() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' m1() { Future f; var x = f.then>>((x) => []); +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'f' must be assigned before it can be used. +// ^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'List' isn't returnable from a 'FutureOr>>' function, as required by the closure's context. Future> y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'Future>>' can't be assigned to a variable of type 'Future>'. } m2() { Future f; var x = f.then>((x) => []); +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'f' must be assigned before it can be used. Future> y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [ - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 34, 1), - error(diag.returnOfInvalidTypeFromClosure, 67, 2), - error(diag.unusedLocalVariable, 92, 1), - error(diag.invalidAssignment, 96, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 135, 1), - error(diag.unusedLocalVariable, 185, 1), - ], - ); +'''); } test_futureThen_upwards() async { @@ -2278,22 +2466,21 @@ $declared foo() => new $declared.value(1); test_futureThen_upwardsFromBlock() async { // Regression test for https://github.com/dart-lang/sdk/issues/27113. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { Future base; var f = base.then((x) { return x == 0; }); +// ^^^^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'base' must be assigned before it can be used. var g = base.then((x) => x == 0); +// ^^^^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'base' must be assigned before it can be used. Future b = f; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. b = g; } -''', - [ - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 39, 4), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 84, 4), - error(diag.unusedLocalVariable, 125, 1), - ], - ); +'''); } test_futureUnion_asyncConditional() async { @@ -2450,46 +2637,42 @@ $downwards> g3() async { // // We need to take a future union into account for both directions of // generic method inference. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' foo() async { Future> f1 = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'Future>'. Future> f2 = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'Future>'. List> merged = await Future.wait([f1, f2]); +// ^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'merged' isn't used. } class A {} -''', - [ - error(diag.invalidAssignment, 37, 4), - error(diag.invalidAssignment, 66, 4), - error(diag.unusedLocalVariable, 88, 6), - ], - ); +'''); } test_futureUnion_downwardsGenericMethodWithGenericReturn() async { // Regression test for https://github.com/dart-lang/sdk/issues/27284 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' T id(T x) => x; main() async { Future f; String s = await id(f); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'f' must be assigned before it can be used. } -''', - [ - error(diag.unusedLocalVariable, 64, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 77, 1), - ], - ); +'''); } test_futureUnion_upwardsGenericMethods() async { // Regression test for https://github.com/dart-lang/sdk/issues/27151 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() async { var b = new Future.value(new B()); var c = new Future.value(new C()); @@ -2497,57 +2680,53 @@ main() async { var result = await Future.wait(lll); var result2 = await Future.wait([b, c]); List list = result; +// ^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'list' isn't used. list = result2; } class A {} class B extends A {} class C extends A {} -''', - [error(diag.unusedLocalVariable, 207, 4)], - ); +'''); } test_genericFunctions_returnTypedef() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' typedef void ToValue(T value); main() { ToValue f(T x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'ToValue'. var x = f(42); var y = f(42); ToValue takesInt = x; +// ^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'takesInt' isn't used. takesInt = y; } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 70, 4), - error(diag.unusedLocalVariable, 130, 8), - ], - ); +'''); } test_genericMethods_basicDownwardInference() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' T f(S s) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. main() { String x = f(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. String y = (f)(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 18, 4), - error(diag.unusedLocalVariable, 42, 1), - error(diag.unusedLocalVariable, 62, 1), - ], - ); +'''); } test_genericMethods_dartMathMinMax() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'dart:math'; void printInt(int x) => print(x); @@ -2564,169 +2743,156 @@ main() { // No help for user-defined functions from num->num->num. printInt(myMax(1, 2)); +// ^^^^^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'num' can't be assigned to the parameter type 'int'. printInt(myMax(1, 2) as int); // An int context means doubles are rejected printInt(max(1, 2.0)); +// ^^^ +// [diag.argumentTypeNotAssignable] The argument type 'double' can't be assigned to the parameter type 'int'. printInt(min(1, 2.0)); +// ^^^ +// [diag.argumentTypeNotAssignable] The argument type 'double' can't be assigned to the parameter type 'int'. // A double context means ints are accepted as doubles printDouble(max(1, 2.0)); printDouble(min(1, 2.0)); // Types other than int and double are not accepted. printInt(min("hi", "there")); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. +// ^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. } -''', - [ - error(diag.argumentTypeNotAssignable, 355, 11), - error(diag.argumentTypeNotAssignable, 467, 3), - error(diag.argumentTypeNotAssignable, 492, 3), - error(diag.argumentTypeNotAssignable, 683, 4), - error(diag.argumentTypeNotAssignable, 689, 7), - ], - ); +'''); } test_genericMethods_doNotInferInvalidOverrideOfGenericMethod() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { T m(T x) => x; +//^ +// [context 1] The member being overridden. } class D extends C { m(x) => x; +// [diag.invalidOverride][column 1][length 1][context 1] 'D.m' ('dynamic Function(dynamic)') isn't a valid override of 'C.m' ('T Function(T)'). } main() { int y = new D().m(42); +// ^^^^^ +// [diag.wrongNumberOfTypeArgumentsElement] The method 'm' is declared with 0 type parameters, but 1 type arguments are given. print(y); } -''', - [ - error( - diag.invalidOverride, - 50, - 1, - contextMessages: [message(testFile, 12, 1)], - ), - error(diag.wrongNumberOfTypeArgumentsElement, 91, 5), - ], - ); +'''); } test_genericMethods_downwardsInferenceAffectsArguments() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' T f(List s) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'T'. main() { String x = f(['hi']); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. String y = f([42]); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^ +// [diag.listElementTypeNotAssignable] The element type 'int' can't be assigned to the list type 'String'. } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 21, 4), - error(diag.unusedLocalVariable, 45, 1), - error(diag.unusedLocalVariable, 69, 1), - error(diag.listElementTypeNotAssignable, 76, 2), - ], - ); +'''); } test_genericMethods_downwardsInferenceFold() async { // Regression from https://github.com/dart-lang/sdk/issues/25491 // The first example works now, but the latter requires a full solution to // https://github.com/dart-lang/sdk/issues/25490 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' void main() { List o; int y = o.fold(0, (x, y) => x + y); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'o' must be assigned before it can be used. var z = o.fold(0, (x, y) => x + y); +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'o' must be assigned before it can be used. y = z; } void functionExpressionInvocation() { List o; int y = (o.fold)(0, (x, y) => x + y); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'o' must be assigned before it can be used. var z = (o.fold)(0, (x, y) => x + y); +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'o' must be assigned before it can be used. y = z; } -''', - [ - error(diag.unusedLocalVariable, 35, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 39, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 77, 1), - error(diag.unusedLocalVariable, 175, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 180, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 220, 1), - ], - ); +'''); } test_genericMethods_handleOverrideOfNonGenericWithGeneric() async { // Regression test for crash when adding genericity - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' class C { m(x) => x; +//^ +// [context 1] The member being overridden. dynamic g(int x) => x; +// ^ +// [context 2] The member being overridden. } class D extends C { T m(T x) => x; +// ^ +// [diag.invalidOverride][context 1] 'D.m' ('T Function(T)') isn't a valid override of 'C.m' ('dynamic Function(dynamic)'). T g(T x) => x; +// ^ +// [diag.invalidOverride][context 2] 'D.g' ('T Function(T)') isn't a valid override of 'C.g' ('dynamic Function(int)'). } main() { int y = (new D() as C).m(42); print(y); } -''', - [ - error( - diag.invalidOverride, - 74, - 1, - contextMessages: [message(testFile, 12, 1)], - ), - error( - diag.invalidOverride, - 94, - 1, - contextMessages: [message(testFile, 33, 1)], - ), - ], - ); +'''); } test_genericMethods_inferenceError() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { List y; Iterable x = y.map((String z) => 1.0); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'y' must be assigned before it can be used. +// ^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'double' isn't returnable from a 'String' function, as required by the closure's context. } -''', - [ - error(diag.unusedLocalVariable, 46, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 50, 1), - error(diag.returnOfInvalidTypeFromClosure, 70, 3), - ], - ); +'''); } test_genericMethods_inferGenericFunctionParameterType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C extends D { f(x) { return null; } +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'F'. } class D { F f(U u) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'F'. } typedef void F(V v); -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 45, 4), - error(diag.returnOfInvalidTypeFromMethod, 88, 4), - ], - ); +'''); var f = _resultLibraryElement.getClass('C')!.methods[0]; _assertTypeStr(f.type, 'void Function(U) Function(U)'); } @@ -2746,21 +2912,19 @@ typedef List G(); } test_genericMethods_inferGenericFunctionReturnType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C extends D { f(x) { return null; } +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'F'. } class D { F f(U u) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'F'. } typedef V F(); -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 45, 4), - error(diag.returnOfInvalidTypeFromMethod, 88, 4), - ], - ); +'''); var f = _resultLibraryElement.getClass('C')!.methods[0]; _assertTypeStr(f.type, 'U Function() Function(U)'); } @@ -2782,36 +2946,36 @@ main() { } test_genericMethods_IterableAndFuture() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' Future make(int x) => (new Future(() => x)); main() { Iterable> list = [1, 2, 3].map(make); Future> results = Future.wait(list); Future results2 = results.then((List list) +// ^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'results2' isn't used. => list.fold('', (x, y) => x + y.toString())); +// ^ +// [diag.undefinedOperator] The operator '+' isn't defined for the type 'FutureOr'. Future results3 = results.then((List list) +// ^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'results3' isn't used. => list.fold('', (String x, y) => x + y.toString())); +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String Function(String, int)' can't be assigned to the parameter type 'FutureOr Function(FutureOr, int)'. Future results4 = results.then((List list) +// ^^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'results4' isn't used. => list.fold('', (x, y) => x + y.toString())); } -''', - [ - error(diag.unusedLocalVariable, 183, 8), - error(diag.undefinedOperator, 257, 1), - error(diag.unusedLocalVariable, 293, 8), - error(diag.argumentTypeNotAssignable, 355, 33), - error(diag.unusedLocalVariable, 410, 8), - ], - ); +'''); } test_genericMethods_nestedGenericInstantiation() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' import 'dart:math' as math; class Trace { List frames = []; @@ -2822,49 +2986,46 @@ class Frame { main() { List traces = []; var longest = traces.map((trace) { +// ^^^^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'longest' isn't used. return trace.frames.map((frame) => frame.location.length) .fold(0, math.max); }).fold(0, math.max); } -''', - [error(diag.unusedLocalVariable, 153, 7)], - ); +'''); } test_genericMethods_usesGreatestLowerBound() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' typedef Iterable F(int x); typedef List G(double x); T generic(a(T _), b(T _)) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'generic' because it has a return type of 'T'. main() { var v = generic((F f) => null, (G g) => null); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 96, 4), - error(diag.unusedLocalVariable, 118, 1), - ], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List Function(num)'); } test_genericMethods_usesGreatestLowerBound_topLevel() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' typedef Iterable F(int x); typedef List G(double x); T generic(a(T _), b(T _)) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'generic' because it has a return type of 'T'. var v = generic((F f) => null, (G g) => null); -''', - [error(diag.returnOfInvalidTypeFromFunction, 96, 4)], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'List Function(num)'); } @@ -2877,10 +3038,11 @@ var b = (a[0] = 1.0); } test_infer_assignToProperty() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { int f; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'f' must be initialized. } var v_assign = (new A().f = 1); var v_plus = (new A().f += 1); @@ -2890,43 +3052,39 @@ var v_prefix_pp = (++new A().f); var v_prefix_mm = (--new A().f); var v_postfix_pp = (new A().f++); var v_postfix_mm = (new A().f--); -''', - [error(diag.notInitializedNonNullableInstanceField, 16, 1)], - ); +'''); } test_infer_assignToProperty_custom() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { A operator +(other) => this; A operator -(other) => this; } class B { A a; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'a' must be initialized. } var v_prefix_pp = (++new B().a); var v_prefix_mm = (--new B().a); var v_postfix_pp = (new B().a++); var v_postfix_mm = (new B().a--); -''', - [error(diag.notInitializedNonNullableInstanceField, 88, 1)], - ); +'''); } test_infer_assignToRef() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class A { int f; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'f' must be initialized. } A a = new A(); var b = (a.f = 1); var c = 0; var d = (c = 1); -''', - [error(diag.notInitializedNonNullableInstanceField, 16, 1)], - ); +'''); } test_infer_binary_custom() async { @@ -3042,16 +3200,17 @@ var v_negate = -a; } test_infer_throw() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' var t = true; var a = (throw 0); var b = (throw 0) ? 1 : 2; +// ^ +// [diag.deadCode] Dead code. +// ^ +// [diag.deadCode] Dead code. var c = t ? (throw 1) : 2; var d = t ? 1 : (throw 2); -''', - [error(diag.deadCode, 53, 1), error(diag.deadCode, 57, 1)], - ); +'''); } test_infer_typeCast() async { @@ -3114,67 +3273,75 @@ const a1 = m2; const a2 = b1; '''); - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart'; const m1 = a1; const m2 = a2; foo() { int i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. i = m1; } -''', - [error(diag.unusedLocalVariable, 62, 1)], - ); +'''); } test_inferCorrectlyOnMultipleVariablesDeclaredTogether() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { var x, y = 2, z = "hi"; } class B implements A { var x = 2, y = 3, z, w = 2; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'z' must be initialized. } foo() { String s; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 's' isn't used. int i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. s = new B().x; s = new B().y; +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. s = new B().z; s = new B().w; +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. i = new B().x; i = new B().y; i = new B().z; +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. i = new B().w; } -''', - [ - error(diag.notInitializedNonNullableInstanceField, 82, 1), - error(diag.unusedLocalVariable, 112, 1), - error(diag.unusedLocalVariable, 121, 1), - error(diag.invalidAssignment, 148, 9), - error(diag.invalidAssignment, 182, 9), - error(diag.invalidAssignment, 234, 9), - ], - ); +'''); } test_inferFromComplexExpressionsIfOuterMostValueIsPrecise() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { int x; B operator+(other) => null; } +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'x' must be initialized. +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method '+' because it has a return type of 'B'. class B extends A { B(ignore); } var a = new A(); // Note: it doesn't matter that some of these refer to 'x'. var b = new B(x); // allocations +// ^ +// [diag.undefinedIdentifier] Undefined name 'x'. var c1 = [x]; // list literals +// ^ +// [diag.undefinedIdentifier] Undefined name 'x'. var c2 = const []; var d = {'a': 'b'}; // map literals var e = new A()..x = 3; // cascades @@ -3184,58 +3351,63 @@ var f = 2 + 3; // binary expressions are OK if the left operand var g = -3; var h = new A() + 3; var i = - new A(); +// ^ +// [diag.undefinedOperator] The operator 'unary-' isn't defined for the type 'A'. var j = null as B; +// ^^^^^^^^^ +// [diag.castFromNullAlwaysFails] This cast always throws an exception because the expression always evaluates to 'null'. test1() { a = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'A'. a = new B(3); b = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'B'. b = new B(3); c1 = []; c1 = {}; +// ^^ +// [diag.invalidAssignment] A value of type 'Set' can't be assigned to a variable of type 'List'. c2 = []; c2 = {}; +// ^^ +// [diag.invalidAssignment] A value of type 'Set' can't be assigned to a variable of type 'List'. d = {}; d = 3; +// ^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'Map'. e = new A(); e = {}; +// ^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'A'. f = 3; f = false; +// ^^^^^ +// [diag.invalidAssignment] A value of type 'bool' can't be assigned to a variable of type 'int'. g = 1; g = false; +// ^^^^^ +// [diag.invalidAssignment] A value of type 'bool' can't be assigned to a variable of type 'int'. h = false; +// ^^^^^ +// [diag.invalidAssignment] A value of type 'bool' can't be assigned to a variable of type 'B'. h = new B('b'); i = false; j = new B('b'); j = false; +// ^^^^^ +// [diag.invalidAssignment] A value of type 'bool' can't be assigned to a variable of type 'B'. j = []; +// ^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'B'. } -''', - [ - error(diag.notInitializedNonNullableInstanceField, 14, 1), - error(diag.returnOfInvalidTypeFromMethod, 39, 4), - error(diag.undefinedIdentifier, 171, 1), - error(diag.undefinedIdentifier, 201, 1), - error(diag.undefinedOperator, 572, 1), - error(diag.castFromNullAlwaysFails, 591, 9), - error(diag.invalidAssignment, 619, 4), - error(diag.invalidAssignment, 647, 4), - error(diag.invalidAssignment, 687, 2), - error(diag.invalidAssignment, 709, 2), - error(diag.invalidAssignment, 729, 1), - error(diag.invalidAssignment, 753, 2), - error(diag.invalidAssignment, 772, 5), - error(diag.invalidAssignment, 794, 5), - error(diag.invalidAssignment, 807, 5), - error(diag.invalidAssignment, 869, 5), - error(diag.invalidAssignment, 882, 2), - ], - ); +'''); } test_inferFromRhsOnlyIfItWontConflictWithOverriddenFields() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { var x; } @@ -3246,19 +3418,17 @@ class B implements A { foo() { String y = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. int z = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 78, 1), - error(diag.unusedLocalVariable, 99, 1), - ], - ); +'''); } test_inferFromRhsOnlyIfItWontConflictWithOverriddenFields2() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { final x = null; } @@ -3269,14 +3439,13 @@ class B implements A { foo() { String y = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. int z = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 89, 1), - error(diag.unusedLocalVariable, 110, 1), - ], - ); +'''); } test_inferFromVariablesInCycleLibsWhenFlagIsOn() async { @@ -3322,21 +3491,19 @@ test1() { var x = 2; '''); - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart'; var y = x; test1() { x = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. y = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.invalidAssignment, 45, 4), - error(diag.invalidAssignment, 57, 4), - ], - ); +'''); } test_inferFromVariablesInNonCycleImportsWithFlag2() async { @@ -3344,112 +3511,109 @@ test1() { class A { static var x = 2; } '''); - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart'; class B { static var y = A.x; } test1() { A.x = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. B.y = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.invalidAssignment, 68, 4), - error(diag.invalidAssignment, 82, 4), - ], - ); +'''); } test_inferGenericMethodType_named() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T m(int a, {String b, T c}) => null; +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'b' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'c' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'm' because it has a return type of 'T'. } main() { var y = new C().m(1, b: 'bbb', c: 2.0); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [ - error(diag.missingDefaultValueForParameter, 34, 1), - error(diag.missingDefaultValueForParameter, 39, 1), - error(diag.returnOfInvalidTypeFromMethod, 46, 4), - error(diag.unusedLocalVariable, 68, 1), - ], - ); +'''); var y = findElement2.localVar('y'); _assertTypeStr(y.type, 'double'); } test_inferGenericMethodType_positional() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T m(int a, [T b]) => null; +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'b' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'm' because it has a return type of 'T'. } main() { var y = new C().m(1, 2.0); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [ - error(diag.missingDefaultValueForParameterPositional, 29, 1), - error(diag.returnOfInvalidTypeFromMethod, 36, 4), - error(diag.unusedLocalVariable, 59, 1), - ], - ); +'''); var y = findElement2.localVar('y'); _assertTypeStr(y.type, 'double'); } test_inferGenericMethodType_positional2() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T m(int a, [String b, T c]) => null; +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'b' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'c' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'm' because it has a return type of 'T'. } main() { var y = new C().m(1, 'bbb', 2.0); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [ - error(diag.missingDefaultValueForParameterPositional, 34, 1), - error(diag.missingDefaultValueForParameterPositional, 39, 1), - error(diag.returnOfInvalidTypeFromMethod, 46, 4), - error(diag.unusedLocalVariable, 69, 1), - ], - ); +'''); var y = findElement2.localVar('y'); _assertTypeStr(y.type, 'double'); } test_inferGenericMethodType_required() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T m(T x) => x; } main() { var y = new C().m(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -''', - [error(diag.unusedLocalVariable, 47, 1)], - ); +'''); var y = findElement2.localVar('y'); _assertTypeStr(y.type, 'int'); } test_inferListLiteralNestedInMapLiteral() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' class Resource {} class Folder extends Resource {} Resource getResource(String str) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'getResource' because it has a return type of 'Resource'. class Foo { Foo(T t); @@ -3458,71 +3622,77 @@ class Foo { main() { // List inside map var map = >{ +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'map' isn't used. 'pkgA': [getResource('/pkgA/lib/')], +// ^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'Resource' can't be assigned to the list type 'Folder'. 'pkgB': [getResource('/pkgB/lib/')] +// ^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'Resource' can't be assigned to the list type 'Folder'. }; // Also try map inside list var list = >[ +// ^^^^ +// [diag.unusedLocalVariable] The value of the local variable 'list' isn't used. { 'pkgA': getResource('/pkgA/lib/') }, +// ^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.mapValueTypeNotAssignable] The element type 'Resource' can't be assigned to the map value type 'Folder'. { 'pkgB': getResource('/pkgB/lib/') }, +// ^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.mapValueTypeNotAssignable] The element type 'Resource' can't be assigned to the map value type 'Folder'. ]; // Instance creation too var foo = new Foo>( +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. [getResource('/pkgA/lib/')] +// ^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.listElementTypeNotAssignable] The element type 'Resource' can't be assigned to the list type 'Folder'. ); } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 88, 4), - error(diag.unusedLocalVariable, 161, 3), - error(diag.listElementTypeNotAssignable, 204, 25), - error(diag.listElementTypeNotAssignable, 245, 25), - error(diag.unusedLocalVariable, 313, 4), - error(diag.mapValueTypeNotAssignable, 357, 25), - error(diag.mapValueTypeNotAssignable, 400, 25), - error(diag.unusedLocalVariable, 467, 3), - error(diag.listElementTypeNotAssignable, 501, 25), - ], - ); +'''); } test_inferLocalFunctionReturnType() async { // Regression test for https://github.com/dart-lang/sdk/issues/26414 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { f0 () => 42; +//^^ +// [diag.unusedElement] The declaration 'f0' isn't referenced. f1 () async => 42; +//^^ +// [diag.unusedElement] The declaration 'f1' isn't referenced. f2 () { return 42; } +//^^ +// [diag.unusedElement] The declaration 'f2' isn't referenced. f3 () async { return 42; } +//^^ +// [diag.unusedElement] The declaration 'f3' isn't referenced. f4 () sync* { yield 42; } +//^^ +// [diag.unusedElement] The declaration 'f4' isn't referenced. f5 () async* { yield 42; } num f6() => 42; +// ^^ +// [diag.unusedElement] The declaration 'f6' isn't referenced. f7 () => f7(); +//^^ +// [diag.unusedElement] The declaration 'f7' isn't referenced. f8 () => f9(); +//^^ +// [diag.unusedElement] The declaration 'f8' isn't referenced. +// ^^ +// [diag.referencedBeforeDeclaration][context 1] Local variable 'f9' can't be referenced before it is declared. f9 () => f5(); +//^^ +// [context 1] The declaration of 'f9' is here. } -''', - [ - error(diag.unusedElement, 11, 2), - error(diag.unusedElement, 26, 2), - error(diag.unusedElement, 48, 2), - error(diag.unusedElement, 71, 2), - error(diag.unusedElement, 100, 2), - error(diag.unusedElement, 162, 2), - error(diag.unusedElement, 177, 2), - error(diag.unusedElement, 194, 2), - error( - diag.referencedBeforeDeclaration, - 203, - 2, - contextMessages: [message(testFile, 211, 2)], - ), - ], - ); +'''); void assertLocalFunctionType(String name, String expected) { var type = findElement2.localFunction(name).type; @@ -3546,17 +3716,16 @@ main() { } test_inferParameterType_setter_fromField() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C extends D { set foo(x) {} } class D { int foo; +// ^^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'foo' must be initialized. } -''', - [error(diag.notInitializedNonNullableInstanceField, 54, 3)], - ); +'''); var f = _resultLibraryElement.getClass('C')!.setters[0]; _assertTypeStr(f.type, 'void Function(int)'); } @@ -3608,99 +3777,91 @@ class C { } test_inferredInitializingFormalChecksDefaultValue() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class Foo { var x = 1; Foo([this.x = "1"]); +// ^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [error(diag.invalidAssignment, 41, 3)], - ); +'''); } test_inferredType_blockClosure_noArgs_noReturn() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var f = () {}; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'f' isn't used. } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var f = findElement2.localVar('f'); _assertTypeStr(f.type, 'Null Function()'); } test_inferredType_cascade() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { int a; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'a' must be initialized. List b; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'b' must be initialized. void m() {} } var v = new A()..a = 1..b.add(2)..m(); -''', - [ - error(diag.notInitializedNonNullableInstanceField, 16, 1), - error(diag.notInitializedNonNullableInstanceField, 31, 1), - ], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'A'); } test_inferredType_customBinaryOp() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { bool operator*(C other) => true; } C c; +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. var x = c*c; -''', - [error(diag.notInitializedNonNullableVariable, 49, 1)], - ); +'''); var x = _resultLibraryElement.topLevelVariables[1]; expect(x.name, 'x'); _assertTypeStr(x.type, 'bool'); } test_inferredType_customBinaryOp_viaInterface() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class I { bool operator*(C other) => true; } abstract class C implements I {} C c; +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. var x = c*c; -''', - [error(diag.notInitializedNonNullableVariable, 82, 1)], - ); +'''); var x = _resultLibraryElement.topLevelVariables[1]; expect(x.name, 'x'); _assertTypeStr(x.type, 'bool'); } test_inferredType_customIndexOp() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { bool operator[](int index) => true; } main() { C c; var x = c[0]; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'c' must be assigned before it can be used. } -''', - [ - error(diag.unusedLocalVariable, 72, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 76, 1), - ], - ); +'''); var x = findElement2.localVar('x'); expect(x.name, 'x'); @@ -3708,8 +3869,7 @@ main() { } test_inferredType_customIndexOp_viaInterface() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class I { bool operator[](int index) => true; } @@ -3717,13 +3877,12 @@ abstract class C implements I {} main() { C c; var x = c[0]; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'c' must be assigned before it can be used. } -''', - [ - error(diag.unusedLocalVariable, 105, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 109, 1), - ], - ); +'''); var x = findElement2.localVar('x'); expect(x.name, 'x'); @@ -3731,66 +3890,62 @@ main() { } test_inferredType_customUnaryOp() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { bool operator-() => true; } C c; +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. var x = -c; -''', - [error(diag.notInitializedNonNullableVariable, 42, 1)], - ); +'''); var x = _resultLibraryElement.topLevelVariables[1]; expect(x.name, 'x'); _assertTypeStr(x.type, 'bool'); } test_inferredType_customUnaryOp_viaInterface() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class I { bool operator-() => true; } abstract class C implements I {} C c; +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. var x = -c; -''', - [error(diag.notInitializedNonNullableVariable, 75, 1)], - ); +'''); var x = _resultLibraryElement.topLevelVariables[1]; expect(x.name, 'x'); _assertTypeStr(x.type, 'bool'); } test_inferredType_extractMethodTearOff() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { bool g() => true; } C f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'C'. var x = f().g; -''', - [error(diag.returnOfInvalidTypeFromFunction, 41, 4)], - ); +'''); var x = _resultLibraryElement.topLevelVariables[0]; expect(x.name, 'x'); _assertTypeStr(x.type, 'bool Function()'); } test_inferredType_extractMethodTearOff_viaInterface() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class I { bool g() => true; } abstract class C implements I {} C f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'C'. var x = f().g; -''', - [error(diag.returnOfInvalidTypeFromFunction, 74, 4)], - ); +'''); var x = _resultLibraryElement.topLevelVariables[0]; expect(x.name, 'x'); _assertTypeStr(x.type, 'bool Function()'); @@ -3805,33 +3960,31 @@ var v = print; } test_inferredType_invokeMethod() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { bool g() => true; } C f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'C'. var x = f().g(); -''', - [error(diag.returnOfInvalidTypeFromFunction, 41, 4)], - ); +'''); var x = _resultLibraryElement.topLevelVariables[0]; expect(x.name, 'x'); _assertTypeStr(x.type, 'bool'); } test_inferredType_invokeMethod_viaInterface() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class I { bool g() => true; } abstract class C implements I {} C f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'C'. var x = f().g(); -''', - [error(diag.returnOfInvalidTypeFromFunction, 74, 4)], - ); +'''); var x = _resultLibraryElement.topLevelVariables[0]; expect(x.name, 'x'); _assertTypeStr(x.type, 'bool'); @@ -3874,85 +4027,79 @@ final x = >{}; } test_inferredType_usesSyntheticFunctionType() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' int f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'int'. String g() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'g' because it has a return type of 'String'. var v = [f, g]; -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 11, 4), - error(diag.returnOfInvalidTypeFromFunction, 31, 4), - ], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'List'); } test_inferredType_usesSyntheticFunctionType_functionTypedParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' int f(int x(String y)) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'int'. String g(int x(String y)) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'g' because it has a return type of 'String'. var v = [f, g]; -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 26, 4), - error(diag.returnOfInvalidTypeFromFunction, 61, 4), - ], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'List'); } test_inferredType_usesSyntheticFunctionType_namedParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' int f({int x}) => null; +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'x' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'int'. String g({int x}) => null; +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'x' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'g' because it has a return type of 'String'. var v = [f, g]; -''', - [ - error(diag.missingDefaultValueForParameter, 11, 1), - error(diag.returnOfInvalidTypeFromFunction, 18, 4), - error(diag.missingDefaultValueForParameter, 38, 1), - error(diag.returnOfInvalidTypeFromFunction, 45, 4), - ], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'List'); } test_inferredType_usesSyntheticFunctionType_positionalParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' int f([int x]) => null; +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'x' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'int'. String g([int x]) => null; +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'x' can't have a value of 'null' because of its type, but the implicit default value is 'null'. +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'g' because it has a return type of 'String'. var v = [f, g]; -''', - [ - error(diag.missingDefaultValueForParameterPositional, 11, 1), - error(diag.returnOfInvalidTypeFromFunction, 18, 4), - error(diag.missingDefaultValueForParameterPositional, 38, 1), - error(diag.returnOfInvalidTypeFromFunction, 45, 4), - ], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'List'); } test_inferredType_usesSyntheticFunctionType_requiredParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' int f(int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'int'. String g(int x) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'g' because it has a return type of 'String'. var v = [f, g]; -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 16, 4), - error(diag.returnOfInvalidTypeFromFunction, 41, 4), - ], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'List'); } @@ -4036,8 +4183,7 @@ foo() { } test_inferStaticsTransitively2() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const x1 = 1; final x2 = 1; final y1 = x1; @@ -4045,12 +4191,12 @@ final y2 = x2; foo() { int i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'i' isn't used. i = y1; i = y2; } -''', - [error(diag.unusedLocalVariable, 73, 1)], - ); +'''); } test_inferStaticsTransitively3() async { @@ -4088,22 +4234,20 @@ foo() { m3(String a, String b, [a1,a2]) {} '''); - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart'; class T { static final T foo = m1(m2(m3('', ''))); static T m1(String m) { return null; } +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'm1' because it has a return type of 'T'. static String m2(e) { return ''; } } -''', - [error(diag.returnOfInvalidTypeFromMethod, 103, 4)], - ); +'''); } test_inferTypeOnOverriddenFields2() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { int x = 2; } @@ -4114,20 +4258,19 @@ class B extends A { foo() { String y = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. int z = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 80, 1), - error(diag.invalidAssignment, 84, 9), - error(diag.unusedLocalVariable, 101, 1), - ], - ); +'''); } test_inferTypeOnOverriddenFields4() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { final int x = 2; } @@ -4138,111 +4281,112 @@ class B implements A { foo() { String y = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. int z = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.unusedLocalVariable, 89, 1), - error(diag.invalidAssignment, 93, 9), - error(diag.unusedLocalVariable, 110, 1), - ], - ); +'''); } test_inferTypeOnVar() async { // Error also expected when declared type is `int`. - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' test1() { int x = 3; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. x = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.unusedLocalVariable, 16, 1), - error(diag.invalidAssignment, 29, 4), - ], - ); +'''); } test_inferTypeOnVar2() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' test2() { var x = 3; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. x = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.unusedLocalVariable, 16, 1), - error(diag.invalidAssignment, 29, 4), - ], - ); +'''); } test_inferTypeOnVarFromField() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { int x = 0; test1() { var a = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. a = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. a = 3; var b = y; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. b = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. b = 4; var c = z; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'c' isn't used. c = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. c = 4; } int y; // field def after use +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'y' must be initialized. final z = 42; // should infer `int` } -''', - [ - error(diag.unusedLocalVariable, 44, 1), - error(diag.invalidAssignment, 59, 4), - error(diag.unusedLocalVariable, 84, 1), - error(diag.invalidAssignment, 99, 4), - error(diag.unusedLocalVariable, 124, 1), - error(diag.invalidAssignment, 139, 4), - error(diag.notInitializedNonNullableInstanceField, 167, 1), - ], - ); +'''); } test_inferTypeOnVarFromTopLevel() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' int x = 0; test1() { var a = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. a = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. a = 3; var b = y; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. b = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. b = 4; var c = z; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'c' isn't used. c = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. c = 4; } int y = 0; // field def after use final z = 42; // should infer `int` -''', - [ - error(diag.unusedLocalVariable, 28, 1), - error(diag.invalidAssignment, 41, 4), - error(diag.unusedLocalVariable, 62, 1), - error(diag.invalidAssignment, 75, 4), - error(diag.unusedLocalVariable, 96, 1), - error(diag.invalidAssignment, 109, 4), - ], - ); +'''); } test_inferTypeRegardlessOfDeclarationOrderOrCycles() async { @@ -4252,88 +4396,90 @@ import 'test.dart'; class B extends A { } '''); - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart'; class C extends B { get x => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'x' because it has a return type of 'int'. } class A { int get x => 0; } foo() { int y = new C().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. String z = new C().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. } -''', - [ - error(diag.returnOfInvalidTypeFromFunction, 48, 4), - error(diag.unusedLocalVariable, 100, 1), - error(diag.unusedLocalVariable, 124, 1), - error(diag.invalidAssignment, 128, 9), - ], - ); +'''); } test_inferTypesOnGenericInstantiations_3() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { final T x = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T'. final T w = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T'. } class B implements A { get x => 3; get w => "hello"; +// ^^^^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'String' can't be returned from the function 'w' because it has a return type of 'int'. } foo() { String y = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. int z = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.invalidAssignment, 27, 4), - error(diag.invalidAssignment, 47, 4), - error(diag.returnOfInvalidTypeFromFunction, 109, 7), - error(diag.unusedLocalVariable, 138, 1), - error(diag.invalidAssignment, 142, 9), - error(diag.unusedLocalVariable, 159, 1), - ], - ); +'''); } test_inferTypesOnGenericInstantiations_4() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { T x; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'x' must be initialized. } class B extends A { E y; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'y' must be initialized. get x => y; } foo() { int y = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. String z = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.notInitializedNonNullableInstanceField, 17, 1), - error(diag.notInitializedNonNullableInstanceField, 53, 1), - error(diag.unusedLocalVariable, 87, 1), - error(diag.invalidAssignment, 91, 17), - error(diag.unusedLocalVariable, 119, 1), - ], - ); +'''); } test_inferTypesOnGenericInstantiations_5() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' abstract class I { String m(a, String f(v, E e)); } @@ -4352,52 +4498,52 @@ class B extends A implements M { int get y => 0; m(a, f(v, E e)) { return null; } +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'm' because it has a return type of 'String'. } foo () { int y = new B().m(null, null); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'dynamic Function(dynamic, dynamic)'. String z = new B().m(null, null); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'dynamic Function(dynamic, dynamic)'. } -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 284, 4), - error(diag.unusedLocalVariable, 310, 1), - error(diag.invalidAssignment, 314, 21), - error(diag.argumentTypeNotAssignable, 330, 4), - error(diag.unusedLocalVariable, 346, 1), - error(diag.argumentTypeNotAssignable, 366, 4), - ], - ); +'''); } test_inferTypesOnGenericInstantiations_infer() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { final T x = null; +// ^ +// [context 1] The member being overridden. +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'T'. } class B implements A { dynamic get x => 3; +// ^ +// [diag.invalidOverride][context 1] 'B.x' ('dynamic Function()') isn't a valid override of 'A.x' ('int Function()'). } foo() { String y = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. int z = new B().x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.invalidAssignment, 27, 4), - error( - diag.invalidOverride, - 78, - 1, - contextMessages: [message(testFile, 23, 1)], - ), - error(diag.unusedLocalVariable, 106, 1), - error(diag.unusedLocalVariable, 127, 1), - ], - ); +'''); } test_inferTypesOnGenericInstantiationsInLibraryCycle() async { @@ -4409,14 +4555,15 @@ abstract class I { } '''); - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' import 'a.dart'; abstract class A implements I { const A(); final E value = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'E'. } abstract class M { @@ -4428,28 +4575,29 @@ class B extends A implements M { int get y => 0; m(a, f(v, int e)) { return null; } +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'm' because it has a return type of 'A'. } foo () { int y = new B().m(null, null).value; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'dynamic Function(dynamic, int)'. String z = new B().m(null, null).value; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'Null' can't be assigned to the parameter type 'dynamic Function(dynamic, int)'. } -''', - [ - error(diag.invalidAssignment, 88, 4), - error(diag.returnOfInvalidTypeFromMethod, 238, 4), - error(diag.unusedLocalVariable, 264, 1), - error(diag.invalidAssignment, 268, 35), - error(diag.argumentTypeNotAssignable, 292, 4), - error(diag.unusedLocalVariable, 314, 1), - error(diag.argumentTypeNotAssignable, 342, 4), - ], - ); +'''); } test_inferTypesOnLoopIndices_forEachLoop() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class Foo { int bar = 42; } @@ -4458,6 +4606,10 @@ class Bar> { void foo(T t) { for (var i in t) { int x = i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } } } @@ -4466,7 +4618,13 @@ class Baz, S extends E> { void foo(S t) { for (var i in t) { int x = i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'T' can't be assigned to a variable of type 'int'. T y = i; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } } } @@ -4475,76 +4633,78 @@ test() { var list = []; for (var x in list) { String y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'Foo' can't be assigned to a variable of type 'String'. } for (dynamic x in list) { String y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } for (String x in list) { +// ^^^^ +// [diag.forInOfInvalidElementType] The type 'List' used in the 'for' loop must implement 'Iterable' with a type argument that can be assigned to 'String'. String y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } var z; for(z in list) { String y = z; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } Iterable iter = list; for (Foo x in iter) { var y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } dynamic iter2 = list; for (Foo x in iter2) { var y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } var map = {}; // Error: map must be an Iterable. for (var x in map) { +// ^^^ +// [diag.forInOfInvalidType] The type 'Map' used in the 'for' loop must implement 'Iterable'. String y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } // We're not properly inferring that map.keys is an Iterable // and that x is a String. for (var x in map.keys) { String y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } } -''', - [ - error(diag.unusedLocalVariable, 122, 1), - error(diag.invalidAssignment, 126, 1), - error(diag.unusedLocalVariable, 244, 1), - error(diag.invalidAssignment, 248, 1), - error(diag.unusedLocalVariable, 259, 1), - error(diag.unusedLocalVariable, 345, 1), - error(diag.invalidAssignment, 349, 1), - error(diag.unusedLocalVariable, 396, 1), - error(diag.forInOfInvalidElementType, 427, 4), - error(diag.unusedLocalVariable, 446, 1), - error(diag.unusedLocalVariable, 497, 1), - error(diag.unusedLocalVariable, 565, 1), - error(diag.unusedLocalVariable, 634, 1), - error(diag.forInOfInvalidType, 728, 3), - error(diag.unusedLocalVariable, 746, 1), - error(diag.unusedLocalVariable, 897, 1), - ], - ); +'''); } test_inferTypesOnLoopIndices_forLoopWithInference() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' test() { for (var i = 0; i < 10; i++) { int j = i + 1; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'j' isn't used. } } -''', - [error(diag.unusedLocalVariable, 50, 1)], - ); +'''); } test_inferVariableVoid() async { @@ -4579,59 +4739,63 @@ void bar() { } test_listLiterals() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' test1() { var x = [1, 2, 3]; x.add('hi'); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. x.add(4.0); +// ^^^ +// [diag.argumentTypeNotAssignable] The argument type 'double' can't be assigned to the parameter type 'int'. x.add(4); List y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } test2() { var x = [1, 2.0, 3]; x.add('hi'); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. x.add(4.0); List y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. } -''', - [ - error(diag.argumentTypeNotAssignable, 39, 4), - error(diag.argumentTypeNotAssignable, 54, 3), - error(diag.unusedLocalVariable, 84, 1), - error(diag.argumentTypeNotAssignable, 134, 4), - error(diag.unusedLocalVariable, 167, 1), - error(diag.invalidAssignment, 171, 1), - ], - ); +'''); } test_listLiterals_topLevel() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' var x1 = [1, 2, 3]; test1() { x1.add('hi'); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. x1.add(4.0); +// ^^^ +// [diag.argumentTypeNotAssignable] The argument type 'double' can't be assigned to the parameter type 'int'. x1.add(4); List y = x1; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } var x2 = [1, 2.0, 3]; test2() { x2.add('hi'); +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. x2.add(4.0); List y = x2; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^ +// [diag.invalidAssignment] A value of type 'List' can't be assigned to a variable of type 'List'. } -''', - [ - error(diag.argumentTypeNotAssignable, 39, 4), - error(diag.argumentTypeNotAssignable, 55, 3), - error(diag.unusedLocalVariable, 86, 1), - error(diag.argumentTypeNotAssignable, 137, 4), - error(diag.unusedLocalVariable, 171, 1), - error(diag.invalidAssignment, 175, 2), - ], - ); +'''); } test_listLiteralsCanInferNull_topLevel() async { @@ -4643,107 +4807,114 @@ var x = [null]; } test_listLiteralsCanInferNullBottom() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' test1() { var x = [null]; x.add(42); +// ^^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'Null'. } -''', - [error(diag.argumentTypeNotAssignable, 36, 2)], - ); +'''); var x = findElement2.localVar('x'); _assertTypeStr(x.type, 'List'); } test_mapLiterals() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' test1() { var x = { 1: 'x', 2: 'y' }; x[3] = 'z'; x['hi'] = 'w'; +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. x[4.0] = 'u'; +// ^^^ +// [diag.argumentTypeNotAssignable] The argument type 'double' can't be assigned to the parameter type 'int'. x[3] = 42; +// ^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. Map y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } test2() { var x = { 1: 'x', 2: 'y', 3.0: new RegExp('.') }; x[3] = 'z'; x['hi'] = 'w'; +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. x[4.0] = 'u'; x[3] = 42; +// ^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'Pattern'. Pattern p = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'Pattern'. x[2] = p; Map y = x; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. } -''', - [ - error(diag.argumentTypeNotAssignable, 58, 4), - error(diag.argumentTypeNotAssignable, 75, 3), - error(diag.invalidAssignment, 96, 2), - error(diag.unusedLocalVariable, 119, 1), - error(diag.argumentTypeNotAssignable, 209, 4), - error(diag.invalidAssignment, 247, 2), - error(diag.invalidAssignment, 265, 4), - error(diag.unusedLocalVariable, 302, 1), - error(diag.invalidAssignment, 306, 1), - ], - ); +'''); } test_mapLiterals_topLevel() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' var x1 = { 1: 'x', 2: 'y' }; test1() { x1[3] = 'z'; x1['hi'] = 'w'; +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'int'. x1[4.0] = 'u'; +// ^^^ +// [diag.argumentTypeNotAssignable] The argument type 'double' can't be assigned to the parameter type 'int'. x1[3] = 42; +// ^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'String'. Map y = x1; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } var x2 = { 1: 'x', 2: 'y', 3.0: new RegExp('.') }; test2() { x2[3] = 'z'; x2['hi'] = 'w'; +// ^^^^ +// [diag.argumentTypeNotAssignable] The argument type 'String' can't be assigned to the parameter type 'num'. x2[4.0] = 'u'; x2[3] = 42; +// ^^ +// [diag.invalidAssignment] A value of type 'int' can't be assigned to a variable of type 'Pattern'. Pattern p = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'Pattern'. x2[2] = p; Map y = x2; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^ +// [diag.invalidAssignment] A value of type 'Map' can't be assigned to a variable of type 'Map'. } -''', - [ - error(diag.argumentTypeNotAssignable, 59, 4), - error(diag.argumentTypeNotAssignable, 77, 3), - error(diag.invalidAssignment, 99, 2), - error(diag.unusedLocalVariable, 122, 1), - error(diag.argumentTypeNotAssignable, 214, 4), - error(diag.invalidAssignment, 254, 2), - error(diag.invalidAssignment, 272, 4), - error(diag.unusedLocalVariable, 310, 1), - error(diag.invalidAssignment, 314, 2), - ], - ); +'''); } test_mapLiteralsCanInferNull() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' test1() { var x = { null: null }; x[3] = 'z'; +// ^ +// [diag.argumentTypeNotAssignable] The argument type 'int' can't be assigned to the parameter type 'Null'. +// ^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'Null'. } -''', - [ - error(diag.argumentTypeNotAssignable, 40, 1), - error(diag.invalidAssignment, 45, 3), - ], - ); +'''); var x = findElement2.localVar('x'); _assertTypeStr(x.type, 'Map'); @@ -4758,118 +4929,111 @@ var x = { null: null }; } test_methodCall_withTypeArguments_instanceMethod() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { D f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'D'. } class D {} var f = new C().f(); -''', - [error(diag.returnOfInvalidTypeFromMethod, 27, 4)], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'D'); } test_methodCall_withTypeArguments_instanceMethod_identifierSequence() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { D f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'D'. } class D {} C c; +//^ +// [diag.notInitializedNonNullableVariable] The non-nullable variable 'c' must be initialized. var f = c.f(); -''', - [ - error(diag.returnOfInvalidTypeFromMethod, 27, 4), - error(diag.notInitializedNonNullableVariable, 51, 1), - ], - ); +'''); var v = _resultLibraryElement.topLevelVariables[1]; expect(v.name, 'f'); _assertTypeStr(v.type, 'D'); } test_methodCall_withTypeArguments_staticMethod() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { static D f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'D'. } class D {} var f = C.f(); -''', - [error(diag.returnOfInvalidTypeFromMethod, 34, 4)], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'D'); } test_methodCall_withTypeArguments_topLevelFunction() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' D f() => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromFunction] A value of type 'Null' can't be returned from the function 'f' because it has a return type of 'D'. class D {} var g = f(); -''', - [error(diag.returnOfInvalidTypeFromFunction, 15, 4)], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'D'); } test_noErrorWhenDeclaredTypeIsNumAndAssignedNull() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' test1() { num x = 3; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. x = null; +// ^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'num'. } -''', - [ - error(diag.unusedLocalVariable, 16, 1), - error(diag.invalidAssignment, 29, 4), - ], - ); +'''); } test_nullCoalescingOperator() async { // Regression test for https://github.com/dart-lang/sdk/issues/26552 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { List x; var y = x ?? []; +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'x' must be assigned before it can be used. +// ^^^^^ +// [diag.deadCode] Dead code. +// ^^ +// [diag.deadNullAwareExpression] The left operand can't be null, so the right operand is never executed. List z = y; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'z' isn't used. } -''', - [ - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 34, 1), - error(diag.deadCode, 36, 5), - error(diag.deadNullAwareExpression, 39, 2), - error(diag.unusedLocalVariable, 55, 1), - ], - ); +'''); } test_nullCoalescingOperator2() async { // Don't do anything if we already have a context type. - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' main() { List x; List y = x ?? []; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'x' must be assigned before it can be used. +// ^^^^^ +// [diag.deadCode] Dead code. +// ^^ +// [diag.deadNullAwareExpression] The left operand can't be null, so the right operand is never executed. } -''', - [ - error(diag.unusedLocalVariable, 36, 1), - error(diag.notAssignedPotentiallyNonNullableLocalVariable, 40, 1), - error(diag.deadCode, 42, 5), - error(diag.deadNullAwareExpression, 45, 2), - ], - ); +'''); var y = findElement2.localVar('y'); _assertTypeStr(y.type, 'List'); @@ -4877,16 +5041,21 @@ main() { test_nullLiteralShouldNotInferAsBottom() async { // Regression test for https://github.com/dart-lang/dev_compiler/issues/47 - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' var h = null; void foo(int f(Object _)) {} main() { var f = (Object x) => null; String y = f(42); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. +// ^^^^^ +// [diag.invalidAssignment] A value of type 'Null' can't be assigned to a variable of type 'String'. f = (x) => 'hello'; +// ^^^^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'String' isn't returnable from a 'Null' function, as required by the closure's context. var g = null; g = 'hello'; @@ -4896,21 +5065,15 @@ main() { (h.foo()); foo((x) => null); +// ^^^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'Null' isn't returnable from a 'int' function, as required by the closure's context. foo((x) => throw "not implemented"); } -''', - [ - error(diag.unusedLocalVariable, 92, 1), - error(diag.invalidAssignment, 96, 5), - error(diag.returnOfInvalidTypeFromClosure, 117, 7), - error(diag.returnOfInvalidTypeFromClosure, 214, 4), - ], - ); +'''); } test_propagateInferenceToFieldInClass() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { int x = 2; } @@ -4918,17 +5081,16 @@ class A { test() { var a = new A(); A b = a; // doesn't require down cast +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. print(a.x); // doesn't require dynamic invoke print(a.x + 2); // ok to use in bigger expression } -''', - [error(diag.unusedLocalVariable, 58, 1)], - ); +'''); } test_propagateInferenceToFieldInClassDynamicWarnings() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { int x = 2; } @@ -4936,17 +5098,16 @@ class A { test() { dynamic a = new A(); A b = a; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'b' isn't used. print(a.x); print((a.x) + 2); } -''', - [error(diag.unusedLocalVariable, 62, 1)], - ); +'''); } test_propagateInferenceTransitively() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class A { int x = 2; } @@ -4954,16 +5115,15 @@ class A { test5() { var a1 = new A(); a1.x = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. A a2 = new A(); a2.x = "hi"; +// ^^^^ +// [diag.invalidAssignment] A value of type 'String' can't be assigned to a variable of type 'int'. } -''', - [ - error(diag.invalidAssignment, 65, 4), - error(diag.invalidAssignment, 99, 4), - ], - ); +'''); } test_propagateInferenceTransitively2() async { @@ -5004,121 +5164,132 @@ final x = F; } test_refineBinaryExpressionType_typeParameter_T_double() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T a; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'a' must be initialized. void op(double b) { double r1 = a + b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r1' isn't used. double r2 = a - b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r2' isn't used. double r3 = a * b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r3' isn't used. double r4 = a / b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r4' isn't used. } } -''', - [ - error(diag.notInitializedNonNullableInstanceField, 29, 1), - error(diag.unusedLocalVariable, 66, 2), - error(diag.unusedLocalVariable, 89, 2), - error(diag.unusedLocalVariable, 112, 2), - error(diag.unusedLocalVariable, 135, 2), - ], - ); +'''); } test_refineBinaryExpressionType_typeParameter_T_int() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T a; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'a' must be initialized. void op(int b) { T r1 = a + b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r1' isn't used. +// ^^^^^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. T r2 = a - b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r2' isn't used. +// ^^^^^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. T r3 = a * b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r3' isn't used. +// ^^^^^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. } void opEq(int b) { a += b; +// ^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. a -= b; +// ^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. a *= b; +// ^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. } } -''', - [ - error(diag.notInitializedNonNullableInstanceField, 29, 1), - error(diag.unusedLocalVariable, 58, 2), - error(diag.invalidAssignment, 63, 5), - error(diag.unusedLocalVariable, 76, 2), - error(diag.invalidAssignment, 81, 5), - error(diag.unusedLocalVariable, 94, 2), - error(diag.invalidAssignment, 99, 5), - error(diag.invalidAssignment, 141, 1), - error(diag.invalidAssignment, 153, 1), - error(diag.invalidAssignment, 165, 1), - ], - ); +'''); } test_refineBinaryExpressionType_typeParameter_T_T() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { T a; +// ^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'a' must be initialized. void op(T b) { T r1 = a + b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r1' isn't used. +// ^^^^^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. T r2 = a - b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r2' isn't used. +// ^^^^^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. T r3 = a * b; +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'r3' isn't used. +// ^^^^^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. } void opEq(T b) { a += b; +// ^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. a -= b; +// ^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. a *= b; +// ^ +// [diag.invalidAssignment] A value of type 'num' can't be assigned to a variable of type 'T'. } } -''', - [ - error(diag.notInitializedNonNullableInstanceField, 29, 1), - error(diag.unusedLocalVariable, 56, 2), - error(diag.invalidAssignment, 61, 5), - error(diag.unusedLocalVariable, 74, 2), - error(diag.invalidAssignment, 79, 5), - error(diag.unusedLocalVariable, 92, 2), - error(diag.invalidAssignment, 97, 5), - error(diag.invalidAssignment, 137, 1), - error(diag.invalidAssignment, 149, 1), - error(diag.invalidAssignment, 161, 1), - ], - ); +'''); } test_staticMethod_tearoff() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' const v = C.f; class C { static int f(String s) => null; +// ^^^^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'Null' can't be returned from the method 'f' because it has a return type of 'int'. } -''', - [error(diag.returnOfInvalidTypeFromMethod, 53, 4)], - ); +'''); var v = _resultLibraryElement.topLevelVariables[0]; _assertTypeStr(v.type, 'int Function(String)'); } test_unsafeBlockClosureInference_closureCall() async { // Regression test for https://github.com/dart-lang/sdk/issues/26962 - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = ((x) => 1.0)(() { return 1; }); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); expect(v.name, 'v'); @@ -5150,20 +5321,19 @@ var v = new C(() { return 1; }); } test_unsafeBlockClosureInference_constructorCall_implicitTypeParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { C(T x()); } main() { var v = new C( +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. () { return 1; }); } -''', - [error(diag.unusedLocalVariable, 42, 1)], - ); +'''); var v = findElement2.localVar('v'); expect(v.name, 'v'); @@ -5249,212 +5419,199 @@ var v = (f)(() { return 1; }); } test_unsafeBlockClosureInference_functionCall_implicitTypeParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = f( +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. () { return 1; }); } List f(T g()) => [g()]; -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List'); } test_unsafeBlockClosureInference_functionCall_implicitTypeParam_viaExpr() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = (f)( +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. () { return 1; }); } List f(T g()) => [g()]; -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List'); } test_unsafeBlockClosureInference_functionCall_noTypeParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = f(() { return 1; }); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } double f(x) => 1.0; -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'double'); } test_unsafeBlockClosureInference_functionCall_noTypeParam_viaExpr() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = (f)(() { return 1; }); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } double f(x) => 1.0; -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'double'); } test_unsafeBlockClosureInference_inList_dynamic() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = [() { return 1; }]; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List'); } test_unsafeBlockClosureInference_inList_typed() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' typedef int F(); main() { var v = [() { return 1; }]; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 32, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List'); } test_unsafeBlockClosureInference_inList_untyped() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = [ +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. () { return 1; }]; } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List'); } test_unsafeBlockClosureInference_inMap_dynamic() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = {1: () { return 1; }}; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'Map'); } test_unsafeBlockClosureInference_inMap_typed() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' typedef int F(); main() { var v = {1: () { return 1; }}; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 32, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'Map'); } test_unsafeBlockClosureInference_inMap_untyped() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' main() { var v = { +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. 1: () { return 1; }}; } -''', - [error(diag.unusedLocalVariable, 15, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'Map'); } test_unsafeBlockClosureInference_methodCall_explicitDynamicParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { List f(T g()) => [g()]; } main() { var v = new C().f(() { return 1; }); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 62, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List'); } test_unsafeBlockClosureInference_methodCall_explicitTypeParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { List f(T g()) => [g()]; } main() { var v = new C().f(() { return 1; }); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. } -''', - [error(diag.unusedLocalVariable, 62, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List'); } test_unsafeBlockClosureInference_methodCall_implicitTypeParam() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(r''' class C { List f(T g()) => [g()]; } main() { var v = new C().f( +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'v' isn't used. () { return 1; }); } -''', - [error(diag.unusedLocalVariable, 62, 1)], - ); +'''); var v = findElement2.localVar('v'); _assertTypeStr(v.type, 'List'); @@ -5473,8 +5630,7 @@ var v = new C().f(() { return 1; }); } test_voidReturnTypeEquivalentToDynamic() async { - await assertErrorsInCode( - r''' + await resolveTestCodeWithDiagnostics(r''' T run(T f()) { print("running"); var t = f(); @@ -5490,18 +5646,17 @@ var y = run(printRunning); main() { void printRunning() { print("running"); } var x = run(printRunning); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. var y = run(printRunning); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. x = 123; x = 'hi'; y = 123; y = 'hi'; } - ''', - [ - error(diag.unusedLocalVariable, 259, 1), - error(diag.unusedLocalVariable, 297, 1), - ], - ); +'''); var x = _resultLibraryElement.topLevelVariables[0]; var y = _resultLibraryElement.topLevelVariables[1]; diff --git a/pkg/analyzer/test/src/wolf/ir/ast_to_ir_test.dart b/pkg/analyzer/test/src/wolf/ir/ast_to_ir_test.dart index c22a704972b..ba4d5d12d25 100644 --- a/pkg/analyzer/test/src/wolf/ir/ast_to_ir_test.dart +++ b/pkg/analyzer/test/src/wolf/ir/ast_to_ir_test.dart @@ -4,7 +4,6 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/element/element.dart'; -import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag; import 'package:analyzer/src/wolf/ir/ast_to_ir.dart'; import 'package:analyzer/src/wolf/ir/call_descriptor.dart'; import 'package:analyzer/src/wolf/ir/coded_ir.dart'; @@ -17,11 +16,13 @@ import 'package:test/test.dart' show fail; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../../dart/resolution/context_collection_resolution.dart'; +import '../../dart/resolution/node_text_expectations.dart'; import 'utils.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(AstToIRTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @@ -1781,15 +1782,14 @@ test() { } test_variableDeclarationList_singleVariable_uninitialized_unsound() async { - await assertErrorsInCode( - ''' + await resolveTestCodeWithDiagnostics(''' test() { int i; return i; // UNSOUND +// ^ +// [diag.notAssignedPotentiallyNonNullableLocalVariable] The non-nullable local variable 'i' must be assigned before it can be used. } -''', - [error(diag.notAssignedPotentiallyNonNullableLocalVariable, 27, 1)], - ); +'''); analyze(findNode.singleFunctionDeclaration); check(astNodes)[findNode.variableDeclarationList('int i')].not( (s) => s.instructions.any((s) => s.opcode.equals(Opcode.writeLocal)),