You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for unsafe as operator to the toolchain. (#5993)
Following the direction of #5913, add support for parsing an `unsafe as`
operator. For now, we allow one additional conversion using `unsafe as`
beyond the conversions supported by `as`: we permit pointer conversions
that remove qualifiers, such as `const T*` -> `T*`.
// 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
+
13
+
// --- qualifiers.carbon
14
+
15
+
library "[[@TEST_NAME]]";
16
+
17
+
class X {}
18
+
19
+
fnConvert(p: const X*) -> X* {
20
+
//@dump-sem-ir-begin
21
+
return p unsafeasX*;
22
+
//@dump-sem-ir-end
23
+
}
24
+
25
+
// --- fail_no_conversion.carbon
26
+
27
+
library "[[@TEST_NAME]]";
28
+
29
+
class A {};
30
+
class B {};
31
+
32
+
fnConvert(a: A) -> B {
33
+
// CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+7]]:10: error: cannot convert expression of type `A` to `B` with `as` [ConversionFailure]
34
+
// CHECK:STDERR: return a unsafe as B;
35
+
// CHECK:STDERR: ^~~~~~~~~~~~~
36
+
// CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+4]]:10: note: type `A` does not implement interface `Core.UnsafeAs(B)` [MissingImplInMemberAccessNote]
// CHECK:STDERR: fail_remove_qualifiers_without_unsafe.carbon:[[@LINE+7]]:10: error: cannot convert expression of type `const X*` to `X*` with `as` [ConversionFailure]
51
+
// CHECK:STDERR: return p as X*;
52
+
// CHECK:STDERR: ^~~~~~~
53
+
// CHECK:STDERR: fail_remove_qualifiers_without_unsafe.carbon:[[@LINE+4]]:10: note: type `const X*` does not implement interface `Core.As(X*)` [MissingImplInMemberAccessNote]
54
+
// CHECK:STDERR: return p as X*;
55
+
// CHECK:STDERR: ^~~~~~~
56
+
// CHECK:STDERR:
57
+
return p asX*;
58
+
}
59
+
60
+
// CHECK:STDOUT: --- qualifiers.carbon
61
+
// CHECK:STDOUT:
62
+
// CHECK:STDOUT: constants {
63
+
// CHECK:STDOUT: %X: type = class_type @X [concrete]
64
+
// CHECK:STDOUT: %ptr.d17: type = ptr_type %X [concrete]
65
+
// CHECK:STDOUT: %const: type = const_type %X [concrete]
66
+
// CHECK:STDOUT: %ptr.cbd: type = ptr_type %const [concrete]
0 commit comments