diff --git a/gen/src/write.rs b/gen/src/write.rs index c6d59d8b9..ef97a29f1 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -204,7 +204,7 @@ fn pick_includes_and_builtins(out: &mut OutFile, apis: &[Api]) { for ty in out.types { match ty { - Type::Ident(ident) => match Atom::from(&ident.rust) { + Type::Ident(ident) => match out.types.builtins.get(&ident.rust) { Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(I8) | Some(I16) | Some(I32) | Some(I64) => out.include.cstdint = true, Some(Usize) => out.include.cstddef = true, @@ -1191,8 +1191,8 @@ fn write_extern_arg(out: &mut OutFile, arg: &Var) { fn write_type(out: &mut OutFile, ty: &Type) { match ty { - Type::Ident(ident) => match Atom::from(&ident.rust) { - Some(atom) => write_atom(out, atom), + Type::Ident(ident) => match out.types.builtins.get(&ident.rust) { + Some(&atom) => write_atom(out, atom), None => write!( out, "{}", diff --git a/syntax/check.rs b/syntax/check.rs index 698782f96..c180a6864 100644 --- a/syntax/check.rs +++ b/syntax/check.rs @@ -1,4 +1,4 @@ -use crate::syntax::atom::Atom::{self, *}; +use crate::syntax::atom::Atom::*; use crate::syntax::report::Errors; use crate::syntax::visit::{self, Visit}; use crate::syntax::{ @@ -81,7 +81,7 @@ impl Check<'_> { fn check_type_ident(cx: &mut Check, name: &NamedType) { let ident = &name.rust; - if Atom::from(ident).is_none() + if cx.types.builtins.get(ident).is_none() && !cx.types.structs.contains_key(ident) && !cx.types.enums.contains_key(ident) && !cx.types.cxx.contains(ident) @@ -102,7 +102,7 @@ fn check_type_box(cx: &mut Check, ptr: &Ty1) { cx.error(ptr, error::BOX_CXX_TYPE.msg); } - if Atom::from(&ident.rust).is_none() { + if cx.types.builtins.get(&ident.rust).is_none() { return; } } @@ -122,7 +122,7 @@ fn check_type_rust_vec(cx: &mut Check, ty: &Ty1) { return; } - match Atom::from(&ident.rust) { + match cx.types.builtins.get(&ident.rust) { None | Some(Char) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) => return, @@ -144,7 +144,7 @@ fn check_type_unique_ptr(cx: &mut Check, ptr: &Ty1) { return; } - match Atom::from(&ident.rust) { + match cx.types.builtins.get(&ident.rust) { None | Some(CxxString) => return, _ => {} } @@ -162,7 +162,7 @@ fn check_type_shared_ptr(cx: &mut Check, ptr: &Ty1) { return; } - match Atom::from(&ident.rust) { + match cx.types.builtins.get(&ident.rust) { None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) | Some(CxxString) => return, @@ -183,7 +183,7 @@ fn check_type_weak_ptr(cx: &mut Check, ptr: &Ty1) { return; } - match Atom::from(&ident.rust) { + match cx.types.builtins.get(&ident.rust) { None | Some(Bool) | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) | Some(CxxString) => return, @@ -207,7 +207,7 @@ fn check_type_cxx_vector(cx: &mut Check, ptr: &Ty1) { return; } - match Atom::from(&ident.rust) { + match cx.types.builtins.get(&ident.rust) { None | Some(U8) | Some(U16) | Some(U32) | Some(U64) | Some(Usize) | Some(I8) | Some(I16) | Some(I32) | Some(I64) | Some(Isize) | Some(F32) | Some(F64) | Some(CxxString) => return, @@ -522,7 +522,7 @@ fn check_api_impl(cx: &mut Check, imp: &Impl) { | Type::WeakPtr(ty) | Type::CxxVector(ty) => { if let Type::Ident(inner) = &ty.inner { - if Atom::from(&inner.rust).is_none() { + if cx.types.builtins.get(&inner.rust).is_none() { return; } } @@ -568,7 +568,7 @@ fn check_mut_return_restriction(cx: &mut Check, efn: &ExternFn) { self.found |= match ty { Type::Ref(ty) => ty.mutable, Type::SliceRef(slice) => slice.mutable, - Type::Ident(ident) if Atom::from(&ident.rust).is_none() => { + Type::Ident(ident) if self.cx.types.builtins.get(&ident.rust).is_none() => { match self.cx.types.try_resolve(ident) { Some(resolve) => !resolve.generics.lifetimes.is_empty(), None => true, @@ -604,7 +604,7 @@ fn check_reserved_name(cx: &mut Check, ident: &Ident) { || ident == "Vec" || ident == "CxxVector" || ident == "str" - || Atom::from(ident).is_some() + || cx.types.builtins.get(ident).is_some() { cx.error(ident, "reserved name"); } @@ -709,9 +709,9 @@ fn describe(cx: &mut Check, ty: &Type) -> String { "opaque C++ type".to_owned() } else if cx.types.rust.contains(&ident.rust) { "opaque Rust type".to_owned() - } else if Atom::from(&ident.rust) == Some(CxxString) { + } else if cx.types.builtins.get(&ident.rust) == Some(&CxxString) { "C++ string".to_owned() - } else if Atom::from(&ident.rust) == Some(Char) { + } else if cx.types.builtins.get(&ident.rust) == Some(&Char) { "C char".to_owned() } else { ident.rust.to_string() diff --git a/syntax/improper.rs b/syntax/improper.rs index f19eb86a7..26eddfa44 100644 --- a/syntax/improper.rs +++ b/syntax/improper.rs @@ -1,5 +1,5 @@ use self::ImproperCtype::*; -use crate::syntax::atom::Atom::{self, *}; +use crate::syntax::atom::Atom::*; use crate::syntax::{Type, Types}; use proc_macro2::Ident; @@ -14,7 +14,7 @@ impl<'a> Types<'a> { match ty { Type::Ident(ident) => { let ident = &ident.rust; - if let Some(atom) = Atom::from(ident) { + if let Some(&atom) = self.builtins.get(ident) { Definite(atom == RustString) } else if let Some(strct) = self.structs.get(ident) { Depends(&strct.name.rust) // iterate to fixed-point diff --git a/syntax/pod.rs b/syntax/pod.rs index 0bf152eea..9af569b0a 100644 --- a/syntax/pod.rs +++ b/syntax/pod.rs @@ -1,4 +1,4 @@ -use crate::syntax::atom::Atom::{self, *}; +use crate::syntax::atom::Atom::*; use crate::syntax::{derive, Trait, Type, Types}; impl<'a> Types<'a> { @@ -6,7 +6,7 @@ impl<'a> Types<'a> { match ty { Type::Ident(ident) => { let ident = &ident.rust; - if let Some(atom) = Atom::from(ident) { + if let Some(&atom) = self.builtins.get(ident) { match atom { Bool | Char | U8 | U16 | U32 | U64 | Usize | I8 | I16 | I32 | I64 | Isize | F32 | F64 => true, diff --git a/syntax/types.rs b/syntax/types.rs index c54682bd0..1733b33b3 100644 --- a/syntax/types.rs +++ b/syntax/types.rs @@ -14,6 +14,7 @@ use quote::ToTokens; pub struct Types<'a> { pub all: OrderedSet<&'a Type>, + pub builtins: UnorderedMap<&'a Ident, Atom>, pub structs: UnorderedMap<&'a Ident, &'a Struct>, pub enums: UnorderedMap<&'a Ident, &'a Enum>, pub cxx: UnorderedSet<&'a Ident>, @@ -38,6 +39,7 @@ impl<'a> Types<'a> { let mut untrusted = UnorderedMap::new(); let mut impls = OrderedMap::new(); let mut resolutions = UnorderedMap::new(); + let builtins = UnorderedMap::new(); let struct_improper_ctypes = UnorderedSet::new(); let toposorted_structs = Vec::new(); @@ -198,6 +200,7 @@ impl<'a> Types<'a> { let mut types = Types { all, + builtins, structs, enums, cxx, @@ -211,6 +214,16 @@ impl<'a> Types<'a> { toposorted_structs, }; + for ty in &types.all { + if let Type::Ident(name) = ty { + if types.resolutions.get(&name.rust).is_none() { + if let Some(atom) = Atom::from(&name.rust) { + types.builtins.insert(&name.rust, atom); + } + } + } + } + types.toposorted_structs = toposort::sort(cx, apis, &types); let mut unresolved_structs = types.structs.keys(); diff --git a/tests/ui/reserved_name.rs b/tests/ui/reserved_name.rs index 409e67c0c..74f3b392e 100644 --- a/tests/ui/reserved_name.rs +++ b/tests/ui/reserved_name.rs @@ -1,15 +1,16 @@ #[cxx::bridge] mod ffi { struct UniquePtr { + // invalid; `UniquePtr` is a builtin val: usize, } extern "C++" { - type Box; + type Box; // invalid; `Box` is a builtin } extern "Rust" { - type String; + type String; // valid; `String` is an atom } } diff --git a/tests/ui/reserved_name.stderr b/tests/ui/reserved_name.stderr index 6684771a1..af0a95ac1 100644 --- a/tests/ui/reserved_name.stderr +++ b/tests/ui/reserved_name.stderr @@ -5,13 +5,7 @@ error: reserved name | ^^^^^^^^^ error: reserved name - --> $DIR/reserved_name.rs:8:14 + --> $DIR/reserved_name.rs:9:14 | -8 | type Box; +9 | type Box; // invalid; `Box` is a builtin | ^^^ - -error: reserved name - --> $DIR/reserved_name.rs:12:14 - | -12 | type String; - | ^^^^^^