INRIA / INRIA/spoon

[Bug]: Spoon resolves wrong variable declaration with negated instanceof pattern and local variable shadowing

Open
#6,645 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

### Describe the bug

In the below code, in the last `System.out.println(i);` in the `main`, the variable reference `i` is resolved as a reference to the first `if (o instanceof String i) {`.

This is wrong, because `String i` is only valid inside the if block, and not outside of it.
The code worked on version `11.2.2-beta-10`, and started failing with `11.2.2-beta-11`.

I think the cause for this issue is a change introduced by https://github.com/INRIA/spoon/pull/6444

### Source code you are trying to analyze/transform

```Java
public class Main {
public static void main(String[] args) {
Object o = "Hello";
if (o instanceof String i) {
System.out.println(i);
}

// negated if
if (!(o instanceof String i)) {
} else {
System.out.println(i);
}

// then branch cannot complete
if (!(o instanceof String i)) {
throw new IllegalArgumentException();
}
System.out.println(i);
}
}
```

### Source code for your Spoon processing

```Java

```

### Actual output

```Java

```

### Expected output

```Java

```

### Spoon Version

11.3.1-beta-8

### JVM Version

openjdk version "21.0.2" 2024-01-16 LTS OpenJDK Runtime Environment Zulu21.32+17-CA (build 21.0.2+13-LTS) OpenJDK 64-Bit Server VM Zulu21.32+17-CA (build 21.0.2+13-LTS, mixed mode, sharing)

### What operating system are you using?

Windows 11

Contributor guide

Open the contributing guide

Research direction

Start by reproducing issue #6645 with the supplied Java source on Spoon 11.3.1-beta-8 and compare the resolved reference for the final `System.out.println(i);` with the expected scope. Trace the handling of negated `instanceof` pattern variables and local-variable shadowing, then add regression coverage showing that the out-of-scope declaration is not selected.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.