Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
f8b796b1
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,发现更多精彩内容 >>
提交
f8b796b1
编写于
4月 20, 2020
作者:
V
varkor
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add error message for using type parameter as the type of a const parameter
上级
3eea7b31
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
49 addition
and
19 deletion
+49
-19
src/librustc_typeck/collect/type_of.rs
src/librustc_typeck/collect/type_of.rs
+39
-13
src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.rs
...enerics/const-param-type-depends-on-type-param-ungated.rs
+1
-1
src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr
...ics/const-param-type-depends-on-type-param-ungated.stderr
+4
-2
src/test/ui/const-generics/const-param-type-depends-on-type-param.rs
.../const-generics/const-param-type-depends-on-type-param.rs
+1
-1
src/test/ui/const-generics/const-param-type-depends-on-type-param.stderr
...st-generics/const-param-type-depends-on-type-param.stderr
+4
-2
未找到文件。
src/librustc_typeck/collect/type_of.rs
浏览文件 @
f8b796b1
...
...
@@ -342,12 +342,37 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
if
traits
::
search_for_structural_match_violation
(
param
.hir_id
,
param
.span
,
tcx
,
ty
)
.is_some
()
{
// We use the same error code in both branches, because this is really the same
// issue: we just special-case the message for type parameters to make it
// clearer.
if
let
ty
::
Param
(
_
)
=
ty
.peel_refs
()
.kind
{
// Const parameters may not have type parameters as their types,
// because we cannot be sure that the type parameter derives `PartialEq`
// and `Eq` (just implementing them is not enough for `structural_match`).
struct_span_err!
(
tcx
.sess
,
hir_ty
.span
,
E0741
,
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as the
\
type of a const parameter"
,
"`{}` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be
\
used as the type of a const parameter"
,
ty
,
)
.span_label
(
hir_ty
.span
,
format!
(
"`{}` may not derive both `PartialEq` and `Eq`"
,
ty
),
)
.note
(
"it is not currently possible to use a type parameter as the type of a
\
const parameter"
,
)
.emit
();
}
else
{
struct_span_err!
(
tcx
.sess
,
hir_ty
.span
,
E0741
,
"`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as
\
the type of a const parameter"
,
ty
,
)
.span_label
(
...
...
@@ -356,6 +381,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
)
.emit
();
}
}
ty
}
x
=>
bug!
(
"unexpected non-type Node::GenericParam: {:?}"
,
x
),
...
...
src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.rs
浏览文件 @
f8b796b1
use
std
::
marker
::
PhantomData
;
struct
B
<
T
,
const
N
:
T
>
(
PhantomData
<
[
T
;
N
]
>
);
//~ ERROR const generics are unstable
//~^ ERROR `T`
must be annotated with
`#[derive(PartialEq, Eq)]`
//~^ ERROR `T`
is not guaranteed to
`#[derive(PartialEq, Eq)]`
fn
main
()
{}
src/test/ui/const-generics/const-param-type-depends-on-type-param-ungated.stderr
浏览文件 @
f8b796b1
...
...
@@ -7,11 +7,13 @@ LL | struct B<T, const N: T>(PhantomData<[T; N]>);
= note: see issue #44580 <https://github.com/rust-lang/rust/issues/44580> for more information
= help: add `#![feature(const_generics)]` to the crate attributes to enable
error[E0741]: `T`
must be annotated with `#[derive(PartialEq, Eq)]` to
be used as the type of a const parameter
error[E0741]: `T`
is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not
be used as the type of a const parameter
--> $DIR/const-param-type-depends-on-type-param-ungated.rs:3:22
|
LL | struct B<T, const N: T>(PhantomData<[T; N]>);
| ^ `T` doesn't derive both `PartialEq` and `Eq`
| ^ `T` may not derive both `PartialEq` and `Eq`
|
= note: it is not currently possible to use a type parameter as the type of a const parameter
error: aborting due to 2 previous errors
...
...
src/test/ui/const-generics/const-param-type-depends-on-type-param.rs
浏览文件 @
f8b796b1
...
...
@@ -7,6 +7,6 @@
// details.
pub
struct
Dependent
<
T
,
const
X
:
T
>
([();
X
]);
//~^ ERROR `T`
must be annotated with
`#[derive(PartialEq, Eq)]`
//~^ ERROR `T`
is not guaranteed to
`#[derive(PartialEq, Eq)]`
fn
main
()
{}
src/test/ui/const-generics/const-param-type-depends-on-type-param.stderr
浏览文件 @
f8b796b1
...
...
@@ -6,11 +6,13 @@ LL | #![feature(const_generics)]
|
= note: `#[warn(incomplete_features)]` on by default
error[E0741]: `T`
must be annotated with `#[derive(PartialEq, Eq)]` to
be used as the type of a const parameter
error[E0741]: `T`
is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not
be used as the type of a const parameter
--> $DIR/const-param-type-depends-on-type-param.rs:9:34
|
LL | pub struct Dependent<T, const X: T>([(); X]);
| ^ `T` doesn't derive both `PartialEq` and `Eq`
| ^ `T` may not derive both `PartialEq` and `Eq`
|
= note: it is not currently possible to use a type parameter as the type of a const parameter
error: aborting due to previous error; 1 warning emitted
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录