basic.carbon 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // CHECK:STDOUT: --- basic.carbon
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: constants {
  52. // CHECK:STDOUT: %Simple.type: type = facet_type <@Simple> [concrete]
  53. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  54. // CHECK:STDOUT: %Simple.impl_witness: <witness> = impl_witness file.%Simple.impl_witness_table [concrete]
  55. // CHECK:STDOUT: %C.as.Simple.impl.F.type: type = fn_type @C.as.Simple.impl.F [concrete]
  56. // CHECK:STDOUT: %C.as.Simple.impl.F: %C.as.Simple.impl.F.type = struct_value () [concrete]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: impl_decl @C.as.Simple.impl [concrete] {} {
  61. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  62. // CHECK:STDOUT: %Simple.ref: type = name_ref Simple, file.%Simple.decl [concrete = constants.%Simple.type]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Simple.impl_witness_table = impl_witness_table (@C.as.Simple.impl.%C.as.Simple.impl.F.decl), @C.as.Simple.impl [concrete]
  65. // CHECK:STDOUT: %Simple.impl_witness: <witness> = impl_witness %Simple.impl_witness_table [concrete = constants.%Simple.impl_witness]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: impl @C.as.Simple.impl: %C.ref as %Simple.ref {
  69. // 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] {} {}
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: !members:
  72. // CHECK:STDOUT: .F = %C.as.Simple.impl.F.decl
  73. // CHECK:STDOUT: witness = file.%Simple.impl_witness
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: fn @C.as.Simple.impl.F() {
  77. // CHECK:STDOUT: !entry:
  78. // CHECK:STDOUT: return
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: