Skip to content

Fix: reshape_array calls private Base._reshape internal API #427

Description

@tmigot

Problem

src/ADNLPProblems/ADNLPProblems.jl:9 defines:

reshape_array(a, dims) = invoke(Base._reshape, Tuple{AbstractArray, typeof(dims)}, a, dims)

Base._reshape is a private, unexported function. It can be renamed, removed, or change signature across Julia versions without warning. This fragile pattern has silently broken other packages in the past.

The function is used in three problem files: bearing.jl, channel.jl, and marine.jl.

The reason it exists is that AD backends (Zygote, ForwardDiff, etc.) need a reshape that is correctly tracked through the AD graph. Plain reshape sometimes fails to do this, so a custom entry point is needed — but going through Base._reshape via invoke is not the right way to achieve it.

Suggested Fix

Replace invoke(Base._reshape, ...) with a method that AD can overload. A common pattern in the AD ecosystem is:

reshape_array(a, dims) = reshape(a, dims)

with a documented extension point for AD backends, or using ArrayInterface.restructure. Alternatively, check whether Functors.jl or the specific AD backends in use (Zygote, Enzyme) already provide a supported reshape utility.

The fix should be verified against the three affected problems (bearing, channel, marine) using ForwardDiff and Zygote to confirm the AD still works correctly.

Affected files

  • src/ADNLPProblems/ADNLPProblems.jl (definition)
  • src/ADNLPProblems/bearing.jl
  • src/ADNLPProblems/channel.jl
  • src/ADNLPProblems/marine.jl

References

Found during repository audit (E-SRC-1).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions