Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
f8af2b50
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,发现更多精彩内容 >>
提交
f8af2b50
编写于
5月 21, 2013
作者:
J
Jihyun Yu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix issue #6209
上级
15e44381
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
181 addition
and
79 deletion
+181
-79
src/librustc/middle/check_match.rs
src/librustc/middle/check_match.rs
+76
-15
src/librustc/middle/const_eval.rs
src/librustc/middle/const_eval.rs
+31
-23
src/librustc/middle/trans/_match.rs
src/librustc/middle/trans/_match.rs
+42
-35
src/librustc/middle/typeck/check/_match.rs
src/librustc/middle/typeck/check/_match.rs
+13
-3
src/librustc/middle/typeck/check/mod.rs
src/librustc/middle/typeck/check/mod.rs
+6
-3
src/test/compile-fail/match-ill-type1.rs
src/test/compile-fail/match-ill-type1.rs
+6
-0
src/test/compile-fail/match-ill-type2.rs
src/test/compile-fail/match-ill-type2.rs
+7
-0
未找到文件。
src/librustc/middle/check_match.rs
浏览文件 @
f8af2b50
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012
-2013
The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
...
...
@@ -498,10 +498,27 @@ pub fn specialize(cx: @MatchCheckCtxt,
lookup_const_by_id
(
cx
.tcx
,
did
)
.get
();
let
e_v
=
eval_const_expr
(
cx
.tcx
,
const_expr
);
let
match_
=
match
*
ctor_id
{
val
(
ref
v
)
=>
compare_const_vals
(
&
e_v
,
v
)
==
0
,
val
(
ref
v
)
=>
{
match
compare_const_vals
(
&
e_v
,
v
)
{
Some
(
val1
)
=>
(
val1
==
0
),
None
=>
{
cx
.tcx.sess
.span_err
(
pat_span
,
"mismatched types between arms"
);
false
}
}
},
range
(
ref
c_lo
,
ref
c_hi
)
=>
{
compare_const_vals
(
c_lo
,
&
e_v
)
>=
0
&&
compare_const_vals
(
c_hi
,
&
e_v
)
<=
0
let
m1
=
compare_const_vals
(
c_lo
,
&
e_v
),
m2
=
compare_const_vals
(
c_hi
,
&
e_v
);
match
(
m1
,
m2
)
{
(
Some
(
val1
),
Some
(
val2
))
=>
(
val1
>=
0
&&
val2
<=
0
),
_
=>
{
cx
.tcx.sess
.span_err
(
pat_span
,
"mismatched types between ranges"
);
false
}
}
}
single
=>
true
,
_
=>
fail
!
(
"type error"
)
...
...
@@ -529,10 +546,26 @@ pub fn specialize(cx: @MatchCheckCtxt,
lookup_const_by_id
(
cx
.tcx
,
did
)
.get
();
let
e_v
=
eval_const_expr
(
cx
.tcx
,
const_expr
);
let
match_
=
match
*
ctor_id
{
val
(
ref
v
)
=>
compare_const_vals
(
&
e_v
,
v
)
==
0
,
val
(
ref
v
)
=>
match
compare_const_vals
(
&
e_v
,
v
)
{
Some
(
val1
)
=>
(
val1
==
0
),
None
=>
{
cx
.tcx.sess
.span_err
(
pat_span
,
"mismatched types between arms"
);
false
}
},
range
(
ref
c_lo
,
ref
c_hi
)
=>
{
compare_const_vals
(
c_lo
,
&
e_v
)
>=
0
&&
compare_const_vals
(
c_hi
,
&
e_v
)
<=
0
let
m1
=
compare_const_vals
(
c_lo
,
&
e_v
),
m2
=
compare_const_vals
(
c_hi
,
&
e_v
);
match
(
m1
,
m2
)
{
(
Some
(
val1
),
Some
(
val2
))
=>
(
val1
>=
0
&&
val2
<=
0
),
_
=>
{
cx
.tcx.sess
.span_err
(
pat_span
,
"mismatched types between ranges"
);
false
}
}
}
single
=>
true
,
_
=>
fail
!
(
"type error"
)
...
...
@@ -619,10 +652,27 @@ pub fn specialize(cx: @MatchCheckCtxt,
pat_lit
(
expr
)
=>
{
let
e_v
=
eval_const_expr
(
cx
.tcx
,
expr
);
let
match_
=
match
*
ctor_id
{
val
(
ref
v
)
=>
compare_const_vals
(
&
e_v
,
v
)
==
0
,
val
(
ref
v
)
=>
{
match
compare_const_vals
(
&
e_v
,
v
)
{
Some
(
val1
)
=>
val1
==
0
,
None
=>
{
cx
.tcx.sess
.span_err
(
pat_span
,
"mismatched types between arms"
);
false
}
}
},
range
(
ref
c_lo
,
ref
c_hi
)
=>
{
compare_const_vals
(
c_lo
,
&
e_v
)
>=
0
&&
compare_const_vals
(
c_hi
,
&
e_v
)
<=
0
let
m1
=
compare_const_vals
(
c_lo
,
&
e_v
),
m2
=
compare_const_vals
(
c_hi
,
&
e_v
);
match
(
m1
,
m2
)
{
(
Some
(
val1
),
Some
(
val2
))
=>
(
val1
>=
0
&&
val2
<=
0
),
_
=>
{
cx
.tcx.sess
.span_err
(
pat_span
,
"mismatched types between ranges"
);
false
}
}
}
single
=>
true
,
_
=>
fail
!
(
"type error"
)
...
...
@@ -638,11 +688,22 @@ pub fn specialize(cx: @MatchCheckCtxt,
_
=>
fail
!
(
"type error"
)
};
let
v_lo
=
eval_const_expr
(
cx
.tcx
,
lo
),
v_hi
=
eval_const_expr
(
cx
.tcx
,
hi
);
let
match_
=
compare_const_vals
(
&
c_lo
,
&
v_lo
)
>=
0
&&
compare_const_vals
(
&
c_hi
,
&
v_hi
)
<=
0
;
if
match_
{
Some
(
vec
::
to_owned
(
r
.tail
()))
}
else
{
None
}
}
v_hi
=
eval_const_expr
(
cx
.tcx
,
hi
);
let
m1
=
compare_const_vals
(
&
c_lo
,
&
v_lo
),
m2
=
compare_const_vals
(
&
c_hi
,
&
v_hi
);
match
(
m1
,
m2
)
{
(
Some
(
val1
),
Some
(
val2
))
if
val1
>=
0
&&
val2
<=
0
=>
{
Some
(
vec
::
to_owned
(
r
.tail
()))
},
(
Some
(
_
),
Some
(
_
))
=>
None
,
_
=>
{
cx
.tcx.sess
.span_err
(
pat_span
,
"mismatched types between ranges"
);
None
}
}
}
pat_vec
(
before
,
slice
,
after
)
=>
{
match
*
ctor_id
{
vec
(
_
)
=>
{
...
...
src/librustc/middle/const_eval.rs
浏览文件 @
f8af2b50
...
...
@@ -420,65 +420,73 @@ pub fn lit_to_const(lit: @lit) -> const_val {
}
}
pub
fn
compare_const_vals
(
a
:
&
const_val
,
b
:
&
const_val
)
->
int
{
pub
fn
compare_const_vals
(
a
:
&
const_val
,
b
:
&
const_val
)
->
Option
<
int
>
{
match
(
a
,
b
)
{
(
&
const_int
(
a
),
&
const_int
(
b
))
=>
{
if
a
==
b
{
0
Some
(
0
)
}
else
if
a
<
b
{
-
1
Some
(
-
1
)
}
else
{
1
Some
(
1
)
}
}
(
&
const_uint
(
a
),
&
const_uint
(
b
))
=>
{
if
a
==
b
{
0
Some
(
0
)
}
else
if
a
<
b
{
-
1
Some
(
-
1
)
}
else
{
1
Some
(
1
)
}
}
(
&
const_float
(
a
),
&
const_float
(
b
))
=>
{
if
a
==
b
{
0
Some
(
0
)
}
else
if
a
<
b
{
-
1
Some
(
-
1
)
}
else
{
1
Some
(
1
)
}
}
(
&
const_str
(
ref
a
),
&
const_str
(
ref
b
))
=>
{
if
(
*
a
)
==
(
*
b
)
{
0
Some
(
0
)
}
else
if
(
*
a
)
<
(
*
b
)
{
-
1
Some
(
-
1
)
}
else
{
1
Some
(
1
)
}
}
(
&
const_bool
(
a
),
&
const_bool
(
b
))
=>
{
if
a
==
b
{
0
Some
(
0
)
}
else
if
a
<
b
{
-
1
Some
(
-
1
)
}
else
{
1
Some
(
1
)
}
}
_
=>
fail
!
(
"compare_const_vals: ill-typed comparison"
)
_
=>
{
None
}
}
}
pub
fn
compare_lit_exprs
(
tcx
:
middle
::
ty
::
ctxt
,
a
:
@
expr
,
b
:
@
expr
)
->
int
{
compare_const_vals
(
&
eval_const_expr
(
tcx
,
a
),
&
eval_const_expr
(
tcx
,
b
))
pub
fn
compare_lit_exprs
(
tcx
:
middle
::
ty
::
ctxt
,
a
:
@
expr
,
b
:
@
expr
)
->
Option
<
int
>
{
compare_const_vals
(
&
eval_const_expr
(
tcx
,
a
),
&
eval_const_expr
(
tcx
,
b
))
}
pub
fn
lit_expr_eq
(
tcx
:
middle
::
ty
::
ctxt
,
a
:
@
expr
,
b
:
@
expr
)
->
bool
{
compare_lit_exprs
(
tcx
,
a
,
b
)
==
0
pub
fn
lit_expr_eq
(
tcx
:
middle
::
ty
::
ctxt
,
a
:
@
expr
,
b
:
@
expr
)
->
Option
<
bool
>
{
match
compare_lit_exprs
(
tcx
,
a
,
b
)
{
Some
(
val
)
=>
Some
(
val
==
0
),
None
=>
None
,
}
}
pub
fn
lit_eq
(
a
:
@
lit
,
b
:
@
lit
)
->
bool
{
compare_const_vals
(
&
lit_to_const
(
a
),
&
lit_to_const
(
b
))
==
0
pub
fn
lit_eq
(
a
:
@
lit
,
b
:
@
lit
)
->
Option
<
bool
>
{
match
compare_const_vals
(
&
lit_to_const
(
a
),
&
lit_to_const
(
b
))
{
Some
(
val
)
=>
Some
(
val
==
0
),
None
=>
None
,
}
}
src/librustc/middle/trans/_match.rs
浏览文件 @
f8af2b50
...
...
@@ -193,48 +193,55 @@ pub enum Opt {
pub
fn
opt_eq
(
tcx
:
ty
::
ctxt
,
a
:
&
Opt
,
b
:
&
Opt
)
->
bool
{
match
(
a
,
b
)
{
(
&
lit
(
a
),
&
lit
(
b
))
=>
{
match
(
a
,
b
)
{
(
UnitLikeStructLit
(
a
),
UnitLikeStructLit
(
b
))
=>
a
==
b
,
_
=>
{
let
a_expr
;
match
a
{
ExprLit
(
existing_a_expr
)
=>
a_expr
=
existing_a_expr
,
ConstLit
(
a_const
)
=>
{
let
e
=
const_eval
::
lookup_const_by_id
(
tcx
,
a_const
);
a_expr
=
e
.get
();
}
UnitLikeStructLit
(
_
)
=>
{
fail
!
(
"UnitLikeStructLit should have been handled
\
above"
)
(
&
lit
(
a
),
&
lit
(
b
))
=>
{
match
(
a
,
b
)
{
(
UnitLikeStructLit
(
a
),
UnitLikeStructLit
(
b
))
=>
a
==
b
,
_
=>
{
let
a_expr
;
match
a
{
ExprLit
(
existing_a_expr
)
=>
a_expr
=
existing_a_expr
,
ConstLit
(
a_const
)
=>
{
let
e
=
const_eval
::
lookup_const_by_id
(
tcx
,
a_const
);
a_expr
=
e
.get
();
}
UnitLikeStructLit
(
_
)
=>
{
fail
!
(
"UnitLikeStructLit should have been handled
\
above"
)
}
}
}
let
b_expr
;
match
b
{
ExprLit
(
existing_b_expr
)
=>
b_expr
=
existing_b_expr
,
ConstLit
(
b_const
)
=>
{
let
e
=
const_eval
::
lookup_const_by_id
(
tcx
,
b_const
);
b_expr
=
e
.get
();
let
b_expr
;
match
b
{
ExprLit
(
existing_b_expr
)
=>
b_expr
=
existing_b_expr
,
ConstLit
(
b_const
)
=>
{
let
e
=
const_eval
::
lookup_const_by_id
(
tcx
,
b_const
);
b_expr
=
e
.get
();
}
UnitLikeStructLit
(
_
)
=>
{
fail
!
(
"UnitLikeStructLit should have been handled
\
above"
)
}
}
UnitLikeStructLit
(
_
)
=>
{
fail
!
(
"UnitLikeStructLit should have been handled
\
above"
)
match
const_eval
::
compare_lit_exprs
(
tcx
,
a_expr
,
b_expr
)
{
Some
(
val1
)
=>
val1
==
0
,
None
=>
fail
!
(
"compare_list_exprs: type mismatch"
),
}
}
const_eval
::
compare_lit_exprs
(
tcx
,
a_expr
,
b_expr
)
==
0
}
}
}
(
&
range
(
a1
,
a2
),
&
range
(
b1
,
b2
))
=>
{
const_eval
::
compare_lit_exprs
(
tcx
,
a1
,
b1
)
==
0
&&
const_eval
::
compare_lit_exprs
(
tcx
,
a2
,
b2
)
==
0
}
(
&
var
(
a
,
_
),
&
var
(
b
,
_
))
=>
a
==
b
,
(
&
vec_len_eq
(
a
),
&
vec_len_eq
(
b
))
=>
a
==
b
,
(
&
vec_len_ge
(
a
,
_
),
&
vec_len_ge
(
b
,
_
))
=>
a
==
b
,
_
=>
false
(
&
range
(
a1
,
a2
),
&
range
(
b1
,
b2
))
=>
{
let
m1
=
const_eval
::
compare_lit_exprs
(
tcx
,
a1
,
b1
);
let
m2
=
const_eval
::
compare_lit_exprs
(
tcx
,
a2
,
b2
);
match
(
m1
,
m2
)
{
(
Some
(
val1
),
Some
(
val2
))
=>
(
val1
==
0
&&
val2
==
0
),
_
=>
fail
!
(
"compare_list_exprs: type mismatch"
),
}
}
(
&
var
(
a
,
_
),
&
var
(
b
,
_
))
=>
a
==
b
,
(
&
vec_len_eq
(
a
),
&
vec_len_eq
(
b
))
=>
a
==
b
,
(
&
vec_len_ge
(
a
,
_
),
&
vec_len_ge
(
b
,
_
))
=>
a
==
b
,
_
=>
false
}
}
...
...
src/librustc/middle/typeck/check/_match.rs
浏览文件 @
f8af2b50
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012
-2013
The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
...
...
@@ -408,8 +408,18 @@ pub fn check_pat(pcx: &pat_ctxt, pat: @ast::pat, expected: ty::t) {
// no-op
}
else
if
!
ty
::
type_is_numeric
(
b_ty
)
{
tcx
.sess
.span_err
(
pat
.span
,
"non-numeric type used in range"
);
}
else
if
!
valid_range_bounds
(
fcx
.ccx
,
begin
,
end
)
{
tcx
.sess
.span_err
(
begin
.span
,
"lower range bound must be less than upper"
);
}
else
{
match
valid_range_bounds
(
fcx
.ccx
,
begin
,
end
)
{
Some
(
false
)
=>
{
tcx
.sess
.span_err
(
begin
.span
,
"lower range bound must be less than upper"
);
},
None
=>
{
tcx
.sess
.span_err
(
begin
.span
,
"mismatched types in range"
);
},
_
=>
{
},
}
}
fcx
.write_ty
(
pat
.id
,
b_ty
);
}
...
...
src/librustc/middle/typeck/check/mod.rs
浏览文件 @
f8af2b50
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012
-2013
The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
...
...
@@ -1044,8 +1044,11 @@ pub fn check_lit(fcx: @mut FnCtxt, lit: @ast::lit) -> ty::t {
pub
fn
valid_range_bounds
(
ccx
:
@
mut
CrateCtxt
,
from
:
@
ast
::
expr
,
to
:
@
ast
::
expr
)
->
bool
{
const_eval
::
compare_lit_exprs
(
ccx
.tcx
,
from
,
to
)
<=
0
->
Option
<
bool
>
{
match
const_eval
::
compare_lit_exprs
(
ccx
.tcx
,
from
,
to
)
{
Some
(
val
)
=>
Some
(
val
<=
0
),
None
=>
None
}
}
pub
fn
check_expr_has_type
(
...
...
src/test/compile-fail/match-ill-type1.rs
0 → 100644
浏览文件 @
f8af2b50
fn
main
()
{
match
1
{
1
..
2u
=>
1
,
//~ ERROR mismatched types in range
_
=>
2
,
};
}
src/test/compile-fail/match-ill-type2.rs
0 → 100644
浏览文件 @
f8af2b50
fn
main
()
{
match
1
{
1
=>
1
,
//~ ERROR mismatched types between arms
2u
=>
1
,
_
=>
2
,
};
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录