Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
0c2f1799
R
Rust
项目概览
int
/
Rust
12 个月 前同步成功
通知
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,发现更多精彩内容 >>
未验证
提交
0c2f1799
编写于
8月 09, 2023
作者:
M
Matthias Krüger
提交者:
GitHub
8月 09, 2023
浏览文件
操作
浏览文件
下载
差异文件
Rollup merge of #114548 - fee1-dead-contrib:migrate-to-trans, r=davidtwco
Migrate a trait selection error to use diagnostic translation
上级
3feab000
1ca4bc96
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
79 addition
and
41 deletion
+79
-41
compiler/rustc_middle/src/ty/closure.rs
compiler/rustc_middle/src/ty/closure.rs
+17
-2
compiler/rustc_middle/src/ty/print/pretty.rs
compiler/rustc_middle/src/ty/print/pretty.rs
+1
-5
compiler/rustc_trait_selection/messages.ftl
compiler/rustc_trait_selection/messages.ftl
+9
-0
compiler/rustc_trait_selection/src/errors.rs
compiler/rustc_trait_selection/src/errors.rs
+35
-1
compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
...r/rustc_trait_selection/src/traits/error_reporting/mod.rs
+17
-33
未找到文件。
compiler/rustc_middle/src/ty/closure.rs
浏览文件 @
0c2f1799
...
...
@@ -7,6 +7,7 @@
use
crate
::
query
::
Providers
;
use
rustc_data_structures
::
fx
::
FxIndexMap
;
use
rustc_errors
::{
DiagnosticArgValue
,
IntoDiagnosticArg
};
use
rustc_hir
::
def_id
::{
DefId
,
LocalDefId
};
use
rustc_hir
::{
self
as
hir
,
LangItem
};
use
rustc_span
::
def_id
::
LocalDefIdMap
;
...
...
@@ -89,10 +90,18 @@ pub enum ClosureKind {
FnOnce
,
}
impl
<
'tcx
>
ClosureKind
{
impl
ClosureKind
{
/// This is the initial value used when doing upvar inference.
pub
const
LATTICE_BOTTOM
:
ClosureKind
=
ClosureKind
::
Fn
;
pub
const
fn
as_str
(
self
)
->
&
'static
str
{
match
self
{
ClosureKind
::
Fn
=>
"Fn"
,
ClosureKind
::
FnMut
=>
"FnMut"
,
ClosureKind
::
FnOnce
=>
"FnOnce"
,
}
}
/// Returns `true` if a type that impls this closure kind
/// must also implement `other`.
pub
fn
extends
(
self
,
other
:
ty
::
ClosureKind
)
->
bool
{
...
...
@@ -115,7 +124,7 @@ pub fn to_def_id(&self, tcx: TyCtxt<'_>) -> DefId {
/// Returns the representative scalar type for this closure kind.
/// See `Ty::to_opt_closure_kind` for more details.
pub
fn
to_ty
(
self
,
tcx
:
TyCtxt
<
'tcx
>
)
->
Ty
<
'tcx
>
{
pub
fn
to_ty
<
'tcx
>
(
self
,
tcx
:
TyCtxt
<
'tcx
>
)
->
Ty
<
'tcx
>
{
match
self
{
ClosureKind
::
Fn
=>
tcx
.types.i8
,
ClosureKind
::
FnMut
=>
tcx
.types.i16
,
...
...
@@ -124,6 +133,12 @@ pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
}
}
impl
IntoDiagnosticArg
for
ClosureKind
{
fn
into_diagnostic_arg
(
self
)
->
DiagnosticArgValue
<
'static
>
{
DiagnosticArgValue
::
Str
(
self
.as_str
()
.into
())
}
}
/// A composite describing a `Place` that is captured by a closure.
#[derive(PartialEq,
Clone,
Debug,
TyEncodable,
TyDecodable,
HashStable)]
#[derive(TypeFoldable,
TypeVisitable)]
...
...
compiler/rustc_middle/src/ty/print/pretty.rs
浏览文件 @
0c2f1799
...
...
@@ -2875,11 +2875,7 @@ pub struct PrintClosureAsImpl<'tcx> {
}
ty
::
ClosureKind
{
match
*
self
{
ty
::
ClosureKind
::
Fn
=>
p!
(
"Fn"
),
ty
::
ClosureKind
::
FnMut
=>
p!
(
"FnMut"
),
ty
::
ClosureKind
::
FnOnce
=>
p!
(
"FnOnce"
),
}
p!
(
write
(
"{}"
,
self
.as_str
()))
}
ty
::
Predicate
<
'tcx
>
{
...
...
compiler/rustc_trait_selection/messages.ftl
浏览文件 @
0c2f1799
...
...
@@ -8,6 +8,15 @@ trait_selection_adjust_signature_remove_borrow = consider adjusting the signatur
*[other] arguments
}
trait_selection_closure_fn_mut_label = closure is `FnMut` because it mutates the variable `{$place}` here
trait_selection_closure_fn_once_label = closure is `FnOnce` because it moves the variable `{$place}` out of its environment
trait_selection_closure_kind_mismatch = expected a closure that implements the `{$expected}` trait, but this closure only implements `{$found}`
.label = this closure implements `{$found}`, not `{$expected}`
trait_selection_closure_kind_requirement = the requirement to implement `{$expected}` derives from here
trait_selection_dump_vtable_entries = vtable entries for `{$trait_ref}`: {$entries}
trait_selection_empty_on_clause_in_rustc_on_unimplemented = empty `on`-clause in `#[rustc_on_unimplemented]`
...
...
compiler/rustc_trait_selection/src/errors.rs
浏览文件 @
0c2f1799
...
...
@@ -4,7 +4,7 @@
SubdiagnosticMessage
,
};
use
rustc_macros
::
Diagnostic
;
use
rustc_middle
::
ty
::{
self
,
PolyTraitRef
,
Ty
};
use
rustc_middle
::
ty
::{
self
,
ClosureKind
,
PolyTraitRef
,
Ty
};
use
rustc_span
::{
Span
,
Symbol
};
#[derive(Diagnostic)]
...
...
@@ -131,3 +131,37 @@ fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
}
}
}
#[derive(Diagnostic)]
#[diag(trait_selection_closure_kind_mismatch,
code
=
"E0525"
)]
pub
struct
ClosureKindMismatch
{
#[primary_span]
#[label]
pub
closure_span
:
Span
,
pub
expected
:
ClosureKind
,
pub
found
:
ClosureKind
,
#[label(trait_selection_closure_kind_requirement)]
pub
cause_span
:
Span
,
#[subdiagnostic]
pub
fn_once_label
:
Option
<
ClosureFnOnceLabel
>
,
#[subdiagnostic]
pub
fn_mut_label
:
Option
<
ClosureFnMutLabel
>
,
}
#[derive(Subdiagnostic)]
#[label(trait_selection_closure_fn_once_label)]
pub
struct
ClosureFnOnceLabel
{
#[primary_span]
pub
span
:
Span
,
pub
place
:
String
,
}
#[derive(Subdiagnostic)]
#[label(trait_selection_closure_fn_mut_label)]
pub
struct
ClosureFnMutLabel
{
#[primary_span]
pub
span
:
Span
,
pub
place
:
String
,
}
compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
浏览文件 @
0c2f1799
...
...
@@ -7,6 +7,7 @@
ObligationCauseCode
,
ObligationCtxt
,
OutputTypeParameterMismatch
,
Overflow
,
PredicateObligation
,
SelectionError
,
TraitNotObjectSafe
,
};
use
crate
::
errors
::{
ClosureFnMutLabel
,
ClosureFnOnceLabel
,
ClosureKindMismatch
};
use
crate
::
infer
::
error_reporting
::{
TyCategory
,
TypeAnnotationNeeded
as
ErrorCode
};
use
crate
::
infer
::
type_variable
::{
TypeVariableOrigin
,
TypeVariableOriginKind
};
use
crate
::
infer
::{
self
,
InferCtxt
};
...
...
@@ -3121,24 +3122,15 @@ fn report_closure_error(
kind
:
ty
::
ClosureKind
,
)
->
DiagnosticBuilder
<
'tcx
,
ErrorGuaranteed
>
{
let
closure_span
=
self
.tcx
.def_span
(
closure_def_id
);
let
mut
err
=
struct_span_err!
(
self
.tcx.sess
,
closure_span
,
E0525
,
"expected a closure that implements the `{}` trait,
\
but this closure only implements `{}`"
,
kind
,
found_kind
);
err
.span_label
(
let
mut
err
=
ClosureKindMismatch
{
closure_span
,
format!
(
"this closure implements `{found_kind}`, not `{kind}`"
)
,
);
err
.span_label
(
obligation
.cause.span
,
f
ormat!
(
"the requirement to implement `{kind}` derives from here"
)
,
)
;
expected
:
kind
,
found
:
found_kind
,
cause_span
:
obligation
.cause.span
,
fn_once_label
:
None
,
f
n_mut_label
:
None
,
}
;
// Additional context information explaining why the closure only implements
// a particular trait.
...
...
@@ -3146,30 +3138,22 @@ fn report_closure_error(
let
hir_id
=
self
.tcx
.hir
()
.local_def_id_to_hir_id
(
closure_def_id
.expect_local
());
match
(
found_kind
,
typeck_results
.closure_kind_origins
()
.get
(
hir_id
))
{
(
ty
::
ClosureKind
::
FnOnce
,
Some
((
span
,
place
)))
=>
{
err
.span_label
(
*
span
,
format!
(
"closure is `FnOnce` because it moves the
\
variable `{}` out of its environment"
,
ty
::
place_to_string_for_capture
(
self
.tcx
,
place
)
),
);
err
.fn_once_label
=
Some
(
ClosureFnOnceLabel
{
span
:
*
span
,
place
:
ty
::
place_to_string_for_capture
(
self
.tcx
,
&
place
),
})
}
(
ty
::
ClosureKind
::
FnMut
,
Some
((
span
,
place
)))
=>
{
err
.span_label
(
*
span
,
format!
(
"closure is `FnMut` because it mutates the
\
variable `{}` here"
,
ty
::
place_to_string_for_capture
(
self
.tcx
,
place
)
),
);
err
.fn_mut_label
=
Some
(
ClosureFnMutLabel
{
span
:
*
span
,
place
:
ty
::
place_to_string_for_capture
(
self
.tcx
,
&
place
),
})
}
_
=>
{}
}
}
err
self
.tcx.sess
.create_err
(
err
)
}
fn
report_type_parameter_mismatch_cyclic_type_error
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录