Skip to content

Dont suggest ! for path in function call if it has generic args #118342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2023
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
22 changes: 16 additions & 6 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
err,
span,
source,
path,
res,
&path_str,
&base_error.fallback_label,
Expand Down Expand Up @@ -1328,6 +1329,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
err: &mut Diagnostic,
span: Span,
source: PathSource<'_>,
path: &[Segment],
res: Res,
path_str: &str,
fallback_label: &str,
Expand Down Expand Up @@ -1523,12 +1525,20 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
| PathSource::Struct,
) => {
err.span_label(span, fallback_label.to_string());
err.span_suggestion_verbose(
span.shrink_to_hi(),
"use `!` to invoke the macro",
"!",
Applicability::MaybeIncorrect,
);

// Don't suggest `!` for a macro invocation if there are generic args
if path
.last()
.is_some_and(|segment| !segment.has_generic_args && !segment.has_lifetime_args)
{
err.span_suggestion_verbose(
span.shrink_to_hi(),
"use `!` to invoke the macro",
"!",
Applicability::MaybeIncorrect,
);
}
Copy link
Contributor

@estebank estebank Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a else { err.span_note(macro_def.span, "there's a macro of the same name"); }? Otherwise the main message already mentions the macro existing, so a span-less note isn't worth it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess we could

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm only getting DUMMY_SP for the macro def spans.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sigh, ok.


if path_str == "try" && span.is_rust_2015() {
err.note("if you want the `try` keyword, you need Rust 2018 or later");
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/resolve/resolve-dont-hint-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let zero = assert_eq::<()>();
//~^ ERROR expected function, found macro `assert_eq`
}
9 changes: 9 additions & 0 deletions tests/ui/resolve/resolve-dont-hint-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0423]: expected function, found macro `assert_eq`
--> $DIR/resolve-dont-hint-macro.rs:2:16
|
LL | let zero = assert_eq::<()>();
| ^^^^^^^^^^^^^^^ not a function

error: aborting due to 1 previous error

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