Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
e39bcecf
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,发现更多精彩内容 >>
提交
e39bcecf
编写于
7月 18, 2017
作者:
E
Esteban Küber
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Handle type ascription cases with a method call instead of a type
上级
faf90351
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
94 addition
and
14 deletion
+94
-14
src/librustc_resolve/lib.rs
src/librustc_resolve/lib.rs
+63
-14
src/test/ui/suggestions/type-ascription-with-fn-call.rs
src/test/ui/suggestions/type-ascription-with-fn-call.rs
+18
-0
src/test/ui/suggestions/type-ascription-with-fn-call.stderr
src/test/ui/suggestions/type-ascription-with-fn-call.stderr
+13
-0
未找到文件。
src/librustc_resolve/lib.rs
浏览文件 @
e39bcecf
...
...
@@ -599,20 +599,25 @@ fn visit_local(&mut self, local: &'tcx Local) {
self
.resolve_local
(
local
);
}
fn
visit_ty
(
&
mut
self
,
ty
:
&
'tcx
Ty
)
{
if
let
TyKind
::
Path
(
ref
qself
,
ref
path
)
=
ty
.node
{
match
ty
.node
{
TyKind
::
Path
(
ref
qself
,
ref
path
)
=>
{
self
.smart_resolve_path
(
ty
.id
,
qself
.as_ref
(),
path
,
PathSource
::
Type
);
}
else
if
let
TyKind
::
ImplicitSelf
=
ty
.node
{
}
TyKind
::
ImplicitSelf
=>
{
let
self_ty
=
keywords
::
SelfType
.ident
();
let
def
=
self
.resolve_ident_in_lexical_scope
(
self_ty
,
TypeNS
,
true
,
ty
.span
)
.map_or
(
Def
::
Err
,
|
d
|
d
.def
());
self
.record_def
(
ty
.id
,
PathResolution
::
new
(
def
));
}
else
if
let
TyKind
::
Array
(
ref
element
,
ref
length
)
=
ty
.node
{
}
TyKind
::
Array
(
ref
element
,
ref
length
)
=>
{
self
.visit_ty
(
element
);
self
.with_constant_rib
(|
this
|
{
this
.visit_expr
(
length
);
});
return
;
}
_
=>
(),
}
visit
::
walk_ty
(
self
,
ty
);
}
fn
visit_poly_trait_ref
(
&
mut
self
,
...
...
@@ -1221,6 +1226,9 @@ pub struct Resolver<'a> {
// This table maps struct IDs into struct constructor IDs,
// it's not used during normal resolution, only for better error reporting.
struct_constructors
:
DefIdMap
<
(
Def
,
ty
::
Visibility
)
>
,
// Only used for better errors on `fn(): fn()`
current_type_ascription
:
Vec
<
Span
>
,
}
pub
struct
ResolverArenas
<
'a
>
{
...
...
@@ -1411,6 +1419,7 @@ pub fn new(session: &'a Session,
struct_constructors
:
DefIdMap
(),
found_unresolved_macro
:
false
,
unused_macros
:
FxHashSet
(),
current_type_ascription
:
Vec
::
new
(),
}
}
...
...
@@ -2495,6 +2504,7 @@ fn smart_resolve_path_fragment(&mut self,
// Fallback label.
if
!
levenshtein_worked
{
err
.span_label
(
base_span
,
fallback_label
);
this
.type_ascription_suggestion
(
&
mut
err
,
base_span
);
}
err
};
...
...
@@ -2550,6 +2560,41 @@ fn smart_resolve_path_fragment(&mut self,
resolution
}
fn
type_ascription_suggestion
(
&
self
,
err
:
&
mut
DiagnosticBuilder
,
base_span
:
Span
)
{
debug!
(
"type_ascription_suggetion {:?}"
,
base_span
);
let
cm
=
self
.session
.codemap
();
debug!
(
"self.current_type_ascription {:?}"
,
self
.current_type_ascription
);
if
let
Some
(
sp
)
=
self
.current_type_ascription
.last
()
{
let
mut
sp
=
*
sp
;
loop
{
// try to find the `:`, bail on first non-':'/non-whitespace
sp
=
sp
.next_point
();
if
let
Ok
(
snippet
)
=
cm
.span_to_snippet
(
sp
.to
(
sp
.next_point
()))
{
debug!
(
"snippet {:?}"
,
snippet
);
let
line_sp
=
cm
.lookup_char_pos
(
sp
.hi
)
.line
;
let
line_base_sp
=
cm
.lookup_char_pos
(
base_span
.lo
)
.line
;
debug!
(
"{:?} {:?}"
,
line_sp
,
line_base_sp
);
if
snippet
==
":"
{
err
.span_label
(
base_span
,
"expecting a type here because of type ascription"
);
if
line_sp
!=
line_base_sp
{
err
.span_suggestion_short
(
sp
,
"did you mean to use `;` here instead?"
,
";"
.to_string
());
}
break
;
}
else
if
snippet
.trim
()
.len
()
!=
0
{
debug!
(
"tried to find type ascription `:` token, couldn't find it"
);
break
;
}
}
else
{
break
;
}
}
}
}
fn
self_type_is_available
(
&
mut
self
,
span
:
Span
)
->
bool
{
let
binding
=
self
.resolve_ident_in_lexical_scope
(
keywords
::
SelfType
.ident
(),
TypeNS
,
false
,
span
);
...
...
@@ -3166,7 +3211,11 @@ fn resolve_expr(&mut self, expr: &Expr, parent: Option<&Expr>) {
self
.resolve_expr
(
argument
,
None
);
}
}
ExprKind
::
Type
(
ref
type_expr
,
_
)
=>
{
self
.current_type_ascription
.push
(
type_expr
.span
);
visit
::
walk_expr
(
self
,
expr
);
self
.current_type_ascription
.pop
();
}
_
=>
{
visit
::
walk_expr
(
self
,
expr
);
}
...
...
src/test/ui/suggestions/type-ascription-with-fn-call.rs
0 → 100644
浏览文件 @
e39bcecf
// 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.
#![feature(type_ascription)]
fn
main
()
{
f
()
:
f
();
}
fn
f
()
{}
src/test/ui/suggestions/type-ascription-with-fn-call.stderr
0 → 100644
浏览文件 @
e39bcecf
error[E0573]: expected type, found function `f`
--> $DIR/type-ascription-with-fn-call.rs:15:5
|
14 | f() :
| - help: did you mean to use `;` here instead?
15 | f();
| ^^^
| |
| not a type
| expecting a type here because of type ascription
error: aborting due to previous error
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录