0

I have the sources as:

src/
  main.rs
  memory.rs
  chunk.rs

memory.rs

I have a macro:

#[macro_export]
macro_rules! grow_capacity {
    ( $x:expr ) => {
        {
            if $x < 8 { 8 } else { $x * 2 }
        }
    };
}

chunk.rs

I want to use it:

use crate::memory;

// define struct Chunk

impl Chunk {
    pub fn write(&mut self, byte: u8) {
        // other lines of code.
        self.capacity = memory::grow_capacity!(self.capacity);
    }

It gives me error:

error[E0433]: failed to resolve: could not find `grow_capacity` in `memory`
  --> src/chunk.rs:27:28
   |
27 |             self.capacity = memory::grow_capacity!(self.capacity);
   |                                     ^^^^^^^^^^^^^ could not find `grow_capacity` in `memory`

To make the post short, I have the full code on Gist.

If I place the macro inside chunk.rs, then I can use it. I don't think How do I use a macro across module files? works. Has Rust changed its feature? I'm on Rust 1.44.1.

knh190
  • 2,744
  • 1
  • 16
  • 30
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Aug 03 '20 at 16:54
  • The accepted answer you linked to [works fine](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b2f7742f52b71036ab1d7a243eab9ef9). – Shepmaster Aug 03 '20 at 16:56
  • @Shepmaster It is separated into files, and it is a new generated project with only the code above. I have annotated lines of code with file name, I have used SO for years, and I know the reproducible rule. – knh190 Aug 03 '20 at 16:59
  • 1
    If it's "only the code above", then your code cannot possibly work as there's no `main.rs` or `lib.rs` with `mod` statements that bring in the files. The code you have provided **does not produce the error you are asking about**. Additionally, your error message contains code that your "reproduction" code doesn't (e.g. `self.capacity =`). – Shepmaster Aug 03 '20 at 17:01
  • My guess is that you didn't fully read / understand the linked answer, specifically this part: *Note: macros always live at the top-level of a crate* – Shepmaster Aug 03 '20 at 17:06
  • @Shepmaster main.rs is included now. And yes I read the line, but if I remove `memory::grow_capacity!` with `grow_capacity!`, it still does not exist in the namespace. – knh190 Aug 03 '20 at 17:07
  • @Shepmaster `self.capacity` here doesn't affect the macro's scope, so is that related? My question is still I couldn't use the macro in the same crate, not with the parameter passed in. I edited the post anyway. Hope it's clear now. – knh190 Aug 03 '20 at 17:12
  • The problem is that the **code you've provided** doesn't **produce the error you are asking about**. For example, the current code is malformed (missing a closing brace), but more importantly it uses a struct that isn't defined (`Chunk`). Thus, we cannot copy your code locally and see the same problem. That means that the problem is not **reproducible**. – Shepmaster Aug 03 '20 at 17:17
  • @Shepmaster for copy-paste use, I uploaded `./src` to [gist](https://gist.github.com/KHN190/1381ca098298a7793258acaacb583d58). This doesn't include the project toml, if that matters. See if it's reproducible now. Thanks. – knh190 Aug 03 '20 at 17:21
  • [To make Stack Overflow a useful resource for future visitors beyond the context of your repository](https://meta.stackoverflow.com/q/380194/155423), please [edit] your question to add a [MRE] in the question itself, in addition to the link to your repository. – Shepmaster Aug 03 '20 at 17:34
  • Your gist doesn't include a [MRE]. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. – Shepmaster Aug 03 '20 at 17:34
  • @Shepmaster I found the solution for updated version of Rust, and I'd post it on the linked question. – knh190 Aug 03 '20 at 17:43

0 Answers0