“Truly Hygienic” Let Statements in Rust (sabrinajewson.org)
from hallettj@leminal.space to rust@programming.dev on 28 Sep 2024 21:33
https://leminal.space/post/10779898

A short post on how variable names can leak out of macros if there is a name collision with a constant. I thought this was a delightful read!

#rust

threaded - newest

ExperimentalGuy@programming.dev on 28 Sep 2024 21:51 next collapse

That was such a cute lil post

livingcoder@programming.dev on 28 Sep 2024 22:52 next collapse

This was a great post, but is the last state of the macro actually bad for performance in any way? I get that it’s ugly (and we should only choose to make code less readable like this when there’s actually an issue) but is it worse for runtime performance?

TehPers@beehaw.org on 29 Sep 2024 00:03 next collapse

Adding a single unused function should no effect on runtime performance. The compiler removes dead code during compilation, and there’s no concept at runtime anyway of “creating a function” since it’s just a compile-time construct to group reusable code (generally speaking - yes the pedants will be right when they say functions appear in the compiled output, to some extent).

Anyway, this can all be tested on Godbolt anyway if you want to verify yourself. Make a function with and without a nested unused function and check the output.

arendjr@programming.dev on 29 Sep 2024 09:52 collapse

Runtime performance is entirely unaffected by the use of macros. It can have a negative impact on compile-time performance though, if you overdo it.

livingcoder@programming.dev on 29 Sep 2024 15:53 collapse

I understand that the macro only affects compile time but I’m talking about the extra function that’s included in the resulting source code when the macro is expanded during compile. Based on other feedback, it looks like the unused function is optimized away.

arendjr@programming.dev on 29 Sep 2024 17:34 collapse

Ah yes, exactly.

BB_C@programming.dev on 29 Sep 2024 02:58 collapse

Maybe a good idea for a post. But the amount of reaches required makes this icky.

  • Pretending people write:
    let Ok(x) = read_input() else { return Err(Error) };
    
    instead of
     let x = read_input().map_err(|_| ...)?;
    
  • Pretending people write:
     const x: &str = "...";
    
    instead of
     const X: &str = "...";
    
  • Pretending there exist people who have such knowledge of rust macros hygiene, ident namespaces, etc, but somehow don’t know about how macro code expands (the “shock” about the compile error).

Maybe there is a reason after all why almost no one (maybe no one, period) was ever in that situation.

BB_C@programming.dev on 29 Sep 2024 03:01 collapse

Also:

A short post on how variable names can leak out of macros

I don’t think you understood the blog OP!