Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upproc_macro! expansion can refer to items defined in the root w/o importing them #50504
Comments
alexcrichton
added
the
A-macros-2.0
label
May 7, 2018
aturon
referenced this issue
May 7, 2018
Closed
Tracking issue for RFC 1566: Procedural macros #38356
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov do you know if this might be related to #50050? The hygiene here is indeed quite suspicious |
This comment has been minimized.
This comment has been minimized.
|
This looks like a different issue to me at a first glance (but we need to check on older releases whether this is a regression or not). |
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov oh this is using the |
This comment has been minimized.
This comment has been minimized.
|
Looks like this is a bug on stable Rust -- // foo.rs
#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::*;
#[proc_macro_derive(Foo)]
pub fn foo1(a: TokenStream) -> TokenStream {
drop(a);
"mod __bar { static mut BAR: Option<Something> = None; }".parse().unwrap()
}// bar.rs
#[macro_use]
extern crate foo;
struct Something;
#[derive(Foo)]
struct Another;
fn main() {}
|
This comment has been minimized.
This comment has been minimized.
|
cc @nrc, you may want to take a look at this as well, especially because it's a bug on stable Rust which fixing may cause a lot of regressions |
This comment has been minimized.
This comment has been minimized.
|
This looks correct to me - aiui we use call-site hygiene for proc macros, and so we're using the hygiene context from where |
This comment has been minimized.
This comment has been minimized.
|
@nrc hm but we've been advocating that "hygiene" in expanded macros for 1.1/1.2 is basically "copy/paste" hygiene, but in this case it's not? |
This comment has been minimized.
This comment has been minimized.
|
No, this is very much a limitation of the hygiene algorithm here - in the proper hygiene setup when you create a new scope in a macro, you get a combination of the hygiene info for the new scope plus the 'base scope'. The intuition you want is that the base scope is the call-site (that gives 'copy and paste' hygiene). However, the macros 1.1/1.2 algorithm is essentially the base scope everywhere (i.e., we ignore any scopes introduced by the macro) and the base scope is the call site. |
This comment has been minimized.
This comment has been minimized.
|
@nrc |
This comment has been minimized.
This comment has been minimized.
|
Even more alarming example: // foo.rs
#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::*;
#[proc_macro_derive(Foo)]
pub fn foo1(_: TokenStream) -> TokenStream {
"
struct Outer;
mod inner {
type Inner = Outer; // `Outer` shouldn't be available from here
}
".parse().unwrap()
}// bar.rs
#![allow(unused)]
#[macro_use]
extern crate foo;
#[derive(Foo)] // OK?!
struct Dummy;
fn main() {}I.e. the current implementation of call-site hygiene effectively flattens any module structures existing inside the macro. |
This comment has been minimized.
This comment has been minimized.
|
Perhaps we need to add some extra feature gate for macros generating modules? |
This comment has been minimized.
This comment has been minimized.
|
Yeah if this is the current state of hygiene then I'd want to definitely gate modules from being in the output of procedural macros and procedural attributes. I'd ideally prefer to aggressviely start warning against modules in the output of custom derive as well. |
alexcrichton
referenced this issue
May 9, 2018
Closed
WIP: Gate generating `mod` items in procedural macros #50587
bors
added a commit
that referenced
this issue
May 9, 2018
This comment has been minimized.
This comment has been minimized.
|
I've posted #50587 to help evaluate the impact of forbidding modules from being generated by procedural derive macros |
bors
added a commit
that referenced
this issue
May 9, 2018
This comment has been minimized.
This comment has been minimized.
|
Given the crater results it seems clear that we cannot retroactively prevent custom derive from generating modules. Current "offenders" are:
My PR purely disallowed I'd still like to gate such features by default for maros 1.2 (attributes and bang macros) as I'd like to avoid worsening this problem further |
alexcrichton
added a commit
to alexcrichton/rust
that referenced
this issue
May 17, 2018
alexcrichton
referenced this issue
May 17, 2018
Merged
rustc: Disallow modules and macros in expansions #50820
This comment has been minimized.
This comment has been minimized.
|
I've opened #50820 to separately gate procedural bang and attribute macros, and will leave custom derive alone for now. |
alexcrichton
added a commit
to alexcrichton/rust
that referenced
this issue
May 17, 2018
alexcrichton
added a commit
to alexcrichton/rust
that referenced
this issue
May 18, 2018
bors
added a commit
that referenced
this issue
May 20, 2018
alexcrichton
added
the
C-bug
label
May 22, 2018
petrochenkov
referenced this issue
Jun 14, 2018
Closed
hygiene: Eliminate expansion hierarchy in favor of call-site hierarchy #51457
This comment has been minimized.
This comment has been minimized.
|
Looks like this issue is going to be fixed as well as a part of #50050. |
This comment has been minimized.
This comment has been minimized.
|
This is not #50050, but a separate issue after all.
EDIT: Expansion IDs remaining unassociated with positions in module system are indeed an issue (fixed in #51952), but the issue was caused by other reason, namely this fallback in name resolution rust/src/librustc_resolve/lib.rs Lines 1948 to 1961 in 0c0315c that worked for all proc macros 2.0 and declarative macros 2.0. #51952 removes this fallback from all macros except for proc macro derives, in which it's reported as a compatibility warning. |
petrochenkov
self-assigned this
Jun 25, 2018
petrochenkov
referenced this issue
Jun 30, 2018
Merged
hygiene: Decouple transparencies from expansion IDs #51952
This comment has been minimized.
This comment has been minimized.
|
Fixed in #51952 |
bors
added a commit
that referenced
this issue
Jun 30, 2018
bors
added a commit
that referenced
this issue
Jul 8, 2018
alexcrichton
added
the
A-macros-1.2
label
Jul 10, 2018
Mark-Simulacrum
added a commit
to Mark-Simulacrum/rust
that referenced
this issue
Jul 11, 2018
bors
closed this
in
#51952
Jul 11, 2018
jebrosen
added a commit
to jebrosen/Rocket
that referenced
this issue
Jul 13, 2018
jebrosen
referenced this issue
Jul 13, 2018
Closed
future-proof FromForm against scope resolution changes #705
adwhit
referenced this issue
Jul 17, 2018
Closed
generate_common_impl seems to be missing a use super::* #22
SergioBenitez
added a commit
to SergioBenitez/Rocket
that referenced
this issue
Jul 25, 2018
This comment has been minimized.
This comment has been minimized.
vityafx
commented
Aug 1, 2018
|
How can I fix these errors now? warning: cannot find type `users` in this scope
--> src/models.rs:45:37
|
45 | #[derive(Debug, Clone, AsChangeset, Insertable, Serialize, Deserialize)]
| ^^^^^^^^^^ names from parent modules are not accessible without an explicit import
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #50504 <https://github.com/rust-lang/rust/issues/50504> |
This comment has been minimized.
This comment has been minimized.
|
@vityafx unfortunately as a user you can't do anything to fix that error, the fix would need to be in diesel's |
This comment has been minimized.
This comment has been minimized.
lukedupin
commented
Aug 5, 2018
•
|
Here is an example of how to suppress the warning: |
apiraino
referenced this issue
Aug 9, 2018
Closed
names from parent modules are not accessible without an explicit import #1785
This comment has been minimized.
This comment has been minimized.
apiraino
commented
Aug 12, 2018
•
|
Nitpicking on @lukedupin solution; maybe better adding:
at the top of your files that trigger the warning. Setting an env variable somehow invalidates the compiler cache, therefore:
and:
are not the same and running one or the another will trigger a full (possible long) rebuild; f.e. your IDE may use |
petrochenkov
referenced this issue
Aug 17, 2018
Merged
Stabilize a few secondary macro features #53459
Mark-Simulacrum
added a commit
to Mark-Simulacrum/rust
that referenced
this issue
Aug 22, 2018
bors
added a commit
that referenced
this issue
Aug 23, 2018
bors
added a commit
that referenced
this issue
Aug 23, 2018
dereckson
referenced this issue
Aug 28, 2018
Closed
Resolution of macro derive behavior has been modified on 1.30 #1829
pgab
pushed a commit
to GiGainfosystems/diesel-oci
that referenced
this issue
Sep 5, 2018
This comment has been minimized.
This comment has been minimized.
xpe
commented
Sep 9, 2018
•
|
@apiraino I'd like to add a small but important correction to what you wrote, which suggested using an outer attribute, prefixed with
This uses an inner attribute, prefixed with |
This comment has been minimized.
This comment has been minimized.
Boscop
commented
Sep 24, 2018
|
Btw, I'm getting these kind of warnings also for diesel's AsChangeset, Insertable, Associations, Identifiable, Queryable and QueryableByName. |
japaric commentedMay 7, 2018
Apologies if this has already been reported; I didn't search too hard.
STR
This code compiles but I would expect it not to because the expanded form doesn't compile:
Meta
cc @jseyfried