Skip to content

Commit 4563bc9

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Ty::as_function
Summary: Expose some `Ty` internals for the following diff D48894467. Reviewed By: ianlevesque Differential Revision: D48894468 fbshipit-source-id: 83a6380610a2a4cd7cdff910001900b59b3697e0
1 parent 808e2e3 commit 4563bc9

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

starlark/src/typing/basic.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use crate::typing::custom::TyCustomImpl;
2727
use crate::typing::starlark_value::TyStarlarkValue;
2828
use crate::typing::tuple::TyTuple;
2929
use crate::typing::Ty;
30+
use crate::typing::TyFunction;
3031
use crate::typing::TyName;
3132
use crate::values::none::NoneType;
3233
use crate::values::string::StarlarkStr;
@@ -128,6 +129,14 @@ impl TyBasic {
128129
self.as_name() == Some("function")
129130
}
130131

132+
/// If this type is function, return the function type.
133+
pub(crate) fn as_function(&self) -> Option<&TyFunction> {
134+
match self {
135+
TyBasic::Custom(c) => c.0.as_function_dyn(),
136+
_ => None,
137+
}
138+
}
139+
131140
/// Type is a tuple, with specified or unspecified member types.
132141
pub(crate) fn is_tuple(&self) -> bool {
133142
matches!(self, TyBasic::Tuple(_))

starlark/src/typing/custom.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use crate::typing::error::TypingOrInternalError;
3737
use crate::typing::Arg;
3838
use crate::typing::Ty;
3939
use crate::typing::TyBasic;
40+
use crate::typing::TyFunction;
4041
use crate::typing::TypingBinOp;
4142
use crate::typing::TypingOracleCtx;
4243
use crate::values::typing::type_compiled::alloc::TypeMatcherAlloc;
@@ -61,6 +62,9 @@ pub trait TyCustomImpl: Debug + Display + Hash + Ord + Allocative + Send + Sync
6162
fn is_callable(&self) -> bool {
6263
false
6364
}
65+
fn as_function(&self) -> Option<&TyFunction> {
66+
None
67+
}
6468
fn bin_op(&self, bin_op: TypingBinOp, rhs: &TyBasic, ctx: &TypingOracleCtx) -> Result<Ty, ()> {
6569
let _unused = (bin_op, rhs, ctx);
6670
Err(())
@@ -100,6 +104,7 @@ pub(crate) trait TyCustomDyn: Debug + Display + Allocative + Send + Sync + 'stat
100104
oracle: TypingOracleCtx,
101105
) -> Result<Ty, TypingOrInternalError>;
102106
fn is_callable_dyn(&self) -> bool;
107+
fn as_function_dyn(&self) -> Option<&TyFunction>;
103108
fn iter_item_dyn(&self) -> Result<Ty, ()>;
104109
fn index_dyn(&self, index: &TyBasic, ctx: &TypingOracleCtx) -> Result<Ty, ()>;
105110
fn attribute_dyn(&self, attr: &str) -> Result<Ty, ()>;
@@ -163,6 +168,10 @@ impl<T: TyCustomImpl> TyCustomDyn for T {
163168
self.is_callable()
164169
}
165170

171+
fn as_function_dyn(&self) -> Option<&TyFunction> {
172+
self.as_function()
173+
}
174+
166175
fn attribute_dyn(&self, attr: &str) -> Result<Ty, ()> {
167176
self.attribute(attr)
168177
}

starlark/src/typing/function.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ pub trait TyCustomFunctionImpl:
173173
args: &[Spanned<Arg>],
174174
oracle: TypingOracleCtx,
175175
) -> Result<Ty, TypingOrInternalError>;
176+
177+
fn as_function(&self) -> Option<&TyFunction> {
178+
None
179+
}
176180
}
177181

178182
#[derive(
@@ -206,6 +210,10 @@ impl<F: TyCustomFunctionImpl> TyCustomImpl for TyCustomFunction<F> {
206210
true
207211
}
208212

213+
fn as_function(&self) -> Option<&TyFunction> {
214+
self.0.as_function()
215+
}
216+
209217
fn bin_op(
210218
&self,
211219
bin_op: TypingBinOp,
@@ -332,4 +340,8 @@ impl TyCustomFunctionImpl for TyFunction {
332340
) -> Result<Ty, TypingOrInternalError> {
333341
oracle.validate_fn_call(span, self, args)
334342
}
343+
344+
fn as_function(&self) -> Option<&TyFunction> {
345+
Some(self)
346+
}
335347
}

starlark/src/typing/ty.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,17 @@ impl Ty {
362362
self.as_name() == Some("function")
363363
}
364364

365+
/// If this type is function, return the function type.
366+
///
367+
/// This is exposed for buck2 providers implementation,
368+
/// probably it does not do what you think.
369+
pub fn as_function(&self) -> Option<&TyFunction> {
370+
match self.iter_union() {
371+
[x] => x.as_function(),
372+
_ => None,
373+
}
374+
}
375+
365376
/// Create a unions type, which will be normalised before being created.
366377
pub fn unions(xs: Vec<Self>) -> Self {
367378
// Handle common cases first.

0 commit comments

Comments
 (0)