]> git.lizzy.rs Git - rust.git/commit
Merge #6158
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>
Wed, 7 Oct 2020 09:32:47 +0000 (09:32 +0000)
committerGitHub <noreply@github.com>
Wed, 7 Oct 2020 09:32:47 +0000 (09:32 +0000)
commitfaddea935332de3156a5462baa07136bf2e98bf9
tree2bf4b95ff97c0cd9ed8a69ed63808fffffd521f2
parenta184c83535230af39fad3e238b7422b4972d597e
parent27798ee575a975a1806ced86aca8aea407897851
Merge #6158

6158: Fix for negative literals in macros r=matklad a=cutsoy

_This pull request fixes #6028._

When writing `-42.0f32` in Rust, it is usually parsed as two different tokens (a minus operator and a float literal).

But a procedural macro can also generate new tokens, including negative [float literals](https://doc.rust-lang.org/stable/proc_macro/struct.Literal.html#method.f32_suffixed):

```rust
#[proc_macro]
fn example_verbose(input: TokenStream) -> TokenStream {
    let literal = Literal::f32_suffixed(-42.0);
    quote! { #literal }
}
```

or even shorter

```rust
#[proc_macro]
fn example(input: TokenStream) -> TokenStream {
    let literal = -42.0f32;
    quote! { #literal }
}
```

Unfortunately, these currently cause RA to crash:

```
thread '<unnamed>' panicked at 'Fail to convert given literal Literal {
    text: "-42.0f32",
    id: TokenId(
        4294967295,
    ),
}', crates/mbe/src/subtree_source.rs:161:28
```

This pull request contains both a fix 8cf9362 and a unit test 27798ee. In addition, I installed the patched server with `cargo xtask install --server` and verified in VSCode that it no longer crashes when a procedural macro returns a negative number literal.

Co-authored-by: Tim <tim@glacyr.com>