Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
a83c3e77
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,发现更多精彩内容 >>
提交
a83c3e77
编写于
9月 23, 2017
作者:
B
bors
浏览文件
操作
浏览文件
下载
差异文件
Auto merge of #43870 - GuillaumeGomez:deref-suggestion, r=nikomatsakis
Add deref suggestion Fixes #34562.
上级
7f8aef94
21d4ba2e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
126 addition
and
0 deletion
+126
-0
src/librustc_typeck/check/demand.rs
src/librustc_typeck/check/demand.rs
+33
-0
src/test/ui/deref-suggestion.rs
src/test/ui/deref-suggestion.rs
+34
-0
src/test/ui/deref-suggestion.stderr
src/test/ui/deref-suggestion.stderr
+59
-0
未找到文件。
src/librustc_typeck/check/demand.rs
浏览文件 @
a83c3e77
...
...
@@ -261,6 +261,39 @@ fn check_ref(&self,
}
None
}
(
_
,
&
ty
::
TyRef
(
_
,
checked
))
=>
{
// We have `&T`, check if what was expected was `T`. If so,
// we may want to suggest adding a `*`, or removing
// a `&`.
//
// (But, also check check the `expn_info()` to see if this is
// a macro; if so, it's hard to extract the text and make a good
// suggestion, so don't bother.)
if
self
.infcx
.can_sub
(
self
.param_env
,
checked
.ty
,
&
expected
)
.is_ok
()
&&
expr
.span
.ctxt
()
.outer
()
.expn_info
()
.is_none
()
{
match
expr
.node
{
// Maybe remove `&`?
hir
::
ExprAddrOf
(
_
,
ref
expr
)
=>
{
if
let
Ok
(
code
)
=
self
.tcx.sess
.codemap
()
.span_to_snippet
(
expr
.span
)
{
return
Some
(
format!
(
"try with `{}`"
,
code
));
}
}
// Maybe add `*`? Only if `T: Copy`.
_
=>
{
if
!
self
.infcx
.type_moves_by_default
(
self
.param_env
,
checked
.ty
,
expr
.span
)
{
let
sp
=
self
.sess
()
.codemap
()
.call_span_if_macro
(
expr
.span
);
if
let
Ok
(
code
)
=
self
.tcx.sess
.codemap
()
.span_to_snippet
(
sp
)
{
return
Some
(
format!
(
"try with `*{}`"
,
code
));
}
}
},
}
}
None
}
_
=>
None
,
}
}
...
...
src/test/ui/deref-suggestion.rs
0 → 100644
浏览文件 @
a83c3e77
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
macro_rules!
borrow
{
(
$x:expr
)
=>
{
&
$x
}
}
fn
foo
(
_
:
String
)
{}
fn
foo2
(
s
:
&
String
)
{
foo
(
s
);
}
fn
foo3
(
_
:
u32
)
{}
fn
foo4
(
u
:
&
u32
)
{
foo3
(
u
);
}
fn
main
()
{
let
s
=
String
::
new
();
let
r_s
=
&
s
;
foo2
(
r_s
);
foo
(
&
"aaa"
.to_owned
());
foo
(
&
mut
"aaa"
.to_owned
());
foo3
(
borrow!
(
0
));
foo4
(
&
0
);
}
src/test/ui/deref-suggestion.stderr
0 → 100644
浏览文件 @
a83c3e77
error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:18:9
|
18 | foo(s);
| ^ expected struct `std::string::String`, found reference
|
= note: expected type `std::string::String`
found type `&std::string::String`
= help: here are some functions which might fulfill your needs:
- .escape_debug()
- .escape_default()
- .escape_unicode()
- .to_lowercase()
- .to_uppercase()
error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:23:10
|
23 | foo3(u);
| ^ expected u32, found &u32
|
= note: expected type `u32`
found type `&u32`
= help: try with `*u`
error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:30:9
|
30 | foo(&"aaa".to_owned());
| ^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found reference
|
= note: expected type `std::string::String`
found type `&std::string::String`
= help: try with `"aaa".to_owned()`
error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:31:9
|
31 | foo(&mut "aaa".to_owned());
| ^^^^^^^^^^^^^^^^^^^^^ expected struct `std::string::String`, found mutable reference
|
= note: expected type `std::string::String`
found type `&mut std::string::String`
= help: try with `"aaa".to_owned()`
error[E0308]: mismatched types
--> $DIR/deref-suggestion.rs:12:20
|
12 | ($x:expr) => { &$x }
| ^^^ expected u32, found &{integer}
...
32 | foo3(borrow!(0));
| ---------- in this macro invocation
|
= note: expected type `u32`
found type `&{integer}`
error: aborting due to 5 previous errors
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录