Skip to content

Commit b5aca7b

Browse files
committed
Collect reason that each alias is required trivial
1 parent 11fd6d0 commit b5aca7b

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

gen/src/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub(super) fn gen(
8181
}
8282
}
8383
Api::TypeAlias(ety) => {
84-
if types.required_trivial_aliases.contains(&ety.ident) {
84+
if types.required_trivial_aliases.contains_key(&ety.ident) {
8585
check_trivial_extern_type(out, &ety.ident)
8686
}
8787
}
@@ -136,7 +136,7 @@ fn write_includes(out: &mut OutFile, types: &Types) {
136136
Some(CxxString) => out.include.string = true,
137137
Some(Bool) | Some(Isize) | Some(F32) | Some(F64) | Some(RustString) => {}
138138
None => {
139-
if types.required_trivial_aliases.contains(&ident) {
139+
if types.required_trivial_aliases.contains_key(&ident) {
140140
out.include.type_traits = true;
141141
}
142142
}

macro/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ fn expand_type_alias_verify(
683683
const _: fn() = #begin #ident, #type_id #end;
684684
};
685685

686-
if types.required_trivial_aliases.contains(&alias.ident) {
686+
if types.required_trivial_aliases.contains_key(&alias.ident) {
687687
let begin = quote_spanned!(begin_span=> ::cxx::private::verify_extern_kind::<);
688688
verify.extend(quote! {
689689
const _: fn() = #begin #ident, ::cxx::kind::Trivial #end;

syntax/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ fn is_unsized(cx: &mut Check, ty: &Type) -> bool {
338338
|| cx.types.cxx.contains(ident)
339339
&& !cx.types.structs.contains_key(ident)
340340
&& !cx.types.enums.contains_key(ident)
341-
&& !cx.types.required_trivial_aliases.contains(ident)
341+
&& !cx.types.required_trivial_aliases.contains_key(ident)
342342
|| cx.types.rust.contains(ident)
343343
}
344344

@@ -377,7 +377,7 @@ fn describe(cx: &mut Check, ty: &Type) -> String {
377377
} else if cx.types.enums.contains_key(ident) {
378378
"enum".to_owned()
379379
} else if cx.types.cxx.contains(ident) {
380-
if cx.types.required_trivial_aliases.contains(ident) {
380+
if cx.types.required_trivial_aliases.contains_key(ident) {
381381
"trivial C++ type".to_owned()
382382
} else {
383383
"non-trivial C++ type".to_owned()

syntax/types.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::syntax::atom::Atom::{self, *};
22
use crate::syntax::report::Errors;
33
use crate::syntax::set::OrderedSet as Set;
4-
use crate::syntax::{Api, Derive, Enum, ExternType, Struct, Type, TypeAlias};
4+
use crate::syntax::{Api, Derive, Enum, ExternFn, ExternType, Struct, Type, TypeAlias};
55
use proc_macro2::Ident;
66
use quote::ToTokens;
77
use std::collections::{BTreeMap as Map, HashSet as UnorderedSet};
@@ -14,7 +14,7 @@ pub struct Types<'a> {
1414
pub rust: Set<&'a Ident>,
1515
pub aliases: Map<&'a Ident, &'a TypeAlias>,
1616
pub untrusted: Map<&'a Ident, &'a ExternType>,
17-
pub required_trivial_aliases: UnorderedSet<&'a Ident>,
17+
pub required_trivial_aliases: Map<&'a Ident, TrivialReason<'a>>,
1818
}
1919

2020
impl<'a> Types<'a> {
@@ -140,27 +140,30 @@ impl<'a> Types<'a> {
140140
// we check that this is permissible. We do this _after_ scanning all
141141
// the APIs above, in case some function or struct references a type
142142
// which is declared subsequently.
143-
let mut required_trivial_aliases = UnorderedSet::new();
144-
let mut insist_alias_types_are_trivial = |ty: &'a Type| {
143+
let mut required_trivial_aliases = Map::new();
144+
let mut insist_alias_types_are_trivial = |ty: &'a Type, reason| {
145145
if let Type::Ident(ident) = ty {
146146
if aliases.contains_key(ident) {
147-
required_trivial_aliases.insert(ident);
147+
required_trivial_aliases.entry(ident).or_insert(reason);
148148
}
149149
}
150150
};
151151
for api in apis {
152152
match api {
153153
Api::Struct(strct) => {
154+
let reason = TrivialReason::StructField(strct);
154155
for field in &strct.fields {
155-
insist_alias_types_are_trivial(&field.ty);
156+
insist_alias_types_are_trivial(&field.ty, reason);
156157
}
157158
}
158159
Api::CxxFunction(efn) | Api::RustFunction(efn) => {
160+
let reason = TrivialReason::FunctionArgument(efn);
159161
for arg in &efn.args {
160-
insist_alias_types_are_trivial(&arg.ty);
162+
insist_alias_types_are_trivial(&arg.ty, reason);
161163
}
162164
if let Some(ret) = &efn.ret {
163-
insist_alias_types_are_trivial(&ret);
165+
let reason = TrivialReason::FunctionReturn(efn);
166+
insist_alias_types_are_trivial(&ret, reason);
164167
}
165168
}
166169
_ => {}
@@ -211,6 +214,13 @@ impl<'t, 'a> IntoIterator for &'t Types<'a> {
211214
}
212215
}
213216

217+
#[derive(Copy, Clone)]
218+
pub enum TrivialReason<'a> {
219+
StructField(&'a Struct),
220+
FunctionArgument(&'a ExternFn),
221+
FunctionReturn(&'a ExternFn),
222+
}
223+
214224
fn duplicate_name(cx: &mut Errors, sp: impl ToTokens, ident: &Ident) {
215225
let msg = format!("the name `{}` is defined multiple times", ident);
216226
cx.error(sp, msg);

0 commit comments

Comments
 (0)