Rust cannot parse this code

From:
https://doc.rust-lang.org/reference/comments.html
"Nested block comments are supported."
The below are valid comments yet rust cannot parse it.
/*
//***
//***
*/

You opened 3 multi line comments, you need to close 3 times.

4 Likes

I didn't.
I have commented two single line comments with one block comment.

Try the compiler. And walk through what's happening in your head :

  1. Opening a multi line comment
  2. A / char
  3. Opening a multi line comment
  4. A / char
  5. Opening a multi line comment
  6. Closing a multi line comment

The examples in the reference are pretty explicit too.

Playground

1 Like

There is an inconsistency here.

OK:

//*
fn main() {}

Not OK:

/*
//*
*/
fn main() {}

OK:

/*
//*
*/
*/
fn main() {}

So //* is a single line comment outside of a scoped comment, but a scoped comment opener within a scoped comment. Not sure if there's anything to do about it given backwards compatibility, though.

4 Likes

It's a weird quirk for sure.

But does it really matter? Given that this is the first time I've even heard about it since Rust went 1.0 more than a Lustrum ago, I'd argue that in practice, it isn't a problem.

3 Likes

I do think it makes sense (maybe it's because I'm writing too many parsers :stuck_out_tongue:): once you open a block comment, the usual tokenization does not apply. So I would guess the parser does not recognize the sequence "//" inside the block, but only looks for "/*" (increasing the nesting) and "*/" (decreasing the nesting).

In fact, in any language with (possibly nested) block comments, I think the following is valid:

/*
//  */
fn hello() {}

Similarly //* parses because "//" starts a line comment, making the "*" irrelevant.

8 Likes

You'd have the same problem here:

fn cleanup() {
    println!("removing dir/*.o");
    ...whatever...
}

Try to comment out the println! line with a block comment...

This is why block comments are finicky, in any language that supports them, and you should prefer your IDE's "comment out" function which usually inserts line comments.

3 Likes

That doesn't change the fact that rust is unable to parse properly commented code

I thought having reliable compiler that is able to parse properly commented out code does matter.

I comment out code all the time. Haven't hit this since I started working with Rust over 5 years ago though. That's because of how my tooling happens to be set up: it just comments out code using // rather than mucking about with nested comments (FWIW, I haven't seen a language yet that supports nested comments without any quirks).

I'd expect any IDE or editor to do much the same, so my question to you is: how did you arrive at this?

Well the parser does work, it's just that apparently you're the first person (on this forum, at least) in over 5 years to come across this particular issue, which explains why it still exists. Apparently it might not be that easy to fix either.

Your definition of "parse" simply doesn't apply here. The content of comments is neither required not expected to be valid Rust code. Most comments aren't.

And special cases like "if it happens to parse as valid Rust, ignore /* within strings and comments" are not helping consistency.

If you wanted a comment that behaves like this, you'd have to introduce a new, separate comment type with separate syntax, that works only on code - a kind of semantic comment. You're welcome to propose this, but I doubt it'll find many supporters.

It's just not something that needs fixing, or can be fixed.

1 Like

Single line comments can't exist within block comments. Just curious, is there some language which support it?

https://doc.rust-lang.org/reference/comments.html

1 Like

This is contrary to the rust specification.

The parser may not be operating according to your intuition, but it is operating correctly.

The specification you linked has the exact parser definition for a BLOCK_COMMENT, and that definition does not include support for line comments inside it, only nested block comments.

Edit:

I can see advantages to having support for line comments - for instance, being able to block comment out code with a line comment like // /* inside of it, and have the compiler handle that.

But to be clear, that is an advantage of an alternate design, not something incorrect about rust.

There are also advantages to the current design. For instance, it allows this pattern:

// /*
some code I want to comment out frequently to test
// */

where only one line modification is required to comment out the given code:

/*
some code I want to comment out frequently to test
// */

I noticed this many years ago in C#.

By playing around mixing line and block comments, you can alter actual compiled code by 'commenting comments'. :smile: