@@ -8,7 +8,9 @@ import 'package:kernel/type_algebra.dart' show containsTypeVariable;
8
8
9
9
import 'package:kernel/util/graph.dart' show Graph, computeStrongComponents;
10
10
11
+ import '../builder/builtin_type_declaration_builder.dart' ;
11
12
import '../builder/class_builder.dart' ;
13
+ import '../builder/extension_builder.dart' ;
12
14
import '../builder/formal_parameter_builder.dart' ;
13
15
import '../builder/function_type_builder.dart' ;
14
16
import '../builder/invalid_type_declaration_builder.dart' ;
@@ -39,6 +41,11 @@ import '../fasta_codes.dart'
39
41
40
42
import '../kernel/utils.dart' ;
41
43
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
+
42
49
/// Initial value for "variance" that is to be computed by the compiler.
43
50
const int pendingVariance = - 1 ;
44
51
@@ -720,7 +727,9 @@ List<Object> findRawTypesWithInboundReferences(TypeBuilder? type) {
720
727
/// generic types with inbound references in its bound. The second element of
721
728
/// the triplet is the error message. The third element is the context.
722
729
List <NonSimplicityIssue > getInboundReferenceIssues (
723
- List <TypeVariableBuilder > variables) {
730
+ List <TypeVariableBuilder >? variables) {
731
+ if (variables == null ) return < NonSimplicityIssue > [];
732
+
724
733
List <NonSimplicityIssue > issues = < NonSimplicityIssue > [];
725
734
for (TypeVariableBuilder variable in variables) {
726
735
if (variable.bound != null ) {
@@ -798,62 +807,54 @@ List<List<RawTypeCycleElement>> findRawTypePathsToDeclaration(
798
807
[Set <TypeDeclarationBuilder >? visited]) {
799
808
visited ?? = new Set <TypeDeclarationBuilder >.identity ();
800
809
List <List <RawTypeCycleElement >> paths = < List <RawTypeCycleElement >> [];
810
+
801
811
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
+
803
829
if (start.arguments == null ) {
804
- if (start.declaration == end) {
830
+ TypeDeclarationBuilder ? declaration = start.declaration;
831
+ if (declaration == end) {
805
832
paths.add (< RawTypeCycleElement > [new RawTypeCycleElement (start, null )]);
806
833
} 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);
821
836
} 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);
837
838
if (declaration.type is FunctionTypeBuilder ) {
838
839
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);
854
841
}
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);
855
856
}
856
- visited.remove (start. declaration);
857
+ visited.remove (declaration);
857
858
}
858
859
} else {
859
860
for (TypeBuilder argument in start.arguments! ) {
@@ -883,6 +884,28 @@ List<List<RawTypeCycleElement>> findRawTypePathsToDeclaration(
883
884
return paths;
884
885
}
885
886
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
+
886
909
/// Finds raw generic type cycles ending and starting with [declaration] .
887
910
///
888
911
/// Returns list of found cycles consisting of [RawTypeCycleElement] s. The
@@ -893,51 +916,27 @@ List<List<RawTypeCycleElement>> findRawTypePathsToDeclaration(
893
916
/// reporting.
894
917
List <List <RawTypeCycleElement >> findRawTypeCycles (
895
918
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));
923
926
if (declaration.type is FunctionTypeBuilder ) {
924
927
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;
938
931
}
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);
939
938
}
940
- return cycles ;
939
+ return const [] ;
941
940
}
942
941
943
942
/// Converts raw generic type [cycles] for [declaration] into reportable issues.
@@ -1012,11 +1011,18 @@ List<NonSimplicityIssue> getNonSimplicityIssuesForDeclaration(
1012
1011
TypeDeclarationBuilder declaration,
1013
1012
{bool performErrorRecovery = true }) {
1014
1013
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);
1020
1026
}
1021
1027
List <List <RawTypeCycleElement >> cyclesToReport =
1022
1028
< List <RawTypeCycleElement >> [];
0 commit comments