Skip to content

Warn for outlives lint when gats are enabled for non-gats #91853

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 2 commits into from
Closed
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
21 changes: 9 additions & 12 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ fn check_gat_where_clauses(
return;
}
let generics: &ty::Generics = tcx.generics_of(trait_item.def_id);
// If the current associated type doesn't have any (own) params, it's not a GAT
// FIXME(jackh726): we can also warn in the more general case
if generics.params.len() == 0 {
return;
}
let associated_items: &ty::AssocItems<'_> = tcx.associated_items(encl_trait_def_id);
let mut clauses: Option<FxHashSet<ty::Predicate<'_>>> = None;
// For every function in this trait...
Expand Down Expand Up @@ -465,10 +460,12 @@ fn check_gat_where_clauses(

if !clauses.is_empty() {
let plural = if clauses.len() > 1 { "s" } else { "" };
let mut err = tcx.sess.struct_span_err(
trait_item.span,
&format!("missing required bound{} on `{}`", plural, trait_item.ident),
);
let severity = if generics.params.len() == 0 { "recommended" } else { "required" };
let mut err =
tcx.sess.struct_span_err(
trait_item.span,
&format!("missing {} bound{} on `{}`", severity, plural, trait_item.ident),
);

let suggestion = format!(
"{} {}",
Expand All @@ -481,15 +478,15 @@ fn check_gat_where_clauses(
);
err.span_suggestion(
trait_item.generics.where_clause.tail_span_for_suggestion(),
&format!("add the required where clause{}", plural),
&format!("add the {} where clause{}", severity, plural),
suggestion,
Applicability::MachineApplicable,
);

let bound = if clauses.len() > 1 { "these bounds are" } else { "this bound is" };
err.note(&format!(
"{} currently required to ensure that impls have maximum flexibility",
bound
"{} currently {} to ensure that impls have maximum flexibility",
bound, severity,
));
err.note(
"we are soliciting feedback, see issue #87479 \
Expand Down
14 changes: 12 additions & 2 deletions src/test/ui/generic-associated-types/self-outlives-lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,22 @@ impl Des3 for () {
}
*/

// Similar case to before, except with GAT.
// Similar case to before, except without GAT.
trait NoGat<'a> {
type Bar;
//~^ missing recommended
fn method(&'a self) -> Self::Bar;
}


// Same as above, except explicit
trait NoGatWarning<'a> {
type Output;
//~^ missing recommended

fn method(&'a self) -> <Self as NoGatWarning<'a>>::Output;
}

// Lifetime is not on function; except `Self: 'a`
// FIXME: we require two bounds (`where Self: 'a, Self: 'b`) when we should only require one
trait TraitLifetime<'a> {
Expand Down Expand Up @@ -184,7 +194,7 @@ trait MultipleMethods {

// We would normally require `Self: 'a`, but we can prove that `Self: 'static`
// because of the the bounds on the trait, so the bound is proven
trait Trait: 'static {
trait StaticTrait: 'static {
type Assoc<'a>;
fn make_assoc(_: &u32) -> Self::Assoc<'_>;
}
Expand Down
36 changes: 29 additions & 7 deletions src/test/ui/generic-associated-types/self-outlives-lint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,30 @@ LL | type Out<'x, D>;
= note: this bound is currently required to ensure that impls have maximum flexibility
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

warning: missing recommended bound on `Bar`
--> $DIR/self-outlives-lint.rs:107:5
|
LL | type Bar;
| ^^^^^^^^-
| |
| help: add the recommended where clause: `where Self: 'a`
|
= note: this bound is currently recommended to ensure that impls have maximum flexibility
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

warning: missing recommended bound on `Output`
--> $DIR/self-outlives-lint.rs:115:5
|
LL | type Output;
| ^^^^^^^^^^^-
| |
| help: add the recommended where clause: `where Self: 'a`
|
= note: this bound is currently recommended to ensure that impls have maximum flexibility
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

error: missing required bounds on `Bar`
--> $DIR/self-outlives-lint.rs:114:5
--> $DIR/self-outlives-lint.rs:124:5
|
LL | type Bar<'b>;
| ^^^^^^^^^^^^-
Expand All @@ -87,7 +109,7 @@ LL | type Bar<'b>;
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

error: missing required bound on `Bar`
--> $DIR/self-outlives-lint.rs:122:5
--> $DIR/self-outlives-lint.rs:132:5
|
LL | type Bar<'b>;
| ^^^^^^^^^^^^-
Expand All @@ -98,7 +120,7 @@ LL | type Bar<'b>;
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

error: missing required bound on `Bar`
--> $DIR/self-outlives-lint.rs:129:5
--> $DIR/self-outlives-lint.rs:139:5
|
LL | type Bar<'b>;
| ^^^^^^^^^^^^-
Expand All @@ -109,7 +131,7 @@ LL | type Bar<'b>;
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

error: missing required bound on `Iterator`
--> $DIR/self-outlives-lint.rs:143:5
--> $DIR/self-outlives-lint.rs:153:5
|
LL | type Iterator<'a>: Iterator<Item = Self::Item<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
Expand All @@ -120,7 +142,7 @@ LL | type Iterator<'a>: Iterator<Item = Self::Item<'a>>;
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

error: missing required bound on `Bar`
--> $DIR/self-outlives-lint.rs:151:5
--> $DIR/self-outlives-lint.rs:161:5
|
LL | type Bar<'a, 'b>;
| ^^^^^^^^^^^^^^^^-
Expand All @@ -131,7 +153,7 @@ LL | type Bar<'a, 'b>;
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

error: missing required bound on `Fut`
--> $DIR/self-outlives-lint.rs:167:5
--> $DIR/self-outlives-lint.rs:177:5
|
LL | type Fut<'out>;
| ^^^^^^^^^^^^^^-
Expand All @@ -141,5 +163,5 @@ LL | type Fut<'out>;
= note: this bound is currently required to ensure that impls have maximum flexibility
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

error: aborting due to 13 previous errors
error: aborting due to 13 previous errors; 2 warnings emitted