File tree Expand file tree Collapse file tree 3 files changed +44
-4
lines changed 
compiler/src/dotty/tools/dotc/core Expand file tree Collapse file tree 3 files changed +44
-4
lines changed Original file line number Diff line number Diff line change @@ -287,11 +287,24 @@ trait ConstraintHandling {
287287  end  legalBound 
288288
289289  protected  def  addOneBound (param : TypeParamRef , rawBound : Type , isUpper : Boolean )(using  Context ):  Boolean  = 
290+ 
291+     //  Replace top-level occurrences of `param` in `bound` by `Nothing`
292+     def  sanitize (bound : Type ):  Type  = 
293+       if  bound.stripped eq param then  defn.NothingType 
294+       else  bound match 
295+         case  bound : AndOrType  => 
296+           bound.derivedAndOrType(sanitize(bound.tp1), sanitize(bound.tp2))
297+         case  _ => 
298+           bound
299+ 
290300    if  ! constraint.contains(param) then  true 
291-     else  if  ! isUpper &&  param.occursIn(rawBound) then 
292-       //  We don't allow recursive lower bounds when defining a type,
293-       //  so we shouldn't allow them as constraints either.
294-       false 
301+     else  if  ! isUpper &&  param.occursIn(rawBound.widen) then 
302+       val  rawBound1  =  sanitize(rawBound.widenDealias)
303+       if  param.occursIn(rawBound1) then 
304+         //  We don't allow recursive lower bounds when defining a type,
305+         //  so we shouldn't allow them as constraints either.
306+         false 
307+       else  addOneBound(param, rawBound1, isUpper)
295308    else 
296309
297310      //  Narrow one of the bounds of type parameter `param`
Original file line number Diff line number Diff line change 1+ -- [E007] Type Mismatch Error: tests/neg/i21535.scala:7:4 --------------------------------------------------------------
2+ 3 |  (if (true) then
3+ 4 |    new A(66)
4+ 5 |  else
5+ 6 |    m1()
6+ 7 |  ).m2(p1 = p); // error
7+   |  ^
8+   |  Found:    (Int | Short) @uncheckedVariance
9+   |  Required: Int & Short
10+   |
11+   | longer explanation available when compiling with `-explain`
Original file line number Diff line number Diff line change 1+ def  test () =  {
2+   val  p  =  10 .toShort
3+   (if  (true ) then 
4+     new  A (66 )
5+   else 
6+     m1()
7+   ).m2(p1 =  p); //  error
8+ 
9+ }
10+ 
11+ def  m1 ():  A [Short ] =  new  A (10 )
12+ 
13+ class  A [D ](var  f :  D ) {
14+ 
15+   def  m2 (p1 : D  =  f, p2 : D  =  f):  Unit  =  {}
16+ }
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments