From 10d6967f1750ea7ac80995eae98c2dd4d73cb7d3 Mon Sep 17 00:00:00 2001 From: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> Date: Mon, 3 Mar 2025 11:08:28 -0500 Subject: [PATCH] Ensure `#[derive(...)]` applies the `#[automatically_derived]` attribute (#786) --- src/adapter/tests.rs | 95 ++++++++++++++++++++ test_crates/automatically_derived/Cargo.toml | 6 ++ test_crates/automatically_derived/src/lib.rs | 2 + 3 files changed, 103 insertions(+) create mode 100644 test_crates/automatically_derived/Cargo.toml create mode 100644 test_crates/automatically_derived/src/lib.rs diff --git a/src/adapter/tests.rs b/src/adapter/tests.rs index c5bd58b0..e5fff384 100644 --- a/src/adapter/tests.rs +++ b/src/adapter/tests.rs @@ -5943,3 +5943,98 @@ fn non_exhaustive_attribute() { similar_asserts::assert_eq!(expected_results, results); } + +/// Ensure that `#[derive(...)]` applies the `#[automatically_derived]` attribute, +/// since that's key for the correct behavior of some of our queries. +#[test] +fn automatically_derived() { + get_test_data!(data, automatically_derived); + let adapter = RustdocAdapter::new(&data, None); + let adapter = Arc::new(&adapter); + + let query = r#" + { + Crate { + item { + ... on Struct { + impl { + attrs @output + + implemented_trait { + instantiated_name @output + bare_name @filter(op: "one_of", value: ["$traits"]) + + trait { + canonical_path { + canonical_path: path @output + } + } + } + } + } + } + } + } + "#; + + let variables: BTreeMap<&str, Vec<&str>> = btreemap! { + "traits" => vec!["Debug", "Clone", "PartialOrd", "Ord", "PartialEq", "Eq", "Hash"], + }; + let schema = + Schema::parse(include_str!("../rustdoc_schema.graphql")).expect("schema failed to parse"); + + #[derive(Debug, PartialOrd, Ord, PartialEq, Eq, serde::Deserialize)] + struct Output { + instantiated_name: String, + attrs: Vec, + canonical_path: Vec, + } + + let mut results: Vec = + trustfall::execute_query(&schema, adapter.clone(), query, variables) + .expect("failed to run query") + .map(|row| row.try_into_struct().expect("shape mismatch")) + .collect(); + results.sort_unstable(); + + let mut expected_results = vec![ + Output { + instantiated_name: "Clone".into(), + attrs: vec!["#[automatically_derived]".into()], + canonical_path: vec!["core".into(), "clone".into(), "Clone".into()], + }, + Output { + instantiated_name: "Debug".into(), + attrs: vec!["#[automatically_derived]".into()], + canonical_path: vec!["core".into(), "fmt".into(), "Debug".into()], + }, + Output { + instantiated_name: "Eq".into(), + attrs: vec!["#[automatically_derived]".into()], + canonical_path: vec!["core".into(), "cmp".into(), "Eq".into()], + }, + Output { + instantiated_name: "Hash".into(), + attrs: vec!["#[automatically_derived]".into()], + canonical_path: vec!["core".into(), "hash".into(), "Hash".into()], + }, + Output { + instantiated_name: "Ord".into(), + attrs: vec!["#[automatically_derived]".into()], + canonical_path: vec!["core".into(), "cmp".into(), "Ord".into()], + }, + Output { + instantiated_name: "PartialEq".into(), + attrs: vec!["#[automatically_derived]".into()], + canonical_path: vec!["core".into(), "cmp".into(), "PartialEq".into()], + }, + Output { + instantiated_name: "PartialOrd".into(), + attrs: vec!["#[automatically_derived]".into()], + canonical_path: vec!["core".into(), "cmp".into(), "PartialOrd".into()], + }, + ]; + expected_results.sort_unstable(); + + similar_asserts::assert_eq!(expected_results, results); +} diff --git a/test_crates/automatically_derived/Cargo.toml b/test_crates/automatically_derived/Cargo.toml new file mode 100644 index 00000000..5e841717 --- /dev/null +++ b/test_crates/automatically_derived/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "automatically_derived" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/test_crates/automatically_derived/src/lib.rs b/test_crates/automatically_derived/src/lib.rs new file mode 100644 index 00000000..74915874 --- /dev/null +++ b/test_crates/automatically_derived/src/lib.rs @@ -0,0 +1,2 @@ +#[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)] +pub struct Example(i64);