Skip to content

Commit 9eea1cb

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Prohibit strlit in type expr
Summary: Prohibit string literals in type expressions unless `starlark-rust: allow_string_literals_in_type_expr` annotation is present. Previous diffs in this stack add this annotations to every file in fbsource where the annotation is present. This is to: * track and migrate string literals in type expressions to proper types * prevent regressions Note this does not prohibit string constants in type expressions, but we need to start somewhere. Reviewed By: cjhopman Differential Revision: D48916740 fbshipit-source-id: 178a672af4f5454fd42525deb9d6d39b32361e1e
1 parent cacecb7 commit 9eea1cb

File tree

17 files changed

+81
-18
lines changed

17 files changed

+81
-18
lines changed

starlark/src/eval/compiler/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub(crate) struct Compiler<'v, 'a, 'e> {
8484
pub(crate) codemap: FrozenRef<'static, CodeMap>,
8585
pub(crate) check_types: bool,
8686
pub(crate) top_level_stmt_count: usize,
87+
pub(crate) allow_string_literals_in_type_expr: bool,
8788
}
8889

8990
impl Compiler<'_, '_, '_> {

starlark/src/eval/compiler/types.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,11 @@ impl<'v> Compiler<'v, '_, '_> {
237237
));
238238
}
239239
// This should not fail because we validated it at parse time.
240-
let unpack = TypeExprUnpackP::unpack(&type_expr.expr, &self.codemap)?;
240+
let unpack = TypeExprUnpackP::unpack(
241+
&type_expr.expr,
242+
&self.codemap,
243+
self.allow_string_literals_in_type_expr,
244+
)?;
241245
let type_value = self.eval_expr_as_type(unpack)?;
242246
type_expr.payload.compiler_ty = Some(type_value.as_ty().clone());
243247
Ok(())

starlark/src/eval/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl<'v, 'a> Evaluator<'v, 'a> {
6464
codemap,
6565
statement,
6666
dialect,
67+
allow_string_literals_in_type_expr,
6768
} = ast;
6869

6970
let codemap = self
@@ -126,6 +127,7 @@ impl<'v, 'a> Evaluator<'v, 'a> {
126127
eval: self,
127128
check_types: dialect.enable_types == DialectTypes::Enable,
128129
top_level_stmt_count,
130+
allow_string_literals_in_type_expr,
129131
};
130132

131133
let res = compiler.eval_module(cst, local_names);

starlark/src/tests/docs/rustdocs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ fn test_rustdoc() {
9999
let got = GlobalsBuilder::new().with(globals).build();
100100
let expected = assert::pass_module(
101101
r#"
102+
# @starlark-rust: allow_string_literals_in_type_expr
103+
102104
def args_kwargs(*args, **kwargs: typing.Any) -> None: pass
103105
def custom_types(arg1: str, arg2: "input") -> "output": pass
104106
def default_arg(arg1 = "_", arg2: typing.Any = None) -> list[str]: pass

starlark/src/typing/fill_types_for_lint.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ struct GlobalTypesBuilder<'a, 'v> {
117117
errors: Vec<TypingError>,
118118
module_scope_data: &'a ModuleScopeData<'a>,
119119
ctx: TypingOracleCtx<'a>,
120+
allow_string_literals_in_type_expr: bool,
120121
}
121122

122123
impl<'a, 'v> GlobalTypesBuilder<'a, 'v> {
@@ -658,8 +659,12 @@ impl<'a, 'v> GlobalTypesBuilder<'a, 'v> {
658659
}
659660

660661
fn ty_expr(&mut self, expr: &CstTypeExpr) -> Result<Ty, InternalError> {
661-
let x = TypeExprUnpackP::unpack(&expr.expr, self.ctx.codemap)
662-
.map_err(InternalError::from_eval_exception)?;
662+
let x = TypeExprUnpackP::unpack(
663+
&expr.expr,
664+
self.ctx.codemap,
665+
self.allow_string_literals_in_type_expr,
666+
)
667+
.map_err(InternalError::from_eval_exception)?;
663668
self.from_type_expr_impl(&x)
664669
}
665670

@@ -708,6 +713,7 @@ pub(crate) fn fill_types_for_lint_typechecker(
708713
ctx: TypingOracleCtx,
709714
module_scope_data: &ModuleScopeData,
710715
approximations: &mut Vec<Approximation>,
716+
allow_string_literals_in_type_expr: bool,
711717
) -> Result<(Vec<TypingError>, ModuleVarTypes), InternalError> {
712718
let heap = Heap::new();
713719
let mut builder = GlobalTypesBuilder {
@@ -717,6 +723,7 @@ pub(crate) fn fill_types_for_lint_typechecker(
717723
errors: Vec::new(),
718724
module_scope_data,
719725
approximations,
726+
allow_string_literals_in_type_expr,
720727
};
721728
for stmt in module.iter_mut() {
722729
builder.top_level_stmt(stmt)?;

starlark/src/typing/golden/call_callable_or_unknown.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# ```
66

77
Code:
8+
# @starlark-rust: allow_string_literals_in_type_expr
9+
810
def foo(x: [typing.Callable, "unknown"], y: ["unknown", typing.Callable]):
911
x()
1012
y()

starlark/src/typing/golden/call_not_callable_or_unknown.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# ```
66

77
Code:
8+
# @starlark-rust: allow_string_literals_in_type_expr
9+
810
def foo(x: [str, "unknown"], y: ["unknown", str]):
911
x()
1012
y()

starlark/src/typing/golden/call_unknown.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# ```
66

77
Code:
8+
# @starlark-rust: allow_string_literals_in_type_expr
9+
810
def foo(x: "unknown"):
911
x()
1012

starlark/src/typing/tests.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ fn test_call_unknown() {
332332
TypeCheck::new().check(
333333
"call_unknown",
334334
r#"
335+
# @starlark-rust: allow_string_literals_in_type_expr
336+
335337
def foo(x: "unknown"):
336338
x()
337339
"#,
@@ -355,6 +357,8 @@ fn test_call_callable_or_unknown() {
355357
TypeCheck::new().check(
356358
"call_callable_or_unknown",
357359
r#"
360+
# @starlark-rust: allow_string_literals_in_type_expr
361+
358362
def foo(x: [typing.Callable, "unknown"], y: ["unknown", typing.Callable]):
359363
x()
360364
y()
@@ -367,6 +371,8 @@ fn test_call_not_callable_or_unknown() {
367371
TypeCheck::new().check(
368372
"call_not_callable_or_unknown",
369373
r#"
374+
# @starlark-rust: allow_string_literals_in_type_expr
375+
370376
def foo(x: [str, "unknown"], y: ["unknown", str]):
371377
x()
372378
y()

starlark/src/typing/typecheck.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ impl AstModuleTypecheck for AstModule {
220220
oracle,
221221
&scope_data,
222222
&mut approximations,
223+
self.allow_string_literals_in_type_expr,
223224
) {
224225
Ok(fill_types_errors) => fill_types_errors,
225226
Err(e) => {

0 commit comments

Comments
 (0)