Skip to content

Commit 6ae0414

Browse files
authored
Rollup merge of #100350 - jhpratt:stringify-vis, r=cjgillot
Stringify non-shorthand visibility correctly This makes `stringify!(pub(in crate))` evaluate to `pub(in crate)` rather than `pub(crate)`, matching the behavior before the `crate` shorthand was removed. Further, this changes `stringify!(pub(in super))` to evaluate to `pub(in super)` rather than the current `pub(super)`. If the latter is not desired (it is _technically_ breaking), it can be undone. Fixes #99981 `@rustbot` label +C-bug +regression-from-stable-to-beta +T-compiler
2 parents e221aaf + be5672e commit 6ae0414

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
@@ -2601,7 +2601,7 @@ pub struct Visibility {
26012601
#[derive(Clone, Encodable, Decodable, Debug)]
26022602
pub enum VisibilityKind {
26032603
Public,
2604-
Restricted { path: P<Path>, id: NodeId },
2604+
Restricted { path: P<Path>, id: NodeId, shorthand: bool },
26052605
Inherited,
26062606
}
26072607

compiler/rustc_ast/src/mut_visit.rs

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

compiler/rustc_ast/src/visit.rs

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

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

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)