Skip to content

Commit 656eec8

Browse files
committed
Auto merge of #97391 - Urgau:cfg_accessible, r=petrochenkov
Handle more cases in cfg_accessible This PR tries to handle more cases in the cfg_accessible implementation by only emitting a "not sure" error only if we have partially resolved a path. This PR also adds many tests for the "not sure" cases and for private items. r? `@petrochenkov`
2 parents 6dadfc0 + b76d112 commit 656eec8

9 files changed

+445
-49
lines changed

compiler/rustc_resolve/src/macros.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -443,11 +443,22 @@ impl<'a> ResolverExpand for Resolver<'a> {
443443
PathResult::NonModule(partial_res) if partial_res.unresolved_segments() == 0 => {
444444
return Ok(true);
445445
}
446+
PathResult::NonModule(..) |
447+
// HACK(Urgau): This shouldn't be necessary
448+
PathResult::Failed { is_error_from_last_segment: false, .. } => {
449+
self.session
450+
.struct_span_err(span, "not sure whether the path is accessible or not")
451+
.note("the type may have associated items, but we are currently not checking them")
452+
.emit();
453+
454+
// If we get a partially resolved NonModule in one namespace, we should get the
455+
// same result in any other namespaces, so we can return early.
456+
return Ok(false);
457+
}
446458
PathResult::Indeterminate => indeterminate = true,
447-
// FIXME: `resolve_path` is not ready to report partially resolved paths
448-
// correctly, so we just report an error if the path was reported as unresolved.
449-
// This needs to be fixed for `cfg_accessible` to be useful.
450-
PathResult::NonModule(..) | PathResult::Failed { .. } => {}
459+
// We can only be sure that a path doesn't exist after having tested all the
460+
// posibilities, only at that time we can return false.
461+
PathResult::Failed { .. } => {}
451462
PathResult::Module(_) => panic!("unexpected path resolution"),
452463
}
453464
}
@@ -456,10 +467,6 @@ impl<'a> ResolverExpand for Resolver<'a> {
456467
return Err(Indeterminate);
457468
}
458469

459-
self.session
460-
.struct_span_err(span, "not sure whether the path is accessible or not")
461-
.span_note(span, "`cfg_accessible` is not fully implemented")
462-
.emit();
463470
Ok(false)
464471
}
465472

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This test is a collection of test that should pass.
2+
//
3+
// check-fail
4+
5+
#![feature(cfg_accessible)]
6+
#![feature(trait_alias)]
7+
8+
trait TraitAlias = std::fmt::Debug + Send;
9+
10+
// FIXME: Currently shows "cannot determine" but should be `false`
11+
#[cfg_accessible(unresolved)] //~ ERROR cannot determine
12+
const C: bool = true;
13+
14+
// FIXME: Currently shows "not sure" but should be `false`
15+
#[cfg_accessible(TraitAlias::unresolved)] //~ ERROR not sure whether the path is accessible or not
16+
const D: bool = true;
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: not sure whether the path is accessible or not
2+
--> $DIR/cfg_accessible-bugs.rs:15:18
3+
|
4+
LL | #[cfg_accessible(TraitAlias::unresolved)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: the type may have associated items, but we are currently not checking them
8+
9+
error: cannot determine whether the path is accessible or not
10+
--> $DIR/cfg_accessible-bugs.rs:11:1
11+
|
12+
LL | #[cfg_accessible(unresolved)]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 2 previous errors
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
error: not sure whether the path is accessible or not
2+
--> $DIR/cfg_accessible-not_sure.rs:14:18
3+
|
4+
LL | #[cfg_accessible(Struct::existing)]
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: the type may have associated items, but we are currently not checking them
8+
9+
error: not sure whether the path is accessible or not
10+
--> $DIR/cfg_accessible-not_sure.rs:16:18
11+
|
12+
LL | #[cfg_accessible(Struct::unresolved)]
13+
| ^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: the type may have associated items, but we are currently not checking them
16+
17+
error: not sure whether the path is accessible or not
18+
--> $DIR/cfg_accessible-not_sure.rs:25:18
19+
|
20+
LL | #[cfg_accessible(Union::existing)]
21+
| ^^^^^^^^^^^^^^^
22+
|
23+
= note: the type may have associated items, but we are currently not checking them
24+
25+
error: not sure whether the path is accessible or not
26+
--> $DIR/cfg_accessible-not_sure.rs:27:18
27+
|
28+
LL | #[cfg_accessible(Union::unresolved)]
29+
| ^^^^^^^^^^^^^^^^^
30+
|
31+
= note: the type may have associated items, but we are currently not checking them
32+
33+
error: not sure whether the path is accessible or not
34+
--> $DIR/cfg_accessible-not_sure.rs:36:18
35+
|
36+
LL | #[cfg_accessible(Enum::Existing::existing)]
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^
38+
|
39+
= note: the type may have associated items, but we are currently not checking them
40+
41+
error: not sure whether the path is accessible or not
42+
--> $DIR/cfg_accessible-not_sure.rs:38:18
43+
|
44+
LL | #[cfg_accessible(Enum::Existing::unresolved)]
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
|
47+
= note: the type may have associated items, but we are currently not checking them
48+
49+
error: not sure whether the path is accessible or not
50+
--> $DIR/cfg_accessible-not_sure.rs:40:18
51+
|
52+
LL | #[cfg_accessible(Enum::unresolved)]
53+
| ^^^^^^^^^^^^^^^^
54+
|
55+
= note: the type may have associated items, but we are currently not checking them
56+
57+
error: not sure whether the path is accessible or not
58+
--> $DIR/cfg_accessible-not_sure.rs:50:18
59+
|
60+
LL | #[cfg_accessible(Trait::existing)]
61+
| ^^^^^^^^^^^^^^^
62+
|
63+
= note: the type may have associated items, but we are currently not checking them
64+
65+
error: not sure whether the path is accessible or not
66+
--> $DIR/cfg_accessible-not_sure.rs:52:18
67+
|
68+
LL | #[cfg_accessible(Trait::unresolved)]
69+
| ^^^^^^^^^^^^^^^^^
70+
|
71+
= note: the type may have associated items, but we are currently not checking them
72+
73+
error: not sure whether the path is accessible or not
74+
--> $DIR/cfg_accessible-not_sure.rs:59:18
75+
|
76+
LL | #[cfg_accessible(TypeAlias::existing)]
77+
| ^^^^^^^^^^^^^^^^^^^
78+
|
79+
= note: the type may have associated items, but we are currently not checking them
80+
81+
error: not sure whether the path is accessible or not
82+
--> $DIR/cfg_accessible-not_sure.rs:61:18
83+
|
84+
LL | #[cfg_accessible(TypeAlias::unresolved)]
85+
| ^^^^^^^^^^^^^^^^^^^^^
86+
|
87+
= note: the type may have associated items, but we are currently not checking them
88+
89+
error: not sure whether the path is accessible or not
90+
--> $DIR/cfg_accessible-not_sure.rs:70:18
91+
|
92+
LL | #[cfg_accessible(ForeignType::unresolved)]
93+
| ^^^^^^^^^^^^^^^^^^^^^^^
94+
|
95+
= note: the type may have associated items, but we are currently not checking them
96+
97+
error: not sure whether the path is accessible or not
98+
--> $DIR/cfg_accessible-not_sure.rs:79:18
99+
|
100+
LL | #[cfg_accessible(AssocType::AssocType::unresolved)]
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102+
|
103+
= note: the type may have associated items, but we are currently not checking them
104+
105+
error: not sure whether the path is accessible or not
106+
--> $DIR/cfg_accessible-not_sure.rs:84:18
107+
|
108+
LL | #[cfg_accessible(u8::unresolved)]
109+
| ^^^^^^^^^^^^^^
110+
|
111+
= note: the type may have associated items, but we are currently not checking them
112+
113+
error: not sure whether the path is accessible or not
114+
--> $DIR/cfg_accessible-not_sure.rs:86:18
115+
|
116+
LL | #[cfg_accessible(u8::is_ascii)]
117+
| ^^^^^^^^^^^^
118+
|
119+
= note: the type may have associated items, but we are currently not checking them
120+
121+
error: aborting due to 15 previous errors
122+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
error: not sure whether the path is accessible or not
2+
--> $DIR/cfg_accessible-not_sure.rs:14:18
3+
|
4+
LL | #[cfg_accessible(Struct::existing)]
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: the type may have associated items, but we are currently not checking them
8+
9+
error: not sure whether the path is accessible or not
10+
--> $DIR/cfg_accessible-not_sure.rs:16:18
11+
|
12+
LL | #[cfg_accessible(Struct::unresolved)]
13+
| ^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: the type may have associated items, but we are currently not checking them
16+
17+
error: not sure whether the path is accessible or not
18+
--> $DIR/cfg_accessible-not_sure.rs:25:18
19+
|
20+
LL | #[cfg_accessible(Union::existing)]
21+
| ^^^^^^^^^^^^^^^
22+
|
23+
= note: the type may have associated items, but we are currently not checking them
24+
25+
error: not sure whether the path is accessible or not
26+
--> $DIR/cfg_accessible-not_sure.rs:27:18
27+
|
28+
LL | #[cfg_accessible(Union::unresolved)]
29+
| ^^^^^^^^^^^^^^^^^
30+
|
31+
= note: the type may have associated items, but we are currently not checking them
32+
33+
error: not sure whether the path is accessible or not
34+
--> $DIR/cfg_accessible-not_sure.rs:36:18
35+
|
36+
LL | #[cfg_accessible(Enum::Existing::existing)]
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^
38+
|
39+
= note: the type may have associated items, but we are currently not checking them
40+
41+
error: not sure whether the path is accessible or not
42+
--> $DIR/cfg_accessible-not_sure.rs:38:18
43+
|
44+
LL | #[cfg_accessible(Enum::Existing::unresolved)]
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
46+
|
47+
= note: the type may have associated items, but we are currently not checking them
48+
49+
error: not sure whether the path is accessible or not
50+
--> $DIR/cfg_accessible-not_sure.rs:40:18
51+
|
52+
LL | #[cfg_accessible(Enum::unresolved)]
53+
| ^^^^^^^^^^^^^^^^
54+
|
55+
= note: the type may have associated items, but we are currently not checking them
56+
57+
error: not sure whether the path is accessible or not
58+
--> $DIR/cfg_accessible-not_sure.rs:50:18
59+
|
60+
LL | #[cfg_accessible(Trait::existing)]
61+
| ^^^^^^^^^^^^^^^
62+
|
63+
= note: the type may have associated items, but we are currently not checking them
64+
65+
error: not sure whether the path is accessible or not
66+
--> $DIR/cfg_accessible-not_sure.rs:52:18
67+
|
68+
LL | #[cfg_accessible(Trait::unresolved)]
69+
| ^^^^^^^^^^^^^^^^^
70+
|
71+
= note: the type may have associated items, but we are currently not checking them
72+
73+
error: not sure whether the path is accessible or not
74+
--> $DIR/cfg_accessible-not_sure.rs:59:18
75+
|
76+
LL | #[cfg_accessible(TypeAlias::existing)]
77+
| ^^^^^^^^^^^^^^^^^^^
78+
|
79+
= note: the type may have associated items, but we are currently not checking them
80+
81+
error: not sure whether the path is accessible or not
82+
--> $DIR/cfg_accessible-not_sure.rs:61:18
83+
|
84+
LL | #[cfg_accessible(TypeAlias::unresolved)]
85+
| ^^^^^^^^^^^^^^^^^^^^^
86+
|
87+
= note: the type may have associated items, but we are currently not checking them
88+
89+
error: not sure whether the path is accessible or not
90+
--> $DIR/cfg_accessible-not_sure.rs:70:18
91+
|
92+
LL | #[cfg_accessible(ForeignType::unresolved)]
93+
| ^^^^^^^^^^^^^^^^^^^^^^^
94+
|
95+
= note: the type may have associated items, but we are currently not checking them
96+
97+
error: not sure whether the path is accessible or not
98+
--> $DIR/cfg_accessible-not_sure.rs:79:18
99+
|
100+
LL | #[cfg_accessible(AssocType::AssocType::unresolved)]
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102+
|
103+
= note: the type may have associated items, but we are currently not checking them
104+
105+
error: not sure whether the path is accessible or not
106+
--> $DIR/cfg_accessible-not_sure.rs:84:18
107+
|
108+
LL | #[cfg_accessible(u8::unresolved)]
109+
| ^^^^^^^^^^^^^^
110+
|
111+
= note: the type may have associated items, but we are currently not checking them
112+
113+
error: not sure whether the path is accessible or not
114+
--> $DIR/cfg_accessible-not_sure.rs:86:18
115+
|
116+
LL | #[cfg_accessible(u8::is_ascii)]
117+
| ^^^^^^^^^^^^
118+
|
119+
= note: the type may have associated items, but we are currently not checking them
120+
121+
error: aborting due to 15 previous errors
122+

0 commit comments

Comments
 (0)