Skip to content

Commit 6fb55b7

Browse files
CopilotRyanCavanaughjakebailey
authored
Port TypeScript PR #62604: Propagate variance reliability (#1916)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: RyanCavanaugh <[email protected]> Co-authored-by: Jake Bailey <[email protected]>
1 parent 240b101 commit 6fb55b7

File tree

3 files changed

+25
-81
lines changed

3 files changed

+25
-81
lines changed

internal/checker/relater.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3870,26 +3870,32 @@ func (r *Relater) typeArgumentsRelatedTo(sources []*Type, targets []*Type, varia
38703870
} else {
38713871
related = r.c.compareTypesIdentical(s, t)
38723872
}
3873-
} else if variance == VarianceFlagsCovariant {
3874-
related = r.isRelatedToEx(s, t, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3875-
} else if variance == VarianceFlagsContravariant {
3876-
related = r.isRelatedToEx(t, s, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3877-
} else if variance == VarianceFlagsBivariant {
3878-
// In the bivariant case we first compare contravariantly without reporting
3879-
// errors. Then, if that doesn't succeed, we compare covariantly with error
3880-
// reporting. Thus, error elaboration will be based on the covariant check,
3881-
// which is generally easier to reason about.
3882-
related = r.isRelatedTo(t, s, RecursionFlagsBoth, false /*reportErrors*/)
3883-
if related == TernaryFalse {
3884-
related = r.isRelatedToEx(s, t, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3885-
}
38863873
} else {
3887-
// In the invariant case we first compare covariantly, and only when that
3888-
// succeeds do we proceed to compare contravariantly. Thus, error elaboration
3889-
// will typically be based on the covariant check.
3890-
related = r.isRelatedToEx(s, t, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3891-
if related != TernaryFalse {
3892-
related &= r.isRelatedToEx(t, s, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3874+
// Propagate unreliable variance flag
3875+
if r.c.inVarianceComputation && varianceFlags&VarianceFlagsUnreliable != 0 {
3876+
r.c.instantiateType(s, r.c.reportUnreliableMapper)
3877+
}
3878+
if variance == VarianceFlagsCovariant {
3879+
related = r.isRelatedToEx(s, t, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3880+
} else if variance == VarianceFlagsContravariant {
3881+
related = r.isRelatedToEx(t, s, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3882+
} else if variance == VarianceFlagsBivariant {
3883+
// In the bivariant case we first compare contravariantly without reporting
3884+
// errors. Then, if that doesn't succeed, we compare covariantly with error
3885+
// reporting. Thus, error elaboration will be based on the covariant check,
3886+
// which is generally easier to reason about.
3887+
related = r.isRelatedTo(t, s, RecursionFlagsBoth, false /*reportErrors*/)
3888+
if related == TernaryFalse {
3889+
related = r.isRelatedToEx(s, t, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3890+
}
3891+
} else {
3892+
// In the invariant case we first compare covariantly, and only when that
3893+
// succeeds do we proceed to compare contravariantly. Thus, error elaboration
3894+
// will typically be based on the covariant check.
3895+
related = r.isRelatedToEx(s, t, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3896+
if related != TernaryFalse {
3897+
related &= r.isRelatedToEx(t, s, RecursionFlagsBoth, reportErrors, nil /*headMessage*/, intersectionState)
3898+
}
38933899
}
38943900
}
38953901
if related == TernaryFalse {

testdata/baselines/reference/submodule/compiler/variancePropagation.errors.txt

Lines changed: 0 additions & 29 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/variancePropagation.errors.txt.diff

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)