|
| 1 | +//@ proc-macro:macro_helpers.rs |
| 2 | +//@ compile-flags: --crate-type=lib |
| 3 | + |
| 4 | +/* There are 5 preludes and 3 namespaces. Test the order in which they are resolved. |
| 5 | + * See https://doc.rust-lang.org/nightly/reference/names/preludes.html. |
| 6 | + * |
| 7 | + * Macros cannot be in the type or value namespace. |
| 8 | + * Tools and extern crates cannot be in the macro or value namespace. |
| 9 | + * |
| 10 | + * Test the following truth tables: |
| 11 | +
|
| 12 | +Type: |
| 13 | +| ...... | tool | extern | macro | lang | libs | |
| 14 | +| tool | N/A | mirror |
| 15 | +| extern | extern | N/A | universe |
| 16 | +| macro | N/A | N/A | N/A | |
| 17 | +| lang | tool | extern | N/A | N/A | |
| 18 | +| libs | tool | extern | N/A | X | N/A | |
| 19 | +
|
| 20 | +Macro: |
| 21 | +| ...... | tool | extern | macro | lang | libs | |
| 22 | +| tool | N/A | mirror |
| 23 | +| extern | N/A | N/A | universe |
| 24 | +| macro | N/A | N/A | N/A | |
| 25 | +| lang | N/A | N/A | macro | N/A | |
| 26 | +| libs | N/A | N/A | macro | X | N/A | |
| 27 | +
|
| 28 | +Value: N/A. Only libs has items in the value namespace. |
| 29 | +
|
| 30 | +† ambiguous |
| 31 | +X don't care (controlled namespace with no overlap) |
| 32 | +
|
| 33 | +* Types are tested with `#[name::inner]`. Macros are tested with `#[name]`. |
| 34 | +* WARNING: I have found in testing that attribute macros give ambiguity errors in some contexts |
| 35 | +* instead of choosing a prelude. Have not been able to replicate. |
| 36 | +* |
| 37 | +* There should be 7 total tests. |
| 38 | +* See `rustc_resolve::ident::visit_scopes` for more information, |
| 39 | +* and for a definition of "controlled namespace". |
| 40 | +*/ |
| 41 | + |
| 42 | +#![feature(register_tool)] |
| 43 | + |
| 44 | +/* tool prelude */ |
| 45 | +#![register_tool(type_ns)] // extern prelude. type. |
| 46 | +#![register_tool(i8)] // lang prelude. type. |
| 47 | +#![register_tool(Sync)] // libs prelude. type. |
| 48 | + |
| 49 | +/* extern prelude */ |
| 50 | +extern crate macro_helpers as type_ns; // tool prelude. type. |
| 51 | +extern crate macro_helpers as usize; // lang prelude. type. |
| 52 | +extern crate macro_helpers as Option; // libs prelude. type. |
| 53 | + |
| 54 | +/* macro_use prelude */ |
| 55 | +#[macro_use] |
| 56 | +extern crate macro_helpers as _; |
| 57 | + |
| 58 | +/* lang and libs implicitly in scope */ |
| 59 | + |
| 60 | +// tool/extern -> extern |
| 61 | +#[type_ns::inner] //~ ERROR could not find `inner` in `type_ns` |
| 62 | +fn t1() {} |
| 63 | + |
| 64 | +// tool/lang -> tool |
| 65 | +#[i8::inner] // ok |
| 66 | +fn t2() {} |
| 67 | + |
| 68 | +// tool/libs -> tool |
| 69 | +#[Sync::not_real] // ok |
| 70 | +fn t3() {} |
| 71 | + |
| 72 | +// extern/lang -> extern |
| 73 | +#[usize::inner] //~ ERROR could not find `inner` in `usize` |
| 74 | +fn e1() {} // NOTE: testing with `-> usize` isn't valid, crates aren't considered in that scope |
| 75 | + // (unless they have generic arguments, for some reason.) |
| 76 | + |
| 77 | +// extern/libs -> extern |
| 78 | +// https://github.com/rust-lang/rust/issues/139095 |
| 79 | +fn e2() -> Option<i32> { None } //~ ERROR: expected type, found crate |
| 80 | + |
| 81 | +// macro/libs -> macro |
| 82 | +#[test] //~ ERROR mismatched types |
| 83 | +fn m1() {} |
| 84 | + |
| 85 | +// macro/lang -> macro |
| 86 | +#[global_allocator] //~ ERROR mismatched types |
| 87 | +fn m2() {} |
| 88 | + |
| 89 | +// lang/libs: no items that currently overlap, in either macro or type ns. |
0 commit comments