Skip to content

fix(hir-def): don't panic instantiating cross-file extends chains (#236)#256

Open
avrabe wants to merge 2 commits into
mainfrom
fix/instance-extends-cross-file-panic
Open

fix(hir-def): don't panic instantiating cross-file extends chains (#236)#256
avrabe wants to merge 2 commits into
mainfrom
fix/instance-extends-cross-file-panic

Conversation

@avrabe
Copy link
Copy Markdown
Contributor

@avrabe avrabe commented Jun 3, 2026

Fixes #236spar instance panicked with index out of bounds: the len is 0 but the index is 0 (instance.rs) when a system implementation extends a parent declared in a different file.

Cause

collect_impl_chain walks the extends chain across trees (files) and collects each inherited subcomponent's SubcomponentIdx — but dropped the tree it was declared in. The caller then paired every inherited sub_idx with the most-derived impl's loc.tree. When that derived impl declares no subcomponents of its own, its tree's subcomponents slice is empty, so subcomponents[sub_idx] (index 0 into len 0) panicked.

Fix

Carry the originating tree_idx per subcomponent through ImplChainResult.subcomponents, and use it instead of loc.tree at the three consumer sites: recursive instantiation (Some((sub_tree, sub_idx))), build_leaf_property_map, and build_anon_property_map.

Verification

  • Reproduced with the reporter's files (dataflow.aadl + dataflow_diagram.aadl + hw.aadl): panic before, full instance tree after.
  • New regression test extends_chain_across_files_does_not_panic — base impl with a subcomponent + a derived impl that extends it from a separate tree with no own subcomponents (the exact panic shape); asserts the inherited subcomponent instantiates.
  • cargo test -p spar-hir-def: 458 + 1 pass, 0 failures.

Note

The reporter's model also surfaces applies_to path 'my_process.c1' could not be resolved warnings for connection properties — that's the separate #237 (properties on connections/features), not addressed here. This PR is scoped to the panic.

🤖 Generated with Claude Code

`spar instance` panicked (`index out of bounds: the len is 0 but the
index is 0` at instance.rs) when a system implementation `extends` a
parent declared in a different file.

Cause: `collect_impl_chain` walks the extends chain across trees and
collects each inherited subcomponent's `SubcomponentIdx`, but dropped the
tree the subcomponent was DECLARED in. The caller then paired every
inherited `sub_idx` with the most-derived impl's `loc.tree`. When that
derived impl declares no subcomponents of its own, its tree's
`subcomponents` slice is empty and `subcomponents[sub_idx]` panics.

Fix: carry the originating `tree_idx` per subcomponent through
`ImplChainResult.subcomponents`, and use it (not `loc.tree`) at the three
consumer sites — recursive instantiation, leaf property map, and anon
property map.

Regression test `extends_chain_across_files_does_not_panic` builds a base
impl (with a subcomponent) and a derived impl that `extends` it from a
separate file/tree and declares no own subcomponents — the exact shape
that panicked — and asserts the inherited subcomponent instantiates.

Closes #236.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@avrabe avrabe enabled auto-merge (squash) June 3, 2026 04:47
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

Rivet verification gate

20/20 passed

count
Passed 20
Failed 0
Skipped (no steps) 0

Filter: (and (= type "feature") (or (has-tag "v093") (has-tag "v0100")))

Failed artifacts

(none)

Updated automatically by tools/post_verification_comment.py. Source of truth: artifacts/verification.yaml.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 3, 2026

Codecov Report

❌ Patch coverage is 94.73684% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/spar-hir-def/src/instance.rs 94.73% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

cargo fmt — no behavior change. (Format CI check.)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ComponentType extension error while instantiating

1 participant