Skip to content
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

Avoid wrong code suggesting for attribute macro #107254

Merged

Conversation

chenyukang
Copy link
Member

Fixes #107113
r? @estebank

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 24, 2023
@m-ou-se
Copy link
Member

m-ou-se commented Jan 24, 2023

It'd be nice if we can find a more general solution.

I don't think there should ever be a suggestion using a call_site span of an attribute macro.

@chenyukang
Copy link
Member Author

chenyukang commented Jan 24, 2023 via email

@@ -706,6 +706,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected: Ty<'tcx>,
expr_ty: Ty<'tcx>,
) -> bool {
if in_external_macro(self.tcx.sess, expr.span) {
Copy link
Member Author

Choose a reason for hiding this comment

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

@compiler-errors
My change at here makes this case failed:

rustc --edition 2021 tests/ui/async-await/proper-span-for-type-error.rs
error[E0308]: mismatched types
 --> tests/ui/async-await/proper-span-for-type-error.rs:8:5
  |
8 |     a().await //~ ERROR mismatched types
  |     ^^^^^^^^^ expected enum `Result`, found `()`
  |
  = note:   expected enum `Result<(), i32>`
          found unit type `()`
help: try adding an expression at the end of the block
  |
8 ~     a().await;
9 ~     Ok(()) //~ ERROR mismatched types
  |

This is because in_external_macro is not right, missing DesugaringKind::Await, so I added it and with extra DesugaringKind::Async.

So the new suggestion comes out:

_consume_reference::<i32>(&*async { Box::new(7_i32) }.await);

It seems right, so I keep it.

By the way, I think our current in_external_macro still missing some DesugaringKind, and the in_external_macro is not a proper name right now considering it actually not just checking Macro, a suitable function name should be something like proper_for_suggesting.

And we have a similar function in span, but it definitely need some fix:

pub fn can_be_used_for_suggestions(self) -> bool {
!self.from_expansion()
// FIXME: If this span comes from a `derive` macro but it points at code the user wrote,
// the callsite span and the span will be pointing at different places. It also means that
// we can safely provide suggestions on this span.
|| (matches!(self.ctxt().outer_expn_data().kind, ExpnKind::Macro(MacroKind::Derive, _))
&& self.parent_callsite().map(|p| (p.lo(), p.hi())) != Some((self.lo(), self.hi())))
}

We need some cleanup for these functions here?

@chenyukang
Copy link
Member Author

It'd be nice if we can find a more general solution.

I don't think there should ever be a suggestion using a call_site span of an attribute macro.

Yes, we'd better find a centralized way to do this since it's relevant for pretty much all reported diagnostics.

This is the second time I solve this kind of issue, I tried to add a span.can_be_used_for_suggestions as filter in this function:

pub fn multipart_suggestion_with_style(

and

pub fn span_suggestion_with_style(

There are about 10+ testcases's result changed, I need to tweak the filter function.
I'm not sure whether this is the right direction, or there is any better way?
@compiler-errors @estebank

@chenyukang chenyukang force-pushed the yukang/fix-107113-wrong-sugg-in-macro branch 2 times, most recently from b3c6813 to 5bcb80c Compare March 4, 2023 15:34
@chenyukang
Copy link
Member Author

@compiler-errors do you have more comments for this PR?
I haven't figured out a better general solution yet, I think we may merge this at the moment.

@estebank
Copy link
Contributor

estebank commented May 4, 2023

@chenyukang I believe that a more general solution would be something along the lines of my attempt at #109082. The problem is that that PR causes a perf regression that I haven't yet been able to shake off. We could land this PR as is (and the change to in_external_macro is correct), but feels like adding yet another corner case check when we could nip this entire category of issues in the bud. Would you be interested in taking on the strategy shown in #109082 and see if we can do that without the perf impact?

@chenyukang
Copy link
Member Author

@estebank I will take a look at your PR, I guess the performance regression is introduced by the changes on some functions of Span.

@Dylan-DPC Dylan-DPC added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 18, 2023
@bors
Copy link
Contributor

bors commented Jul 14, 2023

☔ The latest upstream changes (presumably #113591) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Member

@chenyukang any updates on this?

@chenyukang chenyukang force-pushed the yukang/fix-107113-wrong-sugg-in-macro branch from 5bcb80c to 75b9f53 Compare August 2, 2023 07:01
@chenyukang
Copy link
Member Author

@chenyukang any updates on this?

I just resolved the conflicts, as @estebank said we may land this PR but #109082 is an more completed solution.

#109082 has some perf issue right now. I may also take a look at it.
@estebank would you please approve this PR?

@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the yukang/fix-107113-wrong-sugg-in-macro branch from 851af7f to ac25636 Compare August 2, 2023 08:33
@rustbot
Copy link
Collaborator

rustbot commented Aug 2, 2023

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@chenyukang
Copy link
Member Author

Fix the issue of in_external_macro will make lint checker RedundantLocals fail for async and await.

So I added a another commit to fix it:
ac25636

ref here:

// async fn foo(<pattern>: <ty>, <pattern>: <ty>, <pattern>: <ty>) {

@estebank
Copy link
Contributor

estebank commented Aug 2, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Aug 2, 2023

📌 Commit ac25636 has been approved by estebank

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 2, 2023
@bors
Copy link
Contributor

bors commented Aug 2, 2023

⌛ Testing commit ac25636 with merge 07ac64019fac2eba26112cea9e1a27069103b010...

@bors
Copy link
Contributor

bors commented Aug 2, 2023

💔 Test failed - checks-actions

@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 2, 2023
@estebank
Copy link
Contributor

estebank commented Aug 2, 2023

@bors retry

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 2, 2023
@bors
Copy link
Contributor

bors commented Aug 2, 2023

⌛ Testing commit ac25636 with merge 736ef39...

@bors
Copy link
Contributor

bors commented Aug 3, 2023

☀️ Test successful - checks-actions
Approved by: estebank
Pushing 736ef39 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 3, 2023
@bors bors merged commit 736ef39 into rust-lang:master Aug 3, 2023
@rustbot rustbot added this to the 1.73.0 milestone Aug 3, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (736ef39): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
1.2% [1.2%, 1.2%] 1
Improvements ✅
(primary)
-2.9% [-3.4%, -2.3%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.9% [-3.4%, -2.3%] 2

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.3% [4.1%, 4.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 647.939s -> 648.105s (0.03%)

flip1995 pushed a commit to flip1995/rust that referenced this pull request Aug 11, 2023
…ugg-in-macro, r=estebank

Avoid wrong code suggesting for attribute macro

Fixes rust-lang#107113
r? `@estebank`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

suggests to wrap an attribute in Ok()
9 participants