Skip to content

Commit 2715f3e

Browse files
tamer: sym: Expose raw SymbolId for static symbols
This provides a child `raw` module that exposes a SymbolId representing the inner value of each of the static newtypes. This is needed in situations where the type must match and the type of the static symbol is not important. In particular, when comparing against runtime-allocated symbols in `match` expressions. It is also worth noting that this commit managed to hit a bug in Rustc that was fixed on 10/1/2021. We use nightly, and it doesn't seem that this occurred in stable, from bug reports. - rust-lang/rust#89393 - rust-lang/rust@5ab1245 - Original issue: rust-lang/rust#72476 The error was: compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs:1191:22: Unexpected type for `Single` constructor: <u32 as sym::symbol::SymbolIndexSize>::NonZero thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1146:9 This occurred because we were trying to use `SymbolId` as the type, which uses a projected type as its inner value: `SymbolId<Ix: SymbolIndexSize>(Ix::NonZero)`. This was not a problem with the static newtypes because their inner type was simply `SymbolId<Ix>`, which is not projected. This is one of the risks of using nightly. But, the point is: if you receive this error, upgrade your toolchain.
1 parent 581b9d4 commit 2715f3e

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

tamer/src/sym/prefill.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ macro_rules! static_symbol_consts {
199199
$str,
200200
"\"`."
201201
)]
202+
#[doc=""]
203+
#[doc=concat!(
204+
"For the raw (untyped) version, see [`raw::",
205+
stringify!($name),
206+
"`]."
207+
)]
202208
pub const $name: static_symbol_ty!($ty) =
203209
<static_symbol_ty!($ty)>::new($i);
204210

@@ -239,6 +245,36 @@ macro_rules! static_symbols {
239245
)*
240246
}
241247

248+
/// Expose each of the [typed static symbols](super) as raw
249+
/// [`SymbolId`] values.
250+
///
251+
/// These constants are useful for `match` expressions and any other
252+
/// contexts where the type of the symbol must match,
253+
/// and where the static type metadata is unimportant.
254+
///
255+
/// This is equivalent to calling `as_sym` on the static newtype,
256+
/// or using [`st_as_sym`](super::super::st_as_sym).
257+
pub mod raw {
258+
use super::SymbolId;
259+
260+
$(
261+
#[doc=concat!(
262+
"Raw (untyped) interned `",
263+
stringify!($ty),
264+
"` string `\"",
265+
$str,
266+
"\"`."
267+
)]
268+
#[doc=""]
269+
#[doc=concat!(
270+
"For the typed version, see [`super::",
271+
stringify!($name),
272+
"`]."
273+
)]
274+
pub const $name: SymbolId<$size> = super::$name.as_sym();
275+
)*
276+
}
277+
242278
/// Fill a new interner with static symbols.
243279
///
244280
/// Panics
@@ -317,6 +353,11 @@ static_symbol_newtypes! {
317353
/// These contexts are intended for use in generated code where a better
318354
/// context cannot be derived.
319355
ctx: ContextStaticSymbolId<u16>,
356+
357+
/// This symbol serves only as a marker in the internment pool to
358+
/// delimit symbol ranges;
359+
/// its string value is incidental and should not be relied upon.
360+
mark16: Mark16StaticSymbolId<u16>,
320361
}
321362

322363
/// Static symbols (pre-allocated).
@@ -374,7 +415,7 @@ pub mod st {
374415
}
375416

376417
static_symbols! {
377-
<global::ProgSymSize>;
418+
<crate::global::ProgSymSize>;
378419

379420
// Decimal strings are expected to be at index (n+1).
380421
// See `decimal1`.
@@ -476,7 +517,7 @@ pub mod st16 {
476517

477518
// Marker indicating the end of the static symbols
478519
// (this must always be last).
479-
END_STATIC: mark "{{end}}"
520+
END_STATIC: mark16 "{{end}}"
480521
}
481522
}
482523

@@ -525,6 +566,12 @@ mod test {
525566
assert_eq!(st::L_FALSE.as_sym(), "false".intern());
526567
}
527568

569+
// Just ensure raw symbols are available and match.
570+
#[test]
571+
fn sanity_check_st_raw() {
572+
assert_eq!(st::L_TRUE.as_sym(), st::raw::L_TRUE);
573+
}
574+
528575
#[test]
529576
fn global_sanity_check_st16() {
530577
// If we _don't_ prefill, make sure we're not starting at the first

0 commit comments

Comments
 (0)