#Reference a module being generated in a macro

24 messages · Page 1 of 1 (latest)

harsh surge
#

I'm working on the file in https://pastebin.com/QDnJzukq (sorry about the mess, still trying to get things working). When I use it (usage is independent of the error, I get this error (from nightly):

error[E0433]: failed to resolve: use of undeclared crate or module `_gen_lists`
   --> 2015/src/day3.rs:7:1
    |
7   | #[aoc(day03)]
    | ^^^^^^^^^^^^^
    | |
    | use of undeclared crate or module `_gen_lists`
    | in this procedural macro expansion
    |
   ::: /home/minifig/projects/aoc-zen-runner/aoc-zen-runner-macros/src/lib.rs:548:1
    |
548 | pub fn aoc(args: TokenStream, item: TokenStream) -> TokenStream {
    | --------------------------------------------------------------- in this expansion of `#[aoc]`

The module _gen_lists is being generated in agg_to_solution_lists_mod , and it's the first thing getting called & output to the token stream. Unfortunately, I have hordes of code that need to reference _gen_lists for running the functions reference from there.

Am I no longer allowed to generate modules on the fly and reference those generated modules later in the macro? Or is there another way I need to go about this other than spitting out blocks of quoted code?

drowsy willow
harsh surge
#

That just spits out "not available", so give me a second to play around with cargo-expand

harsh surge
harsh surge
drowsy willow
#

I don't see any _gen_lists module

harsh surge
#

Yeah, that's odd....

drowsy willow
#

isn't it in super?

harsh surge
#

It should be. That would be where aoc would drop it once that macro gets the definition from agg_to_solution_lists_mod

drowsy willow
#

might have issues with order of macro execution?

harsh surge
#

I'm going back through the... large... pile of compiler errors to make sure the compiler's not trying to explain what's going on...

#

Sample un-expanded site:

#[aoc(day1)]
pub mod solutions {
    #[solution(p1, Bytes)]
    pub fn part1_bytes(input: &[u8]) -> i32 {
        input.iter().fold(0, |sum, c| match c {
            b'(' => sum + 1,
            b')' => sum - 1,
            _ => unreachable!(),
        })
    }

    #[solution(part1, Chars)]
    pub fn part1_chars(input: &str) -> i32 {
        input.chars().fold(0, |sum, c| match c {
            '(' => sum + 1,
            ')' => sum - 1,
            _ => unreachable!(),
        })
    }

    #[solution(part2, counter)]
    pub fn part2(input: &str) -> usize {
        let mut sum: u32 = 0;

        for (i, c) in input.as_bytes().iter().enumerate() {
            match c {
                b'(' => sum += 1,
                b')' => if let Some(s) = sum.checked_sub(1) {
                    sum = s;
                } else {
                    return i + 1;
                },
                _ => unreachable!(),
            }
        }

        unreachable!()
    }
}
#

So, the aoc macro drives everything, and it does generate & output the _gen_lists module before doing anything else...

#

OK, now why would the code generating the lists be failing...?

drowsy willow
#

likely the first error causing the rest to fail, if I had to guess

harsh surge
#

....Not precisely the first, but there were several legit problems...

#

Hypothesis: A module with only solutions (and no solvers) will cause agg_to_solution_lists_mod to output nothing. Now to figure out how to disprove that...

#
    if p1_data.len() == 0 && p2_data.len() == 0 {
        let err = agg_result.generators.keys().map(|k| {
            syn::Error::new(k.span(), "Could not locate solver for generator type.")
        }).fold(proc_macro2::TokenStream::new(), |mut acc, err| {
            let ts = err.to_compile_error();
            acc.extend(ts);
            acc
        });
        return err;
    }

This block return an empty token stream if the agg_result has no generators. Which would happen if there's only solutions in the module. 🤦