Skip to content

Commit 046edf6

Browse files
migeed-zmeta-codesync[bot]
authored andcommitted
Add unused infer_variance_ignoring_declared method
Summary: For protocols, we want to check that the inferred variance and compare it to the declared variance. This is a function that infers variance regardless of the declared variance. We will only call it on protocols, but this will happen in the solver phase. Reviewed By: stroxler Differential Revision: D92893695 fbshipit-source-id: 1745afd55107ee762423689c1bad9711b82b037a
1 parent 5cb3bb0 commit 046edf6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

pyrefly/lib/alt/class/variance_inference.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,36 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
573573
env
574574
}
575575

576+
/// Infer variance from structural usage, ignoring declared variance.
577+
/// All type params are treated as having undefined variance, and the
578+
/// fixpoint algorithm discovers what the structure implies.
579+
#[allow(dead_code)]
580+
pub fn infer_variance_ignoring_declared(&self, class: &Class) -> VarianceMap {
581+
let tparams = self.get_class_tparams(class);
582+
let inference_map = tparams
583+
.as_vec()
584+
.iter()
585+
.map(|p| {
586+
(
587+
p.name().clone(),
588+
InferenceStatus {
589+
inferred_variance: Variance::Bivariant,
590+
has_variance_inferred: false,
591+
specified_variance: None,
592+
},
593+
)
594+
})
595+
.collect::<InferenceMap>();
596+
let environment = self.infer_variance_env(class, inference_map);
597+
let class_variances = environment
598+
.get(class)
599+
.expect("class must be present in environment")
600+
.iter()
601+
.map(|(name, status)| (name.clone(), status.inferred_variance))
602+
.collect::<SmallMap<_, _>>();
603+
VarianceMap(class_variances)
604+
}
605+
576606
/// Compute variance for a class, optionally checking for violations.
577607
///
578608
/// When `check_violations` is true, also checks that type variables with

0 commit comments

Comments
 (0)