How to create a handler using ParseSess?


Background: I'm using rustc 1.53.0-nightly. I'm trying to create an AST using rustc. The first step is to create a parser. For this, I want to use new_parser_from_source_str. To create the parser, I also need to provide a ParseSess. Creating this ParseSess is where I'm having trouble.

Code: I'm executing the following:

    let codemap = Rc::new(SourceMap::new(source_map::FilePathMapping::empty()));
    let handler = Handler::with_emitter(false, None, Box::new(emitter));
    let parse_sess = ParseSess::with_span_handler(handler, codemap);

Note that the emitter has been borrowed from here (rather, whatever I'm doing, is pretty much this).

Error: Executing the above gives the following error:

thread 'main' panicked at 'cannot access a scoped thread local variable without calling `set` first', /mnt/c/Users/prgzz/.cargo/registry/src/

This problem is very similar to the one here. However, there doesn't seem to be a solution mentioned for it.

It would be great if someone could help me here, and, if not, direct me to the right place (if there is a more appropriate channel). I've been stuck with this for a few days now. I did come across a few related discussions, but none seems to address the problem properly.


From the top of my head, you probably need to call rustc_span::with_session_globals or similar (this sets the relevant scoped thread local variables that are not set, as the error you're seeing states), see racer/ at 8afa1c76abc1b5e8e884ac7ca0dd2f86fe956eff · racer-rust/racer · GitHub how Racer does it.

1 Like