Skip to content

Non helpful error message when documenting with inner comment #30318

Closed
@est31

Description

@est31

This piece of code fails compilation (playbot):

use std::io;

//! This is a crate to say hello to the world

/// Main entry function
fn main() {
    println!("Hello, world!");
}

This is because the compiler expects inner comments only before the first item (something like this). It doesn't point this out however, instead, it gives the rather cryptic error message:

<anon>:3:1: 3:46 error: expected outer comment
<anon>:3 //! This is a crate to say hello to the world
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It should say something like "Please place the inner comment to the top".

From reading the code, an error message like this would be sufficient.

Can that be achieved by changing the if near line 38:

                if attr.node.style != ast::AttrStyle::Outer {
                  return Err(self.fatal("expected outer comment"));
                }

to something like

                match attr.node.style {
                  ast::AttrStyle::Inner => return Err(self.fatal("Expected outer comment. Inner comments are only allowed at the top, before any item.")),
                  ast::AttrStyle::Outer => (),
                  _ => return Err(self.fatal("expected outer comment")),
                }

? I haven't tested because I haven't set up a development setup of rustc (and don't know whether there is a better way to do it).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions