Skip to content

Commit 48e725b

Browse files
committed
Spot atoms in one localized place
1 parent c59a402 commit 48e725b

File tree

7 files changed

+38
-30
lines changed

7 files changed

+38
-30
lines changed

gen/src/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) {
204204

205205
for ty in out.types {
206206
match ty {
207-
Type::Ident(ident) => match Atom::from(&ident.rust) {
207+
Type::Ident(ident) => match out.types.builtins.get(&ident.rust) {
208208
Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32)
209209
| Some(I64) => out.include.cstdint = true,
210210
Some(Usize) => out.include.cstddef = true,
@@ -1191,8 +1191,8 @@ fn write_extern_arg(out: &mut OutFile, arg: &Var) {
11911191

11921192
fn write_type(out: &mut OutFile, ty: &Type) {
11931193
match ty {
1194-
Type::Ident(ident) => match Atom::from(&ident.rust) {
1195-
Some(atom) => write_atom(out, atom),
1194+
Type::Ident(ident) => match out.types.builtins.get(&ident.rust) {
1195+
Some(&atom) => write_atom(out, atom),
11961196
None => write!(
11971197
out,
11981198
"{}",

syntax/check.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::syntax::atom::Atom::{self, *};
1+
use crate::syntax::atom::Atom::*;
22
use crate::syntax::report::Errors;
33
use crate::syntax::visit::{self, Visit};
44
use crate::syntax::{
@@ -81,7 +81,7 @@ impl Check<'_> {
8181

8282
fn check_type_ident(cx: &mut Check, name: &NamedType) {
8383
let ident = &name.rust;
84-
if Atom::from(ident).is_none()
84+
if cx.types.builtins.get(ident).is_none()
8585
&& !cx.types.structs.contains_key(ident)
8686
&& !cx.types.enums.contains_key(ident)
8787
&& !cx.types.cxx.contains(ident)
@@ -102,7 +102,7 @@ fn check_type_box(cx: &mut Check, ptr: &Ty1) {
102102
cx.error(ptr, error::BOX_CXX_TYPE.msg);
103103
}
104104

105-
if Atom::from(&ident.rust).is_none() {
105+
if cx.types.builtins.get(&ident.rust).is_none() {
106106
return;
107107
}
108108
}
@@ -122,7 +122,7 @@ fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) {
122122
return;
123123
}
124124

125-
match Atom::from(&ident.rust) {
125+
match cx.types.builtins.get(&ident.rust) {
126126
None | Some(Char) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
127127
| Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
128128
| Some(F64) | Some(RustString) => return,
@@ -144,7 +144,7 @@ fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) {
144144
return;
145145
}
146146

147-
match Atom::from(&ident.rust) {
147+
match cx.types.builtins.get(&ident.rust) {
148148
None | Some(CxxString) => return,
149149
_ => {}
150150
}
@@ -162,7 +162,7 @@ fn check_type_shared_ptr(cx: &mut Check, ptr: &Ty1) {
162162
return;
163163
}
164164

165-
match Atom::from(&ident.rust) {
165+
match cx.types.builtins.get(&ident.rust) {
166166
None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
167167
| Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
168168
| Some(F64) | Some(CxxString) => return,
@@ -183,7 +183,7 @@ fn check_type_weak_ptr(cx: &mut Check, ptr: &Ty1) {
183183
return;
184184
}
185185

186-
match Atom::from(&ident.rust) {
186+
match cx.types.builtins.get(&ident.rust) {
187187
None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize)
188188
| Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32)
189189
| Some(F64) | Some(CxxString) => return,
@@ -207,7 +207,7 @@ fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) {
207207
return;
208208
}
209209

210-
match Atom::from(&ident.rust) {
210+
match cx.types.builtins.get(&ident.rust) {
211211
None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8)
212212
| Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64)
213213
| Some(CxxString) => return,
@@ -522,7 +522,7 @@ fn check_api_impl(cx: &mut Check, imp: &Impl) {
522522
| Type::WeakPtr(ty)
523523
| Type::CxxVector(ty) => {
524524
if let Type::Ident(inner) = &ty.inner {
525-
if Atom::from(&inner.rust).is_none() {
525+
if cx.types.builtins.get(&inner.rust).is_none() {
526526
return;
527527
}
528528
}
@@ -568,7 +568,7 @@ fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) {
568568
self.found |= match ty {
569569
Type::Ref(ty) => ty.mutable,
570570
Type::SliceRef(slice) => slice.mutable,
571-
Type::Ident(ident) if Atom::from(&ident.rust).is_none() => {
571+
Type::Ident(ident) if self.cx.types.builtins.get(&ident.rust).is_none() => {
572572
match self.cx.types.try_resolve(ident) {
573573
Some(resolve) => !resolve.generics.lifetimes.is_empty(),
574574
None => true,
@@ -604,7 +604,7 @@ fn check_reserved_name(cx: &mut Check, ident: &Ident) {
604604
|| ident == "Vec"
605605
|| ident == "CxxVector"
606606
|| ident == "str"
607-
|| Atom::from(ident).is_some()
607+
|| cx.types.builtins.get(ident).is_some()
608608
{
609609
cx.error(ident, "reserved name");
610610
}
@@ -709,9 +709,9 @@ fn describe(cx: &mut Check, ty: &Type) -> String {
709709
"opaque C++ type".to_owned()
710710
} else if cx.types.rust.contains(&ident.rust) {
711711
"opaque Rust type".to_owned()
712-
} else if Atom::from(&ident.rust) == Some(CxxString) {
712+
} else if cx.types.builtins.get(&ident.rust) == Some(&CxxString) {
713713
"C++ string".to_owned()
714-
} else if Atom::from(&ident.rust) == Some(Char) {
714+
} else if cx.types.builtins.get(&ident.rust) == Some(&Char) {
715715
"C char".to_owned()
716716
} else {
717717
ident.rust.to_string()

syntax/improper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use self::ImproperCtype::*;
2-
use crate::syntax::atom::Atom::{self, *};
2+
use crate::syntax::atom::Atom::*;
33
use crate::syntax::{Type, Types};
44
use proc_macro2::Ident;
55

@@ -14,7 +14,7 @@ impl<'a> Types<'a> {
1414
match ty {
1515
Type::Ident(ident) => {
1616
let ident = &ident.rust;
17-
if let Some(atom) = Atom::from(ident) {
17+
if let Some(&atom) = self.builtins.get(ident) {
1818
Definite(atom == RustString)
1919
} else if let Some(strct) = self.structs.get(ident) {
2020
Depends(&strct.name.rust) // iterate to fixed-point

syntax/pod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::syntax::atom::Atom::{self, *};
1+
use crate::syntax::atom::Atom::*;
22
use crate::syntax::{derive, Trait, Type, Types};
33

44
impl<'a> Types<'a> {
55
pub fn is_guaranteed_pod(&self, ty: &Type) -> bool {
66
match ty {
77
Type::Ident(ident) => {
88
let ident = &ident.rust;
9-
if let Some(atom) = Atom::from(ident) {
9+
if let Some(&atom) = self.builtins.get(ident) {
1010
match atom {
1111
Bool | Char | U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64
1212
| Isize | F32 | F64 => true,

syntax/types.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use quote::ToTokens;
1414

1515
pub struct Types<'a> {
1616
pub all: OrderedSet<&'a Type>,
17+
pub builtins: UnorderedMap<&'a Ident, Atom>,
1718
pub structs: UnorderedMap<&'a Ident, &'a Struct>,
1819
pub enums: UnorderedMap<&'a Ident, &'a Enum>,
1920
pub cxx: UnorderedSet<&'a Ident>,
@@ -38,6 +39,7 @@ impl<'a> Types<'a> {
3839
let mut untrusted = UnorderedMap::new();
3940
let mut impls = OrderedMap::new();
4041
let mut resolutions = UnorderedMap::new();
42+
let builtins = UnorderedMap::new();
4143
let struct_improper_ctypes = UnorderedSet::new();
4244
let toposorted_structs = Vec::new();
4345

@@ -198,6 +200,7 @@ impl<'a> Types<'a> {
198200

199201
let mut types = Types {
200202
all,
203+
builtins,
201204
structs,
202205
enums,
203206
cxx,
@@ -211,6 +214,16 @@ impl<'a> Types<'a> {
211214
toposorted_structs,
212215
};
213216

217+
for ty in &types.all {
218+
if let Type::Ident(name) = ty {
219+
if types.resolutions.get(&name.rust).is_none() {
220+
if let Some(atom) = Atom::from(&name.rust) {
221+
types.builtins.insert(&name.rust, atom);
222+
}
223+
}
224+
}
225+
}
226+
214227
types.toposorted_structs = toposort::sort(cx, apis, &types);
215228

216229
let mut unresolved_structs = types.structs.keys();

tests/ui/reserved_name.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#[cxx::bridge]
22
mod ffi {
33
struct UniquePtr {
4+
// invalid; `UniquePtr` is a builtin
45
val: usize,
56
}
67

78
extern "C++" {
8-
type Box;
9+
type Box; // invalid; `Box` is a builtin
910
}
1011

1112
extern "Rust" {
12-
type String;
13+
type String; // valid; `String` is an atom
1314
}
1415
}
1516

tests/ui/reserved_name.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ error: reserved name
55
| ^^^^^^^^^
66

77
error: reserved name
8-
--> $DIR/reserved_name.rs:8:14
8+
--> $DIR/reserved_name.rs:9:14
99
|
10-
8 | type Box;
10+
9 | type Box; // invalid; `Box` is a builtin
1111
| ^^^
12-
13-
error: reserved name
14-
--> $DIR/reserved_name.rs:12:14
15-
|
16-
12 | type String;
17-
| ^^^^^^

0 commit comments

Comments
 (0)