diff --git a/pkg/analyzer/test/generated/strong_mode_test.dart b/pkg/analyzer/test/generated/strong_mode_test.dart index f54f907efe9..323ccac5223 100644 --- a/pkg/analyzer/test/generated/strong_mode_test.dart +++ b/pkg/analyzer/test/generated/strong_mode_test.dart @@ -78,7 +78,7 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_async_method_propagation() async { - String code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { Future f0() => new Future.value(3); Future f1() async => new Future.value(3); @@ -96,8 +96,7 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { Future g4() async { return new Future.value(3); } Future g5() async { return await new Future.value(3); } } - '''; - var result = await resolveTestCode(code); + '''); void check(String name, Asserter typeTest) { MethodDeclaration test = AstFinder.getMethodInClass( @@ -139,7 +138,7 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_async_propagation() async { - String code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' Future f0() => new Future.value(3); Future f1() async => new Future.value(3); Future f2() async => await new Future.value(3); @@ -155,8 +154,7 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { Future g3() { return new Future.value(3); } Future g4() async { return new Future.value(3); } Future g5() async { return await new Future.value(3); } - '''; - var result = await resolveTestCode(code); + '''); void check(String name, Asserter typeTest) { FunctionDeclaration test = AstFinder.getTopLevelFunction( @@ -197,16 +195,17 @@ class StrongModeLocalInferenceTest extends PubPackageResolutionTest { } test_cascadeExpression() async { - String code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { List map(T a, List mapper(T x)) => mapper(a); } void main () { A a = new A()..map(0, (x) => [x]); - } - '''; - var result = await resolveTestCode(code); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. + } + '''); List statements = AstFinder.getStatementsInTopLevelFunction( result.unit, "main", @@ -5743,7 +5742,7 @@ class C extends Override implements Base {} } test_localVariableInference_bottom_disabled() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' main() { var v = null; v; // marker @@ -5753,7 +5752,7 @@ main() { } test_localVariableInference_constant() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' main() { var v = 3; v; // marker @@ -5763,7 +5762,7 @@ main() { } test_localVariableInference_declaredType_disabled() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' main() { dynamic v = 3; v; // marker @@ -5773,7 +5772,7 @@ main() { } test_localVariableInference_noInitializer_disabled() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' main() { var v; v = 3; @@ -5802,7 +5801,7 @@ AssignmentExpression } test_localVariableInference_transitive_field_inferred_lexical() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { final x = 3; f() { @@ -5818,7 +5817,7 @@ main() { } test_localVariableInference_transitive_field_inferred_reversed() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { f() { var v = x; @@ -5834,7 +5833,7 @@ main() { } test_localVariableInference_transitive_field_lexical() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { int x = 3; f() { @@ -5850,7 +5849,7 @@ main() { } test_localVariableInference_transitive_field_reversed() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { f() { var v = x; @@ -5866,7 +5865,7 @@ main() { } test_localVariableInference_transitive_list_local() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' main() { var x = [3]; var v = x[0]; @@ -5877,7 +5876,7 @@ main() { } test_localVariableInference_transitive_local() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' main() { var x = 3; var v = x; @@ -5888,7 +5887,7 @@ main() { } test_localVariableInference_transitive_topLevel_inferred_lexical() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' final x = 3; main() { var v = x; @@ -5900,7 +5899,7 @@ main() { } test_localVariableInference_transitive_toplevel_inferred_reversed() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' main() { var v = x; v; // marker @@ -5912,7 +5911,7 @@ final x = 3; } test_localVariableInference_transitive_topLevel_lexical() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' int x = 3; main() { var v = x; @@ -5924,7 +5923,7 @@ main() { } test_localVariableInference_transitive_topLevel_reversed() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' main() { var v = x; v; // marker diff --git a/pkg/analyzer/test/src/dart/ast/element_locator_test.dart b/pkg/analyzer/test/src/dart/ast/element_locator_test.dart index 9044fffabdd..b384ad20573 100644 --- a/pkg/analyzer/test/src/dart/ast/element_locator_test.dart +++ b/pkg/analyzer/test/src/dart/ast/element_locator_test.dart @@ -22,9 +22,11 @@ main() { @reflectiveTest class ElementLocatorTest2 extends PubPackageResolutionTest { test_locate_AssignedVariablePattern() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f() { int foo; +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. (foo, _) = (0, 1); } '''); @@ -36,7 +38,7 @@ foo@17 } test_locate_AssignmentExpression() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' int x = 0; void main() { x += 1; @@ -50,7 +52,9 @@ dart:core::@class::num::@method::+ } test_locate_BinaryExpression() async { - var result = await resolveTestCode('var x = 3 + 4'); + var result = await resolveTestCodeWithDiagnostics(r''' +var x = 3 + 4; +'''); var node = result.findNode.binary('+'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -59,9 +63,11 @@ dart:core::@class::num::@method::+ } test_locate_CatchClauseParameter() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f() { try {} catch (e, s) {} +// ^ +// [diag.unusedCatchStack] The stack trace variable 's' isn't used and can be removed. } '''); var node = result.findNode.catchClauseParameter('e'); @@ -77,7 +83,9 @@ s@30 } test_locate_ClassDeclaration() async { - var result = await resolveTestCode('class A {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +class A {} +'''); var node = result.findNode.classDeclaration('class'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -86,7 +94,7 @@ s@30 } test_locate_ConstructorDeclaration_named() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { A.foo(); } @@ -99,7 +107,7 @@ class A { } test_locate_ConstructorDeclaration_unnamed() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { A(); } @@ -112,7 +120,7 @@ class A { } test_locate_ConstructorSelector_EnumConstantArguments_EnumConstantDeclaration() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' enum E { v.named(); // 0 const E.named(); @@ -126,9 +134,11 @@ enum E { } test_locate_DeclaredVariablePattern() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case int foo) {} +// ^^^ +// [diag.unusedLocalVariable] The value of the local variable 'foo' isn't used. } '''); var node = result.findNode.declaredVariablePattern('foo'); @@ -139,11 +149,13 @@ foo@37 } test_locate_DotShorthandConstructorInvocation() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A {} void main() { A a = .new(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. } '''); var node = result.findNode.singleDotShorthandConstructorInvocation; @@ -154,13 +166,15 @@ void main() { } test_locate_DotShorthandInvocation() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { static A foo() => A(); } void main() { A a = .foo(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. } '''); var node = result.findNode.singleDotShorthandInvocation; @@ -171,13 +185,15 @@ void main() { } test_locate_DotShorthandPropertyAccess() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { static A foo = A(); } void main() { A a = .foo; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. } '''); var node = result.findNode.singleDotShorthandPropertyAccess; @@ -188,7 +204,9 @@ void main() { } test_locate_DottedName_libraryDirective() async { - var result = await resolveTestCode('library foo.bar;'); + var result = await resolveTestCodeWithDiagnostics(r''' +library foo.bar; +'''); var node = result.findNode.singleDottedName; var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -197,7 +215,7 @@ void main() { } test_locate_EnumConstantDeclaration() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' enum E { one } @@ -210,7 +228,9 @@ enum E { } test_locate_ExportDirective() async { - var result = await resolveTestCode("export 'dart:core';"); + var result = await resolveTestCodeWithDiagnostics(r''' +export 'dart:core'; +'''); var node = result.findNode.export('export'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -219,7 +239,9 @@ dart:core } test_locate_ExtensionDeclaration() async { - var result = await resolveTestCode('extension A on int {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +extension A on int {} +'''); var node = result.findNode.singleExtensionDeclaration; var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -228,7 +250,9 @@ dart:core } test_locate_ExtensionTypeDeclaration() async { - var result = await resolveTestCode('extension type A(int it) {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +extension type A(int it) {} +'''); var node = result.findNode.singleExtensionTypeDeclaration; var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -237,9 +261,11 @@ dart:core } test_locate_FunctionDeclaration_local() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f() { int g() => 3; +// ^ +// [diag.unusedElement] The declaration 'g' isn't referenced. } '''); var node = result.findNode.functionDeclaration('g'); @@ -250,7 +276,9 @@ g@17 } test_locate_FunctionDeclaration_topLevel() async { - var result = await resolveTestCode('int f() => 3;'); + var result = await resolveTestCodeWithDiagnostics(r''' +int f() => 3; +'''); var node = result.findNode.functionDeclaration('f'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -259,7 +287,7 @@ g@17 } test_locate_Identifier_annotationClass_namedConstructor() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class Class { const Class.name(); } @@ -273,7 +301,7 @@ void main(@Class.name() parameter) {} } test_locate_Identifier_annotationClass_unnamedConstructor() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class Class { const Class(); } @@ -287,7 +315,9 @@ void main(@Class() parameter) {} } test_locate_Identifier_className() async { - var result = await resolveTestCode('class A {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +class A {} +'''); var node = result.findNode.classDeclaration('A'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -296,7 +326,7 @@ void main(@Class() parameter) {} } test_locate_Identifier_constructor_named() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { A.bar(); } @@ -309,7 +339,7 @@ class A { } test_locate_Identifier_constructor_unnamed() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { A(); } @@ -322,7 +352,7 @@ class A { } test_locate_Identifier_fieldName() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { var x; } @@ -335,7 +365,7 @@ class A { } test_locate_Identifier_functionCallMethod_invocation() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(int a) { f.call(a); } @@ -348,7 +378,7 @@ void f(int a) { } test_locate_Identifier_functionCallMethod_tearOff() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(int a) { f.call; } @@ -361,9 +391,11 @@ void f(int a) { } test_locate_Identifier_propertyAccess() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void main() { int x = 'foo'.length; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } '''); var node = result.findNode.simple('length'); @@ -374,7 +406,9 @@ dart:core::@class::String::@getter::length } test_locate_ImportDirective() async { - var result = await resolveTestCode("import 'dart:core';"); + var result = await resolveTestCodeWithDiagnostics(r''' +import 'dart:core'; +'''); var node = result.findNode.import('import'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -383,10 +417,12 @@ dart:core } test_locate_IndexExpression() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void main() { var x = [1, 2]; var y = x[0]; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } '''); var node = result.findNode.index('[0]'); @@ -399,7 +435,7 @@ MethodMember } test_locate_InstanceCreationExpression() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A {} void main() { @@ -417,7 +453,7 @@ void main() { newFile('$testPackageLibPath/a.dart', r''' class A {} '''); - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' import 'a.dart' as pref; void main() { @@ -434,7 +470,7 @@ package:test/a.dart::@class::A::@constructor::new test_locate_InstanceCreationExpression_type_simpleIdentifier() async { newFile('$testPackageLibPath/a.dart', r''' '''); - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A {} void main() { @@ -449,7 +485,9 @@ void main() { } test_locate_LibraryDirective() async { - var result = await resolveTestCode('library foo;'); + var result = await resolveTestCodeWithDiagnostics(r''' +library foo; +'''); var node = result.findNode.library('library'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -458,7 +496,9 @@ void main() { } test_locate_LibraryElement() async { - var result = await resolveTestCode('// only comment'); + var result = await resolveTestCodeWithDiagnostics(r''' +// only comment +'''); var element = ElementLocator.locate(result.unit); _assertElement(element, r''' @@ -467,7 +507,7 @@ void main() { } test_locate_MethodDeclaration() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { void foo() {} } @@ -480,7 +520,7 @@ class A { } test_locate_MethodInvocation_class_callMethod_argument() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { void call(int i) {} } @@ -496,7 +536,7 @@ void f(A a) { } test_locate_MethodInvocation_class_callMethod_constructor() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { void call(int i) {} } @@ -512,7 +552,7 @@ void f() { } test_locate_MethodInvocation_function_callMethod_invocation() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(int i) { f.call(1); } @@ -525,7 +565,7 @@ void f(int i) { } test_locate_MethodInvocation_function_callMethod_tearOff() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(int i) { f.call; } @@ -538,7 +578,7 @@ void f(int i) { } test_locate_MethodInvocation_method() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { void foo() {} } @@ -555,7 +595,7 @@ void main() { } test_locate_MethodInvocation_topLevel() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' foo(x) {} void main() { @@ -570,7 +610,9 @@ void main() { } test_locate_MixinDeclaration() async { - var result = await resolveTestCode('mixin A {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +mixin A {} +'''); var node = result.findNode.singleMixinDeclaration; var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -579,7 +621,7 @@ void main() { } test_locate_PatternField() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(Object? x) { if (x case int(isEven: true)) {} } @@ -592,7 +634,9 @@ dart:core::@class::int::@getter::isEven } test_locate_PostfixExpression() async { - var result = await resolveTestCode('int addOne(int x) => x++;'); + var result = await resolveTestCodeWithDiagnostics(r''' +int addOne(int x) => x++; +'''); var node = result.findNode.postfix('x++'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -601,7 +645,7 @@ dart:core::@class::num::@method::+ } test_locate_Prefix() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' import 'dart:math' as math; math.Random? r; @@ -614,7 +658,7 @@ math.Random? r; } test_locate_PrefixedIdentifier() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(int a) { a.isEven; } @@ -627,7 +671,7 @@ dart:core::@class::int::@getter::isEven } test_locate_PrefixedIdentifier_functionCallMethod() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(int a) { f.call; } @@ -640,7 +684,9 @@ void f(int a) { } test_locate_PrefixExpression() async { - var result = await resolveTestCode('int addOne(int x) => ++x;'); + var result = await resolveTestCodeWithDiagnostics(r''' +int addOne(int x) => ++x; +'''); var node = result.findNode.prefix('++x'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -649,7 +695,9 @@ dart:core::@class::num::@method::+ } test_locate_PrimaryConstructorBody() async { - var result = await resolveTestCode('class A() { this { } }'); + var result = await resolveTestCodeWithDiagnostics(r''' +class A() { this { } } +'''); var node = result.findNode.singlePrimaryConstructorBody; var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -658,7 +706,9 @@ dart:core::@class::num::@method::+ } test_locate_PrimaryConstructorDeclaration() async { - var result = await resolveTestCode('extension type A(int it) {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +extension type A(int it) {} +'''); var node = result.findNode.singlePrimaryConstructorDeclaration; var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -667,7 +717,9 @@ dart:core::@class::num::@method::+ } test_locate_PrimaryConstructorDeclaration_named() async { - var result = await resolveTestCode('extension type A.named(int it) {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +extension type A.named(int it) {} +'''); var node = result.findNode.singlePrimaryConstructorDeclaration; var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -676,7 +728,9 @@ dart:core::@class::num::@method::+ } test_locate_PrimaryConstructorDeclaration_named_atConstructorName() async { - var result = await resolveTestCode('extension type A.named(int it) {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +extension type A.named(int it) {} +'''); var node = result.findNode.singlePrimaryConstructorDeclaration.constructorName; var element = ElementLocator.locate(node); @@ -686,7 +740,9 @@ dart:core::@class::num::@method::+ } test_locate_PrimaryConstructorDeclaration_namedConstructor_constructorName() async { - var result = await resolveTestCode('class A.named() {}'); + var result = await resolveTestCodeWithDiagnostics(r''' +class A.named() {} +'''); var node = result.findNode.singlePrimaryConstructorDeclaration.constructorName; var element = ElementLocator.locate(node); @@ -697,7 +753,9 @@ dart:core::@class::num::@method::+ test_locate_StringLiteral_exportUri() async { newFile("$testPackageLibPath/foo.dart", ''); - var result = await resolveTestCode("export 'foo.dart';"); + var result = await resolveTestCodeWithDiagnostics(r''' +export 'foo.dart'; +'''); var node = result.findNode.stringLiteral('foo.dart'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -706,7 +764,9 @@ package:test/foo.dart } test_locate_StringLiteral_expression() async { - var result = await resolveTestCode("var x = 'abc';"); + var result = await resolveTestCodeWithDiagnostics(r''' +var x = 'abc'; +'''); var node = result.findNode.stringLiteral('abc'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -716,7 +776,11 @@ package:test/foo.dart test_locate_StringLiteral_importUri() async { newFile("$testPackageLibPath/foo.dart", ''); - var result = await resolveTestCode("import 'foo.dart';"); + var result = await resolveTestCodeWithDiagnostics(r''' +import 'foo.dart'; +// ^^^^^^^^^^ +// [diag.unusedImport] Unused import: 'foo.dart'. +'''); var node = result.findNode.stringLiteral('foo.dart'); var element = ElementLocator.locate(node); _assertElement(element, r''' @@ -725,9 +789,11 @@ package:test/foo.dart } test_locate_VariableDeclaration_Local() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' f() { var x = 42; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } '''); var node = result.findNode.variableDeclaration('x ='); @@ -738,7 +804,9 @@ x@12 } test_locate_VariableDeclaration_TopLevel() async { - var result = await resolveTestCode('var x = 42;'); + var result = await resolveTestCodeWithDiagnostics(r''' +var x = 42; +'''); var node = result.findNode.variableDeclaration('x ='); var element = ElementLocator.locate(node); _assertElement(element, r''' diff --git a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart index 6015edff3cb..c8f79332dbd 100644 --- a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart +++ b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart @@ -106,7 +106,7 @@ const int x = 'foo'; } test_dotShorthand_enum_simple() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' enum E { v1, v2 } const E x1 = .v1; const E x2 = .v2; @@ -303,7 +303,7 @@ E } test_enum_enhanced_named() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' enum E { v1.named(10), v2.named(20); @@ -343,7 +343,7 @@ E } test_enum_enhanced_unnamed() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' enum E { v1(10), v2(20), @@ -398,7 +398,7 @@ E } test_enum_simple() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' enum E { v1, v2 } const x1 = E.v1; const x2 = E.v2; @@ -3897,7 +3897,7 @@ const v2 = -v1; @reflectiveTest mixin ConstantVisitorTestCases on ConstantVisitorTestSupport { test_listLiteral_ifElement_false_withElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = [1, if (1 < 0) 2 else 3, 4]; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -3909,7 +3909,7 @@ const c = [1, if (1 < 0) 2 else 3, 4]; } test_listLiteral_ifElement_false_withoutElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = [1, if (1 < 0) 2, 3]; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -3921,7 +3921,7 @@ const c = [1, if (1 < 0) 2, 3]; } test_listLiteral_ifElement_true_withElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = [1, if (1 > 0) 2 else 3, 4]; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -3933,7 +3933,7 @@ const c = [1, if (1 > 0) 2 else 3, 4]; } test_listLiteral_ifElement_true_withoutElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = [1, if (1 > 0) 2, 3]; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -3945,7 +3945,7 @@ const c = [1, if (1 > 0) 2, 3]; } test_listLiteral_nested() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = [1, if (1 > 0) if (2 > 1) 2, 3]; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -3959,7 +3959,7 @@ const c = [1, if (1 > 0) if (2 > 1) 2, 3]; } test_listLiteral_spreadElement() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = [1, ...[2, 3], 4]; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -3971,7 +3971,7 @@ const c = [1, ...[2, 3], 4]; } test_mapLiteral_ifElement_false_withElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {'a' : 1, if (1 < 0) 'b' : 2 else 'c' : 3, 'd' : 4}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -3991,7 +3991,7 @@ const c = {'a' : 1, if (1 < 0) 'b' : 2 else 'c' : 3, 'd' : 4}; } test_mapLiteral_ifElement_false_withoutElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {'a' : 1, if (1 < 0) 'b' : 2, 'c' : 3}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4011,7 +4011,7 @@ const c = {'a' : 1, if (1 < 0) 'b' : 2, 'c' : 3}; } test_mapLiteral_ifElement_true_withElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {'a' : 1, if (1 > 0) 'b' : 2 else 'c' : 3, 'd' : 4}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4031,7 +4031,7 @@ const c = {'a' : 1, if (1 > 0) 'b' : 2 else 'c' : 3, 'd' : 4}; } test_mapLiteral_ifElement_true_withoutElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {'a' : 1, if (1 > 0) 'b' : 2, 'c' : 3}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4050,17 +4050,15 @@ const c = {'a' : 1, if (1 > 0) 'b' : 2, 'c' : 3}; expect(value.values.map((e) => e.toIntValue()), unorderedEquals([1, 2, 3])); } - @failingTest test_mapLiteral_nested() async { - // Fails because we're not yet parsing nested elements. - var unitResult = await resolveTestCode(''' -const c = {'a' : 1, if (1 > 0) if (2 > 1) {'b' : 2}, 'c' : 3}; + var unitResult = await resolveTestCodeWithDiagnostics(''' +const c = {'a' : 1, if (1 > 0) if (2 > 1) ...{'b' : 2}, 'c' : 3}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); expect( result.type, unitResult.typeProvider.mapType( - unitResult.typeProvider.intType, + unitResult.typeProvider.stringType, unitResult.typeProvider.intType, ), ); @@ -4073,7 +4071,7 @@ const c = {'a' : 1, if (1 > 0) if (2 > 1) {'b' : 2}, 'c' : 3}; } test_mapLiteral_spreadElement() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {'a' : 1, ...{'b' : 2, 'c' : 3}, 'd' : 4}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4096,7 +4094,7 @@ const c = {'a' : 1, ...{'b' : 2, 'c' : 3}, 'd' : 4}; } test_setLiteral_ifElement_false_withElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {1, if (1 < 0) 2 else 3, 4}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4108,7 +4106,7 @@ const c = {1, if (1 < 0) 2 else 3, 4}; } test_setLiteral_ifElement_false_withoutElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {1, if (1 < 0) 2, 3}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4120,7 +4118,7 @@ const c = {1, if (1 < 0) 2, 3}; } test_setLiteral_ifElement_true_withElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {1, if (1 > 0) 2 else 3, 4}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4132,7 +4130,7 @@ const c = {1, if (1 > 0) 2 else 3, 4}; } test_setLiteral_ifElement_true_withoutElse() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {1, if (1 > 0) 2, 3}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4144,7 +4142,7 @@ const c = {1, if (1 > 0) 2, 3}; } test_setLiteral_nested() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {1, if (1 > 0) if (2 > 1) 2, 3}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4156,7 +4154,7 @@ const c = {1, if (1 > 0) if (2 > 1) 2, 3}; } test_setLiteral_spreadElement() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = {1, ...{2, 3}, 4}; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4179,9 +4177,11 @@ String abcdef } test_visitAsExpression_instanceOfSameClass() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' const a = const A(); const b = a as A; +// ^^^^^^ +// [diag.unnecessaryCast] Unnecessary cast. class A { const A(); } @@ -4192,7 +4192,7 @@ class A { } test_visitAsExpression_instanceOfSubclass() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' const a = const B(); const b = a as A; class A { @@ -4343,7 +4343,7 @@ const c = a && true; } test_visitBinaryExpression_and_bool_known_known() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = false & true; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4351,7 +4351,7 @@ const c = false & true; } test_visitBinaryExpression_and_bool_known_unknown() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const b = bool.fromEnvironment('y'); const c = false & b; '''); @@ -4369,7 +4369,7 @@ const c = true && a; } test_visitBinaryExpression_and_bool_unknown_known() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const a = bool.fromEnvironment('x'); const c = a & true; '''); @@ -4378,7 +4378,7 @@ const c = a & true; } test_visitBinaryExpression_and_bool_unknown_unknown() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const a = bool.fromEnvironment('x'); const b = bool.fromEnvironment('y'); const c = a & b; @@ -4583,7 +4583,7 @@ const c = a || true; } test_visitBinaryExpression_or_bool_known_known() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = false | true; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4591,7 +4591,7 @@ const c = false | true; } test_visitBinaryExpression_or_bool_known_unknown() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const b = bool.fromEnvironment('y'); const c = false | b; '''); @@ -4611,7 +4611,7 @@ const c = true || a; } test_visitBinaryExpression_or_bool_unknown_known() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const a = bool.fromEnvironment('x'); const c = a | true; '''); @@ -4620,7 +4620,7 @@ const c = a | true; } test_visitBinaryExpression_or_bool_unknown_unknown() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const a = bool.fromEnvironment('x'); const b = bool.fromEnvironment('y'); const c = a | b; @@ -4630,7 +4630,7 @@ const c = a | b; } test_visitBinaryExpression_or_int() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = 3 | 5; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4661,8 +4661,12 @@ const c = 3 | false; } test_visitBinaryExpression_questionQuestion_notNull_notNull() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = 'a' ?? 'b'; +// ^^^^^^ +// [diag.deadCode] Dead code. +// ^^^ +// [diag.deadNullAwareExpression] The left operand can't be null, so the right operand is never executed. '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); expect(result.type, unitResult.typeProvider.stringType); @@ -4679,7 +4683,7 @@ class C {} } test_visitBinaryExpression_questionQuestion_null_notNull() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = null ?? 'b'; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4688,7 +4692,7 @@ const c = null ?? 'b'; } test_visitBinaryExpression_questionQuestion_null_null() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = null ?? null; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4696,7 +4700,7 @@ const c = null ?? null; } test_visitBinaryExpression_xor_bool_known_known() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = false ^ true; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4704,7 +4708,7 @@ const c = false ^ true; } test_visitBinaryExpression_xor_bool_known_unknown() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const b = bool.fromEnvironment('y'); const c = false ^ b; '''); @@ -4713,7 +4717,7 @@ const c = false ^ b; } test_visitBinaryExpression_xor_bool_unknown_known() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const a = bool.fromEnvironment('x'); const c = a ^ true; '''); @@ -4722,7 +4726,7 @@ const c = a ^ true; } test_visitBinaryExpression_xor_bool_unknown_unknown() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const a = bool.fromEnvironment('x'); const b = bool.fromEnvironment('y'); const c = a ^ b; @@ -4732,7 +4736,7 @@ const c = a ^ b; } test_visitBinaryExpression_xor_int() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const c = 3 ^ 5; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'c'); @@ -4928,7 +4932,7 @@ double 3.45 } test_visitIntegerLiteral_doubleType() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const double d = 3; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'd'); @@ -5181,7 +5185,7 @@ const y = B(x); } test_visitSimpleIdentifier_dynamic() async { - var unitResult = await resolveTestCode(''' + var unitResult = await resolveTestCodeWithDiagnostics(''' const a = dynamic; '''); DartObjectImpl result = _evaluateConstant(unitResult, 'a'); diff --git a/pkg/analyzer/test/src/dart/element/element_test.dart b/pkg/analyzer/test/src/dart/element/element_test.dart index de806e1d215..83d7dd3c927 100644 --- a/pkg/analyzer/test/src/dart/element/element_test.dart +++ b/pkg/analyzer/test/src/dart/element/element_test.dart @@ -35,7 +35,7 @@ class A { } void f(@A('x') int p) {} '''); - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; main() { f(3); @@ -54,7 +54,7 @@ main() { @reflectiveTest class FieldElementImplTest extends PubPackageResolutionTest { test_isEnumConstant() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' enum B {B1, B2, B3} '''); var B = result.findElement.enum_('B'); diff --git a/pkg/analyzer/test/src/dart/resolution/conditional_expression_test.dart b/pkg/analyzer/test/src/dart/resolution/conditional_expression_test.dart index 7bf357b3df6..a1302e86ff0 100644 --- a/pkg/analyzer/test/src/dart/resolution/conditional_expression_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/conditional_expression_test.dart @@ -47,7 +47,7 @@ ConditionalExpression } test_downward_condition() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' void f(int b, int c) { a() ? b : c; } @@ -312,7 +312,7 @@ ConditionalExpression } test_upward() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' void f(bool a, int b, int c) { var d = a ? b : c; print(d); diff --git a/pkg/analyzer/test/src/dart/resolution/constant_test.dart b/pkg/analyzer/test/src/dart/resolution/constant_test.dart index 62afe6ca50e..828bbc18615 100644 --- a/pkg/analyzer/test/src/dart/resolution/constant_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/constant_test.dart @@ -204,8 +204,10 @@ class C { static const int f = 42; } '''); - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; +// ^^^^^^^^ +// [diag.unusedImport] Unused import: 'a.dart'. '''); var import_ = result.findElement.importFind('package:test/a.dart'); @@ -221,8 +223,10 @@ extension E on int { static const int f = 42; } '''); - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; +// ^^^^^^^^ +// [diag.unusedImport] Unused import: 'a.dart'. '''); var import_ = result.findElement.importFind('package:test/a.dart'); @@ -240,8 +244,10 @@ mixin M on C { static const int f = 42; } '''); - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' import 'a.dart'; +// ^^^^^^^^ +// [diag.unusedImport] Unused import: 'a.dart'. '''); var import_ = result.findElement.importFind('package:test/a.dart'); diff --git a/pkg/analyzer/test/src/dart/resolution/constructor_field_initializer_test.dart b/pkg/analyzer/test/src/dart/resolution/constructor_field_initializer_test.dart index 4f36812b99e..b923b2720d2 100644 --- a/pkg/analyzer/test/src/dart/resolution/constructor_field_initializer_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/constructor_field_initializer_test.dart @@ -70,7 +70,7 @@ ConstructorFieldInitializer } test_functionExpressionInvocation_blockBody() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { final x; A(int a) : x = (() {return a + 1;})(); @@ -129,7 +129,7 @@ ConstructorFieldInitializer } test_functionExpressionInvocation_expressionBody() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { final int x; A(int a) : x = (() => a + 1)(); diff --git a/pkg/analyzer/test/src/dart/resolution/field_test.dart b/pkg/analyzer/test/src/dart/resolution/field_test.dart index d5199f42eca..3a1ae8e6d35 100644 --- a/pkg/analyzer/test/src/dart/resolution/field_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/field_test.dart @@ -210,7 +210,7 @@ FieldDeclaration } test_session_getterSetter() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { var f = 0; } @@ -223,7 +223,7 @@ class A { } test_type_inferred_int() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { var f = 0; } @@ -232,7 +232,7 @@ class A { } test_type_inferred_Never() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { var f = throw 42; } @@ -241,7 +241,7 @@ class A { } test_type_inferred_noInitializer() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { var f; } @@ -250,7 +250,7 @@ class A { } test_type_inferred_null() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { var f = null; } diff --git a/pkg/analyzer/test/src/dart/resolution/for_statement_test.dart b/pkg/analyzer/test/src/dart/resolution/for_statement_test.dart index ec8199c2c73..c270f223a26 100644 --- a/pkg/analyzer/test/src/dart/resolution/for_statement_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/for_statement_test.dart @@ -28,7 +28,7 @@ main() { class ForStatementResolutionTest_ForEachPartsWithDeclaration extends PubPackageResolutionTest { test_async_loopVariable_var_stream() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(Stream values) async { await for (var v in values) { v; @@ -395,7 +395,7 @@ ForStatement } test_sync_loopVariable_dynamic() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(List values) { for (dynamic v in values) { v; @@ -478,7 +478,7 @@ ForStatement } test_sync_loopVariable_var_iterable() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(Iterable values) { for (var v in values) { v; @@ -517,7 +517,7 @@ ForStatement } test_sync_loopVariable_var_list() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f(List values) { for (var v in values) { v; diff --git a/pkg/analyzer/test/src/dart/resolution/function_expression_invocation_test.dart b/pkg/analyzer/test/src/dart/resolution/function_expression_invocation_test.dart index fad0ae1927e..dbd14cffabd 100644 --- a/pkg/analyzer/test/src/dart/resolution/function_expression_invocation_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/function_expression_invocation_test.dart @@ -53,7 +53,7 @@ FunctionExpressionInvocation } test_call_infer_fromArguments_listLiteral() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { List call(List _) { throw 42; @@ -61,6 +61,8 @@ class A { } main(A a) { +// ^ +// [diag.mainFirstPositionalParameterType] The type of the first positional parameter of the 'main' function must be a supertype of 'List'. a([0]); } '''); diff --git a/pkg/analyzer/test/src/dart/resolution/instance_member_inference_class_test.dart b/pkg/analyzer/test/src/dart/resolution/instance_member_inference_class_test.dart index 311d81bc2f2..956fa5083e6 100644 --- a/pkg/analyzer/test/src/dart/resolution/instance_member_inference_class_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/instance_member_inference_class_test.dart @@ -7,17 +7,19 @@ import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(InstanceMemberInferenceClassTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class InstanceMemberInferenceClassTest extends PubPackageResolutionTest { test_field_covariant_fromField() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { covariant num foo = 0; } @@ -31,7 +33,7 @@ class B implements A { } test_field_covariant_fromSetter() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(covariant num _) {} } @@ -45,13 +47,15 @@ class B implements A { } test_field_fromInitializer_inherited() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { var foo = 0; } class B implements A { var foo; +// ^^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'foo' must be initialized. } '''); var foo = result.findElement.field('foo', of: 'B'); @@ -59,9 +63,11 @@ class B implements A { } test_field_fromInitializer_preferSuper() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { num foo; +// ^^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'foo' must be initialized. } class B implements A { @@ -73,15 +79,22 @@ class B implements A { } test_field_multiple_fields_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo = throw 0; +// ^^^ +// [context 1] The member being overridden. } class B { String foo = throw 0; +// ^^^ +// [context 2] The member being overridden. } class C implements A, B { var foo; +// ^^^ +// [diag.invalidOverride][context 1] 'C.foo' ('dynamic Function()') isn't a valid override of 'A.foo' ('int Function()'). +// [diag.invalidOverride][context 2] 'C.foo' ('dynamic Function()') isn't a valid override of 'B.foo' ('String Function()'). } '''); var foo = result.findElement.field('foo', of: 'C'); @@ -89,7 +102,7 @@ class C implements A, B { } test_field_multiple_getters_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { num get foo => throw 0; } @@ -98,6 +111,8 @@ class B { } class C implements A, B { var foo; +// ^^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'foo' must be initialized. } '''); var foo = result.findElement.field('foo', of: 'C'); @@ -105,15 +120,22 @@ class C implements A, B { } test_field_multiple_getters_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { String get foo => throw 0; +// ^^^ +// [context 1] The member being overridden. } class B { int get foo => throw 0; +// ^^^ +// [context 2] The member being overridden. } class C implements A, B { var foo; +// ^^^ +// [diag.invalidOverride][context 1] 'C.foo' ('dynamic Function()') isn't a valid override of 'A.foo' ('String Function()'). +// [diag.invalidOverride][context 2] 'C.foo' ('dynamic Function()') isn't a valid override of 'B.foo' ('int Function()'). } '''); var foo = result.findElement.field('foo', of: 'C'); @@ -121,7 +143,7 @@ class C implements A, B { } test_field_multiple_gettersSetters_final_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { num get foo => throw 0; } @@ -132,7 +154,11 @@ class C { set foo(String _) {} } class X implements A, B, C { +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'setter C.foo'. final foo; +// ^^^ +// [diag.finalNotInitialized] The final variable 'foo' must be initialized. } '''); var foo = result.findElement.field('foo', of: 'X'); @@ -140,18 +166,28 @@ class X implements A, B, C { } test_field_multiple_gettersSetters_final_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { String get foo => throw 0; +// ^^^ +// [context 1] The member being overridden. } class B { int get foo => throw 0; +// ^^^ +// [context 2] The member being overridden. } class C { set foo(String _) {} } class X implements A, B, C { +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'setter C.foo'. final foo; +// ^^^ +// [diag.invalidOverride][context 1] 'X.foo' ('dynamic Function()') isn't a valid override of 'A.foo' ('String Function()'). +// [diag.invalidOverride][context 2] 'X.foo' ('dynamic Function()') isn't a valid override of 'B.foo' ('int Function()'). +// [diag.finalNotInitialized] The final variable 'foo' must be initialized. } '''); var foo = result.findElement.field('foo', of: 'X'); @@ -159,18 +195,25 @@ class X implements A, B, C { } test_field_multiple_gettersSetters_notFinal_combined_notSame() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { num get foo => throw 0; +// ^^^ +// [context 1] The member being overridden. } class B { int get foo => throw 0; +// ^^^ +// [context 2] The member being overridden. } class C { set foo(String _) {} } class X implements A, B, C { var foo; +// ^^^ +// [diag.invalidOverride][context 1] 'X.foo' ('dynamic Function()') isn't a valid override of 'A.foo' ('num Function()'). +// [diag.invalidOverride][context 2] 'X.foo' ('dynamic Function()') isn't a valid override of 'B.foo' ('int Function()'). } '''); var foo = result.findElement.field('foo', of: 'X'); @@ -179,7 +222,7 @@ class X implements A, B, C { } test_field_multiple_gettersSetters_notFinal_combined_same() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { num get foo => throw 0; } @@ -191,6 +234,8 @@ class C { } class X implements A, B, C { var foo; +// ^^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'foo' must be initialized. } '''); var foo = result.findElement.field('foo', of: 'X'); @@ -198,18 +243,25 @@ class X implements A, B, C { } test_field_multiple_gettersSetters_notFinal_incompatible_getters() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { String get foo => throw 0; +// ^^^ +// [context 1] The member being overridden. } class B { int get foo => throw 0; +// ^^^ +// [context 2] The member being overridden. } class C { set foo(int _) {} } class X implements A, B, C { var foo; +// ^^^ +// [diag.invalidOverride][context 1] 'X.foo' ('dynamic Function()') isn't a valid override of 'A.foo' ('String Function()'). +// [diag.invalidOverride][context 2] 'X.foo' ('dynamic Function()') isn't a valid override of 'B.foo' ('int Function()'). } '''); var foo = result.findElement.field('foo', of: 'X'); @@ -217,9 +269,11 @@ class X implements A, B, C { } test_field_multiple_gettersSetters_notFinal_incompatible_setters() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int get foo => throw 0; +// ^^^ +// [context 1] The member being overridden. } class B { set foo(String _) {} @@ -229,6 +283,8 @@ class C { } class X implements A, B, C { var foo; +// ^^^ +// [diag.invalidOverride][context 1] 'X.foo' ('dynamic Function()') isn't a valid override of 'A.foo' ('int Function()'). } '''); var foo = result.findElement.field('foo', of: 'X'); @@ -236,7 +292,7 @@ class X implements A, B, C { } test_field_multiple_setters_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(num _) {} } @@ -245,6 +301,8 @@ class B { } class C implements A, B { var foo; +// ^^^ +// [diag.notInitializedNonNullableInstanceField] Non-nullable instance field 'foo' must be initialized. } '''); var foo = result.findElement.field('foo', of: 'C'); @@ -252,7 +310,7 @@ class C implements A, B { } test_field_multiple_setters_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(String _) {} } @@ -268,7 +326,7 @@ class C implements A, B { } test_getter_multiple_getters_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { num get foo => throw 0; } @@ -284,15 +342,22 @@ class C implements A, B { } test_getter_multiple_getters_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { String get foo => throw 0; +// ^^^ +// [context 1] The member being overridden. } class B { int get foo => throw 0; +// ^^^ +// [context 2] The member being overridden. } class C implements A, B { get foo => throw 0; +// ^^^ +// [diag.invalidOverride][context 1] 'C.foo' ('dynamic Function()') isn't a valid override of 'A.foo' ('String Function()'). +// [diag.invalidOverride][context 2] 'C.foo' ('dynamic Function()') isn't a valid override of 'B.foo' ('int Function()'). } '''); var foo = result.findElement.getter('foo', of: 'C'); @@ -300,7 +365,7 @@ class C implements A, B { } test_getter_multiple_getters_same() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int get foo => throw 0; } @@ -316,7 +381,7 @@ class C implements A, B { } test_getter_multiple_gettersSetters_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { num get foo => throw 0; } @@ -327,6 +392,8 @@ class C { set foo(String _) {} } class X implements A, B, C { +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'setter C.foo'. get foo => throw 0; } '''); @@ -335,18 +402,27 @@ class X implements A, B, C { } test_getter_multiple_gettersSetters_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { String get foo => throw 0; +// ^^^ +// [context 1] The member being overridden. } class B { int get foo => throw 0; +// ^^^ +// [context 2] The member being overridden. } class C { set foo(String _) {} } class X implements A, B, C { +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'setter C.foo'. get foo => throw 0; +// ^^^ +// [diag.invalidOverride][context 1] 'X.foo' ('dynamic Function()') isn't a valid override of 'A.foo' ('String Function()'). +// [diag.invalidOverride][context 2] 'X.foo' ('dynamic Function()') isn't a valid override of 'B.foo' ('int Function()'). } '''); var foo = result.findElement.getter('foo', of: 'X'); @@ -354,7 +430,7 @@ class X implements A, B, C { } test_getter_multiple_setters_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(num _) {} } @@ -362,6 +438,8 @@ class B { set foo(int _) {} } class C implements A, B { +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'setter A.foo'. get foo => throw 0; } '''); @@ -370,7 +448,7 @@ class C implements A, B { } test_getter_multiple_setters_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(String _) {} } @@ -378,6 +456,8 @@ class B { set foo(int _) {} } class C implements A, B { +// ^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'foo=': A.foo= (void Function(String)), B.foo= (void Function(int)). get foo => throw 0; } '''); @@ -386,13 +466,17 @@ class C implements A, B { } test_invalid_field_overrides_method() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' abstract class A { List foo() {} +// ^^^ +// [diag.bodyMightCompleteNormally] The body might complete normally, causing 'null' to be returned, but the return type, 'List', is a potentially non-nullable type. } class B implements A { var foo = {}; +// ^^^ +// [diag.conflictingFieldAndMethod] Class 'B' can't define field 'foo' and have method 'A.foo' with the same name. } '''); var foo = result.findElement.field('foo', of: 'B'); @@ -400,23 +484,35 @@ class B implements A { } test_invalid_inheritanceCycle() async { - await resolveTestCode(''' + await resolveTestCodeWithDiagnostics(''' class A extends C {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, C, A. class B extends A {} +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, C, A. class C extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'C' can't be a superinterface of itself: B, C, A. '''); } test_method_parameter_covariant_named() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({num p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo({covariant num p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class C implements A, B { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } '''); var p = result.findElement.method('foo', of: 'C').formalParameters[0]; @@ -424,15 +520,21 @@ class C implements A, B { } test_method_parameter_covariant_positional() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo([num p]) {} +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo([covariant num p]) {} +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class C implements A, B { void foo([int p]) {} +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } '''); var p = result.findElement.method('foo', of: 'C').formalParameters[0]; @@ -440,7 +542,7 @@ class C implements A, B { } test_method_parameter_covariant_required() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(num p) {} } @@ -456,15 +558,21 @@ class C implements A, B { } test_method_parameter_named_multiple_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo({num p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class C implements A, B { void foo({p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } '''); var p = result.findElement.method('foo', of: 'C').formalParameters[0]; @@ -472,15 +580,21 @@ class C implements A, B { } test_method_parameter_named_multiple_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo({int q}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'q' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class C implements A, B { void foo({p}) {} +// ^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'C' from overridden methods: A.foo (void Function({int p})), B.foo (void Function({int q})). } '''); var p = result.findElement.method('foo', of: 'C').formalParameters[0]; @@ -488,15 +602,21 @@ class C implements A, B { } test_method_parameter_named_multiple_same() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class C implements A, B { void foo({p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } '''); var p = result.findElement.method('foo', of: 'C').formalParameters[0]; @@ -504,15 +624,19 @@ class C implements A, B { } test_method_parameter_namedAndRequired() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo(int p) {} } class C implements A, B { void foo(p) {} +// ^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'C' from overridden methods: A.foo (void Function({int p})), B.foo (void Function(int)). } '''); var p = result.findElement.method('foo', of: 'C').formalParameters[0]; @@ -520,7 +644,7 @@ class C implements A, B { } test_method_parameter_required_multiple_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(int p) {} } @@ -536,7 +660,7 @@ class C implements A, B { } test_method_parameter_required_multiple_different_merge() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(Object? p) {} } @@ -554,7 +678,7 @@ class C implements A, B { } test_method_parameter_required_multiple_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(int p) {} } @@ -563,6 +687,8 @@ class B { } class C implements A, B { void foo(p) {} +// ^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'C' from overridden methods: A.foo (void Function(int)), B.foo (void Function(double)). } '''); var p = result.findElement.method('foo', of: 'C').formalParameters[0]; @@ -570,7 +696,7 @@ class C implements A, B { } test_method_parameter_required_multiple_same() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(int p) {} } @@ -586,7 +712,7 @@ class C implements A, B { } test_method_parameter_required_single_generic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(E p) {} } @@ -599,15 +725,21 @@ class C implements A { } test_method_parameter_requiredAndPositional() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(int p) {} } class B { void foo([int p]) {} +// ^^^ +// [context 1] The member being overridden. +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class C implements A, B { void foo(p) {} +// ^^^ +// [diag.invalidOverride][context 1] 'C.foo' ('void Function(int)') isn't a valid override of 'B.foo' ('void Function([int])'). } '''); var p = result.findElement.method('foo', of: 'C').formalParameters[0]; @@ -615,7 +747,7 @@ class C implements A, B { } test_method_return_multiple_different_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -631,7 +763,7 @@ class C implements A, B { } test_method_return_multiple_different_dynamic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -647,7 +779,7 @@ class C implements A, B { } test_method_return_multiple_different_generic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { E foo() => throw 0; } @@ -656,6 +788,8 @@ class B { } class C implements A, B { foo() => throw 0; +//^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'C' from overridden methods: A.foo (int Function()), B.foo (double Function()). } '''); var foo = result.findElement.method('foo', of: 'C'); @@ -663,7 +797,7 @@ class C implements A, B { } test_method_return_multiple_different_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -672,6 +806,8 @@ class B { } class C implements A, B { foo() => 0; +//^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'C' from overridden methods: A.foo (int Function()), B.foo (double Function()). } '''); var foo = result.findElement.method('foo', of: 'C'); @@ -679,7 +815,7 @@ class C implements A, B { } test_method_return_multiple_different_merge() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { Object? foo() => throw 0; } @@ -697,7 +833,7 @@ class C implements A, B { } test_method_return_multiple_different_void() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -713,15 +849,21 @@ class C implements A, B { } test_method_return_multiple_same_generic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { E foo() => 0; +// ^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'int' can't be returned from the method 'foo' because it has a return type of 'E'. } class B { E foo() => 0; +// ^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'int' can't be returned from the method 'foo' because it has a return type of 'E'. } class C implements A, B { foo() => 0; +// ^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'int' can't be returned from the method 'foo' because it has a return type of 'T'. } '''); var foo = result.findElement.method('foo', of: 'C'); @@ -729,7 +871,7 @@ class C implements A, B { } test_method_return_multiple_same_nonVoid() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -745,15 +887,21 @@ class C implements A, B { } test_method_return_multiple_same_void() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo() {}; +// ^ +// [diag.expectedClassMember] Expected a class member. } class B { void foo() {}; +// ^ +// [diag.expectedClassMember] Expected a class member. } class C implements A, B { foo() {}; +// ^ +// [diag.expectedClassMember] Expected a class member. } '''); var foo = result.findElement.method('foo', of: 'C'); @@ -761,7 +909,7 @@ class C implements A, B { } test_method_return_single() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -774,7 +922,7 @@ class B extends A { } test_method_return_single_generic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { E foo() => throw 0; } @@ -787,7 +935,7 @@ class B extends A { } test_setter_covariant_fromSetter() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(num _) {} } @@ -803,7 +951,7 @@ class C implements A, B { } test_setter_multiple_getters_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { num get foo => throw 0; } @@ -811,6 +959,8 @@ class B { int get foo => throw 0; } class C implements A, B { +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'getter B.foo'. set foo(x) {} } '''); @@ -819,7 +969,7 @@ class C implements A, B { } test_setter_multiple_getters_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { String get foo => throw 0; } @@ -827,6 +977,8 @@ class B { int get foo => throw 0; } class C implements A, B { +// ^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'foo': A.foo (String Function()), B.foo (int Function()). set foo(x) {} } '''); @@ -835,7 +987,7 @@ class C implements A, B { } test_setter_multiple_gettersSetters_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(num _) {} } @@ -846,6 +998,8 @@ class C { String get foo => throw 0; } class X implements A, B, C { +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'getter C.foo'. set foo(x) {} } '''); @@ -854,7 +1008,7 @@ class X implements A, B, C { } test_setter_multiple_gettersSetters_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(String _) {} } @@ -865,6 +1019,8 @@ class C { int get foo => throw 0; } class X implements A, B, C { +// ^ +// [diag.nonAbstractClassInheritsAbstractMemberOne] Missing concrete implementation of 'getter C.foo'. set foo(x) {} } '''); @@ -873,7 +1029,7 @@ class X implements A, B, C { } test_setter_multiple_setters_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(num _) {} } @@ -889,7 +1045,7 @@ class C implements A, B { } test_setter_multiple_setters_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo(String _) {} } @@ -905,9 +1061,11 @@ class C implements A, B { } test_setter_single_setter_withoutParameter() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { set foo() {} +// ^^^ +// [diag.wrongNumberOfParametersForSetter] Setters must declare exactly one required positional parameter. } class B implements A { set foo(x) {} diff --git a/pkg/analyzer/test/src/dart/resolution/instance_member_inference_mixin_test.dart b/pkg/analyzer/test/src/dart/resolution/instance_member_inference_mixin_test.dart index 3dece300642..d30f9cf75d4 100644 --- a/pkg/analyzer/test/src/dart/resolution/instance_member_inference_mixin_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/instance_member_inference_mixin_test.dart @@ -5,33 +5,47 @@ import 'package:test_reflective_loader/test_reflective_loader.dart'; import 'context_collection_resolution.dart'; +import 'node_text_expectations.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(InstanceMemberInferenceClassTest); + defineReflectiveTests(UpdateNodeTextExpectations); }); } @reflectiveTest class InstanceMemberInferenceClassTest extends PubPackageResolutionTest { test_invalid_inheritanceCycle() async { - await resolveTestCode(''' + await resolveTestCodeWithDiagnostics(''' class A extends C {} +// ^ +// [diag.recursiveInterfaceInheritance] 'A' can't be a superinterface of itself: B, C, A. class B extends A {} +// ^ +// [diag.recursiveInterfaceInheritance] 'B' can't be a superinterface of itself: B, C, A. class C extends B {} +// ^ +// [diag.recursiveInterfaceInheritance] 'C' can't be a superinterface of itself: B, C, A. '''); } test_method_parameter_named_multiple_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo({num p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } mixin M on A, B { void foo({p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } '''); var p = result.findElement.method('foo', of: 'M').formalParameters[0]; @@ -39,15 +53,23 @@ mixin M on A, B { } test_method_parameter_named_multiple_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo({int q}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'q' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } mixin M on A, B { +// ^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'foo': A.foo (void Function({int p})), B.foo (void Function({int q})). void foo({p}) {} +// ^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'M' from overridden methods: A.foo (void Function({int p})), B.foo (void Function({int q})). } '''); var p = result.findElement.method('foo', of: 'M').formalParameters[0]; @@ -55,15 +77,21 @@ mixin M on A, B { } test_method_parameter_named_multiple_same() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } mixin M on A, B { void foo({p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } '''); var p = result.findElement.method('foo', of: 'M').formalParameters[0]; @@ -71,15 +99,21 @@ mixin M on A, B { } test_method_parameter_namedAndRequired() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo({int p}) {} +// ^ +// [diag.missingDefaultValueForParameter] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } class B { void foo(int p) {} } mixin M on A, B { +// ^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'foo': A.foo (void Function({int p})), B.foo (void Function(int)). void foo(p) {} +// ^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'M' from overridden methods: A.foo (void Function({int p})), B.foo (void Function(int)). } '''); var p = result.findElement.method('foo', of: 'M').formalParameters[0]; @@ -87,7 +121,7 @@ mixin M on A, B { } test_method_parameter_required_multiple_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(int p) {} } @@ -103,7 +137,7 @@ mixin M on A, B { } test_method_parameter_required_multiple_different_merge() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(Object? p) {} } @@ -121,7 +155,7 @@ mixin M on A, B { } test_method_parameter_required_multiple_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(int p) {} } @@ -129,7 +163,11 @@ class B { void foo(double p) {} } mixin M on A, B { +// ^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'foo': A.foo (void Function(int)), B.foo (void Function(double)). void foo(p) {} +// ^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'M' from overridden methods: A.foo (void Function(int)), B.foo (void Function(double)). } '''); var p = result.findElement.method('foo', of: 'M').formalParameters[0]; @@ -137,7 +175,7 @@ mixin M on A, B { } test_method_parameter_required_multiple_same() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(int p) {} } @@ -153,7 +191,7 @@ mixin M on A, B { } test_method_parameter_required_single_generic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(E p) {} } @@ -166,15 +204,21 @@ mixin M on A { } test_method_parameter_requiredAndPositional() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo(int p) {} } class B { void foo([int p]) {} +// ^^^ +// [context 1] The member being overridden. +// ^ +// [diag.missingDefaultValueForParameterPositional] The parameter 'p' can't have a value of 'null' because of its type, but the implicit default value is 'null'. } mixin M on A, B { void foo(p) {} +// ^^^ +// [diag.invalidOverride][context 1] 'M.foo' ('void Function(int)') isn't a valid override of 'B.foo' ('void Function([int])'). } '''); var p = result.findElement.method('foo', of: 'M').formalParameters[0]; @@ -182,7 +226,7 @@ mixin M on A, B { } test_method_return_multiple_different_combined() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -198,7 +242,7 @@ mixin M on A, B { } test_method_return_multiple_different_dynamic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -214,7 +258,7 @@ mixin M on A, B { } test_method_return_multiple_different_generic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { E foo() => throw 0; } @@ -222,7 +266,11 @@ class B { E foo() => throw 0; } mixin M on A, B { +// ^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'foo': A.foo (int Function()), B.foo (double Function()). foo() => throw 0; +//^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'M' from overridden methods: A.foo (int Function()), B.foo (double Function()). } '''); var foo = result.findElement.method('foo', of: 'M'); @@ -230,7 +278,7 @@ mixin M on A, B { } test_method_return_multiple_different_incompatible() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -238,7 +286,11 @@ class B { double foo() => 0.0; } mixin M on A, B { +// ^ +// [diag.inconsistentInheritance] Superinterfaces don't have a valid override for 'foo': A.foo (int Function()), B.foo (double Function()). foo() => 0; +//^^^ +// [diag.noCombinedSuperSignature] Can't infer missing types in 'M' from overridden methods: A.foo (int Function()), B.foo (double Function()). } '''); var foo = result.findElement.method('foo', of: 'M'); @@ -246,7 +298,7 @@ mixin M on A, B { } test_method_return_multiple_different_merge() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { Object? foo() => throw 0; } @@ -264,7 +316,7 @@ mixin M on A, B { } test_method_return_multiple_different_void() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -280,15 +332,21 @@ mixin M on A, B { } test_method_return_multiple_same_generic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { E foo() => 0; +// ^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'int' can't be returned from the method 'foo' because it has a return type of 'E'. } class B { E foo() => 0; +// ^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'int' can't be returned from the method 'foo' because it has a return type of 'E'. } mixin M on A, B { foo() => 0; +// ^ +// [diag.returnOfInvalidTypeFromMethod] A value of type 'int' can't be returned from the method 'foo' because it has a return type of 'T'. } '''); var foo = result.findElement.method('foo', of: 'M'); @@ -296,7 +354,7 @@ mixin M on A, B { } test_method_return_multiple_same_nonVoid() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -312,15 +370,21 @@ mixin M on A, B { } test_method_return_multiple_same_void() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { void foo() {}; +// ^ +// [diag.expectedClassMember] Expected a class member. } class B { void foo() {}; +// ^ +// [diag.expectedClassMember] Expected a class member. } mixin M on A, B { foo() {}; +// ^ +// [diag.expectedClassMember] Expected a class member. } '''); var foo = result.findElement.method('foo', of: 'M'); @@ -328,7 +392,7 @@ mixin M on A, B { } test_method_return_single() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { int foo() => 0; } @@ -341,7 +405,7 @@ class B extends A { } test_method_return_single_generic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' class A { E foo() => throw 0; } diff --git a/pkg/analyzer/test/src/dart/resolution/library_element_test.dart b/pkg/analyzer/test/src/dart/resolution/library_element_test.dart index c56f7a9c14d..056c874a08c 100644 --- a/pkg/analyzer/test/src/dart/resolution/library_element_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/library_element_test.dart @@ -22,10 +22,15 @@ main() { @reflectiveTest class LibraryElementTest_featureSet extends PubPackageResolutionTest { + static String get _currentLanguageVersion { + var currentVersion = ExperimentStatus.currentVersion; + return '${currentVersion.major}.${currentVersion.minor}'; + } + test_language205() async { writeTestPackageConfig(PackageConfigFileBuilder(), languageVersion: '2.5'); - var result = await resolveTestCode(''); + var result = await resolveTestCodeWithDiagnostics(''); _assertLanguageVersion( result, @@ -44,7 +49,7 @@ class LibraryElementTest_featureSet extends PubPackageResolutionTest { test_language208() async { writeTestPackageConfig(PackageConfigFileBuilder(), languageVersion: '2.8'); - var result = await resolveTestCode(''); + var result = await resolveTestCodeWithDiagnostics(''); _assertLanguageVersion( result, @@ -64,7 +69,10 @@ class LibraryElementTest_featureSet extends PubPackageResolutionTest { test_language208_override205() async { writeTestPackageConfig(PackageConfigFileBuilder(), languageVersion: '2.8'); - var result = await resolveTestCode('// @dart = 2.5'); + var result = await resolveTestCodeWithDiagnostics(r''' +// @dart = 2.5 +// [diag.illegalLanguageVersionOverride][column 1][length 14] The language version must be >=2.12.0. +'''); // Valid override, less than the latest supported language version. _assertLanguageVersion( @@ -84,7 +92,7 @@ class LibraryElementTest_featureSet extends PubPackageResolutionTest { test_language209() async { writeTestPackageConfig(PackageConfigFileBuilder(), languageVersion: '2.9'); - var result = await resolveTestCode(''); + var result = await resolveTestCodeWithDiagnostics(''); _assertLanguageVersion( result, @@ -104,7 +112,10 @@ class LibraryElementTest_featureSet extends PubPackageResolutionTest { test_language212_override399() async { writeTestPackageConfig(PackageConfigFileBuilder(), languageVersion: '2.12'); - var result = await resolveTestCode('// @dart = 3.99'); + var result = await resolveTestCodeWithDiagnostics(''' +// @dart = 3.99 +// [diag.invalidLanguageVersionOverrideGreater][column 1][length 15] The language version override can't specify a version greater than the latest known language version: $_currentLanguageVersion. +'''); // Invalid override: minor is greater than the latest minor. _assertLanguageVersion( @@ -126,7 +137,10 @@ class LibraryElementTest_featureSet extends PubPackageResolutionTest { test_language212_override400() async { writeTestPackageConfig(PackageConfigFileBuilder(), languageVersion: '2.12'); - var result = await resolveTestCode('// @dart = 4.00'); + var result = await resolveTestCodeWithDiagnostics(''' +// @dart = 4.00 +// [diag.invalidLanguageVersionOverrideGreater][column 1][length 15] The language version override can't specify a version greater than the latest known language version: $_currentLanguageVersion. +'''); // Invalid override: major is greater than the latest major. _assertLanguageVersion( diff --git a/pkg/analyzer/test/src/dart/resolution/library_export_test.dart b/pkg/analyzer/test/src/dart/resolution/library_export_test.dart index 68440f0462f..6304f87fa82 100644 --- a/pkg/analyzer/test/src/dart/resolution/library_export_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/library_export_test.dart @@ -375,7 +375,7 @@ ExportDirective get f => null; set f(_) {} '''); - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' export 'a.dart'; '''); var exportNamespace = result.libraryElement.exportNamespace; diff --git a/pkg/analyzer/test/src/dart/resolution/logical_or_pattern_test.dart b/pkg/analyzer/test/src/dart/resolution/logical_or_pattern_test.dart index 78670500313..d9f075ed5e9 100644 --- a/pkg/analyzer/test/src/dart/resolution/logical_or_pattern_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/logical_or_pattern_test.dart @@ -77,9 +77,21 @@ LogicalOrPattern test_switchCase_topLevel3() async { // https://github.com/dart-lang/sdk/issues/60168 - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var _ = switch (0) { +// ^ +// [diag.unusedElement] The declaration '_' isn't referenced. var a || var a || var a => 0, +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. +// ^^^^^^^^ +// [diag.deadCode] Dead code. +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. +// ^^^^^^^^ +// [diag.deadCode] Dead code. +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'a' isn't used. }; '''); diff --git a/pkg/analyzer/test/src/dart/resolution/metadata_test.dart b/pkg/analyzer/test/src/dart/resolution/metadata_test.dart index a160682271b..c2455a5c11f 100644 --- a/pkg/analyzer/test/src/dart/resolution/metadata_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/metadata_test.dart @@ -206,13 +206,17 @@ void f(List list) { } test_location_forEachPartsWithDeclaration() async { - await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' +const foo = 42; void f() { - for (var @foo x = 0;;) {} + for (@foo var x = 0;;) { + x; + break; + } } '''); - // This is invalid code. - // No checks, as long as it does not crash. + + _assertAtFoo42(result); } test_location_libraryDirective() async { @@ -295,13 +299,16 @@ A } test_location_localVariableDeclaration() async { - await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' +const foo = 42; void f() { - var @foo x; + @foo + var x; + x; } '''); - // This is invalid code. - // No checks, as long as it does not crash. + + _assertAtFoo42(result); } test_location_methodDeclaration() async { @@ -552,14 +559,18 @@ A } test_value_class_namedConstructor_unresolved_hasFormalParameter() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { const A(); } void f(int named) { @A.named(42) +//^^^^^^^^^^^^ +// [diag.invalidAnnotation] Annotation must be either a const variable reference or const constructor invocation. int x = 0; +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'x' isn't used. } '''); @@ -1007,12 +1018,15 @@ A } test_value_genericClass_instanceGetter() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { T get foo {} +// ^^^ +// [diag.bodyMightCompleteNormally] The body might complete normally, causing 'null' to be returned, but the return type, 'T', is a potentially non-nullable type. } @A.foo +// [diag.invalidAnnotation][column 1][length 6] Annotation must be either a const variable reference or const constructor invocation. void f() {} '''); @@ -1094,12 +1108,15 @@ A } test_value_genericClass_staticGetter() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { static T get foo {} +// ^ +// [diag.typeParameterReferencedByStatic] Static members can't reference type parameters of the class. } @A.foo +// [diag.invalidAnnotation][column 1][length 6] Annotation must be either a const variable reference or const constructor invocation. void f() {} '''); diff --git a/pkg/analyzer/test/src/dart/resolution/super_constructor_invocation_test.dart b/pkg/analyzer/test/src/dart/resolution/super_constructor_invocation_test.dart index 9f48fbad872..8d4351885f8 100644 --- a/pkg/analyzer/test/src/dart/resolution/super_constructor_invocation_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/super_constructor_invocation_test.dart @@ -84,13 +84,15 @@ SuperConstructorInvocation } test_named_unresolved_hasFormalParameter() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A { A(int a); } class B extends A { B(int named) : super.named(0); +// ^^^^^^^^^^^^^^ +// [diag.undefinedConstructorInInitializer] The class 'A' doesn't have a constructor named 'named'. } '''); diff --git a/pkg/analyzer/test/src/dart/resolution/top_level_variable_test.dart b/pkg/analyzer/test/src/dart/resolution/top_level_variable_test.dart index 094405d55e8..46d0e89b4cb 100644 --- a/pkg/analyzer/test/src/dart/resolution/top_level_variable_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/top_level_variable_test.dart @@ -154,7 +154,7 @@ VariableDeclaration } test_session_getterSetter() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = 0; '''); var getter = result.findElement.topGet('v'); @@ -165,28 +165,28 @@ var v = 0; } test_type_inferred_int() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = 0; '''); assertType(result.findElement.topVar('v').type, 'int'); } test_type_inferred_Never() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = throw 42; '''); assertType(result.findElement.topVar('v').type, 'Never'); } test_type_inferred_noInitializer() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v; '''); assertType(result.findElement.topVar('v').type, 'dynamic'); } test_type_inferred_null() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = null; '''); assertType(result.findElement.topVar('v').type, 'dynamic'); diff --git a/pkg/analyzer/test/src/dart/resolution/type_inference/equality_expressions_test.dart b/pkg/analyzer/test/src/dart/resolution/type_inference/equality_expressions_test.dart index f3d45da69cb..87fde8f07d9 100644 --- a/pkg/analyzer/test/src/dart/resolution/type_inference/equality_expressions_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/type_inference/equality_expressions_test.dart @@ -18,7 +18,7 @@ main() { @reflectiveTest class EqualTest extends PubPackageResolutionTest { test_simple() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' void f(Object a, Object b) { var c = a == b; print(c); @@ -31,7 +31,7 @@ void f(Object a, Object b) { @reflectiveTest class NotEqualTest extends PubPackageResolutionTest { test_simple() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' void f(Object a, Object b) { var c = a != b; print(c); diff --git a/pkg/analyzer/test/src/dart/resolution/type_inference/function_expression_test.dart b/pkg/analyzer/test/src/dart/resolution/type_inference/function_expression_test.dart index 85674f83850..3e0cb3df73a 100644 --- a/pkg/analyzer/test/src/dart/resolution/type_inference/function_expression_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/type_inference/function_expression_test.dart @@ -494,7 +494,7 @@ FunctionExpression } test_downward_argumentType_Null() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' void foo(void Function(Null) a) {} main() { @@ -632,7 +632,7 @@ FunctionExpression } test_noContext_returnType_async_blockBody() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () async { return 0; }; @@ -641,14 +641,14 @@ var v = () async { } test_noContext_returnType_async_expressionBody() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () async => 0; '''); _assertReturnType(result, '() async =>', 'Future'); } test_noContext_returnType_asyncStar_blockBody() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () async* { yield 0; }; @@ -657,7 +657,7 @@ var v = () async* { } test_noContext_returnType_asyncStar_blockBody_hasReturn_empty() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () async* { yield 0; return; @@ -667,7 +667,7 @@ var v = () async* { } test_noContext_returnType_asyncStar_blockBody_hasReturn_noYield() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () async* { return; }; @@ -676,7 +676,7 @@ var v = () async* { } test_noContext_returnType_asyncStar_blockBody_lubNum() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () async* { yield 0; yield 1.1; @@ -686,7 +686,7 @@ var v = () async* { } test_noContext_returnType_asyncStar_blockBody_lubObject() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () async* { yield 0; yield ''; @@ -696,7 +696,7 @@ var v = () async* { } test_noContext_returnType_asyncStar_blockBody_lubWithNull() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () async* { yield 0; yield null; @@ -706,7 +706,7 @@ var v = () async* { } test_noContext_returnType_sync_blockBody() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () { return 0; }; @@ -715,7 +715,7 @@ var v = () { } test_noContext_returnType_sync_blockBody_dynamic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = (dynamic a) { return a; }; @@ -724,7 +724,7 @@ var v = (dynamic a) { } test_noContext_returnType_sync_blockBody_Never() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () { throw 42; }; @@ -733,7 +733,7 @@ var v = () { } test_noContext_returnType_sync_blockBody_notNullable() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = (bool b) { if (b) return 0; return 1.2; @@ -826,7 +826,7 @@ main() { } test_noContext_returnType_sync_blockBody_null_hasReturn() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = (bool b) { if (b) return; }; @@ -835,14 +835,14 @@ var v = (bool b) { } test_noContext_returnType_sync_blockBody_null_noReturn() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () {}; '''); _assertReturnType(result, '() {}', 'Null'); } test_noContext_returnType_sync_blockBody_nullable() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = (bool b) { if (b) return 0; }; @@ -880,28 +880,28 @@ main() { } test_noContext_returnType_sync_expressionBody_dynamic() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = (dynamic a) => a; '''); _assertReturnType(result, '(dynamic a) =>', 'dynamic'); } test_noContext_returnType_sync_expressionBody_Never() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () => throw 42; '''); _assertReturnType(result, '() =>', 'Never'); } test_noContext_returnType_sync_expressionBody_notNullable() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () => 42; '''); _assertReturnType(result, '() =>', 'int'); } test_noContext_returnType_sync_expressionBody_Null() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' main() { var v = () => null; v; @@ -911,7 +911,7 @@ main() { } test_noContext_returnType_syncStar_blockBody() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' var v = () sync* { yield 0; }; @@ -920,7 +920,7 @@ var v = () sync* { } test_noContext_returnType_syncStar_blockBody_hasReturn_empty() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () sync* { yield 0; return; @@ -930,7 +930,7 @@ var v = () sync* { } test_noContext_returnType_syncStar_blockBody_hasReturn_noYield() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () sync* { return; }; @@ -939,7 +939,7 @@ var v = () sync* { } test_noContext_returnType_syncStar_blockBody_lubNum() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () sync* { yield 0; yield 1.1; @@ -949,7 +949,7 @@ var v = () sync* { } test_noContext_returnType_syncStar_blockBody_lubObject() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () sync* { yield 0; yield ''; @@ -959,7 +959,7 @@ var v = () sync* { } test_noContext_returnType_syncStar_blockBody_lubWithNull() async { - var result = await resolveTestCode(r''' + var result = await resolveTestCodeWithDiagnostics(r''' var v = () sync* { yield 0; yield null; diff --git a/pkg/analyzer/test/src/dart/resolution/type_inference/map_literal_test.dart b/pkg/analyzer/test/src/dart/resolution/type_inference/map_literal_test.dart index c39f1cfcf50..2914100f315 100644 --- a/pkg/analyzer/test/src/dart/resolution/type_inference/map_literal_test.dart +++ b/pkg/analyzer/test/src/dart/resolution/type_inference/map_literal_test.dart @@ -42,7 +42,7 @@ Map a = {1 : 2}; } test_context_noTypeArgs_noElements_futureOr() async { - var result = await resolveTestCode(''' + var result = await resolveTestCodeWithDiagnostics(''' import 'dart:async'; FutureOr> f() { 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 58cf7482de6..ecdad7ca0fd 100644 --- a/pkg/analyzer/test/src/task/strong/dart2_inference_test.dart +++ b/pkg/analyzer/test/src/task/strong/dart2_inference_test.dart @@ -140,27 +140,31 @@ AssertStatement } test_closure_downwardReturnType_arrow() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' void main() { List Function() g; g = () => 42; +// ^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'List' function, as required by the closure's context. + g; } -'''; - var result = await resolveTestCode(code); +'''); Expression closure = result.findNode.expression('() => 42'); assertType(closure, 'List Function()'); } test_closure_downwardReturnType_block() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' void main() { List Function() g; g = () { // mark return 42; +// ^^ +// [diag.returnOfInvalidTypeFromClosure] The returned type 'int' isn't returnable from a 'List' function, as required by the closure's context. }; + g; } -'''; - var result = await resolveTestCode(code); +'''); Expression closure = result.findNode.expression('() { // mark'); assertType(closure, 'List Function()'); } @@ -187,27 +191,27 @@ main() { } test_forIn_identifier() async { - var code = r''' -T f() => null; + var result = await resolveTestCodeWithDiagnostics(r''' +T f() => throw 0; class A {} -A aTopLevel; +late A aTopLevel; void set aTopLevelSetter(A value) {} class C { - A aField; + late A aField; void set aSetter(A value) {} void test() { - A aLocal; + late A aLocal; for (aLocal in f()) {} // local + aLocal; for (aField in f()) {} // field for (aSetter in f()) {} // setter for (aTopLevel in f()) {} // top variable for (aTopLevelSetter in f()) {} // top setter } -}'''; - var result = await resolveTestCode(code); +}'''); void assertInvocationType(String prefix) { var invocation = result.findNode.methodInvocation(prefix); assertType(invocation, 'Iterable'); @@ -221,7 +225,7 @@ class C { } test_forIn_variable_implicitlyTyped() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' class A {} class B extends A {} @@ -229,13 +233,22 @@ List f(List items) => items; void test(List listA, List listB) { for (var a1 in f(listA)) {} // 1 +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a1' isn't used. for (A a2 in f(listA)) {} // 2 +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'a2' isn't used. for (var b1 in f(listB)) {} // 3 +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'b1' isn't used. for (A b2 in f(listB)) {} // 4 +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'b2' isn't used. for (B b3 in f(listB)) {} // 5 +// ^^ +// [diag.unusedLocalVariable] The value of the local variable 'b3' isn't used. } -'''; - var result = await resolveTestCode(code); +'''); void assertTypes( String vSearch, String vType, @@ -259,13 +272,12 @@ void test(List listA, List listB) { } test_implicitVoidReturnType_default() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' class C { set x(_) {} operator []=(int index, double value) => null; } -'''; - var result = await resolveTestCode(code); +'''); ClassElement c = result.findElement.class_('C'); SetterElement x = c.setters[0]; @@ -277,16 +289,19 @@ class C { } test_implicitVoidReturnType_derived() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' class Base { dynamic set x(_) {} +//^^^^^^^ +// [diag.nonVoidReturnForSetter] The return type of the setter must be 'void' or absent. dynamic operator[]=(int x, int y) => null; +//^^^^^^^ +// [diag.nonVoidReturnForOperator] The return type of the operator []= must be 'void'. } class Derived extends Base { set x(_) {} operator[]=(int x, int y) {} -}'''; - var result = await resolveTestCode(code); +}'''); ClassElement c = result.findElement.class_('Derived'); SetterElement x = c.setters[0]; @@ -298,11 +313,10 @@ class Derived extends Base { } test_listMap_empty() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' var x = []; var y = {}; -'''; - var result = await resolveTestCode(code); +'''); var xNode = result.findNode.variableDeclaration('x = '); var xfragment = xNode.declaredFragment!; assertType(xfragment.element.type, 'List'); @@ -313,11 +327,10 @@ var y = {}; } test_listMap_null() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' var x = [null]; var y = {null: null}; -'''; - var result = await resolveTestCode(code); +'''); var xNode = result.findNode.variableDeclaration('x = '); var xFragment = xNode.declaredFragment!; assertType(xFragment.element.type, 'List'); @@ -416,7 +429,7 @@ BinaryExpression } test_switchExpression_asContext_forCases() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' class C { const C(); } @@ -428,14 +441,13 @@ void test(C x) { default: break; } -}'''; - var result = await resolveTestCode(code); +}'''); var node = result.findNode.instanceCreation('C():'); assertType(node, 'C'); } test_switchExpression_asContext_forCases_language219() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' // @dart = 2.19 class C { const C(); @@ -448,23 +460,23 @@ void test(C x) { default: break; } -}'''; - var result = await resolveTestCode(code); +}'''); var node = result.findNode.instanceCreation('const C():'); assertType(node, 'C'); } test_voidType_method() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' class C { void m() {} } var x = new C().m(); main() { var y = new C().m(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -'''; - var result = await resolveTestCode(code); +'''); var xNode = result.findNode.variableDeclaration('x = '); var xFragment = xNode.declaredFragment!; expect(xFragment.element.type, VoidTypeImpl.instance); @@ -475,14 +487,15 @@ main() { } test_voidType_topLevelFunction() async { - var code = r''' + var result = await resolveTestCodeWithDiagnostics(r''' void f() {} var x = f(); main() { var y = f(); +// ^ +// [diag.unusedLocalVariable] The value of the local variable 'y' isn't used. } -'''; - var result = await resolveTestCode(code); +'''); var xNode = result.findNode.variableDeclaration('x = '); var xFragment = xNode.declaredFragment!; expect(xFragment.element.type, VoidTypeImpl.instance);