fail_bad_condition.carbon 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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: type = generic_interface_type @ImplicitAs [template]
  28. // CHECK:STDOUT: %ImplicitAs: %ImplicitAs.type = struct_value () [template]
  29. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic]
  30. // CHECK:STDOUT: %.4: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic]
  31. // CHECK:STDOUT: %Self.1: @ImplicitAs.%.1 (%.4) = bind_symbolic_name Self 1 [symbolic]
  32. // CHECK:STDOUT: %Self.2: %.4 = bind_symbolic_name Self 1 [symbolic]
  33. // CHECK:STDOUT: %Convert.type.1: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic]
  34. // CHECK:STDOUT: %Convert.1: %Convert.type.1 = struct_value () [symbolic]
  35. // CHECK:STDOUT: %.5: type = assoc_entity_type %.4, %Convert.type.1 [symbolic]
  36. // CHECK:STDOUT: %.6: %.5 = assoc_entity element0, imports.%import_ref.5 [symbolic]
  37. // CHECK:STDOUT: %.7: type = interface_type @ImplicitAs, @ImplicitAs(bool) [template]
  38. // CHECK:STDOUT: %Convert.type.2: type = fn_type @Convert, @ImplicitAs(bool) [template]
  39. // CHECK:STDOUT: %Convert.2: %Convert.type.2 = struct_value () [template]
  40. // CHECK:STDOUT: %.8: type = assoc_entity_type %.7, %Convert.type.2 [template]
  41. // CHECK:STDOUT: %.9: %.8 = assoc_entity element0, imports.%import_ref.5 [template]
  42. // CHECK:STDOUT: %.10: %.5 = assoc_entity element0, imports.%import_ref.6 [symbolic]
  43. // CHECK:STDOUT: }
  44. // CHECK:STDOUT:
  45. // CHECK:STDOUT: imports {
  46. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  47. // CHECK:STDOUT: .ImplicitAs = %import_ref.1
  48. // CHECK:STDOUT: import Core//prelude
  49. // CHECK:STDOUT: import Core//prelude/operators
  50. // CHECK:STDOUT: import Core//prelude/types
  51. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  52. // CHECK:STDOUT: import Core//prelude/operators/as
  53. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  54. // CHECK:STDOUT: import Core//prelude/operators/comparison
  55. // CHECK:STDOUT: import Core//prelude/types/bool
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: %import_ref.1: %ImplicitAs.type = import_ref Core//prelude/operators/as, inst+40, loaded [template = constants.%ImplicitAs]
  58. // CHECK:STDOUT: %import_ref.2 = import_ref Core//prelude/operators/as, inst+45, unloaded
  59. // CHECK:STDOUT: %import_ref.3: @ImplicitAs.%.2 (%.5) = import_ref Core//prelude/operators/as, inst+63, loaded [symbolic = @ImplicitAs.%.3 (constants.%.10)]
  60. // CHECK:STDOUT: %import_ref.4 = import_ref Core//prelude/operators/as, inst+56, unloaded
  61. // CHECK:STDOUT: %import_ref.5 = import_ref Core//prelude/operators/as, inst+56, unloaded
  62. // CHECK:STDOUT: %import_ref.6 = import_ref Core//prelude/operators/as, inst+56, unloaded
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: file {
  66. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  67. // CHECK:STDOUT: .Core = imports.%Core
  68. // CHECK:STDOUT: .While = %While.decl
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %Core.import = import Core
  71. // CHECK:STDOUT: %While.decl: %While.type = fn_decl @While [template = constants.%While] {} {}
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: generic interface @ImplicitAs(constants.%Dest: type) {
  75. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: !definition:
  78. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.4)]
  79. // CHECK:STDOUT: %Self: %.4 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  80. // CHECK:STDOUT: %Convert.type: type = fn_type @Convert, @ImplicitAs(%Dest) [symbolic = %Convert.type (constants.%Convert.type.1)]
  81. // CHECK:STDOUT: %Convert: @ImplicitAs.%Convert.type (%Convert.type.1) = struct_value () [symbolic = %Convert (constants.%Convert.1)]
  82. // CHECK:STDOUT: %.2: type = assoc_entity_type @ImplicitAs.%.1 (%.4), @ImplicitAs.%Convert.type (%Convert.type.1) [symbolic = %.2 (constants.%.5)]
  83. // CHECK:STDOUT: %.3: @ImplicitAs.%.2 (%.5) = assoc_entity element0, imports.%import_ref.5 [symbolic = %.3 (constants.%.6)]
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: interface {
  86. // CHECK:STDOUT: !members:
  87. // CHECK:STDOUT: .Self = imports.%import_ref.2
  88. // CHECK:STDOUT: .Convert = imports.%import_ref.3
  89. // CHECK:STDOUT: witness = (imports.%import_ref.4)
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: fn @While() {
  94. // CHECK:STDOUT: !entry:
  95. // CHECK:STDOUT: br !while.cond
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: !while.cond:
  98. // CHECK:STDOUT: %.loc18_10: String = string_literal "Hello" [template = constants.%.3]
  99. // CHECK:STDOUT: %.loc18_17.1: type = interface_type @ImplicitAs, @ImplicitAs(bool) [template = constants.%.7]
  100. // CHECK:STDOUT: %.loc18_17.2: %.8 = specific_constant imports.%import_ref.3, @ImplicitAs(bool) [template = constants.%.9]
  101. // CHECK:STDOUT: %Convert.ref: %.8 = name_ref Convert, %.loc18_17.2 [template = constants.%.9]
  102. // CHECK:STDOUT: %.loc18_17.3: bool = converted %.loc18_10, <error> [template = <error>]
  103. // CHECK:STDOUT: if <error> br !while.body else br !while.done
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: !while.body:
  106. // CHECK:STDOUT: br !while.cond
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: !while.done:
  109. // CHECK:STDOUT: return
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: generic fn @Convert(constants.%Dest: type, constants.%Self.1: @ImplicitAs.%.1 (%.4)) {
  113. // CHECK:STDOUT: %Dest: type = bind_symbolic_name Dest 0 [symbolic = %Dest (constants.%Dest)]
  114. // CHECK:STDOUT: %.1: type = interface_type @ImplicitAs, @ImplicitAs(%Dest) [symbolic = %.1 (constants.%.4)]
  115. // CHECK:STDOUT: %Self: %.4 = bind_symbolic_name Self 1 [symbolic = %Self (constants.%Self.2)]
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn[%self: @Convert.%Self (%Self.2)]() -> @Convert.%Dest (%Dest);
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: specific @ImplicitAs(constants.%Dest) {
  121. // CHECK:STDOUT: %Dest => constants.%Dest
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: specific @ImplicitAs(@ImplicitAs.%Dest) {
  125. // CHECK:STDOUT: %Dest => constants.%Dest
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: specific @ImplicitAs(@Convert.%Dest) {
  129. // CHECK:STDOUT: %Dest => constants.%Dest
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: specific @Convert(constants.%Dest, constants.%Self.1) {
  133. // CHECK:STDOUT: %Dest => constants.%Dest
  134. // CHECK:STDOUT: %.1 => constants.%.4
  135. // CHECK:STDOUT: %Self => constants.%Self.1
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: specific @ImplicitAs(bool) {
  139. // CHECK:STDOUT: %Dest => bool
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: !definition:
  142. // CHECK:STDOUT: %.1 => constants.%.7
  143. // CHECK:STDOUT: %Self => constants.%Self.2
  144. // CHECK:STDOUT: %Convert.type => constants.%Convert.type.2
  145. // CHECK:STDOUT: %Convert => constants.%Convert.2
  146. // CHECK:STDOUT: %.2 => constants.%.8
  147. // CHECK:STDOUT: %.3 => constants.%.9
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: