Skip to content

Commit

Permalink
Fix crate item lookup by import path for @optional import paths. (#678
Browse files Browse the repository at this point in the history
)
  • Loading branch information
obi1kenobi authored Dec 18, 2024
1 parent 7165cfc commit 15caf7a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/adapter/optimizations/item_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ pub(crate) fn resolve_crate_items<'a, V: AsVertex<Vertex<'a>> + 'a>(
) -> ContextOutcomeIterator<'a, V, VertexIterator<'a, Vertex<'a>>> {
let destination = resolve_info.destination();

// Is the `importable_path` edge being resolved in a subsequent step?
// Is the `importable_path` edge being resolved in a mandatory fashion in a subsequent step?
if let Some(neighbor_info) = destination
.first_edge("importable_path")
.first_mandatory_edge("importable_path")
.as_ref()
.map(|x| x.destination())
{
Expand Down
60 changes: 60 additions & 0 deletions src/adapter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3941,3 +3941,63 @@ fn extern_fn() {
expected_results.sort_unstable();
similar_asserts::assert_eq!(expected_results, results);
}

#[test]
fn item_lookup_by_path_optimization() {
// Any test crate with non-public top-level items would work for this test.
get_test_data!(data, associated_consts);
let adapter = RustdocAdapter::new(&data, None);
let adapter = Arc::new(&adapter);

let query = r#"
{
Crate {
item {
... on Function {
name @output
# Since this edge is optional, this query matches:
# - functions with an importable path matching the filter, and
# - functions that *do not have* importable paths at all.
#
# Failure to return both of these means we have a bug in
# the "item lookup by importable path" optimization code path.
importable_path @optional {
public_api @output
path @output @filter(op: "=", value: ["$path"])
}
}
}
}
}
"#;

let variables = btreemap! {
"path" => vec!["associated_consts", "will_not_match", "anything"],
};

let schema =
Schema::parse(include_str!("../rustdoc_schema.graphql")).expect("schema failed to parse");

#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, serde::Deserialize)]
struct Output {
name: String,
public_api: Option<bool>,
path: Option<Vec<String>>,
}

let mut results: Vec<_> =
trustfall::execute_query(&schema, adapter.clone(), query, variables.clone())
.expect("failed to run query")
.map(|row| row.try_into_struct().expect("shape mismatch"))
.collect();
results.sort_unstable();

let mut expected_results = vec![Output {
name: "min_batch_size".into(),
public_api: None,
path: None,
}];
expected_results.sort_unstable();
similar_asserts::assert_eq!(expected_results, results);
}

0 comments on commit 15caf7a

Please sign in to comment.