8000 Update to syn2 (#5556) · RustPython/RustPython@52208b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 52208b3

Browse files
authored
Update to syn2 (#5556)
1 parent 2721f2d commit 52208b3

File tree

11 files changed

+186
-243
lines changed

11 files changed

+186
-243
lines changed

Cargo.lock

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ schannel = "0.1.27"
177177
static_assertions = "1.1"
178178
strum = "0.27"
179179
strum_macros = "0.27"
180-
syn = "1.0.109"
180+
syn = "2"
181181
thiserror = "2.0"
182182
thread_local = "1.1.8"
183183
unicode_names2 = "1.3.0"

derive-impl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ syn = { workspace = true, features = ["full", "extra-traits"] }
2020
maplit = "1.0.2"
2121
proc-macro2 = "1.0.93"
2222
quote = "1.0.38"
23-
syn-ext = { version = "0.4.0", features = ["full"] }
23+
syn-ext = { version = "0.5.0", features = ["full"] }
2424
textwrap = { version = "0.16.1", default-features = false }
2525

2626
[lints]

derive-impl/src/compile_bytecode.rs

Lines changed: 60 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! )
1414
//! ```
1515
16-
use crate::{extract_spans, Diagnostic};
16+
use crate::Diagnostic;
1717
use once_cell::sync::Lazy;
1818
use proc_macro2::{Span, TokenStream};
1919
use quote::quote;
@@ -25,10 +25,9 @@ use std::{
2525
};
2626
use syn::{
2727
self,
28-
parse::{Parse, ParseStream, Result as ParseResult},
29-
parse2,
28+
parse::{ParseStream, Parser, Result as ParseResult},
3029
spanned::Spanned,
31-
Lit, LitByteStr, LitStr, Macro, Meta, MetaNameValue, Token,
30+
LitByteStr, LitStr, Macro,
3231
};
3332

3433
static CARGO_MANIFEST_DIR: Lazy<PathBuf> = Lazy::new(|| {
@@ -233,83 +232,76 @@ impl CompilationSource {
233232
}
234233
}
235234

236-
/// This is essentially just a comma-separated list of Meta nodes, aka the inside of a MetaList.
237-
struct PyCompileInput {
238-
span: Span,
239-
metas: Vec<Meta>,
240-
}
241-
242-
impl PyCompileInput {
243-
fn parse(&self, allow_dir: bool) -> Result<PyCompileArgs, Diagnostic> {
235+
impl PyCompileArgs {
236+
fn parse(input: TokenStream, allow_dir: bool) -> Result<PyCompileArgs, Diagnostic> {
244237
let mut module_name = None;
245238
let mut mode = None;
246239
let mut source: Option<CompilationSource> = None;
247240
let mut crate_name = None;
248241

249-
fn assert_source_empty(source: &Option<CompilationSource>) -> Result<(), Diagnostic> {
242+
fn assert_source_empty(source: &Option<CompilationSource>) -> Result<(), syn::Error> {
250243
if let Some(source) = source {
251-
Err(Diagnostic::spans_error(
252-
source.span,
244+
Err(syn::Error::new(
245+
source.span.0,
253246
"Cannot have more than one source",
254247
))
255248
} else {
256249
Ok(())
257250
}
258251
}
259252

260-
for meta in &self.metas {
261-
if let Meta::NameValue(name_value) = meta {
262-
let ident = match name_value.path.get_ident() {
263-
Some(ident) => ident,
264-
None => continue,
265-
};
266-
let check_str = || match &name_value.lit {
267-
Lit::Str(s) => Ok(s),
268-
_ => Err(err_span!(name_value.lit, "{ident} must be a string")),
269-
};
270-
if ident == "mode" {
271-
let s = check_str()?;
272-
match s.value().parse() {
273-
Ok(mode_val) => mode = Some(mode_val),
274-
Err(e) => bail_span!(s, "{}", e),
275-
}
276-
} else if ident == "module_name" {
277-
module_name = Some(check_str()?.value())
278-
} else if ident == "source" {
279-
assert_source_empty(&source)?;
280-
let code = check_str()?.value();
281-
source = Some(CompilationSource {
282-
kind: CompilationSourceKind::SourceCode(code),
283-
span: extract_spans(&name_value).unwrap(),
284-
});
285-
} else if ident == "file" {
286-
assert_source_empty(&source)?;
287-
let path = check_str()?.value().into();
288-
source = Some(CompilationSource {
289-
kind: CompilationSourceKind::File(path),
290-
span: extract_spans(&name_value).unwrap(),
291-
});
292-
} else if ident == "dir" {
293-
if !allow_dir {
294-
bail_span!(ident, "py_compile doesn't accept dir")
295-
}
296-
297-
assert_source_empty(&source)?;
298-
let path = check_str()?.value().into();
299-
source = Some(CompilationSource {
300-
kind: CompilationSourceKind::Dir(path),
301-
span: extract_spans(&name_value).unwrap(),
302-
});
303-
} else if ident == "crate_name" {
304-
let name = check_str()?.parse()?;
305-
crate_name = Some(name);
253+
syn::meta::parser(|meta| {
254+
let ident = meta
255+
.path
256+
.get_ident()
257+
.ok_or_else(|| meta.error("unknown arg"))?;
258+
let check_str = || meta.value()?.call(parse_str);
259+
if ident == "mode" {
260+
let s = check_str()?;
261+
match s.value().parse() {
262+
Ok(mode_val) => mode = Some(mode_val),
263+
Err(e) => bail_span!(s, "{}", e),
264+
}
265+
} else if ident == "module_name" {
266+
module_name = Some(check_str()?.value())
267+
} else if ident == "source" {
268+
assert_source_empty(&source)?;
269+
let code = check_str()?.value();
270+
source = Some(CompilationSource {
271+
kind: CompilationSourceKind::SourceCode(code),
272+
span: (ident.span(), meta.input.cursor().span()),
273+
});
274+
} else if ident == "file" {
275+
assert_source_empty(&source)?;
276+
let path = check_str()?.value().into();
277+
source = Some(CompilationSource {
278+
kind: CompilationSourceKind::File(path),
279+
span: (ident.span(), meta.input.cursor().span()),
280+
});
281+
} else if ident == "dir" {
282+
if !allow_dir {
283+
bail_span!(ident, "py_compile doesn't accept dir")
306284
}
285+
286+
assert_source_empty(&source)?;
287+
let path = check_str()?.value().into();
288+
source = Some(CompilationSource {
289+
kind: CompilationSourceKind::Dir(path),
290+
span: (ident.span(), meta.input.cursor().span()),
291+
});
292+
} else if ident == "crate_name" {
293+
let name = check_str()?.parse()?;
294+
crate_name = Some(name);
295+
} else {
296+
return Err(meta.error("unknown attr"));
307297
}
308-
}
298+
Ok(())
299+
})
300+
.parse2(input)?;
309301

310302
let source = source.ok_or_else(|| {
311303
syn::Error::new(
312-
self.span,
304+
Span::call_site(),
313305
"Must have either file or source in py_compile!()/py_freeze!()",
314306
)
315307
})?;
@@ -323,38 +315,17 @@ impl PyCompileInput {
323315
}
324316
}
325317

326-
fn parse_meta(input: ParseStream) -> ParseResult<Meta> {
327-
let path = input.call(syn::Path::parse_mod_style)?;
328-
let eq_token: Token![=] = input.parse()?;
318+
fn parse_str(input: ParseStream) -> ParseResult<LitStr> {
329319
let span = input.span();
330320
if input.peek(LitStr) {
331-
Ok(Meta::NameValue(MetaNameValue {
332-
path,
333-
eq_token,
334-
lit: Lit::Str(input.parse()?),
335-
}))
321+
input.parse()
336322
} else if let Ok(mac) = input.parse::<Macro>() {
337-
Ok(Meta::NameValue(MetaNameValue {
338-
path,
339-
eq_token,
340-
lit: Lit::Str(LitStr::new(&mac.tokens.to_string(), mac.span())),
341-
}))
323+
Ok(LitStr::new(&mac.tokens.to_string(), mac.span()))
342324
} else {
343325
Err(syn::Error::new(span, "Expected string or stringify macro"))
344326
}
345327
}
346328

347-
impl Parse for PyCompileInput {
348-
fn parse(input: ParseStream) -> ParseResult<Self> {
349-
let span = input.cursor().span();
350-
let metas = input
351-
.parse_terminated::<Meta, Token![,]>(parse_meta)?
352-
.into_iter()
353-
.collect();
354-
Ok(PyCompileInput { span, metas })
355-
}
356-
}
357-
358329
struct PyCompileArgs {
359330
source: CompilationSource,
360331
mode: Mode,
@@ -366,8 +337,7 @@ pub fn impl_py_compile(
366337
input: TokenStream,
367338
compiler: &dyn Compiler,
368339
) -> Result<TokenStream, Diagnostic> {
369-
let input: PyCompileInput = parse2(input)?;
370-
let args = input.parse(false)?;
340+
let args = PyCompileArgs::parse(input, false)?;
371341

372342
let crate_name = args.crate_name;
373343
let code = args
@@ -388,8 +358,7 @@ pub fn impl_py_freeze(
388358
input: TokenStream,
389359
compiler: &dyn Compiler,
390360
) -> Result<TokenStream, Diagnostic> {
391-
let input: PyCompileInput = parse2(input)?;
392-
let args = input.parse(true)?;
361+
let args = PyCompileArgs::parse(input, true)?;
393362

394363
let crate_name = args.crate_name;
395364
let code_map = args.source.compile(args.mode, args.module_name, compiler)?;

0 commit comments

Comments
 (0)
0