Skip to content

Commit aff4f11

Browse files
committed
auto merge of #18160 : koshlo/rust/to-source-fix, r=alexcrichton
Fix for issue #18091 The problem seems to be that `ast_util::int_ty_to_string` takes unsigned number, and no one adds `-` to result string. I've fixed it by putting `-` before result string using `format!`. I've also added `test_signed_int_to_string()` to check if implementation is valid.
2 parents 5cba29d + 49ec356 commit aff4f11

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/libsyntax/print/pprust.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2680,8 +2680,9 @@ impl<'a> State<'a> {
26802680
ast_util::int_ty_to_string(st, Some(i as i64)).as_slice())
26812681
}
26822682
ast::SignedIntLit(st, ast::Minus) => {
2683+
let istr = ast_util::int_ty_to_string(st, Some(-(i as i64)));
26832684
word(&mut self.s,
2684-
ast_util::int_ty_to_string(st, Some(-(i as i64))).as_slice())
2685+
format!("-{}", istr).as_slice())
26852686
}
26862687
ast::UnsignedIntLit(ut) => {
26872688
word(&mut self.s, ast_util::uint_ty_to_string(ut, Some(i)).as_slice())
@@ -2930,4 +2931,12 @@ mod test {
29302931
let varstr = variant_to_string(&var);
29312932
assert_eq!(&varstr,&"pub principal_skinner".to_string());
29322933
}
2934+
2935+
#[test]
2936+
fn test_signed_int_to_string() {
2937+
let pos_int = ast::LitInt(42, ast::SignedIntLit(ast::TyI32, ast::Plus));
2938+
let neg_int = ast::LitInt((-42) as u64, ast::SignedIntLit(ast::TyI32, ast::Minus));
2939+
assert_eq!(format!("-{}", lit_to_string(&codemap::dummy_spanned(pos_int))),
2940+
lit_to_string(&codemap::dummy_spanned(neg_int)));
2941+
}
29332942
}

0 commit comments

Comments
 (0)