You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a new diagnostic that warns when a variable assignment inside
a `for`/`while`/`try` block at the top level shadows an existing
global variable, matching the warning Julia itself emits at
runtime as explained in the Julia's [soft scope manual](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#on-soft-scope):
```
global i = 0
while i < 5
i += 1 # Assignment to `i` in soft scope is ambiguous (JETLS lowering/ambiguous-soft-scope)
# Variable `i` may be used before it is defined (JETLS lowering/undef-local-var)
println(i)
end
```
- Add `analyze_ambiguous_soft_scope!` using JuliaLowering's
`is_ambiguous_local` binding flag
- Add code actions: "Insert `global`" (preferred) and
"Insert `local`" declarations with proper indentation
- Enable soft scope semantics for notebook cells via
`is_notebook_cell_uri`, suppressing this diagnostic
Copy file name to clipboardExpand all lines: CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -50,6 +50,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
50
50
Unreachable code is displayed as faded/grayed out with the `Unnecessary` tag.
51
51
A "Delete unreachable code" quick fix code action is also available.
52
52
53
+
- Added [`lowering/ambiguous-soft-scope`](https://aviatesk.github.io/JETLS.jl/release/diagnostic/#diagnostic/reference/lowering/ambiguous-soft-scope) diagnostic that warns when a variable assignment inside a `for`/`while`/`try` block at the top level shadows an existing global variable.
54
+
This matches the warning Julia itself emits at runtime for this pattern.
55
+
Two code actions are offered: "Insert `global` declaration" (preferred) and "Insert `local` declaration".
56
+
This diagnostic is suppressed for notebook cells, where soft scope semantics are enabled.
57
+
53
58
### Changed
54
59
55
60
- [`lowering/undef-local-var`](https://aviatesk.github.io/JETLS.jl/release/diagnostic/#diagnostic/reference/lowering/undef-local-var) now recognizes correlated conditions to reduce false positives.
Copy file name to clipboardExpand all lines: docs/src/diagnostic.md
+81Lines changed: 81 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,6 +115,7 @@ Here is a summary table of the diagnostics explained in this section:
115
115
|[`lowering/unused-import`](@ref diagnostic/reference/lowering/unused-import) |`Information`|`JETLS/live`| Imported names that are never used |
116
116
|[`lowering/unreachable-code`](@ref diagnostic/reference/lowering/unreachable-code) |`Information`|`JETLS/live`| Code after a block terminator that is never reached |
117
117
|[`lowering/unsorted-import-names`](@ref diagnostic/reference/lowering/unsorted-import-names) |`Hint`|`JETLS/live`| Import/export names not sorted alphabetically |
118
+
|[`lowering/ambiguous-soft-scope`](@ref diagnostic/reference/lowering/ambiguous-soft-scope) |`Warning`|`JETLS/live`| Assignment in soft scope shadows a global variable |
118
119
|[`toplevel/error`](@ref diagnostic/reference/toplevel/error) |`Error`|`JETLS/save`| Errors during code loading |
119
120
|[`toplevel/method-overwrite`](@ref diagnostic/reference/toplevel/method-overwrite) |`Warning`|`JETLS/save`| Method definitions that overwrite previous ones |
120
121
|[`toplevel/abstract-field`](@ref diagnostic/reference/toplevel/abstract-field) |`Information`|`JETLS/save`| Struct fields with abstract types |
@@ -602,6 +603,86 @@ export bar, @foo # Names are not sorted alphabetically (JETLS lowering/unsorted
602
603
[Julia's conventional maximum line length](https://docs.julialang.org/en/v1.14-dev/devdocs/contributing/formatting/#General-Formatting-Guidelines-for-Julia-code-contributions)),
603
604
the code action wraps to multiple lines with 4-space continuation indent.
Example ([A Common Confusion](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#A-Common-Confusion-2479cb3548c466db) adapted from the Julia manual):
624
+
625
+
```@eval
626
+
using Markdown
627
+
628
+
mktemp() do file, io
629
+
code ="""
630
+
# Print the numbers 1 through 5
631
+
global i = 0
632
+
while i < 5
633
+
i += 1 # Assignment to `i` in soft scope is ambiguous (JETLS lowering/ambiguous-soft-scope)
634
+
# Variable `i` may be used before it is defined (JETLS lowering/undef-local-var)
0 commit comments