Skip to content

Fix ICE #138415 for invalid extern function body #138854

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Mar 24, 2025
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
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,8 +1724,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
return;
};
let define_opaque = define_opaque.iter().filter_map(|(id, path)| {
let res = self.resolver.get_partial_res(*id).unwrap();
let Some(did) = res.expect_full_res().opt_def_id() else {
let res = self.resolver.get_partial_res(*id);
let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) else {
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
return None;
};
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ pub(crate) mod builtin {
}

/// Provide a list of type aliases and other opaque-type-containing type definitions.
/// This list will be used in the body of the item it is applied to to define opaque
/// This list will be used in the body of the item it is applied to define opaque
/// types' hidden types.
/// Can only be applied to things that have bodies.
#[unstable(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![feature(type_alias_impl_trait)]

extern "C" {
fn a() {
//~^ ERROR incorrect function inside `extern` block
#[define_opaque(String)]
fn c() {}
}
}

pub fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: incorrect function inside `extern` block
--> $DIR/invalid-extern-fn-body.rs:4:8
|
LL | extern "C" {
| ---------- `extern` blocks define existing foreign functions and functions inside of them cannot have a body
LL | fn a() {
| ________^___-
| | |
| | cannot have a body
LL | |
LL | | #[define_opaque(String)]
LL | | fn c() {}
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 1 previous error

Loading