forked from Rust-related/RustPython
cspell derive
This commit is contained in:
@@ -38,7 +38,7 @@ static CARGO_MANIFEST_DIR: Lazy<PathBuf> = Lazy::new(|| {
|
||||
enum CompilationSourceKind {
|
||||
/// Source is a File (Path)
|
||||
File(PathBuf),
|
||||
/// Direct Raw sourcecode
|
||||
/// Direct Raw source code
|
||||
SourceCode(String),
|
||||
/// Source is a directory
|
||||
Dir(PathBuf),
|
||||
|
||||
@@ -86,7 +86,7 @@ impl ArgAttribute {
|
||||
self.default = Some(None);
|
||||
}
|
||||
} else {
|
||||
bail_span!(path, "Unrecognised pyarg attribute");
|
||||
bail_span!(path, "Unrecognized pyarg attribute");
|
||||
}
|
||||
}
|
||||
NestedMeta::Meta(Meta::NameValue(name_value)) => {
|
||||
@@ -109,10 +109,10 @@ impl ArgAttribute {
|
||||
_ => bail_span!(name_value, "Expected string value for name argument"),
|
||||
}
|
||||
} else {
|
||||
bail_span!(name_value, "Unrecognised pyarg attribute");
|
||||
bail_span!(name_value, "Unrecognized pyarg attribute");
|
||||
}
|
||||
}
|
||||
_ => bail_span!(arg, "Unrecognised pyarg attribute"),
|
||||
_ => bail_span!(arg, "Unrecognized pyarg attribute"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -138,24 +138,24 @@ fn generate_field((i, field): (usize, &Field)) -> Result<TokenStream> {
|
||||
};
|
||||
|
||||
let name = field.ident.as_ref();
|
||||
let namestring = name.map(Ident::to_string);
|
||||
if matches!(&namestring, Some(s) if s.starts_with("_phantom")) {
|
||||
let name_string = name.map(Ident::to_string);
|
||||
if matches!(&name_string, Some(s) if s.starts_with("_phantom")) {
|
||||
return Ok(quote! {
|
||||
#name: ::std::marker::PhantomData,
|
||||
});
|
||||
}
|
||||
let fieldname = match name {
|
||||
let field_name = match name {
|
||||
Some(id) => id.to_token_stream(),
|
||||
None => syn::Index::from(i).into_token_stream(),
|
||||
};
|
||||
if let ParameterKind::Flatten = attr.kind {
|
||||
return Ok(quote! {
|
||||
#fieldname: ::rustpython_vm::function::FromArgs::from_args(vm, args)?,
|
||||
#field_name: ::rustpython_vm::function::FromArgs::from_args(vm, args)?,
|
||||
});
|
||||
}
|
||||
let pyname = attr
|
||||
.name
|
||||
.or(namestring)
|
||||
.or(name_string)
|
||||
.ok_or_else(|| err_span!(field, "field in tuple struct must have name attribute"))?;
|
||||
let middle = quote! {
|
||||
.map(|x| ::rustpython_vm::convert::TryFromObject::try_from_object(vm, x)).transpose()?
|
||||
@@ -184,17 +184,17 @@ fn generate_field((i, field): (usize, &Field)) -> Result<TokenStream> {
|
||||
let file_output = match attr.kind {
|
||||
ParameterKind::PositionalOnly => {
|
||||
quote! {
|
||||
#fieldname: args.take_positional()#middle #ending,
|
||||
#field_name: args.take_positional()#middle #ending,
|
||||
}
|
||||
}
|
||||
ParameterKind::PositionalOrKeyword => {
|
||||
quote! {
|
||||
#fieldname: args.take_positional_keyword(#pyname)#middle #ending,
|
||||
#field_name: args.take_positional_keyword(#pyname)#middle #ending,
|
||||
}
|
||||
}
|
||||
ParameterKind::KeywordOnly => {
|
||||
quote! {
|
||||
#fieldname: args.take_keyword(#pyname)#middle #ending,
|
||||
#field_name: args.take_keyword(#pyname)#middle #ending,
|
||||
}
|
||||
}
|
||||
ParameterKind::Flatten => unreachable!(),
|
||||
|
||||
@@ -81,9 +81,9 @@ fn extract_items_into_context<'a, Item>(
|
||||
{
|
||||
for item in items {
|
||||
let r = item.try_split_attr_mut(|attrs, item| {
|
||||
let (pyitems, cfgs) = attrs_to_content_items(attrs, impl_item_new::<Item>)?;
|
||||
for pyitem in pyitems.iter().rev() {
|
||||
let r = pyitem.gen_impl_item(ImplItemArgs::<Item> {
|
||||
let (py_items, cfgs) = attrs_to_content_items(attrs, impl_item_new::<Item>)?;
|
||||
for py_item in py_items.iter().rev() {
|
||||
let r = py_item.gen_impl_item(ImplItemArgs::<Item> {
|
||||
item,
|
||||
attrs,
|
||||
context,
|
||||
@@ -393,6 +393,7 @@ pub(crate) fn impl_define_exception(exc_def: PyExceptionDef) -> Result<TokenStre
|
||||
// We need this method, because of how `CPython` copies `__init__`
|
||||
// from `BaseException` in `SimpleExtendsException` macro.
|
||||
// See: `(initproc)BaseException_init`
|
||||
// spell-checker:ignore initproc
|
||||
let init_method = match init {
|
||||
Some(init_def) => quote! { #init_def(zelf, args, vm) },
|
||||
None => quote! { #base_class::init(zelf, args, vm) },
|
||||
@@ -553,7 +554,7 @@ where
|
||||
quote_spanned! { ident.span() =>
|
||||
class.set_attr(
|
||||
ctx.names.#name_ident,
|
||||
ctx.make_funcdef(#py_name, Self::#ident)
|
||||
ctx.make_func_def(#py_name, Self::#ident)
|
||||
#doc
|
||||
#build_func
|
||||
.into(),
|
||||
@@ -563,7 +564,7 @@ where
|
||||
quote_spanned! { ident.span() =>
|
||||
class.set_str_attr(
|
||||
#py_name,
|
||||
ctx.make_funcdef(#py_name, Self::#ident)
|
||||
ctx.make_func_def(#py_name, Self::#ident)
|
||||
#doc
|
||||
#build_func,
|
||||
ctx,
|
||||
@@ -1048,8 +1049,8 @@ impl ItemMeta for SlotItemMeta {
|
||||
where
|
||||
I: std::iter::Iterator<Item = NestedMeta>,
|
||||
{
|
||||
let meta_map = if let Some(nmeta) = nested.next() {
|
||||
match nmeta {
|
||||
let meta_map = if let Some(nested_meta) = nested.next() {
|
||||
match nested_meta {
|
||||
NestedMeta::Meta(meta) => {
|
||||
Some([("name".to_owned(), (0, meta))].iter().cloned().collect())
|
||||
}
|
||||
@@ -1059,7 +1060,7 @@ impl ItemMeta for SlotItemMeta {
|
||||
Some(HashMap::default())
|
||||
};
|
||||
let (Some(meta_map), None) = (meta_map, nested.next()) else {
|
||||
bail_span!(meta_ident, "#[pyslot] must be of the form #[pyslot] or #[pyslot(slotname)]")
|
||||
bail_span!(meta_ident, "#[pyslot] must be of the form #[pyslot] or #[pyslot(slot_name)]")
|
||||
};
|
||||
Ok(Self::from_inner(ItemMetaInner {
|
||||
item_ident,
|
||||
@@ -1096,7 +1097,7 @@ impl SlotItemMeta {
|
||||
slot_name.ok_or_else(|| {
|
||||
err_span!(
|
||||
inner.meta_ident,
|
||||
"#[pyslot] must be of the form #[pyslot] or #[pyslot(slotname)]",
|
||||
"#[pyslot] must be of the form #[pyslot] or #[pyslot(slot_name)]",
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -74,9 +74,9 @@ pub fn impl_pymodule(attr: AttributeArgs, module_item: Item) -> Result<TokenStre
|
||||
continue;
|
||||
}
|
||||
let r = item.try_split_attr_mut(|attrs, item| {
|
||||
let (pyitems, cfgs) = attrs_to_module_items(attrs, module_item_new)?;
|
||||
for pyitem in pyitems.iter().rev() {
|
||||
let r = pyitem.gen_module_item(ModuleItemArgs {
|
||||
let (py_items, cfgs) = attrs_to_module_items(attrs, module_item_new)?;
|
||||
for py_item in py_items.iter().rev() {
|
||||
let r = py_item.gen_module_item(ModuleItemArgs {
|
||||
item,
|
||||
attrs,
|
||||
context: &mut context,
|
||||
@@ -145,20 +145,20 @@ pub fn impl_pymodule(attr: AttributeArgs, module_item: Item) -> Result<TokenStre
|
||||
fn module_item_new(
|
||||
index: usize,
|
||||
attr_name: AttrName,
|
||||
pyattrs: Vec<usize>,
|
||||
py_attrs: Vec<usize>,
|
||||
) -> Box<dyn ModuleItem<AttrName = AttrName>> {
|
||||
match attr_name {
|
||||
AttrName::Function => Box::new(FunctionItem {
|
||||
inner: ContentItemInner { index, attr_name },
|
||||
pyattrs,
|
||||
py_attrs,
|
||||
}),
|
||||
AttrName::Attr => Box::new(AttributeItem {
|
||||
inner: ContentItemInner { index, attr_name },
|
||||
pyattrs,
|
||||
py_attrs,
|
||||
}),
|
||||
AttrName::Class => Box::new(ClassItem {
|
||||
inner: ContentItemInner { index, attr_name },
|
||||
pyattrs,
|
||||
py_attrs,
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -186,7 +186,7 @@ where
|
||||
}
|
||||
|
||||
let mut closed = false;
|
||||
let mut pyattrs = Vec::new();
|
||||
let mut py_attrs = Vec::new();
|
||||
for (i, attr) in iter {
|
||||
// take py items but no cfgs
|
||||
let attr_name = if let Some(ident) = attr.get_ident() {
|
||||
@@ -218,16 +218,16 @@ where
|
||||
"#[pyattr] must be placed on top of other #[py*] items",
|
||||
)
|
||||
}
|
||||
pyattrs.push(i);
|
||||
py_attrs.push(i);
|
||||
continue;
|
||||
}
|
||||
|
||||
if pyattrs.is_empty() {
|
||||
if py_attrs.is_empty() {
|
||||
result.push(item_new(i, attr_name, Vec::new()));
|
||||
} else {
|
||||
match attr_name {
|
||||
AttrName::Class | AttrName::Function => {
|
||||
result.push(item_new(i, attr_name, pyattrs.clone()));
|
||||
result.push(item_new(i, attr_name, py_attrs.clone()));
|
||||
}
|
||||
_ => {
|
||||
bail_span!(
|
||||
@@ -236,14 +236,14 @@ where
|
||||
)
|
||||
}
|
||||
}
|
||||
pyattrs.clear();
|
||||
py_attrs.clear();
|
||||
closed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(last) = pyattrs.pop() {
|
||||
if let Some(last) = py_attrs.pop() {
|
||||
assert!(!closed);
|
||||
result.push(item_new(last, AttrName::Attr, pyattrs));
|
||||
result.push(item_new(last, AttrName::Attr, py_attrs));
|
||||
}
|
||||
Ok((result, cfgs))
|
||||
}
|
||||
@@ -251,19 +251,19 @@ where
|
||||
/// #[pyfunction]
|
||||
struct FunctionItem {
|
||||
inner: ContentItemInner<AttrName>,
|
||||
pyattrs: Vec<usize>,
|
||||
py_attrs: Vec<usize>,
|
||||
}
|
||||
|
||||
/// #[pyclass]
|
||||
struct ClassItem {
|
||||
inner: ContentItemInner<AttrName>,
|
||||
pyattrs: Vec<usize>,
|
||||
py_attrs: Vec<usize>,
|
||||
}
|
||||
|
||||
/// #[pyattr]
|
||||
struct AttributeItem {
|
||||
inner: ContentItemInner<AttrName>,
|
||||
pyattrs: Vec<usize>,
|
||||
py_attrs: Vec<usize>,
|
||||
}
|
||||
|
||||
impl ContentItem for FunctionItem {
|
||||
@@ -334,14 +334,14 @@ impl ModuleItem for FunctionItem {
|
||||
};
|
||||
let doc = quote!(.with_doc(#doc.to_owned(), &vm.ctx));
|
||||
let new_func = quote_spanned!(ident.span()=>
|
||||
vm.ctx.make_funcdef(#py_name, #ident)
|
||||
vm.ctx.make_func_def(#py_name, #ident)
|
||||
#doc
|
||||
.into_function()
|
||||
.with_module(vm.new_pyobj(#module.to_owned()))
|
||||
.into_ref(&vm.ctx)
|
||||
);
|
||||
|
||||
if self.pyattrs.is_empty() {
|
||||
if self.py_attrs.is_empty() {
|
||||
(
|
||||
quote_spanned! { ident.span() => {
|
||||
let func = #new_func;
|
||||
@@ -352,7 +352,7 @@ impl ModuleItem for FunctionItem {
|
||||
} else {
|
||||
let mut py_names = HashSet::new();
|
||||
py_names.insert(py_name);
|
||||
for attr_index in self.pyattrs.iter().rev() {
|
||||
for attr_index in self.py_attrs.iter().rev() {
|
||||
let mut loop_unit = || {
|
||||
let attr_attr = args.attrs.remove(*attr_index);
|
||||
let item_meta = SimpleItemMeta::from_attr(ident.clone(), &attr_attr)?;
|
||||
@@ -402,7 +402,7 @@ impl ModuleItem for ClassItem {
|
||||
let (class_name, class_new) = {
|
||||
let class_attr = &mut args.attrs[self.inner.index];
|
||||
let no_attr = class_attr.try_remove_name("no_attr")?;
|
||||
if self.pyattrs.is_empty() {
|
||||
if self.py_attrs.is_empty() {
|
||||
// check no_attr before ClassItemMeta::from_attr
|
||||
if no_attr.is_none() {
|
||||
bail_span!(
|
||||
@@ -439,7 +439,7 @@ impl ModuleItem for ClassItem {
|
||||
};
|
||||
|
||||
let mut py_names = Vec::new();
|
||||
for attr_index in self.pyattrs.iter().rev() {
|
||||
for attr_index in self.py_attrs.iter().rev() {
|
||||
let mut loop_unit = || {
|
||||
let attr_attr = args.attrs.remove(*attr_index);
|
||||
let item_meta = SimpleItemMeta::from_attr(ident.clone(), &attr_attr)?;
|
||||
@@ -540,7 +540,7 @@ impl ModuleItem for AttributeItem {
|
||||
)
|
||||
}
|
||||
Item::Use(item) => {
|
||||
if !self.pyattrs.is_empty() {
|
||||
if !self.py_attrs.is_empty() {
|
||||
return Err(self
|
||||
.new_syn_error(item.span(), "Only single #[pyattr] is allowed for `use`"));
|
||||
}
|
||||
@@ -578,7 +578,7 @@ impl ModuleItem for AttributeItem {
|
||||
}
|
||||
};
|
||||
|
||||
let (tokens, py_names) = if self.pyattrs.is_empty() {
|
||||
let (tokens, py_names) = if self.py_attrs.is_empty() {
|
||||
(
|
||||
quote_spanned! { ident.span() => {
|
||||
#let_obj
|
||||
@@ -588,7 +588,7 @@ impl ModuleItem for AttributeItem {
|
||||
)
|
||||
} else {
|
||||
let mut names = vec![py_name];
|
||||
for attr_index in self.pyattrs.iter().rev() {
|
||||
for attr_index in self.py_attrs.iter().rev() {
|
||||
let mut loop_unit = || {
|
||||
let attr_attr = args.attrs.remove(*attr_index);
|
||||
let item_meta = AttrItemMeta::from_attr(ident.clone(), &attr_attr)?;
|
||||
|
||||
@@ -423,7 +423,7 @@ impl AttributeExt for Attribute {
|
||||
let has_name = list
|
||||
.nested
|
||||
.iter()
|
||||
.any(|nmeta| nmeta.get_path().map_or(false, |p| p.is_ident(name)));
|
||||
.any(|nested_meta| nested_meta.get_path().map_or(false, |p| p.is_ident(name)));
|
||||
if !has_name {
|
||||
list.nested.push(new_item())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user