Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
d8983655
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,发现更多精彩内容 >>
提交
d8983655
编写于
11月 28, 2020
作者:
N
Nadrieril
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Correctly detect `usize`/`isize` range overlaps
上级
4ae328be
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
20 addition
and
31 deletion
+20
-31
compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
+12
-29
src/test/ui/pattern/usefulness/integer-ranges/reachability.rs
...test/ui/pattern/usefulness/integer-ranges/reachability.rs
+1
-1
src/test/ui/pattern/usefulness/integer-ranges/reachability.stderr
.../ui/pattern/usefulness/integer-ranges/reachability.stderr
+7
-1
未找到文件。
compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
浏览文件 @
d8983655
...
...
@@ -58,12 +58,6 @@ fn boundaries(&self) -> (u128, u128) {
(
*
self
.range
.start
(),
*
self
.range
.end
())
}
/// Don't treat `usize`/`isize` exhaustively unless the `precise_pointer_size_matching` feature
/// is enabled.
fn
treat_exhaustively
(
&
self
,
tcx
:
TyCtxt
<
'tcx
>
)
->
bool
{
!
self
.ty
.is_ptr_sized_integral
()
||
tcx
.features
()
.precise_pointer_size_matching
}
#[inline]
fn
integral_size_and_signed_bias
(
tcx
:
TyCtxt
<
'tcx
>
,
ty
:
Ty
<
'_
>
)
->
Option
<
(
Size
,
u128
)
>
{
match
*
ty
.kind
()
{
...
...
@@ -147,20 +141,15 @@ fn is_subrange(&self, other: &Self) -> bool {
other
.range
.start
()
<=
self
.range
.start
()
&&
self
.range
.end
()
<=
other
.range
.end
()
}
fn
intersection
(
&
self
,
tcx
:
TyCtxt
<
'tcx
>
,
other
:
&
Self
)
->
Option
<
Self
>
{
fn
intersection
(
&
self
,
other
:
&
Self
)
->
Option
<
Self
>
{
let
ty
=
self
.ty
;
let
(
lo
,
hi
)
=
self
.boundaries
();
let
(
other_lo
,
other_hi
)
=
other
.boundaries
();
if
self
.treat_exhaustively
(
tcx
)
{
if
lo
<=
other_hi
&&
other_lo
<=
hi
{
let
span
=
other
.span
;
Some
(
IntRange
{
range
:
max
(
lo
,
other_lo
)
..=
min
(
hi
,
other_hi
),
ty
,
span
})
}
else
{
None
}
if
lo
<=
other_hi
&&
other_lo
<=
hi
{
let
span
=
other
.span
;
Some
(
IntRange
{
range
:
max
(
lo
,
other_lo
)
..=
min
(
hi
,
other_hi
),
ty
,
span
})
}
else
{
// If the range should not be treated exhaustively, fallback to checking for inclusion.
if
self
.is_subrange
(
other
)
{
Some
(
self
.clone
())
}
else
{
None
}
None
}
}
...
...
@@ -271,7 +260,7 @@ fn range_borders(r: IntRange<'_>) -> impl Iterator<Item = Border> {
.head_ctors
(
pcx
.cx
)
.filter_map
(|
ctor
|
ctor
.as_int_range
())
.filter_map
(|
range
|
{
let
intersection
=
self
.intersection
(
pcx
.cx.tcx
,
&
range
);
let
intersection
=
self
.intersection
(
&
range
);
let
should_lint
=
self
.suspicious_intersection
(
&
range
);
if
let
(
Some
(
range
),
1
,
true
)
=
(
&
intersection
,
row_len
,
should_lint
)
{
// FIXME: for now, only check for overlapping ranges on simple range
...
...
@@ -346,8 +335,8 @@ fn lint_overlapping_patterns(
}
/// See `Constructor::is_covered_by`
fn
is_covered_by
<
'p
>
(
&
self
,
pcx
:
PatCtxt
<
'_
,
'p
,
'tcx
>
,
other
:
&
Self
)
->
bool
{
if
self
.intersection
(
pcx
.cx.tcx
,
other
)
.is_some
()
{
fn
is_covered_by
(
&
self
,
other
:
&
Self
)
->
bool
{
if
self
.intersection
(
other
)
.is_some
()
{
// Constructor splitting should ensure that all intersections we encounter are actually
// inclusions.
assert
!
(
self
.is_subrange
(
other
));
...
...
@@ -694,11 +683,7 @@ pub(super) fn split<'p>(
Wildcard
=>
Constructor
::
split_wildcard
(
pcx
),
// Fast-track if the range is trivial. In particular, we don't do the overlapping
// ranges check.
IntRange
(
ctor_range
)
if
ctor_range
.treat_exhaustively
(
pcx
.cx.tcx
)
&&
!
ctor_range
.is_singleton
()
=>
{
ctor_range
.split
(
pcx
,
hir_id
)
}
IntRange
(
ctor_range
)
if
!
ctor_range
.is_singleton
()
=>
ctor_range
.split
(
pcx
,
hir_id
),
Slice
(
slice
@
Slice
{
kind
:
VarLen
(
..
),
..
})
=>
slice
.split
(
pcx
),
// Any other constructor can be used unchanged.
_
=>
smallvec!
[
self
.clone
()],
...
...
@@ -740,9 +725,7 @@ pub(super) fn is_covered_by<'p>(&self, pcx: PatCtxt<'_, 'p, 'tcx>, other: &Self)
(
Single
,
Single
)
=>
true
,
(
Variant
(
self_id
),
Variant
(
other_id
))
=>
self_id
==
other_id
,
(
IntRange
(
self_range
),
IntRange
(
other_range
))
=>
{
self_range
.is_covered_by
(
pcx
,
other_range
)
}
(
IntRange
(
self_range
),
IntRange
(
other_range
))
=>
self_range
.is_covered_by
(
other_range
),
(
FloatRange
(
self_from
,
self_to
,
self_end
),
FloatRange
(
other_from
,
other_to
,
other_end
),
...
...
@@ -803,7 +786,7 @@ fn is_covered_by_any<'p>(
IntRange
(
range
)
=>
used_ctors
.iter
()
.filter_map
(|
c
|
c
.as_int_range
())
.any
(|
other
|
range
.is_covered_by
(
pcx
,
other
)),
.any
(|
other
|
range
.is_covered_by
(
other
)),
Slice
(
slice
)
=>
used_ctors
.iter
()
.filter_map
(|
c
|
c
.as_slice
())
...
...
@@ -811,7 +794,7 @@ fn is_covered_by_any<'p>(
// This constructor is never covered by anything else
NonExhaustive
=>
false
,
Str
(
..
)
|
FloatRange
(
..
)
|
Opaque
|
Wildcard
=>
{
bug!
(
"found unexpected ctor in all_ctors: {:?}"
,
self
)
span_bug!
(
pcx
.span
,
"found unexpected ctor in all_ctors: {:?}"
,
self
)
}
}
}
...
...
src/test/ui/pattern/usefulness/integer-ranges/reachability.rs
浏览文件 @
d8983655
...
...
@@ -72,7 +72,7 @@ fn main() {
match
0u
size
{
0
..
10
=>
{},
10
..
20
=>
{},
5
..
15
=>
{},
//
FIXME: should be unreachable
5
..
15
=>
{},
//
~ ERROR unreachable pattern
_
=>
{},
}
// Chars between '\u{D7FF}' and '\u{E000}' are invalid even though ranges that contain them are
...
...
src/test/ui/pattern/usefulness/integer-ranges/reachability.stderr
浏览文件 @
d8983655
...
...
@@ -124,6 +124,12 @@ error: unreachable pattern
LL | 5..25 => {},
| ^^^^^
error: unreachable pattern
--> $DIR/reachability.rs:75:9
|
LL | 5..15 => {},
| ^^^^^
error: unreachable pattern
--> $DIR/reachability.rs:82:9
|
...
...
@@ -142,5 +148,5 @@ error: unreachable pattern
LL | BAR => {}
| ^^^
error: aborting due to 2
3
previous errors
error: aborting due to 2
4
previous errors
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录