Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
41cb5e94
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,发现更多精彩内容 >>
未验证
提交
41cb5e94
编写于
6月 23, 2022
作者:
M
Michael Goulet
提交者:
GitHub
6月 23, 2022
浏览文件
操作
浏览文件
下载
差异文件
Rollup merge of #98283 - TaKO8Ki:point-at-private-fields-in-struct-literal, r=compiler-errors
Point at private fields in struct literal closes #95872
上级
3b68700d
eb86daa1
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
107 addition
and
19 deletion
+107
-19
compiler/rustc_lint_defs/src/lib.rs
compiler/rustc_lint_defs/src/lib.rs
+3
-0
compiler/rustc_typeck/src/check/expr.rs
compiler/rustc_typeck/src/check/expr.rs
+59
-13
src/test/ui/issues/issue-76077.rs
src/test/ui/issues/issue-76077.rs
+1
-1
src/test/ui/issues/issue-76077.stderr
src/test/ui/issues/issue-76077.stderr
+3
-1
src/test/ui/privacy/issue-79593.rs
src/test/ui/privacy/issue-79593.rs
+1
-1
src/test/ui/privacy/issue-79593.stderr
src/test/ui/privacy/issue-79593.stderr
+3
-1
src/test/ui/typeck/issue-87872-missing-inaccessible-field-literal.rs
.../typeck/issue-87872-missing-inaccessible-field-literal.rs
+1
-1
src/test/ui/typeck/issue-87872-missing-inaccessible-field-literal.stderr
...eck/issue-87872-missing-inaccessible-field-literal.stderr
+3
-1
src/test/ui/typeck/missing-private-fields-in-struct-literal.rs
...est/ui/typeck/missing-private-fields-in-struct-literal.rs
+18
-0
src/test/ui/typeck/missing-private-fields-in-struct-literal.stderr
...ui/typeck/missing-private-fields-in-struct-literal.stderr
+15
-0
未找到文件。
compiler/rustc_lint_defs/src/lib.rs
浏览文件 @
41cb5e94
...
...
@@ -26,6 +26,9 @@
(
"is"
,
$x:expr
)
=>
{
if
$x
==
1
{
"is"
}
else
{
"are"
}
};
(
"was"
,
$x:expr
)
=>
{
if
$x
==
1
{
"was"
}
else
{
"were"
}
};
(
"this"
,
$x:expr
)
=>
{
if
$x
==
1
{
"this"
}
else
{
"these"
}
};
...
...
compiler/rustc_typeck/src/check/expr.rs
浏览文件 @
41cb5e94
...
...
@@ -26,10 +26,10 @@
use
rustc_ast
as
ast
;
use
rustc_data_structures
::
fx
::
FxHashMap
;
use
rustc_data_structures
::
stack
::
ensure_sufficient_stack
;
use
rustc_errors
::
Diagnostic
;
use
rustc_errors
::
EmissionGuarantee
;
use
rustc_errors
::
ErrorGuaranteed
;
use
rustc_errors
::{
pluralize
,
struct_span_err
,
Applicability
,
DiagnosticBuilder
,
DiagnosticId
};
use
rustc_errors
::
{
pluralize
,
struct_span_err
,
Applicability
,
Diagnostic
,
DiagnosticBuilder
,
DiagnosticId
,
EmissionGuarantee
,
ErrorGuaranteed
,
};
use
rustc_hir
as
hir
;
use
rustc_hir
::
def
::{
CtorKind
,
DefKind
,
Res
};
use
rustc_hir
::
def_id
::
DefId
;
...
...
@@ -1701,12 +1701,17 @@ fn check_expr_struct_fields(
};
self
.typeck_results
.borrow_mut
()
.fru_field_types_mut
()
.insert
(
expr_id
,
fru_tys
);
}
else
if
adt_kind
!=
AdtKind
::
Union
&&
!
remaining_fields
.is_empty
()
{
let
inaccessible_remaining_fields
=
remaining_fields
.iter
()
.any
(|(
_
,
(
_
,
field
))|
{
!
field
.vis
.is_accessible_from
(
tcx
.parent_module
(
expr_id
)
.to_def_id
(),
tcx
)
});
debug!
(
?
remaining_fields
);
let
private_fields
:
Vec
<&
ty
::
FieldDef
>
=
variant
.fields
.iter
()
.filter
(|
field
|
{
!
field
.vis
.is_accessible_from
(
tcx
.parent_module
(
expr_id
)
.to_def_id
(),
tcx
)
})
.collect
();
if
inaccessible_remaining_fields
{
self
.report_
inaccessible_fields
(
adt_ty
,
span
);
if
!
private_fields
.is_empty
()
{
self
.report_
private_fields
(
adt_ty
,
span
,
private_fields
,
ast_fields
);
}
else
{
self
.report_missing_fields
(
adt_ty
,
...
...
@@ -1830,7 +1835,7 @@ fn report_missing_fields(
/// Report an error for a struct field expression when there are invisible fields.
///
/// ```text
/// error: cannot construct `Foo` with struct literal syntax due to
inaccessibl
e fields
/// error: cannot construct `Foo` with struct literal syntax due to
privat
e fields
/// --> src/main.rs:8:5
/// |
/// 8 | foo::Foo {};
...
...
@@ -1838,13 +1843,54 @@ fn report_missing_fields(
///
/// error: aborting due to previous error
/// ```
fn
report_inaccessible_fields
(
&
self
,
adt_ty
:
Ty
<
'tcx
>
,
span
:
Span
)
{
self
.tcx.sess
.span_err
(
fn
report_private_fields
(
&
self
,
adt_ty
:
Ty
<
'tcx
>
,
span
:
Span
,
private_fields
:
Vec
<&
ty
::
FieldDef
>
,
used_fields
:
&
'tcx
[
hir
::
ExprField
<
'tcx
>
],
)
{
let
mut
err
=
self
.tcx.sess
.struct_span_err
(
span
,
&
format!
(
"cannot construct `{adt_ty}` with struct literal syntax due to
inaccessibl
e fields"
,
"cannot construct `{adt_ty}` with struct literal syntax due to
privat
e fields"
,
),
);
let
(
used_private_fields
,
remaining_private_fields
):
(
Vec
<
(
Symbol
,
Span
,
bool
)
>
,
Vec
<
(
Symbol
,
Span
,
bool
)
>
,
)
=
private_fields
.iter
()
.map
(|
field
|
{
match
used_fields
.iter
()
.find
(|
used_field
|
field
.name
==
used_field
.ident.name
)
{
Some
(
used_field
)
=>
(
field
.name
,
used_field
.span
,
true
),
None
=>
(
field
.name
,
self
.tcx
.def_span
(
field
.did
),
false
),
}
})
.partition
(|
field
|
field
.2
);
err
.span_labels
(
used_private_fields
.iter
()
.map
(|(
_
,
span
,
_
)|
*
span
),
"private field"
);
if
!
remaining_private_fields
.is_empty
()
{
let
remaining_private_fields_len
=
remaining_private_fields
.len
();
let
names
=
match
&
remaining_private_fields
.iter
()
.map
(|(
name
,
_
,
_
)|
name
.to_string
())
.collect
::
<
Vec
<
_
>>
()[
..
]
{
_
if
remaining_private_fields_len
>
6
=>
String
::
new
(),
[
name
]
=>
format!
(
"`{name}` "
),
[
names
@
..
,
last
]
=>
{
let
names
=
names
.iter
()
.map
(|
name
|
format!
(
"`{name}`"
))
.collect
::
<
Vec
<
_
>>
();
format!
(
"{} and `{last}` "
,
names
.join
(
", "
))
}
[]
=>
unreachable!
(),
};
err
.note
(
format!
(
"... and other private field{s} {names}that {were} not provided"
,
s
=
pluralize!
(
remaining_private_fields_len
),
were
=
pluralize!
(
"was"
,
remaining_private_fields_len
),
));
}
err
.emit
();
}
fn
report_unknown_field
(
...
...
src/test/ui/issues/issue-76077.rs
浏览文件 @
41cb5e94
...
...
@@ -6,5 +6,5 @@ pub struct Foo {
fn
main
()
{
foo
::
Foo
{};
//~^ ERROR cannot construct `Foo` with struct literal syntax due to
inaccessibl
e fields
//~^ ERROR cannot construct `Foo` with struct literal syntax due to
privat
e fields
}
src/test/ui/issues/issue-76077.stderr
浏览文件 @
41cb5e94
error: cannot construct `Foo` with struct literal syntax due to
inaccessibl
e fields
error: cannot construct `Foo` with struct literal syntax due to
privat
e fields
--> $DIR/issue-76077.rs:8:5
|
LL | foo::Foo {};
| ^^^^^^^^
|
= note: ... and other private field `you_cant_use_this_field` that was not provided
error: aborting due to previous error
src/test/ui/privacy/issue-79593.rs
浏览文件 @
41cb5e94
...
...
@@ -16,7 +16,7 @@ fn correct() {
fn
correct
()
{
foo
::
Pub
{};
//~^ ERROR cannot construct `Pub` with struct literal syntax due to
inaccessibl
e fields
//~^ ERROR cannot construct `Pub` with struct literal syntax due to
privat
e fields
}
fn
wrong
()
{
...
...
src/test/ui/privacy/issue-79593.stderr
浏览文件 @
41cb5e94
...
...
@@ -10,11 +10,13 @@ error[E0063]: missing field `y` in initializer of `Enum`
LL | Enum::Variant { x: () };
| ^^^^^^^^^^^^^ missing `y`
error: cannot construct `Pub` with struct literal syntax due to
inaccessibl
e fields
error: cannot construct `Pub` with struct literal syntax due to
privat
e fields
--> $DIR/issue-79593.rs:18:5
|
LL | foo::Pub {};
| ^^^^^^^^
|
= note: ... and other private field `private` that was not provided
error[E0063]: missing field `y` in initializer of `Enum`
--> $DIR/issue-79593.rs:23:5
...
...
src/test/ui/typeck/issue-87872-missing-inaccessible-field-literal.rs
浏览文件 @
41cb5e94
...
...
@@ -7,5 +7,5 @@ pub struct Foo {
fn
main
()
{
foo
::
Foo
{};
//~^ ERROR cannot construct `Foo` with struct literal syntax due to
inaccessibl
e fields
//~^ ERROR cannot construct `Foo` with struct literal syntax due to
privat
e fields
}
src/test/ui/typeck/issue-87872-missing-inaccessible-field-literal.stderr
浏览文件 @
41cb5e94
error: cannot construct `Foo` with struct literal syntax due to
inaccessibl
e fields
error: cannot construct `Foo` with struct literal syntax due to
privat
e fields
--> $DIR/issue-87872-missing-inaccessible-field-literal.rs:9:5
|
LL | foo::Foo {};
| ^^^^^^^^
|
= note: ... and other private field `you_cant_use_this_field` that was not provided
error: aborting due to previous error
src/test/ui/typeck/missing-private-fields-in-struct-literal.rs
0 → 100644
浏览文件 @
41cb5e94
pub
mod
m
{
pub
struct
S
{
pub
visible
:
bool
,
a
:
(),
b
:
(),
c
:
(),
d
:
(),
e
:
(),
}
}
fn
main
()
{
let
_
=
m
::
S
{
//~ ERROR cannot construct `S` with struct literal syntax due to private fields
visible
:
true
,
a
:
(),
b
:
(),
};
}
src/test/ui/typeck/missing-private-fields-in-struct-literal.stderr
0 → 100644
浏览文件 @
41cb5e94
error: cannot construct `S` with struct literal syntax due to private fields
--> $DIR/missing-private-fields-in-struct-literal.rs:13:13
|
LL | let _ = m::S {
| ^^^^
LL | visible: true,
LL | a: (),
| ----- private field
LL | b: (),
| ----- private field
|
= note: ... and other private fields `c`, `d` and `e` that were not provided
error: aborting due to previous error
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录