Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(refactoring): a write that may not run does not cover a later read in the span (#3589)

Yang Bai968c21f48399
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py b/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py
index 8a2508055..16b30ac80 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/defuse.py
@@ -51,6 +51,8 @@ class Definition:
imports: bool = False
#: The write may not happen (``Occurrence.may``).
may: bool = False
+ #: The declaration gives the name no value (``Occurrence.valueless``).
+ valueless: bool = False
@dataclass
@@ -117,6 +119,7 @@ def compute_def_use(
declares=occ.declares,
imports=occ.imports,
may=occ.may,
+ valueless=occ.valueless,
)
counter += 1
bdu.defs.append(definition)
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py
index ca3c908db..e813d6a65 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py
@@ -82,6 +82,13 @@ class Occurrence:
#: can still be the one a later read sees, so reaching definitions lets it
#: add to what reaches and hide nothing.
may: bool = False
+ #: A declaration with no initializer, in a language where that leaves the
+ #: name without a value to read (Java's ``List<T> xs;``, TypeScript's
+ #: ``let x: T;``): the compiler rejects a read it cannot prove assigned. It
+ #: still declares the name, but it is no value a later read can be handed.
+ #: Never set for Go, whose zero value is a value, nor for a JavaScript
+ #: ``let x;``, which holds ``undefined``.
+ valueless: bool = False
@dataclass(frozen=True)
@@ -259,21 +266,28 @@ class BaseDefUseDialect:
)
def _declare(
- self, defs: list[Occurrence], start: int, declarator: Node, *, binds: bool = True
+ self,
+ defs: list[Occurrence],
+ start: int,
+ declarator: Node,
+ *,
+ binds: bool = True,
+ valueless: bool = False,
) -> None:
"""Mark ``defs[start:]`` as names *declarator* declares.
``declared_at`` is set where the declarator ends on the def's own line;
``declares`` is set on every one of them when *binds* (False for a
- declaration that creates no block-scoped binding, a TS/JS ``var``).
- Call it right after the declarator's targets are collected and before
- its initializer is walked, so a write nested in the initializer is not
- taken for a declaration.
+ declaration that creates no block-scoped binding, a TS/JS ``var``);
+ ``valueless`` when the declarator has no initializer and the language
+ gives such a name no value. Call it right after the declarator's
+ targets are collected and before its initializer is walked, so a write
+ nested in the initializer is not taken for a declaration.
"""
end_row, end_col = declarator.end_point
for i in range(start, len(defs)):
at = end_col if defs[i].line == end_row + 1 else None
- defs[i] = replace(defs[i], declared_at=at, declares=binds)
+ defs[i] = replace(defs[i], declared_at=at, declares=binds, valueless=valueless)
@staticmethod
def _loop_scoped(defs: list[Occurrence], start: int) -> None:
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py
index 738c57eba..c7d650044 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py
@@ -81,6 +81,14 @@ _SCOPE_BOUNDARIES = frozenset({"lambda_expression"})
_CALLEE_NAME_KINDS = frozenset({"identifier", "qualified_identifier", "template_function"})
+def _is_static(declaration: Node) -> bool:
+ """Whether a ``declaration`` carries the ``static`` storage class."""
+ return any(
+ child.type == "storage_class_specifier" and child.text == b"static"
+ for child in declaration.children
+ )
+
+
def _declarator_name(fn_node: Node) -> Node | None:
"""The innermost node of a definition's ``declarator`` chain (its name),
None for a lambda, which has no declarator."""
@@ -311,10 +319,11 @@ class CppDefUseDialect(BaseDefUseDialect):
self._process(child.child_by_field_name("value"), defs, uses)
elif child.type in self.identifier_kinds or child.type in _DECLARATOR_WRAPPERS:
# ``int x;`` / ``int* p;`` — a binding with no initialiser.
+ # A ``static`` one is zero-initialised, which is a value.
binder = self._binder_identifier(child)
if binder is not None:
defs.append(self._occ(binder))
- self._declare(defs, start, child)
+ self._declare(defs, start, child, valueless=not _is_static(node))
return
if t == _CALL:
self.collect_reads(node, uses)
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
index 34c7ce276..4423af30f 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
@@ -299,10 +299,11 @@ class CSharpDefUseDialect(BaseDefUseDialect):
start = len(defs)
name_node = child.child_by_field_name("name")
self._targets(name_node, defs, uses)
- self._declare(defs, start, child)
- for sub in child.named_children:
- if sub != name_node:
- self._process(sub, defs, uses)
+ rest = [sub for sub in child.named_children if sub != name_node]
+ # ``int x;``: definite assignment makes a read before a write an error.
+ self._declare(defs, start, child, valueless=not rest)
+ for sub in rest:
+ self._process(sub, defs, uses)
def _invocation(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:
fn = _callee(node)
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py
index 0a2eac331..2ed0ff73c 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py
@@ -221,8 +221,10 @@ class JavaDefUseDialect(BaseDefUseDialect):
continue
start = len(defs)
self._targets(declarator.child_by_field_name("name"), defs, uses)
- self._declare(defs, start, declarator)
- self._process(declarator.child_by_field_name("value"), defs, uses)
+ value = declarator.child_by_field_name("value")
+ # ``List<T> xs;``: a local must be assigned before it is read.
+ self._declare(defs, start, declarator, valueless=value is None)
+ self._process(value, defs, uses)
return
if t == _METHOD_INVOCATION: # the ``name`` is a method, not a variable
self._process(node.child_by_field_name("object"), defs, uses)
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py
index 7b6c8147e..6718078f2 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py
@@ -223,8 +223,10 @@ class RustDefUseDialect(BaseDefUseDialect):
self._targets(node.child_by_field_name("pattern"), defs, uses)
# ``let`` binds a new name (shadowing included) from the end of
# the statement, so its own initializer reads the outer one.
- self._declare(defs, start, node)
- self._process(node.child_by_field_name("value"), defs, uses)
+ value = node.child_by_field_name("value")
+ # ``let x;`` is assigned later: the borrow checker proves it.
+ self._declare(defs, start, node, valueless=value is None)
+ self._process(value, defs, uses)
# ``let ... else { diverge }``: the arm runs only on mismatch, so
# anything it writes is a may-def.
alternative = node.child_by_field_name("alternative")
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
index 1ed3c646d..2ee60ffa2 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
@@ -108,6 +108,34 @@ class _TsDeclaredTypes(DeclaredTypes):
return f"({typ}) | undefined" if len(arms) == 1 and "=>" in typ else f"{typ} | undefined"
+#: Types under which TypeScript lets a ``let`` be read before it is assigned.
+_ADMITS_UNDEFINED = frozenset({b"undefined", b"any", b"unknown", b"void"})
+_TYPE_WRAPPERS = frozenset({"type_annotation", "union_type", "parenthesized_type"})
+
+
+def _admits_undefined(type_node: Node) -> bool:
+ if type_node.type in _TYPE_WRAPPERS:
+ return any(_admits_undefined(child) for child in type_node.named_children)
+ return type_node.text in _ADMITS_UNDEFINED
+
+
+def _unreadable_until_assigned(declarator: Node) -> bool:
+ """Whether *declarator* leaves its name without a value to read:
+ ``let x: string;``, which TypeScript rejects a read of until an assignment.
+
+ Every other bare declaration holds ``undefined``, a value like any other:
+ one with no annotation (all of JavaScript, an implicit ``any``), one whose
+ type admits ``undefined`` (``let x: T | undefined;``), and one with a
+ definite-assignment ``!``. A type alias is taken at its word: whether
+ ``let x: Maybe;`` admits ``undefined`` is not visible here."""
+ if declarator.child_by_field_name("value") is not None:
+ return False
+ annotation = declarator.child_by_field_name("type")
+ if annotation is None or any(child.type == "!" for child in declarator.children):
+ return False
+ return not _admits_undefined(annotation)
+
+
class TsJsDefUseDialect(BaseDefUseDialect):
language = "typescript"
member_access_kinds = frozenset({"member_expression"})
@@ -248,7 +276,13 @@ class TsJsDefUseDialect(BaseDefUseDialect):
continue
start = len(defs)
self._targets(declarator.child_by_field_name("name"), defs, uses)
- self._declare(defs, start, declarator, binds=t != _VAR_DECL)
+ self._declare(
+ defs,
+ start,
+ declarator,
+ binds=t != _VAR_DECL,
+ valueless=_unreadable_until_assigned(declarator),
+ )
self._process(declarator.child_by_field_name("value"), defs, uses)
return
if t == _CATCH: # a nested scope's handler, walked whole
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
index e90f48580..4861e49c8 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
@@ -190,6 +190,7 @@ def find_extractions(
def_lines, use_lines = _var_lines(analysis.def_use)
import_reads = _import_reads(analysis)
declared_first = _declared_before_read(analysis.def_use)
+ valueless = _valueless_lines(analysis.def_use)
# Built on the first span that clears the cheap gates.
reads: dict[str, list[tuple[int, frozenset[int]]]] | None = None
hoisted = _hoisted_bindings(def_lines, use_lines)
@@ -251,7 +252,7 @@ def find_extractions(
if reads is None:
reads = _reads_observing(analysis, def_lines)
params, returns = _infer_in_out(
- def_lines, use_lines, s, e, declared_first, reads=reads
+ def_lines, use_lines, s, e, declared_first, reads=reads, valueless=valueless
)
if len(params) > _MAX_PARAMS or len(returns) > _MAX_RETURNS:
continue
@@ -642,6 +643,18 @@ def _declared_before_read(def_use: FunctionDefUse) -> dict[str, frozenset[int]]:
return {var: frozenset(lines) for var, lines in declared.items()}
+def _valueless_lines(def_use: FunctionDefUse) -> dict[str, frozenset[int]]:
+ """Per variable, the lines whose only definitions of it are declarations
+ that give it no value (``Definition.valueless``): Java's ``List<T> xs;``,
+ TypeScript's ``let x: T;``. A line that also assigns the name
+ (``int x; x = 1;``) is not one: what a read sees from it is the value."""
+ bare: dict[str, set[int]] = defaultdict(set)
+ valued: dict[str, set[int]] = defaultdict(set)
+ for d in def_use.definitions:
+ (bare if d.valueless else valued)[d.var].add(d.line)
+ return {var: frozenset(at - valued[var]) for var, at in bare.items()}
+
+
def _reads_observing(
analysis: FunctionAnalysis, def_lines: dict[str, list[int]]
) -> dict[str, list[tuple[int, frozenset[int]]]]:
@@ -702,6 +715,7 @@ def _infer_in_out(
declared_first: dict[str, frozenset[int]],
*,
reads: dict[str, list[tuple[int, frozenset[int]]]],
+ valueless: dict[str, frozenset[int]] | None = None,
) -> tuple[tuple[str, ...], tuple[str, ...]]:
"""Infer IN (parameters) and OUT (return) variables for span ``[s, e]``.
@@ -709,12 +723,18 @@ def _infer_in_out(
an in-span write, and which has a definition before the span (a parameter or
an earlier assignment). A write on the same line as that read precedes it
only where *declared_first* (:func:`_declared_before_read`) says the line
- declares the name first. OUT: a variable written in the span with a read
- after it that may observe one of those writes (*reads*,
- :func:`_reads_observing`). A read before the span that a write in it
- reaches around a loop is not an OUT: :func:`_loop_carry_free` refuses
- those spans. Both line lists are sorted (:func:`_var_lines`), so the
- span tests are bisects.
+ declares the name first. An earlier in-span write covers a later read only
+ on the paths that run it, so a variable is also IN when some in-span read
+ of it may still observe a definition from before the span (*reads*,
+ :func:`_reads_observing`): the write sits under an ``if``, in a loop
+ body, or in an arm. A declaration that gives the name no value
+ (*valueless*, :func:`_valueless_lines`) is not such a definition: the
+ compiler has proved the read assigned where the may-analysis cannot, and
+ a call that passed the name would not compile. OUT: a variable written in
+ the span with a read after it that may observe one of those writes
+ (*reads* again). A read before the span that a write in it reaches around
+ a loop is not an OUT: :func:`_loop_carry_free` refuses those spans. Both
+ line lists are sorted (:func:`_var_lines`), so the span tests are bisects.
"""
params: list[str] = []
returns: list[str] = []
@@ -726,8 +746,15 @@ def _infer_in_out(
if u_lo < u_hi and d_lo > 0:
first_use = ul[u_lo]
- declared = first_use in declared_first.get(var, ())
- if not declared and not (d_lo < d_hi and dl[d_lo] < first_use):
+ declaring = declared_first.get(var, ())
+ bare = valueless.get(var, ()) if valueless else ()
+ uncovered = first_use not in declaring and not (d_lo < d_hi and dl[d_lo] < first_use)
+ if uncovered or any(
+ s <= ln <= e
+ and ln not in declaring
+ and any(d < s and d not in bare for d in seen)
+ for ln, seen in reads.get(var, ())
+ ):
params.append(var)
if d_lo < d_hi and any(
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/stages.py b/packages/core/src/repowise/core/analysis/health/dataflow/stages.py
index f308915f1..683df5969 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/stages.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/stages.py
@@ -79,6 +79,7 @@ from .slice import (
_scan_for,
_stmt_assigns,
_unwrap_container,
+ _valueless_lines,
_var_lines,
)
from .span import exiting_guard, is_comment, section_heads
@@ -320,6 +321,7 @@ class _FunctionFacts:
def_use = analysis.def_use
self.def_lines, self.use_lines = _var_lines(def_use)
self.declared_first = _declared_before_read(def_use)
+ self.valueless = _valueless_lines(def_use)
self.hoisted = _hoisted_bindings(self.def_lines, self.use_lines)
self.decl_lines = _declaration_lines(def_use)
self.shared = _closure_state(def_use, self.def_lines, self.use_lines)
@@ -354,6 +356,7 @@ class _Cutter:
self.max_returns = max_returns
self.def_lines, self.use_lines = facts.def_lines, facts.use_lines
self.declared_first = facts.declared_first
+ self.valueless = facts.valueless
self.hoisted = facts.hoisted
self.decl_lines = facts.decl_lines
self.shared = facts.shared
@@ -432,7 +435,13 @@ class _Cutter:
if any(s <= ln <= e for v in self.rereads for ln in self.def_lines.get(v, ())):
return None
params, returns = _infer_in_out(
- self.def_lines, self.use_lines, s, e, self.declared_first, reads=self.reads
+ self.def_lines,
+ self.use_lines,
+ s,
+ e,
+ self.declared_first,
+ reads=self.reads,
+ valueless=self.valueless,
)
returns = self._own_bindings_dropped(span, s, e, returns)
if self.carried:
diff --git a/tests/fixtures/refactoring_corpus/golden_extractions.json b/tests/fixtures/refactoring_corpus/golden_extractions.json
index 9acf3100f..d72513a6a 100644
--- a/tests/fixtures/refactoring_corpus/golden_extractions.json
+++ b/tests/fixtures/refactoring_corpus/golden_extractions.json
@@ -737,6 +737,7 @@
"end_line": 250,
"params": [
"block",
+ "expect",
"limit",
"names"
],
@@ -750,6 +751,7 @@
"params": [
"block",
"depth",
+ "expect",
"limit",
"names"
],
diff --git a/tests/unit/health/test_extract_method_conditional_input.py b/tests/unit/health/test_extract_method_conditional_input.py
new file mode 100644
index 000000000..463409699
--- /dev/null
+++ b/tests/unit/health/test_extract_method_conditional_input.py
@@ -0,0 +1,646 @@
+"""A span that writes a name on one path and reads it afterwards takes the name in (#3523).
+
+The input side of ``_infer_in_out`` went by line order: a write earlier in
+the span covered a later read whether or not it ran. With the write under an
+``if``, or in a loop whose body may not run, the read can still see the value
+from before the span, and the helper was not given it.
+"""
+
+from __future__ import annotations
+
+import textwrap
+
+import pytest
+
+from repowise.core.analysis.health.complexity.languages import get_language_map
+from repowise.core.analysis.health.dataflow import analyze_file, find_extractions
+from repowise.core.analysis.health.dataflow.stages import find_stages
+
+
+def _spans(src: str, language: str = "python", ext: str = "py"):
+ result = analyze_file(
+ f"m.{ext}", language, textwrap.dedent(src).lstrip("\n").encode(), flagged_only=False
+ )
+ if result.stats.functions_seen == 0:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ fn = result.functions[0]
+ return {(x.start_line, x.end_line): x for x in find_extractions(fn, get_language_map(language))}
+
+
+_IF = """
+def f(k, xs):
+ best = xs[0]
+ n = len(xs)
+ log(n)
+ if k == 1:
+ best = {WRITE}
+ else:
+ log(1)
+ log(2)
+ if best > 3:
+ log(best)
+ else:
+ log(0)
+ done()
+ return n
+"""
+
+
+def test_a_write_under_an_if_does_not_cover_the_read_after_it():
+ spans = _spans(_IF.replace("{WRITE}", "7"))
+ assert (5, 13) in spans and (5, 14) in spans
+ for key in ((5, 13), (5, 14)):
+ assert "best" in spans[key].params, (key, spans[key].params)
+
+
+def test_a_write_on_every_path_still_covers_the_read():
+ """The control: both arms assign, so the value from before is never read."""
+ src = _IF.replace("{WRITE}", "7").replace("log(1)", "best = 0")
+ spans = _spans(src)
+ assert (5, 13) in spans
+ assert "best" not in spans[(5, 13)].params
+
+
+def test_a_span_after_the_conditional_write_takes_it_as_before():
+ spans = _spans(_IF.replace("{WRITE}", "7"))
+ assert spans[(9, 13)].params == ("best",)
+
+
+_LOOP = """
+def route(rel_path, matches):
+ if not matches:
+ return None
+ end = 0
+ for m in matches:
+ end = m.end()
+ parts = rel_path[end:].split("/")
+ if len(parts) > 1 and parts[-1] == "route":
+ parts.pop()
+ raw = []
+ for part in parts:
+ raw.extend(split(part))
+ return "/" + "/".join(raw)
+"""
+
+
+def test_a_write_in_a_loop_that_may_not_run_does_not_cover_the_read_after_it():
+ spans = _spans(_LOOP)
+ holding = [x for (s, e), x in spans.items() if s == 5 and e >= 7]
+ assert holding, "expected a span from the loop on"
+ for x in holding:
+ assert "end" in x.params, (x.start_line, x.end_line, x.params)
+
+
+_JAVA = """
+class C {
+ Response challenge(Context context, String error, String field) {
+ Form form = context.form();
+ Session session = context.session();
+ form.setExecution(context.id());
+ audit(session);
+ trace(context);
+ if (session.hidden()) {
+ field = PASSWORD;
+ }
+ if (error != null) {
+ if (field != null) {
+ form.addError(field, error);
+ } else {
+ form.setError(error);
+ }
+ }
+ note(form);
+ flush(session);
+ return create(form);
+ }
+}
+"""
+
+
+def test_a_reassigned_parameter_is_passed_on_java():
+ spans = _spans(_JAVA, "java", "java")
+ holding = [x for (s, e), x in spans.items() if s <= 8 and e >= 16]
+ assert holding, "expected a span holding both ifs"
+ for x in holding:
+ assert "field" in x.params, (x.start_line, x.end_line, x.params)
+
+
+# -- what must stay as it was ---------------------------------------------------------
+
+
+def test_a_read_before_the_span_does_not_make_an_input():
+ """``best`` is read above the span and written in it before every read
+ there: the span does not need the value from before."""
+ src = """
+ def f(k, xs):
+ best = xs[0]
+ log(best)
+ n = len(xs)
+ best = n * 2
+ if k == 1:
+ log(best)
+ else:
+ log(n)
+ log(k)
+ done()
+ return n
+ """
+ spans = _spans(src)
+ holding = [x for (s, e), x in spans.items() if s == 5]
+ assert holding, "expected a span from the write on"
+ assert all("best" not in x.params for x in holding)
+
+
+def test_a_read_after_the_span_does_not_make_an_input():
+ """The span sits in one branch and writes ``best`` before it reads it.
+ The read below the ``if`` sees that write or the one from before, but it is
+ not the span's read: the span hands ``best`` back and takes nothing."""
+ src = """
+ def f(k, xs):
+ best = xs[0]
+ n = len(xs)
+ if k:
+ best = n * 2
+ if n > 3:
+ log(best)
+ else:
+ log(n)
+ log(k)
+ use(best)
+ done()
+ return n
+ """
+ spans = _spans(src)
+ assert (5, 10) in spans, sorted(spans)
+ assert "best" not in spans[(5, 10)].params
+ assert spans[(5, 10)].returns == ("best",)
+
+
+_GO_IF_INIT = """
+package m
+
+func f(c *C, t *T) error {
+ err := c.start()
+ t.note(err)
+ t.mark()
+ if err := c.reload(); err != nil {
+ t.fail(err)
+ }
+ if c.busy() {
+ t.wait()
+ } else {
+ t.pass()
+ }
+ t.done()
+ return nil
+}
+"""
+
+
+def test_a_name_declared_on_the_reads_own_line_is_not_an_input():
+ """Go's ``if err := f(); err != nil``: the read after the declaration sees
+ the new ``err``, though an earlier one is still in scope above the span."""
+ spans = _spans(_GO_IF_INIT, "go", "go")
+ holding = [x for (s, e), x in spans.items() if s == 7]
+ assert holding, "expected a span from the if on"
+ assert all("err" not in x.params for x in holding)
+
+
+def test_a_c_style_loop_variable_is_not_an_input():
+ src = """
+ function f(rows, out) {
+ let i = rows.length;
+ out.note(i);
+ out.mark();
+ for (let i = 0; i < 3; i += 1) {
+ if (rows[i]) {
+ out.add(rows[i]);
+ } else {
+ out.skip(i);
+ }
+ }
+ out.flush();
+ out.done();
+ return out;
+ }
+ """
+ spans = _spans(src, "typescript", "ts")
+ holding = [x for (s, e), x in spans.items() if s == 5]
+ assert holding, "expected a span from the loop on"
+ assert all("i" not in x.params for x in holding)
+
+
+_ARM = """
+fn pick(k: i32, xs: &[i32]) -> usize {
+ let mut best = xs[0];
+ let n = xs.len();
+ log(n);
+ trace(k);
+ match k {
+ 1 => {
+ best = 7;
+ }
+ _ => {}
+ }
+ if best > 3 {
+ log(best);
+ } else {
+ log(0);
+ }
+ done();
+ flush();
+ n
+}
+"""
+
+
+def test_a_write_in_an_arm_does_not_cover_the_read_after_it():
+ """The input side of #3451: the arm may not run."""
+ spans = _spans(_ARM, "rust", "rs")
+ holding = [x for (s, e), x in spans.items() if s <= 6 and e >= 16]
+ assert holding, "expected a span holding the match and the read"
+ for x in holding:
+ assert "best" in x.params, (x.start_line, x.end_line, x.params)
+
+
+# --- A declaration that gives the name no value -----------------------------
+#
+# The compiler has proved a later read is assigned on every path; the
+# may-analysis cannot see that through a ``do`` body or an arm, and would pass
+# a variable that holds nothing. Java refuses that call.
+
+_JAVA_DO = """
+class C {
+ void cleanup(Queue<Integer> queue, boolean resend) {
+ boolean interrupted = Thread.interrupted();
+ List<Integer> results{INIT};
+ do {
+ results = queue.poll();
+ send(results);
+ } while (!results.isEmpty());
+ if (resend) {
+ awaitReplies();
+ }
+ done(interrupted);
+ }
+}
+"""
+
+
+def test_a_java_local_declared_without_a_value_is_not_passed_in():
+ """kestra's ``GrpcWorkerIOSender.cleanup``: ``List<T> results;`` and a
+ ``do`` loop that assigns it before every read."""
+ spans = _spans(_JAVA_DO.replace("{INIT}", ""), "java", "java")
+ assert (5, 11) in spans
+ assert spans[(5, 11)].params == ("queue", "resend")
+
+
+def test_the_same_local_with_a_value_is_passed_in():
+ """The control: with ``= null`` there is a value to see, and the analysis
+ cannot rule it out, so the rule of #3523 applies."""
+ spans = _spans(_JAVA_DO.replace("{INIT}", " = null"), "java", "java")
+ assert spans[(5, 11)].params == ("queue", "resend", "results")
+
+
+_TS_IF = """
+function run(query: string, args: unknown, out: Sink) {
+ out.note(query);
+ let parsedArgs{INIT};
+ if (args) {
+ parsedArgs = typeof args === 'string' ? JSON.parse(args) : args;
+ } else {
+ out.mark();
+ }
+ out.note(1);
+ const body = { statement: query, ...(parsedArgs ? parsedArgs : {}) };
+ out.send(body);
+ out.done();
+}
+"""
+
+
[email protected]("init", ["", ": Args | undefined", " = null"])
+def test_a_typescript_let_that_holds_undefined_is_passed_in(init):
+ """ToolJet's couchbase ``query_operations``: ``let parsedArgs;``. With a
+ falsy ``args`` the read sees ``undefined``, which is a value: the helper
+ needs it like any other."""
+ spans = _spans(_TS_IF.replace("{INIT}", init), "typescript", "ts")
+ holding = [x for (s, e), x in spans.items() if s == 4 and e >= 10]
+ assert holding, sorted(spans)
+ assert all("parsedArgs" in x.params for x in holding)
+
+
+def test_a_javascript_let_is_passed_in():
+ src = _TS_IF.replace("query: string, args: unknown, out: Sink", "query, args, out")
+ spans = _spans(src.replace("{INIT}", ""), "javascript", "js")
+ holding = [x for (s, e), x in spans.items() if s == 4 and e >= 10]
+ assert holding, sorted(spans)
+ assert all("parsedArgs" in x.params for x in holding)
+
+
+_TS_DO = """
+function cleanup(queue: Queue, resend: boolean, out: Sink) {
+ const interrupted = out.interrupted();
+ let results{INIT};
+ do {
+ results = queue.poll();
+ out.send(results);
+ } while (results.length > 0);
+ if (resend) {
+ out.awaitReplies();
+ }
+ out.done(interrupted);
+}
+"""
+
+
+def test_a_typed_typescript_let_is_not_passed_in():
+ """``let results: number[];`` cannot be read until it is assigned, so the
+ compiler has proved the ``do`` body's write; passing it would not compile."""
+ spans = _spans(_TS_DO.replace("{INIT}", ": number[]"), "typescript", "ts")
+ assert (4, 10) in spans
+ assert spans[(4, 10)].params == ("out", "queue", "resend")
+
+
+def test_the_same_let_without_a_type_is_passed_in():
+ spans = _spans(_TS_DO.replace("{INIT}", ""), "typescript", "ts")
+ assert spans[(4, 10)].params == ("out", "queue", "resend", "results")
+
+
+_JAVA_SAME_LINE = """
+class C {
+ void f(boolean c, Sink out) {
+ out.note(1);
+ int x; x = 1;
+ if (c) {
+ x = 7;
+ } else {
+ out.mark();
+ }
+ out.note(2);
+ if (x > 3) {
+ out.log(x);
+ } else {
+ out.log(0);
+ }
+ out.done();
+ }
+}
+"""
+
+
+def test_an_assignment_on_the_declarations_line_is_a_value():
+ """``int x; x = 1;``: the line holds a bare declaration and a value, and
+ what the read sees from it is the value."""
+ spans = _spans(_JAVA_SAME_LINE, "java", "java")
+ holding = [x for (s, e), x in spans.items() if s == 5 and e >= 14]
+ assert holding, sorted(spans)
+ assert all("x" in x.params for x in holding)
+
+
+def _valueless(src: str, language: str, ext: str) -> dict[str, bool]:
+ result = analyze_file(
+ f"m.{ext}", language, textwrap.dedent(src).lstrip("\n").encode(), flagged_only=False
+ )
+ if result.stats.functions_seen == 0:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ flags: dict[str, bool] = {}
+ for d in result.functions[0].def_use.definitions:
+ flags.setdefault(d.var, d.valueless) # the declaration, not a later write
+ return flags
+
+
+ ("language", "ext", "src", "want"),
+ [
+ (
+ "java",
+ "java",
+ "class C { void f() { int a; int b = 1; String c; a = 2; use(a, b, c); } }",
+ {"a": True, "b": False, "c": True},
+ ),
+ (
+ "typescript",
+ "ts",
+ "function f() { let a; let b = 1; let c: string; var d; a = 2; use(a, b, c, d); }",
+ {"a": False, "b": False, "c": True, "d": False},
+ ),
+ # Only a type that cannot hold ``undefined`` makes the name unreadable.
+ (
+ "typescript",
+ "ts",
+ "function f() { let e: string | undefined; let g!: string; let h: any;"
+ " let i: number | (string | undefined); let j: Box<undefined>;"
+ " let k: unknown; let l: void; let m: string = ''; let n: number | string;"
+ " use(e, g, h, i, j, k, l, m, n); }",
+ {
+ "e": False,
+ "g": False,
+ "h": False,
+ "i": False,
+ "j": True,
+ "k": False,
+ "l": False,
+ "m": False,
+ "n": True,
+ },
+ ),
+ (
+ "javascript",
+ "js",
+ "function f() { let a; var d; a = 2; use(a, d); }",
+ {"a": False, "d": False},
+ ),
+ (
+ "csharp",
+ "cs",
+ "class C { void F() { int a; int b = 1; string c; a = 2; Use(a, b, c); } }",
+ {"a": True, "b": False, "c": True},
+ ),
+ (
+ "cpp",
+ "cpp",
+ "void f() { int a; int b = 1; int* p; static int s; a = 2; use(a, b, p, s); }",
+ {"a": True, "b": False, "p": True, "s": False},
+ ),
+ (
+ "rust",
+ "rs",
+ "fn f() { let a; let b = 1; let c: u8; a = 2; c = 3; go(a, b, c); }",
+ {"a": True, "b": False, "c": True},
+ ),
+ # Go's zero value is a value, and Python has no bare declaration.
+ (
+ "go",
+ "go",
+ "package p\nfunc f() { var a int; b := 1; use(a, b) }",
+ {"a": False, "b": False},
+ ),
+ ("python", "py", "def f():\n b = 1\n use(b)\n", {"b": False}),
+ ],
+)
+def test_which_declarations_give_no_value(language, ext, src, want):
+ got = _valueless(src, language, ext)
+ assert {name: got[name] for name in want} == want
+
+
+def test_a_later_assignment_to_the_name_is_a_value():
+ """Only the declaration is without one: ``a = 2`` is an ordinary write."""
+ result = analyze_file(
+ "m.java", "java", b"class C { void f() { int a; a = 2; use(a); } }", flagged_only=False
+ )
+ if result.stats.functions_seen == 0:
+ pytest.skip("tree-sitter language pack missing for java")
+ flags = [d.valueless for d in result.functions[0].def_use.definitions if d.var == "a"]
+ assert flags == [True, False]
+
+
+# --- the staged split goes through the same rule ----------------------------
+#
+# ``stages.py`` sizes each stage with ``_infer_in_out``. Without the input its
+# composition check saw the read lose its value and refused the whole plan.
+
+
+def _stages(src: str, language: str = "python", ext: str = "py", *, max_returns: int = 3):
+ result = analyze_file(
+ f"m.{ext}", language, textwrap.dedent(src).lstrip("\n").encode(), flagged_only=False
+ )
+ if result.stats.functions_seen == 0:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ plan = find_stages(result.functions[0], get_language_map(language), max_returns=max_returns)
+ return plan, [(x.start_line, x.end_line, x.params, x.returns) for x in plan.stages]
+
+
+_PY_STAGES = """
+def load(items, strict):
+ seen = []
+ invalid = False
+ for it in items:
+ if it.a:
+ seen.append(it.a)
+ elif it.b:
+ seen.append(it.b)
+ if it.c and strict:
+ seen.append(it.c)
+ if not seen:
+ if strict:
+ invalid = True
+ elif len(seen) > 9:
+ if strict:
+ invalid = True
+ if invalid and strict:
+ report(seen)
+ return seen
+"""
+
+
+def test_a_stage_takes_the_name_a_branch_of_it_may_write():
+ """The shape of hermes-webui's ``_load_extension_state``: a flag set
+ before the stage, raised on some paths inside it and read at its end."""
+ plan, stages = _stages(_PY_STAGES)
+ assert plan.reason is None
+ assert stages == [
+ (2, 10, ("items", "strict"), ("invalid", "seen")),
+ (11, 18, ("invalid", "seen", "strict"), ()),
+ ]
+
+
+_TS_STAGES = """
+function load(items: Item[], strict: boolean, seen: string[]): string[] {
+ let worst{INIT};
+ for (const it of items) {
+ if (it.a) {
+ seen.push(it.a);
+ } else if (it.b) {
+ seen.push(it.b);
+ } else if (it.c && strict) {
+ seen.push(it.c);
+ }
+ if (it.d || it.e) {
+ seen.push(it.d);
+ }
+ }
+ if (seen.length === 0) {
+ if (strict) {
+ worst = "none";
+ }
+ } else if (seen.length > 9) {
+ if (strict) {
+ worst = "many";
+ }
+ } else if (seen.length > 5 && strict) {
+ worst = "some";
+ }
+ if (worst && strict) {
+ report(worst);
+ }
+ return seen;
+}
+"""
+
+
+def test_a_stage_takes_it_in_typescript_too():
+ plan, stages = _stages(
+ _TS_STAGES.replace("{INIT}", " = null"), "typescript", "ts", max_returns=1
+ )
+ assert plan.reason is None
+ assert stages == [
+ (2, 14, ("items", "seen", "strict"), ("worst",)),
+ (15, 28, ("seen", "strict", "worst"), ()),
+ ]
+
+
+_TS_DRAIN = """
+function drain(items: Item[], strict: boolean, seen: string[], queue: Queue): string[] {
+ let batch{INIT};
+ for (const it of items) {
+ if (it.a) {
+ seen.push(it.a);
+ } else if (it.b) {
+ seen.push(it.b);
+ } else if (it.c && strict) {
+ seen.push(it.c);
+ }
+ if (it.d || it.e) {
+ seen.push(it.d);
+ }
+ }
+ do {
+ batch = queue.poll(seen);
+ if (batch.length > 9 && strict) {
+ report(batch);
+ } else if (batch.length > 5 || seen.length === 0) {
+ seen.pop();
+ }
+ if (strict && seen.length > 3) {
+ queue.resend(seen);
+ }
+ if (seen.length > 7) {
+ seen.shift();
+ } else if (seen.length > 6 && strict) {
+ queue.flush();
+ }
+ } while (batch.length > 0 && strict);
+ return seen;
+}
+"""
+
+
+def test_no_stage_is_handed_a_declaration_without_a_value():
+ """With ``let batch: string[];`` the second stage is not given ``batch``,
+ and the composition check then declines the split, as it did before
+ #3523. Handed on, the first stage would return a name never assigned."""
+ plan, stages = _stages(
+ _TS_DRAIN.replace("{INIT}", ": string[]"), "typescript", "ts", max_returns=1
+ )
+ assert stages == []
+ assert plan.reason == "composition"
+
+
+def test_a_stage_is_handed_a_let_that_holds_undefined():
+ plan, stages = _stages(_TS_DRAIN.replace("{INIT}", ""), "typescript", "ts", max_returns=1)
+ assert plan.reason is None
+ assert stages == [
+ (2, 14, ("items", "seen", "strict"), ("batch",)),
+ (15, 30, ("batch", "queue", "seen", "strict"), ()),
+ ]