Skip to content

Commit

Permalink
Preserve visibility on trait items inside trait and impl
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay authored and calebcartwright committed Aug 22, 2021
1 parent c8bd550 commit fd6b025
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
15 changes: 8 additions & 7 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ pub(crate) struct FnSig<'a> {
constness: ast::Const,
defaultness: ast::Defaultness,
unsafety: ast::Unsafe,
visibility: ast::Visibility,
visibility: &'a ast::Visibility,
}

impl<'a> FnSig<'a> {
pub(crate) fn from_method_sig(
method_sig: &'a ast::FnSig,
generics: &'a ast::Generics,
visibility: ast::Visibility,
visibility: &'a ast::Visibility,
) -> FnSig<'a> {
FnSig {
unsafety: method_sig.header.unsafety,
Expand All @@ -198,7 +198,7 @@ impl<'a> FnSig<'a> {
match *fn_kind {
visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, _) => match fn_ctxt {
visit::FnCtxt::Assoc(..) => {
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis.clone());
let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis);
fn_sig.defaultness = defaultness;
fn_sig
}
Expand All @@ -210,7 +210,7 @@ impl<'a> FnSig<'a> {
is_async: Cow::Borrowed(&fn_sig.header.asyncness),
defaultness,
unsafety: fn_sig.header.unsafety,
visibility: vis.clone(),
visibility: vis,
},
},
_ => unreachable!(),
Expand Down Expand Up @@ -317,6 +317,7 @@ impl<'a> FmtVisitor<'a> {
indent: Indent,
ident: symbol::Ident,
sig: &ast::FnSig,
vis: &ast::Visibility,
generics: &ast::Generics,
span: Span,
) -> Option<String> {
Expand All @@ -328,7 +329,7 @@ impl<'a> FmtVisitor<'a> {
&context,
indent,
ident,
&FnSig::from_method_sig(sig, generics, DEFAULT_VISIBILITY),
&FnSig::from_method_sig(sig, generics, vis),
span,
FnBraceStyle::None,
)?;
Expand Down Expand Up @@ -1754,7 +1755,7 @@ impl<'a> StaticParts<'a> {
};
StaticParts {
prefix: "const",
vis: &DEFAULT_VISIBILITY,
vis: &ti.vis,
ident: ti.ident,
ty,
mutability: ast::Mutability::Not,
Expand Down Expand Up @@ -3110,7 +3111,7 @@ impl Rewrite for ast::ForeignItem {
context,
shape.indent,
self.ident,
&FnSig::from_method_sig(&fn_sig, generics, self.vis.clone()),
&FnSig::from_method_sig(&fn_sig, generics, &self.vis),
span,
FnBraceStyle::None,
)
Expand Down
14 changes: 5 additions & 9 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::Rc;

use rustc_ast::{ast, token::DelimToken, visit, AstLike};
use rustc_data_structures::sync::Lrc;
use rustc_span::{symbol, BytePos, Pos, Span, DUMMY_SP};
use rustc_span::{symbol, BytePos, Pos, Span};

use crate::attr::*;
use crate::comment::{contains_comment, rewrite_comment, CodeCharKind, CommentCodeSlices};
Expand Down Expand Up @@ -568,6 +568,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
indent,
item.ident,
&fn_signature,
&item.vis,
generics,
item.span,
);
Expand Down Expand Up @@ -641,14 +642,9 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let ast::FnKind(defaultness, ref sig, ref generics, ref block) = **fn_kind;
if let Some(ref body) = block {
let inner_attrs = inner_attributes(&ti.attrs);
let vis = ast::Visibility {
kind: ast::VisibilityKind::Inherited,
span: DUMMY_SP,
tokens: None,
};
let fn_ctxt = visit::FnCtxt::Assoc(visit::AssocCtxt::Trait);
self.visit_fn(
visit::FnKind::Fn(fn_ctxt, ti.ident, sig, &vis, Some(body)),
visit::FnKind::Fn(fn_ctxt, ti.ident, sig, &ti.vis, Some(body)),
generics,
&sig.decl,
ti.span,
Expand All @@ -658,7 +654,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} else {
let indent = self.block_indent;
let rewrite =
self.rewrite_required_fn(indent, ti.ident, sig, generics, ti.span);
self.rewrite_required_fn(indent, ti.ident, sig, &ti.vis, generics, ti.span);
self.push_rewrite(ti.span, rewrite);
}
}
Expand Down Expand Up @@ -708,7 +704,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
} else {
let indent = self.block_indent;
let rewrite =
self.rewrite_required_fn(indent, ii.ident, sig, generics, ii.span);
self.rewrite_required_fn(indent, ii.ident, sig, &ii.vis, generics, ii.span);
self.push_rewrite(ii.span, rewrite);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/target/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub impl Foo for Bar {
impl Visible for Bar {
pub const C: i32;
pub type T;
fn f();
pub fn f();
pub fn g() {}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/target/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ where
+ EEEEEEE;

trait Visible {
const C: i32;
pub const C: i32;
pub type T;
fn f();
fn g() {}
pub fn f();
pub fn g() {}
}

0 comments on commit fd6b025

Please sign in to comment.