@@ -6,13 +6,25 @@ guide them towards the right fix.
66For example, if a user wrote the following bridge module:
77
88``` 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 ;
1018```
1119
1220We would want to emit a compile time error along the lines of:
1321
1422``` 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+ | ^^^^^^^^^^^^^^^^
1628```
1729
1830This chapter shows how to add support for compile time errors.
@@ -30,9 +42,31 @@ Here are a few example parse errors:
3042``` rust
3143// via: crates/swift-bridge-ir/src/errors/parse_error.rs
3244
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 ...
3670}
3771```
3872
@@ -42,9 +76,44 @@ Here are a few examples:
4276```` rust
4377// via: crates/swift-bridge-ir/src/errors/parse_error.rs
4478
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+ }
48117 }
49118}
50119````
0 commit comments