Skip to content

Sealed-trait downcast pattern (name: Type) has no subject once by-value dyn is refused; its lowering copies an unsized value, duplicates the payload, and returns uninit on no match #1860

Closed

Description

@ehartford

Context

#1852 refuses a by-value dyn Trait parameter; the spec says dyn Trait is unsized (§11.3). The only other tree users of the form are test/behavior/sealed_trait_match.w and test/compile_errors/err_sealed_not_exhaustive.w. Both match a sealed trait object with a typed-binding downcast:

fn describe(s: dyn Shape) -> i32:
    match s:
        c: Circle => c.radius
        r: Rect => r.width + r.height

The pattern name: Type is not in the grammar: docs/spec/grammar.md PATTERN has no typed binding. Its only working subject is the by-value dyn that #1852 refuses.

What the other spellings do (fix-globals stage1 and main alike)

  • s: &dyn Shape, match s: → Sema refuses it: "type-annotated binding pattern name: Type is only valid to downcast a trait object" (SemaCheck NK_PAT_TYPED_BIND requires a TY_TRAIT_OBJ subject).
  • match *s: → compiles. MIR copies the unsized value out (_3 = copy _2.*). At run time, describe(c) ends the whole program silently with exit 0: print("start"), then nothing, rc=0.
  • In every form, the arm-less path returns a never-written local (switchInt(...) -> [1: bbN, otherwise: bb1], where bb1 does _0 = copy _4; return with _4 unassigned). There is no trap, and check --validate-all reports ok over it.
  • The binding is DYN_DOWNCAST, which loads a byte copy of the payload (CodegenDispatch DYN_DOWNCAST: wl_build_load(concrete_ty, data_ptr)). For an implementor with a Drop, that is a second owner (§2.3: transport is not duplication). MIR and codegen resolve the concrete type and the vtable by symbol text (MirLower NK_PAT_TYPED_BIND named_types by name, codegen sema_symbol_text), a D65 re-derivation.

Needs a ruling

Where does the sealed-trait downcast live once a by-value dyn is gone?

  1. Typed bindings over a &dyn T subject bind a view &C (D22: patterns are transparent to view origins). This takes grammar text for the pattern and fixes of the defects above.
  2. Retire the downcast pattern with the by-value form, and reword the two tests to exercise sealed-ness without downcasting.

Until one lands, the #1852 refusal cannot merge without breaking these two tests. The patch is ready on fix-globals (out/g1852/refusal.patch).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions