Skip to content

Commit 11a338d

Browse files
committed
Fix nested zero-args delegation ICE
1 parent 8a70352 commit 11a338d

File tree

6 files changed

+105
-6
lines changed

6 files changed

+105
-6
lines changed

‎compiler/rustc_ast_lowering/src/delegation.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,17 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
432432
args.push(arg);
433433
}
434434

435+
// If we have no params in signature function but user still wrote some code in
436+
// delegation body, then add this code as first arg, eventually an error will be shown,
437+
// also nested delegations may need to access information about this code (#154332),
438+
// so it is better to leave this code as opposed to bodies of extern functions,
439+
// which are completely erased from existence.
440+
if param_count == 0
441+
&& let Some(block) = block
442+
{
443+
args.push(this.lower_target_expr(&block));
444+
}
445+
435446
let final_expr = this.finalize_body_lowering(delegation, args, generics, span);
436447

437448
(this.arena.alloc_from_iter(parameters), final_expr)

‎tests/ui/delegation/explicit-paths-pass.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ impl Trait for F {}
1717

1818
mod to_reuse {
1919
pub fn foo(x: i32) -> i32 { x + 1 }
20-
pub fn zero_args() -> i32 { 15 }
2120
}
2221

23-
reuse to_reuse::zero_args { self }
24-
2522
struct S(F);
2623
impl Trait for S {
2724
reuse Trait::bar { self.0 }
@@ -49,5 +46,4 @@ fn main() {
4946
#[inline]
5047
reuse to_reuse::foo;
5148
assert_eq!(43, foo(42));
52-
assert_eq!(15, zero_args());
5349
}

‎tests/ui/delegation/inner-attr.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
fn a() {}
55

66
reuse a as b { #![rustc_dummy] self } //~ ERROR an inner attribute is not permitted in this context
7+
//~^ ERROR: this function takes 0 arguments but 1 argument was supplied
78

89
fn main() {}

‎tests/ui/delegation/inner-attr.stderr‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@ error: an inner attribute is not permitted in this context
33
|
44
LL | reuse a as b { #![rustc_dummy] self }
55
| ^^^^^^^^^^^^^^^
6-
LL |
6+
...
77
LL | fn main() {}
88
| ------------ the inner attribute doesn't annotate this function
99
|
1010
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
1111

12-
error: aborting due to 1 previous error
12+
error[E0061]: this function takes 0 arguments but 1 argument was supplied
13+
--> $DIR/inner-attr.rs:6:7
14+
|
15+
LL | reuse a as b { #![rustc_dummy] self }
16+
| ^ ---- unexpected argument
17+
|
18+
note: function defined here
19+
--> $DIR/inner-attr.rs:4:4
20+
|
21+
LL | fn a() {}
22+
| ^
23+
help: remove the extra argument
24+
|
25+
LL - reuse a as b { #![rustc_dummy] self }
26+
LL + reuse self }
27+
|
28+
29+
error: aborting due to 2 previous errors
1330

31+
For more information about this error, try `rustc --explain E0061`.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![feature(fn_delegation)]
2+
#![allow(incomplete_features)]
3+
4+
mod test_ice {
5+
fn a() {}
6+
7+
reuse a as b { //~ ERROR: this function takes 0 arguments but 1 argument was supplied
8+
let closure = || {
9+
fn foo<'a, 'b, T: Clone, const N: usize, U: Clone>(_t: &'a T, _u: &'b U) {}
10+
11+
reuse foo::<String, 1, String> as bar;
12+
bar(&"".to_string(), &"".to_string());
13+
};
14+
15+
closure();
16+
}
17+
}
18+
19+
mod test_2 {
20+
mod to_reuse {
21+
pub fn zero_args() -> i32 {
22+
15
23+
}
24+
}
25+
26+
reuse to_reuse::zero_args { self }
27+
//~^ ERROR: this function takes 0 arguments but 1 argument was supplied
28+
}
29+
30+
fn main() {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0061]: this function takes 0 arguments but 1 argument was supplied
2+
--> $DIR/zero-args-delegations-ice-154332.rs:7:11
3+
|
4+
LL | reuse a as b {
5+
| ___________^______-
6+
LL | | let closure = || {
7+
LL | | fn foo<'a, 'b, T: Clone, const N: usize, U: Clone>(_t: &'a T, _u: &'b U) {}
8+
... |
9+
LL | | closure();
10+
LL | | }
11+
| |_____- unexpected argument of type `()`
12+
|
13+
note: function defined here
14+
--> $DIR/zero-args-delegations-ice-154332.rs:5:8
15+
|
16+
LL | fn a() {}
17+
| ^
18+
help: remove the extra argument
19+
|
20+
LL - reuse a as b {
21+
LL + reuse {
22+
|
23+
24+
error[E0061]: this function takes 0 arguments but 1 argument was supplied
25+
--> $DIR/zero-args-delegations-ice-154332.rs:26:21
26+
|
27+
LL | reuse to_reuse::zero_args { self }
28+
| ^^^^^^^^^ ---- unexpected argument
29+
|
30+
note: function defined here
31+
--> $DIR/zero-args-delegations-ice-154332.rs:21:16
32+
|
33+
LL | pub fn zero_args() -> i32 {
34+
| ^^^^^^^^^
35+
help: remove the extra argument
36+
|
37+
LL - reuse to_reuse::zero_args { self }
38+
LL + reuse to_reuse::zero_argself }
39+
|
40+
41+
error: aborting due to 2 previous errors
42+
43+
For more information about this error, try `rustc --explain E0061`.

0 commit comments

Comments
 (0)