Skip to content

Bug when mixing /**/ and // comments on * operatorΒ #82997

Open
@ALRBP

Description

@ALRBP

Take this code:

fn main()
{
    let mut x = 1;
    let y = &mut x;
    for _ in 0..10
    {
        x+=1;
    }
    for _ in 0..10
    {
        *y+=1;
    }
    println!("{}",*y)
}

It compiles.

You can comment both increment lines with // comments:

fn main()
{
    let mut x = 1;
    let y = &mut x;
    for _ in 0..10
    {
        //x+=1;
    }
    for _ in 0..10
    {
        //*y+=1;
    }
    println!("{}",*y)
}

It compiles.

You can comment both for loops with /**/ comments:

fn main()
{
    let mut x = 1;
    let y = &mut x;
    /*for _ in 0..10
    {
        x+=1;
    }*/
    /*for _ in 0..10
    {
        *y+=1;
    }*/
    println!("{}",*y)
}

It compiles.

You can mix /**/ comment on for loops and increment lines:

fn main()
{
    let mut x = 1;
    let y = &mut x;
    /*for _ in 0..10
    {
        /*x+=1;*/
    }*/
    /*for _ in 0..10
    {
        /**y+=1;*/
    }*/
    println!("{}",*y)
}

It compiles.

You can also mix /**/ and // comments on the first for loop and its increment:

fn main()
{
    let mut x = 1;
    let y = &mut x;
    /*for _ in 0..10
    {
        //x+=1;
    }*/
    for _ in 0..10
    {
        *y+=1;
    }
    println!("{}",*y)
}

It compiles.

But if you try to mix /**/ and // comments on the second for loop and its increment:

fn main()
{
    let mut x = 1;
    let y = &mut x;
    for _ in 0..10
    {
        x+=1;
    }
    /*for _ in 0..10
    {
        //*y+=1;
    }*/
    println!("{}",*y)
}

It does not compile

error[E0758]: unterminated block comment
  --> src/main.rs:9:5
   |
9  | /     /*for _ in 0..10
10 | |     {
11 | |         //*y+=1;
12 | |     }*/
13 | |     println!("{}",*y)
14 | | }
   | |__^

error: aborting due to previous error

It seems that, when parsing a /**/ comment, rustc does not recognize // comments. This is not an issue in general but in the particular case where // is followed by a dereference operator * ("//*"), then rustc reads "/*" and takes this as another level of /**/ comment.

I am not an expert in Rust and I know that /**/ comments are not idiomatic, but I still think this should not happen.
I noticed that my syntax highlighter (KSyntaxHighlighting) makes the same mistake, but it is likely that it is still a bug that happen to be present in both, rustc and the Rust language description of KSyntaxHighlighting (same with GitHub's). Maybe I am wrong and this is intended but, in that case, I would like to know. I have read nothing about mixing /**/ and // comments but if it works in most cases, having particular cases where it does not work is a confusing behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-grammarArea: The grammar of RustC-discussionCategory: Discussion or questions that doesn't represent real issues.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions