Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
5853399a
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,发现更多精彩内容 >>
提交
5853399a
编写于
9月 22, 2021
作者:
N
Nadrieril
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move special `&str` handling to `Constructor` and `Fields`
上级
035c5213
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
21 addition
and
21 deletion
+21
-21
compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
+17
-3
compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
+4
-18
未找到文件。
compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
浏览文件 @
5853399a
...
...
@@ -660,9 +660,10 @@ pub(super) fn from_pat<'p>(cx: &MatchCheckCtxt<'p, 'tcx>, pat: &'p Pat<'tcx>) ->
}
else
{
match
pat
.ty
.kind
()
{
ty
::
Float
(
_
)
=>
FloatRange
(
value
,
value
,
RangeEnd
::
Included
),
// In `expand_pattern`, we convert string literals to `&CONST` patterns with
// `CONST` a pattern of type `str`. In truth this contains a constant of type
// `&str`.
// We make `&str` constants behave like `Deref` patterns, to be compatible
// with other `Deref` patterns. See also `Fields::extract_pattern_arguments`.
ty
::
Ref
(
_
,
t
,
_
)
if
t
.is_str
()
=>
Single
,
// In truth this carries a constant of type `&str`.
ty
::
Str
=>
Str
(
value
),
// All constants that can be structurally matched have already been expanded
// into the corresponding `Pat`s by `const_to_pat`. Constants that remain are
...
...
@@ -1393,6 +1394,19 @@ pub(super) fn extract_pattern_arguments(
self
.fields
[
i
]
=
pat
}
}
PatKind
::
Constant
{
..
}
=>
match
pat
.ty
.kind
()
{
ty
::
Ref
(
_
,
t
,
_
)
if
t
.is_str
()
=>
{
assert_eq!
(
self
.len
(),
1
);
// We want a `&str` constant to behave like a `Deref` pattern, to be compatible
// with other `Deref` patterns. This could have been done in `const_to_pat`,
// but that causes issues with the rest of the matching code.
// The outer constructor is `&`, and the inner one carries the str value.
let
mut
new_pat
=
pat
.clone
();
new_pat
.ty
=
t
;
// `t` is `str`, not `&str`
self
.fields
[
0
]
=
&*
cx
.pattern_arena
.alloc
(
new_pat
);
}
_
=>
{}
},
_
=>
{}
};
self
...
...
compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
浏览文件 @
5853399a
...
...
@@ -365,23 +365,9 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
impl
<
'tcx
>
PatternFolder
<
'tcx
>
for
LiteralExpander
{
fn
fold_pattern
(
&
mut
self
,
pat
:
&
Pat
<
'tcx
>
)
->
Pat
<
'tcx
>
{
debug!
(
"fold_pattern {:?} {:?} {:?}"
,
pat
,
pat
.ty
.kind
(),
pat
.kind
);
match
(
pat
.ty
.kind
(),
pat
.kind
.as_ref
())
{
(
_
,
PatKind
::
Binding
{
subpattern
:
Some
(
s
),
..
})
=>
s
.fold_with
(
self
),
(
_
,
PatKind
::
AscribeUserType
{
subpattern
:
s
,
..
})
=>
s
.fold_with
(
self
),
(
ty
::
Ref
(
_
,
t
,
_
),
PatKind
::
Constant
{
..
})
if
t
.is_str
()
=>
{
// Treat string literal patterns as deref patterns to a `str` constant, i.e.
// `&CONST`. This expands them like other const patterns. This could have been done
// in `const_to_pat`, but that causes issues with the rest of the matching code.
let
mut
new_pat
=
pat
.super_fold_with
(
self
);
// Make a fake const pattern of type `str` (instead of `&str`). That the carried
// constant value still knows it is of type `&str`.
new_pat
.ty
=
t
;
Pat
{
kind
:
Box
::
new
(
PatKind
::
Deref
{
subpattern
:
new_pat
}),
span
:
pat
.span
,
ty
:
pat
.ty
,
}
}
match
pat
.kind
.as_ref
()
{
PatKind
::
Binding
{
subpattern
:
Some
(
s
),
..
}
=>
s
.fold_with
(
self
),
PatKind
::
AscribeUserType
{
subpattern
:
s
,
..
}
=>
s
.fold_with
(
self
),
_
=>
pat
.super_fold_with
(
self
),
}
}
...
...
@@ -1183,7 +1169,7 @@ fn is_useful<'p, 'tcx>(
let
ctor_wild_subpatterns
=
Fields
::
wildcards
(
pcx
.cx
,
pcx
.ty
,
&
ctor
);
let
spec_matrix
=
start_matrix
.specialize_constructor
(
pcx
,
&
ctor
,
&
ctor_wild_subpatterns
);
let
v
=
v
.pop_head_constructor
(
pcx
.
cx
,
&
ctor_wild_subpatterns
);
let
v
=
v
.pop_head_constructor
(
cx
,
&
ctor_wild_subpatterns
);
let
usefulness
=
is_useful
(
cx
,
&
spec_matrix
,
&
v
,
witness_preference
,
hir_id
,
is_under_guard
,
false
);
let
usefulness
=
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录