Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.49 KB | None | 0 0
  1. From 19615ccea05214c00f64e0a15724986376e5aa60 Mon Sep 17 00:00:00 2001
  2. Date: Fri, 15 Mar 2019 01:21:34 +0000
  3. Subject: [PATCH] Patch
  4.  
  5. ---
  6. src/compiler/checker.ts | 49 ++++++++++++++++++++++++++++++++++++-----
  7.  src/compiler/types.ts   |  4 +++-
  8.  2 files changed, 47 insertions(+), 6 deletions(-)
  9.  
  10. diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
  11. index 43bc62ee85..7328d3d2fd 100644
  12. --- a/src/compiler/checker.ts
  13. +++ b/src/compiler/checker.ts
  14. @@ -670,7 +670,7 @@ namespace ts {
  15.  
  16.          let _jsxNamespace: __String;
  17.          let _jsxFactoryEntity: EntityName | undefined;
  18. -        let unmeasurableMarkerHandler: (() => void) | undefined;
  19. +        let unmeasurableMarkerHandler: ((isUnmeasurable: boolean, id?: number) => void) | undefined;
  20.  
  21.          const subtypeRelation = createMap<RelationComparisonResult>();
  22.          const assignableRelation = createMap<RelationComparisonResult>();
  23. @@ -12612,6 +12612,14 @@ namespace ts {
  24.                                  related = isRelatedTo(s, t, reportErrors);
  25.                              }
  26.                          }
  27. +                        else if (variance === Variance.CoKey) {
  28. +                            related = isRelatedTo(filterType(s, t => !(t.flags & (TypeFlags.String | TypeFlags.Number))),
  29. +                                                  filterType(t, t => !(t.flags & (TypeFlags.String | TypeFlags.Number))), reportErrors);
  30. +                        }
  31. +                        else if (variance === Variance.ConKey) {
  32. +                            related = isRelatedTo(filterType(t, t => !(t.flags & (TypeFlags.String | TypeFlags.Number))),
  33. +                                                  filterType(s, t => !(t.flags & (TypeFlags.String | TypeFlags.Number))), reportErrors);
  34. +                        }
  35.                          else {
  36.                              // In the invariant case we first compare covariantly, and only when that
  37.                              // succeeds do we proceed to compare contravariantly. Thus, error elaboration
  38. @@ -12750,7 +12758,9 @@ namespace ts {
  39.  
  40.                  if (target.flags & TypeFlags.TypeParameter) {
  41.                      // A source type { [P in Q]: X } is related to a target type T if keyof T is related to Q and X is related to T[Q].
  42. -                    if (getObjectFlags(source) & ObjectFlags.Mapped && isRelatedTo(getIndexType(target), getConstraintTypeFromMappedType(<MappedType>source))) {
  43. +                    if (getObjectFlags(source) & ObjectFlags.Mapped &&
  44. +                        isRelatedTo(instantiateType(getIndexType(target), reportKeyMarkers),
  45. +                                    instantiateType(getConstraintTypeFromMappedType(<MappedType>source), reportKeyMarkers))) {
  46.                          if (!(getMappedTypeModifiers(<MappedType>source) & MappedTypeModifiers.IncludeOptional)) {
  47.                              const templateType = getTemplateTypeFromMappedType(<MappedType>source);
  48.                              const indexedAccessType = getIndexedAccessType(target, getTypeParameterFromMappedType(<MappedType>source));
  49. @@ -13012,7 +13022,14 @@ namespace ts {
  50.  
  51.              function reportUnmeasurableMarkers(p: TypeParameter) {
  52.                  if (unmeasurableMarkerHandler && (p === markerSuperType || p === markerSubType || p === markerOtherType)) {
  53. -                    unmeasurableMarkerHandler();
  54. +                    unmeasurableMarkerHandler(true);
  55. +                }
  56. +                return p;
  57. +            }
  58. +
  59. +            function reportKeyMarkers(p: TypeParameter) {
  60. +                if (unmeasurableMarkerHandler && (p === markerSuperType || p === markerSubType || p === markerOtherType)) {
  61. +                    unmeasurableMarkerHandler(false, p.id);
  62.                  }
  63.                  return p;
  64.              }
  65. @@ -13026,7 +13043,9 @@ namespace ts {
  66.                  if (modifiersRelated) {
  67.                      let result: Ternary;
  68.                      const targetConstraint = getConstraintTypeFromMappedType(target);
  69. -                    const sourceConstraint = instantiateType(getConstraintTypeFromMappedType(source), reportUnmeasurableMarkers);
  70. +                    const sourceConstraint = getMappedTypeModifiers(source) === 0 ?
  71. +                        instantiateType(getConstraintTypeFromMappedType(source), reportKeyMarkers) :
  72. +                        instantiateType(getConstraintTypeFromMappedType(source), reportUnmeasurableMarkers);
  73.                      if (result = isRelatedTo(targetConstraint, sourceConstraint, reportErrors)) {
  74.                          const mapper = createTypeMapper([getTypeParameterFromMappedType(source)], [getTypeParameterFromMappedType(target)]);
  75.                          return result & isRelatedTo(instantiateType(getTemplateTypeFromMappedType(source), mapper), getTemplateTypeFromMappedType(target), reportErrors);
  76. @@ -13503,8 +13522,17 @@ namespace ts {
  77.                  variances = [];
  78.                  for (const tp of typeParameters) {
  79.                      let unmeasurable = false;
  80. +                    let keyVars: Record<number, true> = {};
  81.                      const oldHandler = unmeasurableMarkerHandler;
  82. -                    unmeasurableMarkerHandler = () => unmeasurable = true;
  83. +                    unmeasurableMarkerHandler = (isUnmeasurable: boolean, id?: number) => {
  84. +                        if (isUnmeasurable) {
  85. +                            unmeasurable = true
  86. +                        }
  87. +                        else {
  88. +                            keyVars[id!] = true;
  89. +                        }
  90. +
  91. +                    };
  92.                      // We first compare instantiations where the type parameter is replaced with
  93.                      // marker types that have a known subtype relationship. From this we can infer
  94.                      // invariance, covariance, contravariance or bivariance.
  95. @@ -13520,6 +13548,17 @@ namespace ts {
  96.                          variance = Variance.Independent;
  97.                      }
  98.                      unmeasurableMarkerHandler = oldHandler;
  99. +                    if (keyVars[markerSubType.id] && keyVars[markerSuperType.id]) {
  100. +                        if (variance === Variance.Contravariant) {
  101. +                            variance = Variance.ConKey;
  102. +                        }
  103. +                        else if (variance === Variance.Covariant) {
  104. +                            variance = Variance.CoKey;
  105. +                        }
  106. +                        else {
  107. +                            unmeasurable = true;
  108. +                        }
  109. +                    }
  110.                      if (unmeasurable) {
  111.                          variance = Variance.Unmeasurable;
  112.                          const covariantID = getRelationKey(typeWithSub, typeWithSuper, assignableRelation);
  113. diff --git a/src/compiler/types.ts b/src/compiler/types.ts
  114. index b8a17470da..d1d3ff2254 100644
  115. --- a/src/compiler/types.ts
  116. +++ b/src/compiler/types.ts
  117. @@ -4133,7 +4133,9 @@ namespace ts {
  118.          Contravariant = 2,  // Contravariant
  119.          Bivariant     = 3,  // Both covariant and contravariant
  120.          Independent   = 4,  // Unwitnessed type parameter
  121. -        Unmeasurable = 5,  // Variance result is unusable - relationship relies on structural comparisons which are not reflected in generic relationships
  122. +        Unmeasurable =  5,  // Variance result is unusable - relationship relies on structural comparisons which are not reflected in generic relationships
  123. +        CoKey = 6,
  124. +        ConKey = 7
  125.      }
  126.  
  127.      // Generic class and interface types
  128. --
  129. 2.17.2 (Apple Git-113)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement