Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 58 additions & 10 deletions crates/perry-hir/src/lower_decl/body_stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,40 @@ pub fn lower_body_stmt(ctx: &mut LoweringContext, stmt: &ast::Stmt) -> Result<Ve
// class VALUE (`exports.C = C; new mod.C()`) can fill the
// synthesized `__perry_cap_<id>` ctor params. Static `new C()`
// sites still pass captures as trailing args directly.
if let Some(captured) = ctx.lookup_class_captures(&class.name) {
if !captured.is_empty() {
let captures: Vec<Expr> =
captured.iter().map(|id| Expr::LocalGet(*id)).collect();
result.push(Stmt::Expr(Expr::RegisterClassCaptures {
class_name: class.name.clone(),
captures,
}));
}
let captured_exprs: Vec<Expr> = ctx
.lookup_class_captures(&class.name)
.map(|ids| ids.iter().map(|id| Expr::LocalGet(*id)).collect())
.unwrap_or_default();
if !captured_exprs.is_empty() {
result.push(Stmt::Expr(Expr::RegisterClassCaptures {
class_name: class.name.clone(),
captures: captured_exprs.clone(),
}));
}
// #6465: a capturing class DECLARATION in a factory needs
// per-evaluation semantics, same as a class expression.
// `RegisterClassCaptures` above is a class-id-keyed,
// last-wins registry — when the enclosing function runs more
// than once (effect's `makeException`, called once per
// exception kind), every escaped class value replayed the
// LAST call's captures: all of effect's core error classes
// reported `_tag` "TimeoutException", masking the real error
// in any `Cause.pretty` output and breaking every
// `catchTag`-style dispatch. Bind the declared name to a
// `ClassExprFresh` value (the #1772/#1787 machinery class
// EXPRESSIONS already use) so each evaluation carries its own
// captured environment on its own heap class object. Scoped
// to capture-only classes: one with static state keeps the
// shared-template path, whose interleaved static-init
// statements below target the template by name. Prototype
// identity is still shared per template — the remaining gap
// tracked on #6465.
let has_static_state = !class.static_fields.is_empty()
|| class
.static_methods
.iter()
.any(|m| m.name.starts_with("__perry_static_init_"));
let fresh_binding = !captured_exprs.is_empty() && !has_static_state;
// Static field initializers + static blocks for a
// function-nested class. The module-level path
// (`lower/stmt.rs`) emits these into `module.init`; here they
Expand All @@ -335,7 +359,31 @@ pub fn lower_body_stmt(ctx: &mut LoweringContext, stmt: &ast::Stmt) -> Result<Ve
&class.static_fields,
&class.static_methods,
));
let template_name = class.name.clone();
ctx.pending_classes.push(class);
// #6465 (see `fresh_binding` above): bind the declared name to
// a per-evaluation heap class object. The local shadows the
// class-registry fallback for every in-scope read — including
// the factory's `return C` — so the escaped value carries THIS
// evaluation's captures instead of the registry's last-wins
// snapshot. `new C()` sites that statically resolve the
// template still pass the live captures as trailing args, so
// in-factory construction is per-evaluation on both paths.
if fresh_binding {
let class_local = ctx.define_local(class_name.clone(), Type::Any);
result.push(Stmt::Let {
id: class_local,
name: class_name.clone(),
ty: Type::Any,
init: Some(Expr::ClassExprFresh {
template: template_name,
named_statics: Vec::new(),
symbol_statics: Vec::new(),
captured_args: captured_exprs,
}),
mutable: false,
});
}
Comment on lines +362 to +386

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
rg -n "^version" Cargo.toml
rg -n "Current Version" CLAUDE.md
head -20 CHANGELOG.md

Repository: PerryTS/perry

Length of output: 2473


🏁 Script executed:

#!/bin/bash
git diff --name-only --cached
git diff --name-only
git status --short

Repository: PerryTS/perry

Length of output: 151


Sync the release metadata too — Cargo.toml and CHANGELOG.md are already at 0.5.1258, but CLAUDE.md still says 0.5.1239. If this lands on main from a maintainer, bump that line to match.

🤖 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-hir/src/lower_decl/body_stmt.rs` around lines 362 - 386, Update
the release version recorded in CLAUDE.md from 0.5.1239 to 0.5.1258 so it
matches the current version in Cargo.toml and CHANGELOG.md.

Source: Coding guidelines

// #5251 follow-up — a function-nested `class X { … }` whose
// name collides with an OUTER same-named local must SHADOW
// that local within this scope, exactly as a nested
Expand All @@ -354,7 +402,7 @@ pub fn lower_body_stmt(ctx: &mut LoweringContext, stmt: &ast::Stmt) -> Result<Ve
// `local_class_aliases`) so the in-scope read resolves to the
// class. Gated on a pre-existing outer binding so working
// packages (no collision) are byte-for-byte unaffected.
if ctx.lookup_local(&class_name).is_some() {
if !fresh_binding && ctx.lookup_local(&class_name).is_some() {
let class_local = ctx.define_local(class_name.clone(), Type::Any);
result.push(Stmt::Let {
id: class_local,
Expand Down
Loading