Skip to content

Commit a62680d

Browse files
committed
Drop vis in FieldDef.
1 parent 4e8046f commit a62680d

File tree

7 files changed

+80
-62
lines changed

7 files changed

+80
-62
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
851851
// FIXME(jseyfried): positional field hygiene.
852852
None => Ident::new(sym::integer(index), self.lower_span(f.span)),
853853
},
854-
vis: self.lower_visibility(&f.vis),
854+
vis_span: self.lower_span(f.vis.span),
855855
ty,
856856
}
857857
}

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2671,8 +2671,8 @@ impl VisibilityKind<'_> {
26712671
#[derive(Debug, HashStable_Generic)]
26722672
pub struct FieldDef<'hir> {
26732673
pub span: Span,
2674+
pub vis_span: Span,
26742675
pub ident: Ident,
2675-
pub vis: Visibility<'hir>,
26762676
pub hir_id: HirId,
26772677
pub ty: &'hir Ty<'hir>,
26782678
}

compiler/rustc_lint/src/builtin.rs

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,66 +1384,63 @@ impl UnreachablePub {
13841384
cx: &LateContext<'_>,
13851385
what: &str,
13861386
def_id: LocalDefId,
1387-
vis: &hir::Visibility<'_>,
1388-
span: Span,
1387+
vis_span: Span,
13891388
exportable: bool,
13901389
) {
13911390
let mut applicability = Applicability::MachineApplicable;
1392-
match vis.node {
1393-
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(def_id) => {
1394-
if span.from_expansion() {
1395-
applicability = Applicability::MaybeIncorrect;
1391+
if !cx.access_levels.is_reachable(def_id) {
1392+
if vis_span.from_expansion() {
1393+
applicability = Applicability::MaybeIncorrect;
1394+
}
1395+
let def_span = cx.tcx.def_span(def_id);
1396+
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
1397+
let mut err = lint.build(&format!("unreachable `pub` {}", what));
1398+
let replacement = if cx.tcx.features().crate_visibility_modifier {
1399+
"crate"
1400+
} else {
1401+
"pub(crate)"
13961402
}
1397-
let def_span = cx.tcx.sess.source_map().guess_head_span(span);
1398-
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
1399-
let mut err = lint.build(&format!("unreachable `pub` {}", what));
1400-
let replacement = if cx.tcx.features().crate_visibility_modifier {
1401-
"crate"
1402-
} else {
1403-
"pub(crate)"
1404-
}
1405-
.to_owned();
1403+
.to_owned();
14061404

1407-
err.span_suggestion(
1408-
vis.span,
1409-
"consider restricting its visibility",
1410-
replacement,
1411-
applicability,
1412-
);
1413-
if exportable {
1414-
err.help("or consider exporting it for use by other crates");
1415-
}
1416-
err.emit();
1417-
});
1418-
}
1419-
_ => {}
1405+
err.span_suggestion(
1406+
vis_span,
1407+
"consider restricting its visibility",
1408+
replacement,
1409+
applicability,
1410+
);
1411+
if exportable {
1412+
err.help("or consider exporting it for use by other crates");
1413+
}
1414+
err.emit();
1415+
});
14201416
}
14211417
}
14221418
}
14231419

14241420
impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
14251421
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
1426-
self.perform_lint(cx, "item", item.def_id, &item.vis, item.span, true);
1422+
if cx.tcx.visibility(item.def_id).is_public() {
1423+
self.perform_lint(cx, "item", item.def_id, item.vis.span, true);
1424+
}
14271425
}
14281426

14291427
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'tcx>) {
1430-
self.perform_lint(
1431-
cx,
1432-
"item",
1433-
foreign_item.def_id,
1434-
&foreign_item.vis,
1435-
foreign_item.span,
1436-
true,
1437-
);
1428+
if cx.tcx.visibility(foreign_item.def_id).is_public() {
1429+
self.perform_lint(cx, "item", foreign_item.def_id, foreign_item.vis.span, true);
1430+
}
14381431
}
14391432

14401433
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
14411434
let def_id = cx.tcx.hir().local_def_id(field.hir_id);
1442-
self.perform_lint(cx, "field", def_id, &field.vis, field.span, false);
1435+
if cx.tcx.visibility(def_id).is_public() {
1436+
self.perform_lint(cx, "field", def_id, field.vis_span, false);
1437+
}
14431438
}
14441439

14451440
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
1446-
self.perform_lint(cx, "item", impl_item.def_id, &impl_item.vis, impl_item.span, false);
1441+
if cx.tcx.visibility(impl_item.def_id).is_public() {
1442+
self.perform_lint(cx, "item", impl_item.def_id, impl_item.vis.span, false);
1443+
}
14471444
}
14481445
}
14491446

compiler/rustc_passes/src/dead.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,24 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
354354
_: hir::HirId,
355355
_: rustc_span::Span,
356356
) {
357+
let tcx = self.tcx;
357358
let has_repr_c = self.repr_has_repr_c;
358359
let inherited_pub_visibility = self.inherited_pub_visibility;
359360
let pub_visibility = self.pub_visibility;
360-
let live_fields = def.fields().iter().filter(|f| {
361-
has_repr_c || (pub_visibility && (inherited_pub_visibility || f.vis.node.is_pub()))
361+
let live_fields = def.fields().iter().filter_map(|f| {
362+
let def_id = tcx.hir().local_def_id(f.hir_id);
363+
if has_repr_c {
364+
return Some(def_id);
365+
}
366+
if !pub_visibility {
367+
return None;
368+
}
369+
if inherited_pub_visibility {
370+
return Some(def_id);
371+
}
372+
if tcx.visibility(def_id).is_public() { Some(def_id) } else { None }
362373
});
363-
let hir = self.tcx.hir();
364-
self.live_symbols.extend(live_fields.map(|f| hir.local_def_id(f.hir_id)));
374+
self.live_symbols.extend(live_fields);
365375

366376
intravisit::walk_struct_def(self, def);
367377
}

compiler/rustc_privacy/src/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,9 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
660660
self.update_with_hir_id(ctor_hir_id, item_level);
661661
}
662662
for field in def.fields() {
663-
if field.vis.node.is_pub() {
663+
let def_id = self.tcx.hir().local_def_id(field.hir_id);
664+
let vis = self.tcx.visibility(def_id);
665+
if vis.is_public() {
664666
self.update_with_hir_id(field.hir_id, item_level);
665667
}
666668
}
@@ -1633,7 +1635,9 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
16331635
}
16341636

16351637
fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
1636-
if s.vis.node.is_pub() || self.in_variant {
1638+
let def_id = self.tcx.hir().local_def_id(s.hir_id);
1639+
let vis = self.tcx.visibility(def_id);
1640+
if vis.is_public() || self.in_variant {
16371641
intravisit::walk_field_def(self, s);
16381642
}
16391643
}

compiler/rustc_save_analysis/src/dump_visitor.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ macro_rules! down_cast_data {
5757
}
5858

5959
macro_rules! access_from {
60-
($save_ctxt:expr, $item:expr, $id:expr) => {
60+
($save_ctxt:expr, $id:expr) => {
6161
Access {
62-
public: $item.vis.node.is_pub(),
62+
public: $save_ctxt.tcx.visibility($id).is_public(),
6363
reachable: $save_ctxt.access_levels.is_reachable($id),
6464
}
6565
};
@@ -302,7 +302,7 @@ impl<'tcx> DumpVisitor<'tcx> {
302302
let field_data = self.save_ctxt.get_field_data(field, parent_id);
303303
if let Some(field_data) = field_data {
304304
self.dumper.dump_def(
305-
&access_from!(self.save_ctxt, field, self.tcx.hir().local_def_id(field.hir_id)),
305+
&access_from!(self.save_ctxt, self.tcx.hir().local_def_id(field.hir_id)),
306306
field_data,
307307
);
308308
}
@@ -369,7 +369,7 @@ impl<'tcx> DumpVisitor<'tcx> {
369369
v.process_formals(body.params, &fn_data.qualname);
370370
v.process_generic_params(ty_params, &fn_data.qualname, item.hir_id());
371371

372-
v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.def_id), fn_data);
372+
v.dumper.dump_def(&access_from!(v.save_ctxt, item.def_id), fn_data);
373373
}
374374

375375
for arg in decl.inputs {
@@ -393,7 +393,7 @@ impl<'tcx> DumpVisitor<'tcx> {
393393
self.nest_typeck_results(item.def_id, |v| {
394394
if let Some(var_data) = v.save_ctxt.get_item_data(item) {
395395
down_cast_data!(var_data, DefData, item.span);
396-
v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.def_id), var_data);
396+
v.dumper.dump_def(&access_from!(v.save_ctxt, item.def_id), var_data);
397397
}
398398
v.visit_ty(&typ);
399399
v.visit_expr(expr);
@@ -469,7 +469,11 @@ impl<'tcx> DumpVisitor<'tcx> {
469469
let fields_str = fields
470470
.iter()
471471
.filter_map(|f| {
472-
if include_priv_fields || f.vis.node.is_pub() {
472+
if include_priv_fields {
473+
return Some(f.ident.to_string());
474+
}
475+
let def_id = self.save_ctxt.tcx.hir().local_def_id(f.hir_id);
476+
if self.save_ctxt.tcx.visibility(def_id).is_public() {
473477
Some(f.ident.to_string())
474478
} else {
475479
None
@@ -487,7 +491,7 @@ impl<'tcx> DumpVisitor<'tcx> {
487491
let span = self.span_from_span(item.ident.span);
488492
let attrs = self.tcx.hir().attrs(item.hir_id());
489493
self.dumper.dump_def(
490-
&access_from!(self.save_ctxt, item, item.def_id),
494+
&access_from!(self.save_ctxt, item.def_id),
491495
Def {
492496
kind,
493497
id: id_from_def_id(item.def_id.to_def_id()),
@@ -527,7 +531,7 @@ impl<'tcx> DumpVisitor<'tcx> {
527531
};
528532
down_cast_data!(enum_data, DefData, item.span);
529533

530-
let access = access_from!(self.save_ctxt, item, item.def_id);
534+
let access = access_from!(self.save_ctxt, item.def_id);
531535

532536
for variant in enum_definition.variants {
533537
let name = variant.ident.name.to_string();
@@ -662,7 +666,7 @@ impl<'tcx> DumpVisitor<'tcx> {
662666
methods.iter().map(|i| id_from_def_id(i.id.def_id.to_def_id())).collect();
663667
let attrs = self.tcx.hir().attrs(item.hir_id());
664668
self.dumper.dump_def(
665-
&access_from!(self.save_ctxt, item, item.def_id),
669+
&access_from!(self.save_ctxt, item.def_id),
666670
Def {
667671
kind: DefKind::Trait,
668672
id,
@@ -724,7 +728,7 @@ impl<'tcx> DumpVisitor<'tcx> {
724728
fn process_mod(&mut self, item: &'tcx hir::Item<'tcx>) {
725729
if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
726730
down_cast_data!(mod_data, DefData, item.span);
727-
self.dumper.dump_def(&access_from!(self.save_ctxt, item, item.def_id), mod_data);
731+
self.dumper.dump_def(&access_from!(self.save_ctxt, item.def_id), mod_data);
728732
}
729733
}
730734

@@ -1147,7 +1151,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
11471151
hir::ItemKind::Use(path, hir::UseKind::Single) => {
11481152
let sub_span = path.segments.last().unwrap().ident.span;
11491153
if !self.span.filter_generated(sub_span) {
1150-
let access = access_from!(self.save_ctxt, item, item.def_id);
1154+
let access = access_from!(self.save_ctxt, item.def_id);
11511155
let ref_id = self.lookup_def_id(item.hir_id()).map(id_from_def_id);
11521156
let span = self.span_from_span(sub_span);
11531157
let parent =
@@ -1176,7 +1180,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
11761180
// we don't want to track anyway, since it's probably macro-internal `use`
11771181
if let Some(sub_span) = self.span.sub_span_of_star(item.span) {
11781182
if !self.span.filter_generated(item.span) {
1179-
let access = access_from!(self.save_ctxt, item, item.def_id);
1183+
let access = access_from!(self.save_ctxt, item.def_id);
11801184
let span = self.span_from_span(sub_span);
11811185
let parent =
11821186
self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id);
@@ -1249,7 +1253,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
12491253
let attrs = self.tcx.hir().attrs(item.hir_id());
12501254

12511255
self.dumper.dump_def(
1252-
&access_from!(self.save_ctxt, item, item.def_id),
1256+
&access_from!(self.save_ctxt, item.def_id),
12531257
Def {
12541258
kind: DefKind::Type,
12551259
id,
@@ -1443,7 +1447,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
14431447
}
14441448

14451449
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
1446-
let access = access_from!(self.save_ctxt, item, item.def_id);
1450+
let access = access_from!(self.save_ctxt, item.def_id);
14471451

14481452
match item.kind {
14491453
hir::ForeignItemKind::Fn(decl, _, ref generics) => {

src/tools/clippy/clippy_lints/src/exhaustive_items.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ impl LateLintPass<'_> for ExhaustiveItems {
7878
if !attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
7979
then {
8080
let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind {
81-
if v.fields().iter().any(|f| !f.vis.node.is_pub()) {
81+
if v.fields().iter().any(|f| {
82+
let def_id = cx.tcx.hir().local_def_id(f.hir_id);
83+
!cx.tcx.visibility(def_id).is_public()
84+
}) {
8285
// skip structs with private fields
8386
return;
8487
}

0 commit comments

Comments
 (0)