Skip to content

Commit

Permalink
Group the Binding.* impls consistently
Browse files Browse the repository at this point in the history
Summary:
Regroup the `Binding.*` code so that the impls come right after the type definitions.

My motivation here is partly readability, but mostly that it makes edits easy: whenever
we modify a `Binding` or add a new kind of `Binding`, we typically need to update or
provide all these impls.

Keeping them together will make that easier.

Reviewed By: ndmitchell, grievejia

Differential Revision: D66477499

fbshipit-source-id: 8a54c117a67a2474f3db24d99bc126356f72a556
  • Loading branch information
stroxler authored and facebook-github-bot committed Nov 26, 2024
1 parent 986a3e9 commit 51823f9
Showing 1 changed file with 92 additions and 92 deletions.
184 changes: 92 additions & 92 deletions pyre2/pyre2/bin/alt/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ impl Display for Key {
}
}

impl DisplayWith<Bindings> for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>, _: &Bindings) -> fmt::Result {
write!(f, "{self}")
}
}

/// Like `Key`, but used for things accessible in another module.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum KeyExported {
Expand Down Expand Up @@ -345,98 +351,6 @@ impl Binding {
}
}

/// Values that reutrn an annotation.
#[derive(Clone, Debug)]
pub enum BindingAnnotation {
/// The type is annotated to be this key, will have the outer type removed.
/// Optionally occuring within a class, in which case Self refers to this class.
AnnotateExpr(Expr, Option<Idx<Key>>),
/// A literal type we know statically.
Type(Type),
/// Type of an attribute.
AttrType(ExprAttribute),
/// A forward reference to another binding.
Forward(Idx<Key>),
}

/// Binding used to compute a `BaseClass`.
///
/// The `Expr` is the base class expression, from the containing class header.
#[derive(Clone, Debug)]
pub struct BindingBaseClass(pub Expr);

/// Binding for the class `Mro`. The `Key` points to the definition of the class.
#[derive(Clone, Debug)]
pub struct BindingMro(pub Idx<Key>);

/// Values that represent type parameters of either functions or classes.
#[derive(Clone, Debug)]
pub enum BindingTypeParams {
/// The first argument is any scoped type parameters.
/// The second argument tracks all names that appear in parameter and return annotations, which might
/// indicate legacy type parameters if they point to variable declarations.
Function(Vec<Quantified>, Vec<Idx<KeyLegacyTypeParam>>),
/// The first argument is a lookup for the class definition.
/// The second argument tracks all names that appear in bases, which might
/// indicate legacy type parameters if they point to variable declarations.
Class(Idx<Key>, Vec<Idx<KeyLegacyTypeParam>>),
}

#[derive(Clone, Debug)]
pub struct BindingLegacyTypeParam(pub Idx<Key>);

impl DisplayWith<Bindings> for BindingAnnotation {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
match self {
Self::AnnotateExpr(x, self_type) => write!(
f,
"_: {}{}",
ctx.module_info().display(x),
match self_type {
None => String::new(),
Some(t) => format!(" (self {})", ctx.display(*t)),
}
),
Self::Forward(k) => write!(f, "{}", ctx.display(*k)),
Self::Type(t) => write!(f, "type {t}"),
Self::AttrType(attr) => write!(f, "type {attr:?}"),
}
}
}

impl DisplayWith<Bindings> for BindingBaseClass {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
write!(f, "base_class {}", ctx.module_info().display(&self.0),)
}
}

impl DisplayWith<Bindings> for BindingMro {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
write!(f, "mro {}", ctx.display(self.0))
}
}

impl DisplayWith<Bindings> for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>, _: &Bindings) -> fmt::Result {
write!(f, "{self}")
}
}

impl DisplayWith<Bindings> for BindingTypeParams {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
match self {
Self::Function(_, _) => write!(f, "function_type_params"),
Self::Class(id, _) => write!(f, "class_type_params {}", ctx.display(*id)),
}
}
}

impl DisplayWith<Bindings> for BindingLegacyTypeParam {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
write!(f, "legacy_type_param {}", ctx.display(self.0))
}
}

impl DisplayWith<Bindings> for Binding {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
let m = ctx.module_info();
Expand Down Expand Up @@ -571,3 +485,89 @@ impl DisplayWith<Bindings> for Binding {
}
}
}

/// Values that reutrn an annotation.
#[derive(Clone, Debug)]
pub enum BindingAnnotation {
/// The type is annotated to be this key, will have the outer type removed.
/// Optionally occuring within a class, in which case Self refers to this class.
AnnotateExpr(Expr, Option<Idx<Key>>),
/// A literal type we know statically.
Type(Type),
/// Type of an attribute.
AttrType(ExprAttribute),
/// A forward reference to another binding.
Forward(Idx<Key>),
}

impl DisplayWith<Bindings> for BindingAnnotation {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
match self {
Self::AnnotateExpr(x, self_type) => write!(
f,
"_: {}{}",
ctx.module_info().display(x),
match self_type {
None => String::new(),
Some(t) => format!(" (self {})", ctx.display(*t)),
}
),
Self::Forward(k) => write!(f, "{}", ctx.display(*k)),
Self::Type(t) => write!(f, "type {t}"),
Self::AttrType(attr) => write!(f, "type {attr:?}"),
}
}
}

/// Binding used to compute a `BaseClass`.
///
/// The `Expr` is the base class expression, from the containing class header.
#[derive(Clone, Debug)]
pub struct BindingBaseClass(pub Expr);

impl DisplayWith<Bindings> for BindingBaseClass {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
write!(f, "base_class {}", ctx.module_info().display(&self.0),)
}
}

/// Binding for the class `Mro`. The `Key` points to the definition of the class.
#[derive(Clone, Debug)]
pub struct BindingMro(pub Idx<Key>);

impl DisplayWith<Bindings> for BindingMro {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
write!(f, "mro {}", ctx.display(self.0))
}
}

/// Values that represent type parameters of either functions or classes.
#[derive(Clone, Debug)]
pub enum BindingTypeParams {
/// The first argument is any scoped type parameters.
/// The second argument tracks all names that appear in parameter and return annotations, which might
/// indicate legacy type parameters if they point to variable declarations.
Function(Vec<Quantified>, Vec<Idx<KeyLegacyTypeParam>>),
/// The first argument is a lookup for the class definition.
/// The second argument tracks all names that appear in bases, which might
/// indicate legacy type parameters if they point to variable declarations.
Class(Idx<Key>, Vec<Idx<KeyLegacyTypeParam>>),
}

impl DisplayWith<Bindings> for BindingTypeParams {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
match self {
Self::Function(_, _) => write!(f, "function_type_params"),
Self::Class(id, _) => write!(f, "class_type_params {}", ctx.display(*id)),
}
}
}

#[derive(Clone, Debug)]
pub struct BindingLegacyTypeParam(pub Idx<Key>);

impl DisplayWith<Bindings> for BindingLegacyTypeParam {
fn fmt(&self, f: &mut fmt::Formatter<'_>, ctx: &Bindings) -> fmt::Result {
write!(f, "legacy_type_param {}", ctx.display(self.0))
}
}

0 comments on commit 51823f9

Please sign in to comment.