Skip to content

Commit 27163ce

Browse files
authored
Unrolled build for rust-lang#138840
Rollup merge of rust-lang#138840 - jyn514:precedence-order, r=wesleywiser rustc_resolve: Test the order that preludes are resolved This test is exhaustive. See attached truth table: ![image](https://github.com/user-attachments/assets/11fe703c-e114-48df-84f8-426b63395784) Companion PR to rust-lang/reference#1765.
2 parents 0b45675 + 41fdd28 commit 27163ce

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* macro namespace. */
2+
3+
extern crate proc_macro;
4+
use proc_macro::*;
5+
use std::str::FromStr;
6+
7+
const ERROR: &str = "fn helper() { \"helper\" }";
8+
// https://doc.rust-lang.org/nightly/std/prelude/v1/index.html#attributes
9+
// NOTE: all the bang macros in std are currently unstable.
10+
#[proc_macro_attribute] pub fn test // lang.
11+
(_: TokenStream, _: TokenStream) -> TokenStream {
12+
TokenStream::from_str("fn test_macro() { \"\" }").unwrap() }
13+
// https://doc.rust-lang.org/nightly/reference/attributes.html#built-in-attributes-index
14+
#[proc_macro_attribute] pub fn global_allocator // lang.
15+
(_: TokenStream, _: TokenStream) -> TokenStream {
16+
TokenStream::from_str("fn global_allocator_macro() { \"\" }").unwrap() }

tests/ui/resolve/prelude-order.rs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.

tests/ui/resolve/prelude-order.stderr

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0433]: failed to resolve: could not find `inner` in `type_ns`
2+
--> $DIR/prelude-order.rs:61:12
3+
|
4+
LL | #[type_ns::inner]
5+
| ^^^^^ could not find `inner` in `type_ns`
6+
7+
error[E0433]: failed to resolve: could not find `inner` in `usize`
8+
--> $DIR/prelude-order.rs:73:10
9+
|
10+
LL | #[usize::inner]
11+
| ^^^^^ could not find `inner` in `usize`
12+
13+
error[E0573]: expected type, found crate `Option`
14+
--> $DIR/prelude-order.rs:79:12
15+
|
16+
LL | fn e2() -> Option<i32> { None }
17+
| ^^^^^^^^^^^ not a type
18+
|
19+
help: consider importing this enum instead
20+
|
21+
LL + use std::option::Option;
22+
|
23+
24+
error[E0308]: mismatched types
25+
--> $DIR/prelude-order.rs:82:1
26+
|
27+
LL | #[test]
28+
| ^^^^^^^- help: try adding a return type: `-> &'static str`
29+
| |
30+
| expected `()`, found `&str`
31+
|
32+
= note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
33+
34+
error[E0308]: mismatched types
35+
--> $DIR/prelude-order.rs:86:1
36+
|
37+
LL | #[global_allocator]
38+
| ^^^^^^^^^^^^^^^^^^^- help: try adding a return type: `-> &'static str`
39+
| |
40+
| expected `()`, found `&str`
41+
|
42+
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)
43+
44+
error: aborting due to 5 previous errors
45+
46+
Some errors have detailed explanations: E0308, E0433, E0573.
47+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)