Skip to content

Commit c018ead

Browse files
committed
Warn when gats are enabled and we trigger the outlives lint
1 parent a2d25b4 commit c018ead

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

compiler/rustc_typeck/src/check/wfcheck.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ fn check_gat_where_clauses(
287287
let generics: &ty::Generics = tcx.generics_of(trait_item.def_id);
288288
// If the current associated type doesn't have any (own) params, it's not a GAT
289289
// FIXME(jackh726): we can also warn in the more general case
290-
if generics.params.len() == 0 {
290+
if generics.params.len() == 0 && !tcx.features().generic_associated_types {
291291
return;
292292
}
293293
let associated_items: &ty::AssocItems<'_> = tcx.associated_items(encl_trait_def_id);
@@ -465,10 +465,18 @@ fn check_gat_where_clauses(
465465

466466
if !clauses.is_empty() {
467467
let plural = if clauses.len() > 1 { "s" } else { "" };
468-
let mut err = tcx.sess.struct_span_err(
469-
trait_item.span,
470-
&format!("missing required bound{} on `{}`", plural, trait_item.ident),
471-
);
468+
let severity = if generics.params.len() == 0 { "recommended" } else { "required" };
469+
let mut err = if generics.params.len() == 0 {
470+
tcx.sess.struct_span_warn(
471+
trait_item.span,
472+
&format!("missing {} bound{} on `{}`", severity, plural, trait_item.ident),
473+
)
474+
} else {
475+
tcx.sess.struct_span_err(
476+
trait_item.span,
477+
&format!("missing {} bound{} on `{}`", severity, plural, trait_item.ident),
478+
)
479+
};
472480

473481
let suggestion = format!(
474482
"{} {}",
@@ -481,15 +489,15 @@ fn check_gat_where_clauses(
481489
);
482490
err.span_suggestion(
483491
trait_item.generics.where_clause.tail_span_for_suggestion(),
484-
&format!("add the required where clause{}", plural),
492+
&format!("add the {} where clause{}", severity, plural),
485493
suggestion,
486494
Applicability::MachineApplicable,
487495
);
488496

489497
let bound = if clauses.len() > 1 { "these bounds are" } else { "this bound is" };
490498
err.note(&format!(
491-
"{} currently required to ensure that impls have maximum flexibility",
492-
bound
499+
"{} currently {} to ensure that impls have maximum flexibility",
500+
bound, severity,
493501
));
494502
err.note(
495503
"we are soliciting feedback, see issue #87479 \

src/test/ui/generic-associated-types/self-outlives-lint.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,22 @@ impl Des3 for () {
102102
}
103103
*/
104104

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

112+
113+
// Same as above, except explicit
114+
trait NoGatWarning<'a> {
115+
type Output;
116+
//~^ missing recommended
117+
118+
fn method(&'a self) -> <Self as NoGatWarning<'a>>::Output;
119+
}
120+
111121
// Lifetime is not on function; except `Self: 'a`
112122
// FIXME: we require two bounds (`where Self: 'a, Self: 'b`) when we should only require one
113123
trait TraitLifetime<'a> {
@@ -184,7 +194,7 @@ trait MultipleMethods {
184194

185195
// We would normally require `Self: 'a`, but we can prove that `Self: 'static`
186196
// because of the the bounds on the trait, so the bound is proven
187-
trait Trait: 'static {
197+
trait StaticTrait: 'static {
188198
type Assoc<'a>;
189199
fn make_assoc(_: &u32) -> Self::Assoc<'_>;
190200
}

src/test/ui/generic-associated-types/self-outlives-lint.stderr

+29-7
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,30 @@ LL | type Out<'x, D>;
7575
= note: this bound is currently required to ensure that impls have maximum flexibility
7676
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information
7777

78+
warning: missing recommended bound on `Bar`
79+
--> $DIR/self-outlives-lint.rs:107:5
80+
|
81+
LL | type Bar;
82+
| ^^^^^^^^-
83+
| |
84+
| help: add the recommended where clause: `where Self: 'a`
85+
|
86+
= note: this bound is currently recommended to ensure that impls have maximum flexibility
87+
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information
88+
89+
warning: missing recommended bound on `Output`
90+
--> $DIR/self-outlives-lint.rs:115:5
91+
|
92+
LL | type Output;
93+
| ^^^^^^^^^^^-
94+
| |
95+
| help: add the recommended where clause: `where Self: 'a`
96+
|
97+
= note: this bound is currently recommended to ensure that impls have maximum flexibility
98+
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information
99+
78100
error: missing required bounds on `Bar`
79-
--> $DIR/self-outlives-lint.rs:114:5
101+
--> $DIR/self-outlives-lint.rs:124:5
80102
|
81103
LL | type Bar<'b>;
82104
| ^^^^^^^^^^^^-
@@ -87,7 +109,7 @@ LL | type Bar<'b>;
87109
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information
88110

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

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

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

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

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

144-
error: aborting due to 13 previous errors
166+
error: aborting due to 13 previous errors; 2 warnings emitted
145167

0 commit comments

Comments
 (0)