Rollup merge of #151399 - unresolved-delegation-ice, r=petrochenkov

Generate error delegation when delegation is not resolved

This PR is a part of the delegation feature rust-lang/rust#118212 and fixes rust-lang/rust#151356.

r? @petrochenkov
This commit is contained in:
Guillaume Gomez 2026-01-20 19:50:09 +01:00 committed by GitHub
commit bfc2fe9080
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 4 deletions

View file

@ -152,10 +152,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
) -> DelegationResults<'hir> {
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
let ids = self.get_delegation_ids(
self.resolver.delegation_infos[&self.local_def_id(item_id)].resolution_node,
span,
);
// Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
let ids = if let Some(delegation_info) =
self.resolver.delegation_infos.get(&self.local_def_id(item_id))
{
self.get_delegation_ids(delegation_info.resolution_node, span)
} else {
return self.generate_delegation_error(
self.dcx().span_delayed_bug(
span,
format!("LoweringContext: the delegation {:?} is unresolved", item_id),
),
span,
delegation,
);
};
match ids {
Ok(ids) => {

View file

@ -0,0 +1,11 @@
#![allow(incomplete_features)]
#![feature(fn_delegation)]
extern "C" {
fn a() {
//~^ ERROR incorrect function inside `extern` block
reuse foo {}
}
}
pub fn main() {}

View file

@ -0,0 +1,19 @@
error: incorrect function inside `extern` block
--> $DIR/unresolved-delegation-ice-151356.rs:5: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 | | reuse foo {}
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