about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBruno A. Muciño <mucinoab@gmail.com>2022-11-08 20:13:10 -0600
committerBruno A. Muciño <mucinoab@gmail.com>2022-11-08 20:13:10 -0600
commitaa5a3266f408bc82dc0c1e8e9e30841c208e8228 (patch)
tree7cf5045a7dd556365d1045699cc76e3de39614ac
parent9be2f35a4c1ed1b04aa4a6945b64763f599259ff (diff)
downloadrust-aa5a3266f408bc82dc0c1e8e9e30841c208e8228.tar.gz
rust-aa5a3266f408bc82dc0c1e8e9e30841c208e8228.zip
Parser: Recover from using colon as path separator in imports
-rw-r--r--compiler/rustc_parse/src/parser/item.rs17
-rw-r--r--src/test/ui/parser/use-colon-as-mod-sep.rs11
-rw-r--r--src/test/ui/parser/use-colon-as-mod-sep.stderr28
3 files changed, 56 insertions, 0 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index bda301c52e9..d657a289117 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -971,6 +971,23 @@ impl<'a> Parser<'a> {
             if self.eat(&token::ModSep) {
                 self.parse_use_tree_glob_or_nested()?
             } else {
+                // Recover from using a colon as path separator.
+                while self.eat_noexpect(&token::Colon) {
+                    self.struct_span_err(self.prev_token.span, "expected `::`, found `:`")
+                        .span_suggestion_short(
+                            self.prev_token.span,
+                            "use double colon",
+                            "::",
+                            Applicability::MachineApplicable,
+                        )
+                        .note_once("import paths are delimited using `::`")
+                        .emit();
+
+                    // We parse the rest of the path and append it to the original prefix.
+                    self.parse_path_segments(&mut prefix.segments, PathStyle::Mod, None)?;
+                    prefix.span = lo.to(self.prev_token.span);
+                }
+
                 UseTreeKind::Simple(self.parse_rename()?, DUMMY_NODE_ID, DUMMY_NODE_ID)
             }
         };
diff --git a/src/test/ui/parser/use-colon-as-mod-sep.rs b/src/test/ui/parser/use-colon-as-mod-sep.rs
new file mode 100644
index 00000000000..e1e8756b03c
--- /dev/null
+++ b/src/test/ui/parser/use-colon-as-mod-sep.rs
@@ -0,0 +1,11 @@
+// Recover from using a colon as a path separator.
+
+use std::process:Command;
+//~^ ERROR expected `::`, found `:`
+use std:fs::File;
+//~^ ERROR expected `::`, found `:`
+use std:collections:HashMap;
+//~^ ERROR expected `::`, found `:`
+//~| ERROR expected `::`, found `:`
+
+fn main() { }
diff --git a/src/test/ui/parser/use-colon-as-mod-sep.stderr b/src/test/ui/parser/use-colon-as-mod-sep.stderr
new file mode 100644
index 00000000000..e825dfed111
--- /dev/null
+++ b/src/test/ui/parser/use-colon-as-mod-sep.stderr
@@ -0,0 +1,28 @@
+error: expected `::`, found `:`
+  --> $DIR/use-colon-as-mod-sep.rs:3:17
+   |
+LL | use std::process:Command;
+   |                 ^ help: use double colon
+   |
+   = note: import paths are delimited using `::`
+
+error: expected `::`, found `:`
+  --> $DIR/use-colon-as-mod-sep.rs:5:8
+   |
+LL | use std:fs::File;
+   |        ^ help: use double colon
+
+error: expected `::`, found `:`
+  --> $DIR/use-colon-as-mod-sep.rs:7:8
+   |
+LL | use std:collections:HashMap;
+   |        ^ help: use double colon
+
+error: expected `::`, found `:`
+  --> $DIR/use-colon-as-mod-sep.rs:7:20
+   |
+LL | use std:collections:HashMap;
+   |                    ^ help: use double colon
+
+error: aborting due to 4 previous errors
+