Rollup merge of #95318 - rust-lang:notriddle/issue-95208, r=wesleywiser

diagnostics: correct generic bounds with doubled colon

Fixes #95208
This commit is contained in:
Dylan DPC 2022-03-28 20:41:50 +02:00 committed by GitHub
commit e10d5039bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 93 additions and 0 deletions

View file

@ -2369,6 +2369,34 @@ impl<'a> Parser<'a> {
Err(err)
}
crate fn maybe_recover_bounds_doubled_colon(&mut self, ty: &Ty) -> PResult<'a, ()> {
let TyKind::Path(qself, path) = &ty.kind else { return Ok(()) };
let qself_position = qself.as_ref().map(|qself| qself.position);
for (i, segments) in path.segments.windows(2).enumerate() {
if qself_position.map(|pos| i < pos).unwrap_or(false) {
continue;
}
if let [a, b] = segments {
let (a_span, b_span) = (a.span(), b.span());
let between_span = a_span.shrink_to_hi().to(b_span.shrink_to_lo());
if self.span_to_snippet(between_span).as_ref().map(|a| &a[..]) == Ok(":: ") {
let mut err = self.struct_span_err(
path.span.shrink_to_hi(),
"expected `:` followed by trait or lifetime",
);
err.span_suggestion(
between_span,
"use single colon",
": ".to_owned(),
Applicability::MachineApplicable,
);
return Err(err);
}
}
}
Ok(())
}
/// Parse and throw away a parenthesized comma separated
/// sequence of patterns until `)` is reached.
fn skip_pat_list(&mut self) -> PResult<'a, ()> {

View file

@ -312,6 +312,7 @@ impl<'a> Parser<'a> {
id: ast::DUMMY_NODE_ID,
}))
} else {
self.maybe_recover_bounds_doubled_colon(&ty)?;
self.unexpected()
}
}

View file

@ -0,0 +1,11 @@
// run-rustfix
#[allow(unused)]
struct Struct<T>(T);
impl<T: Iterator> Struct<T> where <T as std:: iter::Iterator>::Item: std::fmt::Display {
//~^ ERROR expected `:` followed by trait or lifetime
//~| HELP use single colon
}
fn main() {}

View file

@ -0,0 +1,11 @@
// run-rustfix
#[allow(unused)]
struct Struct<T>(T);
impl<T: Iterator> Struct<T> where <T as std:: iter::Iterator>::Item:: std::fmt::Display {
//~^ ERROR expected `:` followed by trait or lifetime
//~| HELP use single colon
}
fn main() {}

View file

@ -0,0 +1,10 @@
error: expected `:` followed by trait or lifetime
--> $DIR/issue-95208-ignore-qself.rs:6:88
|
LL | impl<T: Iterator> Struct<T> where <T as std:: iter::Iterator>::Item:: std::fmt::Display {
| --- ^
| |
| help: use single colon: `:`
error: aborting due to previous error

View file

@ -0,0 +1,11 @@
// run-rustfix
#[allow(unused)]
struct Struct<T>(T);
impl<T> Struct<T> where T: std::fmt::Display {
//~^ ERROR expected `:` followed by trait or lifetime
//~| HELP use single colon
}
fn main() {}

View file

@ -0,0 +1,11 @@
// run-rustfix
#[allow(unused)]
struct Struct<T>(T);
impl<T> Struct<T> where T:: std::fmt::Display {
//~^ ERROR expected `:` followed by trait or lifetime
//~| HELP use single colon
}
fn main() {}

View file

@ -0,0 +1,10 @@
error: expected `:` followed by trait or lifetime
--> $DIR/issue-95208.rs:6:46
|
LL | impl<T> Struct<T> where T:: std::fmt::Display {
| --- ^
| |
| help: use single colon: `:`
error: aborting due to previous error