Skip to content

Commit 482cd56

Browse files
Add UI tests for path resolution errors in impl restriction lowering.
1 parent 25f2ebc commit 482cd56

4 files changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//@ compile-flags: --crate-type=lib
2+
3+
#![feature(impl_restriction)]
4+
#![expect(incomplete_features)]
5+
6+
pub mod a {
7+
pub enum E {}
8+
pub mod d {}
9+
pub mod b {
10+
pub mod c {}
11+
12+
// We do not use crate-relative paths here, since we follow the
13+
// "uniform paths" approach used for type/expression paths.
14+
pub impl(in a::b) trait T1 {} //~ ERROR cannot find module or crate `a` in this scope [E0433]
15+
16+
pub impl(in ::std) trait T2 {} //~ ERROR trait implementation can only be restricted to ancestor modules
17+
18+
pub impl(in self::c) trait T3 {} //~ ERROR trait implementation can only be restricted to ancestor modules
19+
20+
pub impl(in super::d) trait T4 {} //~ ERROR trait implementation can only be restricted to ancestor modules
21+
22+
pub impl(in crate::c) trait T5 {} //~ ERROR cannot find module `c` in the crate root [E0433]
23+
24+
pub impl(in super::E) trait T6 {} //~ ERROR expected module, found enum `super::E` [E0577]
25+
26+
pub impl(in super::super::super) trait T7 {} //~ ERROR too many leading `super` keywords [E0433]
27+
28+
// OK paths
29+
pub impl(crate) trait T8 {}
30+
pub impl(self) trait T9 {}
31+
pub impl(super) trait T10 {}
32+
pub impl(in crate::a) trait T11 {}
33+
pub impl(in super::super) trait T12 {}
34+
35+
// Unlike visibilities, we resolve paths later,
36+
// so paths referring to modules declared later can be resolved.
37+
pub impl(in self::f) trait L1 {} //~ ERROR trait implementation can only be restricted to ancestor modules
38+
39+
pub impl(in super::G) trait L2 {} //~ ERROR expected module, found enum `super::G` [E0577]
40+
41+
pub impl(in super::h) trait L3 {} //~ ERROR trait implementation can only be restricted to ancestor modules
42+
43+
pub mod f {}
44+
}
45+
46+
pub enum G {}
47+
pub mod h {}
48+
}
49+
50+
pub impl(in crate::a) trait T13 {} //~ ERROR trait implementation can only be restricted to ancestor modules
51+
52+
pub impl(in crate::a::E) trait T14 {} //~ ERROR expected module, found enum `crate::a::E` [E0577]
53+
54+
pub impl(crate) trait T15 {}
55+
pub impl(self) trait T16 {}
56+
57+
pub impl(super) trait T17 {} //~ ERROR too many leading `super` keywords [E0433]
58+
59+
// Unlike visibilities, we resolve paths later,
60+
// so paths referring to modules declared later can be resolved.
61+
pub impl(in crate::j) trait L4 {} //~ ERROR trait implementation can only be restricted to ancestor modules
62+
63+
pub impl(in crate::I) trait L5 {} //~ ERROR expected module, found enum `crate::I` [E0577]
64+
65+
pub enum I {}
66+
pub mod j {}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
error[E0433]: too many leading `super` keywords
2+
--> $DIR/restriction_resolution_errors.rs:26:35
3+
|
4+
LL | pub impl(in super::super::super) trait T7 {}
5+
| ^^^^^ there are too many leading `super` keywords
6+
7+
error[E0433]: too many leading `super` keywords
8+
--> $DIR/restriction_resolution_errors.rs:57:10
9+
|
10+
LL | pub impl(super) trait T17 {}
11+
| ^^^^^ there are too many leading `super` keywords
12+
13+
error[E0433]: cannot find module or crate `a` in this scope
14+
--> $DIR/restriction_resolution_errors.rs:14:21
15+
|
16+
LL | pub impl(in a::b) trait T1 {}
17+
| ^ use of unresolved module or unlinked crate `a`
18+
|
19+
help: there is a crate or module with a similar name
20+
|
21+
LL - pub impl(in a::b) trait T1 {}
22+
LL + pub impl(in c::b) trait T1 {}
23+
|
24+
help: consider importing this module
25+
|
26+
LL + use a;
27+
|
28+
29+
error: trait implementation can only be restricted to ancestor modules
30+
--> $DIR/restriction_resolution_errors.rs:16:21
31+
|
32+
LL | pub impl(in ::std) trait T2 {}
33+
| ^^^^^
34+
35+
error: trait implementation can only be restricted to ancestor modules
36+
--> $DIR/restriction_resolution_errors.rs:18:21
37+
|
38+
LL | pub impl(in self::c) trait T3 {}
39+
| ^^^^^^^
40+
41+
error: trait implementation can only be restricted to ancestor modules
42+
--> $DIR/restriction_resolution_errors.rs:20:21
43+
|
44+
LL | pub impl(in super::d) trait T4 {}
45+
| ^^^^^^^^
46+
47+
error[E0433]: cannot find module `c` in the crate root
48+
--> $DIR/restriction_resolution_errors.rs:22:28
49+
|
50+
LL | pub impl(in crate::c) trait T5 {}
51+
| ^ not found in the crate root
52+
53+
error[E0577]: expected module, found enum `super::E`
54+
--> $DIR/restriction_resolution_errors.rs:24:21
55+
|
56+
LL | pub impl(in super::E) trait T6 {}
57+
| ^^^^^^^^ not a module
58+
59+
error: trait implementation can only be restricted to ancestor modules
60+
--> $DIR/restriction_resolution_errors.rs:37:21
61+
|
62+
LL | pub impl(in self::f) trait L1 {}
63+
| ^^^^^^^
64+
65+
error[E0577]: expected module, found enum `super::G`
66+
--> $DIR/restriction_resolution_errors.rs:39:21
67+
|
68+
LL | pub impl(in super::G) trait L2 {}
69+
| ^^^^^^^^ not a module
70+
71+
error: trait implementation can only be restricted to ancestor modules
72+
--> $DIR/restriction_resolution_errors.rs:41:21
73+
|
74+
LL | pub impl(in super::h) trait L3 {}
75+
| ^^^^^^^^
76+
77+
error: trait implementation can only be restricted to ancestor modules
78+
--> $DIR/restriction_resolution_errors.rs:50:13
79+
|
80+
LL | pub impl(in crate::a) trait T13 {}
81+
| ^^^^^^^^
82+
83+
error[E0577]: expected module, found enum `crate::a::E`
84+
--> $DIR/restriction_resolution_errors.rs:52:13
85+
|
86+
LL | pub mod b {
87+
| --------- similarly named module `b` defined here
88+
...
89+
LL | pub impl(in crate::a::E) trait T14 {}
90+
| ^^^^^^^^^^^
91+
|
92+
help: a module with a similar name exists
93+
|
94+
LL - pub impl(in crate::a::E) trait T14 {}
95+
LL + pub impl(in crate::a::b) trait T14 {}
96+
|
97+
98+
error: trait implementation can only be restricted to ancestor modules
99+
--> $DIR/restriction_resolution_errors.rs:61:13
100+
|
101+
LL | pub impl(in crate::j) trait L4 {}
102+
| ^^^^^^^^
103+
104+
error[E0577]: expected module, found enum `crate::I`
105+
--> $DIR/restriction_resolution_errors.rs:63:13
106+
|
107+
LL | pub mod a {
108+
| --------- similarly named module `a` defined here
109+
...
110+
LL | pub impl(in crate::I) trait L5 {}
111+
| ^^^^^^^^
112+
|
113+
help: a module with a similar name exists
114+
|
115+
LL - pub impl(in crate::I) trait L5 {}
116+
LL + pub impl(in crate::a) trait L5 {}
117+
|
118+
119+
error: aborting due to 15 previous errors
120+
121+
Some errors have detailed explanations: E0433, E0577.
122+
For more information about an error, try `rustc --explain E0433`.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ compile-flags: --crate-type=lib
2+
3+
// Restrictions are resolved later, so we allow `use`d paths
4+
// <https://github.com/rust-lang/rust/issues/60552>
5+
6+
#![feature(impl_restriction)]
7+
#![expect(incomplete_features)]
8+
9+
mod m1 {
10+
pub impl(in crate::m2) trait T1 {} // OK
11+
}
12+
13+
use m1 as m2;
14+
15+
mod m3 {
16+
mod m4 {
17+
pub impl(in crate::m2) trait T2 {} //~ ERROR trait implementation can only be restricted to ancestor modules
18+
pub impl(in m6) trait T3 {} // OK
19+
pub impl(in m6::m5) trait T4 {} //~ ERROR trait implementation can only be restricted to ancestor modules
20+
pub impl(in m7) trait T5 {} //~ ERROR expected module, found enum `m7` [E0577]
21+
22+
use crate::m3 as m6;
23+
use crate::m3::E as m7;
24+
}
25+
mod m5 {}
26+
pub enum E {}
27+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: trait implementation can only be restricted to ancestor modules
2+
--> $DIR/restriction_resolution_use.rs:17:21
3+
|
4+
LL | pub impl(in crate::m2) trait T2 {}
5+
| ^^^^^^^^^
6+
7+
error: trait implementation can only be restricted to ancestor modules
8+
--> $DIR/restriction_resolution_use.rs:19:21
9+
|
10+
LL | pub impl(in m6::m5) trait T4 {}
11+
| ^^^^^^
12+
13+
error[E0577]: expected module, found enum `m7`
14+
--> $DIR/restriction_resolution_use.rs:20:21
15+
|
16+
LL | pub impl(in m7) trait T5 {}
17+
| ^^ not a module
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0577`.

0 commit comments

Comments
 (0)