Skip to content

Commit b809207

Browse files
committed
Lint against misplaced where-clauses on assoc tys in traits
1 parent 71f71a5 commit b809207

10 files changed

+81
-21
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -1300,33 +1300,34 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13001300
});
13011301
}
13021302
}
1303-
AssocItemKind::Type(box TyAlias {
1304-
generics,
1305-
where_clauses,
1306-
where_predicates_split,
1307-
bounds,
1308-
ty,
1309-
..
1310-
}) => {
1303+
AssocItemKind::Type(box TyAlias { bounds, ty, .. }) => {
13111304
if ty.is_none() {
13121305
self.session.emit_err(errors::AssocTypeWithoutBody {
13131306
span: item.span,
13141307
replace_span: self.ending_semi_or_hi(item.span),
13151308
});
13161309
}
13171310
self.check_type_no_bounds(bounds, "`impl`s");
1318-
if ty.is_some() {
1319-
self.check_gat_where(
1320-
item.id,
1321-
generics.where_clause.predicates.split_at(*where_predicates_split).0,
1322-
*where_clauses,
1323-
);
1324-
}
13251311
}
13261312
_ => {}
13271313
}
13281314
}
13291315

1316+
if let AssocItemKind::Type(box TyAlias {
1317+
generics,
1318+
where_clauses,
1319+
where_predicates_split,
1320+
ty: Some(_),
1321+
..
1322+
}) = &item.kind
1323+
{
1324+
self.check_gat_where(
1325+
item.id,
1326+
generics.where_clause.predicates.split_at(*where_predicates_split).0,
1327+
*where_clauses,
1328+
);
1329+
}
1330+
13301331
if ctxt == AssocCtxt::Trait || self.in_trait_impl {
13311332
self.visibility_not_permitted(&item.vis, errors::VisibilityNotPermittedNote::TraitImpl);
13321333
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {

compiler/rustc_lint_defs/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4084,7 +4084,7 @@ declare_lint! {
40844084
///
40854085
/// ### Explanation
40864086
///
4087-
/// The preferred location for where clauses on associated types in impls
4087+
/// The preferred location for where clauses on associated types
40884088
/// is after the type. However, for most of generic associated types development,
40894089
/// it was only accepted before the equals. To provide a transition period and
40904090
/// further evaluate this change, both are currently accepted. At some point in

tests/ui/parser/type-alias-where-fixable.stderr tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: where clause not allowed here
2-
--> $DIR/type-alias-where-fixable.rs:13:16
2+
--> $DIR/where-clause-placement-assoc-type-in-impl.rs:13:16
33
|
44
LL | type Assoc where u32: Copy = ();
55
| ^^^^^^^^^^^^^^^
@@ -13,7 +13,7 @@ LL + type Assoc = () where u32: Copy;
1313
|
1414

1515
warning: where clause not allowed here
16-
--> $DIR/type-alias-where-fixable.rs:16:17
16+
--> $DIR/where-clause-placement-assoc-type-in-impl.rs:16:17
1717
|
1818
LL | type Assoc2 where u32: Copy = () where i32: Copy;
1919
| ^^^^^^^^^^^^^^^
@@ -26,7 +26,7 @@ LL + type Assoc2 = () where i32: Copy, u32: Copy;
2626
|
2727

2828
warning: where clause not allowed here
29-
--> $DIR/type-alias-where-fixable.rs:24:17
29+
--> $DIR/where-clause-placement-assoc-type-in-impl.rs:24:17
3030
|
3131
LL | type Assoc2 where u32: Copy, i32: Copy = ();
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
// run-rustfix
3+
4+
#![feature(associated_type_defaults)]
5+
6+
trait Trait {
7+
// Not fine, suggests moving.
8+
type Assoc = () where u32: Copy;
9+
//~^ WARNING where clause not allowed here
10+
// Not fine, suggests moving `u32: Copy`
11+
type Assoc2 = () where i32: Copy, u32: Copy;
12+
//~^ WARNING where clause not allowed here
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// check-pass
2+
// run-rustfix
3+
4+
#![feature(associated_type_defaults)]
5+
6+
trait Trait {
7+
// Not fine, suggests moving.
8+
type Assoc where u32: Copy = ();
9+
//~^ WARNING where clause not allowed here
10+
// Not fine, suggests moving `u32: Copy`
11+
type Assoc2 where u32: Copy = () where i32: Copy;
12+
//~^ WARNING where clause not allowed here
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
warning: where clause not allowed here
2+
--> $DIR/where-clause-placement-assoc-type-in-trait.rs:8:16
3+
|
4+
LL | type Assoc where u32: Copy = ();
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information
8+
= note: `#[warn(deprecated_where_clause_location)]` on by default
9+
help: move it to the end of the type declaration
10+
|
11+
LL - type Assoc where u32: Copy = ();
12+
LL + type Assoc = () where u32: Copy;
13+
|
14+
15+
warning: where clause not allowed here
16+
--> $DIR/where-clause-placement-assoc-type-in-trait.rs:11:17
17+
|
18+
LL | type Assoc2 where u32: Copy = () where i32: Copy;
19+
| ^^^^^^^^^^^^^^^
20+
|
21+
= note: see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information
22+
help: move it to the end of the type declaration
23+
|
24+
LL - type Assoc2 where u32: Copy = () where i32: Copy;
25+
LL + type Assoc2 = () where i32: Copy, u32: Copy;
26+
|
27+
28+
warning: 2 warnings emitted
29+

tests/ui/parser/type-alias-where.stderr tests/ui/where-clauses/where-clause-placement-type-alias.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: where clauses are not allowed after the type for type aliases
2-
--> $DIR/type-alias-where.rs:6:15
2+
--> $DIR/where-clause-placement-type-alias.rs:6:15
33
|
44
LL | type Bar = () where u32: Copy;
55
| ^^^^^^^^^^^^^^^
66
|
77
= note: see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information
88

99
error: where clauses are not allowed after the type for type aliases
10-
--> $DIR/type-alias-where.rs:8:15
10+
--> $DIR/where-clause-placement-type-alias.rs:8:15
1111
|
1212
LL | type Baz = () where;
1313
| ^^^^^

0 commit comments

Comments
 (0)