Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message on remote derive duplicate generics #2331

Merged
merged 2 commits into from Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion serde_derive/Cargo.toml
Expand Up @@ -24,7 +24,7 @@ proc-macro = true
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0.90"
syn = "1.0.104"

[dev-dependencies]
serde = { version = "1.0", path = "../serde" }
Expand Down
23 changes: 23 additions & 0 deletions serde_derive/src/internals/check.rs
Expand Up @@ -6,6 +6,7 @@ use syn::{Member, Type};
/// Cross-cutting checks that require looking at more than a single attrs
/// object. Simpler checks should happen when parsing and building the attrs.
pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
check_remote_generic(cx, cont);
check_getter(cx, cont);
check_flatten(cx, cont);
check_identifier(cx, cont);
Expand All @@ -16,6 +17,28 @@ pub fn check(cx: &Ctxt, cont: &mut Container, derive: Derive) {
check_from_and_try_from(cx, cont);
}

/// Remote derive definition type must have either all of the generics of the
/// remote type:
///
/// #[serde(remote = "Generic")]
/// struct Generic<T> {…}
///
/// or none of them, i.e. defining impls for one concrete instantiation of the
/// remote type only:
///
/// #[serde(remote = "Generic<T>")]
/// struct ConcreteDef {…}
///
fn check_remote_generic(cx: &Ctxt, cont: &Container) {
if let Some(remote) = cont.attrs.remote() {
let local_has_generic = !cont.generics.params.is_empty();
let remote_has_generic = !remote.segments.last().unwrap().arguments.is_none();
if local_has_generic && remote_has_generic {
cx.error_spanned_by(remote, "remove generic parameters from this path");
}
}
}

/// Getters are only allowed inside structs (not enums) with the `remote`
/// attribute.
fn check_getter(cx: &Ctxt, cont: &Container) {
Expand Down
2 changes: 1 addition & 1 deletion serde_derive_internals/Cargo.toml
Expand Up @@ -17,7 +17,7 @@ path = "lib.rs"
[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0.90", default-features = false, features = ["derive", "parsing", "printing", "clone-impls"] }
syn = { version = "1.0.104", default-features = false, features = ["derive", "parsing", "printing", "clone-impls"] }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
17 changes: 17 additions & 0 deletions test_suite/tests/ui/remote/double_generic.rs
@@ -0,0 +1,17 @@
use serde_derive::{Deserialize, Serialize};

mod remote {
pub struct Struct<T, U> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new struct seems not used.

pub t: T,
pub u: U,
}
}

#[derive(Serialize, Deserialize)]
#[serde(remote = "remote::StructGeneric<u8>")]
struct StructDef<U> {
t: u8,
u: U,
}

fn main() {}
5 changes: 5 additions & 0 deletions test_suite/tests/ui/remote/double_generic.stderr
@@ -0,0 +1,5 @@
error: remove generic parameters from this path
--> tests/ui/remote/double_generic.rs:11:18
|
11 | #[serde(remote = "remote::StructGeneric<u8>")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^