Skip to content

Commit 7afe67e

Browse files
committed
Typings for ProviderCollection too
1 parent 9058edf commit 7afe67e

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

app/buck2_build_api/src/interpreter/rule_defs/provider/collection.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ use starlark::environment::Methods;
3737
use starlark::environment::MethodsBuilder;
3838
use starlark::environment::MethodsStatic;
3939
use starlark::typing::Ty;
40+
use starlark::typing::TyCustomIndex;
41+
use starlark::typing::TyStarlarkValue;
42+
use starlark::typing::TyUser;
43+
use starlark::typing::TyUserParams;
4044
use starlark::values::AllocFrozenValue;
4145
use starlark::values::AllocStaticSimple;
4246
use starlark::values::AllocValue;
@@ -64,6 +68,7 @@ use starlark::values::none::NoneOr;
6468
use starlark::values::starlark_value;
6569
use starlark::values::starlark_value_as_type::StarlarkValueAsType;
6670
use starlark::values::type_repr::StarlarkTypeRepr;
71+
use starlark::values::typing::TypeInstanceId;
6772

6873
use crate::interpreter::rule_defs::provider::DefaultInfo;
6974
use crate::interpreter::rule_defs::provider::DefaultInfoCallable;
@@ -372,6 +377,7 @@ impl FrozenProviderCollection {
372377
/// ```
373378
#[starlark_module]
374379
fn provider_collection_methods(builder: &mut MethodsBuilder) {
380+
#[starlark(ty_custom_function = super::dependency::GetTyIdentity)]
375381
fn get<'v>(
376382
this: &ProviderCollection<'v>,
377383
index: Value<'v>,
@@ -380,11 +386,38 @@ fn provider_collection_methods(builder: &mut MethodsBuilder) {
380386
}
381387
}
382388

389+
static PROVIDER_COLLECTION_TYPE: std::sync::LazyLock<Ty> = std::sync::LazyLock::new(|| {
390+
Ty::custom(
391+
TyUser::new(
392+
"ProviderCollection".to_owned(),
393+
TyStarlarkValue::new::<ProviderCollection>(),
394+
TypeInstanceId::r#gen(),
395+
TyUserParams {
396+
index_custom: Some(TyCustomIndex::new(super::dependency::GetTyIdentity)),
397+
..TyUserParams::default()
398+
},
399+
)
400+
.unwrap(),
401+
)
402+
});
403+
383404
#[starlark_value(type = "ProviderCollection")]
384405
impl<'v, V: ValueLike<'v>> StarlarkValue<'v> for ProviderCollectionGen<V>
385406
where
386407
Self: ProvidesStaticType<'v>,
387408
{
409+
fn get_type_starlark_repr() -> Ty {
410+
PROVIDER_COLLECTION_TYPE.dupe()
411+
}
412+
413+
fn eval_type(&self) -> Option<Ty> {
414+
Some(PROVIDER_COLLECTION_TYPE.dupe())
415+
}
416+
417+
fn typechecker_ty(&self) -> Option<Ty> {
418+
Some(PROVIDER_COLLECTION_TYPE.dupe())
419+
}
420+
388421
fn at(&self, index: Value<'v>, _heap: &'v Heap) -> starlark::Result<Value<'v>> {
389422
match self.get_impl(index, GetOp::At)? {
390423
Either::Left(v) => Ok(v),

app/buck2_build_api/src/interpreter/rule_defs/provider/dependency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub(crate) fn register_dependency(globals: &mut GlobalsBuilder) {
278278
}
279279

280280
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Allocative)]
281-
struct GetTyIdentity;
281+
pub(super) struct GetTyIdentity;
282282

283283
impl starlark::typing::TyCustomFunctionImpl for GetTyIdentity {
284284
fn as_callable(&self) -> starlark::typing::TyCallable {

0 commit comments

Comments
 (0)