Skip to content

Commit dc17e5d

Browse files
petrochenkovMark-Simulacrum
authored andcommitted
resolve: Resolve visibilities on fields with non-builtin attributes
1 parent bb75c20 commit dc17e5d

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/librustc_resolve/build_reduced_graph.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ impl<'a> AsMut<Resolver<'a>> for BuildReducedGraphVisitor<'a, '_> {
194194

195195
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
196196
fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility {
197+
self.resolve_visibility_speculative(vis, false)
198+
}
199+
200+
fn resolve_visibility_speculative(
201+
&mut self,
202+
vis: &ast::Visibility,
203+
speculative: bool,
204+
) -> ty::Visibility {
197205
let parent_scope = &self.parent_scope;
198206
match vis.node {
199207
ast::VisibilityKind::Public => ty::Visibility::Public,
@@ -241,13 +249,15 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
241249
&segments,
242250
Some(TypeNS),
243251
parent_scope,
244-
true,
252+
!speculative,
245253
path.span,
246254
CrateLint::SimplePath(id),
247255
) {
248256
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
249257
let res = module.res().expect("visibility resolved to unnamed block");
250-
self.r.record_partial_res(id, PartialRes::new(res));
258+
if !speculative {
259+
self.r.record_partial_res(id, PartialRes::new(res));
260+
}
251261
if module.is_normal() {
252262
if res == Res::Err {
253263
ty::Visibility::Public
@@ -742,7 +752,10 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
742752

743753
// Record field names for error reporting.
744754
let field_names = struct_def.fields().iter().map(|field| {
745-
let field_vis = self.resolve_visibility(&field.vis);
755+
// NOTE: The field may be an expansion placeholder, but expansion sets correct
756+
// visibilities for unnamed field placeholders specifically, so the constructor
757+
// visibility should still be determined correctly.
758+
let field_vis = self.resolve_visibility_speculative(&field.vis, true);
746759
if ctor_vis.is_at_least(field_vis, &*self.r) {
747760
ctor_vis = field_vis;
748761
}
@@ -769,7 +782,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
769782

770783
// Record field names for error reporting.
771784
let field_names = vdata.fields().iter().map(|field| {
772-
self.resolve_visibility(&field.vis);
773785
respan(field.span, field.ident.map_or(kw::Invalid, |ident| ident.name))
774786
}).collect();
775787
let item_def_id = self.r.definitions.local_def_id(item.id);
@@ -1279,6 +1291,7 @@ impl<'a, 'b> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b> {
12791291
if sf.is_placeholder {
12801292
self.visit_invoc(sf.id);
12811293
} else {
1294+
self.resolve_visibility(&sf.vis);
12821295
visit::walk_struct_field(self, sf);
12831296
}
12841297
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Non-builtin attributes do not mess with field visibility resolution (issue #67006).
2+
3+
mod internal {
4+
struct S {
5+
#[rustfmt::skip]
6+
pub(in crate::internal) field: u8 // OK
7+
}
8+
9+
struct Z(
10+
#[rustfmt::skip]
11+
pub(in crate::internal) u8 // OK
12+
);
13+
}
14+
15+
struct S {
16+
#[rustfmt::skip]
17+
pub(in nonexistent) field: u8 //~ ERROR failed to resolve
18+
}
19+
20+
struct Z(
21+
#[rustfmt::skip]
22+
pub(in nonexistent) u8 //~ ERROR failed to resolve
23+
//~| ERROR cannot determine resolution for the visibility
24+
);
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0578]: cannot determine resolution for the visibility
2+
--> $DIR/field-attributes-vis-unresolved.rs:22:12
3+
|
4+
LL | pub(in nonexistent) u8
5+
| ^^^^^^^^^^^
6+
7+
error[E0433]: failed to resolve: maybe a missing crate `nonexistent`?
8+
--> $DIR/field-attributes-vis-unresolved.rs:17:12
9+
|
10+
LL | pub(in nonexistent) field: u8
11+
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
12+
13+
error[E0433]: failed to resolve: maybe a missing crate `nonexistent`?
14+
--> $DIR/field-attributes-vis-unresolved.rs:22:12
15+
|
16+
LL | pub(in nonexistent) u8
17+
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
18+
19+
error: aborting due to 3 previous errors
20+
21+
Some errors have detailed explanations: E0433, E0578.
22+
For more information about an error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)