Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
681a14f2
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,发现更多精彩内容 >>
提交
681a14f2
编写于
8月 20, 2016
作者:
J
Jeffrey Seyfried
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
item_like_imports: Allow unused ambiguous glob imports.
上级
f582fa32
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
79 addition
and
10 deletion
+79
-10
src/librustc_resolve/lib.rs
src/librustc_resolve/lib.rs
+32
-2
src/librustc_resolve/resolve_imports.rs
src/librustc_resolve/resolve_imports.rs
+23
-8
src/test/compile-fail/imports/duplicate.rs
src/test/compile-fail/imports/duplicate.rs
+14
-0
src/test/run-pass/imports.rs
src/test/run-pass/imports.rs
+10
-0
未找到文件。
src/librustc_resolve/lib.rs
浏览文件 @
681a14f2
...
...
@@ -874,6 +874,10 @@ enum NameBindingKind<'a> {
binding
:
&
'a
NameBinding
<
'a
>
,
directive
:
&
'a
ImportDirective
<
'a
>
,
},
Ambiguity
{
b1
:
&
'a
NameBinding
<
'a
>
,
b2
:
&
'a
NameBinding
<
'a
>
,
}
}
#[derive(Clone,
Debug)]
...
...
@@ -885,6 +889,7 @@ fn module(&self) -> Option<Module<'a>> {
NameBindingKind
::
Module
(
module
)
=>
Some
(
module
),
NameBindingKind
::
Def
(
_
)
=>
None
,
NameBindingKind
::
Import
{
binding
,
..
}
=>
binding
.module
(),
NameBindingKind
::
Ambiguity
{
..
}
=>
None
,
}
}
...
...
@@ -893,6 +898,7 @@ fn def(&self) -> Def {
NameBindingKind
::
Def
(
def
)
=>
def
,
NameBindingKind
::
Module
(
module
)
=>
module
.def
.unwrap
(),
NameBindingKind
::
Import
{
binding
,
..
}
=>
binding
.def
(),
NameBindingKind
::
Ambiguity
{
..
}
=>
Def
::
Err
,
}
}
...
...
@@ -922,6 +928,7 @@ fn is_import(&self) -> bool {
fn
is_glob_import
(
&
self
)
->
bool
{
match
self
.kind
{
NameBindingKind
::
Import
{
directive
,
..
}
=>
directive
.is_glob
(),
NameBindingKind
::
Ambiguity
{
..
}
=>
true
,
_
=>
false
,
}
}
...
...
@@ -932,6 +939,14 @@ fn is_importable(&self) -> bool {
_
=>
true
,
}
}
fn
ambiguity
(
&
self
)
->
Option
<
(
&
'a
NameBinding
<
'a
>
,
&
'a
NameBinding
<
'a
>
)
>
{
match
self
.kind
{
NameBindingKind
::
Ambiguity
{
b1
,
b2
}
=>
Some
((
b1
,
b2
)),
NameBindingKind
::
Import
{
binding
,
..
}
=>
binding
.ambiguity
(),
_
=>
None
,
}
}
}
/// Interns the names of the primitive types.
...
...
@@ -1249,7 +1264,8 @@ fn get_ribs<'b>(&'b mut self, ns: Namespace) -> &'b mut Vec<Rib<'a>> {
match
ns
{
ValueNS
=>
&
mut
self
.value_ribs
,
TypeNS
=>
&
mut
self
.type_ribs
}
}
fn
record_use
(
&
mut
self
,
name
:
Name
,
ns
:
Namespace
,
binding
:
&
'a
NameBinding
<
'a
>
)
{
fn
record_use
(
&
mut
self
,
name
:
Name
,
ns
:
Namespace
,
binding
:
&
'a
NameBinding
<
'a
>
,
span
:
Span
)
->
bool
/* true if an error was reported */
{
// track extern crates for unused_extern_crate lint
if
let
Some
(
DefId
{
krate
,
..
})
=
binding
.module
()
.and_then
(
ModuleS
::
def_id
)
{
self
.used_crates
.insert
(
krate
);
...
...
@@ -1259,6 +1275,19 @@ fn record_use(&mut self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>
self
.used_imports
.insert
((
directive
.id
,
ns
));
self
.add_to_glob_map
(
directive
.id
,
name
);
}
if
let
Some
((
b1
,
b2
))
=
binding
.ambiguity
()
{
let
msg1
=
format!
(
"`{}` could resolve to the name imported here"
,
name
);
let
msg2
=
format!
(
"`{}` could also resolve to the name imported here"
,
name
);
self
.session
.struct_span_err
(
span
,
&
format!
(
"`{}` is ambiguous"
,
name
))
.span_note
(
b1
.span
,
&
msg1
)
.span_note
(
b2
.span
,
&
msg2
)
.note
(
&
format!
(
"Consider adding an explicit import of `{}` to disambiguate"
,
name
))
.emit
();
return
true
;
}
false
}
fn
add_to_glob_map
(
&
mut
self
,
id
:
NodeId
,
name
:
Name
)
{
...
...
@@ -2294,7 +2323,8 @@ fn resolve_pattern(&mut self,
Def
::
Struct
(
..
)
|
Def
::
Variant
(
..
)
|
Def
::
Const
(
..
)
|
Def
::
AssociatedConst
(
..
)
if
!
always_binding
=>
{
// A constant, unit variant, etc pattern.
self
.record_use
(
ident
.node.name
,
ValueNS
,
binding
.unwrap
());
let
name
=
ident
.node.name
;
self
.record_use
(
name
,
ValueNS
,
binding
.unwrap
(),
ident
.span
);
Some
(
PathResolution
::
new
(
def
))
}
Def
::
Struct
(
..
)
|
Def
::
Variant
(
..
)
|
...
...
src/librustc_resolve/resolve_imports.rs
浏览文件 @
681a14f2
...
...
@@ -181,7 +181,9 @@ pub fn resolve_name_in_module(&mut self,
if
is_disallowed_private_import
(
binding
)
{
return
Failed
(
None
);
}
self
.record_use
(
name
,
ns
,
binding
);
if
self
.record_use
(
name
,
ns
,
binding
,
span
)
{
return
Success
(
self
.dummy_binding
);
}
if
!
self
.is_accessible
(
binding
.vis
)
{
self
.privacy_errors
.push
(
PrivacyError
(
span
,
name
,
binding
));
}
...
...
@@ -323,7 +325,18 @@ pub fn try_define<T>(&mut self, module: Module<'a>, name: Name, ns: Namespace, b
if
!
this
.new_import_semantics
||
!
old_binding
.is_glob_import
()
{
resolution
.duplicate_globs
.push
(
binding
);
}
else
if
binding
.def
()
!=
old_binding
.def
()
{
resolution
.duplicate_globs
.push
(
binding
);
resolution
.binding
=
Some
(
this
.arenas
.alloc_name_binding
(
NameBinding
{
kind
:
NameBindingKind
::
Ambiguity
{
b1
:
old_binding
,
b2
:
binding
,
},
vis
:
if
old_binding
.vis
.is_at_least
(
binding
.vis
,
this
)
{
old_binding
.vis
}
else
{
binding
.vis
},
span
:
old_binding
.span
,
}));
}
else
if
!
old_binding
.vis
.is_at_least
(
binding
.vis
,
this
)
{
// We are glob-importing the same item but with greater visibility.
resolution
.binding
=
Some
(
binding
);
...
...
@@ -597,7 +610,10 @@ fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> ResolveResu
for
&
(
ns
,
result
)
in
&
[(
ValueNS
,
value_result
),
(
TypeNS
,
type_result
)]
{
if
let
Ok
(
binding
)
=
result
{
self
.record_use
(
name
,
ns
,
binding
);
if
self
.record_use
(
name
,
ns
,
binding
,
directive
.span
)
{
self
.resolution
(
module
,
name
,
ns
)
.borrow_mut
()
.binding
=
Some
(
self
.dummy_binding
);
}
}
}
...
...
@@ -759,17 +775,16 @@ fn finalize_resolutions_in(&mut self, module: Module<'b>) {
}
}
self
.report_conflict
(
module
,
name
,
ns
,
duplicate_glob
,
binding
);
}
}
else
if
binding
.is_glob_import
()
{
for
duplicate_glob
in
resolution
.duplicate_globs
.iter
()
{
self
.report_conflict
(
module
,
name
,
ns
,
duplicate_glob
,
binding
);
}
}
if
binding
.vis
==
ty
::
Visibility
::
Public
&&
(
binding
.is_import
()
||
binding
.is_extern_crate
())
{
reexports
.push
(
Export
{
name
:
name
,
def_id
:
binding
.def
()
.def_id
()
});
let
def
=
binding
.def
();
if
def
!=
Def
::
Err
{
reexports
.push
(
Export
{
name
:
name
,
def_id
:
def
.def_id
()
});
}
}
if
let
NameBindingKind
::
Import
{
binding
:
orig_binding
,
directive
,
..
}
=
binding
.kind
{
...
...
src/test/compile-fail/imports/duplicate.rs
浏览文件 @
681a14f2
...
...
@@ -33,6 +33,20 @@ mod e {
pub
use
c
::
*
;
// ok
}
mod
f
{
pub
use
a
::
*
;
//~ NOTE `foo` could resolve to the name imported here
pub
use
b
::
*
;
//~ NOTE `foo` could also resolve to the name imported here
}
mod
g
{
pub
use
a
::
*
;
//~ NOTE `foo` could resolve to the name imported here
pub
use
f
::
*
;
//~ NOTE `foo` could also resolve to the name imported here
}
fn
main
()
{
e
::
foo
();
f
::
foo
();
//~ ERROR `foo` is ambiguous
//~| NOTE Consider adding an explicit import of `foo` to disambiguate
g
::
foo
();
//~ ERROR `foo` is ambiguous
//~| NOTE Consider adding an explicit import of `foo` to disambiguate
}
src/test/run-pass/imports.rs
浏览文件 @
681a14f2
...
...
@@ -63,4 +63,14 @@ fn test() {
}
}
// Unused names can be ambiguous.
mod
d
{
pub
use
foo
::
*
;
// This imports `f` in the value namespace.
pub
use
bar
::
*
;
// This also imports `f` in the value namespace.
}
mod
e
{
pub
use
d
::
*
;
// n.b. Since `e::f` is not used, this is not considered to be a use of `d::f`.
}
fn
main
()
{}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录