Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
0078e541
R
Rust
项目概览
int
/
Rust
12 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
0078e541
编写于
3月 25, 2022
作者:
B
b-naber
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rebase and use ty::Const in patterns again
上级
ac60db23
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
73 addition
and
144 deletion
+73
-144
compiler/rustc_const_eval/src/const_eval/mod.rs
compiler/rustc_const_eval/src/const_eval/mod.rs
+3
-3
compiler/rustc_middle/src/mir/mod.rs
compiler/rustc_middle/src/mir/mod.rs
+1
-1
compiler/rustc_middle/src/thir.rs
compiler/rustc_middle/src/thir.rs
+2
-6
compiler/rustc_middle/src/thir/visit.rs
compiler/rustc_middle/src/thir/visit.rs
+0
-1
compiler/rustc_middle/src/ty/consts/kind.rs
compiler/rustc_middle/src/ty/consts/kind.rs
+1
-5
compiler/rustc_mir_build/src/build/expr/as_constant.rs
compiler/rustc_mir_build/src/build/expr/as_constant.rs
+3
-61
compiler/rustc_mir_build/src/thir/constant.rs
compiler/rustc_mir_build/src/thir/constant.rs
+50
-0
compiler/rustc_mir_build/src/thir/cx/mod.rs
compiler/rustc_mir_build/src/thir/cx/mod.rs
+2
-1
compiler/rustc_trait_selection/src/traits/const_evaluatable.rs
...ler/rustc_trait_selection/src/traits/const_evaluatable.rs
+0
-52
compiler/rustc_trait_selection/src/traits/fulfill.rs
compiler/rustc_trait_selection/src/traits/fulfill.rs
+1
-1
src/tools/clippy/clippy_lints/src/matches/overlapping_arms.rs
...tools/clippy/clippy_lints/src/matches/overlapping_arms.rs
+7
-10
src/tools/clippy/clippy_lints/src/neg_multiply.rs
src/tools/clippy/clippy_lints/src/neg_multiply.rs
+1
-1
src/tools/clippy/clippy_utils/src/consts.rs
src/tools/clippy/clippy_utils/src/consts.rs
+2
-2
未找到文件。
compiler/rustc_const_eval/src/const_eval/mod.rs
浏览文件 @
0078e541
...
...
@@ -184,13 +184,13 @@ pub(crate) fn destructure_mir_constant<'tcx>(
// We go to `usize` as we cannot allocate anything bigger anyway.
let
(
field_count
,
variant
,
down
)
=
match
val
.ty
()
.kind
()
{
ty
::
Array
(
_
,
len
)
=>
(
usize
::
try_from
(
len
.eval_usize
(
tcx
,
param_env
))
.unwrap
(),
None
,
op
),
ty
::
Adt
(
def
,
_
)
if
def
.variants
.is_empty
()
=>
{
ty
::
Adt
(
def
,
_
)
if
def
.variants
()
.is_empty
()
=>
{
return
mir
::
DestructuredMirConstant
{
variant
:
None
,
fields
:
&
[]
};
}
ty
::
Adt
(
def
,
_
)
=>
{
let
variant
=
ecx
.read_discriminant
(
&
op
)
.unwrap
()
.1
;
let
down
=
ecx
.operand_downcast
(
&
op
,
variant
)
.unwrap
();
(
def
.variants
[
variant
]
.fields
.len
(),
Some
(
variant
),
down
)
(
def
.variants
()
[
variant
]
.fields
.len
(),
Some
(
variant
),
down
)
}
ty
::
Tuple
(
substs
)
=>
(
substs
.len
(),
None
,
op
),
_
=>
bug!
(
"cannot destructure constant {:?}"
,
val
),
...
...
@@ -253,7 +253,7 @@ pub(crate) fn deref_mir_constant<'tcx>(
let
mplace
=
ecx
.deref_operand
(
&
op
)
.unwrap
();
if
let
Some
(
alloc_id
)
=
mplace
.ptr.provenance
{
assert_eq!
(
tcx
.get_global_alloc
(
alloc_id
)
.unwrap
()
.unwrap_memory
()
.mutability
,
tcx
.get_global_alloc
(
alloc_id
)
.unwrap
()
.unwrap_memory
()
.
0.0
.
mutability
,
Mutability
::
Not
,
"deref_const cannot be used with mutable allocations as
\
that could allow pattern matching to observe mutable statics"
,
...
...
compiler/rustc_middle/src/mir/mod.rs
浏览文件 @
0078e541
...
...
@@ -2711,7 +2711,7 @@ pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
if
let
Some
(
val
)
=
c
.val
()
.try_eval
(
tcx
,
param_env
)
{
match
val
{
Ok
(
val
)
=>
Self
::
Val
(
val
,
c
.ty
()),
Err
(
ErrorReported
)
=>
Self
::
Ty
(
tcx
.const_error
(
self
.ty
())),
Err
(
_
)
=>
Self
::
Ty
(
tcx
.const_error
(
self
.ty
())),
}
}
else
{
self
...
...
compiler/rustc_middle/src/thir.rs
浏览文件 @
0078e541
...
...
@@ -193,7 +193,7 @@ pub enum StmtKind<'tcx> {
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch
=
"x86_64"
,
target_pointer_width
=
"64"
))]
rustc_data_structures
::
static_assert_size!
(
Expr
<
'_
>
,
1
4
4
);
rustc_data_structures
::
static_assert_size!
(
Expr
<
'_
>
,
1
0
4
);
/// A THIR expression.
#[derive(Debug,
HashStable)]
...
...
@@ -736,11 +736,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Some
(
adt_def
.variant
(
variant_index
))
}
_
=>
self
.ty
.ty_adt_def
()
.and_then
(|
adt
|
{
if
!
adt
.is_enum
()
{
Some
(
adt
.non_enum_variant
())
}
else
{
None
}
if
!
adt
.is_enum
()
{
Some
(
adt
.non_enum_variant
())
}
else
{
None
}
}),
};
...
...
compiler/rustc_middle/src/thir/visit.rs
浏览文件 @
0078e541
...
...
@@ -2,7 +2,6 @@
Arm
,
Block
,
Expr
,
ExprKind
,
Guard
,
InlineAsmOperand
,
Pat
,
PatKind
,
Stmt
,
StmtKind
,
Thir
,
};
use
crate
::
mir
::
ConstantKind
;
use
crate
::
ty
::
Const
;
pub
trait
Visitor
<
'a
,
'tcx
:
'a
>
:
Sized
{
fn
thir
(
&
self
)
->
&
'a
Thir
<
'tcx
>
;
...
...
compiler/rustc_middle/src/ty/consts/kind.rs
浏览文件 @
0078e541
...
...
@@ -76,11 +76,7 @@ pub enum ConstKind<'tcx> {
impl
<
'tcx
>
ConstKind
<
'tcx
>
{
#[inline]
pub
fn
try_to_value
(
self
)
->
Option
<
ConstValue
<
'tcx
>>
{
if
let
ConstKind
::
Value
(
val
)
=
self
{
Some
(
val
)
}
else
{
None
}
if
let
ConstKind
::
Value
(
val
)
=
self
{
Some
(
val
)
}
else
{
None
}
}
#[inline]
...
...
compiler/rustc_mir_build/src/build/expr/as_constant.rs
浏览文件 @
0078e541
//! See docs in build/expr/mod.rs
use
crate
::
build
::
Builder
;
use
crate
::
thir
::
constant
::
parse_float
;
use
rustc_ast
::
ast
;
use
rustc_hir
::
def_id
::
DefId
;
use
rustc_middle
::
mir
::
interpret
::{
Allocation
,
ConstValue
,
LitToConstError
,
LitToConstInput
,
Scalar
,
};
use
rustc_middle
::
mir
::
interpret
::{
ConstValue
,
LitToConstError
,
LitToConstInput
,
Scalar
};
use
rustc_middle
::
mir
::
*
;
use
rustc_middle
::
thir
::
*
;
use
rustc_middle
::
ty
::
subst
::
SubstsRef
;
use
rustc_middle
::
ty
::{
self
,
CanonicalUserTypeAnnotation
,
Ty
,
TyCtxt
};
use
rustc_target
::
abi
::
Size
;
impl
<
'a
,
'tcx
>
Builder
<
'a
,
'tcx
>
{
/// Compile `expr`, yielding a compile-time constant. Assumes that
...
...
@@ -32,11 +27,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
ExprKind
::
Literal
{
lit
,
neg
}
=>
{
let
literal
=
match
lit_to_constant
(
tcx
,
LitToConstInput
{
lit
:
&
lit
.node
,
ty
,
neg
})
{
match
tcx
.lit_to_mir_constant
(
LitToConstInput
{
lit
:
&
lit
.node
,
ty
,
neg
})
{
Ok
(
c
)
=>
c
,
Err
(
LitToConstError
::
Reported
)
=>
ConstantKind
::
Ty
(
tcx
.const_error
(
ty
)),
Err
(
LitToConstError
::
TypeError
)
=>
{
bug!
(
"encountered type error in `lit_to_constant"
)
bug!
(
"encountered type error in `lit_to_
mir_
constant"
)
}
};
...
...
@@ -85,60 +80,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
Constant
{
span
,
user_ty
:
None
,
literal
}
}
ExprKind
::
ConstBlock
{
value
}
=>
{
Constant
{
span
:
span
,
user_ty
:
None
,
literal
:
value
}
}
_
=>
span_bug!
(
span
,
"expression is not a valid constant {:?}"
,
kind
),
}
}
}
crate
fn
lit_to_constant
<
'tcx
>
(
tcx
:
TyCtxt
<
'tcx
>
,
lit_input
:
LitToConstInput
<
'tcx
>
,
)
->
Result
<
ConstantKind
<
'tcx
>
,
LitToConstError
>
{
let
LitToConstInput
{
lit
,
ty
,
neg
}
=
lit_input
;
let
trunc
=
|
n
|
{
let
param_ty
=
ty
::
ParamEnv
::
reveal_all
()
.and
(
ty
);
let
width
=
tcx
.layout_of
(
param_ty
)
.map_err
(|
_
|
LitToConstError
::
Reported
)
?
.size
;
trace!
(
"trunc {} with size {} and shift {}"
,
n
,
width
.bits
(),
128
-
width
.bits
());
let
result
=
width
.truncate
(
n
);
trace!
(
"trunc result: {}"
,
result
);
Ok
(
ConstValue
::
Scalar
(
Scalar
::
from_uint
(
result
,
width
)))
};
let
value
=
match
(
lit
,
&
ty
.kind
())
{
(
ast
::
LitKind
::
Str
(
s
,
_
),
ty
::
Ref
(
_
,
inner_ty
,
_
))
if
inner_ty
.is_str
()
=>
{
let
s
=
s
.as_str
();
let
allocation
=
Allocation
::
from_bytes_byte_aligned_immutable
(
s
.as_bytes
());
let
allocation
=
tcx
.intern_const_alloc
(
allocation
);
ConstValue
::
Slice
{
data
:
allocation
,
start
:
0
,
end
:
s
.len
()
}
}
(
ast
::
LitKind
::
ByteStr
(
data
),
ty
::
Ref
(
_
,
inner_ty
,
_
))
if
matches!
(
inner_ty
.kind
(),
ty
::
Slice
(
_
))
=>
{
let
allocation
=
Allocation
::
from_bytes_byte_aligned_immutable
(
data
as
&
[
u8
]);
let
allocation
=
tcx
.intern_const_alloc
(
allocation
);
ConstValue
::
Slice
{
data
:
allocation
,
start
:
0
,
end
:
data
.len
()
}
}
(
ast
::
LitKind
::
ByteStr
(
data
),
ty
::
Ref
(
_
,
inner_ty
,
_
))
if
inner_ty
.is_array
()
=>
{
let
id
=
tcx
.allocate_bytes
(
data
);
ConstValue
::
Scalar
(
Scalar
::
from_pointer
(
id
.into
(),
&
tcx
))
}
(
ast
::
LitKind
::
Byte
(
n
),
ty
::
Uint
(
ty
::
UintTy
::
U8
))
=>
{
ConstValue
::
Scalar
(
Scalar
::
from_uint
(
*
n
,
Size
::
from_bytes
(
1
)))
}
(
ast
::
LitKind
::
Int
(
n
,
_
),
ty
::
Uint
(
_
))
|
(
ast
::
LitKind
::
Int
(
n
,
_
),
ty
::
Int
(
_
))
=>
{
trunc
(
if
neg
{
(
*
n
as
i128
)
.overflowing_neg
()
.0
as
u128
}
else
{
*
n
})
?
}
(
ast
::
LitKind
::
Float
(
n
,
_
),
ty
::
Float
(
fty
))
=>
{
parse_float
(
*
n
,
*
fty
,
neg
)
.ok_or
(
LitToConstError
::
Reported
)
?
}
(
ast
::
LitKind
::
Bool
(
b
),
ty
::
Bool
)
=>
ConstValue
::
Scalar
(
Scalar
::
from_bool
(
*
b
)),
(
ast
::
LitKind
::
Char
(
c
),
ty
::
Char
)
=>
ConstValue
::
Scalar
(
Scalar
::
from_char
(
*
c
)),
(
ast
::
LitKind
::
Err
(
_
),
_
)
=>
return
Err
(
LitToConstError
::
Reported
),
_
=>
return
Err
(
LitToConstError
::
TypeError
),
};
Ok
(
ConstantKind
::
Val
(
value
,
ty
))
}
compiler/rustc_mir_build/src/thir/constant.rs
浏览文件 @
0078e541
...
...
@@ -59,6 +59,56 @@
Ok
(
ty
::
Const
::
from_value
(
tcx
,
lit
,
ty
))
}
crate
fn
lit_to_mir_constant
<
'tcx
>
(
tcx
:
TyCtxt
<
'tcx
>
,
lit_input
:
LitToConstInput
<
'tcx
>
,
)
->
Result
<
ConstantKind
<
'tcx
>
,
LitToConstError
>
{
let
LitToConstInput
{
lit
,
ty
,
neg
}
=
lit_input
;
let
trunc
=
|
n
|
{
let
param_ty
=
ty
::
ParamEnv
::
reveal_all
()
.and
(
ty
);
let
width
=
tcx
.layout_of
(
param_ty
)
.map_err
(|
_
|
LitToConstError
::
Reported
)
?
.size
;
trace!
(
"trunc {} with size {} and shift {}"
,
n
,
width
.bits
(),
128
-
width
.bits
());
let
result
=
width
.truncate
(
n
);
trace!
(
"trunc result: {}"
,
result
);
Ok
(
ConstValue
::
Scalar
(
Scalar
::
from_uint
(
result
,
width
)))
};
let
value
=
match
(
lit
,
&
ty
.kind
())
{
(
ast
::
LitKind
::
Str
(
s
,
_
),
ty
::
Ref
(
_
,
inner_ty
,
_
))
if
inner_ty
.is_str
()
=>
{
let
s
=
s
.as_str
();
let
allocation
=
Allocation
::
from_bytes_byte_aligned_immutable
(
s
.as_bytes
());
let
allocation
=
tcx
.intern_const_alloc
(
allocation
);
ConstValue
::
Slice
{
data
:
allocation
,
start
:
0
,
end
:
s
.len
()
}
}
(
ast
::
LitKind
::
ByteStr
(
data
),
ty
::
Ref
(
_
,
inner_ty
,
_
))
if
matches!
(
inner_ty
.kind
(),
ty
::
Slice
(
_
))
=>
{
let
allocation
=
Allocation
::
from_bytes_byte_aligned_immutable
(
data
as
&
[
u8
]);
let
allocation
=
tcx
.intern_const_alloc
(
allocation
);
ConstValue
::
Slice
{
data
:
allocation
,
start
:
0
,
end
:
data
.len
()
}
}
(
ast
::
LitKind
::
ByteStr
(
data
),
ty
::
Ref
(
_
,
inner_ty
,
_
))
if
inner_ty
.is_array
()
=>
{
let
id
=
tcx
.allocate_bytes
(
data
);
ConstValue
::
Scalar
(
Scalar
::
from_pointer
(
id
.into
(),
&
tcx
))
}
(
ast
::
LitKind
::
Byte
(
n
),
ty
::
Uint
(
ty
::
UintTy
::
U8
))
=>
{
ConstValue
::
Scalar
(
Scalar
::
from_uint
(
*
n
,
Size
::
from_bytes
(
1
)))
}
(
ast
::
LitKind
::
Int
(
n
,
_
),
ty
::
Uint
(
_
))
|
(
ast
::
LitKind
::
Int
(
n
,
_
),
ty
::
Int
(
_
))
=>
{
trunc
(
if
neg
{
(
*
n
as
i128
)
.overflowing_neg
()
.0
as
u128
}
else
{
*
n
})
?
}
(
ast
::
LitKind
::
Float
(
n
,
_
),
ty
::
Float
(
fty
))
=>
{
parse_float
(
*
n
,
*
fty
,
neg
)
.ok_or
(
LitToConstError
::
Reported
)
?
}
(
ast
::
LitKind
::
Bool
(
b
),
ty
::
Bool
)
=>
ConstValue
::
Scalar
(
Scalar
::
from_bool
(
*
b
)),
(
ast
::
LitKind
::
Char
(
c
),
ty
::
Char
)
=>
ConstValue
::
Scalar
(
Scalar
::
from_char
(
*
c
)),
(
ast
::
LitKind
::
Err
(
_
),
_
)
=>
return
Err
(
LitToConstError
::
Reported
),
_
=>
return
Err
(
LitToConstError
::
TypeError
),
};
Ok
(
ConstantKind
::
Val
(
value
,
ty
))
}
// FIXME move this to rustc_mir_build::build
pub
(
crate
)
fn
parse_float
<
'tcx
>
(
num
:
Symbol
,
...
...
compiler/rustc_mir_build/src/thir/cx/mod.rs
浏览文件 @
0078e541
...
...
@@ -5,6 +5,7 @@
use
crate
::
thir
::
pattern
::
pat_from_hir
;
use
crate
::
thir
::
util
::
UserAnnotatedTyHelpers
;
use
rustc_ast
::
ast
;
use
rustc_data_structures
::
steal
::
Steal
;
use
rustc_errors
::
ErrorGuaranteed
;
use
rustc_hir
as
hir
;
...
...
@@ -15,7 +16,7 @@
use
rustc_middle
::
mir
::
interpret
::{
LitToConstError
,
LitToConstInput
};
use
rustc_middle
::
mir
::
ConstantKind
;
use
rustc_middle
::
thir
::
*
;
use
rustc_middle
::
ty
::{
self
,
TyCtxt
};
use
rustc_middle
::
ty
::{
self
,
Ty
,
Ty
Ctxt
};
use
rustc_span
::
Span
;
crate
fn
thir_body
<
'tcx
>
(
...
...
compiler/rustc_trait_selection/src/traits/const_evaluatable.rs
浏览文件 @
0078e541
...
...
@@ -377,10 +377,6 @@ fn visit_pat(&mut self, pat: &thir::Pat<'tcx>) {
}
}
fn
visit_const
(
&
mut
self
,
ct
:
ty
::
Const
<
'tcx
>
)
{
self
.is_poly
|
=
ct
.has_param_types_or_consts
();
}
fn
visit_constant
(
&
mut
self
,
ct
:
mir
::
ConstantKind
<
'tcx
>
)
{
self
.is_poly
|
=
ct
.has_param_types_or_consts
();
}
...
...
@@ -815,51 +811,3 @@ fn try_unify(&self, a: AbstractConst<'tcx>, b: AbstractConst<'tcx>) -> bool {
}
}
}
/* Think I need these changes
=======
match (a_ct, b_ct) {
(mir::ConstantKind::Ty(a_ct), mir::ConstantKind::Ty(b_ct)) => {
match (a_ct.val(), b_ct.val()) {
// We can just unify errors with everything to reduce the amount of
// emitted errors here.
(ty::ConstKind::Error(_), _) | (_, ty::ConstKind::Error(_)) => true,
(ty::ConstKind::Param(a_param), ty::ConstKind::Param(b_param)) => {
a_param == b_param
}
(ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => {
a_val == b_val
}
// If we have `fn a<const N: usize>() -> [u8; N + 1]` and `fn b<const M: usize>() -> [u8; 1 + M]`
// we do not want to use `assert_eq!(a(), b())` to infer that `N` and `M` have to be `1`. This
// means that we only allow inference variables if they are equal.
(ty::ConstKind::Infer(a_val), ty::ConstKind::Infer(b_val)) => {
a_val == b_val
}
// We expand generic anonymous constants at the start of this function, so this
// branch should only be taking when dealing with associated constants, at
// which point directly comparing them seems like the desired behavior.
//
// FIXME(generic_const_exprs): This isn't actually the case.
// We also take this branch for concrete anonymous constants and
// expand generic anonymous constants with concrete substs.
(ty::ConstKind::Unevaluated(a_uv), ty::ConstKind::Unevaluated(b_uv)) => {
a_uv == b_uv
}
// FIXME(generic_const_exprs): We may want to either actually try
// to evaluate `a_ct` and `b_ct` if they are are fully concrete or something like
// this, for now we just return false here.
_ => false,
}
}
(mir::ConstantKind::Val(a_val, a_ty), mir::ConstantKind::Val(b_val, b_ty)) => {
a_val == b_val && a_ty == b_ty
}
_ => {
// FIXME Can it happen that we need to compare ConstantKind::Ty(ConstKind::Value)
// with a ConstantKind::Val and vice versa?
false
>>>>>>> 6064f16d846 (change thir to use mir::ConstantKind instead of ty::Const)
*/
compiler/rustc_trait_selection/src/traits/fulfill.rs
浏览文件 @
0078e541
...
...
@@ -407,7 +407,7 @@ fn process_changed_obligations(
let
pred
=
ty
::
Binder
::
dummy
(
infcx
.replace_bound_vars_with_placeholders
(
binder
));
ProcessResult
::
Changed
(
mk_pending
(
vec!
[
obligation
.with
(
pred
.to_predicate
(
self
.selcx
.tcx
()))
obligation
.with
(
pred
.to_predicate
(
self
.selcx
.tcx
()))
,
]))
}
ty
::
PredicateKind
::
TypeWellFormedFromEnv
(
..
)
=>
{
...
...
src/tools/clippy/clippy_lints/src/matches/overlapping_arms.rs
浏览文件 @
0078e541
use
clippy_utils
::
consts
::{
constant
,
constant_full_int
,
miri_to_const
,
FullInt
};
use
clippy_utils
::
consts
::{
constant
,
constant_full_int
,
FullInt
};
use
clippy_utils
::
diagnostics
::
span_lint_and_note
;
use
core
::
cmp
::
Ordering
;
use
rustc_hir
::{
Arm
,
Expr
,
PatKind
,
RangeEnd
};
...
...
@@ -32,18 +32,15 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
.filter_map
(|
arm
|
{
if
let
Arm
{
pat
,
guard
:
None
,
..
}
=
*
arm
{
if
let
PatKind
::
Range
(
ref
lhs
,
ref
rhs
,
range_end
)
=
pat
.kind
{
let
lhs_
const
=
match
lhs
{
Some
(
lhs
)
=>
constant
(
cx
,
cx
.typeck_results
(),
lhs
)
?
.0
,
None
=>
miri_to_const
(
ty
.numeric_min_val
(
cx
.tcx
)
?
)
?
,
let
lhs_
val
=
match
lhs
{
Some
(
lhs
)
=>
constant
(
cx
,
cx
.typeck_results
(),
lhs
)
?
.0
.int_value
(
cx
,
ty
)
?
,
None
=>
FullInt
::
U
(
ty
.numeric_min_val
(
cx
.tcx
)
?
)
,
};
let
rhs_
const
=
match
rhs
{
Some
(
rhs
)
=>
constant
(
cx
,
cx
.typeck_results
(),
rhs
)
?
.0
,
None
=>
miri_to_const
(
ty
.numeric_max_val
(
cx
.tcx
)
?
)
?
,
let
rhs_
val
=
match
rhs
{
Some
(
rhs
)
=>
constant
(
cx
,
cx
.typeck_results
(),
rhs
)
?
.0
.int_value
(
cx
,
ty
)
?
,
None
=>
FullInt
::
U
(
ty
.numeric_max_val
(
cx
.tcx
)
?
)
,
};
let
lhs_val
=
lhs_const
.int_value
(
cx
,
ty
)
?
;
let
rhs_val
=
rhs_const
.int_value
(
cx
,
ty
)
?
;
let
rhs_bound
=
match
range_end
{
RangeEnd
::
Included
=>
EndBound
::
Included
(
rhs_val
),
RangeEnd
::
Excluded
=>
EndBound
::
Excluded
(
rhs_val
),
...
...
src/tools/clippy/clippy_lints/src/neg_multiply.rs
浏览文件 @
0078e541
...
...
@@ -53,7 +53,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
fn
check_mul
(
cx
:
&
LateContext
<
'_
>
,
span
:
Span
,
lit
:
&
Expr
<
'_
>
,
exp
:
&
Expr
<
'_
>
)
{
if_chain!
{
if
let
ExprKind
::
Lit
(
ref
l
)
=
lit
.kind
;
if
consts
::
lit_to_constant
(
&
l
.node
,
cx
.typeck_results
()
.expr_ty_opt
(
lit
))
==
Constant
::
Int
(
1
);
if
consts
::
lit_to_
mir_
constant
(
&
l
.node
,
cx
.typeck_results
()
.expr_ty_opt
(
lit
))
==
Constant
::
Int
(
1
);
if
cx
.typeck_results
()
.expr_ty
(
exp
)
.is_integral
();
then
{
...
...
src/tools/clippy/clippy_utils/src/consts.rs
浏览文件 @
0078e541
...
...
@@ -179,7 +179,7 @@ pub fn peel_refs(mut self) -> Self {
}
/// Parses a `LitKind` to a `Constant`.
pub
fn
lit_to_constant
(
lit
:
&
LitKind
,
ty
:
Option
<
Ty
<
'_
>>
)
->
Constant
{
pub
fn
lit_to_
mir_
constant
(
lit
:
&
LitKind
,
ty
:
Option
<
Ty
<
'_
>>
)
->
Constant
{
match
*
lit
{
LitKind
::
Str
(
ref
is
,
_
)
=>
Constant
::
Str
(
is
.to_string
()),
LitKind
::
Byte
(
b
)
=>
Constant
::
Int
(
u128
::
from
(
b
)),
...
...
@@ -301,7 +301,7 @@ pub fn expr(&mut self, e: &Expr<'_>) -> Option<Constant> {
if
is_direct_expn_of
(
e
.span
,
"cfg"
)
.is_some
()
{
None
}
else
{
Some
(
lit_to_constant
(
&
lit
.node
,
self
.typeck_results
.expr_ty_opt
(
e
)))
Some
(
lit_to_
mir_
constant
(
&
lit
.node
,
self
.typeck_results
.expr_ty_opt
(
e
)))
}
},
ExprKind
::
Array
(
vec
)
=>
self
.multi
(
vec
)
.map
(
Constant
::
Vec
),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录