0

This is rougly what I want to use:

enum DashNumber<N> {
    NegInfinity,
    Number(N),
    Infinity,
}
macro_rules! dn {
    (-∞) => {
        DashNumber::NegInfinity
    };
    (∞) => {
        DashNumber::Infinity
    };
    ($e:expr) => {
        DashNumber::Number($e)
    };
}

however I get the following errors:

error: unknown start of token: \u{221e}
  --> src/main.rs:13:7
   |
13 |     (-∞) => {
   |       ^

error: unknown start of token: \u{221e}
  --> src/main.rs:16:6
   |
16 |     (∞) => {
   |      ^

What is wrong with this macro?

Rainb
  • 1,965
  • 11
  • 32

1 Answers1

1

Macro arguments cannot be arbitrary text. They still have to be valid Rust tokens, and all types of brackets must be balanced; see MacroMatcher here.

The character could only be possibly parsed as an identifier. However, identifiers can only start with characters from the XID_Start Unicode set, which contains "letter-like" characters, or with _. Unfortunately, ∞ is not in that set, so it can't be used as a macro argument.

You'll have to use some alternate syntax, like quoting the symbol ("∞") or using letters (INF).

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • it's also not in XID_Continue, unfortunately, I wish there could be extra characters as identifiers or make macromatcher also parse an extra fragment, for things like math operators and such. I've settled for `(-"∞"|-INF) => {DashNumber::NegInfinity};` – Rainb Mar 13 '23 at 11:14
  • @Rainb Note that `-INF` is a valid expression, so this can cause confusion. – Chayim Friedman Mar 13 '23 at 14:33