Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
5639e52a
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,发现更多精彩内容 >>
提交
5639e52a
编写于
6月 09, 2022
作者:
T
Takayuki Maeda
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move suggestions to its own method
上级
9db03b9b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
107 addition
and
29 deletion
+107
-29
compiler/rustc_resolve/src/late/diagnostics.rs
compiler/rustc_resolve/src/late/diagnostics.rs
+31
-19
src/test/ui/suggestions/suggest-swapping-self-ty-and-trait-edition-2021.rs
...stions/suggest-swapping-self-ty-and-trait-edition-2021.rs
+12
-1
src/test/ui/suggestions/suggest-swapping-self-ty-and-trait-edition-2021.stderr
...ns/suggest-swapping-self-ty-and-trait-edition-2021.stderr
+26
-4
src/test/ui/suggestions/suggest-swapping-self-ty-and-trait.rs
...test/ui/suggestions/suggest-swapping-self-ty-and-trait.rs
+12
-1
src/test/ui/suggestions/suggest-swapping-self-ty-and-trait.stderr
.../ui/suggestions/suggest-swapping-self-ty-and-trait.stderr
+26
-4
未找到文件。
compiler/rustc_resolve/src/late/diagnostics.rs
浏览文件 @
5639e52a
...
...
@@ -250,25 +250,8 @@ struct BaseError<'a> {
let
code
=
source
.error_code
(
res
.is_some
());
let
mut
err
=
self
.r.session
.struct_span_err_with_code
(
base_error
.span
,
&
base_error
.msg
,
code
);
if
let
Some
((
trait_ref
,
self_ty
))
=
self
.diagnostic_metadata.currently_processing_impl_trait
.clone
()
&&
let
TyKind
::
Path
(
_
,
self_ty_path
)
=
&
self_ty
.kind
&&
let
PathResult
::
Module
(
ModuleOrUniformRoot
::
Module
(
module
))
=
self
.resolve_path
(
&
Segment
::
from_path
(
self_ty_path
),
Some
(
TypeNS
),
None
)
&&
let
ModuleKind
::
Def
(
DefKind
::
Trait
,
..
)
=
module
.kind
&&
trait_ref
.path.span
==
span
&&
let
PathSource
::
Trait
(
_
)
=
source
&&
let
Some
(
Res
::
Def
(
DefKind
::
Struct
,
_
))
=
res
&&
let
Ok
(
self_ty_str
)
=
self
.r.session
.source_map
()
.span_to_snippet
(
self_ty
.span
)
&&
let
Ok
(
trait_ref_str
)
=
self
.r.session
.source_map
()
.span_to_snippet
(
trait_ref
.path.span
)
{
err
.multipart_suggestion
(
"consider swapping the struct and the trait"
,
vec!
[(
trait_ref
.path.span
,
self_ty_str
),
(
self_ty
.span
,
trait_ref_str
)],
Applicability
::
MaybeIncorrect
,
);
}
self
.suggest_swapping_misplaced_self_ty_and_trait
(
&
mut
err
,
source
,
res
,
base_error
.span
);
if
let
Some
(
sugg
)
=
base_error
.suggestion
{
err
.span_suggestion_verbose
(
sugg
.0
,
sugg
.1
,
sugg
.2
,
Applicability
::
MaybeIncorrect
);
...
...
@@ -704,6 +687,35 @@ fn detect_assoct_type_constraint_meant_as_path(&self, base_span: Span, err: &mut
}
}
fn
suggest_swapping_misplaced_self_ty_and_trait
(
&
mut
self
,
err
:
&
mut
Diagnostic
,
source
:
PathSource
<
'_
>
,
res
:
Option
<
Res
>
,
span
:
Span
,
)
{
if
let
Some
((
trait_ref
,
self_ty
))
=
self
.diagnostic_metadata.currently_processing_impl_trait
.clone
()
&&
let
TyKind
::
Path
(
_
,
self_ty_path
)
=
&
self_ty
.kind
&&
let
PathResult
::
Module
(
ModuleOrUniformRoot
::
Module
(
module
))
=
self
.resolve_path
(
&
Segment
::
from_path
(
self_ty_path
),
Some
(
TypeNS
),
None
)
&&
let
ModuleKind
::
Def
(
DefKind
::
Trait
,
..
)
=
module
.kind
&&
trait_ref
.path.span
==
span
&&
let
PathSource
::
Trait
(
_
)
=
source
&&
let
Some
(
Res
::
Def
(
DefKind
::
Struct
|
DefKind
::
Enum
|
DefKind
::
Union
,
_
))
=
res
&&
let
Ok
(
self_ty_str
)
=
self
.r.session
.source_map
()
.span_to_snippet
(
self_ty
.span
)
&&
let
Ok
(
trait_ref_str
)
=
self
.r.session
.source_map
()
.span_to_snippet
(
trait_ref
.path.span
)
{
err
.multipart_suggestion
(
"`impl` items mention the trait being implemented first and the type it is being implemented for second"
,
vec!
[(
trait_ref
.path.span
,
self_ty_str
),
(
self_ty
.span
,
trait_ref_str
)],
Applicability
::
MaybeIncorrect
,
);
}
}
fn
get_single_associated_item
(
&
mut
self
,
path
:
&
[
Segment
],
...
...
src/test/ui/suggestions/suggest-swapping-self-ty-and-trait-edition-2021.rs
浏览文件 @
5639e52a
...
...
@@ -2,10 +2,21 @@
pub
trait
Trait
<
'a
,
T
>
{}
pub
struct
Struct
<
T
>
{}
pub
struct
Struct
<
T
>
;
pub
enum
Enum
<
T
>
{}
pub
union
Union
<
T
>
{
f1
:
usize
,
}
impl
<
'a
,
T
>
Struct
<
T
>
for
Trait
<
'a
,
T
>
{}
//~^ ERROR expected trait, found struct `Struct`
//~| ERROR trait objects must include the `dyn` keyword
impl
<
'a
,
T
>
Enum
<
T
>
for
Trait
<
'a
,
T
>
{}
//~^ ERROR expected trait, found enum `Enum`
impl
<
'a
,
T
>
Union
<
T
>
for
Trait
<
'a
,
T
>
{}
//~^ ERROR expected trait, found union `Union`
fn
main
()
{}
src/test/ui/suggestions/suggest-swapping-self-ty-and-trait-edition-2021.stderr
浏览文件 @
5639e52a
error[E0404]: expected trait, found struct `Struct`
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:
7
:13
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:
12
:13
|
LL | impl<'a, T> Struct<T> for Trait<'a, T> {}
| ^^^^^^^^^ not a trait
|
help:
consider swapping the struct and the trait
help:
`impl` items mention the trait being implemented first and the type it is being implemented for second
|
LL | impl<'a, T> Trait<'a, T> for Struct<T> {}
| ~~~~~~~~~~~~ ~~~~~~~~~
error[E0404]: expected trait, found enum `Enum`
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:16:13
|
LL | impl<'a, T> Enum<T> for Trait<'a, T> {}
| ^^^^^^^ not a trait
|
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
|
LL | impl<'a, T> Trait<'a, T> for Enum<T> {}
| ~~~~~~~~~~~~ ~~~~~~~
error[E0404]: expected trait, found union `Union`
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:19:13
|
LL | impl<'a, T> Union<T> for Trait<'a, T> {}
| ^^^^^^^^ not a trait
|
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
|
LL | impl<'a, T> Trait<'a, T> for Union<T> {}
| ~~~~~~~~~~~~ ~~~~~~~~
error[E0782]: trait objects must include the `dyn` keyword
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:
7
:27
--> $DIR/suggest-swapping-self-ty-and-trait-edition-2021.rs:
12
:27
|
LL | impl<'a, T> Struct<T> for Trait<'a, T> {}
| ^^^^^^^^^^^^
...
...
@@ -21,7 +43,7 @@ LL - impl<'a, T> Struct<T> for Trait<'a, T> {}
LL + impl<'a, T> Struct<T> for dyn Trait<'a, T> {}
|
error: aborting due to
2
previous errors
error: aborting due to
4
previous errors
Some errors have detailed explanations: E0404, E0782.
For more information about an error, try `rustc --explain E0404`.
src/test/ui/suggestions/suggest-swapping-self-ty-and-trait.rs
浏览文件 @
5639e52a
pub
trait
Trait
<
'a
,
T
>
{}
pub
struct
Struct
<
T
>
{}
pub
struct
Struct
<
T
>
;
pub
enum
Enum
<
T
>
{}
pub
union
Union
<
T
>
{
f1
:
usize
,
}
impl
<
'a
,
T
>
Struct
<
T
>
for
Trait
<
'a
,
T
>
{}
//~^ ERROR expected trait, found struct `Struct`
//~| WARNING trait objects without an explicit `dyn` are deprecated
//~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
impl
<
'a
,
T
>
Enum
<
T
>
for
Trait
<
'a
,
T
>
{}
//~^ ERROR expected trait, found enum `Enum`
impl
<
'a
,
T
>
Union
<
T
>
for
Trait
<
'a
,
T
>
{}
//~^ ERROR expected trait, found union `Union`
fn
main
()
{}
src/test/ui/suggestions/suggest-swapping-self-ty-and-trait.stderr
浏览文件 @
5639e52a
error[E0404]: expected trait, found struct `Struct`
--> $DIR/suggest-swapping-self-ty-and-trait.rs:
5
:13
--> $DIR/suggest-swapping-self-ty-and-trait.rs:
10
:13
|
LL | impl<'a, T> Struct<T> for Trait<'a, T> {}
| ^^^^^^^^^ not a trait
|
help:
consider swapping the struct and the trait
help:
`impl` items mention the trait being implemented first and the type it is being implemented for second
|
LL | impl<'a, T> Trait<'a, T> for Struct<T> {}
| ~~~~~~~~~~~~ ~~~~~~~~~
error[E0404]: expected trait, found enum `Enum`
--> $DIR/suggest-swapping-self-ty-and-trait.rs:15:13
|
LL | impl<'a, T> Enum<T> for Trait<'a, T> {}
| ^^^^^^^ not a trait
|
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
|
LL | impl<'a, T> Trait<'a, T> for Enum<T> {}
| ~~~~~~~~~~~~ ~~~~~~~
error[E0404]: expected trait, found union `Union`
--> $DIR/suggest-swapping-self-ty-and-trait.rs:18:13
|
LL | impl<'a, T> Union<T> for Trait<'a, T> {}
| ^^^^^^^^ not a trait
|
help: `impl` items mention the trait being implemented first and the type it is being implemented for second
|
LL | impl<'a, T> Trait<'a, T> for Union<T> {}
| ~~~~~~~~~~~~ ~~~~~~~~
warning: trait objects without an explicit `dyn` are deprecated
--> $DIR/suggest-swapping-self-ty-and-trait.rs:
5
:27
--> $DIR/suggest-swapping-self-ty-and-trait.rs:
10
:27
|
LL | impl<'a, T> Struct<T> for Trait<'a, T> {}
| ^^^^^^^^^^^^
...
...
@@ -24,6 +46,6 @@ LL - impl<'a, T> Struct<T> for Trait<'a, T> {}
LL + impl<'a, T> Struct<T> for dyn Trait<'a, T> {}
|
error: aborting due to
previous error
; 1 warning emitted
error: aborting due to
3 previous errors
; 1 warning emitted
For more information about this error, try `rustc --explain E0404`.
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录