Skip to content

Commit

Permalink
Make ParamSpec::new private
Browse files Browse the repository at this point in the history
Summary: Also `Param`.

Reviewed By: IanChilds

Differential Revision: D63434754

fbshipit-source-id: 0efbfec506f069cfc3389c5850198c41854d6a62
  • Loading branch information
stepancheg authored and facebook-github-bot committed Sep 26, 2024
1 parent 3f80c26 commit c124518
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 55 deletions.
1 change: 0 additions & 1 deletion starlark/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ mod tests;

pub use basic::TyBasic;
pub use callable::TyCallable;
pub use callable_param::Param;
pub use callable_param::ParamIsRequired;
pub use callable_param::ParamSpec;
pub use function::TyFunction;
Expand Down
68 changes: 14 additions & 54 deletions starlark/src/typing/callable_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub(crate) enum ParamMode {

/// A parameter argument to a function
#[derive(Debug, Clone, Dupe, PartialEq, Eq, Hash, PartialOrd, Ord, Allocative)]
pub struct Param {
pub(crate) struct Param {
/// The type of parameter
pub(crate) mode: ParamMode,
/// The type of the parameter.
Expand All @@ -72,44 +72,6 @@ pub struct Param {
}

impl Param {
/// Create a positional only parameter.
pub fn pos_only(ty: Ty) -> Self {
Self {
mode: ParamMode::PosOnly(ParamIsRequired::Yes),
ty,
}
}

/// Create a named only parameter.
pub fn name_only(name: &str, ty: Ty) -> Self {
Self {
mode: ParamMode::NameOnly(ArcStr::from(name), ParamIsRequired::Yes),
ty,
}
}

/// Create a positional or named parameter.
pub fn pos_or_name(name: &str, ty: Ty) -> Self {
Self {
mode: ParamMode::PosOrName(ArcStr::from(name), ParamIsRequired::Yes),
ty,
}
}

/// Make a parameter optional.
pub fn optional(self) -> Self {
Param {
mode: match self.mode {
ParamMode::PosOnly(_x) => ParamMode::PosOnly(ParamIsRequired::No),
ParamMode::PosOrName(x, _y) => ParamMode::PosOrName(x, ParamIsRequired::No),
ParamMode::NameOnly(x, _y) => ParamMode::NameOnly(x, ParamIsRequired::No),
ParamMode::Args => ParamMode::Args,
ParamMode::Kwargs => ParamMode::Kwargs,
},
ty: self.ty,
}
}

/// Create a `*args` parameter.
///
/// `ty` is a tuple item type.
Expand Down Expand Up @@ -224,7 +186,7 @@ impl ParamSpec {
/// Constructor.
/// Return an error if the sequence of parameters is incorrect,
/// for example, if positional-only parameters follow named-only.
pub fn new(params: Vec<Param>) -> crate::Result<ParamSpec> {
fn new(params: Vec<Param>) -> crate::Result<ParamSpec> {
if params.as_slice() == Self::any().params() {
Ok(ParamSpec::any())
} else {
Expand Down Expand Up @@ -376,31 +338,29 @@ impl ParamSpec {

/// `*args`.
pub(crate) fn args(ty: Ty) -> ParamSpec {
ParamSpec::new(vec![Param::args(ty)]).unwrap()
ParamSpec::new_parts([], [], Some(ty), [], None).expect("Cannot fail")
}

/// `**kwargs`.
pub fn kwargs(ty: Ty) -> ParamSpec {
ParamSpec::new(vec![Param::kwargs(ty)]).unwrap()
ParamSpec::new_parts([], [], None, [], Some(ty)).expect("Cannot fail")
}

/// `/, arg=, arg=, ..., arg, arg, ...`.
/// `arg=, arg=, ..., arg, arg, ..., /`.
pub(crate) fn pos_only(
required: impl IntoIterator<Item = Ty>,
optional: impl IntoIterator<Item = Ty>,
) -> ParamSpec {
ParamSpec::new(
required
.into_iter()
.map(Param::pos_only)
.chain(
optional
.into_iter()
.map(|ty| Param::pos_only(ty).optional()),
)
.collect(),
ParamSpec::new_parts(
iter::empty()
.chain(required.into_iter().map(|ty| (ParamIsRequired::Yes, ty)))
.chain(optional.into_iter().map(|ty| (ParamIsRequired::No, ty))),
[],
None,
[],
None,
)
.unwrap()
.expect("Cannot fail")
}

/// No parameters.
Expand Down

0 comments on commit c124518

Please sign in to comment.