From b809207dec106b261884daea036fad16eb8a2f89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Tue, 11 Jul 2023 01:11:21 +0200 Subject: [PATCH] Lint against misplaced where-clauses on assoc tys in traits --- .../rustc_ast_passes/src/ast_validation.rs | 31 ++++++++++--------- compiler/rustc_lint_defs/src/builtin.rs | 2 +- ...clause-placement-assoc-type-in-impl.fixed} | 0 ...re-clause-placement-assoc-type-in-impl.rs} | 0 ...lause-placement-assoc-type-in-impl.stderr} | 6 ++-- ...clause-placement-assoc-type-in-trait.fixed | 15 +++++++++ ...re-clause-placement-assoc-type-in-trait.rs | 15 +++++++++ ...lause-placement-assoc-type-in-trait.stderr | 29 +++++++++++++++++ .../where-clause-placement-type-alias.rs} | 0 .../where-clause-placement-type-alias.stderr} | 4 +-- 10 files changed, 81 insertions(+), 21 deletions(-) rename tests/ui/{parser/type-alias-where-fixable.fixed => where-clauses/where-clause-placement-assoc-type-in-impl.fixed} (100%) rename tests/ui/{parser/type-alias-where-fixable.rs => where-clauses/where-clause-placement-assoc-type-in-impl.rs} (100%) rename tests/ui/{parser/type-alias-where-fixable.stderr => where-clauses/where-clause-placement-assoc-type-in-impl.stderr} (87%) create mode 100644 tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.fixed create mode 100644 tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.rs create mode 100644 tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.stderr rename tests/ui/{parser/type-alias-where.rs => where-clauses/where-clause-placement-type-alias.rs} (100%) rename tests/ui/{parser/type-alias-where.stderr => where-clauses/where-clause-placement-type-alias.stderr} (83%) diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index 096cea945b029..418e1df5857a4 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1300,14 +1300,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { }); } } - AssocItemKind::Type(box TyAlias { - generics, - where_clauses, - where_predicates_split, - bounds, - ty, - .. - }) => { + AssocItemKind::Type(box TyAlias { bounds, ty, .. }) => { if ty.is_none() { self.session.emit_err(errors::AssocTypeWithoutBody { span: item.span, @@ -1315,18 +1308,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> { }); } self.check_type_no_bounds(bounds, "`impl`s"); - if ty.is_some() { - self.check_gat_where( - item.id, - generics.where_clause.predicates.split_at(*where_predicates_split).0, - *where_clauses, - ); - } } _ => {} } } + if let AssocItemKind::Type(box TyAlias { + generics, + where_clauses, + where_predicates_split, + ty: Some(_), + .. + }) = &item.kind + { + self.check_gat_where( + item.id, + generics.where_clause.predicates.split_at(*where_predicates_split).0, + *where_clauses, + ); + } + if ctxt == AssocCtxt::Trait || self.in_trait_impl { self.visibility_not_permitted(&item.vis, errors::VisibilityNotPermittedNote::TraitImpl); if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind { diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 87c542dc2e26a..a0f2e9aed811a 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -4084,7 +4084,7 @@ declare_lint! { /// /// ### Explanation /// - /// The preferred location for where clauses on associated types in impls + /// The preferred location for where clauses on associated types /// is after the type. However, for most of generic associated types development, /// it was only accepted before the equals. To provide a transition period and /// further evaluate this change, both are currently accepted. At some point in diff --git a/tests/ui/parser/type-alias-where-fixable.fixed b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.fixed similarity index 100% rename from tests/ui/parser/type-alias-where-fixable.fixed rename to tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.fixed diff --git a/tests/ui/parser/type-alias-where-fixable.rs b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.rs similarity index 100% rename from tests/ui/parser/type-alias-where-fixable.rs rename to tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.rs diff --git a/tests/ui/parser/type-alias-where-fixable.stderr b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.stderr similarity index 87% rename from tests/ui/parser/type-alias-where-fixable.stderr rename to tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.stderr index f0acb388b97a1..b4de051845fa6 100644 --- a/tests/ui/parser/type-alias-where-fixable.stderr +++ b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.stderr @@ -1,5 +1,5 @@ warning: where clause not allowed here - --> $DIR/type-alias-where-fixable.rs:13:16 + --> $DIR/where-clause-placement-assoc-type-in-impl.rs:13:16 | LL | type Assoc where u32: Copy = (); | ^^^^^^^^^^^^^^^ @@ -13,7 +13,7 @@ LL + type Assoc = () where u32: Copy; | warning: where clause not allowed here - --> $DIR/type-alias-where-fixable.rs:16:17 + --> $DIR/where-clause-placement-assoc-type-in-impl.rs:16:17 | LL | type Assoc2 where u32: Copy = () where i32: Copy; | ^^^^^^^^^^^^^^^ @@ -26,7 +26,7 @@ LL + type Assoc2 = () where i32: Copy, u32: Copy; | warning: where clause not allowed here - --> $DIR/type-alias-where-fixable.rs:24:17 + --> $DIR/where-clause-placement-assoc-type-in-impl.rs:24:17 | LL | type Assoc2 where u32: Copy, i32: Copy = (); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.fixed b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.fixed new file mode 100644 index 0000000000000..d171eba50b788 --- /dev/null +++ b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.fixed @@ -0,0 +1,15 @@ +// check-pass +// run-rustfix + +#![feature(associated_type_defaults)] + +trait Trait { + // Not fine, suggests moving. + type Assoc = () where u32: Copy; + //~^ WARNING where clause not allowed here + // Not fine, suggests moving `u32: Copy` + type Assoc2 = () where i32: Copy, u32: Copy; + //~^ WARNING where clause not allowed here +} + +fn main() {} diff --git a/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.rs b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.rs new file mode 100644 index 0000000000000..59afee65794c4 --- /dev/null +++ b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.rs @@ -0,0 +1,15 @@ +// check-pass +// run-rustfix + +#![feature(associated_type_defaults)] + +trait Trait { + // Not fine, suggests moving. + type Assoc where u32: Copy = (); + //~^ WARNING where clause not allowed here + // Not fine, suggests moving `u32: Copy` + type Assoc2 where u32: Copy = () where i32: Copy; + //~^ WARNING where clause not allowed here +} + +fn main() {} diff --git a/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.stderr b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.stderr new file mode 100644 index 0000000000000..a81cb8c8cd62e --- /dev/null +++ b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-trait.stderr @@ -0,0 +1,29 @@ +warning: where clause not allowed here + --> $DIR/where-clause-placement-assoc-type-in-trait.rs:8:16 + | +LL | type Assoc where u32: Copy = (); + | ^^^^^^^^^^^^^^^ + | + = note: see issue #89122 for more information + = note: `#[warn(deprecated_where_clause_location)]` on by default +help: move it to the end of the type declaration + | +LL - type Assoc where u32: Copy = (); +LL + type Assoc = () where u32: Copy; + | + +warning: where clause not allowed here + --> $DIR/where-clause-placement-assoc-type-in-trait.rs:11:17 + | +LL | type Assoc2 where u32: Copy = () where i32: Copy; + | ^^^^^^^^^^^^^^^ + | + = note: see issue #89122 for more information +help: move it to the end of the type declaration + | +LL - type Assoc2 where u32: Copy = () where i32: Copy; +LL + type Assoc2 = () where i32: Copy, u32: Copy; + | + +warning: 2 warnings emitted + diff --git a/tests/ui/parser/type-alias-where.rs b/tests/ui/where-clauses/where-clause-placement-type-alias.rs similarity index 100% rename from tests/ui/parser/type-alias-where.rs rename to tests/ui/where-clauses/where-clause-placement-type-alias.rs diff --git a/tests/ui/parser/type-alias-where.stderr b/tests/ui/where-clauses/where-clause-placement-type-alias.stderr similarity index 83% rename from tests/ui/parser/type-alias-where.stderr rename to tests/ui/where-clauses/where-clause-placement-type-alias.stderr index fb83817926696..b3c155a48ddbc 100644 --- a/tests/ui/parser/type-alias-where.stderr +++ b/tests/ui/where-clauses/where-clause-placement-type-alias.stderr @@ -1,5 +1,5 @@ error: where clauses are not allowed after the type for type aliases - --> $DIR/type-alias-where.rs:6:15 + --> $DIR/where-clause-placement-type-alias.rs:6:15 | LL | type Bar = () where u32: Copy; | ^^^^^^^^^^^^^^^ @@ -7,7 +7,7 @@ LL | type Bar = () where u32: Copy; = note: see issue #89122 for more information error: where clauses are not allowed after the type for type aliases - --> $DIR/type-alias-where.rs:8:15 + --> $DIR/where-clause-placement-type-alias.rs:8:15 | LL | type Baz = () where; | ^^^^^