Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,17 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
args.push(arg);
}

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

let final_expr = this.finalize_body_lowering(delegation, args, generics, span);

(this.arena.alloc_from_iter(parameters), final_expr)
Expand Down
4 changes: 0 additions & 4 deletions tests/ui/delegation/explicit-paths-pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ impl Trait for F {}

mod to_reuse {
pub fn foo(x: i32) -> i32 { x + 1 }
pub fn zero_args() -> i32 { 15 }
}

reuse to_reuse::zero_args { self }

struct S(F);
impl Trait for S {
reuse Trait::bar { self.0 }
Expand Down Expand Up @@ -49,5 +46,4 @@ fn main() {
#[inline]
reuse to_reuse::foo;
assert_eq!(43, foo(42));
assert_eq!(15, zero_args());
}
1 change: 1 addition & 0 deletions tests/ui/delegation/inner-attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
fn a() {}

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

fn main() {}
22 changes: 20 additions & 2 deletions tests/ui/delegation/inner-attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@ error: an inner attribute is not permitted in this context
|
LL | reuse a as b { #![rustc_dummy] self }
| ^^^^^^^^^^^^^^^
LL |
...
LL | fn main() {}
| ------------ the inner attribute doesn't annotate this function
|
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files

error: aborting due to 1 previous error
error[E0061]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/inner-attr.rs:6:7
|
LL | reuse a as b { #![rustc_dummy] self }
| ^ ---- unexpected argument
|
note: function defined here
--> $DIR/inner-attr.rs:4:4
|
LL | fn a() {}
| ^
help: remove the extra argument
|
LL - reuse a as b { #![rustc_dummy] self }
LL + reuse self }
|

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0061`.
30 changes: 30 additions & 0 deletions tests/ui/delegation/zero-args-delegations-ice-154332.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![feature(fn_delegation)]
#![allow(incomplete_features)]

mod test_ice {
fn a() {}

reuse a as b { //~ ERROR: this function takes 0 arguments but 1 argument was supplied
let closure = || {
fn foo<'a, 'b, T: Clone, const N: usize, U: Clone>(_t: &'a T, _u: &'b U) {}

reuse foo::<String, 1, String> as bar;
bar(&"".to_string(), &"".to_string());
};

closure();
}
}

mod test_2 {
mod to_reuse {
pub fn zero_args() -> i32 {
15
}
}

reuse to_reuse::zero_args { self }
//~^ ERROR: this function takes 0 arguments but 1 argument was supplied
}

fn main() {}
43 changes: 43 additions & 0 deletions tests/ui/delegation/zero-args-delegations-ice-154332.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0061]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/zero-args-delegations-ice-154332.rs:7:11
|
LL | reuse a as b {
| ___________^______-
LL | | let closure = || {
LL | | fn foo<'a, 'b, T: Clone, const N: usize, U: Clone>(_t: &'a T, _u: &'b U) {}
... |
LL | | closure();
LL | | }
| |_____- unexpected argument of type `()`
|
note: function defined here
--> $DIR/zero-args-delegations-ice-154332.rs:5:8
|
LL | fn a() {}
| ^
help: remove the extra argument
|
LL - reuse a as b {
LL + reuse {
|

error[E0061]: this function takes 0 arguments but 1 argument was supplied
--> $DIR/zero-args-delegations-ice-154332.rs:26:21
|
LL | reuse to_reuse::zero_args { self }
| ^^^^^^^^^ ---- unexpected argument
|
note: function defined here
--> $DIR/zero-args-delegations-ice-154332.rs:21:16
|
LL | pub fn zero_args() -> i32 {
| ^^^^^^^^^
help: remove the extra argument
|
LL - reuse to_reuse::zero_args { self }
LL + reuse to_reuse::zero_argself }
|

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0061`.
Loading