Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
02e1d5ec
R
Rust
项目概览
int
/
Rust
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
02e1d5ec
编写于
2月 17, 2015
作者:
N
Niko Matsakis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
When converting parameters for an object type, be careful of defaults that reference `Self`.
Fixes #18956.
上级
f1bb6c2f
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
81 addition
and
6 deletion
+81
-6
src/librustc/util/ppaux.rs
src/librustc/util/ppaux.rs
+14
-1
src/librustc_typeck/astconv.rs
src/librustc_typeck/astconv.rs
+18
-5
src/test/compile-fail/type-parameter-defaults-referencing-Self-ppaux.rs
...le-fail/type-parameter-defaults-referencing-Self-ppaux.rs
+26
-0
src/test/compile-fail/type-parameter-defaults-referencing-Self.rs
.../compile-fail/type-parameter-defaults-referencing-Self.rs
+23
-0
未找到文件。
src/librustc/util/ppaux.rs
浏览文件 @
02e1d5ec
...
...
@@ -508,13 +508,26 @@ pub fn parameterized<'tcx,GG>(cx: &ctxt<'tcx>,
// avoid those ICEs.
let
generics
=
get_generics
();
let
has_self
=
substs
.self_ty
()
.is_some
();
let
tps
=
substs
.types
.get_slice
(
subst
::
TypeSpace
);
let
ty_params
=
generics
.types
.get_slice
(
subst
::
TypeSpace
);
let
has_defaults
=
ty_params
.last
()
.map_or
(
false
,
|
def
|
def
.default
.is_some
());
let
num_defaults
=
if
has_defaults
{
ty_params
.iter
()
.zip
(
tps
.iter
())
.rev
()
.take_while
(|
&
(
def
,
&
actual
)|
{
match
def
.default
{
Some
(
default
)
=>
default
.subst
(
cx
,
substs
)
==
actual
,
Some
(
default
)
=>
{
if
!
has_self
&&
ty
::
type_has_self
(
default
)
{
// In an object type, there is no `Self`, and
// thus if the default value references Self,
// the user will be required to give an
// explicit value. We can't even do the
// substitution below to check without causing
// an ICE. (#18956).
false
}
else
{
default
.subst
(
cx
,
substs
)
==
actual
}
}
None
=>
false
}
})
.count
()
...
...
src/librustc_typeck/astconv.rs
浏览文件 @
02e1d5ec
...
...
@@ -404,19 +404,32 @@ fn create_substs_for_ast_path<'tcx>(
let
actual_supplied_ty_param_count
=
substs
.types
.len
(
TypeSpace
);
for
param
in
&
ty_param_defs
[
actual_supplied_ty_param_count
..
]
{
match
param
.default
{
Some
(
default
)
=>
{
if
let
Some
(
default
)
=
param
.default
{
// If we are converting an object type, then the
// `Self` parameter is unknown. However, some of the
// other type parameters may reference `Self` in their
// defaults. This will lead to an ICE if we are not
// careful!
if
self_ty
.is_none
()
&&
ty
::
type_has_self
(
default
)
{
tcx
.sess
.span_err
(
span
,
&
format!
(
"the type parameter `{}` must be explicitly specified
\
in an object type because its default value `{}` references
\
the type `Self`"
,
param
.name
.user_string
(
tcx
),
default
.user_string
(
tcx
)));
substs
.types
.push
(
TypeSpace
,
tcx
.types.err
);
}
else
{
// This is a default type parameter.
let
default
=
default
.subst_spanned
(
tcx
,
&
substs
,
Some
(
span
));
substs
.types
.push
(
TypeSpace
,
default
);
}
None
=>
{
}
else
{
tcx
.sess
.span_bug
(
span
,
"extra parameter without default"
);
}
}
}
return
substs
;
}
...
...
src/test/compile-fail/type-parameter-defaults-referencing-Self-ppaux.rs
0 → 100644
浏览文件 @
02e1d5ec
// Copyright 2015 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.
// Test a default that references `Self` which is then used in an
// object type. Issue #18956. In this case, the value is supplied by
// the user, but pretty-printing the type during the error message
// caused an ICE.
trait
MyAdd
<
Rhs
=
Self
>
{
fn
add
(
&
self
,
other
:
&
Rhs
)
->
Self
;
}
impl
MyAdd
for
i32
{
fn
add
(
&
self
,
other
:
&
i32
)
->
i32
{
*
self
+
*
other
}
}
fn
main
()
{
let
x
=
5
;
let
y
=
x
as
MyAdd
<
i32
>
;
//~^ ERROR as `MyAdd<i32>`
}
src/test/compile-fail/type-parameter-defaults-referencing-Self.rs
0 → 100644
浏览文件 @
02e1d5ec
// Copyright 2015 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.
// Test a default that references `Self` which is then used in an object type.
// Issue #18956.
#![feature(default_type_params)]
trait
Foo
<
T
=
Self
>
{
fn
method
(
&
self
);
}
fn
foo
(
x
:
&
Foo
)
{
}
//~^ ERROR the type parameter `T` must be explicitly specified
fn
main
()
{
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录