Drop vis in FieldDef.

This commit is contained in:
Camille GILLOT 2022-02-13 01:54:13 +01:00
parent 4e8046f67a
commit a62680d108
7 changed files with 81 additions and 63 deletions

View File

@ -851,7 +851,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// FIXME(jseyfried): positional field hygiene.
None => Ident::new(sym::integer(index), self.lower_span(f.span)),
},
vis: self.lower_visibility(&f.vis),
vis_span: self.lower_span(f.vis.span),
ty,
}
}

View File

@ -2671,8 +2671,8 @@ impl VisibilityKind<'_> {
#[derive(Debug, HashStable_Generic)]
pub struct FieldDef<'hir> {
pub span: Span,
pub vis_span: Span,
pub ident: Ident,
pub vis: Visibility<'hir>,
pub hir_id: HirId,
pub ty: &'hir Ty<'hir>,
}

View File

@ -1384,66 +1384,63 @@ impl UnreachablePub {
cx: &LateContext<'_>,
what: &str,
def_id: LocalDefId,
vis: &hir::Visibility<'_>,
span: Span,
vis_span: Span,
exportable: bool,
) {
let mut applicability = Applicability::MachineApplicable;
match vis.node {
hir::VisibilityKind::Public if !cx.access_levels.is_reachable(def_id) => {
if span.from_expansion() {
applicability = Applicability::MaybeIncorrect;
}
let def_span = cx.tcx.sess.source_map().guess_head_span(span);
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
let mut err = lint.build(&format!("unreachable `pub` {}", what));
let replacement = if cx.tcx.features().crate_visibility_modifier {
"crate"
} else {
"pub(crate)"
}
.to_owned();
err.span_suggestion(
vis.span,
"consider restricting its visibility",
replacement,
applicability,
);
if exportable {
err.help("or consider exporting it for use by other crates");
}
err.emit();
});
if !cx.access_levels.is_reachable(def_id) {
if vis_span.from_expansion() {
applicability = Applicability::MaybeIncorrect;
}
_ => {}
let def_span = cx.tcx.def_span(def_id);
cx.struct_span_lint(UNREACHABLE_PUB, def_span, |lint| {
let mut err = lint.build(&format!("unreachable `pub` {}", what));
let replacement = if cx.tcx.features().crate_visibility_modifier {
"crate"
} else {
"pub(crate)"
}
.to_owned();
err.span_suggestion(
vis_span,
"consider restricting its visibility",
replacement,
applicability,
);
if exportable {
err.help("or consider exporting it for use by other crates");
}
err.emit();
});
}
}
}
impl<'tcx> LateLintPass<'tcx> for UnreachablePub {
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
self.perform_lint(cx, "item", item.def_id, &item.vis, item.span, true);
if cx.tcx.visibility(item.def_id).is_public() {
self.perform_lint(cx, "item", item.def_id, item.vis.span, true);
}
}
fn check_foreign_item(&mut self, cx: &LateContext<'_>, foreign_item: &hir::ForeignItem<'tcx>) {
self.perform_lint(
cx,
"item",
foreign_item.def_id,
&foreign_item.vis,
foreign_item.span,
true,
);
if cx.tcx.visibility(foreign_item.def_id).is_public() {
self.perform_lint(cx, "item", foreign_item.def_id, foreign_item.vis.span, true);
}
}
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
let def_id = cx.tcx.hir().local_def_id(field.hir_id);
self.perform_lint(cx, "field", def_id, &field.vis, field.span, false);
if cx.tcx.visibility(def_id).is_public() {
self.perform_lint(cx, "field", def_id, field.vis_span, false);
}
}
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
self.perform_lint(cx, "item", impl_item.def_id, &impl_item.vis, impl_item.span, false);
if cx.tcx.visibility(impl_item.def_id).is_public() {
self.perform_lint(cx, "item", impl_item.def_id, impl_item.vis.span, false);
}
}
}

View File

@ -354,14 +354,24 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
_: hir::HirId,
_: rustc_span::Span,
) {
let tcx = self.tcx;
let has_repr_c = self.repr_has_repr_c;
let inherited_pub_visibility = self.inherited_pub_visibility;
let pub_visibility = self.pub_visibility;
let live_fields = def.fields().iter().filter(|f| {
has_repr_c || (pub_visibility && (inherited_pub_visibility || f.vis.node.is_pub()))
let live_fields = def.fields().iter().filter_map(|f| {
let def_id = tcx.hir().local_def_id(f.hir_id);
if has_repr_c {
return Some(def_id);
}
if !pub_visibility {
return None;
}
if inherited_pub_visibility {
return Some(def_id);
}
if tcx.visibility(def_id).is_public() { Some(def_id) } else { None }
});
let hir = self.tcx.hir();
self.live_symbols.extend(live_fields.map(|f| hir.local_def_id(f.hir_id)));
self.live_symbols.extend(live_fields);
intravisit::walk_struct_def(self, def);
}

View File

@ -660,7 +660,9 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
self.update_with_hir_id(ctor_hir_id, item_level);
}
for field in def.fields() {
if field.vis.node.is_pub() {
let def_id = self.tcx.hir().local_def_id(field.hir_id);
let vis = self.tcx.visibility(def_id);
if vis.is_public() {
self.update_with_hir_id(field.hir_id, item_level);
}
}
@ -1633,7 +1635,9 @@ impl<'a, 'tcx> Visitor<'tcx> for ObsoleteVisiblePrivateTypesVisitor<'a, 'tcx> {
}
fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
if s.vis.node.is_pub() || self.in_variant {
let def_id = self.tcx.hir().local_def_id(s.hir_id);
let vis = self.tcx.visibility(def_id);
if vis.is_public() || self.in_variant {
intravisit::walk_field_def(self, s);
}
}

View File

@ -57,9 +57,9 @@ macro_rules! down_cast_data {
}
macro_rules! access_from {
($save_ctxt:expr, $item:expr, $id:expr) => {
($save_ctxt:expr, $id:expr) => {
Access {
public: $item.vis.node.is_pub(),
public: $save_ctxt.tcx.visibility($id).is_public(),
reachable: $save_ctxt.access_levels.is_reachable($id),
}
};
@ -302,7 +302,7 @@ impl<'tcx> DumpVisitor<'tcx> {
let field_data = self.save_ctxt.get_field_data(field, parent_id);
if let Some(field_data) = field_data {
self.dumper.dump_def(
&access_from!(self.save_ctxt, field, self.tcx.hir().local_def_id(field.hir_id)),
&access_from!(self.save_ctxt, self.tcx.hir().local_def_id(field.hir_id)),
field_data,
);
}
@ -369,7 +369,7 @@ impl<'tcx> DumpVisitor<'tcx> {
v.process_formals(body.params, &fn_data.qualname);
v.process_generic_params(ty_params, &fn_data.qualname, item.hir_id());
v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.def_id), fn_data);
v.dumper.dump_def(&access_from!(v.save_ctxt, item.def_id), fn_data);
}
for arg in decl.inputs {
@ -393,7 +393,7 @@ impl<'tcx> DumpVisitor<'tcx> {
self.nest_typeck_results(item.def_id, |v| {
if let Some(var_data) = v.save_ctxt.get_item_data(item) {
down_cast_data!(var_data, DefData, item.span);
v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.def_id), var_data);
v.dumper.dump_def(&access_from!(v.save_ctxt, item.def_id), var_data);
}
v.visit_ty(&typ);
v.visit_expr(expr);
@ -469,7 +469,11 @@ impl<'tcx> DumpVisitor<'tcx> {
let fields_str = fields
.iter()
.filter_map(|f| {
if include_priv_fields || f.vis.node.is_pub() {
if include_priv_fields {
return Some(f.ident.to_string());
}
let def_id = self.save_ctxt.tcx.hir().local_def_id(f.hir_id);
if self.save_ctxt.tcx.visibility(def_id).is_public() {
Some(f.ident.to_string())
} else {
None
@ -487,7 +491,7 @@ impl<'tcx> DumpVisitor<'tcx> {
let span = self.span_from_span(item.ident.span);
let attrs = self.tcx.hir().attrs(item.hir_id());
self.dumper.dump_def(
&access_from!(self.save_ctxt, item, item.def_id),
&access_from!(self.save_ctxt, item.def_id),
Def {
kind,
id: id_from_def_id(item.def_id.to_def_id()),
@ -527,7 +531,7 @@ impl<'tcx> DumpVisitor<'tcx> {
};
down_cast_data!(enum_data, DefData, item.span);
let access = access_from!(self.save_ctxt, item, item.def_id);
let access = access_from!(self.save_ctxt, item.def_id);
for variant in enum_definition.variants {
let name = variant.ident.name.to_string();
@ -662,7 +666,7 @@ impl<'tcx> DumpVisitor<'tcx> {
methods.iter().map(|i| id_from_def_id(i.id.def_id.to_def_id())).collect();
let attrs = self.tcx.hir().attrs(item.hir_id());
self.dumper.dump_def(
&access_from!(self.save_ctxt, item, item.def_id),
&access_from!(self.save_ctxt, item.def_id),
Def {
kind: DefKind::Trait,
id,
@ -724,7 +728,7 @@ impl<'tcx> DumpVisitor<'tcx> {
fn process_mod(&mut self, item: &'tcx hir::Item<'tcx>) {
if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
down_cast_data!(mod_data, DefData, item.span);
self.dumper.dump_def(&access_from!(self.save_ctxt, item, item.def_id), mod_data);
self.dumper.dump_def(&access_from!(self.save_ctxt, item.def_id), mod_data);
}
}
@ -1147,7 +1151,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
hir::ItemKind::Use(path, hir::UseKind::Single) => {
let sub_span = path.segments.last().unwrap().ident.span;
if !self.span.filter_generated(sub_span) {
let access = access_from!(self.save_ctxt, item, item.def_id);
let access = access_from!(self.save_ctxt, item.def_id);
let ref_id = self.lookup_def_id(item.hir_id()).map(id_from_def_id);
let span = self.span_from_span(sub_span);
let parent =
@ -1176,7 +1180,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
// we don't want to track anyway, since it's probably macro-internal `use`
if let Some(sub_span) = self.span.sub_span_of_star(item.span) {
if !self.span.filter_generated(item.span) {
let access = access_from!(self.save_ctxt, item, item.def_id);
let access = access_from!(self.save_ctxt, item.def_id);
let span = self.span_from_span(sub_span);
let parent =
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> {
let attrs = self.tcx.hir().attrs(item.hir_id());
self.dumper.dump_def(
&access_from!(self.save_ctxt, item, item.def_id),
&access_from!(self.save_ctxt, item.def_id),
Def {
kind: DefKind::Type,
id,
@ -1443,7 +1447,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
}
fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
let access = access_from!(self.save_ctxt, item, item.def_id);
let access = access_from!(self.save_ctxt, item.def_id);
match item.kind {
hir::ForeignItemKind::Fn(decl, _, ref generics) => {

View File

@ -78,7 +78,10 @@ impl LateLintPass<'_> for ExhaustiveItems {
if !attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
then {
let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind {
if v.fields().iter().any(|f| !f.vis.node.is_pub()) {
if v.fields().iter().any(|f| {
let def_id = cx.tcx.hir().local_def_id(f.hir_id);
!cx.tcx.visibility(def_id).is_public()
}) {
// skip structs with private fields
return;
}