1
Fork 0

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
This commit is contained in:
Matthias Krüger 2022-08-11 22:53:06 +02:00 committed by GitHub
commit 6ae0414122
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 9 deletions

View file

@ -2601,7 +2601,7 @@ pub struct Visibility {
#[derive(Clone, Encodable, Decodable, Debug)] #[derive(Clone, Encodable, Decodable, Debug)]
pub enum VisibilityKind { pub enum VisibilityKind {
Public, Public,
Restricted { path: P<Path>, id: NodeId }, Restricted { path: P<Path>, id: NodeId, shorthand: bool },
Inherited, Inherited,
} }

View file

@ -1487,7 +1487,7 @@ pub fn noop_flat_map_stmt_kind<T: MutVisitor>(
pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) { pub fn noop_visit_vis<T: MutVisitor>(visibility: &mut Visibility, vis: &mut T) {
match &mut visibility.kind { match &mut visibility.kind {
VisibilityKind::Public | VisibilityKind::Inherited => {} VisibilityKind::Public | VisibilityKind::Inherited => {}
VisibilityKind::Restricted { path, id } => { VisibilityKind::Restricted { path, id, shorthand: _ } => {
vis.visit_path(path); vis.visit_path(path);
vis.visit_id(id); vis.visit_id(id);
} }

View file

@ -936,7 +936,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) { 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); visitor.visit_path(path, id);
} }
} }

View file

@ -412,9 +412,9 @@ impl<'a> State<'a> {
pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) { pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
match vis.kind { match vis.kind {
ast::VisibilityKind::Public => self.word_nbsp("pub"), 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)); 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)) self.word_nbsp(format!("pub({})", path))
} else { } else {
self.word_nbsp(format!("pub(in {})", path)) self.word_nbsp(format!("pub(in {})", path))

View file

@ -1295,7 +1295,11 @@ impl<'a> Parser<'a> {
self.bump(); // `in` self.bump(); // `in`
let path = self.parse_path(PathStyle::Mod)?; // `path` let path = self.parse_path(PathStyle::Mod)?; // `path`
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)` 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 { return Ok(Visibility {
span: lo.to(self.prev_token.span), span: lo.to(self.prev_token.span),
kind: vis, kind: vis,
@ -1308,7 +1312,11 @@ impl<'a> Parser<'a> {
self.bump(); // `(` self.bump(); // `(`
let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self` let path = self.parse_path(PathStyle::Mod)?; // `crate`/`super`/`self`
self.expect(&token::CloseDelim(Delimiter::Parenthesis))?; // `)` 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 { return Ok(Visibility {
span: lo.to(self.prev_token.span), span: lo.to(self.prev_token.span),
kind: vis, kind: vis,

View file

@ -865,8 +865,9 @@ fn test_vis() {
assert_eq!(stringify_vis!(pub(crate)), "pub(crate) "); assert_eq!(stringify_vis!(pub(crate)), "pub(crate) ");
assert_eq!(stringify_vis!(pub(self)), "pub(self) "); assert_eq!(stringify_vis!(pub(self)), "pub(self) ");
assert_eq!(stringify_vis!(pub(super)), "pub(super) "); assert_eq!(stringify_vis!(pub(super)), "pub(super) ");
assert_eq!(stringify_vis!(pub(in self)), "pub(self) "); assert_eq!(stringify_vis!(pub(in crate)), "pub(in crate) ");
assert_eq!(stringify_vis!(pub(in super)), "pub(super) "); 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 ::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) "); assert_eq!(stringify_vis!(pub(in self::path::to)), "pub(in self::path::to) ");