Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
38d7e273
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,发现更多精彩内容 >>
提交
38d7e273
编写于
6月 05, 2022
作者:
M
Michael Goulet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Properly replace `impl Trait` in fn args, turn {integer} to i32
上级
55805ab4
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
76 addition
and
12 deletion
+76
-12
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
...trait_selection/src/traits/error_reporting/suggestions.rs
+27
-12
src/test/ui/suggestions/issue-97677.rs
src/test/ui/suggestions/issue-97677.rs
+6
-0
src/test/ui/suggestions/issue-97677.stderr
src/test/ui/suggestions/issue-97677.stderr
+16
-0
src/test/ui/suggestions/issue-97760.rs
src/test/ui/suggestions/issue-97760.rs
+9
-0
src/test/ui/suggestions/issue-97760.stderr
src/test/ui/suggestions/issue-97760.stderr
+18
-0
未找到文件。
compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs
浏览文件 @
38d7e273
...
...
@@ -372,21 +372,34 @@ fn suggest_restriction<'tcx>(
// but instead we choose to suggest replacing all instances of `impl Trait` with `T`
// where `T: Trait`.
let
mut
ty_spans
=
vec!
[];
let
impl_trait_str
=
format!
(
"impl {}"
,
bound_str
);
for
input
in
fn_sig
.decl.inputs
{
if
let
hir
::
TyKind
::
Path
(
hir
::
QPath
::
Resolved
(
None
,
hir
::
Path
{
segments
:
[
segment
],
..
},
))
=
input
.kind
{
if
segment
.ident
.as_str
()
==
impl_trait_str
.as_str
()
{
// `fn foo(t: impl Trait)`
// ^^^^^^^^^^ get this to suggest `T` instead
struct
ReplaceImplTraitVisitor
<
'a
>
{
ty_spans
:
&
'a
mut
Vec
<
Span
>
,
bound_str
:
&
'a
str
,
}
impl
<
'a
,
'hir
>
hir
::
intravisit
::
Visitor
<
'hir
>
for
ReplaceImplTraitVisitor
<
'a
>
{
fn
visit_ty
(
&
mut
self
,
t
:
&
'hir
hir
::
Ty
<
'hir
>
)
{
if
let
hir
::
TyKind
::
Path
(
hir
::
QPath
::
Resolved
(
None
,
hir
::
Path
{
segments
:
[
segment
],
..
},
))
=
t
.kind
{
if
segment
.ident
.as_str
()
.strip_prefix
(
"impl "
)
.map
(|
s
|
s
.trim_start
())
==
Some
(
self
.bound_str
)
{
// `fn foo(t: impl Trait)`
// ^^^^^^^^^^ get this to suggest `T` instead
// There might be more than one `impl Trait`.
ty_spans
.push
(
input
.span
);
// There might be more than one `impl Trait`.
self
.ty_spans
.push
(
t
.span
);
return
;
}
}
hir
::
intravisit
::
walk_ty
(
self
,
t
);
}
}
ReplaceImplTraitVisitor
{
ty_spans
:
&
mut
ty_spans
,
bound_str
:
&
bound_str
}
.visit_ty
(
input
);
}
let
type_param_name
=
generics
.params
.next_type_param_name
(
Some
(
&
bound_str
));
...
...
@@ -396,7 +409,7 @@ fn suggest_restriction<'tcx>(
// FIXME: modify the `trait_pred` instead of string shenanigans.
// Turn `<impl Trait as Foo>::Bar: Qux` into `<T as Foo>::Bar: Qux`.
let
pred
=
trait_pred
.to_predicate
(
tcx
)
.to_string
();
let
pred
=
pred
.replace
(
&
impl_trait_str
,
&
type_param_name
);
let
pred
=
pred
.replace
(
&
format!
(
"impl {}"
,
bound_str
)
,
&
type_param_name
);
let
mut
sugg
=
vec!
[
if
let
Some
(
span
)
=
generics
.span_for_param_suggestion
()
{
(
span
,
format!
(
", {}"
,
type_param
))
...
...
@@ -460,6 +473,8 @@ fn suggest_restricting_param_bound(
trait_pred
:
ty
::
PolyTraitPredicate
<
'tcx
>
,
body_id
:
hir
::
HirId
,
)
{
let
trait_pred
=
self
.resolve_numeric_literals_with_default
(
trait_pred
);
let
self_ty
=
trait_pred
.skip_binder
()
.self_ty
();
let
(
param_ty
,
projection
)
=
match
self_ty
.kind
()
{
ty
::
Param
(
_
)
=>
(
true
,
None
),
...
...
src/test/ui/suggestions/issue-97677.rs
0 → 100644
浏览文件 @
38d7e273
fn
add_ten
<
N
>
(
n
:
N
)
->
N
{
n
+
10
//~^ ERROR cannot add `{integer}` to `N`
}
fn
main
()
{}
src/test/ui/suggestions/issue-97677.stderr
0 → 100644
浏览文件 @
38d7e273
error[E0369]: cannot add `{integer}` to `N`
--> $DIR/issue-97677.rs:2:7
|
LL | n + 10
| - ^ -- {integer}
| |
| N
|
help: consider restricting type parameter `N`
|
LL | fn add_ten<N: std::ops::Add<i32>>(n: N) -> N {
| ++++++++++++++++++++
error: aborting due to previous error
For more information about this error, try `rustc --explain E0369`.
src/test/ui/suggestions/issue-97760.rs
0 → 100644
浏览文件 @
38d7e273
pub
fn
print_values
(
values
:
&
impl
IntoIterator
)
where
{
for
x
in
values
.into_iter
()
{
println!
(
"{x}"
);
//~^ ERROR <impl IntoIterator as IntoIterator>::Item` doesn't implement `std::fmt::Display
}
}
fn
main
()
{}
src/test/ui/suggestions/issue-97760.stderr
0 → 100644
浏览文件 @
38d7e273
error[E0277]: `<impl IntoIterator as IntoIterator>::Item` doesn't implement `std::fmt::Display`
--> $DIR/issue-97760.rs:4:20
|
LL | println!("{x}");
| ^ `<impl IntoIterator as IntoIterator>::Item` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `<impl IntoIterator as IntoIterator>::Item`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: introduce a type parameter with a trait bound instead of using `impl Trait`
|
LL ~ pub fn print_values<I: IntoIterator>(values: &I)
LL ~ where <I as IntoIterator>::Item: std::fmt::Display {
|
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录