@@ -8,7 +8,9 @@ import 'package:kernel/type_algebra.dart' show containsTypeVariable;
88
99import 'package:kernel/util/graph.dart' show Graph, computeStrongComponents;
1010
11+ import '../builder/builtin_type_declaration_builder.dart' ;
1112import '../builder/class_builder.dart' ;
13+ import '../builder/extension_builder.dart' ;
1214import '../builder/formal_parameter_builder.dart' ;
1315import '../builder/function_type_builder.dart' ;
1416import '../builder/invalid_type_declaration_builder.dart' ;
@@ -39,6 +41,11 @@ import '../fasta_codes.dart'
3941
4042import '../kernel/utils.dart' ;
4143
44+ import '../problems.dart' ;
45+ import '../source/source_class_builder.dart' ;
46+ import '../source/source_extension_builder.dart' ;
47+ import '../source/source_type_alias_builder.dart' ;
48+
4249/// Initial value for "variance" that is to be computed by the compiler.
4350const int pendingVariance = - 1 ;
4451
@@ -720,7 +727,9 @@ List<Object> findRawTypesWithInboundReferences(TypeBuilder? type) {
720727/// generic types with inbound references in its bound. The second element of
721728/// the triplet is the error message. The third element is the context.
722729List <NonSimplicityIssue > getInboundReferenceIssues (
723- List <TypeVariableBuilder > variables) {
730+ List <TypeVariableBuilder >? variables) {
731+ if (variables == null ) return < NonSimplicityIssue > [];
732+
724733 List <NonSimplicityIssue > issues = < NonSimplicityIssue > [];
725734 for (TypeVariableBuilder variable in variables) {
726735 if (variable.bound != null ) {
@@ -798,62 +807,54 @@ List<List<RawTypeCycleElement>> findRawTypePathsToDeclaration(
798807 [Set <TypeDeclarationBuilder >? visited]) {
799808 visited ?? = new Set <TypeDeclarationBuilder >.identity ();
800809 List <List <RawTypeCycleElement >> paths = < List <RawTypeCycleElement >> [];
810+
801811 if (start is NamedTypeBuilder ) {
802- TypeDeclarationBuilder ? declaration = start.declaration;
812+ void visitTypeVariables (List <TypeVariableBuilder >? typeVariables) {
813+ if (typeVariables == null ) return ;
814+
815+ for (TypeVariableBuilder variable in typeVariables) {
816+ if (variable.bound != null ) {
817+ for (List <RawTypeCycleElement > path
818+ in findRawTypePathsToDeclaration (variable.bound, end, visited)) {
819+ if (path.isNotEmpty) {
820+ paths.add (< RawTypeCycleElement > [
821+ new RawTypeCycleElement (start, null )
822+ ]..addAll (path..first.typeVariable = variable));
823+ }
824+ }
825+ }
826+ }
827+ }
828+
803829 if (start.arguments == null ) {
804- if (start.declaration == end) {
830+ TypeDeclarationBuilder ? declaration = start.declaration;
831+ if (declaration == end) {
805832 paths.add (< RawTypeCycleElement > [new RawTypeCycleElement (start, null )]);
806833 } else if (visited.add (start.declaration! )) {
807- if (declaration is ClassBuilder && declaration.typeVariables != null ) {
808- for (TypeVariableBuilder variable in declaration.typeVariables! ) {
809- if (variable.bound != null ) {
810- for (List <RawTypeCycleElement > path
811- in findRawTypePathsToDeclaration (
812- variable.bound, end, visited)) {
813- if (path.isNotEmpty) {
814- paths.add (< RawTypeCycleElement > [
815- new RawTypeCycleElement (start, null )
816- ]..addAll (path..first.typeVariable = variable));
817- }
818- }
819- }
820- }
834+ if (declaration is ClassBuilder ) {
835+ visitTypeVariables (declaration.typeVariables);
821836 } else if (declaration is TypeAliasBuilder ) {
822- if (declaration.typeVariables != null ) {
823- for (TypeVariableBuilder variable in declaration.typeVariables! ) {
824- if (variable.bound != null ) {
825- for (List <RawTypeCycleElement > dependencyPath
826- in findRawTypePathsToDeclaration (
827- variable.bound, end, visited)) {
828- if (dependencyPath.isNotEmpty) {
829- paths.add (< RawTypeCycleElement > [
830- new RawTypeCycleElement (start, null )
831- ]..addAll (dependencyPath..first.typeVariable = variable));
832- }
833- }
834- }
835- }
836- }
837+ visitTypeVariables (declaration.typeVariables);
837838 if (declaration.type is FunctionTypeBuilder ) {
838839 FunctionTypeBuilder type = declaration.type as FunctionTypeBuilder ;
839- if (type.typeVariables != null ) {
840- for (TypeVariableBuilder variable in type.typeVariables! ) {
841- if (variable.bound != null ) {
842- for (List <RawTypeCycleElement > dependencyPath
843- in findRawTypePathsToDeclaration (
844- variable.bound, end, visited)) {
845- if (dependencyPath.isNotEmpty) {
846- paths.add (< RawTypeCycleElement > [
847- new RawTypeCycleElement (start, null )
848- ]..addAll (dependencyPath..first.typeVariable = variable));
849- }
850- }
851- }
852- }
853- }
840+ visitTypeVariables (type.typeVariables);
854841 }
842+ } else if (declaration is ExtensionBuilder ) {
843+ visitTypeVariables (declaration.typeParameters);
844+ } else if (declaration is TypeVariableBuilder ) {
845+ // Do nothing. The type variable is handled by its parent declaration.
846+ } else if (declaration is BuiltinTypeDeclarationBuilder ) {
847+ // Do nothing.
848+ } else if (declaration is InvalidTypeDeclarationBuilder ) {
849+ // Do nothing.
850+ } else {
851+ unhandled (
852+ '$declaration (${declaration .runtimeType })' ,
853+ 'findRawTypePathsToDeclaration' ,
854+ declaration? .charOffset ?? - 1 ,
855+ declaration? .fileUri);
855856 }
856- visited.remove (start. declaration);
857+ visited.remove (declaration);
857858 }
858859 } else {
859860 for (TypeBuilder argument in start.arguments! ) {
@@ -883,6 +884,28 @@ List<List<RawTypeCycleElement>> findRawTypePathsToDeclaration(
883884 return paths;
884885}
885886
887+ List <List <RawTypeCycleElement >> _findRawTypeCyclesFromTypeVariables (
888+ TypeDeclarationBuilder declaration,
889+ List <TypeVariableBuilder >? typeVariables) {
890+ if (typeVariables == null ) {
891+ return const [];
892+ }
893+
894+ List <List <RawTypeCycleElement >> cycles = < List <RawTypeCycleElement >> [];
895+ for (TypeVariableBuilder variable in typeVariables) {
896+ if (variable.bound != null ) {
897+ for (List <RawTypeCycleElement > dependencyPath
898+ in findRawTypePathsToDeclaration (variable.bound, declaration)) {
899+ if (dependencyPath.isNotEmpty) {
900+ dependencyPath.first.typeVariable = variable;
901+ cycles.add (dependencyPath);
902+ }
903+ }
904+ }
905+ }
906+ return cycles;
907+ }
908+
886909/// Finds raw generic type cycles ending and starting with [declaration] .
887910///
888911/// Returns list of found cycles consisting of [RawTypeCycleElement] s. The
@@ -893,51 +916,27 @@ List<List<RawTypeCycleElement>> findRawTypePathsToDeclaration(
893916/// reporting.
894917List <List <RawTypeCycleElement >> findRawTypeCycles (
895918 TypeDeclarationBuilder declaration) {
896- List <List <RawTypeCycleElement >> cycles = < List <RawTypeCycleElement >> [];
897- if (declaration is ClassBuilder && declaration.typeVariables != null ) {
898- for (TypeVariableBuilder variable in declaration.typeVariables! ) {
899- if (variable.bound != null ) {
900- for (List <RawTypeCycleElement > path
901- in findRawTypePathsToDeclaration (variable.bound, declaration)) {
902- if (path.isNotEmpty) {
903- path.first.typeVariable = variable;
904- cycles.add (path);
905- }
906- }
907- }
908- }
909- } else if (declaration is TypeAliasBuilder ) {
910- if (declaration.typeVariables != null ) {
911- for (TypeVariableBuilder variable in declaration.typeVariables! ) {
912- if (variable.bound != null ) {
913- for (List <RawTypeCycleElement > dependencyPath
914- in findRawTypePathsToDeclaration (variable.bound, declaration)) {
915- if (dependencyPath.isNotEmpty) {
916- dependencyPath.first.typeVariable = variable;
917- cycles.add (dependencyPath);
918- }
919- }
920- }
921- }
922- }
919+ if (declaration is SourceClassBuilder ) {
920+ return _findRawTypeCyclesFromTypeVariables (
921+ declaration, declaration.typeVariables);
922+ } else if (declaration is SourceTypeAliasBuilder ) {
923+ List <List <RawTypeCycleElement >> cycles = < List <RawTypeCycleElement >> [];
924+ cycles.addAll (_findRawTypeCyclesFromTypeVariables (
925+ declaration, declaration.typeVariables));
923926 if (declaration.type is FunctionTypeBuilder ) {
924927 FunctionTypeBuilder type = declaration.type as FunctionTypeBuilder ;
925- if (type.typeVariables != null ) {
926- for (TypeVariableBuilder variable in type.typeVariables! ) {
927- if (variable.bound != null ) {
928- for (List <RawTypeCycleElement > dependencyPath
929- in findRawTypePathsToDeclaration (variable.bound, declaration)) {
930- if (dependencyPath.isNotEmpty) {
931- dependencyPath.first.typeVariable = variable;
932- cycles.add (dependencyPath);
933- }
934- }
935- }
936- }
937- }
928+ cycles.addAll (
929+ _findRawTypeCyclesFromTypeVariables (declaration, type.typeVariables));
930+ return cycles;
938931 }
932+ } else if (declaration is SourceExtensionBuilder ) {
933+ return _findRawTypeCyclesFromTypeVariables (
934+ declaration, declaration.typeParameters);
935+ } else {
936+ unhandled ('$declaration (${declaration .runtimeType })' , 'findRawTypeCycles' ,
937+ declaration.charOffset, declaration.fileUri);
939938 }
940- return cycles ;
939+ return const [] ;
941940}
942941
943942/// Converts raw generic type [cycles] for [declaration] into reportable issues.
@@ -1012,11 +1011,18 @@ List<NonSimplicityIssue> getNonSimplicityIssuesForDeclaration(
10121011 TypeDeclarationBuilder declaration,
10131012 {bool performErrorRecovery = true }) {
10141013 List <NonSimplicityIssue > issues = < NonSimplicityIssue > [];
1015- if (declaration is ClassBuilder && declaration.typeVariables != null ) {
1016- issues.addAll (getInboundReferenceIssues (declaration.typeVariables! ));
1017- } else if (declaration is TypeAliasBuilder &&
1018- declaration.typeVariables != null ) {
1019- issues.addAll (getInboundReferenceIssues (declaration.typeVariables! ));
1014+ if (declaration is SourceClassBuilder ) {
1015+ issues.addAll (getInboundReferenceIssues (declaration.typeVariables));
1016+ } else if (declaration is SourceTypeAliasBuilder ) {
1017+ issues.addAll (getInboundReferenceIssues (declaration.typeVariables));
1018+ } else if (declaration is SourceExtensionBuilder ) {
1019+ issues.addAll (getInboundReferenceIssues (declaration.typeParameters));
1020+ } else {
1021+ unhandled (
1022+ '$declaration (${declaration .runtimeType })' ,
1023+ 'getNonSimplicityIssuesForDeclaration' ,
1024+ declaration.charOffset,
1025+ declaration.fileUri);
10201026 }
10211027 List <List <RawTypeCycleElement >> cyclesToReport =
10221028 < List <RawTypeCycleElement >> [];
0 commit comments