Preserve mutability for typed self arguments

This commit is contained in:
Marcus Klaas 2015-12-25 19:07:51 +01:00
parent c0b7de7c52
commit 954dd0869e
3 changed files with 26 additions and 12 deletions

View file

@ -930,27 +930,35 @@ fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf, args: &[ast::Arg]) -
}
}
ast::ExplicitSelf_::SelfExplicit(ref ty, _) => {
Some(format!("self: {}", pprust::ty_to_string(ty)))
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
let mutability = explicit_self_mutability(&args[0]);
Some(format!("{}self: {}",
format_mutability(mutability),
pprust::ty_to_string(ty)))
}
ast::ExplicitSelf_::SelfValue(_) => {
assert!(args.len() >= 1, "&[ast::Arg] shouldn't be empty.");
assert!(!args.is_empty(), "&[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 {
format_mutability(mutability)
} else {
panic!("there is a bug or change in structure of AST, aborting.");
}
};
let mutability = explicit_self_mutability(&args[0]);
Some(format!("{}self", mut_str))
Some(format!("{}self", format_mutability(mutability)))
}
_ => None,
}
}
// Hacky solution caused by absence of `Mutability` in `SelfValue` and
// `SelfExplicit` variants of `ast::ExplicitSelf_`.
fn explicit_self_mutability(arg: &ast::Arg) -> ast::Mutability {
if let ast::Pat_::PatIdent(ast::BindingMode::BindByValue(mutability), _, _) = arg.pat.node {
mutability
} else {
unreachable!()
}
}
pub fn span_lo_for_arg(arg: &ast::Arg) -> BytePos {
if is_named_arg(arg) {
arg.pat.span.lo

View file

@ -58,3 +58,5 @@ impl Blah {
fn boop() {}
add_fun!();
}
impl X { fn do_parse( mut self : X ) {} }

View file

@ -72,3 +72,7 @@ impl Blah {
fn boop() {}
add_fun!();
}
impl X {
fn do_parse(mut self: X) {}
}