Skip to content

Commit 0365eb5

Browse files
committed
macro expansion issue
1 parent 862156d commit 0365eb5

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1553,17 +1553,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15531553
{
15541554
("(".to_string(), call_span.shrink_to_hi().to(error_span.shrink_to_hi()))
15551555
} 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-
)
1556+
let snippet = source_map.span_to_snippet(full_call_span).unwrap_or_default();
1557+
let method_name = if snippet.contains("::") {
1558+
snippet.rsplit("::").next().unwrap_or(&snippet).to_string()
1559+
} else {
1560+
snippet
1561+
};
1562+
(format!("{}(", method_name), error_span)
15671563
};
15681564
let mut needs_comma = false;
15691565
for (expected_idx, provided_idx) in matched_inputs.iter_enumerated() {

tests/ui/not-enough-arguments.rs

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ fn bar(
2323
println!("{}", f);
2424
}
2525

26+
macro_rules! delegate {
27+
($method:ident) => {
28+
<Self>::$method(8)
29+
//~^ ERROR function takes 2 arguments but 1
30+
};
31+
}
32+
33+
struct Bar;
34+
35+
impl Bar {
36+
fn foo(a: u8, b: u8) {}
37+
fn bar() {
38+
delegate!(foo);
39+
}
40+
}
41+
2642
fn main() {
2743
foo(1, 2, 3);
2844
//~^ ERROR function takes 4 arguments but 3

tests/ui/not-enough-arguments.stderr

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
error[E0061]: this function takes 2 arguments but 1 argument was supplied
2+
--> $DIR/not-enough-arguments.rs:28:7
3+
|
4+
LL | <Self>::$method(8)
5+
| ^^^^^^^^^^^^^^^--- argument #2 of type `u8` is missing
6+
...
7+
LL | delegate!(foo);
8+
| -------------- in this macro invocation
9+
|
10+
note: associated function defined here
11+
--> $DIR/not-enough-arguments.rs:36:6
12+
|
13+
LL | fn foo(a: u8, b: u8) {}
14+
| ^^^ -----
15+
= note: this error originates in the macro `delegate` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
help: provide the argument
17+
|
18+
LL | <Self>::$method(8, /* u8 */)
19+
| ++++++++++
20+
121
error[E0061]: this function takes 4 arguments but 3 arguments were supplied
2-
--> $DIR/not-enough-arguments.rs:27:3
22+
--> $DIR/not-enough-arguments.rs:43:3
323
|
424
LL | foo(1, 2, 3);
525
| ^^^--------- argument #4 of type `isize` is missing
@@ -15,7 +35,7 @@ LL | foo(1, 2, 3, /* isize */);
1535
| +++++++++++++
1636

1737
error[E0061]: this function takes 6 arguments but 3 arguments were supplied
18-
--> $DIR/not-enough-arguments.rs:29:3
38+
--> $DIR/not-enough-arguments.rs:45:3
1939
|
2040
LL | bar(1, 2, 3);
2141
| ^^^--------- three arguments of type `i32`, `i32`, and `i32` are missing
@@ -37,6 +57,6 @@ help: provide the arguments
3757
LL | bar(1, 2, 3, /* i32 */, /* i32 */, /* i32 */);
3858
| +++++++++++++++++++++++++++++++++
3959

40-
error: aborting due to 2 previous errors
60+
error: aborting due to 3 previous errors
4161

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

0 commit comments

Comments
 (0)