Skip to content

Instantly share code, notes, and snippets.

@danielhenrymantilla
Created June 12, 2025 17:14
Show Gist options
  • Save danielhenrymantilla/3bab7cd8595e9c11cfaf0a552abf14c2 to your computer and use it in GitHub Desktop.
Save danielhenrymantilla/3bab7cd8595e9c11cfaf0a552abf14c2 to your computer and use it in GitHub Desktop.
Liveness and lifetime-using drop glue

Liveness and lifetime-using drop glue

Rules of liveness: about the "discard point"

  1. with no drop glue, no "library" code gets executed, so a fortiori, no lifetimes/borrows/w/e are used/dereferenced, so it would be sound for lifetimes of the discardee to be dangling here.

    ⇒ No need for liveness to extend to this point

  2. with drop glue:

    1. either the drop glue does somehow refer to that lifetime,

      ⇒ liveness needs to extend to this discard point,

      • Examples:
        • type T<'lt> = PrintOnDrop<&'lt str>

          Code
          use ::core::fmt::Debug;
          use Drop as PrependExtraDropGlue;
          
          struct PrintOnDrop<T : Debug>(T);
            
          impl<T : Debug> PrependExtraDropGlue for PrintOnDrop<T> {
              fn drop(&mut self) {
                  dbg!(&self.0);
              }
          }
          
          // when T = &'lt str, for instance, we have:
          #[cfg(example)]
          impl<'lt> PrependExtraDropGlue for PrintOnDrop<&'lt str> {
              fn drop(&mut self) {
                  /* stuff we don't know about, from the outside */
              }
          }
        • type T<'lt> = MutexGuard<'lt, …>

        • type T<'lt> = impl use<…, 'lt, …> + Trait

        • type T<'lt> = dyn 'lt + Trait or dyn Trait<… 'lt …>

    2. or it is "known not to", so we are kind of back to the "no drop glue" case, and:

      ⇒ No need for liveness to extend to this point.

      • Examples:
        • type T<'lt> = (&'lt str, Vec<u8>)
        • type U<'lt> = Vec<&'lt str> // <- subtle!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment