Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
b7ed8601
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b7ed8601
编写于
6月 07, 2022
作者:
M
Michael Goulet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
recover `import` instead of `use` in item
上级
9f7e997c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
93 addition
and
20 deletion
+93
-20
compiler/rustc_parse/src/parser/item.rs
compiler/rustc_parse/src/parser/item.rs
+49
-20
src/test/ui/did_you_mean/use_instead_of_import.fixed
src/test/ui/did_you_mean/use_instead_of_import.fixed
+15
-0
src/test/ui/did_you_mean/use_instead_of_import.rs
src/test/ui/did_you_mean/use_instead_of_import.rs
+15
-0
src/test/ui/did_you_mean/use_instead_of_import.stderr
src/test/ui/did_you_mean/use_instead_of_import.stderr
+14
-0
未找到文件。
compiler/rustc_parse/src/parser/item.rs
浏览文件 @
b7ed8601
...
@@ -204,25 +204,7 @@ fn parse_item_kind(
...
@@ -204,25 +204,7 @@ fn parse_item_kind(
let
mut
def
=
||
mem
::
replace
(
def
,
Defaultness
::
Final
);
let
mut
def
=
||
mem
::
replace
(
def
,
Defaultness
::
Final
);
let
info
=
if
self
.eat_keyword
(
kw
::
Use
)
{
let
info
=
if
self
.eat_keyword
(
kw
::
Use
)
{
// USE ITEM
self
.parse_use_item
()
?
let
tree
=
self
.parse_use_tree
()
?
;
// If wildcard or glob-like brace syntax doesn't have `;`,
// the user may not know `*` or `{}` should be the last.
if
let
Err
(
mut
e
)
=
self
.expect_semi
()
{
match
tree
.kind
{
UseTreeKind
::
Glob
=>
{
e
.note
(
"the wildcard token must be last on the path"
);
}
UseTreeKind
::
Nested
(
..
)
=>
{
e
.note
(
"glob-like brace syntax must be last on the path"
);
}
_
=>
(),
}
return
Err
(
e
);
}
(
Ident
::
empty
(),
ItemKind
::
Use
(
tree
))
}
else
if
self
.check_fn_front_matter
(
def_final
)
{
}
else
if
self
.check_fn_front_matter
(
def_final
)
{
// FUNCTION ITEM
// FUNCTION ITEM
let
(
ident
,
sig
,
generics
,
body
)
=
self
.parse_fn
(
attrs
,
fn_parse_mode
,
lo
,
vis
)
?
;
let
(
ident
,
sig
,
generics
,
body
)
=
self
.parse_fn
(
attrs
,
fn_parse_mode
,
lo
,
vis
)
?
;
...
@@ -288,7 +270,12 @@ fn parse_item_kind(
...
@@ -288,7 +270,12 @@ fn parse_item_kind(
}
else
if
let
IsMacroRulesItem
::
Yes
{
has_bang
}
=
self
.is_macro_rules_item
()
{
}
else
if
let
IsMacroRulesItem
::
Yes
{
has_bang
}
=
self
.is_macro_rules_item
()
{
// MACRO_RULES ITEM
// MACRO_RULES ITEM
self
.parse_item_macro_rules
(
vis
,
has_bang
)
?
self
.parse_item_macro_rules
(
vis
,
has_bang
)
?
}
else
if
vis
.kind
.is_pub
()
&&
self
.isnt_macro_invocation
()
{
}
else
if
self
.isnt_macro_invocation
()
&&
(
self
.token
.is_ident_named
(
Symbol
::
intern
(
"import"
))
||
self
.token
.is_ident_named
(
Symbol
::
intern
(
"using"
)))
{
return
self
.recover_import_as_use
();
}
else
if
self
.isnt_macro_invocation
()
&&
vis
.kind
.is_pub
()
{
self
.recover_missing_kw_before_item
()
?
;
self
.recover_missing_kw_before_item
()
?
;
return
Ok
(
None
);
return
Ok
(
None
);
}
else
if
macros_allowed
&&
self
.check_path
()
{
}
else
if
macros_allowed
&&
self
.check_path
()
{
...
@@ -300,6 +287,48 @@ fn parse_item_kind(
...
@@ -300,6 +287,48 @@ fn parse_item_kind(
Ok
(
Some
(
info
))
Ok
(
Some
(
info
))
}
}
fn
recover_import_as_use
(
&
mut
self
)
->
PResult
<
'a
,
Option
<
(
Ident
,
ItemKind
)
>>
{
let
span
=
self
.token.span
;
let
token_name
=
super
::
token_descr
(
&
self
.token
);
let
snapshot
=
self
.create_snapshot_for_diagnostic
();
self
.bump
();
match
self
.parse_use_item
()
{
Ok
(
u
)
=>
{
self
.struct_span_err
(
span
,
format!
(
"expected item, found {token_name}"
))
.span_suggestion_short
(
span
,
"items are imported using the `use` keyword"
,
"use"
.to_owned
(),
Applicability
::
MachineApplicable
,
)
.emit
();
Ok
(
Some
(
u
))
}
Err
(
e
)
=>
{
e
.cancel
();
self
.restore_snapshot
(
snapshot
);
Ok
(
None
)
}
}
}
fn
parse_use_item
(
&
mut
self
)
->
PResult
<
'a
,
(
Ident
,
ItemKind
)
>
{
let
tree
=
self
.parse_use_tree
()
?
;
if
let
Err
(
mut
e
)
=
self
.expect_semi
()
{
match
tree
.kind
{
UseTreeKind
::
Glob
=>
{
e
.note
(
"the wildcard token must be last on the path"
);
}
UseTreeKind
::
Nested
(
..
)
=>
{
e
.note
(
"glob-like brace syntax must be last on the path"
);
}
_
=>
(),
}
return
Err
(
e
);
}
Ok
((
Ident
::
empty
(),
ItemKind
::
Use
(
tree
)))
}
/// When parsing a statement, would the start of a path be an item?
/// When parsing a statement, would the start of a path be an item?
pub
(
super
)
fn
is_path_start_item
(
&
mut
self
)
->
bool
{
pub
(
super
)
fn
is_path_start_item
(
&
mut
self
)
->
bool
{
self
.is_kw_followed_by_ident
(
kw
::
Union
)
// no: `union::b`, yes: `union U { .. }`
self
.is_kw_followed_by_ident
(
kw
::
Union
)
// no: `union::b`, yes: `union U { .. }`
...
...
src/test/ui/did_you_mean/use_instead_of_import.fixed
0 → 100644
浏览文件 @
b7ed8601
// run-rustfix
use std::{
//~^ ERROR expected item, found `import`
io::Write,
rc::Rc,
};
pub use std::io;
//~^ ERROR expected item, found `using`
fn main() {
let x = Rc::new(1);
let _ = write!(io::stdout(), "{:?}", x);
}
src/test/ui/did_you_mean/use_instead_of_import.rs
0 → 100644
浏览文件 @
b7ed8601
// run-rustfix
import
std
::{
//~^ ERROR expected item, found `import`
io
::
Write
,
rc
::
Rc
,
};
pub
using
std
::
io
;
//~^ ERROR expected item, found `using`
fn
main
()
{
let
x
=
Rc
::
new
(
1
);
let
_
=
write!
(
io
::
stdout
(),
"{:?}"
,
x
);
}
src/test/ui/did_you_mean/use_instead_of_import.stderr
0 → 100644
浏览文件 @
b7ed8601
error: expected item, found `import`
--> $DIR/use_instead_of_import.rs:3:1
|
LL | import std::{
| ^^^^^^ help: items are imported using the `use` keyword
error: expected item, found `using`
--> $DIR/use_instead_of_import.rs:9:5
|
LL | pub using std::io;
| ^^^^^ help: items are imported using the `use` keyword
error: aborting due to 2 previous errors
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录