# Pre-RFC: Raw identifiers

**URL:** <https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502>\
**Category:** language design\
**Created:** [July 7, 2017, 10:13pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502 "2017-07-07T22:13:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [July 7, 2017, 10:13pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/1 "2017-07-07T22:13:43Z")

</div>

- Feature Name: raw\_identifiers
- Start Date: 2017-07-07
- RFC PR: (leave this empty)
- Rust Issue: (leave this empty)

# Summary

Add a raw identifier format `r#catch`, so crates written in future language epochs/versions can still use an older API that overlaps with new keywords.

# Motivation

One of the primary examples of breaking changes in the epoch proposal is to add new keywords, and specifically `catch` is the first candidate. However, since that’s seeking crate compatibility across epochs, this would leave a crate in a newer epoch unable to use `catch` identifiers in the API of a crate in an older epoch.

A raw syntax that’s _always_ an identifier would allow these to remain compatible.

# Detailed design

I propose `r#catch` as a syntax which always parses as a `catch` identifier, ignoring any possible keywords. Specifically, `r#` is the beginning of an escaped identifier token, followed by any legal identifer sequence. The `r#` is not kept as part of the resulting identifier in any way.

# How We Teach This

We can call these simply “raw identifiers”. It may also be called [stropping](https://en.wikipedia.org/wiki/Stropping_(syntax)), but this term is not very common.

Using `r#catch` is visually similar to the existing `r#"raw "escaped" string"#` syntax, which will hopefully give an intuitive clue to advanced users what is going on.

This feature will most likely only be seen in rare corner cases. It may only need to be taught at all in documentation for a breaking change which is introducing a new keyword.

Documentation updates:

- _The Rust Programming Language_ Appendix A “Keywords” should mention the `r#` syntax.
- Rust Reference 2.2 “Identifiers” should specify the `r#` syntax.
- Perhaps also Grammar section 3.2.1 “Identifiers” and 3.5.1 “Keywords”.

# Drawbacks

New syntax is always scary/noisy/etc.

It might not be intuitively “raw” to a user coming upon this the first time.

# Alternatives

- The exact syntax is a huge bikeshed.

- The status quo: epochs with new keywords may not be able to use those in the API of prior epochs.

- There might also be a need for raw keywords in the other direction, e.g. so the older epoch can still use the new `catch` functionality somehow. I think this particular case is already served well enough by `do catch { ... }`, if we choose to stabilize it that way.

# Unresolved questions

- Do macros need any special care with such identifier tokens?
- Should diagnostics use the `r#` syntax when printing identifiers that overlap keywords?
- Does `rustdoc` need to use the `r#` syntax? e.g. to document `pub use old_epoch::*`

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [July 7, 2017, 11:04pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/2 "2017-07-07T23:04:53Z")

</div>

`catch` was discussed a lot in the “epoch” RFC, but this is a really bad example to work with.  
It’s not necessary to make it a keyword, `catch` for catch blocks can be introduces as a context dependent identifier with zero breakage in practice, which is much less than what is routinely done by library additions and type inference.

This is kinda offtopic for raw identifiers in general, but still.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [July 7, 2017, 11:19pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/3 "2017-07-07T23:19:50Z")

</div>

Do you have a better motivating example?

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [July 8, 2017, 12:22am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/4 "2017-07-08T00:22:39Z")

</div>

Off the top of my head, the only non-keywords I can come up with that we might plausibly want to make into (real, non-contextual) keywords someday are `catch`, `async`, `await`, `dyn`, and `union`. Though it’s likely half of those will always be mere contextual keywords, and I’ve probably missed some, not to mention excluded a lot of implausible suggestions (like `lifetime`)…

The big question I have about this RFC is how often any of these maybe-someday-keywords are actually used in public Rust APIs. Over in the epochs thread it was very important to point out that no epoch proposal can guarantee flawless interoperability between differently-epoch’d crates because of issues like this, but this is only one of many possible changes epochs might be used for, and it needs to be demonstrated that this is or will be a significant enough problem in practice to justify new syntax. For what little it’s worth, I’ve personally never seen a public method with one of those names.

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [July 8, 2017, 12:52am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/5 "2017-07-08T00:52:13Z")

</div>

> [@cuviper](#):
>
> Do you have a better motivating example?

Not right now. Maybe in a few years, when language constructions start fighting each other over remaining pieces of syntax.

I want `is` and `is not` reserved to some extent for `x is Some(..)`, but it seems doable with context dependent identifiers as well (I also like my `for i in is { for j in js { .... } }` 🙂 ).

---

<div class="post-metadata">

**Author:** ![Havvy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/havvy/32/178_2.png) [@Havvy](https://internals.rust-lang.org/u/Havvy)\
**Post date:** [July 8, 2017, 3:12am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/6 "2017-07-08T03:12:59Z")

</div>

I’d rather we just allow aliasing via `use` e.g. `use baseball::catch as catch_baseball;` as it localizes the keyword as an identifier to just the `use` syntax. On the other hand, stropping would be useful if e.g. one wanted to name a function that same thing as a Rust keyword that already exists for interopt with some other system, but I don’t know of any such systems.

---

<div class="post-metadata">

**Author:** ![bbatha](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bbatha/32/2662_2.png) [@bbatha](https://internals.rust-lang.org/u/bbatha)\
**Post date:** [July 8, 2017, 3:57am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/7 "2017-07-08T03:57:21Z")

</div>

This feature is also pretty important for cross-epoch compatible macros, particularly procedural ones. The motivation section should definitely mention that.

---

<div class="post-metadata">

**Author:** ![jplatte](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jplatte/32/11437_2.png) [@jplatte](https://internals.rust-lang.org/u/jplatte)\
**Post date:** [July 8, 2017, 10:55am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/8 "2017-07-08T10:55:49Z")

</div>

> [@cuviper](#):
>
> It may also be called [stropping](https://en.wikipedia.org/wiki/Stropping_(syntax))

Actually, raw identifiers are the opposite of that. Stropping, according to the linked article, is explicitly marking something as **not** being an identifier.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [July 9, 2017, 6:04am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/9 "2017-07-09T06:04:28Z")

</div>

> [@Ixrec](#):
>
> we might plausibly want to make into (real, non-contextual) keywords someday are catch, async, await, dyn, and union.

Would you kick out `HashSet::union` and `BTreeSet::union`!?! 😉

> [@Ixrec](#):
>
> The big question I have about this RFC is how often any of these maybe-someday-keywords are actually used in public Rust APIs.

My hope is to firm up the compatibility story. Rather than having to say "it's broken but hopefully not a problem in practice," we can offer a definite means of supporting it.

> [@Havvy](#):
>
> I'd rather we just allow aliasing via use e.g. use baseball::catch as catch\_baseball; as it localizes the keyword as an identifier to just the use syntax.

That doesn't help for methods or associated items.

> [@bbatha](#):
>
> This feature is also pretty important for cross-epoch compatible macros, particularly procedural ones. The motivation section should definitely mention that.

Can you elaborate what you would say about that? It's not clear to me what the advice for macro writers would actually be. Should they have to write all their identifiers in the raw form? (I hope not...) See [@aturon's reply about macros](https://github.com/rust-lang/rfcs/pull/2052#issuecomment-312748189) too.

> [@jplatte](#):
>
> Actually, raw identifiers are the opposite of that. Stropping, according to the linked article, is explicitly marking something as not being an identifier.

It's inconsistent -- the examples in the [Modern use](https://en.wikipedia.org/wiki/Stropping_(syntax)#Modern_use) section are all marking identifiers.

---

<div class="post-metadata">

**Author:** ![Havvy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/havvy/32/178_2.png) [@Havvy](https://internals.rust-lang.org/u/Havvy)\
**Post date:** [July 9, 2017, 7:10am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/10 "2017-07-09T07:10:01Z")

</div>

@cuviper Then extend `use` to also create aliases to associated items.

---

<div class="post-metadata">

**Author:** ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)\
**Post date:** [July 9, 2017, 10:38am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/11 "2017-07-09T10:38:51Z")

</div>

> [@Havvy](#):
>
> I'd rather we just allow aliasing via use e.g. use baseball::catch as catch\_baseball; as it localizes the keyword as an identifier to just the use syntax. On the other hand, stropping would be useful if e.g. one wanted to name a function that same thing as a Rust keyword that already exists for interopt with some other system, but I don't know of any such systems.

Inadequate for methods and fields, though (fields especially).

---

<div class="post-metadata">

**Author:** ![sanxiyn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sanxiyn/32/8824_2.png) [@sanxiyn](https://internals.rust-lang.org/u/sanxiyn)\
**Post date:** [July 9, 2017, 12:09pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/12 "2017-07-09T12:09:36Z")

</div>

I like the idea. On the other hand, I prefer following C#'s syntax, that is @.

---

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [July 9, 2017, 2:48pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/13 "2017-07-09T14:48:53Z")

</div>

The biggest problem I see with C#'s syntax is confusion in patterns:

```rust
match Some(5) {
    @match @ Some(_) => println!("{:?}", @match),
    None => (),
}

```

I _think_ it’s guaranteed to be unambiguous, and I have no idea if it would ever be used in the same line in real world usage, but it’s one downside to that syntax I see. I also dislike the `r#ident` syntax _because_ it’s too visually similar to the raw string syntax.

* * *

Some other languages syntaxes:

- [VB.Net](https://msdn.microsoft.com/en-us/library/aa711644(v=vs.71).aspx): square brackets (`[ident]`). Definitely seems like it would be ambiguous with array expressions.
- [Swift](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html) and [Kotlin](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html): backticks (``ident``). I don’t think backticks are used anywhere in Rust yet.
- [Verilog](http://electrosofts.com/verilog/identifiers.html): leading backslash (`\ident`). I’m not certain if this actually escapes keywords, seems to be for escaping strings that would be invalid as identifiers normally (like `\1234`), but I think it would work fine for Rust.
- [VHDL93](http://computer-programming-forum.com/42-vhdl/888790a88593ac22.htm): surrounding backslashes (`\ident\`). This is different to the other languages in that `\ident\` is not treated the same as `ident` (assuming that `ident` is not a keyword so the escaping is not strictly required).
- [F#](https://stackoverflow.com/a/6639705): double backticks (```ident```). Again should be valid in Rust since backticks are unused.
- [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Reserved_word_usage) doesn’t allow escaping reserved words, but does allow using them in some places. For example you can have a reserved word as a property on an object (`var a = { if: 5 }`), you just cannot use them for variable names, free functions, etc. Not sure whether supporting something like that is possible/makes sense in Rust.
- Java/C/C++/Go: I believe do not support escaping reserved words as identifiers.

Personally I’m in favour of backticks (``ident``), it seems the most lightweight and matching other contemporary languages has some advantage.

---

<div class="post-metadata">

**Author:** ![jugglerchris](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jugglerchris/32/2661_2.png) [@jugglerchris](https://internals.rust-lang.org/u/jugglerchris)\
**Post date:** [July 10, 2017, 1:33pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/14 "2017-07-10T13:33:04Z")

</div>

I could imagine a delimited syntax being useful for possible future FFI cases (for example naming methods in languages with different identifier syntax; `r#operator==()#` or something). I could also imagine it not being useful for that, though.

---

<div class="post-metadata">

**Author:** ![bbatha](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bbatha/32/2662_2.png) [@bbatha](https://internals.rust-lang.org/u/bbatha)\
**Post date:** [July 10, 2017, 2:10pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/15 "2017-07-10T14:10:01Z")

</div>

> [@cuviper](#):
>
> Can you elaborate what you would say about that? It's not clear to me what the advice for macro writers would actually be. Should they have to write all their identifiers in the raw form? (I hope not...) See @aturon's reply about macros too.

Actually, @aturon's method seems more robust. I was envisioning manual escapes being useful and the compiler auto-escaping but these don't necessarily need to be the same representation. Providing the full epoch information for the macro seems more generally applicable.

---

<div class="post-metadata">

**Author:** ![fyl2xp1](https://avatars.discourse-cdn.com/v4/letter/f/a3d4f5/32.png) [@fyl2xp1](https://internals.rust-lang.org/u/fyl2xp1)\
**Post date:** [July 10, 2017, 3:56pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/16 "2017-07-10T15:56:49Z")

</div>

I’d really like to have this feature. Especially when generating FFI-code I always have to make sure the omnipresent `type` and the like get’s mangled accordingly.

I’d also vote for backticks as delimiter (as long as they’re not reserved for things like inline-assembly) and allowing all printable characters in between.

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [July 10, 2017, 7:42pm UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/17 "2017-07-10T19:42:56Z")

</div>

Let's try a few examples. I'm not trying to be exhaustive, just using a few interesting positions.

```rust
use foo::r#catch::bar;
let foo = r#catch { bar: 42 };
let foo = Foo { r#catch: 42 };
foo.r#catch = 42;
foo.r#catch(42);

```

```rust
use foo::@catch::bar;
let foo = @catch { bar: 42 };
let foo = Foo { @catch: 42 };
foo.@catch = 42;
foo.@catch(42);

```

```rust
use foo::\catch::bar;
let foo = \catch { bar: 42 };
let foo = Foo { \catch: 42 };
foo.\catch = 42;
foo.\catch(42);

```

```rust
use foo::`catch`::bar;
let foo = `catch` { bar: 42 };
let foo = Foo { `catch`: 42 };
foo.`catch` = 42;
foo.`catch`(42);

```

(`catch` has ceased to look like a real word to me...)

> [@jugglerchris](#):
>
> I could imagine a delimited syntax being useful for possible future FFI cases (for example naming methods in languages with different identifier syntax; r#operator==()# or something). I could also imagine it not being useful for that, though.

Wouldn't FFI cases like this have to be mangled anyway? Linkers are usually a limiting factor too.

> [@fyl2xp1](#):
>
> I'd also vote for backticks as delimiter (as long as they're not reserved for things like inline-assembly) and allowing all printable characters in between.

I don't actually like allowing non-identifier characters myself. What would you use it for?

---

<div class="post-metadata">

**Author:** ![liigo](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/liigo/32/454_2.png) [@liigo](https://internals.rust-lang.org/u/liigo)\
**Post date:** [July 11, 2017, 8:05am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/18 "2017-07-11T08:05:31Z")

</div>

since we have 0xFF, 0o007, eh … 0icatch? A bit ugly.

```rust
use foo::0icatch::bar;
let foo = 0icatch { bar: 42 };
let foo = Foo { 0icatch: 42 };
foo.0icatch = 42;
foo.0icatch(42);

```

```rust
`catch` is shinning. 

```

👍

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [July 11, 2017, 8:24am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/19 "2017-07-11T08:24:28Z")

</div>

I have a use case: Frequently within the compiler, clippy or other code I see things like `krate`, `match_`, … Raw identifiers would once and for all create an idiomatic naming scheme for such identifiers.

---

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)\
**Post date:** [July 11, 2017, 9:25am UTC](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502/20 "2017-07-11T09:25:47Z")

</div>

I have not really read this discussion, so I may be repeating someone else’s point, but I would consider a more general syntax for escaping _any_ identifier (with whitespace, punctuation, etc). We have something like this in Kotlin, and it works really great for naming tests:

```rust
fun `test trait with bounds on itself`() = checkByCode("""
    trait Foo<T: Foo<T>> {
        fn foo(&self) { }
    } //X

    impl Foo<()> for () { }

    fn bar<T: Foo<T>>(t: T) {
        t.foo()
    } //^

    fn main() { bar(()) }
""")

```

Here, the stuff between backtics is an identifier.

[Next page](https://internals.rust-lang.org/t/pre-rfc-raw-identifiers/5502.md?page=2)
