Skip to content

Suggest path separator for single-colon typos #68681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/librustc_parse/parser/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,49 @@ impl<'a> Parser<'a> {
debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
}

self.expect(&token::ModSep)?;
if !self.recover_colon_before_qpath_proj() {
self.expect(&token::ModSep)?;
}

let qself = QSelf { ty, path_span, position: path.segments.len() };
self.parse_path_segments(&mut path.segments, style)?;

Ok((qself, Path { segments: path.segments, span: lo.to(self.prev_span) }))
}

/// Recover from an invalid single colon, when the user likely meant a qualified path.
/// We avoid emitting this if not followed by an identifier, as our assumption that the user
/// intended this to be a qualified path may not be correct.
///
/// ```ignore (diagnostics)
/// <Bar as Baz<T>>:Qux
/// ^ help: use double colon
/// ```
fn recover_colon_before_qpath_proj(&mut self) -> bool {
if self.token.kind != token::Colon
|| self.look_ahead(1, |t| !t.is_ident() || t.is_reserved_ident())
{
return false;
}

self.bump(); // colon

self.diagnostic()
.struct_span_err(
self.prev_span,
"found single colon before projection in qualified path",
)
.span_suggestion(
self.prev_span,
"use double colon",
"::".to_string(),
Applicability::MachineApplicable,
)
.emit();

true
}

/// Parses simple paths.
///
/// `path = [::] segment+`
Expand Down
19 changes: 19 additions & 0 deletions src/test/ui/parser/qualified-path-in-turbofish.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// run-rustfix
trait T {
type Ty;
}

struct Impl;

impl T for Impl {
type Ty = u32;
}

fn template<T>() -> i64 {
3
}

fn main() {
template::<<Impl as T>::Ty>();
//~^ ERROR found single colon before projection in qualified path
}
19 changes: 19 additions & 0 deletions src/test/ui/parser/qualified-path-in-turbofish.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// run-rustfix
trait T {
type Ty;
}

struct Impl;

impl T for Impl {
type Ty = u32;
}

fn template<T>() -> i64 {
3
}

fn main() {
template::<<Impl as T>:Ty>();
//~^ ERROR found single colon before projection in qualified path
}
8 changes: 8 additions & 0 deletions src/test/ui/parser/qualified-path-in-turbofish.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: found single colon before projection in qualified path
--> $DIR/qualified-path-in-turbofish.rs:17:27
|
LL | template::<<Impl as T>:Ty>();
| ^ help: use double colon: `::`

error: aborting due to previous error