Skip to content

rustdoc-search: clean up type unification and "unboxing" #112233

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

Merged
merged 4 commits into from
Jun 15, 2023
Merged
Changes from 1 commit
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
Next Next commit
rustdoc-search: simplify JS in checkGenerics
  • Loading branch information
notriddle committed Jun 12, 2023
commit 04f4493722a29018b85e6c2893c15ab0285d5dcd
136 changes: 68 additions & 68 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ function initSearch(rawSearchIndex) {

/**
* This function checks if the object (`row`) generics match the given type (`elem`)
* generics. If there are no generics on `row`, `defaultDistance` is returned.
* generics.
*
* @param {Row} row - The object to check.
* @param {QueryElement} elem - The element from the parsed query.
Expand All @@ -1196,82 +1196,82 @@ function initSearch(rawSearchIndex) {
// This search engine implements order-agnostic unification. There
// should be no missing duplicates (generics have "bag semantics"),
// and the row is allowed to have extras.
if (elem.generics.length > 0 && row.generics.length >= elem.generics.length) {
const elems = new Map();
const addEntryToElems = function addEntryToElems(entry) {
if (entry.id === -1) {
// Pure generic, needs to check into it.
for (const inner_entry of entry.generics) {
addEntryToElems(inner_entry);
}
return;
}
let currentEntryElems;
if (elems.has(entry.id)) {
currentEntryElems = elems.get(entry.id);
} else {
currentEntryElems = [];
elems.set(entry.id, currentEntryElems);
if (elem.generics.length <= 0 || row.generics.length < elem.generics.length) {
return false;
}
const elems = new Map();
const addEntryToElems = function addEntryToElems(entry) {
if (entry.id === -1) {
// Pure generic, needs to check into it.
for (const inner_entry of entry.generics) {
addEntryToElems(inner_entry);
}
currentEntryElems.push(entry);
};
for (const entry of row.generics) {
addEntryToElems(entry);
return;
}
// We need to find the type that matches the most to remove it in order
// to move forward.
const handleGeneric = generic => {
if (!elems.has(generic.id)) {
return false;
}
const matchElems = elems.get(generic.id);
const matchIdx = matchElems.findIndex(tmp_elem => {
if (generic.generics.length > 0 && !checkGenerics(tmp_elem, generic)) {
return false;
}
return typePassesFilter(generic.typeFilter, tmp_elem.ty);
});
if (matchIdx === -1) {
return false;
}
matchElems.splice(matchIdx, 1);
if (matchElems.length === 0) {
elems.delete(generic.id);
}
return true;
};
// To do the right thing with type filters, we first process generics
// that have them, removing matching ones from the "bag," then do the
// ones with no type filter, which can match any entry regardless of its
// own type.
for (const generic of elem.generics) {
if (generic.typeFilter === TY_PRIMITIVE &&
generic.id === typeNameIdOfArrayOrSlice) {
const genericArray = {
id: typeNameIdOfArray,
typeFilter: TY_PRIMITIVE,
generics: generic.generics,
};
const genericSlice = {
id: typeNameIdOfSlice,
typeFilter: TY_PRIMITIVE,
generics: generic.generics,
};
if (!handleGeneric(genericArray) && !handleGeneric(genericSlice)) {
return false;
}
} else if (generic.typeFilter !== -1 && !handleGeneric(generic)) {
let currentEntryElems;
if (elems.has(entry.id)) {
currentEntryElems = elems.get(entry.id);
} else {
currentEntryElems = [];
elems.set(entry.id, currentEntryElems);
}
currentEntryElems.push(entry);
};
for (const entry of row.generics) {
addEntryToElems(entry);
}
// We need to find the type that matches the most to remove it in order
// to move forward.
const handleGeneric = generic => {
if (!elems.has(generic.id)) {
return false;
}
const matchElems = elems.get(generic.id);
const matchIdx = matchElems.findIndex(tmp_elem => {
if (generic.generics.length > 0 && !checkGenerics(tmp_elem, generic)) {
return false;
}
return typePassesFilter(generic.typeFilter, tmp_elem.ty);
});
if (matchIdx === -1) {
return false;
}
for (const generic of elem.generics) {
if (generic.typeFilter === -1 && !handleGeneric(generic)) {
matchElems.splice(matchIdx, 1);
if (matchElems.length === 0) {
elems.delete(generic.id);
}
return true;
};
// To do the right thing with type filters, we first process generics
// that have them, removing matching ones from the "bag," then do the
// ones with no type filter, which can match any entry regardless of its
// own type.
for (const generic of elem.generics) {
if (generic.typeFilter === TY_PRIMITIVE &&
generic.id === typeNameIdOfArrayOrSlice) {
const genericArray = {
id: typeNameIdOfArray,
typeFilter: TY_PRIMITIVE,
generics: generic.generics,
};
const genericSlice = {
id: typeNameIdOfSlice,
typeFilter: TY_PRIMITIVE,
generics: generic.generics,
};
if (!handleGeneric(genericArray) && !handleGeneric(genericSlice)) {
return false;
}
} else if (generic.typeFilter !== -1 && !handleGeneric(generic)) {
return false;
}
return true;
}
return false;
for (const generic of elem.generics) {
if (generic.typeFilter === -1 && !handleGeneric(generic)) {
return false;
}
}
return true;
}

/**
Expand Down