Skip to content

Commit befdfb5

Browse files
notriddleestebank
andcommitted
Improve error messages for bad type constraints
Co-authored-by: Esteban Kuber <esteban@kuber.com.ar>
1 parent 8a7c130 commit befdfb5

File tree

5 files changed

+34
-30
lines changed

5 files changed

+34
-30
lines changed

compiler/rustc_parse/src/parser/path.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -495,25 +495,28 @@ impl<'a> Parser<'a> {
495495
None => {
496496
let after_eq = eq.shrink_to_hi();
497497
let before_next = self.token.span.shrink_to_lo();
498-
let the_type_placeholder = if matches!(self.token.kind, token::Comma | token::Gt) {
499-
" TheType"
500-
} else {
501-
" TheType "
502-
};
503-
self.struct_span_err(after_eq.to(before_next), "missing type to the right of `=`")
504-
.span_suggestion(
498+
let mut err = self
499+
.struct_span_err(after_eq.to(before_next), "missing type to the right of `=`");
500+
if matches!(self.token.kind, token::Comma | token::Gt) {
501+
err.span_suggestion(
505502
self.sess.source_map().next_point(eq).to(before_next),
506503
"to constrain the associated type, add a type after `=`",
507-
the_type_placeholder.to_string(),
504+
" TheType".to_string(),
508505
Applicability::HasPlaceholders,
509-
)
510-
.span_suggestion(
506+
);
507+
err.span_suggestion(
511508
eq.to(before_next),
512509
&format!("remove the `=` if `{}` is a type", ident),
513510
String::new(),
514511
Applicability::MaybeIncorrect,
515512
)
516-
.emit();
513+
} else {
514+
err.span_label(
515+
self.token.span,
516+
&format!("expected type, found {}", super::token_descr(&self.token)),
517+
)
518+
};
519+
return Err(err);
517520
}
518521
}
519522
Ok(self.mk_ty(span, ast::TyKind::Err))
@@ -584,6 +587,12 @@ impl<'a> Parser<'a> {
584587
"expected lifetime, type, or constant, found keyword `const`",
585588
);
586589
if self.check_const_arg() {
590+
err.span_suggestion_verbose(
591+
start.until(self.token.span),
592+
"the `const` keyword is only needed in the definition of the type",
593+
String::new(),
594+
Applicability::MaybeIncorrect,
595+
);
587596
err.emit();
588597
GenericArg::Const(self.parse_const_arg()?)
589598
} else {

src/test/ui/const-generics/parser-error-recovery/issue-89013-no-assoc.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error: expected lifetime, type, or constant, found keyword `const`
33
|
44
LL | impl Foo<const 3> for Bar {
55
| ^^^^^
6+
|
7+
help: the `const` keyword is only needed in the definition of the type
8+
|
9+
LL - impl Foo<const 3> for Bar {
10+
LL + impl Foo<3> for Bar {
11+
|
612

713
error: aborting due to previous error
814

src/test/ui/const-generics/parser-error-recovery/issue-89013-type.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const T: usize = 42;
88

99
impl Foo<N = type 3> for Bar {
1010
//~^ERROR missing type to the right of `=`
11-
//~^^ERROR found keyword `type`
1211
fn do_x(&self) -> [u8; 3] {
1312
[0u8; 3]
1413
}

src/test/ui/const-generics/parser-error-recovery/issue-89013-type.stderr

+2-18
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,7 @@ error: missing type to the right of `=`
22
--> $DIR/issue-89013-type.rs:9:13
33
|
44
LL | impl Foo<N = type 3> for Bar {
5-
| ^
6-
|
7-
help: to constrain the associated type, add a type after `=`
8-
|
9-
LL | impl Foo<N = TheType type 3> for Bar {
10-
| +++++++
11-
help: remove the `=` if `N` is a type
12-
|
13-
LL - impl Foo<N = type 3> for Bar {
14-
LL + impl Foo<N type 3> for Bar {
15-
|
16-
17-
error: expected one of `,`, `>`, a const expression, lifetime, or type, found keyword `type`
18-
--> $DIR/issue-89013-type.rs:9:14
19-
|
20-
LL | impl Foo<N = type 3> for Bar {
21-
| ^^^^ expected one of `,`, `>`, a const expression, lifetime, or type
5+
| ^---- expected type, found keyword `type`
226

23-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
248

src/test/ui/const-generics/parser-error-recovery/issue-89013.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error: expected lifetime, type, or constant, found keyword `const`
33
|
44
LL | impl Foo<N = const 3> for Bar {
55
| ^^^^^
6+
|
7+
help: the `const` keyword is only needed in the definition of the type
8+
|
9+
LL - impl Foo<N = const 3> for Bar {
10+
LL + impl Foo<N = 3> for Bar {
11+
|
612

713
error: cannot constrain an associated constant to a value
814
--> $DIR/issue-89013.rs:9:10

0 commit comments

Comments
 (0)