Skip to content

Rollup of 6 pull requests #101854

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

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3c184db
Fix raw-dylib with link_name
dpaoliello Sep 12, 2022
7794ea5
Prefer explict closure sig types over expected ones
oli-obk Sep 13, 2022
2506aa0
jsondoclint: New Tool
aDotInTheVoid Aug 23, 2022
404b60b
Constify impl Fn* &(mut) Fn*
onestacked Sep 13, 2022
478c471
Added Tracking Issue number.
onestacked Sep 14, 2022
c5b9cb4
errors: add `emit_note`/`create_note`
davidtwco Sep 1, 2022
8a2f9c3
errors: implement `IntoDiagnosticArg` for `&T`
davidtwco Aug 30, 2022
ae51741
session: impl `IntoDiagnosticArg` for `CrateType`
davidtwco Aug 30, 2022
7d7cd17
errors: impl `IntoDiagnosticArg` for `TargetTriple`
davidtwco Aug 30, 2022
677d4d0
session: diagnostic migration lint on more fns
davidtwco Aug 19, 2022
b058e41
incremental: migrate diagnostics
davidtwco Aug 19, 2022
a7a4fe9
jsondoclint: Tree Walk Validator
aDotInTheVoid Aug 24, 2022
bb1911d
jsondoclint: Add `Kind` abstraction
aDotInTheVoid Aug 24, 2022
5f1bc6f
jsondocck: Better errors
aDotInTheVoid Aug 24, 2022
41d35a9
jsondocck: Find path to Id's not in index
aDotInTheVoid Aug 24, 2022
c98c7cb
Primitives can appear in modules.
aDotInTheVoid Aug 30, 2022
5956b56
jsondoclint: Document validator
aDotInTheVoid Sep 14, 2022
393792d
Remove check_missing_items.py
aDotInTheVoid Sep 14, 2022
24c751b
Rustdoc-Json: Add test for extern_types
aDotInTheVoid Sep 14, 2022
6e21a28
jsondoclint: More precise `Path` checks
aDotInTheVoid Sep 14, 2022
f69a6c2
jsondoclint: Fix TODO's
aDotInTheVoid Sep 14, 2022
4cdf264
cache collect_trait_impl_trait_tys
compiler-errors Sep 9, 2022
02e43ff
Rollup merge of #100754 - davidtwco:translation-incremental, r=compil…
Dylan-DPC Sep 15, 2022
1003a36
Rollup merge of #101738 - dpaoliello:linkname, r=petrochenkov
Dylan-DPC Sep 15, 2022
9e54570
Rollup merge of #101753 - oli-obk:tait_closure_args, r=compiler-errors
Dylan-DPC Sep 15, 2022
80d670d
Rollup merge of #101787 - compiler-errors:cache-rpitit, r=petrochenkov
Dylan-DPC Sep 15, 2022
1965a3c
Rollup merge of #101802 - chriss0612:const_fn_trait_ref_impls, r=fee1…
Dylan-DPC Sep 15, 2022
fe61d29
Rollup merge of #101809 - aDotInTheVoid:jsondoclint, r=GuillaumeGomez
Dylan-DPC Sep 15, 2022
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
Prev Previous commit
Next Next commit
jsondocck: Find path to Id's not in index
  • Loading branch information
aDotInTheVoid committed Sep 14, 2022
commit 41d35a97f9f33e265de53b8f04abe307d7616641
74 changes: 74 additions & 0 deletions src/tools/jsondoclint/src/json_find.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::fmt::Write;

use serde_json::Value;

#[derive(Debug, Clone)]
pub enum SelectorPart {
Field(String),
Index(usize),
}

pub type Selector = Vec<SelectorPart>;

pub fn to_jsonpath(sel: &Selector) -> String {
let mut s = String::from("$");
for part in sel {
match part {
SelectorPart::Field(name) => {
if is_jsonpath_safe(name) {
write!(&mut s, ".{}", name).unwrap();
} else {
// This is probably wrong in edge cases, but all Id's are
// just ascii alphanumerics, `-` `_`, and `:`
write!(&mut s, "[{name:?}]").unwrap();
}
}
SelectorPart::Index(idx) => write!(&mut s, "[{idx}]").unwrap(),
}
}
s
}

fn is_jsonpath_safe(s: &str) -> bool {
s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

pub fn find_selector(haystack: &Value, needle: &Value) -> Vec<Selector> {
let mut result = Vec::new();
let mut sel = Selector::new();
find_selector_recursive(haystack, needle, &mut result, &mut sel);
result
}

fn find_selector_recursive(
haystack: &Value,
needle: &Value,
result: &mut Vec<Selector>,
pos: &mut Selector,
) {
if needle == haystack {
result.push(pos.clone());
// Haystack cant both contain needle and be needle
} else {
match haystack {
Value::Null => {}
Value::Bool(_) => {}
Value::Number(_) => {}
Value::String(_) => {}
Value::Array(arr) => {
for (idx, subhaystack) in arr.iter().enumerate() {
pos.push(SelectorPart::Index(idx));
find_selector_recursive(subhaystack, needle, result, pos);
pos.pop().unwrap();
}
}
Value::Object(obj) => {
for (key, subhaystack) in obj {
pos.push(SelectorPart::Field(key.clone()));
find_selector_recursive(subhaystack, needle, result, pos);
pos.pop().unwrap();
}
}
}
}
}
28 changes: 25 additions & 3 deletions src/tools/jsondoclint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use std::env;
use anyhow::{anyhow, bail, Result};
use fs_err as fs;
use rustdoc_json_types::{Crate, Id, FORMAT_VERSION};
use serde_json::Value;

pub(crate) mod item_kind;
mod json_find;
mod validator;

#[derive(Debug)]
Expand All @@ -21,8 +23,10 @@ enum ErrorKind {

fn main() -> Result<()> {
let path = env::args().nth(1).ok_or_else(|| anyhow!("no path given"))?;
let contents = fs::read_to_string(path)?;
let contents = fs::read_to_string(&path)?;
let krate: Crate = serde_json::from_str(&contents)?;
// TODO: Only load if nessessary.
let krate_json: Value = serde_json::from_str(&contents)?;
assert_eq!(krate.format_version, FORMAT_VERSION);

let mut validator = validator::Validator::new(&krate);
Expand All @@ -31,11 +35,29 @@ fn main() -> Result<()> {
if !validator.errs.is_empty() {
for err in validator.errs {
match err.kind {
ErrorKind::NotFound => eprintln!("{}: Not Found", err.id.0),
ErrorKind::NotFound => {
let sels =
json_find::find_selector(&krate_json, &Value::String(err.id.0.clone()));
match &sels[..] {
[] => unreachable!(
"id must be in crate, or it wouldn't be reported as not found"
),
[sel] => eprintln!(
"{} not in index or paths, but refered to at '{}'",
err.id.0,
json_find::to_jsonpath(&sel)
),
[sel, ..] => eprintln!(
"{} not in index or paths, but refered to at '{}' and more",
err.id.0,
json_find::to_jsonpath(&sel)
),
}
}
ErrorKind::Custom(msg) => eprintln!("{}: {}", err.id.0, msg),
}
}
bail!("Errors validating json");
bail!("Errors validating json {path}");
}

Ok(())
Expand Down