Skip to content

Prefer explicitly declared overloads over generic substitutions#1213

Merged
marcauberer merged 1 commit into
mainfrom
fix/prefer-explicit-overload-over-generic
Jun 15, 2026
Merged

Prefer explicitly declared overloads over generic substitutions#1213
marcauberer merged 1 commit into
mainfrom
fix/prefer-explicit-overload-over-generic

Conversation

@marcauberer

Copy link
Copy Markdown
Member

What changed

  • Overload resolution now prefers an explicitly declared (non-generic) overload over a generic substitution when both match a call equally well, mirroring C++ where a non-template wins over a template specialization. Implemented as a secondary tie-break in FunctionManager::breakOverloadTie, applied after the existing qualifier-specificity narrowing.
  • A losing generic substitution that was only inserted for this very match is removed from its declaration's manifestation list again, so the IR generator never tries to emit a manifestation that was never type-checked (and no dead code is left behind).
  • Repurposed the error-ambiguous-generic-functions test (which encoded the old "this is ambiguous" behavior) into success-generic-overload-prefers-explicit, which now asserts at runtime that the explicit foo(byte*&) overload is selected over the generic foo<byte>(byte*&).

Why

Previously, declaring both a non-generic overload and a generic one that substitutes to the same signature made the call ambiguous. This is a common and legitimate pattern (e.g. a dedicated copy constructor alongside a generic value constructor), so the non-template should win rather than error.

How it was validated

  • cmake --build cmake-build-debug --target spice spicetest — builds clean
  • cmake-build-debug/test/spicetest --gtest_filter='TypeCheckerTests.*:StdTests.*:-StdTests.bindings_llvm' — 296 passed, 1 skipped (net_socketsBasic)
  • StdTests.bindings_llvm is excluded as it fails only due to a local environment issue (mold cannot find the LLVM AArch64 libs); it compiles cleanly. BootstrapCompilerTests are a known local environment failure.

Follow-up / known limitations

A follow-up PR adds a generic value constructor to std/type/any that relies on this overload-resolution behavior.

When an explicitly declared (non-generic) function and a generic
substitution match a call equally well, overload resolution reported an
ambiguity. Mirror C++ semantics and prefer the non-template, applied as a
secondary tie-break after qualifier specificity. A losing generic
substitution that was only inserted for this match is de-registered again,
so the IR generator never emits a manifestation that was never type-checked.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@marcauberer marcauberer requested a review from a team as a code owner June 15, 2026 20:16
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions github-actions Bot added tests Contains changes to the test cases compiler labels Jun 15, 2026
@marcauberer marcauberer merged commit dc32f95 into main Jun 15, 2026
8 of 9 checks passed
@marcauberer marcauberer deleted the fix/prefer-explicit-overload-over-generic branch June 15, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler size/M tests Contains changes to the test cases

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant