Skip to content

Add duplicate trait bound lint #95052

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

Closed
wants to merge 4 commits into from

Conversation

pierwill
Copy link
Member

@pierwill pierwill commented Mar 17, 2022

Do not allow duplicate trait bounds.

Closes #90108.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 17, 2022
@rust-highfive
Copy link
Contributor

r? @lcnr

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 17, 2022
@pierwill
Copy link
Member Author

pierwill commented Mar 17, 2022

@rustbot author

@rustbot rustbot 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 Mar 17, 2022

for bound in bounds_vec {
// LOGIC GOES HERE
if bounds_vec.contains(&bound) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Two things here:

  1. Maybe we can use a pop or remove to check for duplicates in a loop like this?
  2. What's the best way to do the equality check given a PolyTraitRef? Not a lot of Eq implementations on the types here...

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe we can dig all the way in to the corresponding DefId of the trait and use that, so we skip the ones that aren't trait bounds (like : 'a and <X as Y>::Z: A) and ignore the others. It'd be nice to also find redundant bounds for those, but we can start with only trait bounds.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@pierwill pierwill force-pushed the duplicate-trait-bound branch from af48e19 to 0beecd0 Compare March 22, 2022 16:24
@rust-log-analyzer

This comment has been minimized.

@pierwill pierwill force-pushed the duplicate-trait-bound branch from 0beecd0 to 0dea762 Compare May 23, 2022 17:04
@rust-log-analyzer

This comment has been minimized.

@pierwill pierwill force-pushed the duplicate-trait-bound branch from 0dea762 to ab03610 Compare July 13, 2022 17:04
@rust-log-analyzer

This comment has been minimized.

@estebank
Copy link
Contributor

Can you add a test exercising this?

@rust-log-analyzer

This comment has been minimized.

@estebank
Copy link
Contributor

When adding a test file you need to 1) annotate the line with an error with //~ ERROR message to match and 2) run x.py test src/test/ui/path/to/file --blessto generate the corresponding.stderr` file that checks against visual changes.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@pierwill pierwill changed the title [WIP] Add duplicate trait bound lint Add duplicate trait bound lint Jul 20, 2022
@rust-log-analyzer

This comment has been minimized.

@pierwill pierwill force-pushed the duplicate-trait-bound branch from b050abd to bc0bb88 Compare July 20, 2022 16:27
@pierwill
Copy link
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 20, 2022
Co-authored-by: Esteban Kuber <[email protected]>
@pierwill pierwill force-pushed the duplicate-trait-bound branch from bc0bb88 to 416195e Compare July 20, 2022 16:34
@pierwill pierwill requested a review from estebank July 20, 2022 16:43
@rust-log-analyzer

This comment has been minimized.

@pierwill
Copy link
Member Author

@rustbot author

@rustbot rustbot 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 Jul 20, 2022
@pierwill
Copy link
Member Author

Looks like we need to inspect the generics as well...

@rust-log-analyzer

This comment has been minimized.

let Some(did) = polytraitref.trait_ref.trait_def_id() else { continue; };
// If inserting the trait bound into the set returns `false`,
// there is a duplicate.
if !set.insert(did) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using a set, you should use a FxHashMap so that you can keep the Span around and use that Span in the lint to point at the previous trait bound, to minimize confusion.

Copy link
Member Author

@pierwill pierwill Jul 21, 2022

Choose a reason for hiding this comment

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

Thanks for this!

I'm not sure which methods to use here. Using this rough implementation

let mut map = FxHashMap::default();
        for bound in bounds.into_iter() {
            match bound {
                GenericBound::Trait(polytraitref, _) => {
                    let Some(did) = polytraitref.trait_ref.trait_def_id() else { continue; };
                    let span = polytraitref.span;
                    if map.insert(&did, &span).is_none() {
                        let span_of_dup_bound = span;
                        let span_of_prev_bound = map.get(&did).unwrap();
                        cx.struct_span_lint(DUP_TRAIT_BOUNDS, span_of_dup_bound, |lint| {
                            let msg = format!("duplicate trait bound");
                            lint.build(&msg)
                                .span_note(span_of_prev_bound, "trait bound previously used here")
                                .span_help(span_of_dup_bound, "remove this duplicate trait bound")
                                .emit();
                        });
                    };
                }
                _ => continue,
            }
        }
    }

I'm getting

   --> compiler/rustc_lint/src/traits.rs:169:44
    |
169 | ...                   .span_help(span_of_prev_bound, "trait bound previously used here")
    |                        --------- ^^^^^^^^^^^^^^^^^^ the trait `From<&&rustc_span::Span>` is not implemented for `MultiSpan`
    |                        |
    |                        required by a bound introduced by this call
    |
    = help: the following other types implement trait `From<T>`:
              <MultiSpan as From<Vec<rustc_span::Span>>>
              <MultiSpan as From<rustc_span::Span>>
    = note: required because of the requirements on the impl of `Into<MultiSpan>` for `&&rustc_span::Span`
note: required by a bound in `DiagnosticBuilder::<'a, G>::span_help`
   --> /Users/will/repos/rust/compiler/rustc_errors/src/diagnostic_builder.rs:457:18
    |
457 |         sp: impl Into<MultiSpan>,
    |                  ^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::span_help`

I don't yet understand how to work with MultiSpan...

Also, for right now I'm thinking of this span message as a nice-to-have. I'm more worried about this test case and handling the generic parameters:

trait StoreIndex: Indexer<u8> + Indexer<u16> {}

Copy link
Member

Choose a reason for hiding this comment

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

That is because you have a &&Span and you need to dereference it to work.

Copy link
Contributor

Choose a reason for hiding this comment

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

That is because you have a &&Span and you need to dereference it to work.

Fun case, because then we should be suggesting the derefecence in the diagnostic, and we're not :)

Copy link
Contributor

Choose a reason for hiding this comment

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

You can use try_insert:

let mut map = FxHashMap::default();
for bound in bounds.into_iter() {
    match bound {
        GenericBound::Trait(polytraitref, _) => {
            let Some(did) = polytraitref.trait_ref.trait_def_id() else { continue; };
            let span = polytraitref.span;
            if let Err(occupied) = map.try_insert(did, span) {
                cx.struct_span_lint(DUP_TRAIT_BOUNDS, span, |lint| {
                    let msg = format!("duplicate trait bound");
                    lint.build(&msg)
                        .span_label(*occupied.value, "trait bound previously used here")
                        .span_label(span, "duplicate trait bound")
                        .emit();
                });
            };
        }
        _ => continue,
    }
}

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

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

@Dylan-DPC
Copy link
Member

@pierwill any updates on the test failure?

@bors
Copy link
Collaborator

bors commented Dec 2, 2022

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

@Dylan-DPC
Copy link
Member

Closing this as inactive.

@Dylan-DPC Dylan-DPC closed this Jan 14, 2023
@Dylan-DPC Dylan-DPC removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Detect duplicate trait bounds
10 participants