@@ -6,13 +6,25 @@ guide them towards the right fix.
6
6
For example, if a user wrote the following bridge module:
7
7
8
8
``` rust
9
- {{#include .. / .. / .. / .. / crates / swift - bridge - macro / tests / ui / unrecognized - opaque - type - attribute . rs: mdbook - ui - test - example }}
9
+ #[swift_bridge:: bridge]
10
+ mod ffi {
11
+ extern " Rust" {
12
+ #[swift_bridge(InvalidAttribute )]
13
+ type SomeType ;
14
+ }
15
+ }
16
+
17
+ pub struct SomeType ;
10
18
```
11
19
12
20
We would want to emit a compile time error along the lines of:
13
21
14
22
``` sh
15
- {{# include ../../../../crates/swift-bridge-macro/tests/ui/unrecognized-opaque-type-attribute.stderr}}
23
+ error: Unrecognized attribute " InvalidAttribute" .
24
+ --> tests/ui/unrecognized-opaque-type-attribute.rs:8:24
25
+ |
26
+ 8 | # [swift_bridge(InvalidAttribute)]
27
+ | ^^^^^^^^^^^^^^^^
16
28
```
17
29
18
30
This chapter shows how to add support for compile time errors.
@@ -30,9 +42,31 @@ Here are a few example parse errors:
30
42
``` rust
31
43
// via: crates/swift-bridge-ir/src/errors/parse_error.rs
32
44
33
- {{#include .. / .. / .. / .. / crates / swift - bridge - ir / src / errors / parse_error . rs: mdbook - parse - error - enum }}
34
-
35
- // ...
45
+ pub (crate ) enum ParseError {
46
+ ArgsIntoArgNotFound {
47
+ func : ForeignItemFn ,
48
+ missing_arg : Ident ,
49
+ },
50
+ /// `extern {}`
51
+ AbiNameMissing {
52
+ /// `extern {}`
53
+ /// ------
54
+ extern_token : Token! [extern ],
55
+ },
56
+ /// `extern "Foo" {}`
57
+ AbiNameInvalid {
58
+ /// `extern "Foo" {}`
59
+ /// -----
60
+ abi_name : LitStr ,
61
+ },
62
+ /// `fn foo (&self)`
63
+ /// ----
64
+ AmbiguousSelf { self_ : Receiver },
65
+ /// fn foo (bar: &Bar);
66
+ /// If Bar wasn't declared using a `type Bar` declaration.
67
+ UndeclaredType { ty : Type },
68
+
69
+ // ... snip ...
36
70
}
37
71
```
38
72
@@ -42,9 +76,44 @@ Here are a few examples:
42
76
```` rust
43
77
// via: crates/swift-bridge-ir/src/errors/parse_error.rs
44
78
45
- {{#include .. / .. / .. / .. / crates / swift - bridge - ir / src / errors / parse_error . rs: mdbook - parse - error - message }}
46
-
47
- // ...
79
+ impl Into <syn :: Error > for ParseError {
80
+ fn into (self ) -> Error {
81
+ match self {
82
+ ParseError :: AbiNameMissing {
83
+ extern_token : extern_ident ,
84
+ } => Error :: new_spanned (
85
+ extern_ident ,
86
+ format! (
87
+ r # " extern modules must have their abi set to "Rust" or "Swift".
88
+ ```
89
+ extern "Rust" {{ ... }}
90
+ extern "Swift" {{ ... }}
91
+ ```
92
+ " #
93
+ ),
94
+ ),
95
+ ParseError :: UndeclaredType { ty } => {
96
+ let ty_name = ty . to_token_stream (). to_string ();
97
+ let ty_name = ty_name . split_whitespace (). last (). unwrap ();
98
+
99
+ let message = format! (
100
+ r # " Type must be declared with `type {}`.
101
+ " # ,
102
+ ty_name
103
+ );
104
+ Error :: new_spanned (ty , message )
105
+ }
106
+ ParseError :: DeclaredBuiltInType { ty } => {
107
+ let message = format! (
108
+ r # " Type {} is already supported
109
+ " # ,
110
+ ty . to_token_stream (). to_string ()
111
+ );
112
+ Error :: new_spanned (ty , message )
113
+ }
114
+
115
+ // ... snip ...
116
+ }
48
117
}
49
118
}
50
119
````
0 commit comments