From 59554dd5e1121d39cc9c1d716277de03c153a46b Mon Sep 17 00:00:00 2001 From: defyrlt Date: Fri, 1 May 2015 14:08:22 +0300 Subject: [PATCH 1/2] Resolved #56 -- `mut` was eaten out of `mut self` in fn args. --- src/functions.rs | 17 ++++++++++++++++- tests/idem/fn.rs | 6 ++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/functions.rs b/src/functions.rs index 3d6fcb88c25..d4f06c1c7ac 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -164,7 +164,22 @@ impl<'a> FmtVisitor<'a> { arg_item_strs[0] = format!("self: {}", pprust::ty_to_string(ty)); } ast::ExplicitSelf_::SelfValue(_) => { - arg_item_strs[0] = "self".to_owned(); + assert!(args.len() >= 1, "&[ast::Arg] shouldn't be empty."); + + // this hacky solution caused by absence of `Mutability` in `SelfValue`. + let mut_str = { + if let ast::Pat_::PatIdent(ast::BindingMode::BindByValue(mutability), _, _) + = args[0].pat.node { + match mutability { + ast::Mutability::MutMutable => "mut ", + ast::Mutability::MutImmutable => "", + } + } else { + panic!("there is a bug or change in structure of AST, aborting."); + } + }; + + arg_item_strs[0] = format!("{}self", mut_str); min_args = 2; } _ => {} diff --git a/tests/idem/fn.rs b/tests/idem/fn.rs index aec27597b7a..62a059dc464 100644 --- a/tests/idem/fn.rs +++ b/tests/idem/fn.rs @@ -39,6 +39,12 @@ impl Foo { where F: FnOnce(&mut Resolver) -> T { } + + fn foo(mut self, mut bar: u32) { + } + + fn bar(self, mut bazz: u32) { + } } pub fn render<'a, From 59352172b3539c1ecd3a03919fc7e06a7c06af4a Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Fri, 1 May 2015 17:27:54 +0100 Subject: [PATCH 2/2] Change import lists with one item to single import Changes lists such as ``use std::{fmt}`` to a form without a list, eg ``use std::fmt``. --- src/imports.rs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/imports.rs b/src/imports.rs index d29e9eb6e84..aea983e261f 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -18,10 +18,28 @@ use syntax::print::pprust; use {IDEAL_WIDTH, MAX_WIDTH}; -// TODO change import lists with one item to a single import -// remove empty lists (if they're even possible) +// TODO remove empty lists (if they're even possible) // TODO (some day) remove unused imports, expand globs, compress many single imports into a list import +fn rewrite_single_use_list(path_str: String, vpi: ast::PathListItem, vis: &str) -> String { + if let ast::PathListItem_::PathListIdent{ name, .. } = vpi.node { + let name_str = token::get_ident(name).to_string(); + if path_str.len() == 0 { + format!("{}use {};", vis, name_str) + } else { + format!("{}use {}::{};", vis, path_str, name_str) + } + } else { + if path_str.len() != 0 { + format!("{}use {};", vis, path_str) + } else { + // This catches the import: use {self}, which is a compiler error, so we just + // leave it alone. + format!("{}use {{self}};", vis) + } + } +} + impl<'a> FmtVisitor<'a> { // Basically just pretty prints a multi-item import. pub fn rewrite_use_list(&mut self, @@ -29,9 +47,6 @@ impl<'a> FmtVisitor<'a> { path_list: &[ast::PathListItem], visibility: ast::Visibility, vp_span: Span) -> String { - // FIXME check indentation - let l_loc = self.codemap.lookup_char_pos(vp_span.lo); - let path_str = pprust::path_to_string(&path); let vis = match visibility { @@ -39,6 +54,13 @@ impl<'a> FmtVisitor<'a> { _ => "" }; + if path_list.len() == 1 { + return rewrite_single_use_list(path_str, path_list[0], vis); + } + + // FIXME check indentation + let l_loc = self.codemap.lookup_char_pos(vp_span.lo); + // 1 = { let mut indent = l_loc.col.0 + path_str.len() + 1; if path_str.len() > 0 {