Skip to content

Commit

Permalink
Stringify non-shorthand visibility correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Aug 10, 2022
1 parent 34a6cae commit be5672e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2602,7 +2602,7 @@ pub struct Visibility {
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum VisibilityKind {
Public,
Restricted { path: P<Path>, id: NodeId },
Restricted { path: P<Path>, id: NodeId, shorthand: bool },
Inherited,
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,7 +1486,7 @@ pub fn noop_flat_map_stmt_kind<T: MutVisitor>(
pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) {
match &mut visibility.kind {
VisibilityKind::Public | VisibilityKind::Inherited => {}
VisibilityKind::Restricted { path, id } => {
VisibilityKind::Restricted { path, id, shorthand: _ } => {
vis.visit_path(path);
vis.visit_id(id);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
}

pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
if let VisibilityKind::Restricted { ref path, id } = vis.kind {
if let VisibilityKind::Restricted { ref path, id, shorthand: _ } = vis.kind {
visitor.visit_path(path, id);
}
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,9 @@ impl<'a> State<'a> {
pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
match vis.kind {
ast::VisibilityKind::Public => self.word_nbsp("pub"),
ast::VisibilityKind::Restricted { ref path, .. } => {
ast::VisibilityKind::Restricted { ref path, id: _, shorthand } => {
let path = Self::to_string(|s| s.print_path(path, false, 0));
if path == "crate" || path == "self" || path == "super" {
if shorthand && (path == "crate" || path == "self" || path == "super") {
self.word_nbsp(format!("pub({})", path))
} else {
self.word_nbsp(format!("pub(in {})", path))
Expand Down
12 changes: 10 additions & 2 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,11 @@ impl<'a> Parser<'a> {
self.bump(); // `in`
let path = self.parse_path(PathStyle::Mod)?; // `path`
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
let vis = VisibilityKind::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
let vis = VisibilityKind::Restricted {
path: P(path),
id: ast::DUMMY_NODE_ID,
shorthand: false,
};
return Ok(Visibility {
span: lo.to(self.prev_token.span),
kind: vis,
Expand All @@ -1308,7 +1312,11 @@ impl<'a> Parser<'a> {
self.bump(); // `(`
let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
let vis = VisibilityKind::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
let vis = VisibilityKind::Restricted {
path: P(path),
id: ast::DUMMY_NODE_ID,
shorthand: true,
};
return Ok(Visibility {
span: lo.to(self.prev_token.span),
kind: vis,
Expand Down
5 changes: 3 additions & 2 deletions src/test/ui/macros/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,9 @@ fn test_vis() {
assert_eq!(stringify_vis!(pub(crate)), "pub(crate) ");
assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
assert_eq!(stringify_vis!(pub(in self)), "pub(self) ");
assert_eq!(stringify_vis!(pub(in super)), "pub(super) ");
assert_eq!(stringify_vis!(pub(in crate)), "pub(in crate) ");
assert_eq!(stringify_vis!(pub(in self)), "pub(in self) ");
assert_eq!(stringify_vis!(pub(in super)), "pub(in super) ");
assert_eq!(stringify_vis!(pub(in path::to)), "pub(in path::to) ");
assert_eq!(stringify_vis!(pub(in ::path::to)), "pub(in ::path::to) ");
assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self::path::to) ");
Expand Down

0 comments on commit be5672e

Please sign in to comment.