fornwall / rust-script

Run Rust files and expressions as scripts without any setup or compilation step.
https://rust-script.org
Apache License 2.0
1.27k stars 43 forks source link

Main function recognition #95

Closed plasticartsshow closed 1 year ago

plasticartsshow commented 1 year ago

Description

If I add a space after the function identifier main rust-script will not use the function.

Example

#!/usr/bin/env rust-script
//! Dependencies can be specified in the script file itself as follows:
//!
//! ```cargo
//! [dependencies]
//! rand = "0.8.0"
//! ```

use rand::prelude::*;

fn main () {
    let x: u64 = random();
    println!("A random number: {}", x);
}

This can be seen this in the following compiler warnings:

» rust-script script2.rs
warning: unused doc comment
  --> script2.rs:3:5
   |
2  | /      {
3  | |      //! Dependencies can be specified in the script file itself as follows:
   | | _____^
4  | || //!
5  | || //! ```cargo
6  | || //! [dependencies]
7  | || //! rand = "0.8.0"
8  | || //! ```
   | ||_______^
...  |
15 | |  }
16 | |      }
   | |______- rustdoc does not generate documentation for expressions
   |
   = help: use `//` for a plain comment
   = note: `#[warn(unused_doc_comments)]` on by default

warning: function `main` is never used
  --> script2.rs:12:4
   |
12 | fn main () {
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

Why is this an issue?

In my case, I generate rust scripts externally using quote, which injects these errant spaces into the output TokenStream.

As seen above, this is valid Rust code. I am opening this issue here because I assume it's relevant, but please let me know if I should close it and look somewhere else.

Notes

If you feel this is worth changing, I can try to fix it and open a PR. I'm not sure where to look for this problem though, tbh.

fornwall commented 1 year ago

@plasticartsshow Thanks! This should be fixed by https://github.com/fornwall/rust-script/pull/96, which is a part of the just released 0.25.0 version.

Let me know how it works!