Repository navigation
fix: do not emit variance-mismatch for constrained typevars in protocols #5500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
6154fed
3fe3b04
6976056
e579257
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3977,16 +3977,21 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> { | |
| // with a narrower variance. We only check invariant TypeVars here | ||
| // because wrong variance on covariant/contravariant TypeVars is | ||
| // already caught by InvalidVariance at the usage site. | ||
| // Constrained TypeVars are excluded because variance cannot be declared on them. | ||
| let metadata = self.get_metadata_for_class(class); | ||
| if metadata.is_protocol() | ||
| && tparams | ||
| .as_vec() | ||
| .iter() | ||
| .any(|p| p.is_type_var() && p.variance() == PreInferenceVariance::Invariant) | ||
| && tparams.as_vec().iter().any(|p| { | ||
| p.is_type_var() | ||
| && p.variance() == PreInferenceVariance::Invariant | ||
| && !matches!(p.restriction(), Restriction::Constraints(_)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious about the asymmetry with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The argument (originally from @DetachHead) is that variance is generally irrelevant to constrained type variables, because a constrained type variable solves to exactly one of its bounds. As a result, subtyping is almost irrelevant (if the types are not subtypes of one another, like The argument is heuristic because it's possible to use a constrained type variable whose options are orderable in the type system, for example I've seen |
||
| }) | ||
| { | ||
| let inferred = self.infer_variance_ignoring_declared(class); | ||
| for tparam in tparams.as_vec() { | ||
| if !tparam.is_type_var() || tparam.variance() != PreInferenceVariance::Invariant { | ||
| if !tparam.is_type_var() | ||
| || tparam.variance() != PreInferenceVariance::Invariant | ||
| || matches!(tparam.restriction(), Restriction::Constraints(_)) | ||
| { | ||
| continue; | ||
| } | ||
| let inferred_v = inferred.get(tparam.name()); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's soften the wording here. Variance actually can be declared on them.
In many cases the variance doesn't matter, but if some types are subtypes of other it can, e.g. with
(int, float)as a constraint there's a real distinction between covariance and contravariance.I think it would be more precise to just say that invariance usually makes sense, so complaining about it is too noisy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the comment: