@@ -30,7 +30,6 @@ use starlark_derive::starlark_value;
30
30
use starlark_map:: StarlarkHasher ;
31
31
use starlark_syntax:: slice_vec_ext:: SliceExt ;
32
32
use starlark_syntax:: slice_vec_ext:: VecExt ;
33
- use starlark_syntax:: syntax:: type_expr:: type_str_literal_is_wildcard;
34
33
use thiserror:: Error ;
35
34
36
35
use crate as starlark;
@@ -41,7 +40,6 @@ use crate::environment::MethodsBuilder;
41
40
use crate :: environment:: MethodsStatic ;
42
41
use crate :: private:: Private ;
43
42
use crate :: typing:: Ty ;
44
- use crate :: values:: dict:: Dict ;
45
43
use crate :: values:: dict:: DictRef ;
46
44
use crate :: values:: layout:: avalue:: alloc_static;
47
45
use crate :: values:: layout:: avalue:: AValueImpl ;
@@ -53,7 +51,6 @@ use crate::values::type_repr::StarlarkTypeRepr;
53
51
use crate :: values:: types:: tuple:: value:: Tuple ;
54
52
use crate :: values:: typing:: type_compiled:: factory:: TypeCompiledFactory ;
55
53
use crate :: values:: typing:: type_compiled:: matcher:: TypeMatcher ;
56
- use crate :: values:: typing:: type_compiled:: matchers:: IsDict ;
57
54
use crate :: values:: AllocValue ;
58
55
use crate :: values:: Demand ;
59
56
use crate :: values:: Freeze ;
@@ -74,6 +71,8 @@ enum TypingError {
74
71
/// The given type annotation does not represent a type
75
72
#[ error( "Type `{0}` is not a valid type annotation" ) ]
76
73
InvalidTypeAnnotation ( String ) ,
74
+ #[ error( "`{{A: B}}` cannot be used as type, perhaps you meant `dict[A, B]`" ) ]
75
+ Dict ,
77
76
/// The given type annotation does not exist, but the user might have forgotten quotes around
78
77
/// it
79
78
#[ error( r#"Found `{0}` instead of a valid type annotation. Perhaps you meant `"{1}"`?"# ) ]
@@ -380,10 +379,6 @@ impl<'v> TypeCompiled<Value<'v>> {
380
379
} ) )
381
380
}
382
381
383
- fn type_dict ( heap : & ' v Heap ) -> TypeCompiled < Value < ' v > > {
384
- Self :: alloc ( IsDict , Ty :: any_dict ( ) , heap)
385
- }
386
-
387
382
pub ( crate ) fn type_list_of (
388
383
t : TypeCompiled < Value < ' v > > ,
389
384
heap : & ' v Heap ,
@@ -417,10 +412,6 @@ impl<'v> TypeCompiled<Value<'v>> {
417
412
TypeCompiledFactory :: alloc_ty ( & ty, heap)
418
413
}
419
414
420
- pub ( crate ) fn is_wildcard_value ( x : Value ) -> bool {
421
- x. unpack_str ( ) . map ( type_str_literal_is_wildcard) == Some ( true )
422
- }
423
-
424
415
/// For `p: "xxx"`, parse that `"xxx"` as type.
425
416
pub ( crate ) fn from_str ( t : & str , heap : & ' v Heap ) -> TypeCompiled < Value < ' v > > {
426
417
TypeCompiledFactory :: alloc_ty ( & Ty :: name ( t) , heap)
@@ -442,27 +433,6 @@ impl<'v> TypeCompiled<Value<'v>> {
442
433
}
443
434
}
444
435
445
- fn from_dict ( t : DictRef < ' v > , heap : & ' v Heap ) -> anyhow:: Result < TypeCompiled < Value < ' v > > > {
446
- // Dictionary with a single element
447
- fn unpack_singleton_dictionary < ' v > ( x : & Dict < ' v > ) -> Option < ( Value < ' v > , Value < ' v > ) > {
448
- if x. len ( ) == 1 { x. iter ( ) . next ( ) } else { None }
449
- }
450
-
451
- if let Some ( ( tk, tv) ) = unpack_singleton_dictionary ( & t) {
452
- if TypeCompiled :: is_wildcard_value ( tk) && TypeCompiled :: is_wildcard_value ( tv) {
453
- Ok ( TypeCompiled :: type_dict ( heap) )
454
- } else {
455
- // Dict of the form {k: v} must all match the k/v types
456
- let tk = TypeCompiled :: new ( tk, heap) ?;
457
- let tv = TypeCompiled :: new ( tv, heap) ?;
458
- Ok ( TypeCompiled :: type_dict_of ( tk, tv, heap) )
459
- }
460
- } else {
461
- // Dict type with zero or multiple fields is not allowed
462
- Err ( TypingError :: InvalidTypeAnnotation ( t. to_string ( ) ) . into ( ) )
463
- }
464
- }
465
-
466
436
pub ( crate ) fn from_ty ( ty : & Ty , heap : & ' v Heap ) -> Self {
467
437
TypeCompiledFactory :: alloc_ty ( ty, heap)
468
438
}
@@ -479,8 +449,6 @@ impl<'v> TypeCompiled<Value<'v>> {
479
449
Ok ( TypeCompiled :: from_ty ( & Ty :: tuple ( elems) , heap) )
480
450
} else if let Some ( t) = ListRef :: from_value ( ty) {
481
451
TypeCompiled :: from_list ( t, heap)
482
- } else if let Some ( t) = DictRef :: from_value ( ty) {
483
- TypeCompiled :: from_dict ( t, heap)
484
452
} else if ty. request_value :: < & dyn TypeCompiledDyn > ( ) . is_some ( ) {
485
453
// This branch is optimization: `TypeCompiledAsStarlarkValue` implements `eval_type`,
486
454
// but this branch avoids copying the type.
@@ -503,7 +471,9 @@ impl TypeCompiled<FrozenValue> {
503
471
}
504
472
505
473
fn invalid_type_annotation < ' v > ( ty : Value < ' v > , heap : & ' v Heap ) -> TypingError {
506
- if let Some ( name) = ty
474
+ if DictRef :: from_value ( ty) . is_some ( ) {
475
+ TypingError :: Dict
476
+ } else if let Some ( name) = ty
507
477
. get_attr ( "type" , heap)
508
478
. ok ( )
509
479
. flatten ( )
0 commit comments