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

Ensure #[derive(...)] applies the #[automatically_derived] attribute #786

Merged
merged 1 commit into from
Mar 3, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions src/adapter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
canonical_path: Vec<String>,
}

let mut results: Vec<Output> =
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);
}
6 changes: 6 additions & 0 deletions test_crates/automatically_derived/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "automatically_derived"
version = "0.1.0"
edition = "2024"

[dependencies]
2 changes: 2 additions & 0 deletions test_crates/automatically_derived/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
pub struct Example(i64);