Skip to content

Commit 9342be5

Browse files
committed
Recover invalid assoc type bounds using ==
1 parent 78a46ef commit 9342be5

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1955,7 +1955,19 @@ impl<'a> Parser<'a> {
19551955
}
19561956
match self.parse_expr_res(Restrictions::CONST_EXPR, None) {
19571957
Ok(expr) => {
1958-
if token::Comma == self.token.kind || self.token.kind.should_end_const_arg() {
1958+
// Find a mistake like `MyTrait<Assoc == S::Assoc>`.
1959+
if token::EqEq == snapshot.token.kind {
1960+
err.span_suggestion(
1961+
snapshot.token.span,
1962+
"replace `==` with `=`",
1963+
"=".to_string(),
1964+
Applicability::MaybeIncorrect,
1965+
);
1966+
let value = self.mk_expr_err(expr.span);
1967+
err.emit();
1968+
return Ok(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value }));
1969+
} else if token::Comma == self.token.kind || self.token.kind.should_end_const_arg()
1970+
{
19591971
// Avoid the following output by checking that we consumed a full const arg:
19601972
// help: expressions must be enclosed in braces to be used as const generic
19611973
// arguments
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub trait MyTrait {
2+
type Assoc;
3+
}
4+
5+
pub fn foo<S, T>(_s: S, _t: T)
6+
where
7+
S: MyTrait,
8+
T: MyTrait<Assoc == S::Assoc>,
9+
//~^ ERROR: expected one of `,` or `>`, found `==`
10+
//~| ERROR: this trait takes 0 generic arguments but 1 generic argument was supplied
11+
{
12+
}
13+
14+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: expected one of `,` or `>`, found `==`
2+
--> $DIR/issue-87493.rs:8:22
3+
|
4+
LL | T: MyTrait<Assoc == S::Assoc>,
5+
| ^^
6+
| |
7+
| expected one of `,` or `>`
8+
| help: replace `==` with `=`: `=`
9+
10+
error[E0107]: this trait takes 0 generic arguments but 1 generic argument was supplied
11+
--> $DIR/issue-87493.rs:8:8
12+
|
13+
LL | T: MyTrait<Assoc == S::Assoc>,
14+
| ^^^^^^^------------------- help: remove these generics
15+
| |
16+
| expected 0 generic arguments
17+
|
18+
note: trait defined here, with 0 generic parameters
19+
--> $DIR/issue-87493.rs:1:11
20+
|
21+
LL | pub trait MyTrait {
22+
| ^^^^^^^
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)