diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index dd14e91435690..d59b148154c93 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -173,17 +173,19 @@ impl<'a> AstValidator<'a> { { let mut state = State::new(); + let mut needs_comma = !ty_alias.after_where_clause.predicates.is_empty(); if !ty_alias.after_where_clause.has_where_token { state.space(); state.word_space("where"); + } else if !needs_comma { + state.space(); } - let mut first = ty_alias.after_where_clause.predicates.is_empty(); for p in &ty_alias.generics.where_clause.predicates { - if !first { + if needs_comma { state.word_space(","); } - first = false; + needs_comma = true; state.print_where_predicate(p); } @@ -1832,7 +1834,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> { Some((right, snippet)) } }; - let left_sp = err.span; + let left_sp = self + .sess + .source_map() + .span_extend_prev_while(err.span, char::is_whitespace) + .unwrap_or(err.span); self.lint_buffer.dyn_buffer_lint( DEPRECATED_WHERE_CLAUSE_LOCATION, item.id, @@ -1846,9 +1852,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> { sugg, } } - None => { - errors::DeprecatedWhereClauseLocationSugg::RemoveWhere { span: left_sp } - } + None => errors::DeprecatedWhereClauseLocationSugg::RemoveWhere { + span: err.span, + }, }; errors::DeprecatedWhereClauseLocation { suggestion }.into_diag(dcx, level) }, diff --git a/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.fixed b/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.fixed new file mode 100644 index 0000000000000..01da73716e884 --- /dev/null +++ b/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.fixed @@ -0,0 +1,17 @@ +//@ check-pass +//@ run-rustfix +//@ compile-flags: --force-warn deprecated_where_clause_location + + +#![allow(dead_code)] + +trait Trait { + type Assoc; +} + +impl Trait for i32 { + type Assoc = () where i32: Copy; + //~^ WARNING where clause not allowed here +} + +fn main() {} diff --git a/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.rs b/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.rs new file mode 100644 index 0000000000000..cdee4c753b87d --- /dev/null +++ b/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.rs @@ -0,0 +1,17 @@ +//@ check-pass +//@ run-rustfix +//@ compile-flags: --force-warn deprecated_where_clause_location + + +#![allow(dead_code)] + +trait Trait { + type Assoc; +} + +impl Trait for i32 { + type Assoc where i32: Copy = () where; + //~^ WARNING where clause not allowed here +} + +fn main() {} diff --git a/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.stderr b/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.stderr new file mode 100644 index 0000000000000..d3421505bf5ca --- /dev/null +++ b/tests/ui/where-clauses/alias-type-where-suggest-issue-153567.stderr @@ -0,0 +1,16 @@ +warning: where clause not allowed here + --> $DIR/alias-type-where-suggest-issue-153567.rs:13:16 + | +LL | type Assoc where i32: Copy = () where; + | ^^^^^^^^^^^^^^^ + | + = note: see issue #89122 for more information + = note: requested on the command line with `--force-warn deprecated-where-clause-location` +help: move it to the end of the type declaration + | +LL - type Assoc where i32: Copy = () where; +LL + type Assoc = () where i32: Copy; + | + +warning: 1 warning emitted + diff --git a/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.fixed b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.fixed index 9b935b1667878..5f3882ccb412e 100644 --- a/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.fixed +++ b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.fixed @@ -14,12 +14,12 @@ trait Trait { impl Trait for u32 { // Not fine, suggests moving. - type Assoc = () where u32: Copy; + type Assoc = () where u32: Copy; //~^ WARNING where clause not allowed here // Not fine, suggests moving `u32: Copy` - type Assoc2 = () where i32: Copy, u32: Copy; + type Assoc2 = () where i32: Copy, u32: Copy; //~^ WARNING where clause not allowed here - type Assoc3 = () where; + type Assoc3 = () where; //~^ WARNING where clause not allowed here } @@ -27,7 +27,7 @@ impl Trait for i32 { // Fine. type Assoc = () where u32: Copy; // Not fine, suggests moving both. - type Assoc2 = () where u32: Copy, i32: Copy; + type Assoc2 = () where u32: Copy, i32: Copy; //~^ WARNING where clause not allowed here type Assoc3 = () where; //~^ WARNING where clause not allowed here diff --git a/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.stderr b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.stderr index 5809ff8f80347..f31c8b2dd8219 100644 --- a/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.stderr +++ b/tests/ui/where-clauses/where-clause-placement-assoc-type-in-impl.stderr @@ -9,7 +9,7 @@ LL | type Assoc where u32: Copy = (); help: move it to the end of the type declaration | LL - type Assoc where u32: Copy = (); -LL + type Assoc = () where u32: Copy; +LL + type Assoc = () where u32: Copy; | warning: where clause not allowed here @@ -22,7 +22,7 @@ LL | type Assoc2 where u32: Copy = () where i32: Copy; 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; +LL + type Assoc2 = () where i32: Copy, u32: Copy; | warning: where clause not allowed here @@ -35,7 +35,7 @@ LL | type Assoc3 where = (); help: move it to the end of the type declaration | LL - type Assoc3 where = (); -LL + type Assoc3 = () where; +LL + type Assoc3 = () where; | warning: where clause not allowed here @@ -48,7 +48,7 @@ LL | type Assoc2 where u32: Copy, i32: Copy = (); help: move it to the end of the type declaration | LL - type Assoc2 where u32: Copy, i32: Copy = (); -LL + type Assoc2 = () where u32: Copy, i32: Copy; +LL + type Assoc2 = () where u32: Copy, i32: Copy; | warning: where clause not allowed here 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 index 719f1d2a4c603..02dba8f946d6c 100644 --- 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 @@ -6,10 +6,10 @@ trait Trait { // Not fine, suggests moving. - type Assoc = () where u32: Copy; + type Assoc = () where u32: Copy; //~^ WARNING where clause not allowed here // Not fine, suggests moving `u32: Copy` - type Assoc2 = () where i32: Copy, u32: Copy; + type Assoc2 = () where i32: Copy, u32: Copy; //~^ WARNING where clause not allowed here } 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 index 9e9967ef7391a..1b7c09897b7b8 100644 --- 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 @@ -9,7 +9,7 @@ LL | type Assoc where u32: Copy = (); help: move it to the end of the type declaration | LL - type Assoc where u32: Copy = (); -LL + type Assoc = () where u32: Copy; +LL + type Assoc = () where u32: Copy; | warning: where clause not allowed here @@ -22,7 +22,7 @@ LL | type Assoc2 where u32: Copy = () where i32: Copy; 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; +LL + type Assoc2 = () where i32: Copy, u32: Copy; | warning: 2 warnings emitted