err2.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. /*
  6. #include <stdio.h>
  7. typedef struct foo foo_t;
  8. typedef struct bar bar_t;
  9. foo_t *foop;
  10. long double x = 0;
  11. static int transform(int x) { return x; }
  12. typedef void v;
  13. void F(v** p) {}
  14. void fvi(void *p, int x) {}
  15. void fppi(int** p) {}
  16. int i;
  17. void fi(int i) {}
  18. */
  19. import "C"
  20. import (
  21. "unsafe"
  22. )
  23. func main() {
  24. s := ""
  25. _ = s
  26. C.malloc(s) // ERROR HERE
  27. x := (*C.bar_t)(nil)
  28. C.foop = x // ERROR HERE
  29. // issue 13129: used to output error about C.unsignedshort with CC=clang
  30. var x1 C.ushort
  31. x1 = int(0) // ERROR HERE: C\.ushort
  32. // issue 13423
  33. _ = C.fopen() // ERROR HERE
  34. // issue 13467
  35. var x2 rune = '✈'
  36. var _ rune = C.transform(x2) // ERROR HERE: C\.int
  37. // issue 13635: used to output error about C.unsignedchar.
  38. // This test tests all such types.
  39. var (
  40. _ C.uchar = "uc" // ERROR HERE: C\.uchar
  41. _ C.schar = "sc" // ERROR HERE: C\.schar
  42. _ C.ushort = "us" // ERROR HERE: C\.ushort
  43. _ C.uint = "ui" // ERROR HERE: C\.uint
  44. _ C.ulong = "ul" // ERROR HERE: C\.ulong
  45. _ C.longlong = "ll" // ERROR HERE: C\.longlong
  46. _ C.ulonglong = "ull" // ERROR HERE: C\.ulonglong
  47. _ C.complexfloat = "cf" // ERROR HERE: C\.complexfloat
  48. _ C.complexdouble = "cd" // ERROR HERE: C\.complexdouble
  49. )
  50. // issue 13830
  51. // cgo converts C void* to Go unsafe.Pointer, so despite appearances C
  52. // void** is Go *unsafe.Pointer. This test verifies that we detect the
  53. // problem at build time.
  54. {
  55. type v [0]byte
  56. f := func(p **v) {
  57. C.F((**C.v)(unsafe.Pointer(p))) // ERROR HERE
  58. }
  59. var p *v
  60. f(&p)
  61. }
  62. // issue 16116
  63. _ = C.fvi(1) // ERROR HERE
  64. // Issue 16591: Test that we detect an invalid call that was being
  65. // hidden by a type conversion inserted by cgo checking.
  66. {
  67. type x *C.int
  68. var p *x
  69. C.fppi(p) // ERROR HERE
  70. }
  71. // issue 26745
  72. _ = func(i int) int {
  73. // typecheck reports at column 14 ('+'), but types2 reports at
  74. // column 10 ('C').
  75. // TODO(mdempsky): Investigate why, and see if types2 can be
  76. // updated to match typecheck behavior.
  77. return C.i + 1 // ERROR HERE: \b(10|14)\b
  78. }
  79. _ = func(i int) {
  80. // typecheck reports at column 7 ('('), but types2 reports at
  81. // column 8 ('i'). The types2 position is more correct, but
  82. // updating typecheck here is fundamentally challenging because of
  83. // IR limitations.
  84. C.fi(i) // ERROR HERE: \b(7|8)\b
  85. }
  86. C.fi = C.fi // ERROR HERE
  87. }