reject_ptr_to_int.mx raw

   1  // Restriction tests ensure the compiler rejects specific patterns with
   2  // a clean error message rather than crashing.
   3  //
   4  // Format: each .mx file starts with "// expected: <substring>"
   5  // The runner compiles the file, asserts exit != 0, and checks that
   6  // stderr contains the expected substring. It also asserts that the
   7  // compiler does NOT crash (no "panic:", "SIGSEGV", "nil pointer").
   8  //
   9  // expected: a pointer converts to an integer only through unsafe.Pointer
  10  package main
  11  
  12  type file struct {
  13  	fd int32
  14  }
  15  
  16  func main() {
  17  	f := &file{fd: 3}
  18  	// Go has no direct pointer-to-integer conversion: the unsafe.Pointer step
  19  	// is what makes the integer meaningful. Legacy rejects both spellings; so
  20  	// must stage4, or code written here compiles and then fails to bootstrap.
  21  	direct := int32(f)
  22  	wide := uintptr(f)
  23  	if direct == 0 || wide == 0 {
  24  		panic("unreachable")
  25  	}
  26  }
  27