Skip to content

Commit 42b6736

Browse files
committed
[RFC2603] Extend <const> to include str and structural constants.
1 parent 281e27f commit 42b6736

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

text/2603-rust-symbol-name-mangling-v0.md

+47-20
Original file line numberDiff line numberDiff line change
@@ -691,28 +691,30 @@ Mangled names conform to the following grammar:
691691
| "D" <dyn-bounds> <lifetime> // dyn Trait<Assoc = X> + Send + 'a
692692
| <backref>
693693
694-
<basic-type> = "a" // i8
694+
<basic-type> = <int-type>
695695
| "b" // bool
696696
| "c" // char
697697
| "d" // f64
698698
| "e" // str
699699
| "f" // f32
700-
| "h" // u8
701-
| "i" // isize
702-
| "j" // usize
703-
| "l" // i32
704-
| "m" // u32
705-
| "n" // i128
706-
| "o" // u128
707-
| "s" // i16
708-
| "t" // u16
709700
| "u" // ()
710701
| "v" // ...
711-
| "x" // i64
712-
| "y" // u64
713702
| "z" // !
714703
| "p" // placeholder (e.g. for generic params), shown as _
715704
705+
<int-type> = "a" // i8
706+
| "h" // u8
707+
| "i" // isize
708+
| "j" // usize
709+
| "l" // i32
710+
| "m" // u32
711+
| "n" // i128
712+
| "o" // u128
713+
| "s" // i16
714+
| "t" // u16
715+
| "x" // i64
716+
| "y" // u64
717+
716718
// If the "U" is present then the function is `unsafe`.
717719
// The return type is always present, but demanglers can
718720
// choose to omit the ` -> ()` by special-casing "u".
@@ -724,16 +726,40 @@ Mangled names conform to the following grammar:
724726
<dyn-bounds> = [<binder>] {<dyn-trait>} "E"
725727
<dyn-trait> = <path> {<dyn-trait-assoc-binding>}
726728
<dyn-trait-assoc-binding> = "p" <undisambiguated-identifier> <type>
727-
<const> = <type> <const-data>
728-
| "p" // placeholder, shown as _
729+
730+
// Constants are encoded structurally, as a tree of array/tuple/ADT constructors,
731+
// with integer(-like) leaves, not using the constant's memory representation.
732+
// See the comments on <const-int> & <const-str> for more details on leaf encoding.
733+
<const> = <int-type> <const-int>
734+
| "b" <const-int> // false, true
735+
| "c" <const-int> // '...'
736+
| "e" <const-str> // "..."
737+
| "R" <const> // &value
738+
| "Q" <const> // &mut value
739+
| "A" {<const>} "E" // [a, b, c, ...]
740+
| "T" {<const>} "E" // (a, b, c, ...)
741+
| "V" <path> <const-fields> // named struct/variant
742+
| "p" // placeholder, shown as _
729743
| <backref>
730744
731-
// The encoding of a constant depends on its type. Integers use their value,
732-
// in base 16 (0-9a-f), not their memory representation. Negative integer
733-
// values are preceded with "n". The bool value false is encoded as `0_`, true
734-
// value as `1_`. The char constants are encoded using their Unicode scalar
735-
// value.
736-
<const-data> = ["n"] {<hex-digit>} "_"
745+
<const-fields> = "U" // X
746+
| "T" {<const>} "E" // X(a, b, c, ...)
747+
| "S" {<identifier> <const>} "E" // X { field: value, ... }
748+
749+
// An integer(-like) constant's numeric value is encoded in base 16 (0-9a-f),
750+
// with negative integer values being preceded with "n".
751+
// For other types, the numeric value is the same one used for `as` casts, i.e.:
752+
// * `bool`: 0 for `false` (encoded as `0_`), 1 for `true` (encoded as `1_`)
753+
// * `char`: the Unicode scalar value
754+
<const-int> = ["n"] {<hex-digit>} "_"
755+
756+
// `str` constants are encoded as their (UTF-8) byte sequence, where each byte
757+
// always uses two hex nibbles.
758+
// Because the constant has `str` type, and not `&str`, demangling should make
759+
// that clear by e.g. demangling `616263_` as `*"abc"` (instead of `"abc"`).
760+
// In order to have constants of type `&str` demangle as a plain string literal
761+
// (i.e. without `&*`), demanglers can special-case `Re...` constants.
762+
<const-str> = {<hex-digit> <hex-digit>} "_"
737763
738764
// <base-62-number> uses 0-9-a-z-A-Z as digits, i.e. 'a' is decimal 10 and
739765
// 'Z' is decimal 61.
@@ -1156,3 +1182,4 @@ pub static QUUX: u32 = {
11561182
- Make `<binder>` optional in `<fn-sig>` and `<dyn-bounds>` productions.
11571183
- Extend `<const-data>` to include `bool` values, `char` values, and negative integer values.
11581184
- Remove type from constant placeholders.
1185+
- Extend `<const>` to include `str` and structural constants

0 commit comments

Comments
 (0)