Skip to content

Commit 41e3f95

Browse files
authored
Don't remove the forked trait for b/w compat reasons (#1842)
1 parent c6ddcee commit 41e3f95

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+469
-331
lines changed

fixtures/coverall/src/lib.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl From<InternalCoverallError> for CoverallError {
8080
}
8181
}
8282

83-
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
83+
#[derive(Clone, Debug, thiserror::Error, PartialEq, Eq)]
8484
pub enum ComplexError {
8585
#[error("OsError: {code} ({extended_code})")]
8686
OsError { code: i16, extended_code: i16 },
@@ -108,6 +108,63 @@ fn throw_complex_macro_error() -> Result<(), ComplexMacroError> {
108108
})
109109
}
110110

111+
// Note: intentionally *does not* derive `uniffi::Error`, yet ends with `Error`, just to
112+
// mess with Kotlin etc.
113+
#[derive(Clone, Debug, uniffi::Enum)]
114+
pub enum OtherError {
115+
Unexpected,
116+
}
117+
118+
#[derive(Clone, Debug, thiserror::Error, uniffi::Error)]
119+
pub enum RootError {
120+
#[error(transparent)]
121+
// XXX - note Kotlin fails if this variant was called ComplexError
122+
// (ie, the variant name can't match an existing type)
123+
Complex {
124+
#[from]
125+
error: ComplexError,
126+
},
127+
#[error("Other Error")]
128+
Other { error: OtherError },
129+
}
130+
131+
// For Kotlin, we throw a variant which itself is a plain enum.
132+
#[uniffi::export]
133+
fn throw_root_error() -> Result<(), RootError> {
134+
Err(RootError::Complex {
135+
error: ComplexError::OsError {
136+
code: 1,
137+
extended_code: 2,
138+
},
139+
})
140+
}
141+
142+
#[uniffi::export]
143+
fn get_root_error() -> RootError {
144+
RootError::Other {
145+
error: OtherError::Unexpected,
146+
}
147+
}
148+
149+
#[uniffi::export]
150+
fn get_complex_error(e: Option<ComplexError>) -> ComplexError {
151+
e.unwrap_or(ComplexError::PermissionDenied {
152+
reason: "too complex".to_string(),
153+
})
154+
}
155+
156+
#[uniffi::export]
157+
fn get_error_dict(d: Option<ErrorDict>) -> ErrorDict {
158+
d.unwrap_or(Default::default())
159+
}
160+
161+
#[derive(Default, Debug, uniffi::Record)]
162+
pub struct ErrorDict {
163+
complex_error: Option<ComplexError>,
164+
root_error: Option<RootError>,
165+
errors: Vec<RootError>,
166+
}
167+
111168
#[derive(Clone, Debug, Default)]
112169
pub struct SimpleDict {
113170
text: String,

fixtures/coverall/tests/bindings/test_coverall.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@ Coveralls("test_complex_errors").use { coveralls ->
202202
}
203203
}
204204

205+
Coveralls("test_error_values").use { _coveralls ->
206+
try {
207+
throwRootError()
208+
throw RuntimeException("Expected method to throw exception")
209+
} catch(e: RootException.Complex) {
210+
assert(e.error is ComplexException.OsException)
211+
}
212+
val e = getRootError()
213+
if (e is RootException.Other)
214+
assert(e.error == OtherError.UNEXPECTED) {
215+
} else {
216+
throw RuntimeException("Unexpected error subclass")
217+
}
218+
val ce = getComplexError(null)
219+
assert(ce is ComplexException.PermissionDenied)
220+
assert(getErrorDict(null).complexError == null)
221+
}
222+
205223
Coveralls("test_interfaces_in_dicts").use { coveralls ->
206224
coveralls.addPatch(Patch(Color.RED))
207225
coveralls.addRepair(

fixtures/coverall/tests/bindings/test_coverall.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ def test_complex_errors(self):
179179
with self.assertRaises(InternalError) as cm:
180180
coveralls.maybe_throw_complex(4)
181181

182+
def test_error_values(self):
183+
with self.assertRaises(RootError.Complex) as cm:
184+
throw_root_error()
185+
self.assertEqual(cm.exception.error.code, 1)
186+
187+
e = get_root_error()
188+
self.assertEqual(e.error, OtherError.UNEXPECTED)
189+
190+
self.assertTrue(isinstance(get_complex_error(None), ComplexError.PermissionDenied))
191+
self.assertIsNone(get_error_dict(None).complex_error)
192+
182193
def test_enums(self):
183194
e = get_simple_flat_macro_enum(0)
184195
self.assertTrue(isinstance(e, SimpleFlatMacroEnum.FIRST))

fixtures/coverall/tests/bindings/test_coverall.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,38 @@ do {
179179

180180
}
181181

182+
// Test error values, including error enums with error variants.
183+
do {
184+
do {
185+
let _ = try throwRootError()
186+
fatalError("should have thrown")
187+
} catch let e as RootError {
188+
if case let .Complex(error) = e {
189+
if case let .OsError(code, extendedCode) = error {
190+
assert(code == 1)
191+
assert(extendedCode == 2)
192+
} else {
193+
fatalError("wrong error variant: \(e)")
194+
}
195+
} else {
196+
fatalError("wrong error variant: \(e)")
197+
}
198+
}
199+
let e = getRootError();
200+
if case let .Other(error) = e {
201+
assert(error == OtherError.unexpected)
202+
} else {
203+
fatalError("wrong error variant: \(e)")
204+
}
205+
let e2 = getComplexError(e: nil);
206+
if case let .PermissionDenied(error) = e2 {
207+
assert(error == "too complex")
208+
} else {
209+
fatalError("wrong error variant: \(e)")
210+
}
211+
assert(getErrorDict(d: nil).complexError == nil)
212+
}
213+
182214
// Swift GC is deterministic, `coveralls` is freed when it goes out of scope.
183215
assert(getNumAlive() == 0);
184216

uniffi_bindgen/src/backend/types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
use super::Literal;
1111
use std::fmt::Debug;
1212

13+
// XXX - Note that this trait is not used internally. It exists just to avoid an unnecessary
14+
// breaking change for external bindings which use this trait.
15+
// It is likely to be removed some time after 0.26.x.
16+
1317
/// A Trait to help render types in a language specific format.
1418
pub trait CodeType: Debug {
1519
/// The language specific label used to reference this type. This will be used in

uniffi_bindgen/src/bindings/kotlin/gen_kotlin/callback_interface.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use crate::backend::{CodeType, Literal};
5+
use super::CodeType;
6+
use crate::ComponentInterface;
67

78
#[derive(Debug)]
89
pub struct CallbackInterfaceCodeType {
@@ -16,18 +17,14 @@ impl CallbackInterfaceCodeType {
1617
}
1718

1819
impl CodeType for CallbackInterfaceCodeType {
19-
fn type_label(&self) -> String {
20-
super::KotlinCodeOracle.class_name(&self.id)
20+
fn type_label(&self, ci: &ComponentInterface) -> String {
21+
super::KotlinCodeOracle.class_name(ci, &self.id)
2122
}
2223

2324
fn canonical_name(&self) -> String {
2425
format!("Type{}", self.id)
2526
}
2627

27-
fn literal(&self, _literal: &Literal) -> String {
28-
unreachable!();
29-
}
30-
3128
fn initialization_fn(&self) -> Option<String> {
3229
Some(format!("uniffiCallbackInterface{}.register", self.id))
3330
}

uniffi_bindgen/src/bindings/kotlin/gen_kotlin/compounds.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use crate::backend::{CodeType, Literal, Type};
5+
use super::{AsCodeType, CodeType};
6+
use crate::backend::{Literal, Type};
7+
use crate::ComponentInterface;
68
use paste::paste;
79

8-
fn render_literal(literal: &Literal, inner: &Type) -> String {
10+
fn render_literal(literal: &Literal, inner: &Type, ci: &ComponentInterface) -> String {
911
match literal {
1012
Literal::Null => "null".into(),
1113
Literal::EmptySequence => "listOf()".into(),
1214
Literal::EmptyMap => "mapOf()".into(),
1315

1416
// For optionals
15-
_ => super::KotlinCodeOracle.find(inner).literal(literal),
17+
_ => super::KotlinCodeOracle.find(inner).literal(literal, ci),
1618
}
1719
}
1820

@@ -34,16 +36,16 @@ macro_rules! impl_code_type_for_compound {
3436
}
3537

3638
impl CodeType for $T {
37-
fn type_label(&self) -> String {
38-
format!($type_label_pattern, super::KotlinCodeOracle.find(self.inner()).type_label())
39+
fn type_label(&self, ci: &ComponentInterface) -> String {
40+
format!($type_label_pattern, super::KotlinCodeOracle.find(self.inner()).type_label(ci))
3941
}
4042

4143
fn canonical_name(&self) -> String {
4244
format!($canonical_name_pattern, super::KotlinCodeOracle.find(self.inner()).canonical_name())
4345
}
4446

45-
fn literal(&self, literal: &Literal) -> String {
46-
render_literal(literal, self.inner())
47+
fn literal(&self, literal: &Literal, ci: &ComponentInterface) -> String {
48+
render_literal(literal, self.inner(), ci)
4749
}
4850
}
4951
}
@@ -74,23 +76,23 @@ impl MapCodeType {
7476
}
7577

7678
impl CodeType for MapCodeType {
77-
fn type_label(&self) -> String {
79+
fn type_label(&self, ci: &ComponentInterface) -> String {
7880
format!(
7981
"Map<{}, {}>",
80-
super::KotlinCodeOracle.find(self.key()).type_label(),
81-
super::KotlinCodeOracle.find(self.value()).type_label(),
82+
super::KotlinCodeOracle.find(self.key()).type_label(ci),
83+
super::KotlinCodeOracle.find(self.value()).type_label(ci),
8284
)
8385
}
8486

8587
fn canonical_name(&self) -> String {
8688
format!(
8789
"Map{}{}",
88-
super::KotlinCodeOracle.find(self.key()).canonical_name(),
89-
super::KotlinCodeOracle.find(self.value()).canonical_name(),
90+
self.key().as_codetype().canonical_name(),
91+
self.value().as_codetype().canonical_name(),
9092
)
9193
}
9294

93-
fn literal(&self, literal: &Literal) -> String {
94-
render_literal(literal, &self.value)
95+
fn literal(&self, literal: &Literal, ci: &ComponentInterface) -> String {
96+
render_literal(literal, &self.value, ci)
9597
}
9698
}

uniffi_bindgen/src/bindings/kotlin/gen_kotlin/custom.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use crate::backend::{CodeType, Literal};
5+
use super::CodeType;
6+
use crate::ComponentInterface;
67

78
#[derive(Debug)]
89
pub struct CustomCodeType {
@@ -16,15 +17,11 @@ impl CustomCodeType {
1617
}
1718

1819
impl CodeType for CustomCodeType {
19-
fn type_label(&self) -> String {
20+
fn type_label(&self, _ci: &ComponentInterface) -> String {
2021
self.name.clone()
2122
}
2223

2324
fn canonical_name(&self) -> String {
2425
format!("Type{}", self.name)
2526
}
26-
27-
fn literal(&self, _literal: &Literal) -> String {
28-
unreachable!("Can't have a literal of a custom type");
29-
}
3027
}

uniffi_bindgen/src/bindings/kotlin/gen_kotlin/enum_.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use crate::backend::{CodeType, Literal};
5+
use super::CodeType;
6+
use crate::backend::Literal;
7+
use crate::ComponentInterface;
68

79
#[derive(Debug)]
810
pub struct EnumCodeType {
@@ -16,19 +18,19 @@ impl EnumCodeType {
1618
}
1719

1820
impl CodeType for EnumCodeType {
19-
fn type_label(&self) -> String {
20-
super::KotlinCodeOracle.class_name(&self.id)
21+
fn type_label(&self, ci: &ComponentInterface) -> String {
22+
super::KotlinCodeOracle.class_name(ci, &self.id)
2123
}
2224

2325
fn canonical_name(&self) -> String {
2426
format!("Type{}", self.id)
2527
}
2628

27-
fn literal(&self, literal: &Literal) -> String {
29+
fn literal(&self, literal: &Literal, ci: &ComponentInterface) -> String {
2830
if let Literal::Enum(v, _) = literal {
2931
format!(
3032
"{}.{}",
31-
self.type_label(),
33+
self.type_label(ci),
3234
super::KotlinCodeOracle.enum_variant_name(v)
3335
)
3436
} else {

uniffi_bindgen/src/bindings/kotlin/gen_kotlin/error.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)