[EXPERIMENT] Disallow all literal suffixes except the standard numeric ones by nnethercote · Pull Request #103872 · rust-lang/rust (original) (raw)
To summarize the crater results: dozens of crates use literal suffixes in all sorts of interesting ways. Here are some examples from many (but not all) of the crates that were broken by the crater run disallowing arbitrary suffixes:
- https://github.com/ThatsNoMoon/evalvana:
r"[^a-z0-9\-_]"i
// seeregex!
fromlaxy-regex
crate - https://github.com/TheKK/gemuboi:
8bits
,16bits
- https://github.com/daniel5151/analog_literals:
1D
,2D_TOP
,2D_MID
,2D_BOTTOM
,3D_TOP_l_h
,3D_TOP_l_BOTTOM_l
,3D_MID_w
,3D_BOTTOM_h
- https://github.com/farmaazon/sixtyfps-bugs:
20px
,100px
- https://github.com/fenhl/lore-seeker-discord:
:2B:624281333740339210
,:2G:624281333769961514
,:2R:624281334948298752
,:2U:624281333350268929
,:2W:624281333824356379
- https://github.com/josh65536/tsurust:
<stop offset="0%" stop-color=("#"{color.x;02x}{color.y;02x}{color.z;02x})/>
- https://github.com/kriogenia/bresenham_zip:
3D:X
,3D:Y
,3D:Z
,2D:X
,2D:Y
- https://github.com/lonelyhentai/rusty-leetcode:
1e-64f
- https://github.com/maekoos/aar:
10t
,10x
,11n
,21s
,21h
,31i
,51l
,21c
- https://github.com/qiuchengxuan/ascii-osd-hud:
-1.0i8
,0.0i8
- https://github.com/qiuchengxuan/bmp280:
16bit
,20bit
- https://github.com/qiuchengxuan/max7456:
20ns
,100ns
- https://github.com/recmo/uint:
2_U512
,0x80000000000000000000000000000000_U512
- https://github.com/rpadaki/bio:
1s
,100px
,#4444aa
,#00bb88
- https://github.com/stavenko/gerb-view-spa:
800px
,1fr
- https://github.com/tjwilson90/goat:
88777S
,42H
- https://github.com/vrmiguel/negate:
compile_error!("Expected names (in name-value pairs) are either "docs" or "name", but a different name was found.")
// oh no - https://github.com/woodgear/simple-replace-templete-engine:
"hello _t_name_t_ _t_sec_name_t_"_
// Make underscore_literal_suffix a hard error. #103914 will disallow all_
suffixes - anything 0.1.3:
btu^2J^2
- 1ws-nitro-enclaves-attestation-ffi 0.1.0:
614967200ULL
- bitcoin-script 0.1.2:
12g34
- bmp280-core 0.2.0:
16bit
,0.0050dC
- cocogitto 5.2.0:
35B66CC21AEBFC9B0E8C89F1FD753A01E06E05D7
- cyfs-base-derive:
0u256
- dashu-float 0.2.1:
0x81p-6
,-0x817p-10
,0x915b1p-18
,0x1p4
- fomat-macros 0.3.1:
fomat!({=13:05b} ".")
- freebsd-kpi-13-1 0.1.4:
1U
,0xffff000000000000L
- guid-parser 0.1.0:
0cf00d
- hexlit 0.5.5:
0A
,0B
,0C
,0d
,0A_0B_0C
- hobo_css 0.3.0:
easy_enum! {transform-style flat preserve-3d}
- if_rust_version 1.0.0:
1010u543
- ifmt 0.3.3:
.3s
,11.3S
,420;#06x
, - mpu6000 0.3.0:
+/-16g
,2048LSB/g
,+/-2000dps
- onenote_parser 0.3.0:
guid!({1A5A319C-C26B-41AA-B9C5-9BD8C44E07D4})
- seq-macro 0.3.1:
0X09..0X10
// ?! - smallnum 0.4.1:
size_of()::<128TypeNeg>()
// ?! - starship 1.11.0:
2018-01-01T00:00:00Z
- test-with 0.8.0:
999GB
- typ 0.1.1:
0u
,1u
,2u
Use cases:
- Lots of custom units
- Some coordinates
- Some custom numeric suffixes, like
_U256
- Emulating syntax of others languages/formats, like C, HTML, CSS, UUIDs, timestamps
- A few bizarre cases that possibly aren't doing what the author thinks they're doing
Correctly handling some of these on the macro side must be quite the task.
Note that arbitrary literal suffixes go back to #19103 RFC 463, where the idea was introduced to "futureproof" Rust for the possibility of fancier suffixes, e.g. for different kinds of literals. One of the unresolved questions in that RFC was:
Should it be the parser or the tokenizer rejecting invalid suffixes? This is effectively asking if it is legal for syntax extensions to be passed the raw literals? That is, can a foo procedural syntax extension accept and handle literals like
foo!(1u2)
?
The answer chosen by the implementation was "the parser", which allowed arbitrary suffixes to be used as macro inputs. This arguably broke the futureproofing. Can new suffixes can be reasonably added, given that existing macros can (and do) effectively define their own? I'm honestly not sure.