Compilation Error when Mixing Line and Block comment

e083aaf
Opened by Gurdit Singh Bedi at 2022-05-16 17:56:12

I have written a small program.

fn main() {
    println!("Hello, one!");
    //*
    println!("Hello, two!");
    //*/
    println!("Hello, three!");
}

The above program compiles and runs successfully. But.

fn main() {
    println!("Hello, one!");
    /*
    println!("Hello, two!");
    //*/
    println!("Hello, three!");
}

The above program does not compiles and produces this error: unterminated block comment. But as you can see the block comment is terminated.

This is a small trick to toggle comment a large group of statements by addition and removal of a /. C language, javascript and Rust Lang share the same type of commenting style ( // and /* */ ). This trick works in C lang ( I tried gcc and clang compiler) and javascript as well, but currently it is not working in Rust Lang.

  1. You can use

    fn main() {
        println!("Hello, one!");
        /*
        println!("Hello, two!");
        // */
        println!("Hello, three!");
    }
    

    Note the space

    Oli Scherer at 2017-10-25 10:45:57

  2. I don't think we can change this without a breaking change.

    bjorn3 at 2022-05-15 17:17:03

  3. We could improve the diagnostic by re-parsing and checking for */ more aggressively. Not sure if that's worth doing

    cc @rust-lang/wg-diagnostics

    Oli Scherer at 2022-05-16 06:23:01