Skip to content

Commit e719bea

Browse files
ahayzen-kdabprzempore
authored andcommitted
cxx-qt-gen: have an explicit error for non public qobjects
Before this patch if you didn't mark a qobject struct as public the following error would occur. ``` [build] error[E0432]: unresolved import `super` [build] --> examples/qml_minimal/rust/src/cxxqt_object.rs:24:12 [build] | [build] 24 | struct MyObject { [build] | ^^^^^^^^ no `MyObject` in `cxxqt_object` [build] | [build] help: consider importing this type alias instead [build] | [build] 24 | struct crate::cxxqt_object::qobject::MyObject; [build] | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` After this patch the error is explicit. ``` [build] error: qobject marked structs must be public [build] --> examples/qml_minimal/rust/src/cxxqt_object.rs:24:5 [build] | [build] 24 | struct MyObject { [build] | ^^^^^^ ``` Closes KDAB#457
1 parent 7b68182 commit e719bea

File tree

7 files changed

+52
-43
lines changed

7 files changed

+52
-43
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ mod tests {
5959
#[cxx_qt::bridge]
6060
mod ffi {
6161
#[cxx_qt::qobject]
62-
struct MyObject;
62+
pub struct MyObject;
6363
}
6464
});
6565
let parser = Parser::from(module).unwrap();
@@ -76,7 +76,7 @@ mod tests {
7676
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
7777
mod ffi {
7878
#[cxx_qt::qobject]
79-
struct MyObject;
79+
pub struct MyObject;
8080
}
8181
});
8282
let parser = Parser::from(module).unwrap();
@@ -93,7 +93,7 @@ mod tests {
9393
#[cxx_qt::bridge(namespace = "cxx_qt")]
9494
mod ffi {
9595
#[cxx_qt::qobject]
96-
struct MyObject;
96+
pub struct MyObject;
9797
}
9898
});
9999
let parser = Parser::from(module).unwrap();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ mod tests {
132132
#[cxx_qt::bridge]
133133
mod ffi {
134134
#[cxx_qt::qobject]
135-
struct MyObject;
135+
pub struct MyObject;
136136
}
137137
});
138138
let parser = Parser::from(module).unwrap();
@@ -156,7 +156,7 @@ mod tests {
156156
#[cxx_qt::bridge(namespace = "cxx_qt")]
157157
mod ffi {
158158
#[cxx_qt::qobject(base = "QStringListModel")]
159-
struct MyObject;
159+
pub struct MyObject;
160160
}
161161
});
162162
let parser = Parser::from(module).unwrap();
@@ -177,7 +177,7 @@ mod tests {
177177
#[cxx_qt::bridge(namespace = "cxx_qt")]
178178
mod ffi {
179179
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_name = "MyQmlElement")]
180-
struct MyNamedObject;
180+
pub struct MyNamedObject;
181181
}
182182
});
183183
let parser = Parser::from(module).unwrap();
@@ -201,7 +201,7 @@ mod tests {
201201
#[cxx_qt::bridge(namespace = "cxx_qt")]
202202
mod ffi {
203203
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
204-
struct MyObject;
204+
pub struct MyObject;
205205
}
206206
});
207207
let parser = Parser::from(module).unwrap();
@@ -226,7 +226,7 @@ mod tests {
226226
#[cxx_qt::bridge(namespace = "cxx_qt")]
227227
mod ffi {
228228
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_uncreatable)]
229-
struct MyObject;
229+
pub struct MyObject;
230230
}
231231
});
232232
let parser = Parser::from(module).unwrap();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ mod tests {
7171
#[cxx_qt::bridge]
7272
mod ffi {
7373
#[cxx_qt::qobject]
74-
struct MyObject;
74+
pub struct MyObject;
7575
}
7676
});
7777
let parser = Parser::from(module).unwrap();
@@ -98,7 +98,7 @@ mod tests {
9898
#[cxx_qt::bridge(namespace = "cxx_qt")]
9999
mod ffi {
100100
#[cxx_qt::qobject]
101-
struct MyObject;
101+
pub struct MyObject;
102102
}
103103
});
104104
let parser = Parser::from(module).unwrap();
@@ -119,7 +119,7 @@ mod tests {
119119
use std::collections::HashMap;
120120

121121
#[cxx_qt::qobject]
122-
struct MyObject;
122+
pub struct MyObject;
123123
}
124124
});
125125
let parser = Parser::from(module).unwrap();
@@ -138,7 +138,7 @@ mod tests {
138138
#[cxx_qt::bridge(cxx_file_stem = "my_object")]
139139
mod ffi {
140140
#[cxx_qt::qobject]
141-
struct MyObject;
141+
pub struct MyObject;
142142
}
143143
});
144144
let parser = Parser::from(module).unwrap();

crates/cxx-qt-gen/src/generator/rust/qobject.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ mod tests {
195195
#[cxx_qt::bridge]
196196
mod ffi {
197197
#[cxx_qt::qobject]
198-
struct MyObject;
198+
pub struct MyObject;
199199
}
200200
});
201201
let parser = Parser::from(module).unwrap();
@@ -218,7 +218,7 @@ mod tests {
218218
#[cxx_qt::bridge(namespace = "cxx_qt")]
219219
mod ffi {
220220
#[cxx_qt::qobject]
221-
struct MyObject;
221+
pub struct MyObject;
222222
}
223223
});
224224
let parser = Parser::from(module).unwrap();
@@ -241,7 +241,7 @@ mod tests {
241241
#[cxx_qt::bridge(namespace = "cxx_qt")]
242242
mod ffi {
243243
#[cxx_qt::qobject(qml_uri = "com.kdab", qml_version = "1.0", qml_singleton)]
244-
struct MyObject;
244+
pub struct MyObject;
245245
}
246246
});
247247
let parser = Parser::from(module).unwrap();

crates/cxx-qt-gen/src/parser/cxxqtdata.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ mod tests {
330330
mod module {
331331
struct Other;
332332
#[cxx_qt::qobject]
333-
struct MyObject;
333+
pub struct MyObject;
334334
}
335335
});
336336
let result = cxx_qt_data.find_qobject_structs(&module.content.unwrap().1);
@@ -345,11 +345,11 @@ mod tests {
345345

346346
let module: ItemMod = tokens_to_syn(quote! {
347347
mod module {
348-
struct Other;
348+
pub struct Other;
349349
#[cxx_qt::qobject]
350-
struct MyObject;
350+
pub struct MyObject;
351351
#[cxx_qt::qobject]
352-
struct SecondObject;
352+
pub struct SecondObject;
353353
}
354354
});
355355
let result = cxx_qt_data.find_qobject_structs(&module.content.unwrap().1);
@@ -370,11 +370,11 @@ mod tests {
370370

371371
let module: ItemMod = tokens_to_syn(quote! {
372372
mod module {
373-
struct Other;
373+
pub struct Other;
374374
#[cxx_qt::qobject(namespace = "qobject_namespace")]
375-
struct MyObject;
375+
pub struct MyObject;
376376
#[cxx_qt::qobject]
377-
struct SecondObject;
377+
pub struct SecondObject;
378378
}
379379
});
380380
cxx_qt_data
@@ -405,8 +405,8 @@ mod tests {
405405

406406
let module: ItemMod = tokens_to_syn(quote! {
407407
mod module {
408-
struct Other;
409-
struct MyObject;
408+
pub struct Other;
409+
pub struct MyObject;
410410
}
411411
});
412412
let result = cxx_qt_data.find_qobject_structs(&module.content.unwrap().1);
@@ -477,7 +477,7 @@ mod tests {
477477

478478
let item: Item = tokens_to_syn(quote! {
479479
#[cxx_qt::qobject]
480-
struct MyObject;
480+
pub struct MyObject;
481481
});
482482
let result = cxx_qt_data.parse_cxx_qt_item(item).unwrap();
483483
assert!(result.is_none());

crates/cxx-qt-gen/src/parser/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ mod tests {
163163
#[cxx_qt::bridge(namespace = "cxx_qt")]
164164
mod ffi {
165165
#[cxx_qt::qobject]
166-
struct MyObject;
166+
pub struct MyObject;
167167

168168
#[cxx_qt::qsignals(MyObject)]
169169
enum MySignals {
@@ -188,7 +188,7 @@ mod tests {
188188
#[cxx_qt::bridge]
189189
mod ffi {
190190
#[cxx_qt::qobject]
191-
struct MyObject;
191+
pub struct MyObject;
192192

193193
#[cxx_qt::qsignals(MyObject)]
194194
enum MySignals {
@@ -217,7 +217,7 @@ mod tests {
217217
#[cxx_qt::bridge]
218218
mod ffi {
219219
#[cxx_qt::qobject]
220-
struct MyObject;
220+
pub struct MyObject;
221221

222222
#[cxx_qt::qsignals(UnknownObj)]
223223
enum MySignals {

crates/cxx-qt-gen/src/parser/qobject.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::syntax::{
1515
};
1616
use syn::{
1717
spanned::Spanned, Error, Fields, Ident, ImplItem, ImplItemMethod, Item, ItemStruct, LitStr,
18-
Result,
18+
Result, Visibility,
1919
};
2020

2121
/// Metadata for registering QML element
@@ -94,6 +94,15 @@ impl ParsedQObject {
9494
// and remove the #[qproperty] attribute
9595
let (properties, fields) = Self::parse_struct_fields(&mut qobject_struct.fields)?;
9696

97+
// Ensure that the QObject is marked as pub otherwise the error is non obvious
98+
// https://github.com/KDAB/cxx-qt/issues/457
99+
if !matches!(qobject_struct.vis, Visibility::Public(..)) {
100+
return Err(Error::new(
101+
qobject_struct.span(),
102+
"qobject marked structs must be public",
103+
));
104+
}
105+
97106
Ok(Self {
98107
base_class,
99108
qobject_struct,
@@ -259,7 +268,7 @@ pub mod tests {
259268
pub fn create_parsed_qobject() -> ParsedQObject {
260269
let qobject_struct: ItemStruct = tokens_to_syn(quote! {
261270
#[cxx_qt::qobject]
262-
struct MyObject;
271+
pub struct MyObject;
263272
});
264273
ParsedQObject::from_struct(&qobject_struct, 0).unwrap()
265274
}
@@ -268,7 +277,7 @@ pub mod tests {
268277
fn test_from_struct_no_base_class() {
269278
let qobject_struct: ItemStruct = tokens_to_syn(quote! {
270279
#[cxx_qt::qobject]
271-
struct MyObject;
280+
pub struct MyObject;
272281
});
273282

274283
let qobject = ParsedQObject::from_struct(&qobject_struct, 0).unwrap();
@@ -280,7 +289,7 @@ pub mod tests {
280289
fn test_from_struct_base_class() {
281290
let qobject_struct: ItemStruct = tokens_to_syn(quote! {
282291
#[cxx_qt::qobject(base = "QStringListModel")]
283-
struct MyObject;
292+
pub struct MyObject;
284293
});
285294

286295
let qobject = ParsedQObject::from_struct(&qobject_struct, 0).unwrap();
@@ -291,7 +300,7 @@ pub mod tests {
291300
fn test_from_struct_properties_and_fields() {
292301
let qobject_struct: ItemStruct = tokens_to_syn(quote! {
293302
#[cxx_qt::qobject]
294-
struct MyObject {
303+
pub struct MyObject {
295304
#[qproperty]
296305
int_property: i32,
297306

@@ -311,7 +320,7 @@ pub mod tests {
311320
fn test_from_struct_fields() {
312321
let qobject_struct: ItemStruct = tokens_to_syn(quote! {
313322
#[cxx_qt::qobject]
314-
struct MyObject {
323+
pub struct MyObject {
315324
field: i32,
316325
}
317326
});
@@ -388,7 +397,7 @@ pub mod tests {
388397
fn test_parse_struct_fields_valid() {
389398
let item: ItemStruct = tokens_to_syn(quote! {
390399
#[cxx_qt::qobject]
391-
struct T {
400+
pub struct T {
392401
#[qproperty]
393402
f64_property: f64,
394403

@@ -423,7 +432,7 @@ pub mod tests {
423432
fn test_parse_struct_fields_invalid() {
424433
let item: ItemStruct = tokens_to_syn(quote! {
425434
#[cxx_qt::qobject]
426-
struct T(f64);
435+
pub struct T(f64);
427436
});
428437
assert!(ParsedQObject::from_struct(&item, 0).is_err());
429438
}
@@ -432,7 +441,7 @@ pub mod tests {
432441
fn test_qml_metadata() {
433442
let item: ItemStruct = tokens_to_syn(quote! {
434443
#[cxx_qt::qobject(qml_uri = "foo.bar", qml_version = "1.0")]
435-
struct MyObject;
444+
pub struct MyObject;
436445
});
437446
let qobject = ParsedQObject::from_struct(&item, 0).unwrap();
438447
assert_eq!(
@@ -452,7 +461,7 @@ pub mod tests {
452461
fn test_qml_metadata_named() {
453462
let item: ItemStruct = tokens_to_syn(quote! {
454463
#[cxx_qt::qobject(qml_uri = "foo.bar", qml_version = "1", qml_name = "MyQmlElement")]
455-
struct MyNamedObject;
464+
pub struct MyNamedObject;
456465
});
457466
let qobject = ParsedQObject::from_struct(&item, 0).unwrap();
458467
assert_eq!(
@@ -472,7 +481,7 @@ pub mod tests {
472481
fn test_qml_metadata_singleton() {
473482
let item: ItemStruct = tokens_to_syn(quote! {
474483
#[cxx_qt::qobject(qml_uri = "foo.bar", qml_version = "1", qml_singleton)]
475-
struct MyObject;
484+
pub struct MyObject;
476485
});
477486
let qobject = ParsedQObject::from_struct(&item, 0).unwrap();
478487
assert_eq!(
@@ -492,7 +501,7 @@ pub mod tests {
492501
fn test_qml_metadata_uncreatable() {
493502
let item: ItemStruct = tokens_to_syn(quote! {
494503
#[cxx_qt::qobject(qml_uri = "foo.bar", qml_version = "1", qml_uncreatable)]
495-
struct MyObject;
504+
pub struct MyObject;
496505
});
497506
let qobject = ParsedQObject::from_struct(&item, 0).unwrap();
498507
assert_eq!(
@@ -512,7 +521,7 @@ pub mod tests {
512521
fn test_qml_metadata_no_version() {
513522
let item: ItemStruct = tokens_to_syn(quote! {
514523
#[cxx_qt::qobject(qml_uri = "foo.bar")]
515-
struct MyObject;
524+
pub struct MyObject;
516525
});
517526
assert!(ParsedQObject::from_struct(&item, 0).is_err());
518527
}
@@ -521,7 +530,7 @@ pub mod tests {
521530
fn test_qml_metadata_no_uri() {
522531
let item: ItemStruct = tokens_to_syn(quote! {
523532
#[cxx_qt::qobject(qml_version = "1.0")]
524-
struct MyObject;
533+
pub struct MyObject;
525534
});
526535
assert!(ParsedQObject::from_struct(&item, 0).is_err());
527536
}
@@ -530,7 +539,7 @@ pub mod tests {
530539
fn test_qml_metadata_only_name_no_version_no_uri() {
531540
let item: ItemStruct = tokens_to_syn(quote! {
532541
#[cxx_qt::qobject(qml_name = "MyQmlElement")]
533-
struct MyObject;
542+
pub struct MyObject;
534543
});
535544
assert!(ParsedQObject::from_struct(&item, 0).is_err());
536545
}

0 commit comments

Comments
 (0)