Skip to content

A bounded Drop impl on a std generic (impl[K: Ord + Clone, V] Drop for BTreeMap[K, V]) is not taken as drop glue: locals are not dropped at scope end, an explicit drop() works #2571

Closed

Description

@ehartford

Observed

lib/std/collections.w (branch mlib-phase4) declared:

impl[K: Ord + Clone, V] Drop for BTreeMap[K, V]:
    move fn drop(): ...

A BTreeMap[i32, Tag] local (Tag counts its drops) at scope end ran no destructor (total 0); an explicit drop(map) ran it (total 10). Changing the impl to impl[K, V] Drop for BTreeMap[K, V] made the implicit drop run (1, 11).

A user-tier generic with a bounded Drop impl (impl[T: Clone] Drop for Holder[T]) runs its destructor at scope end (probe printed 1111 with four drops), so the miss is specific to the std tier or to a std generic: Sema.type_has_drop_impl resolves the owner through type_sym_is_shadowed/select_trait_impl_tiered or impl_lookup, and one of those paths does not see a bounded impl.

Expected

A bounded Drop impl is the type's destructor exactly as an unbounded one is; a type whose Drop impl is skipped silently leaks (§ leaks are defects). The workaround in std (an unbounded impl) stands until this is fixed; a check that refuses a bounded Drop impl that drop glue cannot see would at least be loud.

Activity

  1. ehartford commented on Oct 23, 2026

    @ehartford
    ContributorAuthor

    Closed by #2580 (c7fa606), D154: a Drop impl's bounds are the type's own. An impl demanding a bound the type's declaration does not is refused ("a destructor runs for every instantiation", §2.4), which closes the silent-leak class; an impl restating the type's bounds is its destructor and drop glue already runs it (behav_2571). std's BTreeMap impl stays unbounded because the type declares no bounds. err_2571 pins the refusal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions