Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc test accepts semicolon struct definition with braces #113362

Closed
rasmuspeders1 opened this issue Jul 5, 2023 · 4 comments
Closed

Doc test accepts semicolon struct definition with braces #113362

rasmuspeders1 opened this issue Jul 5, 2023 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@rasmuspeders1
Copy link

This doc test code compiles fine even though the code would not be accepted normally. Notice the semicolon in struct Example{}; this semicolon is not allowed in the actual code but is accepted in the doc example.

/// # Example
/// ```
/// struct Example{};
/// ```
struct Example{}

I would have expected a compile error when running the cargo test command.

Instead it compiles and runs the doc example without problems.

Meta

rustc --version --verbose:

rustc 1.72.0-nightly (839e9a6e1 2023-07-02)
binary: rustc
commit-hash: 839e9a6e1210934fd24b15548b811a97c77138fc
commit-date: 2023-07-02
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.5
@rasmuspeders1 rasmuspeders1 added the C-bug Category: This is a bug. label Jul 5, 2023
@rasmuspeders1
Copy link
Author

Sorry if this is a potential duplicate or if this is expected behavior.
I found issue #91398 which indicates that rust doc did indeed handle this as I would expect at some point earlier.

@lukas-code
Copy link
Member

I think this is expected behavior. Because the doctest contains no main function, it gets wrapped in an implicit main function and this is perfectly valid rust code:

fn main() {
    struct Example{};
}

It's an item statement, followed by an empty statement.

The issue you linked fails to compile, because its doctest does contain a main function and is therefore not wrapped in another main function.

Sources:

@rasmuspeders1
Copy link
Author

I see now. Thanks.
I was expecting to be able to rely on the doc test code always compiling if copy pasted and compiled as a standalone program.
I can see how I cannot always rely on this and how it is not something that can(or maybe even should) be fixed easily.

@jyn514
Copy link
Member

jyn514 commented Jul 5, 2023

i'm going to close this since it's not a bug.

@jyn514 jyn514 closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants