From 2b15fc6d9acc50072b1717a7631ddc6e0968970f Mon Sep 17 00:00:00 2001 From: yukang Date: Fri, 5 Aug 2022 19:20:03 +0800 Subject: [PATCH] recover require,include instead of use in item --- compiler/rustc_parse/src/parser/item.rs | 5 ++++- compiler/rustc_span/src/symbol.rs | 1 + .../ui/did_you_mean/use_instead_of_import.fixed | 8 ++++++++ .../ui/did_you_mean/use_instead_of_import.rs | 8 ++++++++ .../ui/did_you_mean/use_instead_of_import.stderr | 16 ++++++++++++++-- 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index 5670729253d..ee26578ac10 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -271,7 +271,10 @@ fn parse_item_kind( // MACRO_RULES ITEM self.parse_item_macro_rules(vis, has_bang)? } else if self.isnt_macro_invocation() - && (self.token.is_ident_named(sym::import) || self.token.is_ident_named(sym::using)) + && (self.token.is_ident_named(sym::import) + || self.token.is_ident_named(sym::using) + || self.token.is_ident_named(sym::include) + || self.token.is_ident_named(sym::require)) { return self.recover_import_as_use(); } else if self.isnt_macro_invocation() && vis.kind.is_pub() { diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 791160ff694..f81a69c1cce 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1170,6 +1170,7 @@ repr_packed, repr_simd, repr_transparent, + require, residual, result, rhs, diff --git a/src/test/ui/did_you_mean/use_instead_of_import.fixed b/src/test/ui/did_you_mean/use_instead_of_import.fixed index 87d453e1565..a8aae76f4fc 100644 --- a/src/test/ui/did_you_mean/use_instead_of_import.fixed +++ b/src/test/ui/did_you_mean/use_instead_of_import.fixed @@ -6,10 +6,18 @@ use std::{ rc::Rc, }; +use std::time::Duration; +//~^ ERROR expected item, found `require` + +use std::time::Instant; +//~^ ERROR expected item, found `include` + pub use std::io; //~^ ERROR expected item, found `using` fn main() { let x = Rc::new(1); let _ = write!(io::stdout(), "{:?}", x); + let _ = Duration::new(5, 0); + let _ = Instant::now(); } diff --git a/src/test/ui/did_you_mean/use_instead_of_import.rs b/src/test/ui/did_you_mean/use_instead_of_import.rs index 59e83732328..2db7c240752 100644 --- a/src/test/ui/did_you_mean/use_instead_of_import.rs +++ b/src/test/ui/did_you_mean/use_instead_of_import.rs @@ -6,10 +6,18 @@ rc::Rc, }; +require std::time::Duration; +//~^ ERROR expected item, found `require` + +include std::time::Instant; +//~^ ERROR expected item, found `include` + pub using std::io; //~^ ERROR expected item, found `using` fn main() { let x = Rc::new(1); let _ = write!(io::stdout(), "{:?}", x); + let _ = Duration::new(5, 0); + let _ = Instant::now(); } diff --git a/src/test/ui/did_you_mean/use_instead_of_import.stderr b/src/test/ui/did_you_mean/use_instead_of_import.stderr index b22954af80f..2aac8f68c5e 100644 --- a/src/test/ui/did_you_mean/use_instead_of_import.stderr +++ b/src/test/ui/did_you_mean/use_instead_of_import.stderr @@ -4,11 +4,23 @@ error: expected item, found `import` LL | import std::{ | ^^^^^^ help: items are imported using the `use` keyword +error: expected item, found `require` + --> $DIR/use_instead_of_import.rs:9:1 + | +LL | require std::time::Duration; + | ^^^^^^^ help: items are imported using the `use` keyword + +error: expected item, found `include` + --> $DIR/use_instead_of_import.rs:12:1 + | +LL | include std::time::Instant; + | ^^^^^^^ help: items are imported using the `use` keyword + error: expected item, found `using` - --> $DIR/use_instead_of_import.rs:9:5 + --> $DIR/use_instead_of_import.rs:15:5 | LL | pub using std::io; | ^^^^^ help: items are imported using the `use` keyword -error: aborting due to 2 previous errors +error: aborting due to 4 previous errors