Skip to content

Commit

Permalink
Properly ignore bivariate types in type-name-incorrect-variance (#5660
Browse files Browse the repository at this point in the history
)

## Summary

#5658 didn't actually ignore bivariate types in some all cases (sorry
about that). This PR fixes that and adds bivariate types to the test
fixture.

## Test Plan

`cargo test`
  • Loading branch information
tjkuson authored Jul 10, 2023
1 parent 5ab9538 commit b8a6ce4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@
T_contra = TypeVar("T_contra", covariant=False, contravariant=True)
P_contra = ParamSpec("P_contra", contravariant=True)
P_contra = ParamSpec("P_contra", covariant=False, contravariant=True)

# Bivariate types are errors, but not covered by this check.

T = TypeVar("T", covariant=True, contravariant=True)
P = ParamSpec("P", covariant=True, contravariant=True)
T_co = TypeVar("T_co", covariant=True, contravariant=True)
P_co = ParamSpec("P_co", covariant=True, contravariant=True)
T_contra = TypeVar("T_contra", covariant=True, contravariant=True)
P_contra = ParamSpec("P_contra", covariant=True, contravariant=True)
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub(crate) fn type_name_incorrect_variance(checker: &mut Checker, value: &Expr)
.trim_end_matches("_co")
.trim_end_matches("_contra");
let replacement_name: String = match variance {
VarVariance::Bivariance => return, // Bivariate type are invalid, so ignore them for this rule.
VarVariance::Covariance => format!("{name_root}_co"),
VarVariance::Contravariance => format!("{name_root}_contra"),
VarVariance::Invariance => name_root.to_string(),
Expand Down Expand Up @@ -163,6 +164,7 @@ fn variance(covariant: Option<&Expr>, contravariant: Option<&Expr>) -> VarVarian
covariant.map(is_const_true),
contravariant.map(is_const_true),
) {
(Some(true), Some(true)) => VarVariance::Bivariance,
(Some(true), _) => VarVariance::Covariance,
(_, Some(true)) => VarVariance::Contravariance,
_ => VarVariance::Invariance,
Expand All @@ -186,6 +188,7 @@ impl fmt::Display for VarKind {

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum VarVariance {
Bivariance,
Covariance,
Contravariance,
Invariance,
Expand All @@ -194,6 +197,7 @@ enum VarVariance {
impl fmt::Display for VarVariance {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
VarVariance::Bivariance => fmt.write_str("bivariance"),
VarVariance::Covariance => fmt.write_str("covariance"),
VarVariance::Contravariance => fmt.write_str("contravariance"),
VarVariance::Invariance => fmt.write_str("invariance"),
Expand Down

0 comments on commit b8a6ce4

Please sign in to comment.