Skip to content

Commit be5672e

Browse files
committed
Stringify non-shorthand visibility correctly
1 parent 34a6cae commit be5672e

File tree

6 files changed

+18
-9
lines changed

6 files changed

+18
-9
lines changed

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2602,7 +2602,7 @@ pub struct Visibility {
26022602
#[derive(Clone, Encodable, Decodable, Debug)]
26032603
pub enum VisibilityKind {
26042604
Public,
2605-
Restricted { path: P<Path>, id: NodeId },
2605+
Restricted { path: P<Path>, id: NodeId, shorthand: bool },
26062606
Inherited,
26072607
}
26082608

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ pub fn noop_flat_map_stmt_kind<T: MutVisitor>(
14861486
pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) {
14871487
match &mut visibility.kind {
14881488
VisibilityKind::Public | VisibilityKind::Inherited => {}
1489-
VisibilityKind::Restricted { path, id } => {
1489+
VisibilityKind::Restricted { path, id, shorthand: _ } => {
14901490
vis.visit_path(path);
14911491
vis.visit_id(id);
14921492
}

compiler/rustc_ast/src/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ pub fn walk_arm<'a, V: Visitor<'a>>(visitor: &mut V, arm: &'a Arm) {
935935
}
936936

937937
pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
938-
if let VisibilityKind::Restricted { ref path, id } = vis.kind {
938+
if let VisibilityKind::Restricted { ref path, id, shorthand: _ } = vis.kind {
939939
visitor.visit_path(path, id);
940940
}
941941
}

compiler/rustc_ast_pretty/src/pprust/state/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,9 @@ impl<'a> State<'a> {
412412
pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
413413
match vis.kind {
414414
ast::VisibilityKind::Public => self.word_nbsp("pub"),
415-
ast::VisibilityKind::Restricted { ref path, .. } => {
415+
ast::VisibilityKind::Restricted { ref path, id: _, shorthand } => {
416416
let path = Self::to_string(|s| s.print_path(path, false, 0));
417-
if path == "crate" || path == "self" || path == "super" {
417+
if shorthand && (path == "crate" || path == "self" || path == "super") {
418418
self.word_nbsp(format!("pub({})", path))
419419
} else {
420420
self.word_nbsp(format!("pub(in {})", path))

compiler/rustc_parse/src/parser/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,11 @@ impl<'a> Parser<'a> {
12951295
self.bump(); // `in`
12961296
let path = self.parse_path(PathStyle::Mod)?; // `path`
12971297
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
1298-
let vis = VisibilityKind::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
1298+
let vis = VisibilityKind::Restricted {
1299+
path: P(path),
1300+
id: ast::DUMMY_NODE_ID,
1301+
shorthand: false,
1302+
};
12991303
return Ok(Visibility {
13001304
span: lo.to(self.prev_token.span),
13011305
kind: vis,
@@ -1308,7 +1312,11 @@ impl<'a> Parser<'a> {
13081312
self.bump(); // `(`
13091313
let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
13101314
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)`
1311-
let vis = VisibilityKind::Restricted { path: P(path), id: ast::DUMMY_NODE_ID };
1315+
let vis = VisibilityKind::Restricted {
1316+
path: P(path),
1317+
id: ast::DUMMY_NODE_ID,
1318+
shorthand: true,
1319+
};
13121320
return Ok(Visibility {
13131321
span: lo.to(self.prev_token.span),
13141322
kind: vis,

src/test/ui/macros/stringify.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,9 @@ fn test_vis() {
865865
assert_eq!(stringify_vis!(pub(crate)), "pub(crate) ");
866866
assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
867867
assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
868-
assert_eq!(stringify_vis!(pub(in self)), "pub(self) ");
869-
assert_eq!(stringify_vis!(pub(in super)), "pub(super) ");
868+
assert_eq!(stringify_vis!(pub(in crate)), "pub(in crate) ");
869+
assert_eq!(stringify_vis!(pub(in self)), "pub(in self) ");
870+
assert_eq!(stringify_vis!(pub(in super)), "pub(in super) ");
870871
assert_eq!(stringify_vis!(pub(in path::to)), "pub(in path::to) ");
871872
assert_eq!(stringify_vis!(pub(in ::path::to)), "pub(in ::path::to) ");
872873
assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self::path::to) ");

0 commit comments

Comments
 (0)