Skip to content

Commit bdf426a

Browse files
committed
Do not ICE when lowering invalid extern fn with bodies
Fix #75283.
1 parent db870ea commit bdf426a

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/librustc_ast_lowering/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
574574
.resolver
575575
.trait_map()
576576
.iter()
577-
.map(|(&k, v)| (self.node_id_to_hir_id[k].unwrap(), v.clone()))
577+
.filter_map(|(&k, v)| {
578+
self.node_id_to_hir_id.get(k).and_then(|id| id.as_ref()).map(|id| (*id, v.clone()))
579+
})
578580
.collect();
579581

580582
let mut def_id_to_hir_id = IndexVec::default();

src/test/ui/issues/issue-75283.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern "C" {
2+
fn lol() { //~ ERROR incorrect function inside `extern` block
3+
println!("");
4+
}
5+
}
6+
fn main() {}

src/test/ui/issues/issue-75283.stderr

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: incorrect function inside `extern` block
2+
--> $DIR/issue-75283.rs:2:8
3+
|
4+
LL | extern "C" {
5+
| ---------- `extern` blocks define existing foreign functions and functions inside of them cannot have a body
6+
LL | fn lol() {
7+
| ________^^^___-
8+
| | |
9+
| | cannot have a body
10+
LL | | println!("");
11+
LL | | }
12+
| |_____- help: remove the invalid body: `;`
13+
|
14+
= 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
15+
= note: for more information, visit https://doc.rust-lang.org/std/keyword.extern.html
16+
17+
error: aborting due to previous error
18+

0 commit comments

Comments
 (0)