Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: pkg/analyzer/lib/src/summary/summarize_ast.dart

Issue 2668493002: Issue 28547. Fix for summarizing code with invalid type parameter reference. (Closed)
Patch Set: Handle invalid type parameter reference in expressions. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/resynthesize_common.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 serialization.summarize_ast; 5 library serialization.summarize_ast;
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/token.dart'; 8 import 'package:analyzer/dart/ast/token.dart';
9 import 'package:analyzer/dart/ast/visitor.dart'; 9 import 'package:analyzer/dart/ast/visitor.dart';
10 import 'package:analyzer/dart/element/type.dart' show DartType; 10 import 'package:analyzer/dart/element/type.dart' show DartType;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 } else { 92 } else {
93 localIndex = localClosureIndexMap[functionExpression.offset]; 93 localIndex = localClosureIndexMap[functionExpression.offset];
94 assert(localIndex != null); 94 assert(localIndex != null);
95 return <int>[0, localIndex]; 95 return <int>[0, localIndex];
96 } 96 }
97 } 97 }
98 98
99 EntityRefBuilder serializeIdentifier(Identifier identifier) { 99 EntityRefBuilder serializeIdentifier(Identifier identifier) {
100 EntityRefBuilder b = new EntityRefBuilder(); 100 EntityRefBuilder b = new EntityRefBuilder();
101 if (identifier is SimpleIdentifier) { 101 if (identifier is SimpleIdentifier) {
102 int index = visitor.serializeSimpleReference(identifier.name, 102 int index = visitor.serializeSimpleReference(identifier.name);
103 allowTypeParameter: true);
104 if (index < 0) { 103 if (index < 0) {
105 b.paramReference = -index; 104 b.paramReference = -index;
106 } else { 105 } else {
107 b.reference = index; 106 b.reference = index;
108 } 107 }
109 } else if (identifier is PrefixedIdentifier) { 108 } else if (identifier is PrefixedIdentifier) {
110 int prefix = visitor.serializeSimpleReference(identifier.prefix.name); 109 int prefix = visitor.serializeSimpleReference(identifier.prefix.name);
110 if (prefix < 0) {
111 throw new StateError('Invalid type parameter usage: $identifier}');
112 }
111 b.reference = 113 b.reference =
112 visitor.serializeReference(prefix, identifier.identifier.name); 114 visitor.serializeReference(prefix, identifier.identifier.name);
113 } else { 115 } else {
114 throw new StateError( 116 throw new StateError(
115 'Unexpected identifier type: ${identifier.runtimeType}'); 117 'Unexpected identifier type: ${identifier.runtimeType}');
116 } 118 }
117 return b; 119 return b;
118 } 120 }
119 121
120 @override 122 @override
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 */ 261 */
260 final List<UnlinkedImportBuilder> unlinkedImports = <UnlinkedImportBuilder>[]; 262 final List<UnlinkedImportBuilder> unlinkedImports = <UnlinkedImportBuilder>[];
261 263
262 /** 264 /**
263 * The unlinked portion of the "references table". This is the list of 265 * The unlinked portion of the "references table". This is the list of
264 * objects which should be written to [UnlinkedUnit.references]. 266 * objects which should be written to [UnlinkedUnit.references].
265 */ 267 */
266 final List<UnlinkedReferenceBuilder> unlinkedReferences = 268 final List<UnlinkedReferenceBuilder> unlinkedReferences =
267 <UnlinkedReferenceBuilder>[new UnlinkedReferenceBuilder()]; 269 <UnlinkedReferenceBuilder>[new UnlinkedReferenceBuilder()];
268 270
269
270 /** 271 /**
271 * List of [_Scope]s currently in effect. This is used to resolve type names 272 * List of [_Scope]s currently in effect. This is used to resolve type names
272 * to type parameters within classes, typedefs, and executables, as well as 273 * to type parameters within classes, typedefs, and executables, as well as
273 * references to class members. 274 * references to class members.
274 */ 275 */
275 final List<_Scope> scopes = <_Scope>[]; 276 final List<_Scope> scopes = <_Scope>[];
276 277
277 /** 278 /**
278 * True if 'dart:core' has been explicitly imported. 279 * True if 'dart:core' has been explicitly imported.
279 */ 280 */
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 int index = unlinkedReferences.length; 836 int index = unlinkedReferences.length;
836 unlinkedReferences.add(new UnlinkedReferenceBuilder( 837 unlinkedReferences.add(new UnlinkedReferenceBuilder(
837 prefixReference: prefixIndex, name: name)); 838 prefixReference: prefixIndex, name: name));
838 return index; 839 return index;
839 }); 840 });
840 841
841 /** 842 /**
842 * Serialize a reference to a name declared either at top level or in a 843 * Serialize a reference to a name declared either at top level or in a
843 * nested scope. 844 * nested scope.
844 * 845 *
845 * If [allowTypeParameter] is `true`, then references to type 846 * References to type parameters are returned as negative numbers.
846 * parameters are allowed, and are returned as negative numbers.
847 */ 847 */
848 int serializeSimpleReference(String name, {bool allowTypeParameter: false}) { 848 int serializeSimpleReference(String name) {
849 int indexOffset = 0; 849 int indexOffset = 0;
850 for (int i = scopes.length - 1; i >= 0; i--) { 850 for (int i = scopes.length - 1; i >= 0; i--) {
851 _Scope scope = scopes[i]; 851 _Scope scope = scopes[i];
852 _ScopedEntity entity = scope[name]; 852 _ScopedEntity entity = scope[name];
853 if (entity != null) { 853 if (entity != null) {
854 if (entity is _ScopedClassMember) { 854 if (entity is _ScopedClassMember) {
855 return serializeReference( 855 return serializeReference(
856 serializeReference(null, entity.className), name); 856 serializeReference(null, entity.className), name);
857 } else if (allowTypeParameter && entity is _ScopedTypeParameter) { 857 } else if (entity is _ScopedTypeParameter) {
858 int paramReference = indexOffset + entity.index; 858 int paramReference = indexOffset + entity.index;
859 return -paramReference; 859 return -paramReference;
860 } else {
861 // Invalid reference to a type parameter. Should never happen in
862 // legal Dart code.
863 // TODO(paulberry): could this exception ever be uncaught in illegal
864 // code?
865 throw new StateError('Invalid identifier reference');
866 } 860 }
867 } 861 }
868 if (scope is _TypeParameterScope) { 862 if (scope is _TypeParameterScope) {
869 indexOffset += scope.length; 863 indexOffset += scope.length;
870 } 864 }
871 } 865 }
872 return serializeReference(null, name); 866 return serializeReference(null, name);
873 } 867 }
874 868
875 /** 869 /**
(...skipping 26 matching lines...) Expand all
902 return b; 896 return b;
903 } 897 }
904 } 898 }
905 if (scope is _TypeParameterScope) { 899 if (scope is _TypeParameterScope) {
906 indexOffset += scope.length; 900 indexOffset += scope.length;
907 } 901 }
908 } 902 }
909 b.reference = serializeReference(null, name); 903 b.reference = serializeReference(null, name);
910 } else if (identifier is PrefixedIdentifier) { 904 } else if (identifier is PrefixedIdentifier) {
911 int prefixIndex = serializeSimpleReference(identifier.prefix.name); 905 int prefixIndex = serializeSimpleReference(identifier.prefix.name);
912 b.reference = 906 if (prefixIndex < 0) {
913 serializeReference(prefixIndex, identifier.identifier.name); 907 // Type parameters are not expected here, so this is an error and the
908 // type should be treated as a reference to `dynamic`.
909 b.reference = serializeReference(null, 'dynamic');
910 return b;
911 } else {
912 b.reference =
913 serializeReference(prefixIndex, identifier.identifier.name);
914 }
914 } else { 915 } else {
915 throw new StateError( 916 throw new StateError(
916 'Unexpected identifier type: ${identifier.runtimeType}'); 917 'Unexpected identifier type: ${identifier.runtimeType}');
917 } 918 }
918 if (typeArguments != null) { 919 if (typeArguments != null) {
919 b.typeArguments = 920 b.typeArguments =
920 typeArguments.arguments.map(serializeTypeName).toList(); 921 typeArguments.arguments.map(serializeTypeName).toList();
921 } 922 }
922 return b; 923 return b;
923 } 924 }
(...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after
1414 /** 1415 /**
1415 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. 1416 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s.
1416 */ 1417 */
1417 class _TypeParameterScope extends _Scope { 1418 class _TypeParameterScope extends _Scope {
1418 /** 1419 /**
1419 * Get the number of [_ScopedTypeParameter]s defined in this 1420 * Get the number of [_ScopedTypeParameter]s defined in this
1420 * [_TypeParameterScope]. 1421 * [_TypeParameterScope].
1421 */ 1422 */
1422 int get length => _definedNames.length; 1423 int get length => _definedNames.length;
1423 } 1424 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/resynthesize_common.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698