Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
d38a8ad4
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,发现更多精彩内容 >>
提交
d38a8ad4
编写于
1月 29, 2017
作者:
V
Vadim Petrochenkov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve diagnostics for inaccessible constructors
上级
c9788fdd
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
103 addition
and
12 deletion
+103
-12
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/build_reduced_graph.rs
+11
-5
src/librustc_resolve/lib.rs
src/librustc_resolve/lib.rs
+14
-4
src/test/compile-fail/issue-38412.rs
src/test/compile-fail/issue-38412.rs
+1
-0
src/test/ui/resolve/auxiliary/privacy-struct-ctor.rs
src/test/ui/resolve/auxiliary/privacy-struct-ctor.rs
+0
-0
src/test/ui/resolve/privacy-struct-ctor.rs
src/test/ui/resolve/privacy-struct-ctor.rs
+9
-3
src/test/ui/resolve/privacy-struct-ctor.stderr
src/test/ui/resolve/privacy-struct-ctor.stderr
+68
-0
未找到文件。
src/librustc_resolve/build_reduced_graph.rs
浏览文件 @
d38a8ad4
...
...
@@ -345,9 +345,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
let
ctor_def
=
Def
::
StructCtor
(
self
.definitions
.local_def_id
(
struct_def
.id
()),
CtorKind
::
from_ast
(
struct_def
));
self
.define
(
parent
,
ident
,
ValueNS
,
(
ctor_def
,
ctor_vis
,
sp
,
expansion
));
if
!
ctor_vis
.is_at_least
(
vis
,
&*
self
)
{
self
.legacy_ctor_visibilities
.insert
(
def
.def_id
(),
(
ctor_def
,
ctor_vis
));
}
self
.struct_constructors
.insert
(
def
.def_id
(),
(
ctor_def
,
ctor_vis
));
}
}
...
...
@@ -441,9 +439,17 @@ fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'a>, chi
Def
::
Variant
(
..
)
|
Def
::
TyAlias
(
..
)
=>
{
self
.define
(
parent
,
ident
,
TypeNS
,
(
def
,
vis
,
DUMMY_SP
,
Mark
::
root
()));
}
Def
::
Fn
(
..
)
|
Def
::
Static
(
..
)
|
Def
::
Const
(
..
)
|
Def
::
VariantCtor
(
..
)
|
Def
::
StructCtor
(
..
)
=>
{
Def
::
Fn
(
..
)
|
Def
::
Static
(
..
)
|
Def
::
Const
(
..
)
|
Def
::
VariantCtor
(
..
)
=>
{
self
.define
(
parent
,
ident
,
ValueNS
,
(
def
,
vis
,
DUMMY_SP
,
Mark
::
root
()));
}
Def
::
StructCtor
(
..
)
=>
{
self
.define
(
parent
,
ident
,
ValueNS
,
(
def
,
vis
,
DUMMY_SP
,
Mark
::
root
()));
if
let
Some
(
struct_def_id
)
=
self
.session.cstore
.def_key
(
def_id
)
.parent
.map
(|
index
|
DefId
{
krate
:
def_id
.krate
,
index
:
index
})
{
self
.struct_constructors
.insert
(
struct_def_id
,
(
def
,
vis
));
}
}
Def
::
Trait
(
..
)
=>
{
let
module_kind
=
ModuleKind
::
Def
(
def
,
ident
.name
);
...
...
src/librustc_resolve/lib.rs
浏览文件 @
d38a8ad4
...
...
@@ -1132,8 +1132,9 @@ pub struct Resolver<'a> {
potentially_unused_imports
:
Vec
<&
'a
ImportDirective
<
'a
>>
,
// Auxiliary map used only for reporting `legacy_constructor_visibility` lint.
legacy_ctor_visibilities
:
DefIdMap
<
(
Def
,
ty
::
Visibility
)
>
,
// This table maps struct IDs into struct constructor IDs,
// it's not used during normal resolution, only for better error reporting.
struct_constructors
:
DefIdMap
<
(
Def
,
ty
::
Visibility
)
>
,
}
pub
struct
ResolverArenas
<
'a
>
{
...
...
@@ -1313,7 +1314,7 @@ pub fn new(session: &'a Session,
proc_macro_enabled
:
features
.proc_macro
,
warned_proc_macros
:
FxHashSet
(),
potentially_unused_imports
:
Vec
::
new
(),
legacy_ctor_visibilitie
s
:
DefIdMap
(),
struct_constructor
s
:
DefIdMap
(),
}
}
...
...
@@ -2209,6 +2210,15 @@ fn smart_resolve_path_fragment(&mut self,
_
=>
{}
},
_
if
ns
==
ValueNS
&&
is_struct_like
(
def
)
=>
{
if
let
Def
::
Struct
(
def_id
)
=
def
{
if
let
Some
((
ctor_def
,
ctor_vis
))
=
this
.struct_constructors
.get
(
&
def_id
)
.cloned
()
{
if
is_expected
(
ctor_def
)
&&
!
this
.is_accessible
(
ctor_vis
)
{
err
.span_label
(
span
,
&
format!
(
"constructor is not visible
\
here due to private fields"
));
}
}
}
err
.span_label
(
span
,
&
format!
(
"did you mean `{} {{ /* fields */ }}`?"
,
path_str
));
return
err
;
...
...
@@ -2244,7 +2254,7 @@ fn smart_resolve_path_fragment(&mut self,
let
mut
res
=
None
;
if
let
Def
::
Struct
(
def_id
)
=
resolution
.base_def
{
if
let
Some
((
ctor_def
,
ctor_vis
))
=
self
.
legacy_ctor_visibilitie
s
.get
(
&
def_id
)
.cloned
()
{
=
self
.
struct_constructor
s
.get
(
&
def_id
)
.cloned
()
{
if
is_expected
(
ctor_def
)
&&
self
.is_accessible
(
ctor_vis
)
{
let
lint
=
lint
::
builtin
::
LEGACY_CONSTRUCTOR_VISIBILITY
;
self
.session
.add_lint
(
lint
,
id
,
span
,
...
...
src/test/compile-fail/issue-38412.rs
浏览文件 @
d38a8ad4
...
...
@@ -11,6 +11,7 @@
fn
main
()
{
let
Box
(
a
)
=
loop
{
};
//~^ ERROR expected tuple struct/variant, found struct `Box`
//~| ERROR expected tuple struct/variant, found struct `Box`
// (The below is a trick to allow compiler to infer a type for
// variable `a` without attempting to ascribe a type to the
...
...
src/test/
compile-fail
/auxiliary/privacy-struct-ctor.rs
→
src/test/
ui/resolve
/auxiliary/privacy-struct-ctor.rs
浏览文件 @
d38a8ad4
文件已移动
src/test/
compile-fail
/privacy-struct-ctor.rs
→
src/test/
ui/resolve
/privacy-struct-ctor.rs
浏览文件 @
d38a8ad4
...
...
@@ -25,7 +25,9 @@ pub mod n {
fn
f
()
{
n
::
Z
;
//~ ERROR tuple struct `Z` is private
Z
;
//~ ERROR expected value, found struct `Z`
Z
;
//~^ ERROR expected value, found struct `Z`
//~| NOTE tuple struct constructors with private fields are invisible outside of their mod
}
}
...
...
@@ -33,10 +35,14 @@ fn f() {
fn
main
()
{
m
::
S
;
//~ ERROR tuple struct `S` is private
S
;
//~ ERROR expected value, found struct `S`
S
;
//~^ ERROR expected value, found struct `S`
//~| NOTE constructor is not visible here due to private fields
m
::
n
::
Z
;
//~ ERROR tuple struct `Z` is private
xcrate
::
m
::
S
;
//~ ERROR tuple struct `S` is private
xcrate
::
S
;
//~ ERROR expected value, found struct `xcrate::S`
xcrate
::
S
;
//~^ ERROR expected value, found struct `xcrate::S`
//~| NOTE constructor is not visible here due to private fields
xcrate
::
m
::
n
::
Z
;
//~ ERROR tuple struct `Z` is private
}
src/test/ui/resolve/privacy-struct-ctor.stderr
0 → 100644
浏览文件 @
d38a8ad4
error[E0423]: expected value, found struct `Z`
--> $DIR/privacy-struct-ctor.rs:28:9
|
28 | Z;
| ^
| |
| did you mean `Z { /* fields */ }`?
| constructor is not visible here due to private fields
|
= help: possible better candidate is found in another module, you can import it into scope:
`use m::n::Z;`
error[E0423]: expected value, found struct `S`
--> $DIR/privacy-struct-ctor.rs:38:5
|
38 | S;
| ^
| |
| did you mean `S { /* fields */ }`?
| constructor is not visible here due to private fields
|
= help: possible better candidate is found in another module, you can import it into scope:
`use m::S;`
error[E0423]: expected value, found struct `xcrate::S`
--> $DIR/privacy-struct-ctor.rs:44:5
|
44 | xcrate::S;
| ^^^^^^^^^
| |
| did you mean `xcrate::S { /* fields */ }`?
| constructor is not visible here due to private fields
|
= help: possible better candidate is found in another module, you can import it into scope:
`use m::S;`
error: tuple struct `Z` is private
--> $DIR/privacy-struct-ctor.rs:27:9
|
27 | n::Z; //~ ERROR tuple struct `Z` is private
| ^^^^
error: tuple struct `S` is private
--> $DIR/privacy-struct-ctor.rs:37:5
|
37 | m::S; //~ ERROR tuple struct `S` is private
| ^^^^
error: tuple struct `Z` is private
--> $DIR/privacy-struct-ctor.rs:41:5
|
41 | m::n::Z; //~ ERROR tuple struct `Z` is private
| ^^^^^^^
error: tuple struct `S` is private
--> $DIR/privacy-struct-ctor.rs:43:5
|
43 | xcrate::m::S; //~ ERROR tuple struct `S` is private
| ^^^^^^^^^^^^
error: tuple struct `Z` is private
--> $DIR/privacy-struct-ctor.rs:47:5
|
47 | xcrate::m::n::Z; //~ ERROR tuple struct `Z` is private
| ^^^^^^^^^^^^^^^
error: aborting due to 8 previous errors
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录