diff --git a/src/items.rs b/src/items.rs index 197f5403e7c..2224d9a5cb6 100644 --- a/src/items.rs +++ b/src/items.rs @@ -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 diff --git a/tests/source/impls.rs b/tests/source/impls.rs index 4382c4fd0e6..016350ca6d6 100644 --- a/tests/source/impls.rs +++ b/tests/source/impls.rs @@ -58,3 +58,5 @@ impl Blah { fn boop() {} add_fun!(); } + +impl X { fn do_parse( mut self : X ) {} } diff --git a/tests/target/impls.rs b/tests/target/impls.rs index 35ddc23a20a..9f12a6fd7ff 100644 --- a/tests/target/impls.rs +++ b/tests/target/impls.rs @@ -72,3 +72,7 @@ impl Blah { fn boop() {} add_fun!(); } + +impl X { + fn do_parse(mut self: X) {} +}