rust/src/test/ui/foreign
Ayaz Hafiz 230393993f
Don't visit foreign function bodies when lowering ast to hir
Previously the existence of bodies inside a foreign function block would
cause a panic in the hir `NodeCollector` during its collection of crate
bodies to compute a crate hash:

e59b08e62e/src/librustc_middle/hir/map/collector.rs (L154-L158)

The collector walks the hir tree and creates a map of hir nodes, then
attaching bodies in the crate to their owner in the map. For a code like

```rust
extern "C" {
    fn f() {
        fn g() {}
    }
}
```

The crate bodies include the body of the function `g`. But foreign
functions cannot have bodies, and while the parser AST permits a foreign
function to have a body, the hir doesn't. This means that the body of
`f` is not present in the hir, and so neither is `g`. So when the
`NodeCollector` finishes the walking the hir, it has no record of `g`,
cannot find an owner for the body of `g` it sees in the crate bodies,
and blows up.

Why do the crate bodies include the body of `g`? The AST walker has a
need a for walking function bodies, and FFIs share the same AST node as
functions in other contexts.

There are at least two options to fix this:

- Don't unwrap the map entry for an hir node in the `NodeCollector`
- Modifier the ast->hir lowering visitor to ignore foreign function
  blocks

I don't think the first is preferrable, since we want to know when we
can't find a body for an hir node that we thought had one (dropping this
information may lead to an invalid hash). So this commit implements the
second option.

Closes #74120
2020-07-09 19:21:14 -07:00
..
auxiliary Group all ui tests and move to abi #62593 2019-08-15 16:00:54 +02:00
foreign-mod-src tests: Move run-pass tests without naming conflicts to ui 2019-07-27 18:56:16 +03:00
foreign-src tests: Move run-pass tests without naming conflicts to ui 2019-07-27 18:56:16 +03:00
foreign-fn-linkname.rs tests: Move run-pass tests without naming conflicts to ui 2019-07-27 18:56:16 +03:00
foreign-int-types.rs tests: Move run-pass tests without naming conflicts to ui 2019-07-27 18:56:16 +03:00
foreign-mod-unused-const.rs tests: Move run-pass tests without naming conflicts to ui 2019-07-27 18:56:16 +03:00
foreign-truncated-arguments.rs tests: Move run-pass tests without naming conflicts to ui 2019-07-27 18:56:16 +03:00
foreign2.rs tests: Move run-pass tests without naming conflicts to ui 2019-07-27 18:56:16 +03:00
issue-74120-lowering-of-ffi-block-bodies.rs Don't visit foreign function bodies when lowering ast to hir 2020-07-09 19:21:14 -07:00
issue-74120-lowering-of-ffi-block-bodies.stderr Don't visit foreign function bodies when lowering ast to hir 2020-07-09 19:21:14 -07:00