Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
976b3d1d
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,发现更多精彩内容 >>
提交
976b3d1d
编写于
4月 25, 2019
作者:
V
varkor
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Prevent const parameters having type parameters as types
上级
bd31c392
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
52 addition
and
2 deletion
+52
-2
src/librustc_resolve/error_codes.rs
src/librustc_resolve/error_codes.rs
+10
-0
src/librustc_resolve/lib.rs
src/librustc_resolve/lib.rs
+42
-2
未找到文件。
src/librustc_resolve/error_codes.rs
浏览文件 @
976b3d1d
...
@@ -1642,6 +1642,16 @@ fn main() {
...
@@ -1642,6 +1642,16 @@ fn main() {
```
```
"##
,
"##
,
E0671
:
r##"
Const parameters cannot depend on type parameters.
The following is therefore invalid:
```
fn const_id<T, const N: T>() -> T {
N
}
```
"##
,
}
}
register_diagnostics!
{
register_diagnostics!
{
...
...
src/librustc_resolve/lib.rs
浏览文件 @
976b3d1d
...
@@ -185,6 +185,8 @@ enum ResolutionError<'a> {
...
@@ -185,6 +185,8 @@ enum ResolutionError<'a> {
BindingShadowsSomethingUnacceptable
(
&
'a
str
,
Name
,
&
'a
NameBinding
<
'a
>
),
BindingShadowsSomethingUnacceptable
(
&
'a
str
,
Name
,
&
'a
NameBinding
<
'a
>
),
/// Error E0128: type parameters with a default cannot use forward-declared identifiers.
/// Error E0128: type parameters with a default cannot use forward-declared identifiers.
ForwardDeclaredTyParam
,
// FIXME(const_generics:defaults)
ForwardDeclaredTyParam
,
// FIXME(const_generics:defaults)
/// Error E0671: const parameter cannot depend on type parameter.
ConstParamDependentOnTypeParam
,
}
}
/// Combines an error with provided span and emits it.
/// Combines an error with provided span and emits it.
...
@@ -440,6 +442,16 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
...
@@ -440,6 +442,16 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
span
,
"defaulted type parameters cannot be forward declared"
.to_string
());
span
,
"defaulted type parameters cannot be forward declared"
.to_string
());
err
err
}
}
ResolutionError
::
ConstParamDependentOnTypeParam
=>
{
let
mut
err
=
struct_span_err!
(
resolver
.session
,
span
,
E0671
,
"const parameters cannot depend on type parameters"
);
err
.span_label
(
span
,
format!
(
"const parameter depends on type parameter"
));
err
}
}
}
}
}
...
@@ -915,6 +927,18 @@ fn visit_generics(&mut self, generics: &'tcx Generics) {
...
@@ -915,6 +927,18 @@ fn visit_generics(&mut self, generics: &'tcx Generics) {
}
}
}));
}));
// We also ban access to type parameters for use as the types of const parameters.
let
mut
const_ty_param_ban_rib
=
Rib
::
new
(
TyParamAsConstParamTy
);
const_ty_param_ban_rib
.bindings
.extend
(
generics
.params
.iter
()
.filter
(|
param
|
{
if
let
GenericParamKind
::
Type
{
..
}
=
param
.kind
{
true
}
else
{
false
}
})
.map
(|
param
|
(
Ident
::
with_empty_ctxt
(
param
.ident.name
),
Def
::
Err
)));
for
param
in
&
generics
.params
{
for
param
in
&
generics
.params
{
match
param
.kind
{
match
param
.kind
{
GenericParamKind
::
Lifetime
{
..
}
=>
self
.visit_generic_param
(
param
),
GenericParamKind
::
Lifetime
{
..
}
=>
self
.visit_generic_param
(
param
),
...
@@ -933,11 +957,15 @@ fn visit_generics(&mut self, generics: &'tcx Generics) {
...
@@ -933,11 +957,15 @@ fn visit_generics(&mut self, generics: &'tcx Generics) {
default_ban_rib
.bindings
.remove
(
&
Ident
::
with_empty_ctxt
(
param
.ident.name
));
default_ban_rib
.bindings
.remove
(
&
Ident
::
with_empty_ctxt
(
param
.ident.name
));
}
}
GenericParamKind
::
Const
{
ref
ty
}
=>
{
GenericParamKind
::
Const
{
ref
ty
}
=>
{
self
.ribs
[
TypeNS
]
.push
(
const_ty_param_ban_rib
);
for
bound
in
&
param
.bounds
{
for
bound
in
&
param
.bounds
{
self
.visit_param_bound
(
bound
);
self
.visit_param_bound
(
bound
);
}
}
self
.visit_ty
(
ty
);
self
.visit_ty
(
ty
);
const_ty_param_ban_rib
=
self
.ribs
[
TypeNS
]
.pop
()
.unwrap
();
}
}
}
}
}
}
...
@@ -994,6 +1022,9 @@ enum RibKind<'a> {
...
@@ -994,6 +1022,9 @@ enum RibKind<'a> {
/// from the default of a type parameter because they're not declared
/// from the default of a type parameter because they're not declared
/// before said type parameter. Also see the `visit_generics` override.
/// before said type parameter. Also see the `visit_generics` override.
ForwardTyParamBanRibKind
,
ForwardTyParamBanRibKind
,
/// We forbid the use of type parameters as the types of const parameters.
TyParamAsConstParamTy
,
}
}
/// A single local scope.
/// A single local scope.
...
@@ -3944,6 +3975,15 @@ fn adjust_local_def(&mut self,
...
@@ -3944,6 +3975,15 @@ fn adjust_local_def(&mut self,
return
Def
::
Err
;
return
Def
::
Err
;
}
}
// An invalid use of a type parameter as the type of a const parameter.
if
let
TyParamAsConstParamTy
=
self
.ribs
[
ns
][
rib_index
]
.kind
{
if
record_used
{
resolve_error
(
self
,
span
,
ResolutionError
::
ConstParamDependentOnTypeParam
);
}
assert_eq!
(
def
,
Def
::
Err
);
return
Def
::
Err
;
}
match
def
{
match
def
{
Def
::
Upvar
(
..
)
=>
{
Def
::
Upvar
(
..
)
=>
{
span_bug!
(
span
,
"unexpected {:?} in bindings"
,
def
)
span_bug!
(
span
,
"unexpected {:?} in bindings"
,
def
)
...
@@ -3955,7 +3995,7 @@ fn adjust_local_def(&mut self,
...
@@ -3955,7 +3995,7 @@ fn adjust_local_def(&mut self,
for
rib
in
ribs
{
for
rib
in
ribs
{
match
rib
.kind
{
match
rib
.kind
{
NormalRibKind
|
ModuleRibKind
(
..
)
|
MacroDefinition
(
..
)
|
NormalRibKind
|
ModuleRibKind
(
..
)
|
MacroDefinition
(
..
)
|
ForwardTyParamBanRibKind
=>
{
ForwardTyParamBanRibKind
|
TyParamAsConstParamTy
=>
{
// Nothing to do. Continue.
// Nothing to do. Continue.
}
}
ClosureRibKind
(
function_id
)
=>
{
ClosureRibKind
(
function_id
)
=>
{
...
@@ -4013,7 +4053,7 @@ fn adjust_local_def(&mut self,
...
@@ -4013,7 +4053,7 @@ fn adjust_local_def(&mut self,
match
rib
.kind
{
match
rib
.kind
{
NormalRibKind
|
TraitOrImplItemRibKind
|
ClosureRibKind
(
..
)
|
NormalRibKind
|
TraitOrImplItemRibKind
|
ClosureRibKind
(
..
)
|
ModuleRibKind
(
..
)
|
MacroDefinition
(
..
)
|
ForwardTyParamBanRibKind
|
ModuleRibKind
(
..
)
|
MacroDefinition
(
..
)
|
ForwardTyParamBanRibKind
|
ConstantItemRibKind
=>
{
ConstantItemRibKind
|
TyParamAsConstParamTy
=>
{
// Nothing to do. Continue.
// Nothing to do. Continue.
}
}
ItemRibKind
|
FnItemRibKind
=>
{
ItemRibKind
|
FnItemRibKind
=>
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录