Skip to content

Commit fa38323

Browse files
LeonMatthesKDABahayzen-kdab
authored andcommitted
TypeNames: Return Result from namespace()
If the type isn't known, we shouldn't assume it to be valid. So return an "Unknown type" error instead.
1 parent e4231f2 commit fa38323

File tree

9 files changed

+163
-104
lines changed

9 files changed

+163
-104
lines changed

crates/cxx-qt-gen/src/generator/cpp/externcxxqt.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ mod tests {
5858
type MyObject;
5959

6060
#[qsignal]
61-
fn signal1(self: Pin<&mut ObjRust>);
61+
fn signal1(self: Pin<&mut MyObject>);
6262

6363
#[qsignal]
64-
fn signal2(self: Pin<&mut ObjRust>);
64+
fn signal2(self: Pin<&mut MyObject>);
6565
}
6666
})
6767
.unwrap()];
68-
let generated = generate(&blocks, &TypeNames::default()).unwrap();
68+
69+
// Unknown types
70+
assert!(generate(&blocks, &TypeNames::default()).is_err());
71+
72+
let generated = generate(&blocks, &TypeNames::mock()).unwrap();
6973
assert_eq!(generated.len(), 2);
7074
}
7175

crates/cxx-qt-gen/src/generator/cpp/property/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ mod tests {
8282
let qobject_idents = create_qobjectname();
8383

8484
let generated =
85-
generate_cpp_properties(&properties, &qobject_idents, &TypeNames::default()).unwrap();
85+
generate_cpp_properties(&properties, &qobject_idents, &TypeNames::mock()).unwrap();
8686

8787
// metaobjects
8888
assert_eq!(generated.metaobjects.len(), 2);
@@ -360,7 +360,7 @@ mod tests {
360360
}];
361361
let qobject_idents = create_qobjectname();
362362

363-
let mut type_names = TypeNames::default();
363+
let mut type_names = TypeNames::mock();
364364
type_names.insert("A", None, Some("A1"), None);
365365

366366
let generated = generate_cpp_properties(&properties, &qobject_idents, &type_names).unwrap();

crates/cxx-qt-gen/src/generator/cpp/signal.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn generate_cpp_signal(
9696

9797
// Prepare the idents
9898
let idents = QSignalName::from(signal);
99-
let idents_helper = QSignalHelperName::new(&idents, qobject_ident, type_names);
99+
let idents_helper = QSignalHelperName::new(&idents, qobject_ident, type_names)?;
100100

101101
let signal_ident = idents.name.cpp;
102102
let free_connect_ident_cpp = idents_helper.connect_name.cpp;
@@ -251,7 +251,7 @@ mod tests {
251251
let qobject_idents = create_qobjectname();
252252

253253
let generated =
254-
generate_cpp_signals(&signals, &qobject_idents, &TypeNames::default()).unwrap();
254+
generate_cpp_signals(&signals, &qobject_idents, &TypeNames::mock()).unwrap();
255255

256256
assert_eq!(generated.methods.len(), 1);
257257
let header = if let CppFragment::Header(header) = &generated.methods[0] {
@@ -349,7 +349,7 @@ mod tests {
349349
}];
350350
let qobject_idents = create_qobjectname();
351351

352-
let mut type_names = TypeNames::default();
352+
let mut type_names = TypeNames::mock();
353353
type_names.insert("A", None, Some("A1"), None);
354354

355355
let generated = generate_cpp_signals(&signals, &qobject_idents, &type_names).unwrap();
@@ -444,9 +444,8 @@ mod tests {
444444
private: false,
445445
}];
446446
let qobject_idents = create_qobjectname();
447-
448447
let generated =
449-
generate_cpp_signals(&signals, &qobject_idents, &TypeNames::default()).unwrap();
448+
generate_cpp_signals(&signals, &qobject_idents, &TypeNames::mock()).unwrap();
450449

451450
assert_eq!(generated.methods.len(), 0);
452451
assert_eq!(generated.fragments.len(), 1);
@@ -531,8 +530,9 @@ mod tests {
531530
private: false,
532531
};
533532

534-
let generated =
535-
generate_cpp_signal(&signal, &signal.qobject_ident, &TypeNames::default()).unwrap();
533+
let mut type_names = TypeNames::default();
534+
type_names.insert("ObjRust", None, None, None);
535+
let generated = generate_cpp_signal(&signal, &signal.qobject_ident, &type_names).unwrap();
536536

537537
assert_eq!(generated.methods.len(), 0);
538538

crates/cxx-qt-gen/src/generator/naming/signals.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::parser::signals::ParsedSignal;
66
use crate::{generator::naming::CombinedIdent, naming::TypeNames};
77
use convert_case::{Case, Casing};
88
use quote::format_ident;
9-
use syn::Ident;
9+
use syn::{Ident, Result};
1010

1111
/// Names for parts of a Q_SIGNAL
1212
pub struct QSignalName {
@@ -52,7 +52,11 @@ pub struct QSignalHelperName {
5252
}
5353

5454
impl QSignalHelperName {
55-
pub fn new(idents: &QSignalName, qobject_ident: &Ident, type_names: &TypeNames) -> Self {
55+
pub fn new(
56+
idents: &QSignalName,
57+
qobject_ident: &Ident,
58+
type_names: &TypeNames,
59+
) -> Result<Self> {
5660
let signal_ident = &idents.name.cpp;
5761
let handler_alias = format_ident!("{qobject_ident}CxxQtSignalHandler{signal_ident}");
5862
let namespace = {
@@ -67,7 +71,7 @@ impl QSignalHelperName {
6771
//
6872
// See the comment on TypeNames::cxx_qualified for why fully qualifying is
6973
// unfortunately not possible.
70-
let qobject_namespace = type_names.namespace(qobject_ident);
74+
let qobject_namespace = type_names.namespace(qobject_ident)?;
7175
let namespace: Vec<_> = qobject_namespace
7276
.into_iter()
7377
.chain(vec!["rust::cxxqtgen1".to_owned()])
@@ -78,7 +82,7 @@ impl QSignalHelperName {
7882

7983
// TODO: in the future we might improve the naming of the methods
8084
// to avoid collisions (maybe use a separator similar to how CXX uses $?)
81-
Self {
85+
Ok(Self {
8286
connect_name: CombinedIdent {
8387
cpp: format_ident!("{}_{}", qobject_ident, idents.connect_name.cpp),
8488
rust: format_ident!("{}_{}", qobject_ident, idents.connect_name.rust),
@@ -90,7 +94,7 @@ impl QSignalHelperName {
9094
struct_param: format_ident!("{qobject_ident}CxxQtSignalParams{signal_ident}"),
9195
namespace,
9296
handler_alias,
93-
}
97+
})
9498
}
9599
}
96100

crates/cxx-qt-gen/src/generator/rust/property/mod.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ mod tests {
9292
let generated = generate_rust_properties(
9393
&properties,
9494
&qobject_idents,
95-
&TypeNames::default(),
95+
&TypeNames::mock(),
9696
&format_ident!("ffi"),
9797
)
9898
.unwrap();
@@ -116,7 +116,7 @@ mod tests {
116116
assert_tokens_eq(
117117
&generated.cxx_qt_mod_contents[0],
118118
parse_quote! {
119-
impl MyObject {
119+
impl qobject::MyObject {
120120
#[doc = "Getter for the Q_PROPERTY "]
121121
#[doc = "trivial_property"]
122122
pub fn trivial_property(&self) -> &i32 {
@@ -139,7 +139,7 @@ mod tests {
139139
assert_tokens_eq(
140140
&generated.cxx_qt_mod_contents[1],
141141
parse_quote! {
142-
impl MyObject {
142+
impl qobject::MyObject {
143143
#[doc = "Setter for the Q_PROPERTY "]
144144
#[doc = "trivial_property"]
145145
pub fn set_trivial_property(mut self: core::pin::Pin<&mut Self>, value: i32) {
@@ -169,7 +169,7 @@ mod tests {
169169
assert_tokens_eq(
170170
&generated.cxx_qt_mod_contents[2],
171171
parse_quote! {
172-
impl MyObject {
172+
impl qobject::MyObject {
173173
#[doc = "Getter for the Q_PROPERTY "]
174174
#[doc = "opaque_property"]
175175
pub fn opaque_property(&self) -> &cxx::UniquePtr<QColor> {
@@ -192,7 +192,7 @@ mod tests {
192192
assert_tokens_eq(
193193
&generated.cxx_qt_mod_contents[3],
194194
parse_quote! {
195-
impl MyObject {
195+
impl qobject::MyObject {
196196
#[doc = "Setter for the Q_PROPERTY "]
197197
#[doc = "opaque_property"]
198198
pub fn set_opaque_property(mut self: core::pin::Pin<&mut Self>, value: cxx::UniquePtr<QColor>) {
@@ -222,7 +222,7 @@ mod tests {
222222
assert_tokens_eq(
223223
&generated.cxx_qt_mod_contents[4],
224224
parse_quote! {
225-
impl MyObject {
225+
impl qobject::MyObject {
226226
#[doc = "Getter for the Q_PROPERTY "]
227227
#[doc = "unsafe_property"]
228228
pub fn unsafe_property(&self) -> &*mut T {
@@ -245,7 +245,7 @@ mod tests {
245245
assert_tokens_eq(
246246
&generated.cxx_qt_mod_contents[5],
247247
parse_quote! {
248-
impl MyObject {
248+
impl qobject::MyObject {
249249
#[doc = "Setter for the Q_PROPERTY "]
250250
#[doc = "unsafe_property"]
251251
pub fn set_unsafe_property(mut self: core::pin::Pin<&mut Self>, value: *mut T) {
@@ -307,11 +307,11 @@ mod tests {
307307
assert_tokens_eq(
308308
&generated.cxx_qt_mod_contents[6],
309309
parse_quote! {
310-
impl MyObject {
310+
impl qobject::MyObject {
311311
#[doc = "Connect the given function pointer to the signal "]
312312
#[doc = "trivialPropertyChanged"]
313313
#[doc = ", so that when the signal is emitted the function pointer is executed."]
314-
pub fn connect_trivial_property_changed<F: FnMut(core::pin::Pin<&mut MyObject>, ) + 'static>(self: core::pin::Pin<&mut MyObject>, mut closure: F, conn_type: cxx_qt::ConnectionType) -> cxx_qt::QMetaObjectConnectionGuard
314+
pub fn connect_trivial_property_changed<F: FnMut(core::pin::Pin<&mut qobject::MyObject>, ) + 'static>(self: core::pin::Pin<&mut qobject::MyObject>, mut closure: F, conn_type: cxx_qt::ConnectionType) -> cxx_qt::QMetaObjectConnectionGuard
315315
{
316316
cxx_qt::QMetaObjectConnectionGuard::from(ffi::MyObject_connect_trivial_property_changed(
317317
self,
@@ -325,13 +325,13 @@ mod tests {
325325
assert_tokens_eq(
326326
&generated.cxx_qt_mod_contents[7],
327327
parse_quote! {
328-
impl MyObject {
328+
impl qobject::MyObject {
329329
#[doc = "Connect the given function pointer to the signal "]
330330
#[doc = "trivialPropertyChanged"]
331331
#[doc = ", so that when the signal is emitted the function pointer is executed."]
332332
#[doc = "\n"]
333333
#[doc = "Note that this method uses a AutoConnection connection type."]
334-
pub fn on_trivial_property_changed<F: FnMut(core::pin::Pin<&mut MyObject>, ) + 'static>(self: core::pin::Pin<&mut MyObject>, mut closure: F) -> cxx_qt::QMetaObjectConnectionGuard
334+
pub fn on_trivial_property_changed<F: FnMut(core::pin::Pin<&mut qobject::MyObject>, ) + 'static>(self: core::pin::Pin<&mut qobject::MyObject>, mut closure: F) -> cxx_qt::QMetaObjectConnectionGuard
335335
{
336336
cxx_qt::QMetaObjectConnectionGuard::from(ffi::MyObject_connect_trivial_property_changed(
337337
self,
@@ -354,7 +354,7 @@ mod tests {
354354
parse_quote! {
355355
impl cxx_qt::signalhandler::CxxQtSignalHandlerClosure for MyObjectCxxQtSignalClosuretrivialPropertyChanged {
356356
type Id = cxx::type_id!("::rust::cxxqtgen1::MyObjectCxxQtSignalHandlertrivialPropertyChanged");
357-
type FnType = dyn FnMut(core::pin::Pin<&mut MyObject>, );
357+
type FnType = dyn FnMut(core::pin::Pin<&mut qobject::MyObject>, );
358358
}
359359
},
360360
);
@@ -369,7 +369,7 @@ mod tests {
369369
parse_quote! {
370370
fn call_MyObject_signal_handler_trivialPropertyChanged(
371371
handler: &mut cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosuretrivialPropertyChanged>,
372-
self_value: core::pin::Pin<&mut MyObject>,
372+
self_value: core::pin::Pin<&mut qobject::MyObject>,
373373
) {
374374
handler.closure()(self_value, );
375375
}
@@ -433,11 +433,11 @@ mod tests {
433433
assert_tokens_eq(
434434
&generated.cxx_qt_mod_contents[14],
435435
parse_quote! {
436-
impl MyObject {
436+
impl qobject::MyObject {
437437
#[doc = "Connect the given function pointer to the signal "]
438438
#[doc = "opaquePropertyChanged"]
439439
#[doc = ", so that when the signal is emitted the function pointer is executed."]
440-
pub fn connect_opaque_property_changed<F: FnMut(core::pin::Pin<&mut MyObject>, ) + 'static>(self: core::pin::Pin<&mut MyObject>, mut closure: F, conn_type: cxx_qt::ConnectionType) -> cxx_qt::QMetaObjectConnectionGuard
440+
pub fn connect_opaque_property_changed<F: FnMut(core::pin::Pin<&mut qobject::MyObject>, ) + 'static>(self: core::pin::Pin<&mut qobject::MyObject>, mut closure: F, conn_type: cxx_qt::ConnectionType) -> cxx_qt::QMetaObjectConnectionGuard
441441
{
442442
cxx_qt::QMetaObjectConnectionGuard::from(ffi::MyObject_connect_opaque_property_changed(
443443
self,
@@ -451,13 +451,13 @@ mod tests {
451451
assert_tokens_eq(
452452
&generated.cxx_qt_mod_contents[15],
453453
parse_quote! {
454-
impl MyObject {
454+
impl qobject::MyObject {
455455
#[doc = "Connect the given function pointer to the signal "]
456456
#[doc = "opaquePropertyChanged"]
457457
#[doc = ", so that when the signal is emitted the function pointer is executed."]
458458
#[doc = "\n"]
459459
#[doc = "Note that this method uses a AutoConnection connection type."]
460-
pub fn on_opaque_property_changed<F: FnMut(core::pin::Pin<&mut MyObject>, ) + 'static>(self: core::pin::Pin<&mut MyObject>, mut closure: F) -> cxx_qt::QMetaObjectConnectionGuard
460+
pub fn on_opaque_property_changed<F: FnMut(core::pin::Pin<&mut qobject::MyObject>, ) + 'static>(self: core::pin::Pin<&mut qobject::MyObject>, mut closure: F) -> cxx_qt::QMetaObjectConnectionGuard
461461
{
462462
cxx_qt::QMetaObjectConnectionGuard::from(ffi::MyObject_connect_opaque_property_changed(
463463
self,
@@ -480,7 +480,7 @@ mod tests {
480480
parse_quote! {
481481
impl cxx_qt::signalhandler::CxxQtSignalHandlerClosure for MyObjectCxxQtSignalClosureopaquePropertyChanged {
482482
type Id = cxx::type_id!("::rust::cxxqtgen1::MyObjectCxxQtSignalHandleropaquePropertyChanged");
483-
type FnType = dyn FnMut(core::pin::Pin<&mut MyObject>, );
483+
type FnType = dyn FnMut(core::pin::Pin<&mut qobject::MyObject>, );
484484
}
485485
},
486486
);
@@ -495,7 +495,7 @@ mod tests {
495495
parse_quote! {
496496
fn call_MyObject_signal_handler_opaquePropertyChanged(
497497
handler: &mut cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosureopaquePropertyChanged>,
498-
self_value: core::pin::Pin<&mut MyObject>,
498+
self_value: core::pin::Pin<&mut qobject::MyObject>,
499499
) {
500500
handler.closure()(self_value, );
501501
}
@@ -559,11 +559,11 @@ mod tests {
559559
assert_tokens_eq(
560560
&generated.cxx_qt_mod_contents[22],
561561
parse_quote! {
562-
impl MyObject {
562+
impl qobject::MyObject {
563563
#[doc = "Connect the given function pointer to the signal "]
564564
#[doc = "unsafePropertyChanged"]
565565
#[doc = ", so that when the signal is emitted the function pointer is executed."]
566-
pub fn connect_unsafe_property_changed<F: FnMut(core::pin::Pin<&mut MyObject>, ) + 'static>(self: core::pin::Pin<&mut MyObject>, mut closure: F, conn_type: cxx_qt::ConnectionType) -> cxx_qt::QMetaObjectConnectionGuard
566+
pub fn connect_unsafe_property_changed<F: FnMut(core::pin::Pin<&mut qobject::MyObject>, ) + 'static>(self: core::pin::Pin<&mut qobject::MyObject>, mut closure: F, conn_type: cxx_qt::ConnectionType) -> cxx_qt::QMetaObjectConnectionGuard
567567
{
568568
cxx_qt::QMetaObjectConnectionGuard::from(ffi::MyObject_connect_unsafe_property_changed(
569569
self,
@@ -577,13 +577,13 @@ mod tests {
577577
assert_tokens_eq(
578578
&generated.cxx_qt_mod_contents[23],
579579
parse_quote! {
580-
impl MyObject {
580+
impl qobject::MyObject {
581581
#[doc = "Connect the given function pointer to the signal "]
582582
#[doc = "unsafePropertyChanged"]
583583
#[doc = ", so that when the signal is emitted the function pointer is executed."]
584584
#[doc = "\n"]
585585
#[doc = "Note that this method uses a AutoConnection connection type."]
586-
pub fn on_unsafe_property_changed<F: FnMut(core::pin::Pin<&mut MyObject>, ) + 'static>(self: core::pin::Pin<&mut MyObject>, mut closure: F) -> cxx_qt::QMetaObjectConnectionGuard
586+
pub fn on_unsafe_property_changed<F: FnMut(core::pin::Pin<&mut qobject::MyObject>, ) + 'static>(self: core::pin::Pin<&mut qobject::MyObject>, mut closure: F) -> cxx_qt::QMetaObjectConnectionGuard
587587
{
588588
cxx_qt::QMetaObjectConnectionGuard::from(ffi::MyObject_connect_unsafe_property_changed(
589589
self,
@@ -606,7 +606,7 @@ mod tests {
606606
parse_quote! {
607607
impl cxx_qt::signalhandler::CxxQtSignalHandlerClosure for MyObjectCxxQtSignalClosureunsafePropertyChanged {
608608
type Id = cxx::type_id!("::rust::cxxqtgen1::MyObjectCxxQtSignalHandlerunsafePropertyChanged");
609-
type FnType = dyn FnMut(core::pin::Pin<&mut MyObject>, );
609+
type FnType = dyn FnMut(core::pin::Pin<&mut qobject::MyObject>, );
610610
}
611611
},
612612
);
@@ -621,7 +621,7 @@ mod tests {
621621
parse_quote! {
622622
fn call_MyObject_signal_handler_unsafePropertyChanged(
623623
handler: &mut cxx_qt::signalhandler::CxxQtSignalHandler<MyObjectCxxQtSignalClosureunsafePropertyChanged>,
624-
self_value: core::pin::Pin<&mut MyObject>,
624+
self_value: core::pin::Pin<&mut qobject::MyObject>,
625625
) {
626626
handler.closure()(self_value, );
627627
}

0 commit comments

Comments
 (0)