Skip to content

Commit

Permalink
Rollup merge of rust-lang#87267 - dtolnay:negspace, r=Aaron1011
Browse files Browse the repository at this point in the history
Remove space after negative sign in Literal to_string

Negative proc macro literal tokens used to be printed with a space between the minus sign and the magnitude. That's because `impl ToString for Literal` used to convert the Literal into a TokenStream, which splits the minus sign into a separate Punct token.

```rust
Literal::isize_unsuffixed(-10).to_string()  // "- 10"
```

This PR updates the ToString impl to directly use `rustc_ast::token::Lit`'s ToString, which matches the way Rust negative numbers are idiomatically written without a space.

```rust
Literal::isize_unsuffixed(-10).to_string()  // "-10"
```
  • Loading branch information
JohnTitor committed Aug 3, 2021
2 parents e252bc6 + 3744dc8 commit a53bede
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_expand/src/proc_macro_server.rs
Expand Up @@ -582,6 +582,9 @@ impl server::Literal for Rustc<'_> {

Ok(Literal { lit, span: self.call_site })
}
fn to_string(&mut self, literal: &Self::Literal) -> String {
literal.lit.to_string()
}
fn debug_kind(&mut self, literal: &Self::Literal) -> String {
format!("{:?}", literal.lit.kind)
}
Expand Down
1 change: 1 addition & 0 deletions library/proc_macro/src/bridge/mod.rs
Expand Up @@ -109,6 +109,7 @@ macro_rules! with_api {
fn drop($self: $S::Literal);
fn clone($self: &$S::Literal) -> $S::Literal;
fn from_str(s: &str) -> Result<$S::Literal, ()>;
fn to_string($self: &$S::Literal) -> String;
fn debug_kind($self: &$S::Literal) -> String;
fn symbol($self: &$S::Literal) -> String;
fn suffix($self: &$S::Literal) -> Option<String>;
Expand Down
2 changes: 1 addition & 1 deletion library/proc_macro/src/lib.rs
Expand Up @@ -1195,7 +1195,7 @@ impl FromStr for Literal {
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
impl ToString for Literal {
fn to_string(&self) -> String {
TokenStream::from(TokenTree::from(self.clone())).to_string()
self.0.to_string()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/proc-macro/auxiliary/api/parse.rs
Expand Up @@ -6,8 +6,8 @@ pub fn test() {
}

fn test_display_literal() {
assert_eq!(Literal::isize_unsuffixed(-10).to_string(), "- 10");
assert_eq!(Literal::isize_suffixed(-10).to_string(), "- 10isize");
assert_eq!(Literal::isize_unsuffixed(-10).to_string(), "-10");
assert_eq!(Literal::isize_suffixed(-10).to_string(), "-10isize");
}

fn test_parse_literal() {
Expand All @@ -18,7 +18,7 @@ fn test_parse_literal() {
assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\"");
assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##");
assert_eq!("10ulong".parse::<Literal>().unwrap().to_string(), "10ulong");
assert_eq!("-10ulong".parse::<Literal>().unwrap().to_string(), "- 10ulong");
assert_eq!("-10ulong".parse::<Literal>().unwrap().to_string(), "-10ulong");

assert!("true".parse::<Literal>().is_err());
assert!(".8".parse::<Literal>().is_err());
Expand Down

0 comments on commit a53bede

Please sign in to comment.