fail_unbound_field.carbon 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/fail_unbound_field.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/fail_unbound_field.carbon
  10. class Class {
  11. var field: i32;
  12. fn F() -> i32 {
  13. // CHECK:STDERR: fail_unbound_field.carbon:[[@LINE+4]]:12: error: expression cannot be used as a value [UseOfNonExprAsValue]
  14. // CHECK:STDERR: return field;
  15. // CHECK:STDERR: ^~~~~
  16. // CHECK:STDERR:
  17. return field;
  18. }
  19. }
  20. fn G() -> i32 {
  21. // CHECK:STDERR: fail_unbound_field.carbon:[[@LINE+4]]:10: error: expression cannot be used as a value [UseOfNonExprAsValue]
  22. // CHECK:STDERR: return Class.field;
  23. // CHECK:STDERR: ^~~~~~~~~~~
  24. // CHECK:STDERR:
  25. return Class.field;
  26. }
  27. // CHECK:STDOUT: --- fail_unbound_field.carbon
  28. // CHECK:STDOUT:
  29. // CHECK:STDOUT: constants {
  30. // CHECK:STDOUT: %Class: type = class_type @Class [concrete]
  31. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  32. // CHECK:STDOUT: %Int.type: type = generic_class_type @Int [concrete]
  33. // CHECK:STDOUT: %Int.generic: %Int.type = struct_value () [concrete]
  34. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  35. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %i32 [concrete]
  36. // CHECK:STDOUT: %pattern_type.7ce: type = pattern_type %i32 [concrete]
  37. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  38. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  39. // CHECK:STDOUT: %struct_type.field: type = struct_type {.field: %i32} [concrete]
  40. // CHECK:STDOUT: %complete_type.d48: <witness> = complete_type_witness %struct_type.field [concrete]
  41. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  42. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: imports {
  46. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  47. // CHECK:STDOUT: .Int = %Core.Int
  48. // CHECK:STDOUT: import Core//prelude
  49. // CHECK:STDOUT: import Core//prelude/...
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT: %Core.Int: %Int.type = import_ref Core//prelude/types/int, Int, loaded [concrete = constants.%Int.generic]
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: file {
  55. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  56. // CHECK:STDOUT: .Core = imports.%Core
  57. // CHECK:STDOUT: .Class = %Class.decl
  58. // CHECK:STDOUT: .G = %G.decl
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %Core.import = import Core
  61. // CHECK:STDOUT: %Class.decl: type = class_decl @Class [concrete = constants.%Class] {} {}
  62. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  63. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  64. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  65. // CHECK:STDOUT: } {
  66. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  67. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  68. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  69. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: class @Class {
  74. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  75. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  76. // CHECK:STDOUT: %.loc12: %Class.elem = field_decl field, element0 [concrete]
  77. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  78. // CHECK:STDOUT: %return.patt: %pattern_type.7ce = return_slot_pattern [concrete]
  79. // CHECK:STDOUT: %return.param_patt: %pattern_type.7ce = out_param_pattern %return.patt, call_param0 [concrete]
  80. // CHECK:STDOUT: } {
  81. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  82. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  83. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param0
  84. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %struct_type.field: type = struct_type {.field: %i32} [concrete = constants.%struct_type.field]
  87. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.field [concrete = constants.%complete_type.d48]
  88. // CHECK:STDOUT: complete_type_witness = %complete_type
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: !members:
  91. // CHECK:STDOUT: .Self = constants.%Class
  92. // CHECK:STDOUT: .field = %.loc12
  93. // CHECK:STDOUT: .F = %F.decl
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: fn @F() -> %i32 {
  97. // CHECK:STDOUT: !entry:
  98. // CHECK:STDOUT: %field.ref: %Class.elem = name_ref field, @Class.%.loc12 [concrete = @Class.%.loc12]
  99. // CHECK:STDOUT: return <error>
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: fn @G() -> %i32 {
  103. // CHECK:STDOUT: !entry:
  104. // CHECK:STDOUT: %Class.ref: type = name_ref Class, file.%Class.decl [concrete = constants.%Class]
  105. // CHECK:STDOUT: %field.ref: %Class.elem = name_ref field, @Class.%.loc12 [concrete = @Class.%.loc12]
  106. // CHECK:STDOUT: return <error>
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: