Skip to content

Commit 08046db

Browse files
authored
Rollup merge of rust-lang#103443 - mucinoab:recover-colon-as-path-separetor, r=compiler-errors
Parser: Recover from using colon as path separator in imports I don't know if this is the right approach, any feedback is welcome. r? `@compiler-errors` Fixes rust-lang#103269
2 parents 4205ebd + aa5a326 commit 08046db

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

compiler/rustc_parse/src/parser/item.rs

+17
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,23 @@ impl<'a> Parser<'a> {
971971
if self.eat(&token::ModSep) {
972972
self.parse_use_tree_glob_or_nested()?
973973
} else {
974+
// Recover from using a colon as path separator.
975+
while self.eat_noexpect(&token::Colon) {
976+
self.struct_span_err(self.prev_token.span, "expected `::`, found `:`")
977+
.span_suggestion_short(
978+
self.prev_token.span,
979+
"use double colon",
980+
"::",
981+
Applicability::MachineApplicable,
982+
)
983+
.note_once("import paths are delimited using `::`")
984+
.emit();
985+
986+
// We parse the rest of the path and append it to the original prefix.
987+
self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None)?;
988+
prefix.span = lo.to(self.prev_token.span);
989+
}
990+
974991
UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
975992
}
976993
};
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Recover from using a colon as a path separator.
2+
3+
use std::process:Command;
4+
//~^ ERROR expected `::`, found `:`
5+
use std:fs::File;
6+
//~^ ERROR expected `::`, found `:`
7+
use std:collections:HashMap;
8+
//~^ ERROR expected `::`, found `:`
9+
//~| ERROR expected `::`, found `:`
10+
11+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: expected `::`, found `:`
2+
--> $DIR/use-colon-as-mod-sep.rs:3:17
3+
|
4+
LL | use std::process:Command;
5+
| ^ help: use double colon
6+
|
7+
= note: import paths are delimited using `::`
8+
9+
error: expected `::`, found `:`
10+
--> $DIR/use-colon-as-mod-sep.rs:5:8
11+
|
12+
LL | use std:fs::File;
13+
| ^ help: use double colon
14+
15+
error: expected `::`, found `:`
16+
--> $DIR/use-colon-as-mod-sep.rs:7:8
17+
|
18+
LL | use std:collections:HashMap;
19+
| ^ help: use double colon
20+
21+
error: expected `::`, found `:`
22+
--> $DIR/use-colon-as-mod-sep.rs:7:20
23+
|
24+
LL | use std:collections:HashMap;
25+
| ^ help: use double colon
26+
27+
error: aborting due to 4 previous errors
28+

0 commit comments

Comments
 (0)