reject_shadow_type_switch.mx raw

   1  // expected: shadows declaration in enclosing scope
   2  //
   3  // The binding a type switch introduces is a declaration like any other, so
   4  // `switch s := s.(type)` shadows the parameter it reads. Legacy rejects it
   5  // through go/types scopes; stage4's position-insensitive shadow pass never saw
   6  // the binding, because a type switch keeps it in the guard rather than in an
   7  // assignment, so the same source compiled. The binding is declared now.
   8  package main
   9  
  10  type shape interface {
  11  	area() (a int32)
  12  }
  13  
  14  type square struct {
  15  	side int32
  16  }
  17  
  18  func (s *square) area() (a int32) { return s.side * s.side }
  19  
  20  func measure(s shape) (n int32) {
  21  	switch s := s.(type) {
  22  	case *square:
  23  		return s.area()
  24  	}
  25  	return 0
  26  }
  27  
  28  func main() {
  29  	println(measure(&square{side: 2}))
  30  }
  31