- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.1k
 
Modify pattern matcher to warn when | Null is used in patterns #23288
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
          
     Open
      
      
            SuperCl4sh
  wants to merge
  6
  commits into
  scala:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
SuperCl4sh:fix-pattern
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      31428b2
              
                Modify pattern matcher to raise an error instead of generating illega…
              
              
                SuperCl4sh 725bf5e
              
                Revert previous changes and simplify Nothing | T to T and Null | T to…
              
              
                SuperCl4sh 464d605
              
                Merge branch 'main' into fix-pattern
              
              
                SuperCl4sh 3e25a19
              
                Modify logic type tests logic to strip the | Null's from testType and…
              
              
                SuperCl4sh 558d67f
              
                Add warning for having | Null's in patterns in pattern matching
              
              
                SuperCl4sh 8bfb003
              
                Merge branch 'main' into fix-pattern
              
              
                SuperCl4sh File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -926,8 +926,38 @@ object SpaceEngine { | |
| cases match | ||
| case Nil => | ||
| case (c @ CaseDef(pat, _, _)) :: rest => | ||
| def checkForUnnecessaryNullable(p: Tree): Unit = p match { | ||
| case Bind(_, body) => checkForUnnecessaryNullable(body) | ||
| case Typed(expr, tpt) => | ||
| if tpt.tpe.isNullableUnion(stripFlexibleTypes = false, forceStrip = true) then | ||
| report.warning(MatchCaseUnnecessaryNullable(tpt.tpe), p.srcPos) | ||
| else | ||
| checkForUnnecessaryNullable(expr) | ||
| case UnApply(_, _, patterns) => | ||
| patterns.map(checkForUnnecessaryNullable) | ||
| case Alternative(patterns) => patterns.map(checkForUnnecessaryNullable) | ||
| case _ => | ||
| } | ||
| 
     | 
||
| def handlesNull(p: Tree): Boolean = p match { | ||
| case Literal(Constant(null)) => true | ||
| case Typed(expr, tpt) => tpt.tpe.isSingleton || handlesNull(expr) // assume all SingletonType's handle null (see redundant-null.scala) | ||
| case Bind(_, body) => handlesNull(body) | ||
| case Alternative(patterns) => patterns.exists(handlesNull) | ||
| case _ => false | ||
| } | ||
| val curr = trace(i"project($pat)")(projectPat(pat)) | ||
| val covered = trace("covered")(simplify(intersect(curr, targetSpace))) | ||
| var covered = trace("covered")(simplify(intersect(curr, targetSpace))) | ||
| 
     | 
||
| checkForUnnecessaryNullable(pat) | ||
| 
     | 
||
| if !handlesNull(pat) && !isWildcardArg(pat) then | ||
| // Remove nullSpace from covered only if: | ||
| // 1. No pattern is the null constant (e.g., `case null =>` or `case ... | null | ... =>`) or | ||
| // has a SingletonType (e.g., `case _: n.type =>` where `val n = null`, see redundant-null.scala) in one of its pattern(s) | ||
| // 2. The pattern is a wildcard pattern. | ||
| covered = minus(covered, nullSpace) | ||
| 
         
      Comment on lines
    
      +954
     to 
      +959
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to modify the space calculation if we just want to add a warning? Can you point to a test which will fail if we don't remove   | 
||
| 
     | 
||
| val prev = trace("prev")(simplify(Or(prevs))) | ||
| if prev == Empty && covered == Empty then // defer until a case is reachable | ||
| recur(rest, prevs, pat :: deferred) | ||
| 
          
            
          
           | 
    ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| object Extractor: | ||
| def unapply(s: String|Null): Boolean = true | ||
| 
     | 
||
| class A | ||
| 
     | 
||
| def main = | ||
| ("foo": (A|String)) match | ||
| case Extractor() => println("foo matched") // warn: String | Null is nullable | ||
| case _ => println("foo didn't match") | ||
| val s: Some[Any] = Some(5) | ||
| 
     | 
||
| s match { | ||
| case Some(s: (String | Null)) => // warn: String | Null is nullable | ||
| case Some(Some(s: (String | Null))) => // warn: String | Null is nullable | ||
| case _: s.type => | ||
| case Some(s: Int) => | ||
| case _ => | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| def main = { | ||
| ("foo": String) match { | ||
| case a: (String | Nothing) => // error | ||
| println("bar") | ||
| case _ => | ||
| println("foo") | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| @main def Test = | ||
| "a".isInstanceOf[Null] // error | ||
| null.isInstanceOf[Null] // error | ||
| "a".isInstanceOf[Nothing] // error | ||
| "a".isInstanceOf[Singleton] // error | ||
| 
     | 
||
| "a" match { | ||
| case _: Singleton => () // error | ||
| case _ => () | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| -- [E222] Pattern Match Warning: tests/warn/i23243.scala:8:9 ----------------------------------------------------------- | ||
| 8 | case Extractor() => println("foo matched") // warn: String | Null is nullable | ||
| | ^^^^^^^^^^^ | ||
| | String | Null is nullable, but will not be matched by null. | ||
| -- [E222] Pattern Match Warning: tests/warn/i23243.scala:13:17 --------------------------------------------------------- | ||
| 13 | case Some(s: (String | Null)) => // warn: String | Null is nullable | ||
| | ^^^^^^^^^^^^^^^ | ||
| | String | Null is nullable, but will not be matched by null. | ||
| -- [E222] Pattern Match Warning: tests/warn/i23243.scala:14:22 --------------------------------------------------------- | ||
| 14 | case Some(Some(s: (String | Null))) => // warn: String | Null is nullable | ||
| | ^^^^^^^^^^^^^^^ | ||
| | String | Null is nullable, but will not be matched by null. | ||
| -- [E222] Pattern Match Warning: tests/warn/i23243.scala:17:22 --------------------------------------------------------- | ||
| 17 | case (null | Some(_: (Int | Null)))| Some(_: (String | Null)) => // warn: Int | Null is nullable // warn: String | Null is nullable | ||
| | ^^^^^^^^^^^^^^^ | ||
| | Int | Null is nullable, but will not be matched by null. | ||
| -- [E222] Pattern Match Warning: tests/warn/i23243.scala:17:46 --------------------------------------------------------- | ||
| 17 | case (null | Some(_: (Int | Null)))| Some(_: (String | Null)) => // warn: Int | Null is nullable // warn: String | Null is nullable | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | String | Null is nullable, but will not be matched by null. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| object Extractor: | ||
| def unapply(s: String|Null): Boolean = true | ||
| 
     | 
||
| class A | ||
| 
     | 
||
| def main = | ||
| ("foo": (A|String)) match | ||
| case Extractor() => println("foo matched") // warn: String | Null is nullable | ||
| case _ => println("foo didn't match") | ||
| val s: Any = 5 | ||
| 
     | 
||
| s match { | ||
| case Some(s: (String | Null)) => // warn: String | Null is nullable | ||
| case Some(Some(s: (String | Null))) => // warn: String | Null is nullable | ||
| case Some(null) => | ||
| case _: s.type => | ||
| case (null | Some(_: (Int | Null)))| Some(_: (String | Null)) => // warn: Int | Null is nullable // warn: String | Null is nullable | ||
| case _ => | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should rename the parameters here to give them a better meaning. Do we really need
stripFlexibleTypes?