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

reproducer: fails to query trait imported from a dependecy #333

Open
wants to merge 1 commit into
base: rustdoc-v29
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions src/adapter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,69 @@ fn impl_for_ref() {
);
}

#[test]
fn impl_name_for_impl_owners() {
let path = "./localdata/test_data/impl_for_ref/rustdoc.json";
let content = std::fs::read_to_string(path)
.with_context(|| format!("Could not load {path} file, did you forget to run ./scripts/regenerate_test_rustdocs.sh ?"))
.expect("failed to load rustdoc");

let crate_ = serde_json::from_str(&content).expect("failed to parse rustdoc");
let indexed_crate = IndexedCrate::new(&crate_);
let adapter = RustdocAdapter::new(&indexed_crate, None);
Comment on lines +104 to +111
Copy link
Owner

Choose a reason for hiding this comment

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

Note how only rustdoc from impl_for_ref is included in the adapter. No dummy_ext rustdoc is loaded, so it's as if none of its types exist from the perspective of the RustdocAdapter.

Copy link
Author

Choose a reason for hiding this comment

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

I don't know the internals but that's not entirely true.

I have two reference to the external trait in the rustdoc json generated for impl_for_ref:

{
  "0:8": {
    "id": "0:8",
    "crate_id": 0,
    "name": null,
    "span": {
      "filename": "src/lib.rs",
      "begin": [
        9,
        0
      ],
      "end": [
        11,
        1
      ]
    },
    "visibility": "default",
    "docs": null,
    "links": {},
    "attrs": [],
    "deprecation": null,
    "inner": {
      "impl": {
        "is_unsafe": false,
        "generics": {
          "params": [],
          "where_predicates": []
        },
        "provided_trait_methods": [],
        "trait": {
          "name": "DummyExternalTrait",
          "id": "20:3:2305",
          "args": {
            "angle_bracketed": {
              "args": [],
              "bindings": []
            }
          }
        },
        "for": {
          "resolved_path": {
            "name": "StringHolder",
            "id": "0:5:2306",
            "args": {
              "angle_bracketed": {
                "args": [],
                "bindings": []
              }
            }
          }
        },
        "items": [
          "0:9:2308"
        ],
        "negative": false,
        "synthetic": false,
        "blanket_impl": null
      }
    }
  }
}
{
  "20:3:2305": {
    "crate_id": 20,
    "path": [
      "dummy_ext",
      "DummyExternalTrait"
    ],
    "kind": "trait"
  }
}

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately this wouldn't help.

I am puzzled. I turned to whole thing into a static mutable, add added this to the test and now it passes.

    crate::indexed_crate::add_manual_trait_item(ManualTraitItem {
        name: "DummyExternalTrait",
        is_auto: false,
        is_unsafe: false,
    });

Copy link
Owner

Choose a reason for hiding this comment

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

What I mean is that the actual definition of item 20:3:2305 AKA DummyExternalTrait is not present in the file: we don't know its attributes, its doc comment, its items, etc. We just know it as a foreign item from an unknown version of a crate called dummy_ext.

With the approach you propose, queries like the following will produce unexpected results: playground

query {
  Crate {
    item {
      ... on ImplOwner {
        name @output
        impl {
          implemented_trait {
            name @output(name: "impls")

            impls_: trait {
              docs @output  # <-- no data here
              doc_hidden @output  # <-- no data here either

              importable_path {  # <-- no known instances of this edge
                path @output
              }
            }
          }
        }
      }
    }
  }
}

It will claim that the implemented DummyExternalTrait trait is not actually importable from other crates (obviously false — it's implemented by a type in another crate!), because the data to determine its import paths was not present.

That sounds like a pretty serious footgun to me — one made even more unpredictable and dangerous if we add add_manual_trait_item() to the crate's public API. This is why I'd love to hear more about your use case — there may be a less footgun-y way to accomplish what you're after.

Copy link
Author

Choose a reason for hiding this comment

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

This is why I'd love to hear more about your use case

I am trying to generate automated documentation for the zed editor actions (those are struct implementing the Action trait spread across several crates in a cargo workspace).
I could probably parse everything with syn but I was curious about trustfall.

Copy link
Owner

Choose a reason for hiding this comment

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

In that use case, is it guaranteed that you'll have at most one major version of each crate you are querying in the workspace? (This isn't true in general, for example it isn't true when semver-checking cargo-semver-checks.)

If that's the case, you should be able to accomplish the goal with Trustfall in a better (but slightly more work) way: you could generate the rustdoc for all the crates, and use the external item description

{
  "20:3:2305": {
    "crate_id": 20,
    "path": [
      "dummy_ext",
      "DummyExternalTrait"
    ],
    "kind": "trait"
  }
}

to then look up the item in the other rustdoc using the import path index: crate dummy_ext import path dummy_ext::DummyExternalTrait.

Such a prototype would lay the groundwork for trustfall-rustdoc-adapter supporting cross-crate querying once the compiler MCP is approved and the rustdoc linking limitation is lifted. I'd love to see it and if you're interested in building it, I'd love to support you in it.

But I totally understand if that sounds like too much work, and you'd prefer to just fork the crate and make the add_manual_trait_item() function public in your build.

Copy link
Author

Choose a reason for hiding this comment

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

I will give a try, this sound like a lot of fun! I'll let you know if I need your help later.

Thanks a lot for the help so far !

Copy link
Owner

Choose a reason for hiding this comment

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

Awesome! Always happy to help :)

Copy link
Owner

Choose a reason for hiding this comment

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

Hi! I wanted to check in and see how you're doing on this. Are you still working on the prototype, or did something else take priority? Anything I can do to help you along?


let query = r#"
{
Crate {
item {
... on ImplOwner {
name @output
impl {
implemented_trait {
name @output(name: "impls")
}
}
}
}
}
}
"#;

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,
impls: Option<String>,
}

let vars: BTreeMap<&str, &str> = Default::default();
let mut results: Vec<_> = trustfall::execute_query(&schema, adapter.into(), query, vars)
.expect("failed to run query")
.map(|row| row.try_into_struct().expect("shape mismatch"))
.collect();

results.sort_unstable();

// OK
assert!(results.contains(&Output {
name: "StringHolder".to_string(),
impls: Some("PartialEq".to_string())
}));

// Ok
assert!(results.contains(&Output {
name: "StringHolder".to_string(),
impls: Some("Trait".to_string())
}));

// Fails !
assert!(results.contains(&Output {
name: "StringHolder".to_string(),
impls: Some("DummyExternalTrait".to_string())
}));
}
#[test]
fn rustdoc_finds_supertrait() {
let path = "./localdata/test_data/supertrait/rustdoc.json";
Expand Down
6 changes: 6 additions & 0 deletions test_crates/dummy_ext/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "dummy_ext"
version = "0.1.0"
edition = "2021"

[dependencies]
3 changes: 3 additions & 0 deletions test_crates/dummy_ext/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait DummyExternalTrait {
fn do_something(&self);
}
1 change: 1 addition & 0 deletions test_crates/impl_for_ref/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
dummy_ext = { path = "../dummy_ext" }
8 changes: 8 additions & 0 deletions test_crates/impl_for_ref/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use dummy_ext::DummyExternalTrait;
pub trait Trait {}

pub struct StringHolder {
content: String,
}

impl Trait for StringHolder {}
impl DummyExternalTrait for StringHolder {
fn do_something(&self) {}
}

impl<'a> PartialEq<StringHolder> for &'a str {
fn eq(&self, other: &StringHolder) -> bool {
(*self).eq(&other.content)
Expand Down
Loading