Skip to content

Commit a25ae74

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

File tree

8 files changed

+41
-42
lines changed

8 files changed

+41
-42
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
149149
}
150150
}
151151

152+
fn is_assoc_fn(&self, sig_id: DefId) -> bool {
153+
match sig_id.as_local() {
154+
Some(local_sig_id) => match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
155+
Some(sig) => sig.is_assoc_item,
156+
None => false,
157+
},
158+
None => self.tcx.def_kind(sig_id) == DefKind::AssocFn,
159+
}
160+
}
161+
152162
fn lower_delegation_decl(
153163
&mut self,
154164
sig_id: DefId,
@@ -324,7 +334,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
324334

325335
let call = if self
326336
.get_resolution_id(delegation.id, span)
327-
.and_then(|def_id| Ok(self.has_self(def_id, span)))
337+
.and_then(|def_id| Ok(self.has_self(def_id, span) && self.is_assoc_fn(def_id)))
328338
.unwrap_or_default()
329339
&& delegation.qself.is_none()
330340
&& !has_generic_args

compiler/rustc_middle/src/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ pub struct DelegationFnSig {
224224
pub has_self: bool,
225225
pub c_variadic: bool,
226226
pub target_feature: bool,
227+
pub is_assoc_item: bool,
227228
}
228229

229230
#[derive(Clone, Copy, Debug)]

compiler/rustc_resolve/src/late.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -5056,13 +5056,20 @@ struct ItemInfoCollector<'a, 'ra, 'tcx> {
50565056
}
50575057

50585058
impl ItemInfoCollector<'_, '_, '_> {
5059-
fn collect_fn_info(&mut self, sig: &FnSig, id: NodeId, attrs: &[Attribute]) {
5059+
fn collect_fn_info(
5060+
&mut self,
5061+
sig: &FnSig,
5062+
id: NodeId,
5063+
attrs: &[Attribute],
5064+
is_assoc_item: bool,
5065+
) {
50605066
let sig = DelegationFnSig {
50615067
header: sig.header,
50625068
param_count: sig.decl.inputs.len(),
50635069
has_self: sig.decl.has_self(),
50645070
c_variadic: sig.decl.c_variadic(),
50655071
target_feature: attrs.iter().any(|attr| attr.has_name(sym::target_feature)),
5072+
is_assoc_item,
50665073
};
50675074
self.r.delegation_fn_sigs.insert(self.r.local_def_id(id), sig);
50685075
}
@@ -5081,7 +5088,7 @@ impl<'ast> Visitor<'ast> for ItemInfoCollector<'_, '_, '_> {
50815088
| ItemKind::Trait(box Trait { generics, .. })
50825089
| ItemKind::TraitAlias(generics, _) => {
50835090
if let ItemKind::Fn(box Fn { sig, .. }) = &item.kind {
5084-
self.collect_fn_info(sig, item.id, &item.attrs);
5091+
self.collect_fn_info(sig, item.id, &item.attrs, false);
50855092
}
50865093

50875094
let def_id = self.r.local_def_id(item.id);
@@ -5114,7 +5121,7 @@ impl<'ast> Visitor<'ast> for ItemInfoCollector<'_, '_, '_> {
51145121

51155122
fn visit_assoc_item(&mut self, item: &'ast AssocItem, ctxt: AssocCtxt) {
51165123
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
5117-
self.collect_fn_info(sig, item.id, &item.attrs);
5124+
self.collect_fn_info(sig, item.id, &item.attrs, true);
51185125
}
51195126
visit::walk_assoc_item(self, item, ctxt);
51205127
}

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)