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

Don't visit foreign function bodies when lowering ast to hir #74204

Merged
merged 5 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 20 additions & 1 deletion src/librustc_ast_lowering/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use rustc_ast::ast::*;
use rustc_ast::attr;
use rustc_ast::node_id::NodeMap;
use rustc_ast::ptr::P;
use rustc_ast::visit::{self, AssocCtxt, Visitor};
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
use rustc_ast::walk_list;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::struct_span_err;
use rustc_hir as hir;
Expand Down Expand Up @@ -76,6 +77,24 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
}
}

fn visit_fn(&mut self, fk: FnKind<'a>, _: Span, _: NodeId) {
match fk {
FnKind::Fn(FnCtxt::Foreign, _, sig, _, _) => {
self.visit_fn_header(&sig.header);
visit::walk_fn_decl(self, &sig.decl);
}
ayazhafiz marked this conversation as resolved.
Show resolved Hide resolved
FnKind::Fn(_, _, sig, _, body) => {
self.visit_fn_header(&sig.header);
visit::walk_fn_decl(self, &sig.decl);
walk_list!(self, visit_block, body);
}
FnKind::Closure(decl, body) => {
visit::walk_fn_decl(self, decl);
self.visit_expr(body);
}
ayazhafiz marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
self.lctx.with_hir_id_owner(item.id, |lctx| match ctxt {
AssocCtxt::Trait => {
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/foreign/issue-74120-lowering-of-ffi-block-bodies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Previously this ICE'd because `fn g()` would be lowered, but the block associated with `fn f()`
// wasn't.

// compile-flags: --crate-type=lib

extern "C" {
fn f() {
//~^ incorrect function inside `extern` block
fn g() {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: incorrect function inside `extern` block
--> $DIR/issue-74120-lowering-of-ffi-block-bodies.rs:7:8
|
LL | extern "C" {
| ---------- `extern` blocks define existing foreign functions and functions inside of them cannot have a body
LL | fn f() {
| ________^___-
| | |
| | cannot have a body
LL | |
LL | | fn g() {}
LL | | }
| |_____- help: remove the invalid body: `;`
|
= help: you might have meant to write a function accessible through FFI, which can be done by writing `extern fn` outside of the `extern` block
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html

error: aborting due to previous error