From 1d084a1454d4d71699da1632abc4a1b9ecf4f9d6 Mon Sep 17 00:00:00 2001 From: Stiopa Koltsov Date: Sat, 12 Aug 2023 12:20:00 -0700 Subject: [PATCH] Prohibit {X: Y} as dict[X, Y] in type expressions Summary: It is still allowed in regular expressions like `isinstance(1, {"": ""})`. Reviewed By: ndmitchell Differential Revision: D48217281 fbshipit-source-id: 5f8caaab8430fb9a78a124c70f14616c6f734a35 --- starlark/src/eval/compiler/types.rs | 5 ----- starlark/src/syntax/type_expr.rs | 25 +------------------------ starlark/src/typing/ty.rs | 4 ---- 3 files changed, 1 insertion(+), 33 deletions(-) diff --git a/starlark/src/eval/compiler/types.rs b/starlark/src/eval/compiler/types.rs index c4fa26bba..15625d6ac 100644 --- a/starlark/src/eval/compiler/types.rs +++ b/starlark/src/eval/compiler/types.rs @@ -196,11 +196,6 @@ impl<'v> Compiler<'v, '_, '_> { let xs = xs.into_try_map(|x| self.eval_expr_as_type(x))?; Ok(TypeCompiled::type_any_of(xs, self.eval.heap())) } - TypeExprUnpackP::DictOf(k, v) => { - let k = self.eval_expr_as_type(*k)?; - let v = self.eval_expr_as_type(*v)?; - Ok(TypeCompiled::type_dict_of(k, v, self.eval.heap())) - } TypeExprUnpackP::Tuple(xs) => { let xs = xs.into_try_map(|x| self.eval_expr_as_type(x))?; Ok(TypeCompiled::type_tuple_of(xs, self.eval.heap())) diff --git a/starlark/src/syntax/type_expr.rs b/starlark/src/syntax/type_expr.rs index bdc122467..171133304 100644 --- a/starlark/src/syntax/type_expr.rs +++ b/starlark/src/syntax/type_expr.rs @@ -32,8 +32,6 @@ enum TypeExprUnpackError { InvalidType(&'static str), #[error("Empty list is not allowed in type expression")] EmptyListInType, - #[error("Only dict literal with single entry is allowed in type expression")] - DictNot1InType, #[error("Only dot expression of form `ident.ident` is allowed in type expression")] DotInType, } @@ -51,10 +49,6 @@ pub(crate) enum TypeExprUnpackP<'a, P: AstPayload> { Box>>, ), Union(Vec>>), - DictOf( - Box>>, - Box>>, - ), Tuple(Vec>>), Literal(Spanned<&'a str>), } @@ -182,24 +176,7 @@ impl<'a, P: AstPayload> TypeExprUnpackP<'a, P> { }) } } - ExprP::Dict(xs) => { - if xs.len() != 1 { - Err(EvalException::new( - TypeExprUnpackError::DictNot1InType.into(), - expr.span, - codemap, - )) - } else { - let (k, v) = &xs[0]; - Ok(Spanned { - span, - node: TypeExprUnpackP::DictOf( - Box::new(TypeExprUnpackP::unpack(k, codemap)?), - Box::new(TypeExprUnpackP::unpack(v, codemap)?), - ), - }) - } - } + ExprP::Dict(..) => err("dict"), ExprP::ListComprehension(..) => err("list comprehension"), ExprP::DictComprehension(..) => err("dict comprehension"), ExprP::FString(..) => err("f-string"), diff --git a/starlark/src/typing/ty.rs b/starlark/src/typing/ty.rs index 521550504..71f8592a9 100644 --- a/starlark/src/typing/ty.rs +++ b/starlark/src/typing/ty.rs @@ -456,10 +456,6 @@ impl Ty { TypeExprUnpackP::Union(xs) => { Ty::unions(xs.map(|x| Self::from_expr_impl(x, approximations))) } - TypeExprUnpackP::DictOf(k, v) => Ty::dict( - Self::from_expr_impl(k, approximations), - Self::from_expr_impl(v, approximations), - ), TypeExprUnpackP::Literal(x) => { if x.is_empty() || x.starts_with('_') { Ty::any()