facebook / starlark-rust

A Rust implementation of the Starlark language
Apache License 2.0
699 stars 57 forks source link

enum/ record/ string types don't work anymore in standard dialect (doc question) #94

Closed benmkw closed 1 year ago

benmkw commented 1 year ago

the following code does not work anymore:

fn main() {
    let content = include_str!("./main.star");
    let ast = AstModule::parse(
        "main.star",
        content.to_owned(),
        &Dialect {
            enable_types: DialectTypes::Enable,
            enable_def: true,
            ..Dialect::Standard
        },
    )
    .unwrap();
    let globals = GlobalsBuilder::new().build();
    let module = Module::new();
    let mut eval = Evaluator::new(&module);
    eval.enable_static_typechecking(true);
    eval.eval_module(ast, &globals).unwrap();
}

rt = enum("one", "two")

 --> main.star:1:6
  |
1 | rt = enum("one", "two")
  |      ^^^^
  |

and also

def hello(input : str) -> str:
     return input + "world"
 --> main.star:1:19
  |
1 | def hello(input : str) -> str:
  |                   ^^^
  |

and

 --> main.star:1:1
  |
1 | print(hello("world"))
  | ^^^^^
  |

I saw a commit which changed str.type to str but could not find a way to enable it, probably I'm missing some basic config option?

stepancheg commented 1 year ago
GlobalsBuilder::new().build()

creates empty globals. I'm not sure this ever worked.

stepancheg commented 1 year ago

P.S. Next time when reporting bugs, please include full error. I think message (which is omitted) states that symbol is not resolved.

benmkw commented 1 year ago

thanks for the quick response, GlobalsBuilder::extended_by worked

I think message (which is omitted) states that symbol is not resolved.

somehow I only looked at the dialect type and thought that its settings would enable all the things because enable_types: DialectTypes::Enable seemed to me like it would include e.g. starlark::environment::LibraryExtension::RecordType but on second though thats of course not true cause it can be enabled with only custom types as well so these two settings are somewhat orthogonal.

on the other hand starlark::environment::LibraryExtension::Typing seems like it is closely related to Dialect.enable_types so maybe there is still an opportunity to unify both/ combine them somewhat

ndmitchell commented 1 year ago

The idea is that LibraryExtension is changing your standard library, whereas enable_types is changing your parser. You can still write some types without the LibraryExtension (e.g. str), it's just things like typing.Any that doesn't make a whole lot of sense without the other. I've created an internal patch to make each reference the other, which should be in the repo later today.