From 117ef22142db12eea644b91610592cd5edc20b78 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 9 Jul 2023 11:09:20 -0700 Subject: [PATCH 1/2] Add ui test with suffixed string literals in attribute --- test_suite/tests/ui/malformed/str_suffix.rs | 10 ++++++++++ test_suite/tests/ui/malformed/str_suffix.stderr | 11 +++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test_suite/tests/ui/malformed/str_suffix.rs create mode 100644 test_suite/tests/ui/malformed/str_suffix.stderr diff --git a/test_suite/tests/ui/malformed/str_suffix.rs b/test_suite/tests/ui/malformed/str_suffix.rs new file mode 100644 index 000000000..836842ee6 --- /dev/null +++ b/test_suite/tests/ui/malformed/str_suffix.rs @@ -0,0 +1,10 @@ +use serde::Serialize; + +#[derive(Serialize)] +#[serde(bound = ""huh)] +pub struct Struct { + #[serde(rename = ""what)] + pub field: i32, +} + +fn main() {} diff --git a/test_suite/tests/ui/malformed/str_suffix.stderr b/test_suite/tests/ui/malformed/str_suffix.stderr new file mode 100644 index 000000000..3d4beae65 --- /dev/null +++ b/test_suite/tests/ui/malformed/str_suffix.stderr @@ -0,0 +1,11 @@ +error: unexpected suffix `huh` on string literal + --> tests/ui/malformed/str_suffix.rs:4:17 + | +4 | #[serde(bound = ""huh)] + | ^^^^^ + +error: unexpected suffix `what` on string literal + --> tests/ui/malformed/str_suffix.rs:6:22 + | +6 | #[serde(rename = ""what)] + | ^^^^^^ From 8264e002a7598d25b11c7e11df1b1acfa0938620 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 9 Jul 2023 10:55:43 -0700 Subject: [PATCH 2/2] Reject suffixed string literals inside serde attrs --- serde_derive/Cargo.toml | 2 +- serde_derive/src/internals/attr.rs | 7 +++++++ serde_derive_internals/Cargo.toml | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/serde_derive/Cargo.toml b/serde_derive/Cargo.toml index 1897cb1a6..46246bb60 100644 --- a/serde_derive/Cargo.toml +++ b/serde_derive/Cargo.toml @@ -24,7 +24,7 @@ proc-macro = true [dependencies] proc-macro2 = "1.0" quote = "1.0" -syn = "2.0.24" +syn = "2.0.25" [dev-dependencies] serde = { version = "1.0", path = "../serde" } diff --git a/serde_derive/src/internals/attr.rs b/serde_derive/src/internals/attr.rs index bff82191b..42212a64d 100644 --- a/serde_derive/src/internals/attr.rs +++ b/serde_derive/src/internals/attr.rs @@ -1418,6 +1418,13 @@ fn get_lit_str2( .. }) = value { + let suffix = lit.suffix(); + if !suffix.is_empty() { + cx.error_spanned_by( + lit, + format!("unexpected suffix `{}` on string literal", suffix), + ); + } Ok(Some(lit.clone())) } else { cx.error_spanned_by( diff --git a/serde_derive_internals/Cargo.toml b/serde_derive_internals/Cargo.toml index 703a43cbb..eb084af67 100644 --- a/serde_derive_internals/Cargo.toml +++ b/serde_derive_internals/Cargo.toml @@ -17,7 +17,7 @@ path = "lib.rs" [dependencies] proc-macro2 = "1.0" quote = "1.0" -syn = { version = "2.0.24", default-features = false, features = ["clone-impls", "derive", "parsing", "printing"] } +syn = { version = "2.0.25", default-features = false, features = ["clone-impls", "derive", "parsing", "printing"] } [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"]