Skip to content

Commit edf4464

Browse files
committed
Fix panic during type serialization
1 parent 0dd32df commit edf4464

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

cobalt-ast/src/context.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,22 @@ impl Serialize for CompCtx<'_, '_> {
369369
S: Serializer,
370370
{
371371
use ser::*;
372+
#[allow(clippy::unnecessary_cast)]
373+
SERIALIZATION_CONTEXT.with(|c| {
374+
if let Some(ptr) = c.replace(Some(
375+
self as *const CompCtx<'_, '_> as *const CompCtx<'static, 'static>, // this intermediate cast is necessary
376+
)) {
377+
panic!("serialization context is already in use with an address of {ptr:p}");
378+
}
379+
});
372380
let mut map = serializer.serialize_struct("Context", 3)?;
373381
map.serialize_field("version", &HEADER_FMT_VERSION)?;
374382
map.serialize_field("types", &CtxTypeSerde(self))?;
375383
self.with_vars(|v| map.serialize_field("vars", v))?;
384+
SERIALIZATION_CONTEXT.with(|c| {
385+
c.replace(None)
386+
.expect("serialization context is empty after serialization")
387+
});
376388
map.end()
377389
}
378390
}

0 commit comments

Comments
 (0)