Skip to content

Commit 5f7f58b

Browse files
committed
cxx-qt-build: do not panic for cxx_gen failures instead use Result
This allows for CXX errors to appear with a span in the macro expansion Before this change if you had a unsupported type in an invokable only the following error would occur. ``` [build] thread 'main' panicked at 'Could not generate C++ from Rust file: Syn(Error("unsupported type: QUrl"))', crates/cxx-qt-build/src/lib.rs:120:14 ``` Now with this change we have an additional error from the macro expansion. ``` [build] error: unsupported type: QUrl [build] --> examples/qml_minimal/rust/src/cxxqt_object.rs:57:35 [build] | [build] 57 | pub fn test(&self, _url: &QUrl) { [build] | ^^^^ ``` Note however this does cause loads of extra errors below but this is a side effect of the rest of the CXX macro not being expanded, this first error is the correct one though. Related to #38
1 parent 752305b commit 5f7f58b

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

crates/cxx-qt-build/src/lib.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ use cxx_qt_gen::{
3434
// QObject macros and at most one "raw CXX" macro per file already. For now this remains a TODO
3535
// as to keep things simpler. We also want to able to warn users about duplicate names eventually.
3636

37+
/// We need to wrap the CXX and CXX-Qt errors in a single error type so that they
38+
/// can be returned from the generation phases. This then allows for us to not unwrap
39+
/// the CXX failures meaning that we can display the macro expansion correctly.
40+
#[derive(Debug)]
41+
enum GeneratedError {
42+
Cxx(cxx_gen::Error),
43+
CxxQt(cxx_qt_gen::Error),
44+
}
45+
46+
impl From<cxx_gen::Error> for GeneratedError {
47+
fn from(err: cxx_gen::Error) -> Self {
48+
Self::Cxx(err)
49+
}
50+
}
51+
52+
impl From<cxx_qt_gen::Error> for GeneratedError {
53+
fn from(err: cxx_qt_gen::Error) -> Self {
54+
Self::CxxQt(err)
55+
}
56+
}
57+
58+
type GeneratedResult<T> = Result<T, GeneratedError>;
59+
3760
struct GeneratedCppFilePaths {
3861
plain_cpp: PathBuf,
3962
qobject: Option<PathBuf>,
@@ -54,9 +77,9 @@ struct GeneratedCpp {
5477

5578
impl GeneratedCpp {
5679
/// Generate QObject and cxx header/source C++ file contents
57-
pub fn new(rust_file_path: impl AsRef<Path>) -> cxx_qt_gen::Result<Self> {
80+
pub fn new(rust_file_path: impl AsRef<Path>) -> GeneratedResult<Self> {
5881
let rust_file_path = rust_file_path.as_ref();
59-
let file = parse_qt_file(rust_file_path).unwrap();
82+
let file = parse_qt_file(rust_file_path)?;
6083

6184
let mut cxx_qt = None;
6285
let mut qml_metadata = Vec::new();
@@ -123,8 +146,7 @@ impl GeneratedCpp {
123146
}
124147

125148
let opt = cxx_gen::Opt::default();
126-
let cxx = cxx_gen::generate_header_and_cc(tokens, &opt)
127-
.expect("Could not generate C++ from Rust file");
149+
let cxx = cxx_gen::generate_header_and_cc(tokens, &opt)?;
128150

129151
Ok(GeneratedCpp {
130152
cxx_qt,
@@ -217,7 +239,7 @@ impl GeneratedCpp {
217239
fn generate_cxxqt_cpp_files(
218240
rs_source: &[PathBuf],
219241
header_dir: impl AsRef<Path>,
220-
) -> cxx_qt_gen::Result<Vec<GeneratedCppFilePaths>> {
242+
) -> GeneratedResult<Vec<GeneratedCppFilePaths>> {
221243
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
222244

223245
let mut generated_file_paths: Vec<GeneratedCppFilePaths> = Vec::with_capacity(rs_source.len());

0 commit comments

Comments
 (0)