xref: /third_party/rust/crates/cxx/syntax/error.rs (revision 33d722a9)
1use std::fmt::{self, Display};
2
3#[derive(Copy, Clone)]
4pub struct Error {
5    pub msg: &'static str,
6    pub label: Option<&'static str>,
7    pub note: Option<&'static str>,
8}
9
10impl Display for Error {
11    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
12        self.msg.fmt(formatter)
13    }
14}
15
16pub static ERRORS: &[Error] = &[
17    BOX_CXX_TYPE,
18    CXXBRIDGE_RESERVED,
19    CXX_STRING_BY_VALUE,
20    CXX_TYPE_BY_VALUE,
21    DISCRIMINANT_OVERFLOW,
22    DOT_INCLUDE,
23    DOUBLE_UNDERSCORE,
24    RESERVED_LIFETIME,
25    RUST_TYPE_BY_VALUE,
26    UNSUPPORTED_TYPE,
27    USE_NOT_ALLOWED,
28];
29
30pub static BOX_CXX_TYPE: Error = Error {
31    msg: "Box of a C++ type is not supported yet",
32    label: None,
33    note: Some("hint: use UniquePtr<> or SharedPtr<>"),
34};
35
36pub static CXXBRIDGE_RESERVED: Error = Error {
37    msg: "identifiers starting with cxxbridge are reserved",
38    label: Some("reserved identifier"),
39    note: Some("identifiers starting with cxxbridge are reserved"),
40};
41
42pub static CXX_STRING_BY_VALUE: Error = Error {
43    msg: "C++ string by value is not supported",
44    label: None,
45    note: Some("hint: wrap it in a UniquePtr<>"),
46};
47
48pub static CXX_TYPE_BY_VALUE: Error = Error {
49    msg: "C++ type by value is not supported",
50    label: None,
51    note: Some("hint: wrap it in a UniquePtr<> or SharedPtr<>"),
52};
53
54pub static DISCRIMINANT_OVERFLOW: Error = Error {
55    msg: "discriminant overflow on value after ",
56    label: Some("discriminant overflow"),
57    note: Some("note: explicitly set `= 0` if that is desired outcome"),
58};
59
60pub static DOT_INCLUDE: Error = Error {
61    msg: "#include relative to `.` or `..` is not supported in Cargo builds",
62    label: Some("#include relative to `.` or `..` is not supported in Cargo builds"),
63    note: Some("note: use a path starting with the crate name"),
64};
65
66pub static DOUBLE_UNDERSCORE: Error = Error {
67    msg: "identifiers containing double underscore are reserved in C++",
68    label: Some("reserved identifier"),
69    note: Some("identifiers containing double underscore are reserved in C++"),
70};
71
72pub static RESERVED_LIFETIME: Error = Error {
73    msg: "invalid lifetime parameter name: `'static`",
74    label: Some("'static is a reserved lifetime name"),
75    note: None,
76};
77
78pub static RUST_TYPE_BY_VALUE: Error = Error {
79    msg: "opaque Rust type by value is not supported",
80    label: None,
81    note: Some("hint: wrap it in a Box<>"),
82};
83
84pub static UNSUPPORTED_TYPE: Error = Error {
85    msg: "unsupported type: ",
86    label: Some("unsupported type"),
87    note: None,
88};
89
90pub static USE_NOT_ALLOWED: Error = Error {
91    msg: "`use` items are not allowed within cxx bridge",
92    label: Some("not allowed"),
93    note: Some(
94        "`use` items are not allowed within cxx bridge; only types defined\n\
95         within your bridge, primitive types, or types exported by the cxx\n\
96         crate may be used",
97    ),
98};
99