Skip to content

Commit 7fc953a

Browse files
committed
macro expansion issue
1 parent 862156d commit 7fc953a

File tree

4 files changed

+71
-55
lines changed

4 files changed

+71
-55
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,23 +1548,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15481548
};
15491549
if let Some(suggestion_text) = suggestion_text {
15501550
let source_map = self.sess().source_map();
1551-
let (mut suggestion, suggestion_span) = if let Some(call_span) =
1552-
full_call_span.find_ancestor_inside_same_ctxt(error_span)
1553-
{
1554-
("(".to_string(), call_span.shrink_to_hi().to(error_span.shrink_to_hi()))
1551+
let suggestion_span = if let Some(args_span) = error_span.trim_start(full_call_span) {
1552+
// Span of the braces, e.g. `(a, b, c)`.
1553+
args_span
15551554
} else {
1556-
(
1557-
format!(
1558-
"{}(",
1559-
source_map.span_to_snippet(full_call_span).unwrap_or_else(|_| {
1560-
fn_def_id.map_or("".to_string(), |fn_def_id| {
1561-
tcx.item_name(fn_def_id).to_string()
1562-
})
1563-
})
1564-
),
1565-
error_span,
1566-
)
1555+
// The arg span of a function call that wasn't even given braces
1556+
// like what might happen with delegation reuse.
1557+
// e.g. `reuse HasSelf::method;` should suggest `reuse HasSelf::method($args);`.
1558+
full_call_span.shrink_to_hi()
15671559
};
1560+
let mut suggestion = "(".to_owned();
15681561
let mut needs_comma = false;
15691562
for (expected_idx, provided_idx) in matched_inputs.iter_enumerated() {
15701563
if needs_comma {

tests/ui/auxiliary/delegate_macro.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[macro_export]
2+
macro_rules! delegate {
3+
($method:ident) => {
4+
<Self>::$method(8)
5+
};
6+
}

tests/ui/not-enough-arguments.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1+
//@ aux-build: delegate_macro.rs
2+
extern crate delegate_macro;
3+
use delegate_macro::delegate;
4+
15
// Check that the only error msg we report is the
26
// mismatch between the # of params, and not other
37
// unrelated errors.
4-
5-
fn foo(a: isize, b: isize, c: isize, d:isize) {
6-
panic!();
8+
fn foo(a: isize, b: isize, c: isize, d: isize) {
9+
panic!();
710
}
811

912
// Check that all arguments are shown in the error message, even if they're across multiple lines.
10-
fn bar(
11-
a: i32,
12-
b: i32,
13-
c: i32,
14-
d: i32,
15-
e: i32,
16-
f: i32,
17-
) {
13+
fn bar(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) {
1814
println!("{}", a);
1915
println!("{}", b);
2016
println!("{}", c);
@@ -23,9 +19,19 @@ fn bar(
2319
println!("{}", f);
2420
}
2521

22+
struct Bar;
23+
24+
impl Bar {
25+
fn foo(a: u8, b: u8) {}
26+
fn bar() {
27+
delegate!(foo);
28+
//~^ ERROR function takes 2 arguments but 1
29+
}
30+
}
31+
2632
fn main() {
27-
foo(1, 2, 3);
28-
//~^ ERROR function takes 4 arguments but 3
29-
bar(1, 2, 3);
30-
//~^ ERROR function takes 6 arguments but 3
33+
foo(1, 2, 3);
34+
//~^ ERROR function takes 4 arguments but 3
35+
bar(1, 2, 3);
36+
//~^ ERROR function takes 6 arguments but 3
3137
}

tests/ui/not-enough-arguments.stderr

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,53 @@
1+
error[E0061]: this function takes 2 arguments but 1 argument was supplied
2+
--> $DIR/not-enough-arguments.rs:27:9
3+
|
4+
LL | delegate!(foo);
5+
| ^^^^^^^^^^^^^^ argument #2 of type `u8` is missing
6+
|
7+
note: associated function defined here
8+
--> $DIR/not-enough-arguments.rs:25:8
9+
|
10+
LL | fn foo(a: u8, b: u8) {}
11+
| ^^^ -----
12+
= note: this error originates in the macro `delegate` (in Nightly builds, run with -Z macro-backtrace for more info)
13+
help: provide the argument
14+
--> $DIR/auxiliary/delegate_macro.rs:4:26
15+
|
16+
LL | <Self>::$method(8, /* u8 */)
17+
| ++++++++++
18+
119
error[E0061]: this function takes 4 arguments but 3 arguments were supplied
2-
--> $DIR/not-enough-arguments.rs:27:3
20+
--> $DIR/not-enough-arguments.rs:33:5
321
|
4-
LL | foo(1, 2, 3);
5-
| ^^^--------- argument #4 of type `isize` is missing
22+
LL | foo(1, 2, 3);
23+
| ^^^--------- argument #4 of type `isize` is missing
624
|
725
note: function defined here
8-
--> $DIR/not-enough-arguments.rs:5:4
26+
--> $DIR/not-enough-arguments.rs:8:4
927
|
10-
LL | fn foo(a: isize, b: isize, c: isize, d:isize) {
11-
| ^^^ -------
28+
LL | fn foo(a: isize, b: isize, c: isize, d: isize) {
29+
| ^^^ --------
1230
help: provide the argument
1331
|
14-
LL | foo(1, 2, 3, /* isize */);
15-
| +++++++++++++
32+
LL | foo(1, 2, 3, /* isize */);
33+
| +++++++++++++
1634

1735
error[E0061]: this function takes 6 arguments but 3 arguments were supplied
18-
--> $DIR/not-enough-arguments.rs:29:3
36+
--> $DIR/not-enough-arguments.rs:35:5
1937
|
20-
LL | bar(1, 2, 3);
21-
| ^^^--------- three arguments of type `i32`, `i32`, and `i32` are missing
38+
LL | bar(1, 2, 3);
39+
| ^^^--------- three arguments of type `i32`, `i32`, and `i32` are missing
2240
|
2341
note: function defined here
24-
--> $DIR/not-enough-arguments.rs:10:4
25-
|
26-
LL | fn bar(
27-
| ^^^
28-
...
29-
LL | d: i32,
30-
| ------
31-
LL | e: i32,
32-
| ------
33-
LL | f: i32,
34-
| ------
42+
--> $DIR/not-enough-arguments.rs:13:4
43+
|
44+
LL | fn bar(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32) {
45+
| ^^^ ------ ------ ------
3546
help: provide the arguments
3647
|
37-
LL | bar(1, 2, 3, /* i32 */, /* i32 */, /* i32 */);
38-
| +++++++++++++++++++++++++++++++++
48+
LL | bar(1, 2, 3, /* i32 */, /* i32 */, /* i32 */);
49+
| +++++++++++++++++++++++++++++++++
3950

40-
error: aborting due to 2 previous errors
51+
error: aborting due to 3 previous errors
4152

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

0 commit comments

Comments
 (0)