Skip to content

Parser silent exit on invalid input #63288

Closed
@fkohlgrueber

Description

@fkohlgrueber

I'm using rustc's parser (parse_crate_mod more specifically) as part of a program I'm writing and for some inputs, the parser terminates my program.

Using the current nightly and fn test(a as the input, the following code terminates without printing the parsing result:

#![feature(rustc_private)]

extern crate syntax;
use std::rc::Rc;
use syntax::source_map::{FilePathMapping, SourceMap};
use syntax::parse::ParseSess;
use syntax::errors::emitter::ColorConfig;
use syntax::errors::Handler;

fn main() {
    let text = "fn test() ->".to_string();  // produces Err result
    let text = "fn test(a".to_string();     // exits silently

    syntax::with_globals(syntax::source_map::edition::Edition::Edition2018, || {
        let source_map = Rc::new(SourceMap::new(FilePathMapping::empty()));
        let parse_session = ParseSess::with_span_handler(
            Handler::with_tty_emitter(ColorConfig::Auto, true, None, Some(source_map.clone())),
            source_map
        );
        let parser = syntax::parse::maybe_new_parser_from_source_str(
            &parse_session,
            syntax::source_map::FileName::Custom("stdin".to_owned()),
            text,
        );

        println!("{:?}", parser.unwrap().parse_crate_mod().map_err(|mut x| {x.cancel(); x}));
    })
}

Full test repository

Output:

felix@the-machine:~/tmp/rust_parser_test$ cargo run
   Compiling rust_parser_test v0.1.0 (/home/felix/tmp/rust_parser_test)
    Finished dev [unoptimized + debuginfo] target(s) in 1.89s
     Running `target/debug/rust_parser_test`
error: this file contains an un-closed delimiter
 --> <stdin>:1:10
  |
1 | fn test(a
  |        - ^
  |        |
  |        un-closed delimiter

error: expected one of `:` or `@`, found `)`
 --> <stdin>:1:9
  |
1 | fn test(a
  |         ^ expected one of `:` or `@` here
  |
  = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: if this was a parameter name, give it a type
  |
1 | fn test(a: TypeName
  |         ^^^^^^^^^^^
help: if this is a type, explicitly ignore the parameter name
  |
1 | fn test(_: a
  |         ^^^^

felix@the-machine:~/tmp/rust_parser_test$ 

For other invalid code (e.g. fn test() ->), the parser correctly returns an Err result:

felix@the-machine:~/tmp/rust_parser_test$ cargo run
   Compiling rust_parser_test v0.1.0 (/home/felix/tmp/rust_parser_test)
    Finished dev [unoptimized + debuginfo] target(s) in 0.96s
     Running `target/debug/rust_parser_test`
Err(Diagnostic { level: Cancelled, message: [("expected type, found `<eof>`", NoStyle)], code: None, span: MultiSpan { primary_spans: [Span { lo: BytePos(10), hi: BytePos(12), ctxt: #0 }], span_labels: [(Span { lo: BytePos(10), hi: BytePos(12), ctxt: #0 }, "expected type")] }, children: [], suggestions: [] })
felix@the-machine:~/tmp/rust_parser_test$ 

Is this a bug or am I missing something?

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an AST

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions