Skip to content

Using doc comment at end of line triggers inner attribute error #80326

@landaire

Description

@landaire

I tried this code:

fn main() {
    if true {//!= bar
    println!("test");
    }
}

I expected to see this happen: the code should compile

Instead, this happened: the code does not compile with an error.

error: an inner attribute is not permitted in this context
 --> src/main.rs:2:14
  |
2 |     if true {//!= bar
  |              ^^^^^^^^
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.

This behavior surprised me since I tripped this compiler error after I commented out the right-hand side of a condition expression. IMO the error should either be clear that this is being parsed as a doc comment or should interpret this as a comment when not at the start of the line.

Meta

rustc --version --verbose:

rustc 1.49.0-nightly (8dae8cdcc 2020-10-12)
binary: rustc
commit-hash: 8dae8cdcc8fa879cea6a4bbbfa5b32e97be4c306
commit-date: 2020-10-12
host: x86_64-apple-darwin
release: 1.49.0-nightly
LLVM version: 11.0

This bug affects the latest stable as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-parserArea: The lexing & parsing of Rust source code to an ASTC-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