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

explicit-outlives-requirements should remove entire where clause if it removes the only bound #105150

Closed
matthiaskrgr opened this issue Dec 1, 2022 · 6 comments · Fixed by #112612
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Dec 1, 2022

Given the following code:

// run-pass
#![allow(dead_code)]
#![allow(unused_variables)]
#![feature(unsize, coerce_unsized)]

#[repr(packed)]
struct UnalignedPtr<'a, T: ?Sized>
    where T: 'a,
{
    data: &'a T,
}

fn main() {

    impl<'a, T, U> std::ops::CoerceUnsized<UnalignedPtr<'a, U>> for UnalignedPtr<'a, T>
        where
        T: std::marker::Unsize<U> + ?Sized,
        U: ?Sized,
    { }

    let arr = [1, 2, 3];
    let arr_unaligned: UnalignedPtr<[i32; 3]> = UnalignedPtr { data: &arr };
    let arr_unaligned: UnalignedPtr<[i32]> = arr_unaligned;
}

The current output is:


warning: outlives requirements can be inferred
 --> src/main.rs:8:11
  |
8 |     where T: 'a,
  |           ^^^^^ help: remove this bound
  |
  = note: requested on the command line with `-W explicit-outlives-requirements`

Rustfix will fail to apply this:

The following errors were reported:
error: expected `{` after struct name, found `,`
 --> src/main.rs:8:11
  |
8 |     where ,
  |           ^ expected `{` after struct name

error: aborting due to previous error

So we need to extend span to include the where: as well?

@matthiaskrgr matthiaskrgr added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 1, 2022
@cjgillot cjgillot added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Dec 2, 2022
@cjgillot
Copy link
Contributor

cjgillot commented Dec 2, 2022

The relevant code is in rustc_lint/src/builtin.rs, with impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements.
The span should be computed using generics.span_for_predicate_removal(i) in the loop, instead of crafting a specific computation. Likewise, collect_outlives_bound_spans should use hir::Generics::span_for_bound_removal method. If using those 2 methods from hir::Generics is not enough to solve the issue, those methods should perhaps be adapted.

@nirmal-j-patel
Copy link

@rustbot claim

@nirmal-j-patel
Copy link

@rustbot release-assignment

Haven't been able to focus on this. Releasing assignment.

@reez12g
Copy link
Contributor

reez12g commented Jan 9, 2023

@rustbot claim

@reez12g
Copy link
Contributor

reez12g commented Jan 17, 2023

Progress report: I'm working on it.

@sginnett
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
5 participants