unsafe_as.carbon 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/convert.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/as/unsafe_as.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/as/unsafe_as.carbon
  12. // --- qualifiers.carbon
  13. library "[[@TEST_NAME]]";
  14. class X {}
  15. fn Convert(p: const X*) -> X* {
  16. //@dump-sem-ir-begin
  17. return p unsafe as X*;
  18. //@dump-sem-ir-end
  19. }
  20. // --- fail_no_conversion.carbon
  21. library "[[@TEST_NAME]]";
  22. class A {};
  23. class B {};
  24. fn Convert(a: A) -> B {
  25. // CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+7]]:10: error: cannot convert expression of type `A` to `B` with `as` [ConversionFailure]
  26. // CHECK:STDERR: return a unsafe as B;
  27. // CHECK:STDERR: ^~~~~~~~~~~~~
  28. // CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+4]]:10: note: type `A` does not implement interface `Core.UnsafeAs(B)` [MissingImplInMemberAccessNote]
  29. // CHECK:STDERR: return a unsafe as B;
  30. // CHECK:STDERR: ^~~~~~~~~~~~~
  31. // CHECK:STDERR:
  32. return a unsafe as B;
  33. }
  34. // --- fail_remove_qualifiers_without_unsafe.carbon
  35. library "[[@TEST_NAME]]";
  36. class X {}
  37. fn Convert(p: const X*) -> X* {
  38. // CHECK:STDERR: fail_remove_qualifiers_without_unsafe.carbon:[[@LINE+7]]:10: error: cannot convert expression of type `const X*` to `X*` with `as` [ConversionFailure]
  39. // CHECK:STDERR: return p as X*;
  40. // CHECK:STDERR: ^~~~~~~
  41. // CHECK:STDERR: fail_remove_qualifiers_without_unsafe.carbon:[[@LINE+4]]:10: note: type `const X*` does not implement interface `Core.As(X*)` [MissingImplInMemberAccessNote]
  42. // CHECK:STDERR: return p as X*;
  43. // CHECK:STDERR: ^~~~~~~
  44. // CHECK:STDERR:
  45. return p as X*;
  46. }
  47. // CHECK:STDOUT: --- qualifiers.carbon
  48. // CHECK:STDOUT:
  49. // CHECK:STDOUT: constants {
  50. // CHECK:STDOUT: %X: type = class_type @X [concrete]
  51. // CHECK:STDOUT: %ptr.d17: type = ptr_type %X [concrete]
  52. // CHECK:STDOUT: %const: type = const_type %X [concrete]
  53. // CHECK:STDOUT: %ptr.cbd: type = ptr_type %const [concrete]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: fn @Convert(%p.param: %ptr.cbd) -> %ptr.d17 {
  60. // CHECK:STDOUT: !entry:
  61. // CHECK:STDOUT: %p.ref: %ptr.cbd = name_ref p, %p
  62. // CHECK:STDOUT: %X.ref.loc8: type = name_ref X, file.%X.decl [concrete = constants.%X]
  63. // CHECK:STDOUT: %ptr.loc8: type = ptr_type %X.ref.loc8 [concrete = constants.%ptr.d17]
  64. // CHECK:STDOUT: %.loc8_19.1: %ptr.d17 = as_compatible %p.ref
  65. // CHECK:STDOUT: %.loc8_19.2: %ptr.d17 = converted %p.ref, %.loc8_19.1
  66. // CHECK:STDOUT: return %.loc8_19.2
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: