OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library engine.resolver_test; | 5 library engine.resolver_test; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/src/context/context.dart'; | 9 import 'package:analyzer/src/context/context.dart'; |
10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
(...skipping 12954 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12965 g/*<S>*/(/*=S*/ x) => f; | 12965 g/*<S>*/(/*=S*/ x) => f; |
12966 return null; | 12966 return null; |
12967 } | 12967 } |
12968 '''); | 12968 '''); |
12969 SimpleIdentifier g = _findIdentifier('f'); | 12969 SimpleIdentifier g = _findIdentifier('f'); |
12970 expect(g.staticType.toString(), '<S>(S) → S'); | 12970 expect(g.staticType.toString(), '<S>(S) → S'); |
12971 SimpleIdentifier f = _findIdentifier('g'); | 12971 SimpleIdentifier f = _findIdentifier('g'); |
12972 expect(f.staticType.toString(), '<S>(S) → dynamic'); | 12972 expect(f.staticType.toString(), '<S>(S) → dynamic'); |
12973 } | 12973 } |
12974 | 12974 |
| 12975 void test_genericMethod_override() { |
| 12976 _resolveTestUnit(r''' |
| 12977 class C { |
| 12978 /*=T*/ f/*<T>*/(/*=T*/ x) => null; |
| 12979 } |
| 12980 class D extends C { |
| 12981 /*=T*/ f/*<T>*/(/*=T*/ x) => null; // from D |
| 12982 } |
| 12983 '''); |
| 12984 SimpleIdentifier f = |
| 12985 _findIdentifier('f/*<T>*/(/*=T*/ x) => null; // from D'); |
| 12986 MethodElementImpl e = f.staticElement; |
| 12987 expect(e.typeParameters.toString(), '[T]'); |
| 12988 expect(e.type.boundTypeParameters.toString(), '[T]'); |
| 12989 expect(e.type.toString(), '<T>(T) → T'); |
| 12990 |
| 12991 FunctionType ft = e.type.instantiate([typeProvider.stringType]); |
| 12992 expect(ft.toString(), '(String) → String'); |
| 12993 } |
| 12994 |
| 12995 void test_genericMethod_override_bounds() { |
| 12996 _resolveTestUnit(r''' |
| 12997 class A {} |
| 12998 class B extends A {} |
| 12999 class C { |
| 13000 /*=T*/ f/*<T extends B>*/(/*=T*/ x) => null; |
| 13001 } |
| 13002 class D extends C { |
| 13003 /*=T*/ f/*<T extends A>*/(/*=T*/ x) => null; |
| 13004 } |
| 13005 '''); |
| 13006 } |
| 13007 |
| 13008 void test_genericMethod_override_invalidReturnType() { |
| 13009 Source source = addSource(r''' |
| 13010 class C { |
| 13011 Iterable/*<T>*/ f/*<T>*/(/*=T*/ x) => null; |
| 13012 } |
| 13013 class D extends C { |
| 13014 String f/*<S>*/(/*=S*/ x) => null; |
| 13015 }'''); |
| 13016 // TODO(jmesserly): we can't use assertErrors because STRONG_MODE_* errors |
| 13017 // from CodeChecker don't have working equality. |
| 13018 List<AnalysisError> errors = analysisContext2.computeErrors(source); |
| 13019 expect(errors.map((e) => e.errorCode.name), [ |
| 13020 'INVALID_METHOD_OVERRIDE_RETURN_TYPE', |
| 13021 'STRONG_MODE_INVALID_METHOD_OVERRIDE' |
| 13022 ]); |
| 13023 expect(errors[0].message, contains('Iterable<S>'), |
| 13024 reason: 'errors should be in terms of the type parameters ' |
| 13025 'at the error location'); |
| 13026 verify([source]); |
| 13027 } |
| 13028 |
| 13029 void test_genericMethod_override_invalidTypeParamBounds() { |
| 13030 Source source = addSource(r''' |
| 13031 class A {} |
| 13032 class B extends A {} |
| 13033 class C { |
| 13034 /*=T*/ f/*<T extends A>*/(/*=T*/ x) => null; |
| 13035 } |
| 13036 class D extends C { |
| 13037 /*=T*/ f/*<T extends B>*/(/*=T*/ x) => null; |
| 13038 }'''); |
| 13039 // TODO(jmesserly): this is modified code from assertErrors, which we can't |
| 13040 // use directly because STRONG_MODE_* errors don't have working equality. |
| 13041 List<AnalysisError> errors = analysisContext2.computeErrors(source); |
| 13042 expect(errors.map((e) => e.errorCode.name), [ |
| 13043 'STRONG_MODE_INVALID_METHOD_OVERRIDE', |
| 13044 'INVALID_METHOD_OVERRIDE_TYPE_PARAMETER_BOUND' |
| 13045 ]); |
| 13046 verify([source]); |
| 13047 } |
| 13048 |
| 13049 void test_genericMethod_override_invalidTypeParamCount() { |
| 13050 Source source = addSource(r''' |
| 13051 class C { |
| 13052 /*=T*/ f/*<T>*/(/*=T*/ x) => null; |
| 13053 } |
| 13054 class D extends C { |
| 13055 /*=S*/ f/*<T, S>*/(/*=T*/ x) => null; |
| 13056 }'''); |
| 13057 // TODO(jmesserly): we can't use assertErrors because STRONG_MODE_* errors |
| 13058 // from CodeChecker don't have working equality. |
| 13059 List<AnalysisError> errors = analysisContext2.computeErrors(source); |
| 13060 expect(errors.map((e) => e.errorCode.name), [ |
| 13061 'STRONG_MODE_INVALID_METHOD_OVERRIDE', |
| 13062 'INVALID_METHOD_OVERRIDE_TYPE_PARAMETERS' |
| 13063 ]); |
| 13064 verify([source]); |
| 13065 } |
| 13066 |
12975 void test_pseudoGeneric_max_doubleDouble() { | 13067 void test_pseudoGeneric_max_doubleDouble() { |
12976 String code = r''' | 13068 String code = r''' |
12977 import 'dart:math'; | 13069 import 'dart:math'; |
12978 main() { | 13070 main() { |
12979 var foo = max(1.0, 2.0); | 13071 var foo = max(1.0, 2.0); |
12980 } | 13072 } |
12981 '''; | 13073 '''; |
12982 _resolveTestUnit(code); | 13074 _resolveTestUnit(code); |
12983 | 13075 |
12984 SimpleIdentifier identifier = _findIdentifier('foo'); | 13076 SimpleIdentifier identifier = _findIdentifier('foo'); |
(...skipping 2972 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
15957 | 16049 |
15958 void _resolveTestUnit(String code) { | 16050 void _resolveTestUnit(String code) { |
15959 testCode = code; | 16051 testCode = code; |
15960 testSource = addSource(testCode); | 16052 testSource = addSource(testCode); |
15961 LibraryElement library = resolve2(testSource); | 16053 LibraryElement library = resolve2(testSource); |
15962 assertNoErrors(testSource); | 16054 assertNoErrors(testSource); |
15963 verify([testSource]); | 16055 verify([testSource]); |
15964 testUnit = resolveCompilationUnit(testSource, library); | 16056 testUnit = resolveCompilationUnit(testSource, library); |
15965 } | 16057 } |
15966 } | 16058 } |
OLD | NEW |