Improve error messages for bad type constraints
Co-authored-by: Esteban Kuber <esteban@kuber.com.ar>
This commit is contained in:
parent
8a7c1306b4
commit
befdfb5c71
5 changed files with 34 additions and 30 deletions
|
@ -495,25 +495,28 @@ impl<'a> Parser<'a> {
|
||||||
None => {
|
None => {
|
||||||
let after_eq = eq.shrink_to_hi();
|
let after_eq = eq.shrink_to_hi();
|
||||||
let before_next = self.token.span.shrink_to_lo();
|
let before_next = self.token.span.shrink_to_lo();
|
||||||
let the_type_placeholder = if matches!(self.token.kind, token::Comma | token::Gt) {
|
let mut err = self
|
||||||
" TheType"
|
.struct_span_err(after_eq.to(before_next), "missing type to the right of `=`");
|
||||||
} else {
|
if matches!(self.token.kind, token::Comma | token::Gt) {
|
||||||
" TheType "
|
err.span_suggestion(
|
||||||
};
|
|
||||||
self.struct_span_err(after_eq.to(before_next), "missing type to the right of `=`")
|
|
||||||
.span_suggestion(
|
|
||||||
self.sess.source_map().next_point(eq).to(before_next),
|
self.sess.source_map().next_point(eq).to(before_next),
|
||||||
"to constrain the associated type, add a type after `=`",
|
"to constrain the associated type, add a type after `=`",
|
||||||
the_type_placeholder.to_string(),
|
" TheType".to_string(),
|
||||||
Applicability::HasPlaceholders,
|
Applicability::HasPlaceholders,
|
||||||
)
|
);
|
||||||
.span_suggestion(
|
err.span_suggestion(
|
||||||
eq.to(before_next),
|
eq.to(before_next),
|
||||||
&format!("remove the `=` if `{}` is a type", ident),
|
&format!("remove the `=` if `{}` is a type", ident),
|
||||||
String::new(),
|
String::new(),
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
)
|
)
|
||||||
.emit();
|
} else {
|
||||||
|
err.span_label(
|
||||||
|
self.token.span,
|
||||||
|
&format!("expected type, found {}", super::token_descr(&self.token)),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
return Err(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(self.mk_ty(span, ast::TyKind::Err))
|
Ok(self.mk_ty(span, ast::TyKind::Err))
|
||||||
|
@ -584,6 +587,12 @@ impl<'a> Parser<'a> {
|
||||||
"expected lifetime, type, or constant, found keyword `const`",
|
"expected lifetime, type, or constant, found keyword `const`",
|
||||||
);
|
);
|
||||||
if self.check_const_arg() {
|
if self.check_const_arg() {
|
||||||
|
err.span_suggestion_verbose(
|
||||||
|
start.until(self.token.span),
|
||||||
|
"the `const` keyword is only needed in the definition of the type",
|
||||||
|
String::new(),
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
err.emit();
|
err.emit();
|
||||||
GenericArg::Const(self.parse_const_arg()?)
|
GenericArg::Const(self.parse_const_arg()?)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,6 +3,12 @@ error: expected lifetime, type, or constant, found keyword `const`
|
||||||
|
|
|
|
||||||
LL | impl Foo<const 3> for Bar {
|
LL | impl Foo<const 3> for Bar {
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
|
||||||
|
help: the `const` keyword is only needed in the definition of the type
|
||||||
|
|
|
||||||
|
LL - impl Foo<const 3> for Bar {
|
||||||
|
LL + impl Foo<3> for Bar {
|
||||||
|
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ const T: usize = 42;
|
||||||
|
|
||||||
impl Foo<N = type 3> for Bar {
|
impl Foo<N = type 3> for Bar {
|
||||||
//~^ERROR missing type to the right of `=`
|
//~^ERROR missing type to the right of `=`
|
||||||
//~^^ERROR found keyword `type`
|
|
||||||
fn do_x(&self) -> [u8; 3] {
|
fn do_x(&self) -> [u8; 3] {
|
||||||
[0u8; 3]
|
[0u8; 3]
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,23 +2,7 @@ error: missing type to the right of `=`
|
||||||
--> $DIR/issue-89013-type.rs:9:13
|
--> $DIR/issue-89013-type.rs:9:13
|
||||||
|
|
|
|
||||||
LL | impl Foo<N = type 3> for Bar {
|
LL | impl Foo<N = type 3> for Bar {
|
||||||
| ^
|
| ^---- expected type, found keyword `type`
|
||||||
|
|
|
||||||
help: to constrain the associated type, add a type after `=`
|
|
||||||
|
|
|
||||||
LL | impl Foo<N = TheType type 3> for Bar {
|
|
||||||
| +++++++
|
|
||||||
help: remove the `=` if `N` is a type
|
|
||||||
|
|
|
||||||
LL - impl Foo<N = type 3> for Bar {
|
|
||||||
LL + impl Foo<N type 3> for Bar {
|
|
||||||
|
|
|
||||||
|
|
||||||
error: expected one of `,`, `>`, a const expression, lifetime, or type, found keyword `type`
|
error: aborting due to previous error
|
||||||
--> $DIR/issue-89013-type.rs:9:14
|
|
||||||
|
|
|
||||||
LL | impl Foo<N = type 3> for Bar {
|
|
||||||
| ^^^^ expected one of `,`, `>`, a const expression, lifetime, or type
|
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,12 @@ error: expected lifetime, type, or constant, found keyword `const`
|
||||||
|
|
|
|
||||||
LL | impl Foo<N = const 3> for Bar {
|
LL | impl Foo<N = const 3> for Bar {
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
|
|
||||||
|
help: the `const` keyword is only needed in the definition of the type
|
||||||
|
|
|
||||||
|
LL - impl Foo<N = const 3> for Bar {
|
||||||
|
LL + impl Foo<N = 3> for Bar {
|
||||||
|
|
|
||||||
|
|
||||||
error: cannot constrain an associated constant to a value
|
error: cannot constrain an associated constant to a value
|
||||||
--> $DIR/issue-89013.rs:9:10
|
--> $DIR/issue-89013.rs:9:10
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue