blob: 5158c9070561ecbc4d13a94a87a9597667ed963d [file] [log] [blame]
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library analyzer.test.generated.compile_time_error_code_test;
import 'dart:async';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
import 'package:analyzer/src/generated/source_io.dart';
import 'package:test/test.dart' show expect;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'resolver_test_case.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(CompileTimeErrorCodeTest);
});
}
@reflectiveTest
class CompileTimeErrorCodeTest extends ResolverTestCase {
fail_awaitInWrongContext_sync() async {
// This test requires better error recovery than we currently have. In
// particular, we need to be able to distinguish between an await expression
// in the wrong context, and the use of 'await' as an identifier.
Source source = addSource(r'''
f(x) {
return await x;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT]);
verify([source]);
}
fail_awaitInWrongContext_syncStar() async {
// This test requires better error recovery than we currently have. In
// particular, we need to be able to distinguish between an await expression
// in the wrong context, and the use of 'await' as an identifier.
Source source = addSource(r'''
f(x) sync* {
yield await x;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT]);
verify([source]);
}
fail_constEvalThrowsException() async {
Source source = addSource(r'''
class C {
const C();
}
f() { return const C(); }''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_THROWS_EXCEPTION]);
verify([source]);
}
fail_invalidIdentifierInAsync_async() async {
// TODO(brianwilkerson) Report this error.
Source source = addSource(r'''
class A {
m() async {
int async;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_IDENTIFIER_IN_ASYNC]);
verify([source]);
}
fail_invalidIdentifierInAsync_await() async {
// TODO(brianwilkerson) Report this error.
Source source = addSource(r'''
class A {
m() async {
int await;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_IDENTIFIER_IN_ASYNC]);
verify([source]);
}
fail_invalidIdentifierInAsync_yield() async {
// TODO(brianwilkerson) Report this error.
Source source = addSource(r'''
class A {
m() async {
int yield;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_IDENTIFIER_IN_ASYNC]);
verify([source]);
}
fail_mixinDeclaresConstructor() async {
Source source = addSource(r'''
class A {
A() {}
}
class B extends Object mixin A {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.MIXIN_DECLARES_CONSTRUCTOR]);
verify([source]);
}
fail_mixinOfNonClass() async {
// TODO(brianwilkerson) Compare with MIXIN_WITH_NON_CLASS_SUPERCLASS.
Source source = addSource(r'''
var A;
class B extends Object mixin A {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.MIXIN_OF_NON_CLASS]);
verify([source]);
}
fail_objectCannotExtendAnotherClass() async {
Source source = addSource(r'''
''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.OBJECT_CANNOT_EXTEND_ANOTHER_CLASS]);
verify([source]);
}
fail_superInitializerInObject() async {
Source source = addSource(r'''
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.SUPER_INITIALIZER_IN_OBJECT]);
verify([source]);
}
fail_yieldEachInNonGenerator_async() async {
// TODO(brianwilkerson) We are currently parsing the yield statement as a
// binary expression.
Source source = addSource(r'''
f() async {
yield* 0;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.YIELD_EACH_IN_NON_GENERATOR]);
verify([source]);
}
fail_yieldEachInNonGenerator_sync() async {
// TODO(brianwilkerson) We are currently parsing the yield statement as a
// binary expression.
Source source = addSource(r'''
f() {
yield* 0;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.YIELD_IN_NON_GENERATOR]);
verify([source]);
}
fail_yieldInNonGenerator_async() async {
// TODO(brianwilkerson) We are currently trying to parse the yield statement
// as a binary expression.
Source source = addSource(r'''
f() async {
yield 0;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.YIELD_IN_NON_GENERATOR]);
verify([source]);
}
fail_yieldInNonGenerator_sync() async {
// TODO(brianwilkerson) We are currently trying to parse the yield statement
// as a binary expression.
Source source = addSource(r'''
f() {
yield 0;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.YIELD_EACH_IN_NON_GENERATOR]);
verify([source]);
}
test_accessPrivateEnumField() async {
Source source = addSource(r'''
enum E { ONE }
String name(E e) {
return e._name;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.ACCESS_PRIVATE_ENUM_FIELD]);
// Cannot verify because "_name" cannot be resolved.
}
test_ambiguousExport() async {
Source source = addSource(r'''
library L;
export 'lib1.dart';
export 'lib2.dart';''');
addNamedSource("/lib1.dart", r'''
library lib1;
class N {}''');
addNamedSource("/lib2.dart", r'''
library lib2;
class N {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.AMBIGUOUS_EXPORT]);
verify([source]);
}
test_annotationWithNotClass() async {
Source source = addSource('''
class Property {
final int value;
const Property(this.value);
}
const Property property = const Property(42);
@property(123)
main() {
}
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.ANNOTATION_WITH_NON_CLASS]);
verify([source]);
}
test_annotationWithNotClass_prefixed() async {
addNamedSource("/annotations.dart", r'''
class Property {
final int value;
const Property(this.value);
}
const Property property = const Property(42);
''');
Source source = addSource('''
import 'annotations.dart' as pref;
@pref.property(123)
main() {
}
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.ANNOTATION_WITH_NON_CLASS]);
verify([source]);
}
test_async_used_as_identifier_in_annotation() async {
Source source = addSource('''
const int async = 0;
f() async {
g(@async x) {}
g(0);
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_argument_label() async {
Source source = addSource('''
@proxy
class C {}
f() async {
new C().g(async: 0);
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
// Note: we don't call verify([source]) because verify() doesn't understand
// about @proxy.
}
test_async_used_as_identifier_in_async_method() async {
Source source = addSource('''
f() async {
var async = 1;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_async_star_method() async {
Source source = addSource('''
f() async* {
var async = 1;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_break_statement() async {
Source source = addSource('''
f() async {
while (true) {
break async;
}
}
''');
await computeAnalysisResult(source);
assertErrors(source, [
ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER,
CompileTimeErrorCode.LABEL_UNDEFINED
]);
// Note: we don't call verify([source]) because the reference to the
// "async" label is unresolved.
}
test_async_used_as_identifier_in_cascaded_invocation() async {
Source source = addSource('''
class C {
int async() => 1;
}
f() async {
return new C()..async();
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_cascaded_setter_invocation() async {
Source source = addSource('''
class C {
void set async(int i) {}
}
f() async {
return new C()..async = 1;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_catch_exception_argument() async {
Source source = addSource('''
g() {}
f() async {
try {
g();
} catch (async) { }
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_catch_stacktrace_argument() async {
Source source = addSource('''
g() {}
f() async {
try {
g();
} catch (e, async) { }
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_continue_statement() async {
Source source = addSource('''
f() async {
while (true) {
continue async;
}
}
''');
await computeAnalysisResult(source);
assertErrors(source, [
ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER,
CompileTimeErrorCode.LABEL_UNDEFINED
]);
// Note: we don't call verify([source]) because the reference to the
// "async" label is unresolved.
}
test_async_used_as_identifier_in_for_statement() async {
Source source = addSource('''
var async;
f() async {
for (async in []) {}
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_formal_parameter_name() async {
Source source = addSource('''
f() async {
g(int async) {}
g(0);
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_getter_name() async {
Source source = addSource('''
class C {
int get async => 1;
}
f() async {
return new C().async;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_invocation() async {
Source source = addSource('''
class C {
int async() => 1;
}
f() async {
return new C().async();
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_local_function_name() async {
Source source = addSource('''
f() async {
int async() => null;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_prefix() async {
Source source = addSource('''
import 'dart:async' as async;
f() async {
return new async.Future.value(0);
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_setter_name() async {
Source source = addSource('''
class C {
void set async(int i) {}
}
f() async {
new C().async = 1;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_statement_label() async {
Source source = addSource('''
f() async {
async: g();
}
g() {}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_string_interpolation() async {
Source source = addSource(r'''
int async = 1;
f() async {
return "$async";
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_suffix() async {
addNamedSource("/lib1.dart", r'''
library lib1;
int async;
''');
Source source = addSource('''
import 'lib1.dart' as l;
f() async {
return l.async;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_switch_label() async {
Source source = addSource('''
f() async {
switch (0) {
async: case 0: break;
}
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_async_used_as_identifier_in_sync_star_method() async {
Source source = addSource('''
f() sync* {
var async = 1;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_asyncForInWrongContext() async {
Source source = addSource(r'''
f(list) {
await for (var e in list) {
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.ASYNC_FOR_IN_WRONG_CONTEXT]);
verify([source]);
}
test_await_used_as_identifier_in_async_method() async {
Source source = addSource('''
f() async {
var await = 1;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_await_used_as_identifier_in_async_star_method() async {
Source source = addSource('''
f() async* {
var await = 1;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_await_used_as_identifier_in_sync_star_method() async {
Source source = addSource('''
f() sync* {
var await = 1;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [ParserErrorCode.ASYNC_KEYWORD_USED_AS_IDENTIFIER]);
verify([source]);
}
test_bug_23176() async {
Source source = addSource('''
class A {
const A([x]);
}
class B {
dynamic @A(const A()) x;
}
''');
await computeAnalysisResult(source);
assertErrors(source, [
ParserErrorCode.EXPECTED_CLASS_MEMBER,
ParserErrorCode.MISSING_CONST_FINAL_VAR_OR_TYPE
]);
verify([source]);
}
test_builtInIdentifierAsMixinName_classTypeAlias() async {
Source source = addSource(r'''
class A {}
class B {}
class as = A with B;''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME]);
verify([source]);
}
test_builtInIdentifierAsPrefixName() async {
Source source = addSource("import 'dart:async' as abstract;");
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_PREFIX_NAME,
HintCode.UNUSED_IMPORT
]);
verify([source]);
}
test_builtInIdentifierAsType_formalParameter_field() async {
Source source = addSource(r'''
class A {
var x;
A(static this.x);
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE]);
verify([source]);
}
test_builtInIdentifierAsType_formalParameter_simple() async {
Source source = addSource(r'''
f(static x) {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE]);
verify([source]);
}
test_builtInIdentifierAsType_variableDeclaration() async {
Source source = addSource(r'''
f() {
typedef x;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE]);
verify([source]);
}
test_builtInIdentifierAsTypedefName_functionTypeAlias() async {
Source source = addSource("typedef bool as();");
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME]);
verify([source]);
}
test_builtInIdentifierAsTypeName() async {
Source source = addSource("class as {}");
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME]);
verify([source]);
}
test_builtInIdentifierAsTypeParameterName() async {
Source source = addSource("class A<as> {}");
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME]);
verify([source]);
}
test_caseExpressionTypeImplementsEquals() async {
Source source = addSource(r'''
class IntWrapper {
final int value;
const IntWrapper(this.value);
bool operator ==(IntWrapper x) {
return value == x.value;
}
get hashCode => value;
}
f(var a) {
switch(a) {
case(const IntWrapper(1)) : return 1;
default: return 0;
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CASE_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
verify([source]);
}
test_conflictingConstructorNameAndMember_field() async {
Source source = addSource(r'''
class A {
int x;
A.x() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_NAME_AND_FIELD]);
verify([source]);
}
test_conflictingConstructorNameAndMember_getter() async {
Source source = addSource(r'''
class A {
int get x => 42;
A.x() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_NAME_AND_FIELD]);
verify([source]);
}
test_conflictingConstructorNameAndMember_method() async {
Source source = addSource(r'''
class A {
const A.x();
void x() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_CONSTRUCTOR_NAME_AND_METHOD]);
verify([source]);
}
test_conflictingGetterAndMethod_field_method() async {
Source source = addSource(r'''
class A {
final int m = 0;
}
class B extends A {
m() {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONFLICTING_GETTER_AND_METHOD]);
verify([source]);
}
test_conflictingGetterAndMethod_getter_method() async {
Source source = addSource(r'''
class A {
get m => 0;
}
class B extends A {
m() {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONFLICTING_GETTER_AND_METHOD]);
verify([source]);
}
test_conflictingGetterAndMethod_method_field() async {
Source source = addSource(r'''
class A {
m() {}
}
class B extends A {
int m;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONFLICTING_METHOD_AND_GETTER]);
verify([source]);
}
test_conflictingGetterAndMethod_method_getter() async {
Source source = addSource(r'''
class A {
m() {}
}
class B extends A {
get m => 0;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONFLICTING_METHOD_AND_GETTER]);
verify([source]);
}
test_conflictingTypeVariableAndClass() async {
Source source = addSource(r'''
class T<T> {
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_CLASS]);
verify([source]);
}
test_conflictingTypeVariableAndMember_field() async {
Source source = addSource(r'''
class A<T> {
var T;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
verify([source]);
}
test_conflictingTypeVariableAndMember_getter() async {
Source source = addSource(r'''
class A<T> {
get T => null;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
verify([source]);
}
test_conflictingTypeVariableAndMember_method() async {
Source source = addSource(r'''
class A<T> {
T() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
verify([source]);
}
test_conflictingTypeVariableAndMember_method_static() async {
Source source = addSource(r'''
class A<T> {
static T() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
verify([source]);
}
test_conflictingTypeVariableAndMember_setter() async {
Source source = addSource(r'''
class A<T> {
set T(x) {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER]);
verify([source]);
}
test_consistentCaseExpressionTypes_dynamic() async {
// Even though A.S and S have a static type of "dynamic", we should see
// that they match 'abc', because they are constant strings.
Source source = addSource(r'''
class A {
static const S = 'A.S';
}
const S = 'S';
foo(var p) {
switch (p) {
case S:
break;
case A.S:
break;
case 'abc':
break;
}
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_const_invalid_constructorFieldInitializer_fromLibrary() async {
addNamedSource('/lib.dart', r'''
class A<T> {
final int f;
const A() : f = T.foo;
}
''');
Source source = addSource(r'''
import 'lib.dart';
const a = const A();
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
}
test_constConstructor_redirect_generic() async {
Source source = addSource(r'''
class A<T> {
const A(T value) : this._(value);
const A._(T value) : value = value;
final T value;
}
void main(){
const A<int>(1);
}
''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constConstructorWithFieldInitializedByNonConst() async {
Source source = addSource(r'''
class A {
final int i = f();
const A();
}
int f() {
return 3;
}''');
// TODO(paulberry): the error CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE is
// redundant and ought to be suppressed.
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode
.CONST_CONSTRUCTOR_WITH_FIELD_INITIALIZED_BY_NON_CONST,
CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE
]);
verify([source]);
}
test_constConstructorWithFieldInitializedByNonConst_static() async {
Source source = addSource(r'''
class A {
static final int i = f();
const A();
}
int f() {
return 3;
}''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constConstructorWithMixin() async {
Source source = addSource(r'''
class M {
}
class A extends Object with M {
const A();
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_MIXIN]);
verify([source]);
}
test_constConstructorWithNonConstSuper_explicit() async {
Source source = addSource(r'''
class A {
A();
}
class B extends A {
const B(): super();
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER]);
verify([source]);
}
test_constConstructorWithNonConstSuper_implicit() async {
Source source = addSource(r'''
class A {
A();
}
class B extends A {
const B();
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER]);
verify([source]);
}
test_constConstructorWithNonFinalField_mixin() async {
Source source = addSource(r'''
class A {
var a;
}
class B extends Object with A {
const B();
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_MIXIN,
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD
]);
verify([source]);
}
test_constConstructorWithNonFinalField_super() async {
Source source = addSource(r'''
class A {
var a;
}
class B extends A {
const B();
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD,
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER
]);
verify([source]);
}
test_constConstructorWithNonFinalField_this() async {
Source source = addSource(r'''
class A {
int x;
const A();
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD]);
verify([source]);
}
test_constDeferredClass() async {
await resolveWithErrors(<String>[
r'''
library lib1;
class A {
const A();
}''',
r'''
library root;
import 'lib1.dart' deferred as a;
main() {
const a.A();
}'''
], <ErrorCode>[
CompileTimeErrorCode.CONST_DEFERRED_CLASS
]);
}
test_constDeferredClass_namedConstructor() async {
await resolveWithErrors(<String>[
r'''
library lib1;
class A {
const A.b();
}''',
r'''
library root;
import 'lib1.dart' deferred as a;
main() {
const a.A.b();
}'''
], <ErrorCode>[
CompileTimeErrorCode.CONST_DEFERRED_CLASS
]);
}
test_constEval_newInstance_constConstructor() async {
Source source = addSource(r'''
class A {
const A();
}
const a = new A();''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
verify([source]);
}
test_constEval_newInstance_externalFactoryConstConstructor() async {
// We can't evaluate "const A()" because its constructor is external. But
// the code is correct--we shouldn't report an error.
Source source = addSource(r'''
class A {
external factory const A();
}
const x = const A();''');
await computeAnalysisResult(source);
assertNoErrors(source);
verify([source]);
}
test_constEval_nonStaticField_inGenericClass() async {
Source source = addSource('''
class C<T> {
const C();
T get t => null;
}
const x = const C().t;''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
verify([source]);
}
test_constEval_propertyExtraction_targetNotConst() async {
Source source = addSource(r'''
class A {
const A();
m() {}
}
final a = const A();
const C = a.m;''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
verify([source]);
}
test_constEvalThrowsException_binaryMinus_null() async {
await _check_constEvalThrowsException_binary_null("null - 5", false);
await _check_constEvalThrowsException_binary_null("5 - null", true);
}
test_constEvalThrowsException_binaryPlus_null() async {
await _check_constEvalThrowsException_binary_null("null + 5", false);
await _check_constEvalThrowsException_binary_null("5 + null", true);
}
test_constEvalThrowsException_divisionByZero() async {
Source source = addSource("const C = 1 ~/ 0;");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_IDBZE]);
verify([source]);
}
test_constEvalThrowsException_finalAlreadySet_initializer() async {
// If a final variable has an initializer at the site of its declaration,
// and at the site of the constructor, then invoking that constructor would
// produce a runtime error; hence invoking that constructor via the "const"
// keyword results in a compile-time error.
Source source = addSource('''
class C {
final x = 1;
const C() : x = 2;
}
var x = const C();
''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
StaticWarningCode.FIELD_INITIALIZED_IN_INITIALIZER_AND_DECLARATION
]);
verify([source]);
}
test_constEvalThrowsException_finalAlreadySet_initializing_formal() async {
// If a final variable has an initializer at the site of its declaration,
// and it is initialized using an initializing formal at the site of the
// constructor, then invoking that constructor would produce a runtime
// error; hence invoking that constructor via the "const" keyword results
// in a compile-time error.
Source source = addSource('''
class C {
final x = 1;
const C(this.x);
}
var x = const C(2);
''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
StaticWarningCode.FINAL_INITIALIZED_IN_DECLARATION_AND_CONSTRUCTOR
]);
verify([source]);
}
test_constEvalThrowsException_unaryBitNot_null() async {
Source source = addSource("const C = ~null;");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
// no verify(), '~null' is not resolved
}
test_constEvalThrowsException_unaryNegated_null() async {
Source source = addSource("const C = -null;");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
// no verify(), '-null' is not resolved
}
test_constEvalThrowsException_unaryNot_null() async {
Source source = addSource("const C = !null;");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION]);
verify([source]);
}
test_constEvalTypeBool_binary() async {
await _check_constEvalTypeBool_withParameter_binary("p && ''");
await _check_constEvalTypeBool_withParameter_binary("p || ''");
}
test_constEvalTypeBool_binary_leftTrue() async {
Source source = addSource("const C = (true || 0);");
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL,
StaticTypeWarningCode.NON_BOOL_OPERAND,
HintCode.DEAD_CODE
]);
verify([source]);
}
test_constEvalTypeBoolNumString_equal() async {
Source source = addSource(r'''
class A {
const A();
}
class B {
final a;
const B(num p) : a = p == const A();
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_NUM_STRING]);
verify([source]);
}
test_constEvalTypeBoolNumString_notEqual() async {
Source source = addSource(r'''
class A {
const A();
}
class B {
final a;
const B(String p) : a = p != const A();
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_NUM_STRING]);
verify([source]);
}
test_constEvalTypeInt_binary() async {
await _check_constEvalTypeInt_withParameter_binary("p ^ ''");
await _check_constEvalTypeInt_withParameter_binary("p & ''");
await _check_constEvalTypeInt_withParameter_binary("p | ''");
await _check_constEvalTypeInt_withParameter_binary("p >> ''");
await _check_constEvalTypeInt_withParameter_binary("p << ''");
}
test_constEvalTypeNum_binary() async {
await _check_constEvalTypeNum_withParameter_binary("p + ''");
await _check_constEvalTypeNum_withParameter_binary("p - ''");
await _check_constEvalTypeNum_withParameter_binary("p * ''");
await _check_constEvalTypeNum_withParameter_binary("p / ''");
await _check_constEvalTypeNum_withParameter_binary("p ~/ ''");
await _check_constEvalTypeNum_withParameter_binary("p > ''");
await _check_constEvalTypeNum_withParameter_binary("p < ''");
await _check_constEvalTypeNum_withParameter_binary("p >= ''");
await _check_constEvalTypeNum_withParameter_binary("p <= ''");
await _check_constEvalTypeNum_withParameter_binary("p % ''");
}
test_constFormalParameter_fieldFormalParameter() async {
Source source = addSource(r'''
class A {
var x;
A(const this.x) {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_FORMAL_PARAMETER]);
verify([source]);
}
test_constFormalParameter_simpleFormalParameter() async {
Source source = addSource("f(const x) {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_FORMAL_PARAMETER]);
verify([source]);
}
test_constInitializedWithNonConstValue() async {
Source source = addSource(r'''
f(p) {
const C = p;
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
verify([source]);
}
test_constInitializedWithNonConstValue_finalField() async {
// Regression test for bug #25526 which previously
// caused two errors to be reported.
Source source = addSource(r'''
class Foo {
final field = [];
foo([int x = field]) {}
}
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE]);
verify([source]);
}
test_constInitializedWithNonConstValue_missingConstInListLiteral() async {
Source source = addSource("const List L = [0];");
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
verify([source]);
}
test_constInitializedWithNonConstValue_missingConstInMapLiteral() async {
Source source = addSource("const Map M = {'a' : 0};");
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE]);
verify([source]);
}
test_constInitializedWithNonConstValueFromDeferredClass() async {
await resolveWithErrors(<String>[
r'''
library lib1;
const V = 1;''',
r'''
library root;
import 'lib1.dart' deferred as a;
const B = a.V;'''
], <ErrorCode>[
CompileTimeErrorCode
.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE_FROM_DEFERRED_LIBRARY
]);
}
test_constInitializedWithNonConstValueFromDeferredClass_nested() async {
await resolveWithErrors(<String>[
r'''
library lib1;
const V = 1;''',
r'''
library root;
import 'lib1.dart' deferred as a;
const B = a.V + 1;'''
], <ErrorCode>[
CompileTimeErrorCode
.CONST_INITIALIZED_WITH_NON_CONSTANT_VALUE_FROM_DEFERRED_LIBRARY
]);
}
test_constInstanceField() async {
Source source = addSource(r'''
class C {
const int f = 0;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_INSTANCE_FIELD]);
verify([source]);
}
test_constMapKeyTypeImplementsEquals_direct() async {
Source source = addSource(r'''
class A {
const A();
operator ==(other) => false;
}
main() {
const {const A() : 0};
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
verify([source]);
}
test_constMapKeyTypeImplementsEquals_dynamic() async {
// Note: static type of B.a is "dynamic", but actual type of the const
// object is A. We need to make sure we examine the actual type when
// deciding whether there is a problem with operator==.
Source source = addSource(r'''
class A {
const A();
operator ==(other) => false;
}
class B {
static const a = const A();
}
main() {
const {B.a : 0};
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
verify([source]);
}
test_constMapKeyTypeImplementsEquals_factory() async {
Source source = addSource(r'''
class A { const factory A() = B; }
class B implements A {
const B();
operator ==(o) => true;
}
main() {
var m = const { const A(): 42 };
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
verify([source]);
}
test_constMapKeyTypeImplementsEquals_super() async {
Source source = addSource(r'''
class A {
const A();
operator ==(other) => false;
}
class B extends A {
const B();
}
main() {
const {const B() : 0};
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_MAP_KEY_EXPRESSION_TYPE_IMPLEMENTS_EQUALS]);
verify([source]);
}
test_constWithInvalidTypeParameters() async {
Source source = addSource(r'''
class A {
const A();
}
f() { return const A<A>(); }''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS]);
verify([source]);
}
test_constWithInvalidTypeParameters_tooFew() async {
Source source = addSource(r'''
class A {}
class C<K, V> {
const C();
}
f(p) {
return const C<A>();
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS]);
verify([source]);
}
test_constWithInvalidTypeParameters_tooMany() async {
Source source = addSource(r'''
class A {}
class C<E> {
const C();
}
f(p) {
return const C<A, A>();
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_WITH_INVALID_TYPE_PARAMETERS]);
verify([source]);
}
test_constWithNonConst() async {
Source source = addSource(r'''
class T {
T(a, b, {c, d}) {}
}
f() { return const T(0, 1, c: 2, d: 3); }''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_WITH_NON_CONST]);
verify([source]);
}
test_constWithNonConst_with() async {
Source source = addSource(r'''
class B {
const B();
}
class C = B with M;
class M {}
const x = const C();
main() {
print(x);
}
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_WITH_NON_CONST]);
verify([source]);
}
test_constWithNonConstantArgument_annotation() async {
Source source = addSource(r'''
class A {
const A(int p);
}
var v = 42;
@A(v)
main() {
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_WITH_NON_CONSTANT_ARGUMENT]);
verify([source]);
}
test_constWithNonConstantArgument_instanceCreation() async {
Source source = addSource(r'''
class A {
const A(a);
}
f(p) { return const A(p); }''');
// TODO(paulberry): the error INVALID_CONSTAT is redundant and ought to be
// suppressed.
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_WITH_NON_CONSTANT_ARGUMENT,
CompileTimeErrorCode.INVALID_CONSTANT
]);
verify([source]);
}
test_constWithNonType() async {
Source source = addSource(r'''
int A;
f() {
return const A();
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_WITH_NON_TYPE]);
verify([source]);
}
test_constWithNonType_fromLibrary() async {
Source source1 = addNamedSource("/lib.dart", "");
Source source2 = addNamedSource("/lib2.dart", r'''
import 'lib.dart' as lib;
void f() {
const lib.A();
}''');
await computeAnalysisResult(source1);
await computeAnalysisResult(source2);
assertErrors(source2, [CompileTimeErrorCode.CONST_WITH_NON_TYPE]);
verify([source1]);
}
test_constWithTypeParameters_direct() async {
Source source = addSource(r'''
class A<T> {
static const V = const A<T>();
const A();
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS,
StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC
]);
verify([source]);
}
test_constWithTypeParameters_indirect() async {
Source source = addSource(r'''
class A<T> {
static const V = const A<List<T>>();
const A();
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS,
StaticWarningCode.TYPE_PARAMETER_REFERENCED_BY_STATIC
]);
verify([source]);
}
test_constWithUndefinedConstructor() async {
Source source = addSource(r'''
class A {
const A();
}
f() {
return const A.noSuchConstructor();
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR]);
// no verify(), 'noSuchConstructor' is not resolved
}
test_constWithUndefinedConstructorDefault() async {
Source source = addSource(r'''
class A {
const A.name();
}
f() {
return const A();
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT]);
verify([source]);
}
test_defaultValueInFunctionTypeAlias() async {
Source source = addSource("typedef F([x = 0]);");
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPE_ALIAS]);
verify([source]);
}
test_defaultValueInFunctionTypedParameter_named() async {
Source source = addSource("f(g({p: null})) {}");
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPED_PARAMETER]);
verify([source]);
}
test_defaultValueInFunctionTypedParameter_optional() async {
Source source = addSource("f(g([p = null])) {}");
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.DEFAULT_VALUE_IN_FUNCTION_TYPED_PARAMETER]);
verify([source]);
}
test_defaultValueInRedirectingFactoryConstructor() async {
Source source = addSource(r'''
class A {
factory A([int x = 0]) = B;
}
class B implements A {
B([int x = 1]) {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.DEFAULT_VALUE_IN_REDIRECTING_FACTORY_CONSTRUCTOR
]);
verify([source]);
}
test_deferredImportWithInvalidUri() async {
Source source = addSource(r'''
import '[invalid uri]' deferred as p;
main() {
p.loadLibrary();
}''');
await computeAnalysisResult(source);
if (enableNewAnalysisDriver) {
assertErrors(source, [CompileTimeErrorCode.URI_DOES_NOT_EXIST]);
} else {
assertErrors(source, [
CompileTimeErrorCode.URI_DOES_NOT_EXIST,
StaticWarningCode.UNDEFINED_IDENTIFIER
]);
}
}
test_duplicateConstructorName_named() async {
Source source = addSource(r'''
class A {
A.a() {}
A.a() {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_NAME,
CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_NAME
]);
verify([source]);
}
test_duplicateConstructorName_unnamed() async {
Source source = addSource(r'''
class A {
A() {}
A() {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT,
CompileTimeErrorCode.DUPLICATE_CONSTRUCTOR_DEFAULT
]);
verify([source]);
}
test_duplicateDefinition_acrossLibraries() async {
Source librarySource = addNamedSource("/lib.dart", r'''
library lib;
part 'a.dart';
part 'b.dart';''');
Source sourceA = addNamedSource("/a.dart", r'''
part of lib;
class A {}''');
Source sourceB = addNamedSource("/b.dart", r'''
part of lib;
class A {}''');
await computeAnalysisResult(librarySource);
await computeAnalysisResult(sourceA);
await computeAnalysisResult(sourceB);
assertNoErrors(librarySource);
assertErrors(sourceB, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([librarySource, sourceA, sourceB]);
}
test_duplicateDefinition_catch() async {
Source source = addSource(r'''
main() {
try {} catch (e, e) {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_classMembers_fields() async {
Source source = addSource(r'''
class A {
int a;
int a;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_classMembers_fields_oneStatic() async {
Source source = addSource(r'''
class A {
int x;
static int x;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_classMembers_methods() async {
Source source = addSource(r'''
class A {
m() {}
m() {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_inPart() async {
Source librarySource = addNamedSource("/lib.dart", r'''
library test;
part 'a.dart';
class A {}''');
Source sourceA = addNamedSource("/a.dart", r'''
part of test;
class A {}''');
await computeAnalysisResult(librarySource);
await computeAnalysisResult(sourceA);
assertNoErrors(librarySource);
assertErrors(sourceA, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([librarySource, sourceA]);
}
test_duplicateDefinition_locals_inCase() async {
Source source = addSource(r'''
main() {
switch(1) {
case 1:
var a;
var a;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_locals_inFunctionBlock() async {
Source source = addSource(r'''
main() {
int m = 0;
m(a) {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_locals_inIf() async {
Source source = addSource(r'''
main(int p) {
if (p != 0) {
var a;
var a;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_locals_inMethodBlock() async {
Source source = addSource(r'''
class A {
m() {
int a;
int a;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_parameters_inConstructor() async {
Source source = addSource(r'''
class A {
int a;
A(int a, this.a);
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_parameters_inFunctionTypeAlias() async {
Source source = addSource(r'''
typedef F(int a, double a);
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_parameters_inLocalFunction() async {
Source source = addSource(r'''
main() {
f(int a, double a) {
};
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_parameters_inMethod() async {
Source source = addSource(r'''
class A {
m(int a, double a) {
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_parameters_inTopLevelFunction() async {
Source source = addSource(r'''
f(int a, double a) {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinition_typeParameters() async {
Source source = addSource(r'''
class A<T, T> {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_DEFINITION]);
verify([source]);
}
test_duplicateDefinitionInheritance_instanceGetter_staticGetter() async {
Source source = addSource(r'''
class A {
int get x => 0;
}
class B extends A {
static int get x => 0;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
verify([source]);
}
test_duplicateDefinitionInheritance_instanceGetterAbstract_staticGetter() async {
Source source = addSource(r'''
abstract class A {
int get x;
}
class B extends A {
static int get x => 0;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
verify([source]);
}
test_duplicateDefinitionInheritance_instanceMethod_staticMethod() async {
Source source = addSource(r'''
class A {
x() {}
}
class B extends A {
static x() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
verify([source]);
}
test_duplicateDefinitionInheritance_instanceMethodAbstract_staticMethod() async {
Source source = addSource(r'''
abstract class A {
x();
}
abstract class B extends A {
static x() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
verify([source]);
}
test_duplicateDefinitionInheritance_instanceSetter_staticSetter() async {
Source source = addSource(r'''
class A {
set x(value) {}
}
class B extends A {
static set x(value) {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
verify([source]);
}
test_duplicateDefinitionInheritance_instanceSetterAbstract_staticSetter() async {
Source source = addSource(r'''
abstract class A {
set x(value);
}
class B extends A {
static set x(value) {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.DUPLICATE_DEFINITION_INHERITANCE]);
verify([source]);
}
test_duplicateNamedArgument() async {
Source source = addSource(r'''
f({a, b}) {}
main() {
f(a: 1, a: 2);
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_NAMED_ARGUMENT]);
verify([source]);
}
test_duplicatePart_sameSource() async {
addNamedSource('/part.dart', 'part of lib;');
Source source = addSource(r'''
library lib;
part 'part.dart';
part 'foo/../part.dart';
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_PART]);
verify([source]);
}
test_duplicatePart_sameUri() async {
addNamedSource('/part.dart', 'part of lib;');
Source source = addSource(r'''
library lib;
part 'part.dart';
part 'part.dart';
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.DUPLICATE_PART]);
verify([source]);
}
test_exportInternalLibrary() async {
Source source = addSource("export 'dart:_interceptors';");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXPORT_INTERNAL_LIBRARY]);
verify([source]);
}
test_exportOfNonLibrary() async {
Source source = addSource(r'''
library L;
export 'lib1.dart';''');
addNamedSource("/lib1.dart", "part of lib;");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXPORT_OF_NON_LIBRARY]);
verify([source]);
}
test_extendsDeferredClass() async {
await resolveWithErrors(<String>[
r'''
library lib1;
class A {}''',
r'''
library root;
import 'lib1.dart' deferred as a;
class B extends a.A {}'''
], <ErrorCode>[
CompileTimeErrorCode.EXTENDS_DEFERRED_CLASS
]);
}
test_extendsDeferredClass_classTypeAlias() async {
await resolveWithErrors(<String>[
r'''
library lib1;
class A {}''',
r'''
library root;
import 'lib1.dart' deferred as a;
class M {}
class C = a.A with M;'''
], <ErrorCode>[
CompileTimeErrorCode.EXTENDS_DEFERRED_CLASS
]);
}
test_extendsDisallowedClass_class_bool() async {
Source source = addSource("class A extends bool {}");
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
]);
verify([source]);
}
test_extendsDisallowedClass_class_double() async {
Source source = addSource("class A extends double {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
verify([source]);
}
test_extendsDisallowedClass_class_int() async {
Source source = addSource("class A extends int {}");
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
]);
verify([source]);
}
test_extendsDisallowedClass_class_Null() async {
Source source = addSource("class A extends Null {}");
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
]);
verify([source]);
}
test_extendsDisallowedClass_class_num() async {
Source source = addSource("class A extends num {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
verify([source]);
}
test_extendsDisallowedClass_class_String() async {
Source source = addSource("class A extends String {}");
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS,
CompileTimeErrorCode.NO_DEFAULT_SUPER_CONSTRUCTOR_IMPLICIT
]);
verify([source]);
}
test_extendsDisallowedClass_classTypeAlias_bool() async {
Source source = addSource(r'''
class M {}
class C = bool with M;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
verify([source]);
}
test_extendsDisallowedClass_classTypeAlias_double() async {
Source source = addSource(r'''
class M {}
class C = double with M;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
verify([source]);
}
test_extendsDisallowedClass_classTypeAlias_int() async {
Source source = addSource(r'''
class M {}
class C = int with M;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
verify([source]);
}
test_extendsDisallowedClass_classTypeAlias_Null() async {
Source source = addSource(r'''
class M {}
class C = Null with M;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
verify([source]);
}
test_extendsDisallowedClass_classTypeAlias_num() async {
Source source = addSource(r'''
class M {}
class C = num with M;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
verify([source]);
}
test_extendsDisallowedClass_classTypeAlias_String() async {
Source source = addSource(r'''
class M {}
class C = String with M;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS]);
verify([source]);
}
test_extendsEnum() async {
Source source = addSource(r'''
enum E { ONE }
class A extends E {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_ENUM]);
verify([source]);
}
test_extendsNonClass_class() async {
Source source = addSource(r'''
int A;
class B extends A {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_NON_CLASS]);
verify([source]);
}
test_extendsNonClass_dynamic() async {
Source source = addSource("class B extends dynamic {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTENDS_NON_CLASS]);
verify([source]);
}
test_extraPositionalArguments_const() async {
Source source = addSource(r'''
class A {
const A();
}
main() {
const A(0);
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS]);
verify([source]);
}
test_extraPositionalArguments_const_super() async {
Source source = addSource(r'''
class A {
const A();
}
class B extends A {
const B() : super(0);
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS]);
verify([source]);
}
test_extraPositionalArgumentsCouldBeNamed_const() async {
Source source = addSource(r'''
class A {
const A({int x});
}
main() {
const A(0);
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS_COULD_BE_NAMED]);
verify([source]);
}
test_extraPositionalArgumentsCouldBeNamed_const_super() async {
Source source = addSource(r'''
class A {
const A({int x});
}
class B extends A {
const B() : super(0);
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS_COULD_BE_NAMED]);
verify([source]);
}
test_fieldFormalParameter_assignedInInitializer() async {
Source source = addSource(r'''
class A {
int x;
A(this.x) : x = 3 {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER]);
verify([source]);
}
test_fieldInitializedByMultipleInitializers() async {
Source source = addSource(r'''
class A {
int x;
A() : x = 0, x = 1 {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS]);
verify([source]);
}
test_fieldInitializedByMultipleInitializers_multipleInits() async {
Source source = addSource(r'''
class A {
int x;
A() : x = 0, x = 1, x = 2 {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS,
CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS
]);
verify([source]);
}
test_fieldInitializedByMultipleInitializers_multipleNames() async {
Source source = addSource(r'''
class A {
int x;
int y;
A() : x = 0, x = 1, y = 0, y = 1 {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS,
CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS
]);
verify([source]);
}
test_fieldInitializedInParameterAndInitializer() async {
Source source = addSource(r'''
class A {
int x;
A(this.x) : x = 1 {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER]);
verify([source]);
}
test_fieldInitializerFactoryConstructor() async {
Source source = addSource(r'''
class A {
int x;
factory A(this.x) => null;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.FIELD_INITIALIZER_FACTORY_CONSTRUCTOR]);
verify([source]);
}
test_fieldInitializerOutsideConstructor() async {
// TODO(brianwilkerson) Fix the duplicate error messages.
Source source = addSource(r'''
class A {
int x;
m(this.x) {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
ParserErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR,
CompileTimeErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR
]);
verify([source]);
}
test_fieldInitializerOutsideConstructor_defaultParameter() async {
Source source = addSource(r'''
class A {
int x;
m([this.x]) {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR]);
verify([source]);
}
test_fieldInitializerOutsideConstructor_inFunctionTypeParameter() async {
Source source = addSource(r'''
class A {
int x;
A(int p(this.x));
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR]);
verify([source]);
}
test_fieldInitializerRedirectingConstructor_afterRedirection() async {
Source source = addSource(r'''
class A {
int x;
A.named() {}
A() : this.named(), x = 42;
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR]);
verify([source]);
}
test_fieldInitializerRedirectingConstructor_beforeRedirection() async {
Source source = addSource(r'''
class A {
int x;
A.named() {}
A() : x = 42, this.named();
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR]);
verify([source]);
}
test_fieldInitializingFormalRedirectingConstructor() async {
Source source = addSource(r'''
class A {
int x;
A.named() {}
A(this.x) : this.named();
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR]);
verify([source]);
}
test_finalInitializedMultipleTimes_initializers() async {
Source source = addSource(r'''
class A {
final x;
A() : x = 0, x = 0 {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.FIELD_INITIALIZED_BY_MULTIPLE_INITIALIZERS]);
verify([source]);
}
/**
* This test doesn't test the FINAL_INITIALIZED_MULTIPLE_TIMES code, but tests the
* FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER code instead. It is provided here to show
* coverage over all of the permutations of initializers in constructor declarations.
*
* Note: FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER covers a subset of
* FINAL_INITIALIZED_MULTIPLE_TIMES, since it more specific, we use it instead of the broader code
*/
test_finalInitializedMultipleTimes_initializingFormal_initializer() async {
Source source = addSource(r'''
class A {
final x;
A(this.x) : x = 0 {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.FIELD_INITIALIZED_IN_PARAMETER_AND_INITIALIZER]);
verify([source]);
}
test_finalInitializedMultipleTimes_initializingFormals() async {
Source source = addSource(r'''
class A {
final x;
A(this.x, this.x) {}
}''');
// TODO(brianwilkerson) There should only be one error here.
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.DUPLICATE_DEFINITION,
CompileTimeErrorCode.FINAL_INITIALIZED_MULTIPLE_TIMES
]);
verify([source]);
}
test_finalNotInitialized_instanceField_const_static() async {
Source source = addSource(r'''
class A {
static const F;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_NOT_INITIALIZED]);
verify([source]);
}
test_finalNotInitialized_library_const() async {
Source source = addSource("const F;");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_NOT_INITIALIZED]);
verify([source]);
}
test_finalNotInitialized_local_const() async {
Source source = addSource(r'''
f() {
const int x;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.CONST_NOT_INITIALIZED]);
verify([source]);
}
test_fromEnvironment_bool_badArgs() async {
Source source = addSource(r'''
var b1 = const bool.fromEnvironment(1);
var b2 = const bool.fromEnvironment('x', defaultValue: 1);''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE,
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
]);
verify([source]);
}
test_fromEnvironment_bool_badDefault_whenDefined() async {
// The type of the defaultValue needs to be correct even when the default
// value isn't used (because the variable is defined in the environment).
if (enableNewAnalysisDriver) {
driver.declaredVariables.define("x", "true");
} else {
analysisContext2.declaredVariables.define("x", "true");
}
Source source =
addSource("var b = const bool.fromEnvironment('x', defaultValue: 1);");
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION,
StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE
]);
verify([source]);
}
test_genericFunctionTypedParameter() async {
// Once dartbug.com/28515 is fixed, this syntax should no longer generate an
// error.
// TODO(paulberry): When dartbug.com/28515 is fixed, convert this into a
// NonErrorResolverTest.
Source source = addSource('void g(T f<T>(T x)) {}');
await computeAnalysisResult(source);
var expectedErrorCodes = <ErrorCode>[
CompileTimeErrorCode.GENERIC_FUNCTION_TYPED_PARAM_UNSUPPORTED
];
if (enableNewAnalysisDriver) {
// Due to dartbug.com/28515, some additional errors appear when using the
// new analysis driver.
expectedErrorCodes.addAll([
StaticWarningCode.UNDEFINED_CLASS,
StaticWarningCode.UNDEFINED_CLASS
]);
}
assertErrors(source, expectedErrorCodes);
verify([source]);
}
test_getterAndMethodWithSameName() async {
Source source = addSource(r'''
class A {
x(y) {}
get x => 0;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.GETTER_AND_METHOD_WITH_SAME_NAME]);
verify([source]);
}
test_implementsDeferredClass() async {
await resolveWithErrors(<String>[
r'''
library lib1;
class A {}''',
r'''
library root;
import 'lib1.dart' deferred as a;
class B implements a.A {}'''
], <ErrorCode>[
CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS
]);
}
test_implementsDeferredClass_classTypeAlias() async {
await resolveWithErrors(<String>[
r'''
library lib1;
class A {}''',
r'''
library root;
import 'lib1.dart' deferred as a;
class B {}
class M {}
class C = B with M implements a.A;'''
], <ErrorCode>[
CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS
]);
}
test_implementsDisallowedClass_class_bool() async {
Source source = addSource("class A implements bool {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_class_double() async {
Source source = addSource("class A implements double {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_class_int() async {
Source source = addSource("class A implements int {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_class_Null() async {
Source source = addSource("class A implements Null {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_class_num() async {
Source source = addSource("class A implements num {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_class_String() async {
Source source = addSource("class A implements String {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_class_String_num() async {
Source source = addSource("class A implements String, num {}");
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS,
CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS
]);
verify([source]);
}
test_implementsDisallowedClass_classTypeAlias_bool() async {
Source source = addSource(r'''
class A {}
class M {}
class C = A with M implements bool;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_classTypeAlias_double() async {
Source source = addSource(r'''
class A {}
class M {}
class C = A with M implements double;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_classTypeAlias_int() async {
Source source = addSource(r'''
class A {}
class M {}
class C = A with M implements int;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_classTypeAlias_Null() async {
Source source = addSource(r'''
class A {}
class M {}
class C = A with M implements Null;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_classTypeAlias_num() async {
Source source = addSource(r'''
class A {}
class M {}
class C = A with M implements num;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_classTypeAlias_String() async {
Source source = addSource(r'''
class A {}
class M {}
class C = A with M implements String;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS]);
verify([source]);
}
test_implementsDisallowedClass_classTypeAlias_String_num() async {
Source source = addSource(r'''
class A {}
class M {}
class C = A with M implements String, num;''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS,
CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS
]);
verify([source]);
}
test_implementsDynamic() async {
Source source = addSource("class A implements dynamic {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_DYNAMIC]);
verify([source]);
}
test_implementsEnum() async {
Source source = addSource(r'''
enum E { ONE }
class A implements E {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_ENUM]);
verify([source]);
}
test_implementsNonClass_class() async {
Source source = addSource(r'''
int A;
class B implements A {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_NON_CLASS]);
verify([source]);
}
test_implementsNonClass_typeAlias() async {
Source source = addSource(r'''
class A {}
class M {}
int B;
class C = A with M implements B;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_NON_CLASS]);
verify([source]);
}
test_implementsRepeated() async {
Source source = addSource(r'''
class A {}
class B implements A, A {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_REPEATED]);
verify([source]);
}
test_implementsRepeated_3times() async {
Source source = addSource(r'''
class A {} class C{}
class B implements A, A, A, A {}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.IMPLEMENTS_REPEATED,
CompileTimeErrorCode.IMPLEMENTS_REPEATED,
CompileTimeErrorCode.IMPLEMENTS_REPEATED
]);
verify([source]);
}
test_implementsSuperClass() async {
Source source = addSource(r'''
class A {}
class B extends A implements A {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_SUPER_CLASS]);
verify([source]);
}
test_implementsSuperClass_Object() async {
Source source = addSource("class A implements Object {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_SUPER_CLASS]);
verify([source]);
}
test_implementsSuperClass_Object_typeAlias() async {
Source source = addSource(r'''
class M {}
class A = Object with M implements Object;
''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_SUPER_CLASS]);
verify([source]);
}
test_implementsSuperClass_typeAlias() async {
Source source = addSource(r'''
class A {}
class M {}
class B = A with M implements A;''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPLEMENTS_SUPER_CLASS]);
verify([source]);
}
test_implicitThisReferenceInInitializer_field() async {
Source source = addSource(r'''
class A {
var v;
A() : v = f;
var f;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
verify([source]);
}
test_implicitThisReferenceInInitializer_field2() async {
Source source = addSource(r'''
class A {
final x = 0;
final y = x;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
verify([source]);
}
test_implicitThisReferenceInInitializer_invocation() async {
Source source = addSource(r'''
class A {
var v;
A() : v = f();
f() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
verify([source]);
}
test_implicitThisReferenceInInitializer_invocationInStatic() async {
Source source = addSource(r'''
class A {
static var F = m();
m() {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
verify([source]);
}
test_implicitThisReferenceInInitializer_redirectingConstructorInvocation() async {
Source source = addSource(r'''
class A {
A(p) {}
A.named() : this(f);
var f;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
verify([source]);
}
test_implicitThisReferenceInInitializer_superConstructorInvocation() async {
Source source = addSource(r'''
class A {
A(p) {}
}
class B extends A {
B() : super(f);
var f;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);
verify([source]);
}
test_importInternalLibrary() async {
Source source = addSource("import 'dart:_interceptors';");
// Note, in these error cases we may generate an UNUSED_IMPORT hint, while
// we could prevent the hint from being generated by testing the import
// directive for the error, this is such a minor corner case that we don't
// think we should add the additional computation time to figure out such
// cases.
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.IMPORT_INTERNAL_LIBRARY, HintCode.UNUSED_IMPORT]);
verify([source]);
}
test_importOfNonLibrary() async {
Source source = addSource(r'''
library lib;
import 'part.dart';
A a;''');
addNamedSource("/part.dart", r'''
part of lib;
class A{}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.IMPORT_OF_NON_LIBRARY]);
verify([source]);
}
test_inconsistentCaseExpressionTypes() async {
Source source = addSource(r'''
f(var p) {
switch (p) {
case 1:
break;
case 'a':
break;
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES]);
verify([source]);
}
test_inconsistentCaseExpressionTypes_dynamic() async {
// Even though A.S and S have a static type of "dynamic", we should see
// that they fail to match 3, because they are constant strings.
Source source = addSource(r'''
class A {
static const S = 'A.S';
}
const S = 'S';
foo(var p) {
switch (p) {
case 3:
break;
case S:
break;
case A.S:
break;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES,
CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES
]);
verify([source]);
}
test_inconsistentCaseExpressionTypes_repeated() async {
Source source = addSource(r'''
f(var p) {
switch (p) {
case 1:
break;
case 'a':
break;
case 'b':
break;
}
}''');
await computeAnalysisResult(source);
assertErrors(source, [
CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES,
CompileTimeErrorCode.INCONSISTENT_CASE_EXPRESSION_TYPES
]);
verify([source]);
}
test_initializerForNonExistent_const() async {
// Check that the absence of a matching field doesn't cause a
// crash during constant evaluation.
Source source = addSource(r'''
class A {
const A() : x = 'foo';
}
A a = const A();''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD]);
}
test_initializerForNonExistent_initializer() async {
Source source = addSource(r'''
class A {
A() : x = 0 {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD]);
}
test_initializerForStaticField() async {
Source source = addSource(r'''
class A {
static int x;
A() : x = 0 {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INITIALIZER_FOR_STATIC_FIELD]);
verify([source]);
}
test_initializingFormalForNonExistentField() async {
Source source = addSource(r'''
class A {
A(this.x) {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD]);
verify([source]);
}
test_initializingFormalForNonExistentField_notInEnclosingClass() async {
Source source = addSource(r'''
class A {
int x;
}
class B extends A {
B(this.x) {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD]);
verify([source]);
}
test_initializingFormalForNonExistentField_optional() async {
Source source = addSource(r'''
class A {
A([this.x]) {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD]);
verify([source]);
}
test_initializingFormalForNonExistentField_synthetic() async {
Source source = addSource(r'''
class A {
int get x => 1;
A(this.x) {}
}''');
await computeAnalysisResult(source);
assertErrors(source,
[CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_NON_EXISTENT_FIELD]);
verify([source]);
}
test_initializingFormalForStaticField() async {
Source source = addSource(r'''
class A {
static int x;
A([this.x]) {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INITIALIZING_FORMAL_FOR_STATIC_FIELD]);
verify([source]);
}
test_instanceMemberAccessFromFactory_named() async {
Source source = addSource(r'''
class A {
m() {}
A();
factory A.make() {
m();
return new A();
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY]);
verify([source]);
}
test_instanceMemberAccessFromFactory_unnamed() async {
Source source = addSource(r'''
class A {
m() {}
A._();
factory A() {
m();
return new A._();
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY]);
verify([source]);
}
test_instanceMemberAccessFromStatic_field() async {
Source source = addSource(r'''
class A {
int f;
static foo() {
f;
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC]);
verify([source]);
}
test_instanceMemberAccessFromStatic_getter() async {
Source source = addSource(r'''
class A {
get g => null;
static foo() {
g;
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC]);
verify([source]);
}
test_instanceMemberAccessFromStatic_method() async {
Source source = addSource(r'''
class A {
m() {}
static foo() {
m();
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC]);
verify([source]);
}
test_instantiateEnum_const() async {
Source source = addSource(r'''
enum E { ONE }
E e(String name) {
return const E();
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INSTANTIATE_ENUM]);
verify([source]);
}
test_instantiateEnum_new() async {
Source source = addSource(r'''
enum E { ONE }
E e(String name) {
return new E();
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INSTANTIATE_ENUM]);
verify([source]);
}
test_invalidAnnotation_getter() async {
Source source = addSource(r'''
get V => 0;
@V
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
verify([source]);
}
test_invalidAnnotation_importWithPrefix_getter() async {
addNamedSource("/lib.dart", r'''
library lib;
get V => 0;''');
Source source = addSource(r'''
import 'lib.dart' as p;
@p.V
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
verify([source]);
}
test_invalidAnnotation_importWithPrefix_notConstantVariable() async {
addNamedSource("/lib.dart", r'''
library lib;
final V = 0;''');
Source source = addSource(r'''
import 'lib.dart' as p;
@p.V
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
verify([source]);
}
test_invalidAnnotation_importWithPrefix_notVariableOrConstructorInvocation() async {
addNamedSource("/lib.dart", r'''
library lib;
typedef V();''');
Source source = addSource(r'''
import 'lib.dart' as p;
@p.V
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
verify([source]);
}
test_invalidAnnotation_notConstantVariable() async {
Source source = addSource(r'''
final V = 0;
@V
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
verify([source]);
}
test_invalidAnnotation_notVariableOrConstructorInvocation() async {
Source source = addSource(r'''
typedef V();
@V
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
verify([source]);
}
test_invalidAnnotation_staticMethodReference() async {
Source source = addSource(r'''
class A {
static f() {}
}
@A.f
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
verify([source]);
}
test_invalidAnnotation_unresolved_identifier() async {
Source source = addSource(r'''
@unresolved
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
}
test_invalidAnnotation_unresolved_invocation() async {
Source source = addSource(r'''
@Unresolved()
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
}
test_invalidAnnotation_unresolved_prefixedIdentifier() async {
Source source = addSource(r'''
import 'dart:math' as p;
@p.unresolved
main() {
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
}
test_invalidAnnotation_useLibraryScope() async {
Source source = addSource(r'''
@foo
class A {
static const foo = null;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_ANNOTATION]);
}
test_invalidAnnotationFromDeferredLibrary() async {
// See test_invalidAnnotation_notConstantVariable
await resolveWithErrors(<String>[
r'''
library lib1;
class V { const V(); }
const v = const V();''',
r'''
library root;
import 'lib1.dart' deferred as a;
@a.v main () {}'''
], <ErrorCode>[
CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY
]);
}
test_invalidAnnotationFromDeferredLibrary_constructor() async {
// See test_invalidAnnotation_notConstantVariable
await resolveWithErrors(<String>[
r'''
library lib1;
class C { const C(); }''',
r'''
library root;
import 'lib1.dart' deferred as a;
@a.C() main () {}'''
], <ErrorCode>[
CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY
]);
}
test_invalidAnnotationFromDeferredLibrary_namedConstructor() async {
// See test_invalidAnnotation_notConstantVariable
await resolveWithErrors(<String>[
r'''
library lib1;
class C { const C.name(); }''',
r'''
library root;
import 'lib1.dart' deferred as a;
@a.C.name() main () {}'''
], <ErrorCode>[
CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY
]);
}
test_invalidConstructorName_notEnclosingClassName_defined() async {
Source source = addSource(r'''
class A {
B() : super();
}
class B {}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_CONSTRUCTOR_NAME]);
// no verify() call, "B" is not resolved
}
test_invalidConstructorName_notEnclosingClassName_undefined() async {
Source source = addSource(r'''
class A {
B() : super();
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_CONSTRUCTOR_NAME]);
// no verify() call, "B" is not resolved
}
test_invalidFactoryNameNotAClass_notClassName() async {
Source source = addSource(r'''
int B;
class A {
factory B() => null;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INVALID_FACTORY_NAME_NOT_A_CLASS]);
verify([source]);
}
test_invalidFactoryNameNotAClass_notEnclosingClassName() async {
Source source = addSource(r'''
class A {
factory B() => null;
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INVALID_FACTORY_NAME_NOT_A_CLASS]);
// no verify() call, "B" is not resolved
}
test_invalidModifierOnConstructor_async() async {
Source source = addSource(r'''
class A {
A() async {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR]);
verify([source]);
}
test_invalidModifierOnConstructor_asyncStar() async {
Source source = addSource(r'''
class A {
A() async* {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR]);
verify([source]);
}
test_invalidModifierOnConstructor_syncStar() async {
Source source = addSource(r'''
class A {
A() sync* {}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR]);
verify([source]);
}
test_invalidModifierOnSetter_member_async() async {
Source source = addSource(r'''
class A {
set x(v) async {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
verify([source]);
}
test_invalidModifierOnSetter_member_asyncStar() async {
Source source = addSource(r'''
class A {
set x(v) async* {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
verify([source]);
}
test_invalidModifierOnSetter_member_syncStar() async {
Source source = addSource(r'''
class A {
set x(v) sync* {}
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
verify([source]);
}
test_invalidModifierOnSetter_topLevel_async() async {
Source source = addSource("set x(v) async {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
verify([source]);
}
test_invalidModifierOnSetter_topLevel_asyncStar() async {
Source source = addSource("set x(v) async* {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
verify([source]);
}
test_invalidModifierOnSetter_topLevel_syncStar() async {
Source source = addSource("set x(v) sync* {}");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER]);
verify([source]);
}
test_invalidReferenceToThis_factoryConstructor() async {
Source source = addSource(r'''
class A {
factory A() { return this; }
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
verify([source]);
}
test_invalidReferenceToThis_instanceVariableInitializer_inConstructor() async {
Source source = addSource(r'''
class A {
var f;
A() : f = this;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
verify([source]);
}
test_invalidReferenceToThis_instanceVariableInitializer_inDeclaration() async {
Source source = addSource(r'''
class A {
var f = this;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
verify([source]);
}
test_invalidReferenceToThis_staticMethod() async {
Source source = addSource(r'''
class A {
static m() { return this; }
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
verify([source]);
}
test_invalidReferenceToThis_staticVariableInitializer() async {
Source source = addSource(r'''
class A {
static A f = this;
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
verify([source]);
}
test_invalidReferenceToThis_superInitializer() async {
Source source = addSource(r'''
class A {
A(var x) {}
}
class B extends A {
B() : super(this);
}''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
verify([source]);
}
test_invalidReferenceToThis_topLevelFunction() async {
Source source = addSource("f() { return this; }");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
verify([source]);
}
test_invalidReferenceToThis_variableInitializer() async {
Source source = addSource("int x = this;");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS]);
verify([source]);
}
test_invalidTypeArgumentInConstList() async {
Source source = addSource(r'''
class A<E> {
m() {
return const <E>[];
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_LIST]);
verify([source]);
}
test_invalidTypeArgumentInConstMap() async {
Source source = addSource(r'''
class A<E> {
m() {
return const <String, E>{};
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.INVALID_TYPE_ARGUMENT_IN_CONST_MAP]);
verify([source]);
}
test_invalidUri_export() async {
Source source = addSource("export 'ht:';");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_URI]);
}
test_invalidUri_import() async {
Source source = addSource("import 'ht:';");
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_URI]);
}
test_invalidUri_part() async {
Source source = addSource(r'''
library lib;
part 'ht:';''');
await computeAnalysisResult(source);
assertErrors(source, [CompileTimeErrorCode.INVALID_URI]);
}
test_isInConstInstanceCreation_restored() async {
// If ErrorVerifier._isInConstInstanceCreation is not properly restored on
// exit from visitInstanceCreationExpression, the error at (1) will be
// treated as a warning rather than an error.
Source source = addSource(r'''
class Foo<T extends num> {
const Foo(x, y);
}
const x = const Foo<int>(const Foo<int>(0, 1),
const <Foo<String>>[]); // (1)
''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.TYPE_ARGUMENT_NOT_MATCHING_BOUNDS]);
verify([source]);
}
test_isInInstanceVariableInitializer_restored() async {
// If ErrorVerifier._isInInstanceVariableInitializer is not properly
// restored on exit from visitVariableDeclaration, the error at (1)
// won't be detected.
Source source = addSource(r'''
class Foo {
var bar;
Map foo = {
'bar': () {
var _bar;
},
'bop': _foo // (1)
};
_foo() {
}
}''');
await computeAnalysisResult(source);
assertErrors(
source, [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER]);