Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_ts_dispose_resources is not correctly invoked for using keyword #9673

Closed
jtbandes opened this issue Oct 23, 2024 · 1 comment · Fixed by #9776
Closed

_ts_dispose_resources is not correctly invoked for using keyword #9673

jtbandes opened this issue Oct 23, 2024 · 1 comment · Fixed by #9776
Assignees
Labels
Milestone

Comments

@jtbandes
Copy link

jtbandes commented Oct 23, 2024

Describe the bug

The Symbol.dispose function is not invoked when using the using keyword.

The output includes

finally{
    _ts_dispose_resources(env);
}

However, this function is defined as

function _ts_dispose_resources(SuppressedError1) {
    return function(env) {
        // ...
    };
}

So calling _ts_dispose_resources(env); does not actually invoke the dispose logic.

The function does seem to be invoked here, so something else must be going wrong:

Input code

using x = {
  [Symbol.dispose]() {
    console.log("disposed")
  }
};

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false,
      "explicitResourceManagement":true
    },
    "target": "es2022",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "commonjs"
  },
  "minify": false,
  "isModule": true
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.7.39&code=H4sIAAAAAAAAAystzsxLV6hQsFWo5lJQiA6uzE3Kz9FLySwuyC9OjdXQBAsrKCTn5xXn56Tq5eSnayhBZVOUNIFytVy11gBoxdWzRQAAAA%3D%3D&config=H4sIAAAAAAAAA1VPOw6DMAzdOQXyzFAx9g4svYGVGhSUn2IjgRB3bxIgbbe8f7w3bQszK3i2e3omEDAyxYoTw5sTXBMDpCyyijoIdLc6c5ZGNEwd0BqMVlpexH6JigZ0OJElJ%2FCUuFAJHWcWBONEUnq5f%2FT91QnGe6baeXJWOz1uv79S3oZIzP%2FGbEU3mZo%2FF5trFax%2FL0W8rpUtZJTbrHczw9d5L9Z20Dzc8XLM8QEIU%2BriPgEAAA%3D%3D

SWC Info output

No response

Expected behavior

console.log("disposed") should run

Actual behavior

No log message

Version

1.7.39

Additional context

See also #9576

@jtbandes jtbandes added the C-bug label Oct 23, 2024
@kdy1 kdy1 self-assigned this Oct 24, 2024
@kdy1 kdy1 added this to the Planned milestone Oct 24, 2024
@kdy1
Copy link
Member

kdy1 commented Dec 2, 2024

Investigation:

helpers/Marker (resolver for injected inline helpers) seems to be the cause.

struct Marker {
base: SyntaxContext,
decls: FxHashMap<JsWord, SyntaxContext>,
decl_ctxt: SyntaxContext,
}
impl VisitMut for Marker {
noop_visit_mut_type!();
fn visit_mut_fn_decl(&mut self, n: &mut FnDecl) {
let old_decl_ctxt = replace(
&mut self.decl_ctxt,
SyntaxContext::empty().apply_mark(Mark::new()),
);
let old_decls = self.decls.clone();
n.visit_mut_children_with(self);
self.decls = old_decls;
self.decl_ctxt = old_decl_ctxt;
}
fn visit_mut_fn_expr(&mut self, n: &mut FnExpr) {
let old_decl_ctxt = replace(
&mut self.decl_ctxt,
SyntaxContext::empty().apply_mark(Mark::new()),
);
let old_decls = self.decls.clone();
n.visit_mut_children_with(self);
self.decls = old_decls;
self.decl_ctxt = old_decl_ctxt;
}
fn visit_mut_ident(&mut self, i: &mut Ident) {
i.ctxt = self.decls.get(&i.sym).copied().unwrap_or(self.base);
}
fn visit_mut_member_prop(&mut self, p: &mut MemberProp) {
if let MemberProp::Computed(p) = p {
p.visit_mut_with(self);
}
}
fn visit_mut_param(&mut self, n: &mut Param) {
if let Pat::Ident(i) = &n.pat {
self.decls.insert(i.sym.clone(), self.decl_ctxt);
}
n.visit_mut_children_with(self);
}
fn visit_mut_prop_name(&mut self, n: &mut PropName) {
if let PropName::Computed(e) = n {
e.visit_mut_with(self);
}
}
fn visit_mut_super_prop(&mut self, p: &mut SuperProp) {
if let SuperProp::Computed(p) = p {
p.visit_mut_with(self);
}
}
fn visit_mut_var_declarator(&mut self, v: &mut VarDeclarator) {
if let Pat::Ident(i) = &mut v.name {
if &*i.sym == "id" || &*i.sym == "resource" {
i.ctxt = self.base;
self.decls.insert(i.sym.clone(), self.base);
return;
}
if !i.sym.starts_with("__") {
self.decls.insert(i.sym.clone(), self.decl_ctxt);
}
}
v.visit_mut_children_with(self);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

5 participants
@jtbandes @kdy1 and others