fail_bad_condition.carbon 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/while/fail_bad_condition.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/while/fail_bad_condition.carbon
  10. fn While() {
  11. // CHECK:STDERR: fail_bad_condition.carbon:[[@LINE+6]]:9: error: cannot implicitly convert from `String` to `bool`
  12. // CHECK:STDERR: while ("Hello") {}
  13. // CHECK:STDERR: ^~~~~~~~~
  14. // CHECK:STDERR: fail_bad_condition.carbon:[[@LINE+3]]:9: note: type `String` does not implement interface `ImplicitAs`
  15. // CHECK:STDERR: while ("Hello") {}
  16. // CHECK:STDERR: ^~~~~~~~~
  17. while ("Hello") {}
  18. }
  19. // CHECK:STDOUT: --- fail_bad_condition.carbon
  20. // CHECK:STDOUT:
  21. // CHECK:STDOUT: constants {
  22. // CHECK:STDOUT: %While.type: type = fn_type @While [template]
  23. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  24. // CHECK:STDOUT: %While: %While.type = struct_value () [template]
  25. // CHECK:STDOUT: %.2: type = ptr_type String [template]
  26. // CHECK:STDOUT: %.3: String = string_literal "Hello" [template]
  27. // CHECK:STDOUT: %ImplicitAs.type.1: type = generic_interface_type @ImplicitAs [template]
  28. // CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type.1 = struct_value () [template]
  29. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic]
  30. // CHECK:STDOUT: %ImplicitAs.type.2: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
  31. // CHECK:STDOUT: %Self.1: @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2) = bind_symbolic_name Self, 1 [symbolic]
  32. // CHECK:STDOUT: %Dest.patt: type = symbolic_binding_pattern Dest, 0 [symbolic]
  33. // CHECK:STDOUT: %Self.2: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic]
  34. // CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic]
  35. // CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
  36. // CHECK:STDOUT: %.4: type = assoc_entity_type %ImplicitAs.type.2, %Convert.type.1 [symbolic]
  37. // CHECK:STDOUT: %.5: %.4 = assoc_entity element0, imports.%import_ref.5 [symbolic]
  38. // CHECK:STDOUT: %ImplicitAs.type.3: type = interface_type @ImplicitAs, @ImplicitAs(bool) [template]
  39. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @ImplicitAs(bool) [template]
  40. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
  41. // CHECK:STDOUT: %.6: type = assoc_entity_type %ImplicitAs.type.3, %Convert.type.2 [template]
  42. // CHECK:STDOUT: %.7: %.6 = assoc_entity element0, imports.%import_ref.5 [template]
  43. // CHECK:STDOUT: %.8: %.4 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: imports {
  47. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  48. // CHECK:STDOUT: .ImplicitAs = %import_ref.1
  49. // CHECK:STDOUT: import Core//prelude
  50. // CHECK:STDOUT: import Core//prelude/operators
  51. // CHECK:STDOUT: import Core//prelude/types
  52. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  53. // CHECK:STDOUT: import Core//prelude/operators/as
  54. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  55. // CHECK:STDOUT: import Core//prelude/operators/comparison
  56. // CHECK:STDOUT: import Core//prelude/types/bool
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT: %import_ref.1: %ImplicitAs.type.1 = import_ref Core//prelude/operators/as, inst+46, loaded [template = constants.%ImplicitAs]
  59. // CHECK:STDOUT: %import_ref.2 = import_ref Core//prelude/operators/as, inst+52, unloaded
  60. // CHECK:STDOUT: %import_ref.3: @ImplicitAs.%.1 (%.4) = import_ref Core//prelude/operators/as, inst+71, loaded [symbolic = @ImplicitAs.%.2 (constants.%.8)]
  61. // CHECK:STDOUT: %import_ref.4 = import_ref Core//prelude/operators/as, inst+64, unloaded
  62. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+64, unloaded
  63. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+64, unloaded
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: file {
  67. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  68. // CHECK:STDOUT: .Core = imports.%Core
  69. // CHECK:STDOUT: .While = %While.decl
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %Core.import = import Core
  72. // CHECK:STDOUT: %While.decl: %While.type = fn_decl @While [template = constants.%While] {} {}
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  76. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic = %Dest (constants.%Dest)]
  77. // CHECK:STDOUT: %Dest.patt: type = symbolic_binding_pattern Dest, 0 [symbolic = %Dest.patt (constants.%Dest.patt)]
  78. // CHECK:STDOUT:
  79. // CHECK:STDOUT: !definition:
  80. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %ImplicitAs.type (constants.%ImplicitAs.type.2)]
  81. // CHECK:STDOUT: %Self: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self (constants.%Self.2)]
  82. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  83. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  84. // CHECK:STDOUT: %.1: type = assoc_entity_type @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.1 (constants.%.4)]
  85. // CHECK:STDOUT: %.2: @ImplicitAs.%.1 (%.4) = assoc_entity element0, imports.%import_ref.5 [symbolic = %.2 (constants.%.5)]
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: interface {
  88. // CHECK:STDOUT: !members:
  89. // CHECK:STDOUT: .Self = imports.%import_ref.2
  90. // CHECK:STDOUT: .Convert = imports.%import_ref.3
  91. // CHECK:STDOUT: witness = (imports.%import_ref.4)
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: fn @While() {
  96. // CHECK:STDOUT: !entry:
  97. // CHECK:STDOUT: br !while.cond
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: !while.cond:
  100. // CHECK:STDOUT: %.loc18_10: String = string_literal "Hello" [template = constants.%.3]
  101. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(bool) [template = constants.%ImplicitAs.type.3]
  102. // CHECK:STDOUT: %.loc18_17.1: %.6 = specific_constant imports.%import_ref.3, @ImplicitAs(bool) [template = constants.%.7]
  103. // CHECK:STDOUT: %Convert.ref: %.6 = name_ref Convert, %.loc18_17.1 [template = constants.%.7]
  104. // CHECK:STDOUT: %.loc18_17.2: bool = converted %.loc18_10, <error> [template = <error>]
  105. // CHECK:STDOUT: if <error> br !while.body else br !while.done
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: !while.body:
  108. // CHECK:STDOUT: br !while.cond
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: !while.done:
  111. // CHECK:STDOUT: return
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%ImplicitAs.type (%ImplicitAs.type.2)) {
  115. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest, 0 [symbolic = %Dest (constants.%Dest)]
  116. // CHECK:STDOUT: %ImplicitAs.type: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %ImplicitAs.type (constants.%ImplicitAs.type.2)]
  117. // CHECK:STDOUT: %Self: %ImplicitAs.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self (constants.%Self.2)]
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn[%self.param_patt: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  123. // CHECK:STDOUT: %Dest => constants.%Dest
  124. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  128. // CHECK:STDOUT: %Dest => constants.%Dest
  129. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  133. // CHECK:STDOUT: %Dest => constants.%Dest
  134. // CHECK:STDOUT: %Dest.patt => constants.%Dest
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  138. // CHECK:STDOUT: %Dest => constants.%Dest
  139. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.2
  140. // CHECK:STDOUT: %Self => constants.%Self.1
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: specific @ImplicitAs(bool) {
  144. // CHECK:STDOUT: %Dest => bool
  145. // CHECK:STDOUT: %Dest.patt => bool
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: !definition:
  148. // CHECK:STDOUT: %ImplicitAs.type => constants.%ImplicitAs.type.3
  149. // CHECK:STDOUT: %Self => constants.%Self.2
  150. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  151. // CHECK:STDOUT: %Convert => constants.%Convert.2
  152. // CHECK:STDOUT: %.1 => constants.%.6
  153. // CHECK:STDOUT: %.2 => constants.%.7
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT: