rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Escape sequences in identifiers

Open kryptine opened this issue 4 years ago • 15 comments

C# and JavaScript allows escape sequences to be used in identifiers. Both languages use the same Unicode identifier convention as Rust, but also allows Unicode escape sequences to be inserted directly into identifiers. This is useful for calling foreign code with Unicode identifiers in them.

Edit: "Foreign" code includes Rust code imported from other modules or crates which may use Unicode characters in identifiers, not just code in other languages.

kryptine avatar Jul 18 '22 10:07 kryptine

This is useful for calling foreign code with Unicode identifiers in them.

Note that you can already do this:

extern "C" {
    #[link_name = "λö§"]
    fn lambda_omlaut_para();
}

fn main() {
    unsafe { lambda_omlaut_para(); }
}

On playground we get this linker error:

error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/tmp/rustcrwTOxA/symbols.o" "/playground/target/debug/deps/playground-f30fbb6d88a6f004.playground.8b8c9ccc-cgu.0.rcgu.o" "/playground/target/debug/deps/playground-f30fbb6d88a6f004.4i1b8ushxzihkqu6.rcgu.o" "-Wl,--as-needed" "-L" "/playground/target/debug/deps" "-L" "/playground/target/debug/build/libsqlite3-sys-66f95ec5f0e221b1/out" "-L" "/playground/target/debug/build/ring-4aead75a5a56aa0f/out" "-L" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-7ca39ac42651c3df.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-62c6d032818141a1.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-50484fc03eb1eb5b.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-758be083b246d9c6.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-3cdf9a3c68f76e2d.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-3a1b74821c25a0e1.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-e046d82ebd84bb7f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-a61cdd33cfa8394f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-410c38f8df854235.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-f79d7458e122215f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-7d24b750ce5b22e8.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-4c2aa1ea3133ab73.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-41220dc85a7f114f.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-230d004276c898f9.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-03ae30169a5438be.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-6f82c44b7818af35.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-1a0b7681f7efa789.rlib" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-83735dd4dae9b02c.rlib" "-Wl,--end-group" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-385029872275478f.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/playground/target/debug/deps/playground-f30fbb6d88a6f004" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
  = note: /usr/bin/ld: /playground/target/debug/deps/playground-f30fbb6d88a6f004.playground.8b8c9ccc-cgu.0.rcgu.o: in function `playground::main':
          /playground/src/main.rs:7: undefined reference to `λö§'
          collect2: error: ld returned 1 exit status
          
  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

Also, as another aside, out of λö§, only § is not valid in Rust identifiers, and generally any language that uses Unicode-recommended XID_Start XID_Continue* identifiers will have matching restrictions to Rust.

eddyb avatar Jul 18 '22 15:07 eddyb

I mean calling code in other Rust crates, not "foreign" code as in code in other languages. C# and JavaScript are cited only as languages that have the feature I am proposing. Since Rust supports Unicode identifiers, it should also support escape sequences to make them easy to type.

kryptine avatar Jul 18 '22 16:07 kryptine

I encourage you to provide an example in this issue with what exactly you want to enable and why it would be useful.

shepmaster avatar Jul 18 '22 17:07 shepmaster

I mean calling code in other Rust crates, not "foreign" code as in code in other languages.

Ah, my bad, "foreign" is almost always used in the context of "FFI" (Foreign Function Interface) in Rust, so I just assumed that's what you meant.

it should also support escape sequences to make them easy to type

I would expect that with both unicode (when you can't type them directly) and escape sequences, you'd almost always copy-paste such identifiers (instead of trying to inputs dozens of hex nibbles by hand).

cc @Manishearth

eddyb avatar Jul 18 '22 17:07 eddyb

Yeah, I agree with @shepmaster here: I'd like to see an example.

Overall the point of identifiers is for them to be read by the programmer, I do not think people should define identifiers that need escapes to be distinguishable.

(Also, JS and C# do not use the same identifier conventions as Rust)

Manishearth avatar Jul 18 '22 18:07 Manishearth

For example, сумма could be represented as \u{441}\u{443}\u{43C}\u{43C}\u{430} and 總和 could be represented as \u{7E3D}\u{548C}. Existing equivalent identifiers will continue to be equivalent, as in C# and JavaScript which also use XID_Start and XID_Continue. Current Rust already allows people to define visually identical identifiers. This allows IDEs to autocomplete the right identifier for you.

kryptine avatar Jul 19 '22 00:07 kryptine

That's not a sufficient example, I understand that we allow non ascii identifiers (I designed the feature), but so far I don't see any strong motivation for why one would want to use escapes to refer to them.

Autocomplete ought to work with them already, and there's already builtin protections around potentially visually identical identifiers.

Manishearth avatar Jul 19 '22 06:07 Manishearth

My keyboard and environment doesn't allow me to enter non-ASCII characters, is there any other way for me to refer to any identifier without having to type Unicode characters? Does Rust even allow entering the whole source code file in Base58 or Base64 or UTF-7? If you didn't support non-ASCII identifiers there would be no problem, now that you support them you must allow people to easily type them.

kryptine avatar Jul 20 '22 23:07 kryptine

copy paste them, they're identifiers

is typing out an escape sequence really better?

in general I'd say the ability of their target users to type API calls is something library designers have to determine. If they're making a crate for use amongst the general rust community, it's probably not a good idea for them to use non ascii identifiers in public APIs. I'd kinda imagine this feature would be used more for local identifiers and public ones for multi crate applications, not library APIs.

Overall this feels like a solution in search of a problem: we don't even have many crates doing this yet.

I'm inclined to close this (in part because issues on the RFC repo are not really a part of the process)

Manishearth avatar Jul 21 '22 02:07 Manishearth

Of course it is better, copy-pasting multiple identifiers is very slow and may not even be possible, compared to typing escape sequences and then tab. Should I start by writing a RFC and submit a pull-request? The instructions on this repo are very confusing, which is why I started on this "Issues" tab.

kryptine avatar Jul 21 '22 03:07 kryptine

@kryptine When you said "type escape sequence and then tab", i think you're talking about IDE experience, instead of the interaction between actual stored-on-disk source code file and the compiler. If that's the case, rust-analyzer is the place you want to talk about this. If there's any more questions, feel free to create a topic on our Zulip!

crlf0710 avatar Jul 21 '22 05:07 crlf0710

You could write an RFC. It may help to write a pre-rfc first on internals.rust-lang.org. I am skeptical that it will be accepted, however (not that I am in charge of that discussion).

Manishearth avatar Jul 21 '22 15:07 Manishearth

Here is an example where people use unfamiliar unicode codepoint to prevent others from using it:

https://docs.rs/lending-iterator/latest/lending_iterator/lending_iterator/trait.LendingIterator%E0%B6%9EItem.html

If you find it hard to type certain characters, it's a feature, not a bug.

lebensterben avatar Jul 21 '22 17:07 lebensterben

Why would you have non-ASCII identifiers to start with if you did not intend to type them out directly?

SOF3 avatar Aug 01 '22 04:08 SOF3

Why would you have non-ASCII identifiers to start with if you did not intend to type them out directly?

The author of a library that uses non-ASCII identifiers may not be the same person as the consumer of the library. The author may have the capability to enter the non-ASCII identifiers while the consumer does not.

Note that I don't particularly want Rust to gain di/trigraph-equivalents, but it's worth understanding the original poster's intent.

shepmaster avatar Aug 01 '22 19:08 shepmaster