fix(types): repeat struct bounds in where-clauses to stop silent widening to Any#372
Merged
Conversation
…ning to Any
A `where {X}` clause that names a type parameter without repeating the bound
the struct already declares silently widens it to `<:Any`. This is invisible
via `isa` on concrete instances but breaks Julia's method-specificity ranking
the moment a competing method exists — either mis-dispatching silently or
throwing `MethodError: ... is ambiguous`.
Fixes the 6 sites found in the 2026-07-12 alias/where bound-dropping audit:
- Models/model.jl: `time_dependence(::Model{TD})` now `where {TD<:TimeDependence}`.
- Components/constraints_accessors.jl: the 5 ConstraintsModel accessors each
restrict 4 params to `<:Tuple` in the type-parameter position but extracted
the 5th via a bare `where`; now repeat `<:Tuple` on it.
Also applies the audit's two optional hardening bounds, verified safe against
every construction site (production and tests):
- CoercedTrajectory: `coerce::C` is always `only`/`identity`
→ `C<:Union{typeof(only),typeof(identity)}`.
- MergedTrajectory: `comps::C` is always `Dict{Int,Function}`
→ `C<:AbstractDict{Int,<:Function}` (struct + both hand-written constructors).
Adds a regression guard (test/suite/meta/test_where_bounds.jl) asserting each
fixed method's induced TypeVar upper bound is the intended bound, not `Any`.
Full suite: 4014/4014 pass (incl. Aqua ambiguity checks). No behaviour change —
tightening a where/field bound cannot invalidate a previously-valid call.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
The 2026-07-12 alias/
wherebound-dropping audit found 6 genuine bugs: methods whosewhere {X}clause names a type parameter without repeating the bound the struct already declares, silently widening it to<:Any. This is invisible viaisaon concrete instances, but it breaks Julia's method-specificity ranking the moment a competing method exists — either mis-dispatching silently or throwingMethodError: ... is ambiguous.All changes only tighten
where/field bounds. Tightening a bound cannot make a previously-valid call invalid, so there is no behaviour change.Changes
The 6 fixes
Models/model.jl:time_dependence(::Model{TD})→where {TD<:TimeDependence}(matches the struct declaration and inner constructor).Components/constraints_accessors.jl: the 5ConstraintsModelaccessors each restrict 4 params to<:Tuplein the type-parameter position but extracted the 5th via a barewhere— now repeat<:Tupleon it.§5 hardening (both verified safe against every construction site, production + tests)
CoercedTrajectory:coerce::Cis alwaysonly/identity→C<:Union{typeof(only),typeof(identity)}.MergedTrajectory:comps::Cis alwaysDict{Int,Function}→C<:AbstractDict{Int,<:Function}(struct + both hand-written constructors).Regression guard
test/suite/meta/test_where_bounds.jl(auto-discovered by the runner) asserts each fixed method's inducedTypeVarupper bound is the intended bound, notAny— a future re-drop fails loudly here.Verification
ConstraintsModelfixes.Note
The audit's "exactly one method per name" was slightly off — each accessor also has a convenience
Model-forwarding overload. Harmless; the guard useswhich(f, Tuple{ConstraintsModel})to target the correct method.🤖 Generated with Claude Code