include!(...) (still) does not see local variables #32379
Labels
A-macros
Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
C-feature-request
Category: A feature request, i.e: not implemented / a PR.
This is a dupe of #11390 from two years ago; the current behavior appears to be the same, and I just ran into it in my project. That issue was closed as expected behavior, but I don't think the behavior makes any sense.
The contents of
include!
d files, like the definitions of macros, have a separate hygiene scope and cannot reference local variables by name. However, whereas macros can be granted access to local names by passing them as parameters,include!
has no way to pass parameters, so as far as I can tell, included files essentially can't access locals at all. (Wrapping the included file in amacro_rules!
declaration doesn't work because the macro name itself is subject to hygiene, so it can't be called. There may be some more complex workaround but I can't figure it out.)Of course, later-compilation-stage approaches work, like having the include evaluate to a lambda expression which the including code then calls. Alternately, a build script can manually combine the two to produce a file containing the full module, which can then be included or specified with the
path
attribute on amod
declaration. But both of these have drawbacks and shouldn't be necessary wheninclude!
is right there.Even if the local variable limitation somehow weren't an issue, I'm skeptical that the benefits of hygiene apply to the way
include!
seems to be typically used in the first place: rarely, and usually as an artifact of the build process, a way to combine checked-in and generated code, rather than as a self-contained abstraction like macros tend to be.Unfortunately, at this point, changing the behavior of
include!
would be a breaking change, so I don't propose it. But it would be nice to have some sort of alternate form or argument to the macro that puts the included text in the same hygiene scope as the caller.The text was updated successfully, but these errors were encountered: