basic.carbon 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/none.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/impl/basic.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/basic.carbon
  12. // --- basic.carbon
  13. library "[[@TEST_NAME]]";
  14. interface Simple {
  15. fn F();
  16. }
  17. class C {}
  18. //@dump-sem-ir-begin
  19. impl C as Simple {
  20. fn F() {}
  21. }
  22. //@dump-sem-ir-end
  23. // --- fail_invalid_impl.carbon
  24. library "[[@TEST_NAME]]";
  25. interface I {
  26. fn Op[self: Self]();
  27. }
  28. class C {}
  29. // This produces an invalid impl.
  30. // CHECK:STDERR: fail_invalid_impl.carbon:[[@LINE+4]]:6: error: name `Unknown` not found [NameNotFound]
  31. // CHECK:STDERR: impl Unknown as I {
  32. // CHECK:STDERR: ^~~~~~~
  33. // CHECK:STDERR:
  34. impl Unknown as I {
  35. fn Op[self: Self]() {}
  36. }
  37. // --- fail_import_invalid_impl.carbon
  38. library "[[@TEST_NAME]]";
  39. import library "invalid_impl";
  40. fn F() {
  41. // This impl doesn't exist, but it still tests that we can ignore the
  42. // `impl Unknown as I` on import.
  43. // CHECK:STDERR: fail_import_invalid_impl.carbon:[[@LINE+4]]:3: error: cannot access member of interface `I` in type `type` that does not implement that interface [MissingImplInMemberAccess]
  44. // CHECK:STDERR: C.(I.Op)();
  45. // CHECK:STDERR: ^~~~~~~~
  46. // CHECK:STDERR:
  47. C.(I.Op)();
  48. }
  49. // --- fail_conflicting_bad_impls.carbon
  50. library "[[@TEST_NAME]]";
  51. interface I {}
  52. class C(T:! type) {}
  53. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE+4]]:12: error: name `invalid` not found [NameNotFound]
  54. // CHECK:STDERR: final impl invalid as I {}
  55. // CHECK:STDERR: ^~~~~~~
  56. // CHECK:STDERR:
  57. final impl invalid as I {}
  58. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE+11]]:12: error: name `invalid` not found [NameNotFound]
  59. // CHECK:STDERR: final impl invalid as I {}
  60. // CHECK:STDERR: ^~~~~~~
  61. // CHECK:STDERR:
  62. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE+7]]:1: error: redefinition of `impl <error> as I` [ImplRedefinition]
  63. // CHECK:STDERR: final impl invalid as I {}
  64. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  65. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE-8]]:1: note: previous definition was here [ImplPreviousDefinition]
  66. // CHECK:STDERR: final impl invalid as I {}
  67. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  68. // CHECK:STDERR:
  69. final impl invalid as I {}
  70. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE+4]]:12: error: name `alsoinvalid` not found [NameNotFound]
  71. // CHECK:STDERR: final impl alsoinvalid as I {}
  72. // CHECK:STDERR: ^~~~~~~~~~~
  73. // CHECK:STDERR:
  74. final impl alsoinvalid as I {}
  75. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE+4]]:14: error: name `invalid` not found [NameNotFound]
  76. // CHECK:STDERR: final impl C(invalid) as I {}
  77. // CHECK:STDERR: ^~~~~~~
  78. // CHECK:STDERR:
  79. final impl C(invalid) as I {}
  80. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE+11]]:14: error: name `invalid` not found [NameNotFound]
  81. // CHECK:STDERR: final impl C(invalid) as I {}
  82. // CHECK:STDERR: ^~~~~~~
  83. // CHECK:STDERR:
  84. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE+7]]:1: error: redefinition of `impl <error> as I` [ImplRedefinition]
  85. // CHECK:STDERR: final impl C(invalid) as I {}
  86. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. // CHECK:STDERR: fail_conflicting_bad_impls.carbon:[[@LINE-8]]:1: note: previous definition was here [ImplPreviousDefinition]
  88. // CHECK:STDERR: final impl C(invalid) as I {}
  89. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. // CHECK:STDERR:
  91. final impl C(invalid) as I {}
  92. // CHECK:STDOUT: --- basic.carbon
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: constants {
  95. // CHECK:STDOUT: %Simple.type: type = facet_type <@Simple> [concrete]
  96. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  97. // CHECK:STDOUT: %Simple.impl_witness: <witness> = impl_witness file.%Simple.impl_witness_table [concrete]
  98. // CHECK:STDOUT: %C.as.Simple.impl.F.type: type = fn_type @C.as.Simple.impl.F [concrete]
  99. // CHECK:STDOUT: %C.as.Simple.impl.F: %C.as.Simple.impl.F.type = struct_value () [concrete]
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: file {
  103. // CHECK:STDOUT: impl_decl @C.as.Simple.impl [concrete] {} {
  104. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  105. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [concrete = constants.%Simple.type]
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %Simple.impl_witness_table = impl_witness_table (@C.as.Simple.impl.%C.as.Simple.impl.F.decl), @C.as.Simple.impl [concrete]
  108. // CHECK:STDOUT: %Simple.impl_witness: <witness> = impl_witness %Simple.impl_witness_table [concrete = constants.%Simple.impl_witness]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: impl @C.as.Simple.impl: %C.ref as %Simple.ref {
  112. // CHECK:STDOUT: %C.as.Simple.impl.F.decl: %C.as.Simple.impl.F.type = fn_decl @C.as.Simple.impl.F [concrete = constants.%C.as.Simple.impl.F] {} {}
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: !members:
  115. // CHECK:STDOUT: .F = %C.as.Simple.impl.F.decl
  116. // CHECK:STDOUT: witness = file.%Simple.impl_witness
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @C.as.Simple.impl.F() {
  120. // CHECK:STDOUT: !entry:
  121. // CHECK:STDOUT: return
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: