Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
2e6bf092
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,发现更多精彩内容 >>
提交
2e6bf092
编写于
7月 02, 2020
作者:
B
Bastian Kauschke
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
const_eval_resolve
上级
58031c7c
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
96 addition
and
28 deletion
+96
-28
src/librustc_codegen_ssa/mir/constant.rs
src/librustc_codegen_ssa/mir/constant.rs
+1
-1
src/librustc_infer/infer/mod.rs
src/librustc_infer/infer/mod.rs
+2
-2
src/librustc_middle/mir/interpret/queries.rs
src/librustc_middle/mir/interpret/queries.rs
+2
-2
src/librustc_middle/query/mod.rs
src/librustc_middle/query/mod.rs
+9
-0
src/librustc_middle/ty/instance.rs
src/librustc_middle/ty/instance.rs
+11
-0
src/librustc_middle/ty/mod.rs
src/librustc_middle/ty/mod.rs
+2
-2
src/librustc_middle/ty/print/pretty.rs
src/librustc_middle/ty/print/pretty.rs
+2
-2
src/librustc_middle/ty/query/keys.rs
src/librustc_middle/ty/query/keys.rs
+11
-0
src/librustc_middle/ty/sty.rs
src/librustc_middle/ty/sty.rs
+1
-1
src/librustc_mir/monomorphize/collector.rs
src/librustc_mir/monomorphize/collector.rs
+1
-1
src/librustc_trait_selection/traits/fulfill.rs
src/librustc_trait_selection/traits/fulfill.rs
+1
-1
src/librustc_trait_selection/traits/select/mod.rs
src/librustc_trait_selection/traits/select/mod.rs
+1
-1
src/librustc_trait_selection/traits/wf.rs
src/librustc_trait_selection/traits/wf.rs
+3
-3
src/librustc_ty/instance.rs
src/librustc_ty/instance.rs
+43
-9
src/librustc_typeck/check/wfcheck.rs
src/librustc_typeck/check/wfcheck.rs
+5
-2
src/tools/clippy/clippy_lints/src/consts.rs
src/tools/clippy/clippy_lints/src/consts.rs
+1
-1
未找到文件。
src/librustc_codegen_ssa/mir/constant.rs
浏览文件 @
2e6bf092
...
...
@@ -28,7 +28,7 @@ pub fn eval_mir_constant(
ty
::
ConstKind
::
Unevaluated
(
def
,
substs
,
promoted
)
=>
self
.cx
.tcx
()
.const_eval_resolve
(
ty
::
ParamEnv
::
reveal_all
(),
def
.did
,
substs
,
promoted
,
None
)
.const_eval_resolve
(
ty
::
ParamEnv
::
reveal_all
(),
def
,
substs
,
promoted
,
None
)
.map_err
(|
err
|
{
if
promoted
.is_none
()
{
self
.cx
...
...
src/librustc_infer/infer/mod.rs
浏览文件 @
2e6bf092
...
...
@@ -1536,7 +1536,7 @@ pub fn create_next_universe(&self) -> ty::UniverseIndex {
pub
fn
const_eval_resolve
(
&
self
,
param_env
:
ty
::
ParamEnv
<
'tcx
>
,
def
_id
:
DefId
,
def
:
ty
::
WithOptParam
<
DefId
>
,
substs
:
SubstsRef
<
'tcx
>
,
promoted
:
Option
<
mir
::
Promoted
>
,
span
:
Option
<
Span
>
,
...
...
@@ -1547,7 +1547,7 @@ pub fn const_eval_resolve(
let
(
param_env
,
substs
)
=
canonical
.value
;
// The return value is the evaluated value which doesn't contain any reference to inference
// variables, thus we don't need to substitute back the original values.
self
.tcx
.const_eval_resolve
(
param_env
,
def
_id
,
substs
,
promoted
,
span
)
self
.tcx
.const_eval_resolve
(
param_env
,
def
,
substs
,
promoted
,
span
)
}
/// If `typ` is a type variable of some kind, resolve it one level
...
...
src/librustc_middle/mir/interpret/queries.rs
浏览文件 @
2e6bf092
...
...
@@ -34,12 +34,12 @@ pub fn const_eval_poly(self, def_id: DefId) -> ConstEvalResult<'tcx> {
pub
fn
const_eval_resolve
(
self
,
param_env
:
ty
::
ParamEnv
<
'tcx
>
,
def
_id
:
DefId
,
def
:
ty
::
WithOptParam
<
DefId
>
,
substs
:
SubstsRef
<
'tcx
>
,
promoted
:
Option
<
mir
::
Promoted
>
,
span
:
Option
<
Span
>
,
)
->
ConstEvalResult
<
'tcx
>
{
match
ty
::
Instance
::
resolve
(
self
,
param_env
,
def_id
,
substs
)
{
match
ty
::
Instance
::
resolve
_const_arg
(
self
,
param_env
,
def
,
substs
)
{
Ok
(
Some
(
instance
))
=>
{
let
cid
=
GlobalId
{
instance
,
promoted
};
self
.const_eval_global_id
(
param_env
,
cid
,
span
)
...
...
src/librustc_middle/query/mod.rs
浏览文件 @
2e6bf092
...
...
@@ -1463,5 +1463,14 @@ fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String {
)
->
Result
<
Option
<
ty
::
Instance
<
'tcx
>>
,
ErrorReported
>
{
desc
{
"resolving instance `{}`"
,
ty
::
Instance
::
new
(
key
.value
.0
,
key
.value
.1
)
}
}
query
resolve_instance_of_const_arg
(
key
:
ty
::
ParamEnvAnd
<
'tcx
,
(
ty
::
WithOptParam
<
DefId
>
,
SubstsRef
<
'tcx
>
)
>
)
->
Result
<
Option
<
ty
::
Instance
<
'tcx
>>
,
ErrorReported
>
{
desc
{
"resolving instance of the potential const argument `{}`"
,
ty
::
Instance
::
new
(
key
.value
.0
.did
,
key
.value
.1
),
}
}
}
}
src/librustc_middle/ty/instance.rs
浏览文件 @
2e6bf092
...
...
@@ -336,6 +336,17 @@ pub fn resolve(
tcx
.resolve_instance
(
tcx
.erase_regions
(
&
param_env
.and
((
def_id
,
substs
))))
}
// This should be kept up to date with `resolve`.
pub
fn
resolve_const_arg
(
tcx
:
TyCtxt
<
'tcx
>
,
param_env
:
ty
::
ParamEnv
<
'tcx
>
,
def
:
ty
::
WithOptParam
<
DefId
>
,
substs
:
SubstsRef
<
'tcx
>
,
)
->
Result
<
Option
<
Instance
<
'tcx
>>
,
ErrorReported
>
{
let
substs
=
tcx
.erase_regions
(
&
substs
);
tcx
.resolve_instance_of_const_arg
(
tcx
.erase_regions
(
&
param_env
.and
((
def
,
substs
))))
}
pub
fn
resolve_for_fn_ptr
(
tcx
:
TyCtxt
<
'tcx
>
,
param_env
:
ty
::
ParamEnv
<
'tcx
>
,
...
...
src/librustc_middle/ty/mod.rs
浏览文件 @
2e6bf092
...
...
@@ -1100,7 +1100,7 @@ pub enum PredicateKind<'tcx> {
Subtype
(
PolySubtypePredicate
<
'tcx
>
),
/// Constant initializer must evaluate successfully.
ConstEvaluatable
(
DefId
,
SubstsRef
<
'tcx
>
),
ConstEvaluatable
(
ty
::
WithOptParam
<
DefId
>
,
SubstsRef
<
'tcx
>
),
/// Constants must be equal. The first component is the const that is expected.
ConstEquate
(
&
'tcx
Const
<
'tcx
>
,
&
'tcx
Const
<
'tcx
>
),
...
...
@@ -1571,7 +1571,7 @@ fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHas
pub
type
PlaceholderConst
=
Placeholder
<
BoundVar
>
;
#[derive(Copy,
Clone,
Debug,
RustcEncodable,
RustcDecodable)]
#[derive(Copy,
Clone,
Debug,
TypeFoldable,
Lift,
RustcEncodable,
RustcDecodable)]
#[derive(PartialEq,
Eq,
PartialOrd,
Ord)]
#[derive(Hash,
HashStable)]
pub
struct
WithOptParam
<
T
>
{
...
...
src/librustc_middle/ty/print/pretty.rs
浏览文件 @
2e6bf092
...
...
@@ -2027,9 +2027,9 @@ pub fn print_only_trait_path(self) -> ty::Binder<TraitRefPrintOnlyTraitPath<'tcx
print_value_path
(
closure_def_id
,
&
[]),
write
(
"` implements the trait `{}`"
,
kind
))
}
&
ty
::
PredicateKind
::
ConstEvaluatable
(
def
_id
,
substs
)
=>
{
&
ty
::
PredicateKind
::
ConstEvaluatable
(
def
,
substs
)
=>
{
p!
(
write
(
"the constant `"
),
print_value_path
(
def
_
id
,
substs
),
print_value_path
(
def
.d
id
,
substs
),
write
(
"` can be evaluated"
))
}
ty
::
PredicateKind
::
ConstEquate
(
c1
,
c2
)
=>
{
...
...
src/librustc_middle/ty/query/keys.rs
浏览文件 @
2e6bf092
...
...
@@ -171,6 +171,17 @@ fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
}
}
impl
<
'tcx
>
Key
for
(
ty
::
WithOptParam
<
DefId
>
,
SubstsRef
<
'tcx
>
)
{
type
CacheSelector
=
DefaultCacheSelector
;
fn
query_crate
(
&
self
)
->
CrateNum
{
LOCAL_CRATE
}
fn
default_span
(
&
self
,
tcx
:
TyCtxt
<
'_
>
)
->
Span
{
self
.0
.did
.default_span
(
tcx
)
}
}
impl
<
'tcx
>
Key
for
(
ty
::
ParamEnv
<
'tcx
>
,
ty
::
PolyTraitRef
<
'tcx
>
)
{
type
CacheSelector
=
DefaultCacheSelector
;
...
...
src/librustc_middle/ty/sty.rs
浏览文件 @
2e6bf092
...
...
@@ -2373,7 +2373,7 @@ pub fn eval(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> &Const<'tcx>
let
(
param_env
,
substs
)
=
param_env_and_substs
.into_parts
();
// try to resolve e.g. associated constants to their definition on an impl, and then
// evaluate the const.
match
tcx
.const_eval_resolve
(
param_env
,
def
.did
,
substs
,
promoted
,
None
)
{
match
tcx
.const_eval_resolve
(
param_env
,
def
,
substs
,
promoted
,
None
)
{
// NOTE(eddyb) `val` contains no lifetimes/types/consts,
// and we use the original type, so nothing from `substs`
// (which may be identity substs, see above),
...
...
src/librustc_mir/monomorphize/collector.rs
浏览文件 @
2e6bf092
...
...
@@ -623,7 +623,7 @@ fn visit_const(&mut self, constant: &&'tcx ty::Const<'tcx>, location: Location)
match
substituted_constant
.val
{
ty
::
ConstKind
::
Value
(
val
)
=>
collect_const_value
(
self
.tcx
,
val
,
self
.output
),
ty
::
ConstKind
::
Unevaluated
(
def
,
substs
,
promoted
)
=>
{
match
self
.tcx
.const_eval_resolve
(
param_env
,
def
.did
,
substs
,
promoted
,
None
)
{
match
self
.tcx
.const_eval_resolve
(
param_env
,
def
,
substs
,
promoted
,
None
)
{
Ok
(
val
)
=>
collect_const_value
(
self
.tcx
,
val
,
self
.output
),
Err
(
ErrorHandled
::
Reported
(
ErrorReported
)
|
ErrorHandled
::
Linted
)
=>
{}
Err
(
ErrorHandled
::
TooGeneric
)
=>
span_bug!
(
...
...
src/librustc_trait_selection/traits/fulfill.rs
浏览文件 @
2e6bf092
...
...
@@ -527,7 +527,7 @@ fn process_obligation(
if
let
ty
::
ConstKind
::
Unevaluated
(
def
,
substs
,
promoted
)
=
c
.val
{
match
self
.selcx
.infcx
()
.const_eval_resolve
(
obligation
.param_env
,
def
.did
,
def
,
substs
,
promoted
,
Some
(
obligation
.cause.span
),
...
...
src/librustc_trait_selection/traits/select/mod.rs
浏览文件 @
2e6bf092
...
...
@@ -511,7 +511,7 @@ fn evaluate_predicate_recursively<'o>(
self
.infcx
.const_eval_resolve
(
obligation
.param_env
,
def
.did
,
def
,
substs
,
promoted
,
Some
(
obligation
.cause.span
),
...
...
src/librustc_trait_selection/traits/wf.rs
浏览文件 @
2e6bf092
...
...
@@ -116,8 +116,8 @@ pub fn predicate_obligations<'a, 'tcx>(
wf
.compute
(
data
.skip_binder
()
.a
.into
());
// (*)
wf
.compute
(
data
.skip_binder
()
.b
.into
());
// (*)
}
&
ty
::
PredicateKind
::
ConstEvaluatable
(
def
_id
,
substs
)
=>
{
let
obligations
=
wf
.nominal_obligations
(
def
_
id
,
substs
);
&
ty
::
PredicateKind
::
ConstEvaluatable
(
def
,
substs
)
=>
{
let
obligations
=
wf
.nominal_obligations
(
def
.d
id
,
substs
);
wf
.out
.extend
(
obligations
);
for
arg
in
substs
.iter
()
{
...
...
@@ -365,7 +365,7 @@ fn compute(&mut self, arg: GenericArg<'tcx>) {
let
obligations
=
self
.nominal_obligations
(
def
.did
,
substs
);
self
.out
.extend
(
obligations
);
let
predicate
=
ty
::
PredicateKind
::
ConstEvaluatable
(
def
.did
,
substs
)
let
predicate
=
ty
::
PredicateKind
::
ConstEvaluatable
(
def
,
substs
)
.to_predicate
(
self
.tcx
());
let
cause
=
self
.cause
(
traits
::
MiscObligation
);
self
.out
.push
(
traits
::
Obligation
::
new
(
...
...
src/librustc_ty/instance.rs
浏览文件 @
2e6bf092
...
...
@@ -14,15 +14,48 @@ fn resolve_instance<'tcx>(
tcx
:
TyCtxt
<
'tcx
>
,
key
:
ty
::
ParamEnvAnd
<
'tcx
,
(
DefId
,
SubstsRef
<
'tcx
>
)
>
,
)
->
Result
<
Option
<
Instance
<
'tcx
>>
,
ErrorReported
>
{
let
(
param_env
,
(
def_id
,
substs
))
=
key
.into_parts
();
let
(
param_env
,
(
did
,
substs
))
=
key
.into_parts
();
if
let
param_did
@
Some
(
_
)
=
did
.as_local
()
.and_then
(|
did
|
tcx
.opt_const_param_of
(
did
))
{
tcx
.resolve_instance_of_const_arg
(
param_env
.and
((
ty
::
WithOptParam
{
did
,
param_did
},
substs
)),
)
}
else
{
inner_resolve_instance
(
tcx
,
param_env
.and
((
ty
::
WithOptParam
::
dummy
(
did
),
substs
)))
}
}
fn
resolve_instance_of_const_arg
<
'tcx
>
(
tcx
:
TyCtxt
<
'tcx
>
,
key
:
ty
::
ParamEnvAnd
<
'tcx
,
(
ty
::
WithOptParam
<
DefId
>
,
SubstsRef
<
'tcx
>
)
>
,
)
->
Result
<
Option
<
Instance
<
'tcx
>>
,
ErrorReported
>
{
let
(
param_env
,
(
def
,
substs
))
=
key
.into_parts
();
if
def
.param_did
.is_none
()
{
if
let
Some
(
did
)
=
def
.did
.as_local
()
{
if
let
param_did
@
Some
(
_
)
=
tcx
.opt_const_param_of
(
did
)
{
return
tcx
.resolve_instance_of_const_arg
(
param_env
.and
((
ty
::
WithOptParam
{
param_did
,
..
def
},
substs
)),
);
}
}
tcx
.resolve_instance
(
param_env
.and
((
def
.did
,
substs
)))
}
else
{
inner_resolve_instance
(
tcx
,
param_env
.and
((
def
,
substs
)))
}
}
fn
inner_resolve_instance
<
'tcx
>
(
tcx
:
TyCtxt
<
'tcx
>
,
key
:
ty
::
ParamEnvAnd
<
'tcx
,
(
ty
::
WithOptParam
<
DefId
>
,
SubstsRef
<
'tcx
>
)
>
,
)
->
Result
<
Option
<
Instance
<
'tcx
>>
,
ErrorReported
>
{
let
(
param_env
,
(
def
,
substs
))
=
key
.into_parts
();
debug!
(
"resolve(def
_id={:?}, substs={:?})"
,
def_
id
,
substs
);
let
result
=
if
let
Some
(
trait_def_id
)
=
tcx
.trait_of_item
(
def
_
id
)
{
debug!
(
"resolve(def
={:?}, substs={:?})"
,
def
.d
id
,
substs
);
let
result
=
if
let
Some
(
trait_def_id
)
=
tcx
.trait_of_item
(
def
.d
id
)
{
debug!
(
" => associated item, attempting to find impl in param_env {:#?}"
,
param_env
);
let
item
=
tcx
.associated_item
(
def
_
id
);
let
item
=
tcx
.associated_item
(
def
.d
id
);
resolve_associated_item
(
tcx
,
&
item
,
param_env
,
trait_def_id
,
substs
)
}
else
{
let
ty
=
tcx
.type_of
(
def
_id
);
let
ty
=
tcx
.type_of
(
def
.ty_def_id
()
);
let
item_type
=
tcx
.subst_and_normalize_erasing_regions
(
substs
,
param_env
,
&
ty
);
let
def
=
match
item_type
.kind
{
...
...
@@ -33,7 +66,7 @@ fn resolve_instance<'tcx>(
}
=>
{
debug!
(
" => intrinsic"
);
ty
::
InstanceDef
::
Intrinsic
(
def
_
id
)
ty
::
InstanceDef
::
Intrinsic
(
def
.d
id
)
}
ty
::
FnDef
(
def_id
,
substs
)
if
Some
(
def_id
)
==
tcx
.lang_items
()
.drop_in_place_fn
()
=>
{
let
ty
=
substs
.type_at
(
0
);
...
...
@@ -53,12 +86,12 @@ fn resolve_instance<'tcx>(
}
_
=>
{
debug!
(
" => free item"
);
ty
::
InstanceDef
::
Item
(
def
_
id
)
ty
::
InstanceDef
::
Item
(
def
.d
id
)
}
};
Ok
(
Some
(
Instance
{
def
,
substs
}))
};
debug!
(
"resolve(def
_id={:?}, substs={:?}) = {:?}"
,
def_
id
,
substs
,
result
);
debug!
(
"resolve(def
.did={:?}, substs={:?}) = {:?}"
,
def
.d
id
,
substs
,
result
);
result
}
...
...
@@ -244,5 +277,6 @@ fn resolve_associated_item<'tcx>(
}
pub
fn
provide
(
providers
:
&
mut
ty
::
query
::
Providers
)
{
*
providers
=
ty
::
query
::
Providers
{
resolve_instance
,
..*
providers
};
*
providers
=
ty
::
query
::
Providers
{
resolve_instance
,
resolve_instance_of_const_arg
,
..*
providers
};
}
src/librustc_typeck/check/wfcheck.rs
浏览文件 @
2e6bf092
...
...
@@ -423,8 +423,11 @@ fn check_type_defn<'tcx, F>(
fcx
.register_predicate
(
traits
::
Obligation
::
new
(
cause
,
fcx
.param_env
,
ty
::
PredicateKind
::
ConstEvaluatable
(
discr_def_id
.to_def_id
(),
discr_substs
)
.to_predicate
(
fcx
.tcx
),
ty
::
PredicateKind
::
ConstEvaluatable
(
ty
::
WithOptParam
::
dummy
(
discr_def_id
.to_def_id
()),
discr_substs
,
)
.to_predicate
(
fcx
.tcx
),
));
}
}
...
...
src/tools/clippy/clippy_lints/src/consts.rs
浏览文件 @
2e6bf092
...
...
@@ -332,7 +332,7 @@ fn fetch_path(&mut self, qpath: &QPath<'_>, id: HirId, ty: Ty<'tcx>) -> Option<C
let
result
=
self
.lcx
.tcx
.const_eval_resolve
(
self
.param_env
,
def_id
,
substs
,
None
,
None
)
.const_eval_resolve
(
self
.param_env
,
ty
::
WithOptParam
::
dummy
(
def_id
)
,
substs
,
None
,
None
)
.ok
()
.map
(|
val
|
rustc_middle
::
ty
::
Const
::
from_value
(
self
.lcx.tcx
,
val
,
ty
))
?
;
let
result
=
miri_to_const
(
&
result
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录