fail_address_of_value.carbon 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/pointer/fail_address_of_value.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/pointer/fail_address_of_value.carbon
  12. fn G() -> i32;
  13. fn H() -> {.a: i32};
  14. fn AddressOfLiteral() {
  15. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  16. // CHECK:STDERR: &0;
  17. // CHECK:STDERR: ^
  18. // CHECK:STDERR:
  19. &0;
  20. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  21. // CHECK:STDERR: &true;
  22. // CHECK:STDERR: ^
  23. // CHECK:STDERR:
  24. &true;
  25. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  26. // CHECK:STDERR: &1.0;
  27. // CHECK:STDERR: ^
  28. // CHECK:STDERR:
  29. &1.0;
  30. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  31. // CHECK:STDERR: &"Hello";
  32. // CHECK:STDERR: ^
  33. // CHECK:STDERR:
  34. &"Hello";
  35. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  36. // CHECK:STDERR: &(1, 2);
  37. // CHECK:STDERR: ^
  38. // CHECK:STDERR:
  39. &(1, 2);
  40. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  41. // CHECK:STDERR: &{.a = 5};
  42. // CHECK:STDERR: ^
  43. // CHECK:STDERR:
  44. &{.a = 5};
  45. }
  46. fn AddressOfOperator() {
  47. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  48. // CHECK:STDERR: &(true and false);
  49. // CHECK:STDERR: ^
  50. // CHECK:STDERR:
  51. &(true and false);
  52. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of a temporary object [AddrOfEphemeralRef]
  53. // CHECK:STDERR: &H().a;
  54. // CHECK:STDERR: ^
  55. // CHECK:STDERR:
  56. &H().a;
  57. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  58. // CHECK:STDERR: &(not true);
  59. // CHECK:STDERR: ^
  60. // CHECK:STDERR:
  61. &(not true);
  62. }
  63. fn AddressOfCall() {
  64. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  65. // CHECK:STDERR: &G();
  66. // CHECK:STDERR: ^
  67. // CHECK:STDERR:
  68. &G();
  69. }
  70. fn AddressOfType() {
  71. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  72. // CHECK:STDERR: &i32;
  73. // CHECK:STDERR: ^
  74. // CHECK:STDERR:
  75. &i32;
  76. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  77. // CHECK:STDERR: &(const i32*);
  78. // CHECK:STDERR: ^
  79. // CHECK:STDERR:
  80. &(const i32*);
  81. }
  82. fn AddressOfTupleElementValue() {
  83. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:3: error: cannot take the address of non-reference expression [AddrOfNonRef]
  84. // CHECK:STDERR: &((1, 2).0);
  85. // CHECK:STDERR: ^
  86. // CHECK:STDERR:
  87. &((1, 2).0);
  88. }
  89. fn AddressOfParam(param: i32) {
  90. // CHECK:STDERR: fail_address_of_value.carbon:[[@LINE+4]]:26: error: cannot take the address of non-reference expression [AddrOfNonRef]
  91. // CHECK:STDERR: var param_addr: i32* = &param;
  92. // CHECK:STDERR: ^
  93. // CHECK:STDERR:
  94. var param_addr: i32* = &param;
  95. }