Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
87242f3c
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,发现更多精彩内容 >>
提交
87242f3c
编写于
1月 03, 2018
作者:
E
Esteban Küber
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Provide suggestion when trying to use method on numeric literal
上级
885011ef
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
161 addition
and
33 deletion
+161
-33
src/librustc_typeck/check/method/suggest.rs
src/librustc_typeck/check/method/suggest.rs
+57
-15
src/librustc_typeck/diagnostics.rs
src/librustc_typeck/diagnostics.rs
+25
-0
src/test/ui/issue-41652/issue_41652.rs
src/test/ui/issue-41652/issue_41652.rs
+1
-1
src/test/ui/issue-41652/issue_41652.stderr
src/test/ui/issue-41652/issue_41652.stderr
+4
-10
src/test/ui/macros/macro-backtrace-invalid-internals.rs
src/test/ui/macros/macro-backtrace-invalid-internals.rs
+14
-0
src/test/ui/macros/macro-backtrace-invalid-internals.stderr
src/test/ui/macros/macro-backtrace-invalid-internals.stderr
+33
-7
src/test/ui/suggestions/method-on-ambiguous-numeric-type.rs
src/test/ui/suggestions/method-on-ambiguous-numeric-type.rs
+15
-0
src/test/ui/suggestions/method-on-ambiguous-numeric-type.stderr
...st/ui/suggestions/method-on-ambiguous-numeric-type.stderr
+12
-0
未找到文件。
src/librustc_typeck/check/method/suggest.rs
浏览文件 @
87242f3c
...
...
@@ -195,15 +195,53 @@ pub fn report_method_error(&self,
}
};
let
mut
err
=
if
!
actual
.references_error
()
{
struct_span_err!
(
tcx
.sess
,
span
,
E0599
,
"no {} named `{}` found for type `{}` in the current scope"
,
type_str
,
item_name
,
ty_string
)
// Suggest clamping down the type if the method that is being attempted to
// be used exists at all, and the type is an ambiuous numeric type
// ({integer}/{float}).
let
mut
candidates
=
all_traits
(
self
.tcx
)
.filter
(|
info
|
{
self
.associated_item
(
info
.def_id
,
item_name
,
Namespace
::
Value
)
.is_some
()
});
if
let
(
true
,
false
,
Some
(
expr
),
Some
(
_
))
=
(
actual
.is_numeric
(),
actual
.has_concrete_skeleton
(),
rcvr_expr
,
candidates
.next
())
{
let
mut
err
=
struct_span_err!
(
tcx
.sess
,
span
,
E0689
,
"can't call {} `{}` on ambiguous numeric type `{}`"
,
type_str
,
item_name
,
ty_string
);
let
snippet
=
tcx
.sess
.codemap
()
.span_to_snippet
(
expr
.span
)
.unwrap_or
(
"4"
.to_string
());
let
concrete_type
=
if
actual
.is_integral
()
{
"u32"
}
else
{
"f32"
};
err
.span_suggestion
(
expr
.span
,
&
format!
(
"you must specify a concrete type for
\
this numeric value, like `{}`"
,
concrete_type
),
format!
(
"({} as {})"
,
snippet
,
concrete_type
));
err
.emit
();
return
;
}
else
{
struct_span_err!
(
tcx
.sess
,
span
,
E0599
,
"no {} named `{}` found for type `{}` in the current scope"
,
type_str
,
item_name
,
ty_string
)
}
}
else
{
tcx
.sess
.diagnostic
()
.struct_dummy
()
};
...
...
@@ -305,12 +343,16 @@ pub fn report_method_error(&self,
bound_list
));
}
self
.suggest_traits_to_import
(
&
mut
err
,
span
,
rcvr_ty
,
item_name
,
rcvr_expr
,
out_of_scope_traits
);
if
actual
.is_numeric
()
&&
actual
.is_fresh
()
{
}
else
{
self
.suggest_traits_to_import
(
&
mut
err
,
span
,
rcvr_ty
,
item_name
,
rcvr_expr
,
out_of_scope_traits
);
}
if
let
Some
(
lev_candidate
)
=
lev_candidate
{
err
.help
(
&
format!
(
"did you mean `{}`?"
,
lev_candidate
.name
));
...
...
src/librustc_typeck/diagnostics.rs
浏览文件 @
87242f3c
...
...
@@ -4641,6 +4641,31 @@ fn foo<U: Iterator>(&self, _: &U) { } // error method `foo` has incompatible
```
"##
,
E0689
:
r##"
This error indicates that the numeric value for the method being passed exists
but the type of the numeric value or binding could not be identified.
The error happens on numeric literals:
```compile_fail,E0689
2.0.powi(2);
```
and on numeric bindings without an identified concrete type:
```compile_fail,E0689
let x = 2.0;
x.powi(2); // same error as above
```
Because of this, you must give the numeric literal or binding a type:
```
let _ = (2.0 as f32).powi(2);
let x: f32 = 2.0;
let _ = x.powi(2);
```
"##
,
}
register_diagnostics!
{
...
...
src/test/ui/issue-41652/issue_41652.rs
浏览文件 @
87242f3c
...
...
@@ -17,7 +17,7 @@
impl
issue_41652_b
::
Tr
for
S
{
fn
f
()
{
3
.f
()
//~^ ERROR
no method named `f` found for type `{integer}` in the current scope
//~^ ERROR
can't call method `f` on ambiguous numeric type `{integer}`
}
}
...
...
src/test/ui/issue-41652/issue_41652.stderr
浏览文件 @
87242f3c
error[E0
599]: no method named `f` found for type `{integer}` in the current scope
error[E0
689]: can't call method `f` on ambiguous numeric type `{integer}`
--> $DIR/issue_41652.rs:19:11
|
19 | 3.f()
| ^
help: you must specify a concrete type for this numeric value, like `u32`
|
= note: found the following associated functions; to be used as methods, functions must have a `self` parameter
= help: try with `{integer}::f`
note: candidate #1 is defined in the trait `issue_41652_b::Tr`
--> $DIR/auxiliary/issue_41652_b.rs:14:5
|
14 | / fn f()
15 | | where Self: Sized;
| |__________________________^
= help: to disambiguate the method call, write `issue_41652_b::Tr::f(3)` instead
19 | (3 as u32).f()
| ^^^^^^^^^^
error: aborting due to previous error
src/test/ui/macros/macro-backtrace-invalid-internals.rs
浏览文件 @
87242f3c
...
...
@@ -46,12 +46,26 @@
}
}
macro_rules!
real_method_stmt
{
()
=>
{
2.0
.powi
(
2
)
//~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
}
}
macro_rules!
real_method_expr
{
()
=>
{
2.0
.powi
(
2
)
//~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
}
}
fn
main
()
{
fake_method_stmt!
();
fake_field_stmt!
();
fake_anon_field_stmt!
();
real_method_stmt!
();
let
_
=
fake_method_expr!
();
let
_
=
fake_field_expr!
();
let
_
=
fake_anon_field_expr!
();
let
_
=
real_method_expr!
();
}
src/test/ui/macros/macro-backtrace-invalid-internals.stderr
浏览文件 @
87242f3c
...
...
@@ -4,7 +4,7 @@ error[E0599]: no method named `fake` found for type `{integer}` in the current s
15 | 1.fake() //~ ERROR no method
| ^^^^
...
50
| fake_method_stmt!();
62
| fake_method_stmt!();
| -------------------- in this macro invocation
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
...
...
@@ -13,7 +13,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
21 | 1.fake //~ ERROR doesn't have fields
| ^^^^
...
51
| fake_field_stmt!();
63
| fake_field_stmt!();
| ------------------- in this macro invocation
error[E0609]: no field `0` on type `{integer}`
...
...
@@ -22,16 +22,29 @@ error[E0609]: no field `0` on type `{integer}`
27 | (1).0 //~ ERROR no field
| ^^^^^
...
52
| fake_anon_field_stmt!();
64
| fake_anon_field_stmt!();
| ------------------------ in this macro invocation
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
--> $DIR/macro-backtrace-invalid-internals.rs:51:15
|
51 | 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^
...
65 | real_method_stmt!();
| -------------------- in this macro invocation
help: you must specify a concrete type for this numeric value, like `f32`
|
51 | (2.0 as f32).powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^^^^^^^^^
error[E0599]: no method named `fake` found for type `{integer}` in the current scope
--> $DIR/macro-backtrace-invalid-internals.rs:33:13
|
33 | 1.fake() //~ ERROR no method
| ^^^^
...
54
| let _ = fake_method_expr!();
67
| let _ = fake_method_expr!();
| ------------------- in this macro invocation
error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
...
...
@@ -40,7 +53,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
39 | 1.fake //~ ERROR doesn't have fields
| ^^^^
...
55
| let _ = fake_field_expr!();
68
| let _ = fake_field_expr!();
| ------------------ in this macro invocation
error[E0609]: no field `0` on type `{integer}`
...
...
@@ -49,8 +62,21 @@ error[E0609]: no field `0` on type `{integer}`
45 | (1).0 //~ ERROR no field
| ^^^^^
...
56
| let _ = fake_anon_field_expr!();
69
| let _ = fake_anon_field_expr!();
| ----------------------- in this macro invocation
error: aborting due to 6 previous errors
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
--> $DIR/macro-backtrace-invalid-internals.rs:57:15
|
57 | 2.0.powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^
...
70 | let _ = real_method_expr!();
| ------------------- in this macro invocation
help: you must specify a concrete type for this numeric value, like `f32`
|
57 | (2.0 as f32).powi(2) //~ ERROR can't call method `powi` on ambiguous numeric type `{float}`
| ^^^^^^^^^^^^
error: aborting due to 8 previous errors
src/test/ui/suggestions/method-on-ambiguous-numeric-type.rs
0 → 100644
浏览文件 @
87242f3c
// Copyright 2018 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.
fn
main
()
{
let
x
=
2.0
.powi
(
2
);
//~^ ERROR can't call method `powi` on ambiguous numeric type `{float}`
println!
(
"{:?}"
,
x
);
}
src/test/ui/suggestions/method-on-ambiguous-numeric-type.stderr
0 → 100644
浏览文件 @
87242f3c
error[E0689]: can't call method `powi` on ambiguous numeric type `{float}`
--> $DIR/method-on-ambiguous-numeric-type.rs:12:17
|
12 | let x = 2.0.powi(2);
| ^^^^
help: you must specify a concrete type for this numeric value, like `f32`
|
12 | let x = (2.0 as f32).powi(2);
| ^^^^^^^^^^^^
error: aborting due to previous error
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录