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
Merged
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
20 changes: 18 additions & 2 deletions src/librustdoc/scrape_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,28 @@ where
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
intravisit::walk_expr(self, ex);

// Get type of function if expression is a function call
let tcx = self.tcx;

// If we visit an item that contains an expression outside a function body,
// then we need to exit before calling typeck (which will panic). See
// test/run-make/rustdoc-scrape-examples-invalid-expr for an example.
let hir = tcx.hir();
let owner = hir.local_def_id_to_hir_id(ex.hir_id.owner);
if hir.maybe_body_owned_by(owner).is_none() {
return;
}

// Get type of function if expression is a function call
let (ty, span) = match ex.kind {
hir::ExprKind::Call(f, _) => {
let types = tcx.typeck(ex.hir_id.owner);
(types.node_type(f.hir_id), ex.span)

match types.node_type_opt(f.hir_id) {
Some(ty) => (ty, ex.span),
None => {
return;
}
}
Comment on lines +151 to +156
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?

}
hir::ExprKind::MethodCall(_, _, _, span) => {
let types = tcx.typeck(ex.hir_id.owner);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
deps := ex

-include ../rustdoc-scrape-examples-multiple/scrape.mk

all: scrape
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub struct Foo([usize; foobar::f()]);
fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const fn f() -> usize { 5 }
2 changes: 2 additions & 0 deletions src/test/run-make/rustdoc-scrape-examples-multiple/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @has foobar/fn.ok.html '//*[@class="docblock scraped-example-list"]//*[@class="prev"]' ''
// @has foobar/fn.ok.html '//*[@class="more-scraped-examples"]' ''
// @has src/ex/ex.rs.html
// @has foobar/fn.ok.html '//a[@href="../src/ex/ex.rs.html#2"]' ''

pub fn ok() {}