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

Fix rare ICE during typeck in rustdoc scrape_examples #90349

Merged
merged 1 commit into from
Nov 1, 2021

Conversation

willcrichton
Copy link
Contributor

@willcrichton willcrichton commented Oct 27, 2021

While testing the --scrape-examples extension on the wasmtime repository, I found some additional edge cases. Specifically, when asking to typecheck a body containing a function call, I would sometimes get an ICE if:

  • The body doesn't exist
  • The function's HIR node didn't have a type

This adds checks for both of those conditions.

(Also this updates a test to check that the sources of a reverse-dependency are correctly generated and linked.)

r? @jyn514

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 27, 2021
src/librustdoc/scrape_examples.rs Outdated Show resolved Hide resolved
@camelid camelid added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Oct 27, 2021
src/librustdoc/scrape_examples.rs Outdated Show resolved Hide resolved
Comment on lines +149 to +156
match types.node_type_opt(f.hir_id) {
Some(ty) => (ty, ex.span),
None => {
return;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't see how this can be related to overriding the typeck query, even the overridden version still calls DEFAULT_QUERY_PROVIDERS.typeck. Can you post the error you were seeing before?

@jyn514 jyn514 added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 29, 2021
@willcrichton
Copy link
Contributor Author

@jyn514 the specific error I encountered was programs with const expressions in type declarations:

const fn f() -> usize { 4 }
pub struct Foo([usize; foobar::f()]);

Then the visitor would find the expression foobar::f(), ask for the type of the body containing it, and then get an ICE. So my solution was to first check that a body exists containing the expression.

Specifically, this line assumes a body exists: https://github.com/rust-lang/rust/blob/master/src/librustdoc/core.rs#L290

Note that the types.node_type_opt change actually solves a different ICE, which I haven't been able to reproduce a test case for.

@jyn514
Copy link
Member

jyn514 commented Oct 29, 2021

Then the visitor would find the expression foobar::f(), ask for the type of the body containing it, and then get an ICE. So my solution was to first check that a body exists containing the expression.

I don't understand though; why isn't the type of the body available? Is this something specific to constants? If so I think the comment about overriding typeck is misleading and should be removed.

@willcrichton
Copy link
Contributor Author

It's not that the type of the body isn't available, it's that there is no body. A type definition has a DefId, but it doesn't have a body, only functions do (AFAIK). TyCtxt::typeck just takes any LocalDefId as input, but the Rustdoc typeck provider assumes that that LocalDefId corresponds to an item that actually has a body.

@jyn514
Copy link
Member

jyn514 commented Oct 29, 2021

the Rustdoc typeck provider assumes that that LocalDefId corresponds to an item that actually has a body.

Ok. That's a pre-existing bug then - can you fix that instead of special-casing constant items only for scrape-examples? Otherwise I'm worried that rustdoc will ICE elsewhere and we just haven't found the code that triggers it yet.

@willcrichton
Copy link
Contributor Author

Upon further investigation, it seems like typeck is only designed to work for items with bodies. I changed the typeck provider to not panic if a body doesn't exist, and then rustc promptly ICEs here: https://github.com/rust-lang/rust/blob/master/compiler/rustc_typeck/src/check/mod.rs#L361

So I think it's correct that the caller of typeck needs to check that the body exists, it shouldn't be handled by the typeck provider.

@willcrichton
Copy link
Contributor Author

@jyn514 does that resolve your concern?

@jyn514
Copy link
Member

jyn514 commented Oct 29, 2021

@willcrichton I think so - I'm still slightly confused how this can happen when visitor passes OnlyBodies, but not enough to block on it. Can you update the comment to say "the caller needs to ensure this is a function body" rather having a red herring about overriding typeck?

@willcrichton
Copy link
Contributor Author

Done!

@jyn514
Copy link
Member

jyn514 commented Oct 31, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Oct 31, 2021

📌 Commit b8ecc9f has been approved by jyn514

@bors bors 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 31, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 31, 2021
…n514

Fix rare ICE during typeck in rustdoc scrape_examples

While testing the `--scrape-examples` extension on the [wasmtime](https://github.com/bytecodealliance/wasmtime) repository, I found some additional edge cases. Specifically, when asking to typecheck a body containing a function call, I would sometimes get an ICE if:
* The body doesn't exist
* The function's HIR node didn't have a type

This adds checks for both of those conditions.

(Also this updates a test to check that the sources of a reverse-dependency are correctly generated and linked.)

r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 1, 2021
…askrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#89826 (Feature gate + make must_not_suspend allow-by-default)
 - rust-lang#89929 (Handling submodule update failures more gracefully from x.py)
 - rust-lang#90333 (rustdoc: remove flicker during page load)
 - rust-lang#90349 (Fix rare ICE during typeck in rustdoc scrape_examples)
 - rust-lang#90398 (Document `doc(keyword)` unstable attribute)
 - rust-lang#90441 (Test that promotion follows references when looking for drop)
 - rust-lang#90450 (Remove `rustc_hir::hir_id::HirIdVec`)
 - rust-lang#90452 (Remove unnecessary `Option` from `promote_candidate` return type)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 3730485 into rust-lang:master Nov 1, 2021
@rustbot rustbot added this to the 1.58.0 milestone Nov 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants