Skip to content

Commit

Permalink
Prohibit {X: Y} as dict[X, Y] in type expressions
Browse files Browse the repository at this point in the history
Summary: It is still allowed in regular expressions like `isinstance(1, {"": ""})`.

Reviewed By: ndmitchell

Differential Revision: D48217281

fbshipit-source-id: 5f8caaab8430fb9a78a124c70f14616c6f734a35
  • Loading branch information
stepancheg authored and facebook-github-bot committed Aug 12, 2023
1 parent b0830b2 commit 1d084a1
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 33 deletions.
5 changes: 0 additions & 5 deletions starlark/src/eval/compiler/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
25 changes: 1 addition & 24 deletions starlark/src/syntax/type_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -51,10 +49,6 @@ pub(crate) enum TypeExprUnpackP<'a, P: AstPayload> {
Box<Spanned<TypeExprUnpackP<'a, P>>>,
),
Union(Vec<Spanned<TypeExprUnpackP<'a, P>>>),
DictOf(
Box<Spanned<TypeExprUnpackP<'a, P>>>,
Box<Spanned<TypeExprUnpackP<'a, P>>>,
),
Tuple(Vec<Spanned<TypeExprUnpackP<'a, P>>>),
Literal(Spanned<&'a str>),
}
Expand Down Expand Up @@ -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"),
Expand Down
4 changes: 0 additions & 4 deletions starlark/src/typing/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1d084a1

Please sign in to comment.