Refactor how global paths are represented (for both ast and hir).

This commit is contained in:
Jeffrey Seyfried
2016-12-05 03:51:11 +00:00
parent 164619a8cf
commit f10f50b426
27 changed files with 242 additions and 257 deletions

View File

@@ -371,7 +371,7 @@ pub fn fn_block_to_string(p: &ast::FnDecl) -> String {
}
pub fn path_to_string(p: &ast::Path) -> String {
to_string(|s| s.print_path(p, false, 0))
to_string(|s| s.print_path(p, false, 0, false))
}
pub fn ident_to_string(id: ast::Ident) -> String {
@@ -435,7 +435,8 @@ pub fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
match *vis {
ast::Visibility::Public => format!("pub {}", s),
ast::Visibility::Crate(_) => format!("pub(crate) {}", s),
ast::Visibility::Restricted { ref path, .. } => format!("pub({}) {}", path, s),
ast::Visibility::Restricted { ref path, .. } =>
format!("pub({}) {}", to_string(|s| s.print_path(path, false, 0, true)), s),
ast::Visibility::Inherited => s.to_string()
}
}
@@ -1021,7 +1022,7 @@ impl<'a> State<'a> {
&generics));
}
ast::TyKind::Path(None, ref path) => {
try!(self.print_path(path, false, 0));
try!(self.print_path(path, false, 0, false));
}
ast::TyKind::Path(Some(ref qself), ref path) => {
try!(self.print_qpath(path, qself, false))
@@ -1332,7 +1333,7 @@ impl<'a> State<'a> {
}
ast::ItemKind::Mac(codemap::Spanned { ref node, .. }) => {
try!(self.print_visibility(&item.vis));
try!(self.print_path(&node.path, false, 0));
try!(self.print_path(&node.path, false, 0, false));
try!(word(&mut self.s, "! "));
try!(self.print_ident(item.ident));
try!(self.cbox(INDENT_UNIT));
@@ -1347,7 +1348,7 @@ impl<'a> State<'a> {
}
fn print_trait_ref(&mut self, t: &ast::TraitRef) -> io::Result<()> {
self.print_path(&t.path, false, 0)
self.print_path(&t.path, false, 0, false)
}
fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> io::Result<()> {
@@ -1405,8 +1406,10 @@ impl<'a> State<'a> {
match *vis {
ast::Visibility::Public => self.word_nbsp("pub"),
ast::Visibility::Crate(_) => self.word_nbsp("pub(crate)"),
ast::Visibility::Restricted { ref path, .. } =>
self.word_nbsp(&format!("pub({})", path)),
ast::Visibility::Restricted { ref path, .. } => {
let path = to_string(|s| s.print_path(path, false, 0, true));
self.word_nbsp(&format!("pub({})", path))
}
ast::Visibility::Inherited => Ok(())
}
}
@@ -1571,7 +1574,7 @@ impl<'a> State<'a> {
}
ast::TraitItemKind::Macro(codemap::Spanned { ref node, .. }) => {
// code copied from ItemKind::Mac:
self.print_path(&node.path, false, 0)?;
self.print_path(&node.path, false, 0, false)?;
word(&mut self.s, "! ")?;
self.cbox(INDENT_UNIT)?;
self.popen()?;
@@ -1607,7 +1610,7 @@ impl<'a> State<'a> {
}
ast::ImplItemKind::Macro(codemap::Spanned { ref node, .. }) => {
// code copied from ItemKind::Mac:
try!(self.print_path(&node.path, false, 0));
try!(self.print_path(&node.path, false, 0, false));
try!(word(&mut self.s, "! "));
try!(self.cbox(INDENT_UNIT));
try!(self.popen());
@@ -1793,7 +1796,7 @@ impl<'a> State<'a> {
pub fn print_mac(&mut self, m: &ast::Mac, delim: token::DelimToken)
-> io::Result<()> {
try!(self.print_path(&m.node.path, false, 0));
try!(self.print_path(&m.node.path, false, 0, false));
try!(word(&mut self.s, "!"));
match delim {
token::Paren => try!(self.popen()),
@@ -1885,7 +1888,7 @@ impl<'a> State<'a> {
fields: &[ast::Field],
wth: &Option<P<ast::Expr>>,
attrs: &[Attribute]) -> io::Result<()> {
try!(self.print_path(path, true, 0));
try!(self.print_path(path, true, 0, false));
try!(word(&mut self.s, "{"));
try!(self.print_inner_attributes_inline(attrs));
try!(self.commasep_cmnt(
@@ -2186,7 +2189,7 @@ impl<'a> State<'a> {
}
}
ast::ExprKind::Path(None, ref path) => {
try!(self.print_path(path, true, 0))
try!(self.print_path(path, true, 0, false))
}
ast::ExprKind::Path(Some(ref qself), ref path) => {
try!(self.print_qpath(path, qself, true))
@@ -2334,23 +2337,25 @@ impl<'a> State<'a> {
fn print_path(&mut self,
path: &ast::Path,
colons_before_params: bool,
depth: usize)
depth: usize,
defaults_to_global: bool)
-> io::Result<()>
{
try!(self.maybe_print_comment(path.span.lo));
let mut first = !path.global;
for segment in &path.segments[..path.segments.len()-depth] {
if first {
first = false
} else {
let mut segments = path.segments[..path.segments.len()-depth].iter();
if defaults_to_global && path.is_global() {
segments.next();
}
for (i, segment) in segments.enumerate() {
if i > 0 {
try!(word(&mut self.s, "::"))
}
try!(self.print_ident(segment.identifier));
if let Some(ref parameters) = segment.parameters {
try!(self.print_path_parameters(parameters, colons_before_params))
if segment.identifier.name != keywords::CrateRoot.name() {
try!(self.print_ident(segment.identifier));
if let Some(ref parameters) = segment.parameters {
try!(self.print_path_parameters(parameters, colons_before_params));
}
}
}
@@ -2369,7 +2374,7 @@ impl<'a> State<'a> {
try!(space(&mut self.s));
try!(self.word_space("as"));
let depth = path.segments.len() - qself.position;
try!(self.print_path(&path, false, depth));
try!(self.print_path(&path, false, depth, false));
}
try!(word(&mut self.s, ">"));
try!(word(&mut self.s, "::"));
@@ -2472,7 +2477,7 @@ impl<'a> State<'a> {
}
}
PatKind::TupleStruct(ref path, ref elts, ddpos) => {
try!(self.print_path(path, true, 0));
try!(self.print_path(path, true, 0, false));
try!(self.popen());
if let Some(ddpos) = ddpos {
try!(self.commasep(Inconsistent, &elts[..ddpos], |s, p| s.print_pat(&p)));
@@ -2490,13 +2495,13 @@ impl<'a> State<'a> {
try!(self.pclose());
}
PatKind::Path(None, ref path) => {
try!(self.print_path(path, true, 0));
try!(self.print_path(path, true, 0, false));
}
PatKind::Path(Some(ref qself), ref path) => {
try!(self.print_qpath(path, qself, false));
}
PatKind::Struct(ref path, ref fields, etc) => {
try!(self.print_path(path, true, 0));
try!(self.print_path(path, true, 0, false));
try!(self.nbsp());
try!(self.word_space("{"));
try!(self.commasep_cmnt(
@@ -2843,7 +2848,7 @@ impl<'a> State<'a> {
try!(self.print_lifetime_bounds(lifetime, bounds));
}
ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ref path, ref ty, ..}) => {
try!(self.print_path(path, false, 0));
try!(self.print_path(path, false, 0, false));
try!(space(&mut self.s));
try!(self.word_space("="));
try!(self.print_type(&ty));
@@ -2857,7 +2862,7 @@ impl<'a> State<'a> {
pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> io::Result<()> {
match vp.node {
ast::ViewPathSimple(ident, ref path) => {
try!(self.print_path(path, false, 0));
try!(self.print_path(path, false, 0, true));
if path.segments.last().unwrap().identifier.name !=
ident.name {
@@ -2870,7 +2875,7 @@ impl<'a> State<'a> {
}
ast::ViewPathGlob(ref path) => {
try!(self.print_path(path, false, 0));
try!(self.print_path(path, false, 0, true));
word(&mut self.s, "::*")
}
@@ -2878,7 +2883,7 @@ impl<'a> State<'a> {
if path.segments.is_empty() {
try!(word(&mut self.s, "{"));
} else {
try!(self.print_path(path, false, 0));
try!(self.print_path(path, false, 0, true));
try!(word(&mut self.s, "::{"));
}
try!(self.commasep(Inconsistent, &idents[..], |s, w| {