Skip to content

Keep generic specialization identities distinct across type kinds and source names #952

Closed

Description

@LunaStev

Problem

Generic instance names are used as specialization cache keys, but their type encoding is not injective. A user struct named p_i32 and ptr<i32> both encode as p_i32. The first instantiation is silently reused for the second type and can produce wrong executable results without a diagnostic.

Audited canonical master d59bdab376bdc04edacdc8029b256cb8d55b5adf on Linux amd64 with checkout-built Wave 0.0.1-alpha-dev, Rust 1.89.0 and LLVM 21.1.8.

Reproduction

struct p_i32 { x: i64; y: i64; }
fun width<T>() -> u64 {
    return __wave_size_of<T>();
}
fun main() {
    println("{} {}", width<ptr<i32>>(), width<p_i32>());
}
target/debug/wavec build repro.wave --run -O0 -o /tmp/wave-generic-collision-o0
target/debug/wavec build repro.wave --run -O2 -o /tmp/wave-generic-collision-o2

Both commands succeed and print 8 8. Expected on this target: 8 16. Reversing the two calls prints 16 16, confirming that the first specialization is reused. Renaming the struct to Item, or using direct __wave_size_of<ptr<i32>>() / __wave_size_of<p_i32>() calls, prints 8 16 at both optimization levels.

A typed identity function additionally rejects valid source after the same collision: calling identity<ptr<i32>> followed by identity<p_i32> reports that identity$g$p_i32 expected a pointer but received the struct. Distinct Unicode names such as 가 and 나 both become _ through sanitize_ident, reproducing another collision in the same scheme.

Scope

Make the internal generic instance identity collision-free across semantic type kinds and source names, including UTF-8 and nested applications. Cache identity must distinguish types even when an emitted readable name would otherwise collide. Preserve deterministic reuse for genuinely identical types, recursive specialization behavior, and the current instantiation limits. Do not reject valid user type names as a workaround.

Starting points: specialization cache lookup, mangle_instance_name and mangle_type, lossy sanitization. The same naming helper is used by struct instantiation.

The source explicitly describes this as an internal compiler symbol scheme. This concrete frontend identity defect is separate from the future public ABI design tracker #350 and its fixture format #368.

Difficulty: medium; help wanted. Not marked good first issue because the fix must preserve distinct type identities across functions, structs and nested generic applications.

Completion criteria

  • The reproduction prints 8 16 regardless of call order at O0/O2.
  • Named structs cannot collide with pointer, array, scalar, future or variant type encodings.
  • Distinct UTF-8 names and nested generic applications remain distinct.
  • Identical repeated instantiations still deduplicate deterministically; recursive generic regressions continue to pass.

Activity

  1. added
    bug
    help wanted
    on Oct 18, 2026
  2. added a commit that references this issue on Nov 13, 2026
    be6a7a8
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

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions