rust-lang / rust-lang/rust

`include!()` code inside of a decl macro cannot access identifiers declared within the macro

Open
#118,475 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-bug
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Related issue: https://github.com/rust-sailfish/sailfish/issues/59#issuecomment-1833296032

Please test with the crate source code.
Test once with the original code, it works well.

And then modify the file examples/simple.rs as:

use sailfish::TemplateOnce;

macro_rules! template {
    ($name:ident, $path:literal, { $($field:ident: $type:ty),* } ) => {
        #[derive(TemplateOnce)]
        #[template(path=$path)]
        struct $name {
            $($field: $type,)*
        }
    }
}

template!(Simple, "simple.stpl", { messages: Vec<String> });

fn main() {
    let messages = vec![String::from("Message 1"), String::from("<Message 2>")];
    println!("{}", Simple { messages }.render_once().unwrap();
}

It will throw errors. The built codes between these two operations are TOTTALY the SAME.
So I guess it maybe is caused by the rustc compiler.

Meta

rustc --version --verbose:

rustc 1.74.0 (79e9716c9 2023-11-13)
Backtrace

Running `cargo +nightly run -p sailfish-examples --bin simple --message-format=json`...
   Compiling sailfish-compiler v0.8.3 (E:\Codes\Source\sailfish\sailfish-compiler)
   Compiling sailfish-macros v0.8.3 (E:\Codes\Source\sailfish\sailfish-macros)
   Compiling sailfish v0.8.3 (E:\Codes\Source\sailfish\sailfish)
   Compiling sailfish-examples v0.8.3 (E:\Codes\Source\sailfish\examples)
error[E0425]: cannot find value `__sf_buf` in this scope
 --> e:\Codes\Source\sailfish\target\debug\build\sailfish-compiler-6296a0e0a31320dd\out\templates\ac48a56bd5e90428-4d1386660dfbab60:2:29
  |
2 | { __sf_rt :: render_text ! (__sf_buf , "<!DOCTYPE html>\n<html>\n  <body>\n    \n    ") ; for (i , msg) in messages . iter () . enumerate...
  |                             ^^^^^^^^ not found in this scope

error[E0425]: cannot find value `__sf_buf` in this scope
 --> e:\Codes\Source\sailfish\target\debug\build\sailfish-compiler-6296a0e0a31320dd\out\templates\ac48a56bd5e90428-4d1386660dfbab60:2:170
  |
2 | ...iter () . enumerate () { __sf_rt :: render_text ! (__sf_buf , "\n      ") ; if i == 0 { __sf_rt :: render_text ! (__sf_buf , "\n      ...
  |                                                       ^^^^^^^^ not found in this scope

error[E0425]: cannot find value `__sf_buf` in this scope
 --> e:\Codes\Source\sailfish\target\debug\build\sailfish-compiler-6296a0e0a31320dd\out\templates\ac48a56bd5e90428-4d1386660dfbab60:2:233
  |
2 | ...\n      ") ; if i == 0 { __sf_rt :: render_text ! (__sf_buf , "\n        <h1>Hello, world!</h1>\n      ") ; } __sf_rt :: render_text !...
  |                                                       ^^^^^^^^ not found in this scope

error[E0425]: cannot find value `__sf_buf` in this scope
 --> e:\Codes\Source\sailfish\target\debug\build\sailfish-compiler-6296a0e0a31320dd\out\templates\ac48a56bd5e90428-4d1386660dfbab60:2:318
  |
2 | ...orld!</h1>\n      ") ; } __sf_rt :: render_text ! (__sf_buf , "\n      <div>") ; __sf_rt :: render_escaped ! (__sf_buf , * msg) ; __sf...
  |                                                       ^^^^^^^^ not found in this scope

error[E0425]: cannot find value `__sf_buf` in this scope
 --> e:\Codes\Source\sailfish\target\debug\build\sailfish-compiler-6296a0e0a31320dd\out\templates\ac48a56bd5e90428-4d1386660dfbab60:2:377
  |
2 | ... , "\n      <div>") ; __sf_rt :: render_escaped ! (__sf_buf , * msg) ; __sf_rt :: render_text ! (__sf_buf , "</div>\n    ") ; } __sf_r...
  |                                                       ^^^^^^^^ not found in this scope

error[E0425]: cannot find value `__sf_buf` in this scope
 --> e:\Codes\Source\sailfish\target\debug\build\sailfish-compiler-6296a0e0a31320dd\out\templates\ac48a56bd5e90428-4d1386660dfbab60:2:423
  |
2 | ...d ! (__sf_buf , * msg) ; __sf_rt :: render_text ! (__sf_buf , "</div>\n    ") ; } __sf_rt :: render_text ! (__sf_buf , "\n  </body>\n<...
  |                                                       ^^^^^^^^ not found in this scope

error[E0425]: cannot find value `__sf_buf` in this scope
 --> e:\Codes\Source\sailfish\target\debug\build\sailfish-compiler-6296a0e0a31320dd\out\templates\ac48a56bd5e90428-4d1386660dfbab60:2:480
  |
2 | ...uf , "</div>\n    ") ; } __sf_rt :: render_text ! (__sf_buf , "\n  </body>\n</html>") ; }
  |                                                       ^^^^^^^^ not found in this scope

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0425`.
error: could not compile `sailfish-examples` (bin "simple") due to 8 previous errors
Error: Cargo invocation failed.
        at t.Cargo.getCargoArtifacts (c:\Users\Sling\.vscode\extensions\vadimcn.vscode-lldb-1.10.0\extension.js:1:14943)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async Object.open (c:\Users\Sling\.vscode\extensions\vadimcn.vscode-lldb-1.10.0\extension.js:1:13253)
Caused by: Error: exit code: 101.
        at ChildProcess.<anonymous> (c:\Users\Sling\.vscode\extensions\vadimcn.vscode-lldb-1.10.0\extension.js:1:16610)
        at ChildProcess.emit (node:events:513:28)
        at ChildProcess.emit (node:domain:489:12)
        at maybeClose (node:internal/child_process:1091:16)
        at ChildProcess._handle.onexit (node:internal/child_process:302:5)
 *  The terminal process terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

Seems after using macro_rules, the include!(); cannot get the current scope variable "__sf_buf".
The root code is here: https://github.com/rust-sailfish/sailfish/blob/2e392994830084874b2695bf7953f245752d7d44/sailfish-compiler/src/procmacro.rs#L374

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure with the modified examples/simple.rs using cargo +nightly run -p sailfish-examples, then inspect the referenced sailfish-compiler/src/procmacro.rs around line 374. Compare the generated code and scope behavior for the original and macro_rules! cases. Done means the macro_rules! example no longer reports E0425 errors for __sf_buf.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.