Skip to content

Commit 0130dcb

Browse files
committed
Fix ICE in fn_delegation when child segment resolves to a trait
When a delegation path like `reuse Trait::<> as bar4` has only one segment resolving to a trait (not a function), the child args processing in `get_delegation_user_specified_args` called `lower_generic_args_of_path` with `self_ty = None`. Since the trait's generics have `has_self = true`, this triggered `assert!(self_ty.is_some())`. Fix by computing and providing `self_ty` when the child segment's `def_id` has `has_self`. In valid delegation code the child segment always resolves to a function, so this only affects error recovery.
1 parent b90dc1e commit 0130dcb

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,25 @@ fn get_delegation_user_specified_args<'tcx>(
610610
}
611611
};
612612

613+
// Provide `self_ty` only when the child segment resolves to a non-function
614+
// item with Self (e.g. a Trait during error recovery). For trait methods
615+
// (AssocFn), `has_self` is true (inherited from parent trait) but Self
616+
// comes from `parent_args`, not `self_ty`.
617+
let self_ty = if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn)
618+
&& tcx.generics_of(def_id).has_self
619+
{
620+
get_delegation_self_ty(tcx, delegation_id)
621+
} else {
622+
None
623+
};
624+
613625
let args = lowerer
614626
.lower_generic_args_of_path(
615627
segment.ident.span,
616628
def_id,
617629
parent_args,
618630
segment,
619-
None,
631+
self_ty,
620632
GenericArgPosition::Value,
621633
)
622634
.0;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(fn_delegation)]
2+
#![allow(incomplete_features)]
3+
4+
trait Trait {
5+
fn bar4();
6+
}
7+
8+
impl Trait for () {
9+
reuse Trait::<> as bar4;
10+
//~^ ERROR expected function, found trait `Trait`
11+
}
12+
13+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0423]: expected function, found trait `Trait`
2+
--> $DIR/ice-reuse-trait-path-with-empty-generics.rs:9:11
3+
|
4+
LL | reuse Trait::<> as bar4;
5+
| ^^^^^^^^^ not a function
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)