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

unsafe_op_in_unsafe_fn suggestion overlaps when used in a macro #123304

Closed
ehuss opened this issue Mar 31, 2024 · 11 comments
Closed

unsafe_op_in_unsafe_fn suggestion overlaps when used in a macro #123304

ehuss opened this issue Mar 31, 2024 · 11 comments
Labels
A-edition-2024 Area: The 2024 edition C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Mar 31, 2024

The following example triggers the unsafe_op_in_unsafe_fn lint twice in the same position:

#![warn(unsafe_op_in_unsafe_fn)]

macro_rules! foo {
    ($x:ident) => {
        pub unsafe fn $x() {
            let _ = String::new().as_mut_vec();
        }
    };
}

foo!(a);
foo!(b);

This causes cargo fix to apply both suggestions which ends up with:

// ...snip
pub unsafe fn $x() { unsafe { unsafe {
    let _ = String::new().as_mut_vec();
}}}
// ...snip

which triggers the unused_unsafe lint.

Would it be possible to avoid duplicate suggestions when used in a macro?
cc @asquared31415

I suspect this may be encountered frequently with the 2024 edition migration (crater run is pending in #122960). Although it isn't too much of a problem to repair, a frequently used macro could have many unsafe blocks, which could be confusing.

rustc 1.79.0-nightly (8df7e723e 2024-03-30)
binary: rustc
commit-hash: 8df7e723ea729a7f917501cc2d91d640b7021373
commit-date: 2024-03-30
host: aarch64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.2
@ehuss ehuss added D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-edition-2024 Area: The 2024 edition labels Mar 31, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 31, 2024
@ehuss ehuss added the C-bug Category: This is a bug. label Mar 31, 2024
@ehuss
Copy link
Contributor Author

ehuss commented Mar 31, 2024

Another option I am considering is to have rustfix ignore a top-level diagnostic if all of its suggestions are identical to another top-level diagnostic. That might take a bit of reworking of how rustfix collects suggestions, though. I'm also trying to think of what problems that might introduce, though I think it should be safe.

@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 2, 2024
@weihanglo
Copy link
Member

Just looked into the given reproduction. It seems that the duplications happens between different diagnostic messages. I am not sure if such a fix should be in rustfix. Rustfix only deal with one diagnostic at a time. Perhaps we need to get it done at a higher level like here in Cargo? Alternatively, we can have rustfix::CodeFix aware of duplications and skip applying those. Do you have any preference?

@weihanglo
Copy link
Member

Is there any situation that we'll get identical machine applicable suggestions, and they are all necessary not duplicated?

@ehuss
Copy link
Contributor Author

ehuss commented Apr 10, 2024

Is there any situation that we'll get identical machine applicable suggestions, and they are all necessary not duplicated?

I brought this up in https://rust-lang.zulipchat.com/#narrow/stream/147480-t-compiler.2Fwg-diagnostics/topic/overlapping.20suggestions, and I and others couldn't think of any concerns with that.

@ehuss
Copy link
Contributor Author

ehuss commented Apr 10, 2024

Rustfix only deal with one diagnostic at a time. Perhaps we need to get it done at a higher level like here in Cargo? Alternatively, we can have rustfix::CodeFix aware of duplications and skip applying those. Do you have any preference?

It looks like in rust-lang/cargo#13728 that you maybe figured this out? The rustfix library handles collecting multiple diagnostics all at once, and that is where it should be able to filter things.

@weihanglo
Copy link
Member

The rustfix library handles collecting multiple diagnostics all at once

I didn't fully get it. Did you mean get_suggestions_from_json? Cargo doesn't use that API and instead calls collect_suggestions, which collects from one top-level diagnostics at a time.

rust-lang/cargo#13728 addresses it on Cargo side, not inside rustfix crate. I feel it more nature for rustfix being as dumb as possible, and let Cargo handle business logic. Let me know if you have other thought :)

@ehuss
Copy link
Contributor Author

ehuss commented Apr 10, 2024

Oh, indeed, I was thinking of get_suggestions_from_json. I did not realize it was not used in cargo, though.

The issue with doing it on the cargo side is that we also need to update compiletest to do the same thing (and ui_test and anyone else using it). I think it would be nice to avoid duplicating the logic between cargo and all other users.

Is there some reason cargo doesn't use get_suggestions_from_json? It looks like cargo's collector is a little more resilient, perhaps get_suggestions_from_json could be updated to have the same behavior as the way cargo collects the messages?

@weihanglo
Copy link
Member

rust-lang/cargo#13728 has been merged. rustfix@0.8.3 should contain a similar fix when it gets released.

@tmandry

This comment was marked as outdated.

@tmandry
Copy link
Member

tmandry commented Apr 17, 2024

Sorry, I just caught up on the discussion here. It looks like this can be closed then, now that rust-lang/cargo#13728 is merged?

@tmandry
Copy link
Member

tmandry commented May 1, 2024

I've verified that the reported issue no longer occurs, and the migration behaves correctly with cargo fix.

@tmandry tmandry closed this as completed May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2024 Area: The 2024 edition C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants