Skip to content

Commit 3c91bdc

Browse files
committed
Suggest path separator for single-colon typos
This commit adds guidance for when a user means to type a path, but ends up typing a single colon, such as `<<Impl as T>:Ty>`. This change seemed pertinent as the current error message is particularly misleading, emitting `error: unmatched angle bracket`, despite the angle bracket being matched later on, leaving the user to track down the typo'd colon.
1 parent 212b2c7 commit 3c91bdc

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

src/librustc_parse/parser/path.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,23 @@ impl<'a> Parser<'a> {
7171
debug!("parse_qpath: (decrement) count={:?}", self.unmatched_angle_bracket_count);
7272
}
7373

74-
self.expect(&token::ModSep)?;
74+
let lo_colon = self.token.span;
75+
if self.eat(&token::Colon) {
76+
// <Bar as Baz<T>>:Qux
77+
// ^
78+
let span = lo_colon.to(self.prev_span);
79+
self.diagnostic()
80+
.struct_span_err(span, "found single colon where type path was expected")
81+
.span_suggestion(
82+
span,
83+
"use double colon",
84+
"::".to_string(),
85+
Applicability::MachineApplicable,
86+
)
87+
.emit();
88+
} else {
89+
self.expect(&token::ModSep)?;
90+
}
7591

7692
let qself = QSelf { ty, path_span, position: path.segments.len() };
7793
self.parse_path_segments(&mut path.segments, style)?;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run-rustfix
2+
trait T {
3+
type Ty;
4+
}
5+
6+
struct Impl;
7+
8+
impl T for Impl {
9+
type Ty = u32;
10+
}
11+
12+
fn template<T>() -> i64 {
13+
3
14+
}
15+
16+
fn main() {
17+
template::<<Impl as T>::Ty>();
18+
//~^ ERROR found single colon where type path was expected
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// run-rustfix
2+
trait T {
3+
type Ty;
4+
}
5+
6+
struct Impl;
7+
8+
impl T for Impl {
9+
type Ty = u32;
10+
}
11+
12+
fn template<T>() -> i64 {
13+
3
14+
}
15+
16+
fn main() {
17+
template::<<Impl as T>:Ty>();
18+
//~^ ERROR found single colon where type path was expected
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: found single colon where type path was expected
2+
--> $DIR/qualified-path-in-turbofish.rs:17:27
3+
|
4+
LL | template::<<Impl as T>:Ty>();
5+
| ^ help: use double colon: `::`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)