Fixes #10749, feature/pattern variable shadow warning#25196
Fixes #10749, feature/pattern variable shadow warning#25196Bbn08 wants to merge 1 commit intoscala:mainfrom
Conversation
|
There is a general directive that lints should not infect typer. They should have a mini-phase like current shadowing lint. |
|
@som-snytt Thank you for the review and the clarification! I understand, I should move the pattern variable shadow check out of Typer and into a dedicated MiniPhase, similar to how CheckShadowing works currently. I will refactor the PR to implement this as a separate phase to avoid complicating the main type checker. I'll start working on that refactor now. |
36fb276 to
070d4f9
Compare
|
Hi @som-snytt, thank you again for the review feedback. I've refactored the implementation to move the pattern variable shadow check out of Summary of Changes 1. CheckShadowing.scala
1.
2. ScalaSettings.scalaAdded 3. Typer.scalaRemoved the shadow checking logic that was previously in 4. Tests (tests/warn/i10749.scala)
If there's anything else you'd like me to work on or improve, please feel free to let me know as I'm happy to make any additional changes :) |
|
I'll review shortly, when the other shadowing PR is merged. A quick note, the shadowing code uses a custom model for scopes. The "unused import" used to do that, but was changed for reasons of correctness and performance. The Scala 2 was scala/scala#8806 which is now ancient history, but maybe the feature is the same. |
|
Noted. |
614a843 to
60c24ad
Compare
|
Rebased on top of #25101 and adapted the pattern variable shadow warning to the new unified reporting structure. Here's what changed: Removed the custom case-class hierarchy (
|
Problem
When a pattern-bound variable has the same name as a variable in an enclosing scope, the pattern variable silently shadows the outer one. This can lead to subtle bugs:
Solution
Add a new warning option
-Wshadow:pattern-variable-shadowthat detects when a pattern variable shadows an existing term in the enclosing scope.Implementation
pattern-variable-shadowto the-Wshadowmulti-choice settingtypedBind()that traverses outer scopes to check for existing terms with the same nameWarning Output
Test Coverage
Added tests/warn/i10749.scala with 8 test cases covering:
Usage