Skip to content

Commit 61122d1

Browse files
committed
Delegation: fix ICE with invalid MethodCall generation
1 parent 30f168e commit 61122d1

File tree

7 files changed

+37
-54
lines changed

7 files changed

+37
-54
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,27 @@ pub(crate) struct DelegationResults<'hir> {
6060
}
6161

6262
impl<'hir> LoweringContext<'_, 'hir> {
63-
pub(crate) fn delegation_has_self(&self, item_id: NodeId, path_id: NodeId, span: Span) -> bool {
63+
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
64+
pub(crate) fn delegatee_is_method(&self, item_id: NodeId, path_id: NodeId, span: Span) -> bool {
6465
let sig_id = self.get_delegation_sig_id(item_id, path_id, span);
6566
let Ok(sig_id) = sig_id else {
6667
return false;
6768
};
68-
self.has_self(sig_id, span)
69+
self.is_method(sig_id, span)
6970
}
7071

71-
fn has_self(&self, def_id: DefId, span: Span) -> bool {
72-
if let Some(local_sig_id) = def_id.as_local() {
73-
// The value may be missing due to recursive delegation.
74-
// Error will be emitted later during HIR ty lowering.
75-
self.resolver.delegation_fn_sigs.get(&local_sig_id).is_some_and(|sig| sig.has_self)
76-
} else {
77-
match self.tcx.def_kind(def_id) {
78-
DefKind::Fn => false,
79-
DefKind::AssocFn => self.tcx.associated_item(def_id).fn_has_self_parameter,
80-
_ => span_bug!(span, "unexpected DefKind for delegation item"),
81-
}
72+
fn is_method(&self, def_id: DefId, span: Span) -> bool {
73+
match self.tcx.def_kind(def_id) {
74+
DefKind::Fn => false,
75+
DefKind::AssocFn => match def_id.as_local() {
76+
Some(local_def_id) => self
77+
.resolver
78+
.delegation_fn_sigs
79+
.get(&local_def_id)
80+
.is_some_and(|sig| sig.has_self),
81+
None => self.tcx.associated_item(def_id).fn_has_self_parameter,
82+
},
83+
_ => span_bug!(span, "unexpected DefKind for delegation item"),
8284
}
8385
}
8486

@@ -324,7 +326,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
324326

325327
let call = if self
326328
.get_resolution_id(delegation.id, span)
327-
.and_then(|def_id| Ok(self.has_self(def_id, span)))
329+
.and_then(|def_id| Ok(self.is_method(def_id, span)))
328330
.unwrap_or_default()
329331
&& delegation.qself.is_none()
330332
&& !has_generic_args

compiler/rustc_ast_lowering/src/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
871871
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
872872
}
873873
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
874-
has_self: self.delegation_has_self(i.id, delegation.id, i.span),
874+
has_self: self.delegatee_is_method(i.id, delegation.id, i.span),
875875
},
876876
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
877877
panic!("macros should have been expanded by now")
@@ -1000,7 +1000,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10001000
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
10011001
}
10021002
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
1003-
has_self: self.delegation_has_self(i.id, delegation.id, i.span),
1003+
has_self: self.delegatee_is_method(i.id, delegation.id, i.span),
10041004
},
10051005
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
10061006
panic!("macros should have been expanded by now")

tests/crashes/127916.rs

-16
This file was deleted.

tests/crashes/128119.rs

-15
This file was deleted.

tests/crashes/128190.rs

-7
This file was deleted.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(fn_delegation)]
2+
#![allow(incomplete_features)]
3+
4+
fn a(&self) {}
5+
//~^ ERROR `self` parameter is only allowed in associated functions
6+
7+
reuse a as b;
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `self` parameter is only allowed in associated functions
2+
--> $DIR/ice-isssue-128190.rs:4:6
3+
|
4+
LL | fn a(&self) {}
5+
| ^^^^^ not semantically valid as function parameter
6+
|
7+
= note: associated functions are those in `impl` or `trait` definitions
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)