Skip to content

Refactor imported class construction#6265

Open
TheHypnoo wants to merge 3 commits into
mainfrom
refactor/imported-class-construction
Open

Refactor imported class construction#6265
TheHypnoo wants to merge 3 commits into
mainfrom
refactor/imported-class-construction

Conversation

@TheHypnoo

@TheHypnoo TheHypnoo commented Jul 10, 2026

Copy link
Copy Markdown
Member

Summary

  • centralize ImportedClass construction behind one helper
  • preserve route-specific source prefixes and alias decisions
  • add multi-module characterization coverage for imported-class metadata

Validation

  • cargo fmt --all
  • CARGO_INCREMENTAL=0 cargo check -p perry-codegen
  • CARGO_INCREMENTAL=0 cargo check -p perry --no-default-features --features compile-cli
  • CARGO_INCREMENTAL=0 cargo test -p perry-codegen --lib (156 passed)

The new Perry integration test could not be linked locally because the workspace volume ran out of disk space.

Summary by CodeRabbit

  • Bug Fixes

    • Preserved imported class metadata when consuming classes via aliases, re-exports, and namespace (import * as ...) imports.
    • Improved correctness for constructors, instance fields (including setter-backed), inherited instance properties, static fields/methods (including computed static), and instanceof across module boundaries.
  • Tests

    • Added regression tests covering renamed re-exports (including aliased local imports) and namespace imports, verifying runtime behavior and metadata preservation.

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: b352420b-ecb9-468d-ae96-28aecddbe8b5

📥 Commits

Reviewing files that changed from the base of the PR and between 75afd16 and 34751cf.

📒 Files selected for processing (2)
  • crates/perry/src/commands/compile/run_pipeline.rs
  • crates/perry/tests/functional_batch2_regressions.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/perry/src/commands/compile/run_pipeline.rs

📝 Walkthrough

Walkthrough

The compiler centralizes ImportedClass construction across import and re-export paths, preserving class metadata and aliases. Regression tests validate inherited behavior, fields, methods, static members, setters, and instanceof through aliased and namespace imports.

Changes

Imported class metadata

Layer / File(s) Summary
Centralize ImportedClass construction
crates/perry/src/commands/compile/run_pipeline.rs
Adds imported_class_from_hir(...) to populate complete class metadata and uses it across namespace imports, re-exports, aliases, interface-driven pulls, and transitive class resolution.
Validate alias and namespace metadata
crates/perry/tests/functional_batch2_regressions.rs
Adds regression coverage for aliased and namespace imports, including inherited properties, methods, setters, static fields, computed values, and instanceof.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • PerryTS/perry#5171: Related to imported constructor metadata used by cross-module super dispatch.
  • PerryTS/perry#5556: Related to alias-aware imported class metadata and constructor dispatch.
  • PerryTS/perry#5874: Related to ImportedClass metadata propagation through imports and re-exports.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning It covers summary and validation, but omits required template sections like Changes, Related issue, Test plan, and Checklist. Add the missing template sections, especially a concrete Changes list, Related issue or n/a, a Test plan with commands/results, and the checklist items.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and matches the main change: centralizing imported class construction.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/imported-class-construction

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@crates/perry/src/commands/compile/run_pipeline.rs`:
- Line 2496: Preserve the namespace-visible alias when importing re-exported
classes. In both class-import branches around imported_class_from_hir, resolve
the class using the visible re-export key (such as PublicChild) and pass
Some(export_name.clone()) when it differs from class.name, otherwise None, so
generated metadata uses the exported alias.
- Line 3639: The transitive closure worklist currently uses a non-canonical
module prefix when importing parent classes, causing re-exported classes to
reference barrel modules. In the closure branch around imported_classes.push and
imported_class_from_hir, replace compute_module_prefix(&src_path) with
canonical_class_source_prefix(...) using the resolved class/source context,
matching the direct import paths.

In `@crates/perry/tests/functional_batch2_regressions.rs`:
- Around line 217-242: The regression test does not access the computed static
field declared on Child. In the main.ts test source, alongside the existing
LocalChild.plain assertion, add an assertion reading
LocalChild["not-a-static-global"] and verify it produces "computed".
- Around line 234-242: Update the regression fixture around the namespace import
test so it does not first import `PublicChild` as `LocalChild`; add a separate
namespace-only entry point or fixture that imports `* as barrel` and performs
the `barrel.PublicChild` construction/`instanceof` assertion independently. Keep
the existing named-import coverage separate, ensuring namespace metadata
registration is validated without prior registration by `LocalChild`.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2f42b10d-40b3-47a6-bd36-8252e0803136

📥 Commits

Reviewing files that changed from the base of the PR and between 7b8c737 and 75afd16.

📒 Files selected for processing (2)
  • crates/perry/src/commands/compile/run_pipeline.rs
  • crates/perry/tests/functional_batch2_regressions.rs

Comment thread crates/perry/src/commands/compile/run_pipeline.rs Outdated
.collect(),
source_class_id: Some(class.id),
});
imported_classes.push(imported_class_from_hir(class, class_prefix, alias));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate relevant symbols and surrounding context
rg -n "exported_classes|canonical_class_source_prefix|compute_module_prefix|imported_class_from_hir|closure import|imported_classes.push" crates/perry/src/commands/compile/run_pipeline.rs

# Show a focused slice around the reported line
sed -n '3560,3665p' crates/perry/src/commands/compile/run_pipeline.rs

# Find definitions/usages in the repo
rg -n "fn canonical_class_source_prefix|canonical_class_source_prefix\(|fn compute_module_prefix|compute_module_prefix\(|fn imported_class_from_hir|imported_class_from_hir\(" crates/perry/src

Repository: PerryTS/perry

Length of output: 12800


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Read the helper and nearby tests for canonical prefix selection
sed -n '90,150p' crates/perry/src/commands/compile/helpers.rs
sed -n '230,340p' crates/perry/src/commands/compile/helpers.rs

# Read the surrounding import-creation helpers in run_pipeline
sed -n '1,80p' crates/perry/src/commands/compile/run_pipeline.rs
sed -n '2470,2965p' crates/perry/src/commands/compile/run_pipeline.rs
sed -n '3048,3505p' crates/perry/src/commands/compile/run_pipeline.rs

Repository: PerryTS/perry

Length of output: 50369


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Find where ImportedClass.source_prefix is consumed in codegen
rg -n "source_prefix" crates/perry/src crates/perry_codegen/src

# Read the relevant ImportedClass definition and its consumers
sed -n '1,220p' crates/perry_codegen/src/lib.rs
sed -n '1,260p' crates/perry_codegen/src/imported_class.rs 2>/dev/null || true
sed -n '1,260p' crates/perry_codegen/src/*.rs 2>/dev/null | sed -n '1,260p'

Repository: PerryTS/perry

Length of output: 4342


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate every use of ImportedClass.source_prefix
rg -n "source_prefix" crates/perry/src crates/perry_codegen/src

# Show the ImportedClass definition if present
rg -n "struct ImportedClass|pub struct ImportedClass" crates/perry_codegen/src crates/perry/src

# Read the files that consume imported classes in codegen (narrowed by search hits)
rg -n "ImportedClass|imported_class_names|class_prefix|source_prefix" crates/perry_codegen/src crates/perry/src/commands/compile -g '*.rs'

Repository: PerryTS/perry

Length of output: 4342


🏁 Script executed:

#!/bin/bash
set -euo pipefail

sed -n '600,625p' crates/perry/src/commands/compile/run_pipeline.rs
sed -n '3608,3650p' crates/perry/src/commands/compile/run_pipeline.rs
sed -n '2482,2502p' crates/perry/src/commands/compile/run_pipeline.rs
sed -n '2726,2746p' crates/perry/src/commands/compile/run_pipeline.rs
sed -n '2912,2958p' crates/perry/src/commands/compile/run_pipeline.rs

Repository: PerryTS/perry

Length of output: 9583


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the surrounding comment and the exact closure-import block
sed -n '3520,3650p' crates/perry/src/commands/compile/run_pipeline.rs

# Find any helper or comment that explicitly says closure imports should canonicalize
rg -n "closure|canonical_class_source_prefix|barrel|re-export" crates/perry/src/commands/compile/run_pipeline.rs crates/perry/src/commands/compile/helpers.rs

# Find all places where imported_class_from_hir is called with compute_module_prefix vs canonical_class_source_prefix
rg -n "imported_class_from_hir\(class, (compute_module_prefix|canonical_class_source_prefix)" crates/perry/src/commands/compile/run_pipeline.rs

Repository: PerryTS/perry

Length of output: 18160


Use the canonical class prefix in the transitive closure path. The closure-worklist branch still stamps imported parents with compute_module_prefix(&src_path). If the lookup lands on a re-export/barrel entry, the emitted method/static symbols point at the barrel module instead of the defining module and can fail at link time. Use canonical_class_source_prefix(...) here, as in the direct import paths.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/perry/src/commands/compile/run_pipeline.rs` at line 3639, The
transitive closure worklist currently uses a non-canonical module prefix when
importing parent classes, causing re-exported classes to reference barrel
modules. In the closure branch around imported_classes.push and
imported_class_from_hir, replace compute_module_prefix(&src_path) with
canonical_class_source_prefix(...) using the resolved class/source context,
matching the direct import paths.

Comment thread crates/perry/tests/functional_batch2_regressions.rs Outdated
Comment thread crates/perry/tests/functional_batch2_regressions.rs Outdated
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.

2 participants