Skip to content

rustdoc: more js cleanup#151386

Open
lolbinarycat wants to merge 7 commits intorust-lang:mainfrom
lolbinarycat:rustdoc-ts-cleanup
Open

rustdoc: more js cleanup#151386
lolbinarycat wants to merge 7 commits intorust-lang:mainfrom
lolbinarycat:rustdoc-ts-cleanup

Conversation

@lolbinarycat
Copy link
Contributor

Continuing the effort of removing @ts-expect-error from the codebase wherever possible, this time focusing on main.js. Found some oddities with register_type_impls, fixed most of them, but the one that I couldn't figure out is what's going on with sidebarTraitList. It's queried, then if there are any trait imps, unconditionally overwritten, then latter code assumes that one of these two things has initialized it, but it's not obvious why this would be the case, or if there's a reason this wasn't done in a more straightforwards way.

r? @GuillaumeGomez

@rustbot
Copy link
Collaborator

rustbot commented Jan 19, 2026

Some changes occurred in HTML/CSS/JS.

cc @GuillaumeGomez

@rustbot rustbot added A-rustdoc-js Area: Rustdoc's JS front-end S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Jan 19, 2026
if (v !== null) {
return v;
}
throw "rustdoc var " + name + " is missing";
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
throw "rustdoc var " + name + " is missing";
throw `rustdoc var "${name}" is missing`;

while (elem) {
if (elem.tagName === "DETAILS") {
// @ts-expect-error
if (elem instanceof HTMLDetailsElement) {
Copy link
Member

Choose a reason for hiding this comment

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

Although it's more correct, I find it so much less readable. ^^' (nothing to change here, just me sad that better code doesn't always look better)

let methods = document.querySelector(".sidebar .block.method");
let associatedTypes = document.querySelector(".sidebar .block.associatedtype");
let associatedConstants = document.querySelector(".sidebar .block.associatedconstant");

Copy link
Member

Choose a reason for hiding this comment

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

nitpick: any reason for this new line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope. will fix.

}
}
const template = document.createElement("template");
// @ts-expect-error
Copy link
Member

Choose a reason for hiding this comment

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

What error is expected here btw?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

string | 0 not assignable to string | null. this lead me to investigating TypeImpls generation and reading the code more thoroughly, which led me to realize only the second item in the array could be 0, thus the more complicated type definition.

a.href = `#${nonnull(template.content.querySelector(".impl")).id}`;
// @ts-expect-error
a.href = `#${template.content.querySelector(".impl").id}`;
a.textContent = traitName;
Copy link
Member

Choose a reason for hiding this comment

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

Why does this one has expect-error now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It shouldn't, I think the intermediate commit got messed up by some rebasing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, wait, I misread the diff, this is also Type 'string | 0' is not assignable to type 'string | null'., but this time the error is somewhat legitimate (well, it's a limitation of typescript not realizing that the value of isTrait is linked to the type of traitName)

Copy link
Member

Choose a reason for hiding this comment

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

Can you add this as comment so future us will not have to search again for this explanation please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah will do. i originally didn't because i hoped i could find a way to get rid of it easily.

mainContent.insertBefore(outputListHeader, trait_implementations_header);
mainContent.insertBefore(outputList, trait_implementations_header);
} else {
const mainContent = nonnull(document.querySelector("#main-content"));
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch. 👍

@GuillaumeGomez
Copy link
Member

Apart from minor improvements, looks much better, thanks!

@lolbinarycat lolbinarycat force-pushed the rustdoc-ts-cleanup branch 2 times, most recently from 197be7f to 519c0d9 Compare January 19, 2026 23:39
Copy link
Member

@GuillaumeGomez GuillaumeGomez left a comment

Choose a reason for hiding this comment

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

Once the comment has been added to the code and the CI is green, r=me.

View changes since this review

@lolbinarycat
Copy link
Contributor Author

Typing out "typescript doesn't understand this relation" made me wonder if I could make it understand, and the answer was yes, so...

Instead of adding a comment explaining the @ts-expect-error, I just removed it.

this allows TypeScript to understand the relation between isTrait
and traitName's type, getting rid of a type error.
const text = impList[0];
const isTrait = impList[1] !== 0;
const traitName = impList[1];
const isTrait = typeof traitName === "string";
Copy link
Member

Choose a reason for hiding this comment

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

That's not the same check at all. Is there a reason why we go from integer comparison to checking it's a string? (lack of context on my side here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

impList[1] has type string | 0, so this is actually an equivalent check, though I could see how it could really not look like it without that context.

Copy link
Member

Choose a reason for hiding this comment

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

I see. JS is weird and we really abuse it sometimes. XD

@GuillaumeGomez
Copy link
Member

Thanks!

@bors r+ rollup

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 13, 2026

📌 Commit bd1c36a has been approved by GuillaumeGomez

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 13, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Feb 14, 2026
…=GuillaumeGomez

rustdoc: more js cleanup

Continuing the effort of removing `@ts-expect-error` from the codebase wherever possible, this time focusing on `main.js`.  Found some oddities with `register_type_impls`, fixed most of them, but the one that I couldn't figure out is what's going on with `sidebarTraitList`.  It's queried, then if there are any trait imps, unconditionally overwritten, then latter code assumes that one of these two things has initialized it, but it's not obvious why this would be the case, or if there's a reason this wasn't done in a more straightforwards way.

r? @GuillaumeGomez
rust-bors bot pushed a commit that referenced this pull request Feb 14, 2026
Rollup of 11 pull requests

Successful merges:

 - #151998 (Set hidden visibility on naked functions in compiler-builtins)
 - #149460 (rustdoc: sort stable items first)
 - #152076 (Feed `ErrorGuaranteed` from late lifetime resolution errors through to bound variable resolution)
 - #152471 (improve associated-type suggestions from bounds)
 - #152573 (move `escape_symbol_name` to `cg_ssa`)
 - #152594 (c-variadic: implement `va_arg` for `wasm64`)
 - #151386 (rustdoc: more js cleanup)
 - #152567 (nix-dev-shell: fix a typo)
 - #152568 (Port `#[lang]` and `#[panic_handler]` to the new attribute parsers)
 - #152575 (layout_of unexpected rigid alias delayed bug)
 - #152587 (A couple of tiny polonius things)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-rustdoc-js Area: Rustdoc's JS front-end S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants