Skip to content

feat(sv): support generate-block imports of types and subroutines that differ from the module's - #1440

Merged
mergify[bot] merged 1 commit into
masterfrom
stack/tignear/feat/sv-generate-block-imports/support-generate-block-imports-types-subroutines--c706ae8c
Oct 18, 2026
Merged

mergify[bot] merged 1 commit into
masterfrom
stack/tignear/feat/sv-generate-block-imports/support-generate-block-imports-types-subroutines--c706ae8c

Conversation

@tignear

@tignear tignear commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1206.

A generate block that imports a type or subroutine under a name the module
binds to another declaration was rejected, because types, constant functions
and subroutine metadata were looked up module-wide by the plain name. This
gives the generate scope its own view of those tables instead.

  • Scope and Item record the names the block's imports bind
    (imports, filled by bind_import). BlockImports keeps the ones an
    inner declaration has not hidden again, and derives from them:
    • the type aliases the block sees: a name the blocks declare or import
      hides the module's type of that name, and an imported t denotes
      p::t's type;
    • the installed constant-function table, with f aliased to p::f,
      or hidden when the block's f is no constant function (a DPI-C
      import). Calls in the bodies of the functions it hides are renamed
      to the outer binding, so a module function still calls the f it
      was declared with.
  • The elaborator evaluates a block's declarations, conditions and nested
    blocks with that view, and resolves a generate condition's call to the
    qualified declaration ahead of a module function of the same name.
  • Item::dimensions carries the block's type aliases (casts, $bits(t)),
    next to the function, formal-name and shape aliases it already had.
    The signal, process, instance, function and subroutine collectors use
    the item's type aliases and install its constant functions.
  • The two "imports as another declaration than its module" rejections are
    removed.
  • resolve_imports also records a block import of the declaration the
    module sees when a block around it imports another declaration or
    declares the name, so the inner block no longer inherits the outer
    binding. This was already wrong for values since fix(sv): scope imports in generate blocks to their block #1184.
  • As a result, a generate-local declaration such as localparam T now
    also hides a module typedef T, so T n; under it is rejected instead
    of taking the outer type (IEEE 1800-2023 23.9).

Validation:

  • cargo fmt --all -- --check
  • cargo clippy -p celox-sv-analyzer -p celox-frontend-sv -p celox --all-targets --features celox/systemverilog,celox/sv-dpi --no-deps
  • cargo test -p celox-sv-analyzer -p celox-frontend-sv
  • cargo test -p celox --features systemverilog,sv-dpi (11465 passed, 0 failed)

New regressions: generate_block_imports_bind_their_own_subroutines,
generate_block_imports_bind_their_own_types,
nested_generate_block_imports_restore_the_module_binding and
generate_block_declarations_hide_outer_types in sv_packages.rs, and
generate_block_imports_bind_their_own_dpi_formals and
generate_block_dpi_imports_hide_outer_constant_functions in sv_dpi.rs.
Every case also uses the name outside the block, so the block-local binding
is taken rather than the module-wide alias for names only blocks use.

Unrelated defects found while testing are filed as #1435, #1436, #1437,
#1438 and #1461.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 99a00bde18

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +85 to +88
for (name, target) in self.bindings() {
if let Some(r#type) = aliases.get(target) {
visible.to_mut().insert(name.clone(), r#type.clone());
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove type aliases hidden by non-type imports

When a module sees a typedef such as q::T but a generate block imports a value or subroutine p::T, this loop leaves the module's T entry untouched because p::T is absent from the alias table. The block import still owns that name, so declarations such as T n must not resolve to the outer typedef; retaining it can silently give the declaration the wrong type instead of rejecting the non-type name. Remove the simple-name alias whenever an effective block import targets a non-type declaration.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 80ea43a: when a block import binds a name to something that is not a type, BlockImports::type_aliases now removes the module's type alias of that name, so a block declaration T n; can no longer take the outer typedef. With a module typedef q::T and a block import of localparam int T from p, T n; in the block is rejected, and assign a = T; there still reads p::T. Both are covered in generate_block_imports_bind_their_own_types.

Comment on lines +98 to +103
let rebound: Vec<_> = self
.bindings()
.filter(|(_, target)| installed.contains(target))
.collect();
if rebound.is_empty() {
return None;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Hide outer constant functions for every effective import

If a module has a constant function f and a generate block imports a same-named declaration that is not in the installed constant-function table—most notably a DPI function, or a function whose table has only a global conversion error—this filter drops the binding entirely and the early return leaves the module's f installed. A block-local parameter or width expression calling f can therefore execute the outer function and produce a wrong elaborated value instead of binding to or rejecting the imported declaration. The outer binding must be hidden even when the target cannot be installed as a constant function.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 80ea43a: install_constant_functions now also covers block imports whose target is not in the constant-function table, as long as the name hides an outer constant function. That outer function is renamed away and nothing is installed under the name. A block localparam int Y = f(1); with a DPI-C p::f is now rejected ("user constant function call") instead of running the module's f, whether the module declares f or imports q::f. For a target with only a module-wide conversion error, a call of the hidden name reports that error. Covered by generate_block_dpi_imports_hide_outer_constant_functions in sv_dpi.rs.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T01:37:40.250620Z b78b0f5 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@tignear
tignear force-pushed the stack/tignear/feat/sv-generate-block-imports/support-generate-block-imports-types-subroutines--c706ae8c branch from 99a00bd to 80ea43a Compare October 17, 2026 22:48
@tignear

tignear commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Revision history

# Type Changes Reason Date
1 initial 99a00bd 2026-10-17 22:48 UTC
2 content 99a00bd → 80ea43a Address review: block imports of non-types hide outer type aliases; block imports of non-constant functions hide outer constant functions 2026-10-17 22:48 UTC
3 content 80ea43a → cd8ca9c Address review: a nested block import of the module's declaration hides an enclosing block's binding 2026-10-17 23:35 UTC
4 content cd8ca9c → ac7255d 2026-10-18 00:56 UTC
5 content ac7255d → b78b0f5 Address review: generate-local declarations hide outer type aliases 2026-10-18 01:26 UTC

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 80ea43a5f3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// The declarations and nested blocks are elaborated with the types and
// constant functions those imports bind.
let _constant_functions = scope.block_imports().install_constant_functions();
let aliases = scope.block_imports().type_aliases(self.module_aliases);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear inherited overrides when an inner import restores the module binding

When a module imports q::t, an outer generate block imports p::t, and a nested block imports q::t again, resolve_imports omits the nested binding because it matches the module-level target. The nested Scope is cloned from its parent, however, so this lookup still sees the inherited p::t entry and declarations such as t n use the outer block's type (for example, 4 bits instead of 8). Applying a nested import that returns to the module binding must remove the inherited override rather than leaving it active.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in cd8ca9c. The bug was in resolve_imports: it skipped an inner block's import whenever that import matched the module's binding. It now records the import when an enclosing block imports another declaration or declares the name, so the inner block no longer inherits the outer override. This was already wrong for plain values since #1184 (an inner import q::* under an outer import p::* read p::X), and also when an outer block declared localparam X and an inner block did import q::X. nested_generate_block_imports_restore_the_module_binding covers values, types, constant and runtime calls, an alternating p/q/p chain, and the outer-declaration case. All five cases fail without the fix.

@mergify

mergify Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Queued — the merge queue status continues in this comment ↓.

@tignear
tignear force-pushed the stack/tignear/feat/sv-generate-block-imports/support-generate-block-imports-types-subroutines--c706ae8c branch 2 times, most recently from cd8ca9c to ac7255d Compare October 18, 2026 00:57

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ac7255d9ee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

fn bindings(self) -> impl Iterator<Item = (&'s String, &'s String)> {
self.imports
.iter()
.filter(move |(name, target)| self.names.get(*name) == Some(*target))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove type aliases hidden by local declarations

When a generate-local value declaration hides an inherited imported type, this filter drops the import from bindings() but leaves the module's simple-name type alias visible. For example, if the module imports q::T, an outer generate block imports and uses p::T, a child declares localparam T, and a grandchild declares T n, the newly accepted design lowers n as q::T; the local parameter should instead hide both outer type bindings, making T n invalid. Preserve the declaration's shadowing information so type_aliases removes T rather than falling back to the module alias.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b78b0f5. BlockImports::type_aliases now uses the generate scope's shadowed set: any name the enclosing blocks declare or import hides the module's type alias of that name, and imported types are then re-added under their name. With the import chain you describe, a child localparam T makes a grandchild's T n; invalid, and it is now rejected. The same was true without any import: a module typedef T was never hidden by a generate-local localparam T or logic T, so that case is fixed too. The lookup walks the smaller of the alias table and the shadowed set. Covered by generate_block_declarations_hide_outer_types, which fails without the fix.

…t differ from the module's

Closes #1206.

A generate block that imports a type or subroutine under a name the module
binds to another declaration was rejected, because types, constant functions
and subroutine metadata were looked up module-wide by the plain name. This
gives the generate scope its own view of those tables instead.

- `Scope` and `Item` record the names the block's imports bind
  (`imports`, filled by `bind_import`). `BlockImports` keeps the ones an
  inner declaration has not hidden again, and derives from them:
  - the type aliases the block sees: a name the blocks declare or import
    hides the module's type of that name, and an imported `t` denotes
    `p::t`'s type;
  - the installed constant-function table, with `f` aliased to `p::f`,
    or hidden when the block's `f` is no constant function (a DPI-C
    import). Calls in the bodies of the functions it hides are renamed
    to the outer binding, so a module function still calls the `f` it
    was declared with.
- The elaborator evaluates a block's declarations, conditions and nested
  blocks with that view, and resolves a generate condition's call to the
  qualified declaration ahead of a module function of the same name.
- `Item::dimensions` carries the block's type aliases (casts, `$bits(t)`),
  next to the function, formal-name and shape aliases it already had.
  The signal, process, instance, function and subroutine collectors use
  the item's type aliases and install its constant functions.
- The two "imports as another declaration than its module" rejections are
  removed.
- `resolve_imports` also records a block import of the declaration the
  module sees when a block around it imports another declaration or
  declares the name, so the inner block no longer inherits the outer
  binding. This was already wrong for values since #1184.
- As a result, a generate-local declaration such as `localparam T` now
  also hides a module typedef `T`, so `T n;` under it is rejected instead
  of taking the outer type (IEEE 1800-2023 23.9).

Validation:

- `cargo fmt --all -- --check`
- `cargo clippy -p celox-sv-analyzer -p celox-frontend-sv -p celox --all-targets --features celox/systemverilog,celox/sv-dpi --no-deps`
- `cargo test -p celox-sv-analyzer -p celox-frontend-sv`
- `cargo test -p celox --features systemverilog,sv-dpi` (11465 passed, 0 failed)

New regressions: `generate_block_imports_bind_their_own_subroutines`,
`generate_block_imports_bind_their_own_types`,
`nested_generate_block_imports_restore_the_module_binding` and
`generate_block_declarations_hide_outer_types` in `sv_packages.rs`, and
`generate_block_imports_bind_their_own_dpi_formals` and
`generate_block_dpi_imports_hide_outer_constant_functions` in `sv_dpi.rs`.
Every case also uses the name outside the block, so the block-local binding
is taken rather than the module-wide alias for names only blocks use.

Unrelated defects found while testing are filed as #1435, #1436, #1437,
#1438 and #1461.

Change-Id: Ic706ae8c0a61ae10d0a6e8afa53fc99b83d759d2
@tignear
tignear force-pushed the stack/tignear/feat/sv-generate-block-imports/support-generate-block-imports-types-subroutines--c706ae8c branch from ac7255d to b78b0f5 Compare October 18, 2026 01:26
@tignear tignear added the merge-ready Request merging through Mergify after required checks label Oct 18, 2026
@mergify mergify Bot added the queued Mergify queue entry label Oct 18, 2026
@mergify

mergify Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

This pull request spent 52 minutes 12 seconds in the queue, including 49 minutes 18 seconds running CI.

Required conditions to merge
  • check-success = Conventional Commit title
  • check-success = Queue validation
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Lint & Format
    • check-neutral = @github-actions/Lint & Format
    • check-skipped = @github-actions/Lint & Format
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-neutral = @github-actions/NAPI Build (Linux ARM64 GNU)
    • check-skipped = @github-actions/NAPI Build (Linux ARM64 GNU)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/NAPI Build (Windows)
    • check-neutral = @github-actions/NAPI Build (Windows)
    • check-skipped = @github-actions/NAPI Build (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Rust Test & NAPI Build
    • check-neutral = @github-actions/Rust Test & NAPI Build
    • check-skipped = @github-actions/Rust Test & NAPI Build
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Ubuntu)
    • check-neutral = @github-actions/JS Test (Ubuntu)
    • check-skipped = @github-actions/JS Test (Ubuntu)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/JS Test (Windows)
    • check-neutral = @github-actions/JS Test (Windows)
    • check-skipped = @github-actions/JS Test (Windows)
  • any of [🛡 GitHub repository ruleset rule master]:
    • check-success = @github-actions/Conventional Commit title
    • check-neutral = @github-actions/Conventional Commit title
    • check-skipped = @github-actions/Conventional Commit title

@mergify
mergify Bot merged commit d4a029c into master Oct 18, 2026
30 checks passed
@mergify
mergify Bot deleted the stack/tignear/feat/sv-generate-block-imports/support-generate-block-imports-types-subroutines--c706ae8c branch October 18, 2026 02:41
@mergify mergify Bot removed the queued Mergify queue entry label Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready Request merging through Mergify after required checks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(sv): support generate-block imports of types and subroutines that differ from the module's

1 participant