Skip to content

Commit 3f504f6

Browse files
committed
Change to lint
1 parent c20b4f5 commit 3f504f6

File tree

8 files changed

+75
-21
lines changed

8 files changed

+75
-21
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ use rustc_ast_pretty::pprust::{self, State};
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_errors::{error_code, pluralize, struct_span_err, Applicability};
1717
use rustc_parse::validate_attr;
18-
use rustc_session::lint::builtin::{MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY};
18+
use rustc_session::lint::builtin::{
19+
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY,
20+
};
1921
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
2022
use rustc_session::Session;
2123
use rustc_span::source_map::Spanned;
@@ -123,11 +125,11 @@ impl<'a> AstValidator<'a> {
123125
}
124126

125127
fn check_gat_where(
126-
&self,
128+
&mut self,
129+
id: NodeId,
127130
before_predicates: &[WherePredicate],
128131
where_clauses: (ast::TyAliasWhereClause, ast::TyAliasWhereClause),
129132
) {
130-
let sess = &self.session;
131133
if !before_predicates.is_empty() {
132134
let mut state = State::new();
133135
if !where_clauses.1.0 {
@@ -145,14 +147,16 @@ impl<'a> AstValidator<'a> {
145147
state.print_where_predicate(p);
146148
}
147149
let suggestion = state.s.eof();
148-
sess.struct_span_err(where_clauses.0.1, "where clause not allowed here")
149-
.span_suggestion(
150+
self.lint_buffer.buffer_lint_with_diagnostic(
151+
DEPRECATED_WHERE_CLAUSE_LOCATION,
152+
id,
153+
where_clauses.0.1,
154+
"where clause not allowed here",
155+
BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(
150156
where_clauses.1.1.shrink_to_hi(),
151-
"move it here",
152157
suggestion,
153-
Applicability::MachineApplicable,
154-
)
155-
.emit();
158+
),
159+
);
156160
}
157161
}
158162

@@ -1568,6 +1572,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15681572
self.check_type_no_bounds(bounds, "`impl`s");
15691573
if ty.is_some() {
15701574
self.check_gat_where(
1575+
item.id,
15711576
generics.where_clause.predicates.split_at(*where_predicates_split).0,
15721577
*where_clauses,
15731578
);

compiler/rustc_lint/src/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,14 @@ pub trait LintContext: Sized {
818818
}
819819
}
820820
},
821+
BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(new_span, suggestion) => {
822+
db.span_suggestion(
823+
new_span,
824+
"move it here",
825+
suggestion,
826+
Applicability::MachineApplicable,
827+
);
828+
},
821829
}
822830
// Rewrap `db`, and pass control to the user.
823831
decorate(LintDiagnosticBuilder::new(db));

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,7 @@ declare_lint_pass! {
31273127
DUPLICATE_MACRO_ATTRIBUTES,
31283128
SUSPICIOUS_AUTO_TRAIT_IMPLS,
31293129
UNEXPECTED_CFGS,
3130+
DEPRECATED_WHERE_CLAUSE_LOCATION,
31303131
]
31313132
}
31323133

@@ -3737,3 +3738,36 @@ declare_lint! {
37373738
reference: "issue #93367 <https://github.com/rust-lang/rust/issues/93367>",
37383739
};
37393740
}
3741+
3742+
declare_lint! {
3743+
/// The `deprecated_where_clause_location` lint detects when a where clause in front of the equals
3744+
/// in an associated type.
3745+
///
3746+
/// ### Example
3747+
///
3748+
/// ```rust
3749+
/// #![feature(generic_associated_types)]
3750+
///
3751+
/// trait Trait {
3752+
/// type Assoc<'a> where Self: 'a;
3753+
/// }
3754+
///
3755+
/// impl Trait for () {
3756+
/// type Assoc<'a> where Self: 'a = ();
3757+
/// }
3758+
/// ```
3759+
///
3760+
/// {{produces}}
3761+
///
3762+
/// ### Explanation
3763+
///
3764+
/// The preferred location for where clauses on associated types in impls
3765+
/// is after the type. However, for most of generic associated types development,
3766+
/// it was only accepted before the equals. To provide a transition period and
3767+
/// further evaluate this change, both are currently accepted. At some point in
3768+
/// the future, this may be disallowed at an edition boundary; but, that is
3769+
/// undecided currently.
3770+
pub DEPRECATED_WHERE_CLAUSE_LOCATION,
3771+
Warn,
3772+
"deprecated where clause location"
3773+
}

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ pub enum BuiltinLintDiagnostics {
427427
NamedAsmLabel(String),
428428
UnicodeTextFlow(Span, String),
429429
UnexpectedCfg((Symbol, Span), Option<(Symbol, Span)>),
430+
DeprecatedWhereclauseLocation(Span, String),
430431
}
431432

432433
/// Lints that are buffered up early on in the `Session` before the

src/test/ui/generic-associated-types/bugs/issue-87735.stderr

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
error: where clause not allowed here
1+
warning: where clause not allowed here
22
--> $DIR/issue-87735.rs:15:19
33
|
44
LL | type Output<'a> where Self: 'a = &'a [T];
55
| ^^^^^^^^^^^^^^ - help: move it here: `where Self: 'a`
6+
|
7+
= note: `#[warn(deprecated_where_clause_location)]` on by default
68

7-
error: where clause not allowed here
9+
warning: where clause not allowed here
810
--> $DIR/issue-87735.rs:36:19
911
|
1012
LL | type Output<'a> where Self: 'a = FooRef<'a, U>;
@@ -16,6 +18,6 @@ error[E0207]: the type parameter `U` is not constrained by the impl trait, self
1618
LL | impl<'b, T, U> AsRef2 for Foo<T>
1719
| ^ unconstrained type parameter
1820

19-
error: aborting due to 3 previous errors
21+
error: aborting due to previous error; 2 warnings emitted
2022

2123
For more information about this error, try `rustc --explain E0207`.

src/test/ui/generic-associated-types/bugs/issue-87748.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
error: where clause not allowed here
1+
warning: where clause not allowed here
22
--> $DIR/issue-87748.rs:16:24
33
|
44
LL | type Assoc<'a, 'b> where 'b: 'a = u32;
55
| ^^^^^^^^^^^^ - help: move it here: `where 'b: 'a`
6+
|
7+
= note: `#[warn(deprecated_where_clause_location)]` on by default
68

79
error[E0478]: lifetime bound not satisfied
810
--> $DIR/issue-87748.rs:18:5
@@ -21,6 +23,6 @@ note: but lifetime parameter must outlive the anonymous lifetime #1 defined here
2123
LL | fn do_sth(_: u32) {}
2224
| ^^^^^^^^^^^^^^^^^
2325

24-
error: aborting due to 2 previous errors
26+
error: aborting due to previous error; 1 warning emitted
2527

2628
For more information about this error, try `rustc --explain E0478`.

src/test/ui/parser/type-alias-where.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ trait Trait {
2020
impl Trait for u32 {
2121
// Not fine, suggests moving.
2222
type Assoc where u32: Copy = ();
23-
//~^ ERROR where clause not allowed here
23+
//~^ WARNING where clause not allowed here
2424
// Not fine, suggests moving `u32: Copy`
2525
type Assoc2 where u32: Copy = () where i32: Copy;
26-
//~^ ERROR where clause not allowed here
26+
//~^ WARNING where clause not allowed here
2727
}
2828

2929
impl Trait for i32 {
3030
// Fine.
3131
type Assoc = () where u32: Copy;
3232
// Not fine, suggests moving both.
3333
type Assoc2 where u32: Copy, i32: Copy = ();
34-
//~^ ERROR where clause not allowed here
34+
//~^ WARNING where clause not allowed here
3535
}
3636

3737
fn main() {}

src/test/ui/parser/type-alias-where.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,25 @@ error: where clauses are not allowed after the type for type aliases
1010
LL | type Baz = () where;
1111
| ^^^^^
1212

13-
error: where clause not allowed here
13+
warning: where clause not allowed here
1414
--> $DIR/type-alias-where.rs:22:16
1515
|
1616
LL | type Assoc where u32: Copy = ();
1717
| ^^^^^^^^^^^^^^^ - help: move it here: `where u32: Copy`
18+
|
19+
= note: `#[warn(deprecated_where_clause_location)]` on by default
1820

19-
error: where clause not allowed here
21+
warning: where clause not allowed here
2022
--> $DIR/type-alias-where.rs:25:17
2123
|
2224
LL | type Assoc2 where u32: Copy = () where i32: Copy;
2325
| ^^^^^^^^^^^^^^^ - help: move it here: `, u32: Copy`
2426

25-
error: where clause not allowed here
27+
warning: where clause not allowed here
2628
--> $DIR/type-alias-where.rs:33:17
2729
|
2830
LL | type Assoc2 where u32: Copy, i32: Copy = ();
2931
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ - help: move it here: `where u32: Copy, i32: Copy`
3032

31-
error: aborting due to 5 previous errors
33+
error: aborting due to 2 previous errors; 3 warnings emitted
3234

0 commit comments

Comments
 (0)