Merge AnnotationStyle::ForwardedInitial and Forwarded Summary: Forwarded and ForwardedInitial behave identically, so they can be merged. ___ Differential Revision: D121334168 fbshipit-source-id: e2138f5a588351ced7d1aecf4ac972577a5ad8f1
diff --git a/pyrefly/lib/alt/solve.rs b/pyrefly/lib/alt/solve.rs index 13d1d9a..b1e915d 100644 --- a/pyrefly/lib/alt/solve.rs +++ b/pyrefly/lib/alt/solve.rs
@@ -3784,9 +3784,7 @@ let tcc: &dyn Fn() -> TypeCheckContext = &|| { TypeCheckContext::of_kind(match style { AnnotationStyle::Direct => TypeCheckKind::AnnAssign, - AnnotationStyle::ForwardedInitial | AnnotationStyle::Forwarded => { - TypeCheckKind::AnnotatedName(name.clone()) - } + AnnotationStyle::Forwarded => TypeCheckKind::AnnotatedName(name.clone()), }) .with_annotation(annot_range, "declared type".to_owned()) }; @@ -3840,29 +3838,31 @@ let hint = annot_ty.as_ref().map(|t| (t, tcc)); self.expr_check(expr, hint, errors) }; - let ty = if style == &AnnotationStyle::Direct { - if attrs_field_specifier.is_some() { - self.heap.mk_any_implicit() - } else { - // For direct assignments, user-provided annotation takes - // precedence over inferred expr type. - annot_ty.unwrap_or(expr_ty) + let ty = match style { + AnnotationStyle::Direct => { + if attrs_field_specifier.is_some() { + self.heap.mk_any_implicit() + } else { + // For direct assignments, user-provided annotation takes + // precedence over inferred expr type. + annot_ty.unwrap_or(expr_ty) + } } - } else if matches!( - style, - AnnotationStyle::ForwardedInitial | AnnotationStyle::Forwarded - ) && let Some(annot) = annot_ty - // Usually, if we reassign a name with an annotation, we use the type of the - // expression going forward. We have an exception to prevent an `Any` - // expression from overwriting an annotation it is less informative than: if - // the expression is `Any` and the annotation is not, and the name's - // flow-sensitive type still matches the annotation, then we use the annotation. - && expr_ty.is_any() && !annot.is_any() - && last_value_or_narrow.is_none_or(|prev_idx| self.get_idx(prev_idx).ty() == &annot) - { - annot - } else { - expr_ty + AnnotationStyle::Forwarded => { + if let Some(annot) = annot_ty + // Usually, if we reassign a name with an annotation, we use the type of the + // expression going forward. We have an exception to prevent an `Any` + // expression from overwriting an annotation it is less informative than: if + // the expression is `Any` and the annotation is not, and the name's + // flow-sensitive type still matches the annotation, then we use the annotation. + && expr_ty.is_any() && !annot.is_any() + && last_value_or_narrow.is_none_or(|prev_idx| self.get_idx(prev_idx).ty() == &annot) + { + annot + } else { + expr_ty + } + } }; (Some(annot), ty) }
diff --git a/pyrefly/lib/binding/binding.rs b/pyrefly/lib/binding/binding.rs index b68a8a4..6377811 100644 --- a/pyrefly/lib/binding/binding.rs +++ b/pyrefly/lib/binding/binding.rs
@@ -2121,10 +2121,8 @@ pub enum AnnotationStyle { /// Annotated assignment: `x: MyType = my_value` Direct, - /// First assignment after a bare annotation: `x: MyType` then `x = value`. - /// Annotation takes precedence (the variable had no prior value). - ForwardedInitial, - /// Reassignment of an already-initialized annotated variable. + /// Assignment or reassignment of an already-declared annotated variable: + /// for example, `x: MyType` then `x = value`. /// Expression type takes precedence; annotation is an upper-bound hint. Forwarded, }
diff --git a/pyrefly/lib/binding/target.rs b/pyrefly/lib/binding/target.rs index 2c643da..899b490 100644 --- a/pyrefly/lib/binding/target.rs +++ b/pyrefly/lib/binding/target.rs
@@ -735,22 +735,11 @@ FlowStyle::Other }; // Must check before bind_name updates the flow. - let was_uninitialized = matches!( - self.scopes.flow_style_for_name(&name.id), - Some(FlowStyle::Uninitialized) - ); let last_value_or_narrow = self.scopes.last_value_or_narrow_for(&name.id); let canonical_ann = self.bind_name(&name.id, scope_idx, style); let ann = match direct_ann { Some((_, idx)) => Some((AnnotationStyle::Direct, idx)), - None => canonical_ann.map(|idx| { - let forwarded_style = if was_uninitialized { - AnnotationStyle::ForwardedInitial - } else { - AnnotationStyle::Forwarded - }; - (forwarded_style, idx) - }), + None => canonical_ann.map(|idx| (AnnotationStyle::Forwarded, idx)), }; // Compute def_idx before building the binding, since the NameAssign needs // its own idx for partial type inference support.