Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
f5569ecd
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,发现更多精彩内容 >>
提交
f5569ecd
编写于
9月 17, 2015
作者:
J
Jorge Aparicio
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
address Niko's comments
上级
3ae3a5fc
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
82 addition
and
52 deletion
+82
-52
src/librustc/middle/expr_use_visitor.rs
src/librustc/middle/expr_use_visitor.rs
+3
-6
src/librustc_typeck/check/op.rs
src/librustc_typeck/check/op.rs
+16
-9
src/librustc_typeck/check/writeback.rs
src/librustc_typeck/check/writeback.rs
+39
-37
src/test/compile-fail/augmented-assignments-trait.rs
src/test/compile-fail/augmented-assignments-trait.rs
+24
-0
未找到文件。
src/librustc/middle/expr_use_visitor.rs
浏览文件 @
f5569ecd
...
...
@@ -526,13 +526,10 @@ pub fn walk_expr(&mut self, expr: &hir::Expr) {
}
hir
::
ExprAssignOp
(
op
,
ref
lhs
,
ref
rhs
)
=>
{
let
pass_args
=
if
::
rustc_front
::
util
::
is_by_value_binop
(
op
.node
)
{
PassArgs
::
ByValue
}
else
{
PassArgs
::
ByRef
};
// NB All our assignment operations take the RHS by value
assert
!
(::
rustc_front
::
util
::
is_by_value_binop
(
op
.node
));
if
!
self
.walk_overloaded_operator
(
expr
,
&**
lhs
,
vec!
[
&**
rhs
],
pass_args
)
{
if
!
self
.walk_overloaded_operator
(
expr
,
lhs
,
vec!
[
rhs
],
PassArgs
::
ByValue
)
{
self
.mutate_expr
(
expr
,
&**
lhs
,
WriteAndRead
);
self
.consume_expr
(
&**
rhs
);
}
...
...
src/librustc_typeck/check/op.rs
浏览文件 @
f5569ecd
...
...
@@ -36,7 +36,7 @@ pub fn check_binop_assign<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
let
lhs_ty
=
fcx
.resolve_type_vars_if_possible
(
fcx
.expr_ty
(
lhs_expr
));
let
(
rhs_ty
,
return_ty
)
=
check_overloaded_binop
(
fcx
,
expr
,
lhs_expr
,
lhs_ty
,
rhs_expr
,
op
,
true
);
check_overloaded_binop
(
fcx
,
expr
,
lhs_expr
,
lhs_ty
,
rhs_expr
,
op
,
IsAssign
::
Yes
);
let
rhs_ty
=
fcx
.resolve_type_vars_if_possible
(
rhs_ty
);
if
!
lhs_ty
.is_ty_var
()
&&
!
rhs_ty
.is_ty_var
()
&&
is_builtin_binop
(
lhs_ty
,
rhs_ty
,
op
)
{
...
...
@@ -83,7 +83,7 @@ pub fn check_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
// overloaded. This is the way to be most flexible w/r/t
// types that get inferred.
let
(
rhs_ty
,
return_ty
)
=
check_overloaded_binop
(
fcx
,
expr
,
lhs_expr
,
lhs_ty
,
rhs_expr
,
op
,
false
);
check_overloaded_binop
(
fcx
,
expr
,
lhs_expr
,
lhs_ty
,
rhs_expr
,
op
,
IsAssign
::
No
);
// Supply type inference hints if relevant. Probably these
// hints should be enforced during select as part of the
...
...
@@ -156,15 +156,15 @@ fn check_overloaded_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
lhs_ty
:
Ty
<
'tcx
>
,
rhs_expr
:
&
'tcx
hir
::
Expr
,
op
:
hir
::
BinOp
,
assign
:
bool
)
is_assign
:
IsAssign
)
->
(
Ty
<
'tcx
>
,
Ty
<
'tcx
>
)
{
debug!
(
"check_overloaded_binop(expr.id={}, lhs_ty={:?},
assign={
})"
,
debug!
(
"check_overloaded_binop(expr.id={}, lhs_ty={:?},
is_assign={:?
})"
,
expr
.id
,
lhs_ty
,
assign
);
is_
assign
);
let
(
name
,
trait_def_id
)
=
name_and_trait_def_id
(
fcx
,
op
,
assign
);
let
(
name
,
trait_def_id
)
=
name_and_trait_def_id
(
fcx
,
op
,
is_
assign
);
// NB: As we have not yet type-checked the RHS, we don't have the
// type at hand. Make a variable to represent it. The whole reason
...
...
@@ -181,7 +181,7 @@ fn check_overloaded_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
Err
(())
=>
{
// error types are considered "builtin"
if
!
lhs_ty
.references_error
()
{
if
assign
{
if
let
IsAssign
::
Yes
=
is_
assign
{
span_err!
(
fcx
.tcx
()
.sess
,
lhs_expr
.span
,
E0368
,
"binary assignment operation `{}=` cannot be applied to type `{}`"
,
hir_util
::
binop_to_string
(
op
.node
),
...
...
@@ -230,11 +230,11 @@ pub fn check_user_unop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
fn
name_and_trait_def_id
(
fcx
:
&
FnCtxt
,
op
:
hir
::
BinOp
,
assign
:
bool
)
is_assign
:
IsAssign
)
->
(
&
'static
str
,
Option
<
DefId
>
)
{
let
lang
=
&
fcx
.tcx
()
.lang_items
;
if
assign
{
if
let
IsAssign
::
Yes
=
is_
assign
{
match
op
.node
{
hir
::
BiAdd
=>
(
"add_assign"
,
lang
.add_assign_trait
()),
hir
::
BiSub
=>
(
"sub_assign"
,
lang
.sub_assign_trait
()),
...
...
@@ -383,6 +383,13 @@ fn from(op: hir::BinOp) -> BinOpCategory {
}
}
/// Whether the binary operation is an assignment (`a += b`), or not (`a + b`)
#[derive(Clone,
Copy,
Debug)]
enum
IsAssign
{
No
,
Yes
,
}
/// Returns true if this is a built-in arithmetic operation (e.g. u32
/// + u32, i16x4 == i16x4) and false if these types would have to be
/// overloaded to be legal. There are two reasons that we distinguish
...
...
src/librustc_typeck/check/writeback.rs
浏览文件 @
f5569ecd
...
...
@@ -93,48 +93,50 @@ fn tcx(&self) -> &'cx ty::ctxt<'tcx> {
fn
fix_scalar_binary_expr
(
&
mut
self
,
e
:
&
hir
::
Expr
)
{
match
e
.node
{
hir
::
ExprBinary
(
ref
op
,
ref
lhs
,
ref
rhs
)
|
hir
::
ExprAssignOp
(
ref
op
,
ref
lhs
,
ref
rhs
)
=>
{
let
lhs_ty
=
self
.fcx
.node_ty
(
lhs
.id
);
let
lhs_ty
=
self
.fcx
.infcx
()
.resolve_type_vars_if_possible
(
&
lhs_ty
);
let
rhs_ty
=
self
.fcx
.node_ty
(
rhs
.id
);
let
rhs_ty
=
self
.fcx
.infcx
()
.resolve_type_vars_if_possible
(
&
rhs_ty
);
if
lhs_ty
.is_scalar
()
&&
rhs_ty
.is_scalar
()
{
self
.fcx.inh.tables
.borrow_mut
()
.method_map
.remove
(
&
MethodCall
::
expr
(
e
.id
));
// weird but true: the by-ref binops put an
// adjustment on the lhs but not the rhs; the
// adjustment for rhs is kind of baked into the
// system.
match
e
.node
{
hir
::
ExprBinary
(
..
)
=>
{
if
!
hir_util
::
is_by_value_binop
(
op
.node
)
{
self
.fcx.inh.tables
.borrow_mut
()
.adjustments
.remove
(
&
lhs
.id
);
}
},
hir
::
ExprAssignOp
(
..
)
=>
{
hir
::
ExprAssignOp
(
ref
op
,
ref
lhs
,
ref
rhs
)
=>
{
let
lhs_ty
=
self
.fcx
.node_ty
(
lhs
.id
);
let
lhs_ty
=
self
.fcx
.infcx
()
.resolve_type_vars_if_possible
(
&
lhs_ty
);
let
rhs_ty
=
self
.fcx
.node_ty
(
rhs
.id
);
let
rhs_ty
=
self
.fcx
.infcx
()
.resolve_type_vars_if_possible
(
&
rhs_ty
);
if
lhs_ty
.is_scalar
()
&&
rhs_ty
.is_scalar
()
{
self
.fcx.inh.tables
.borrow_mut
()
.method_map
.remove
(
&
MethodCall
::
expr
(
e
.id
));
// weird but true: the by-ref binops put an
// adjustment on the lhs but not the rhs; the
// adjustment for rhs is kind of baked into the
// system.
match
e
.node
{
hir
::
ExprBinary
(
..
)
=>
{
if
!
hir_util
::
is_by_value_binop
(
op
.node
)
{
self
.fcx.inh.tables
.borrow_mut
()
.adjustments
.remove
(
&
lhs
.id
);
},
_
=>
{},
}
}
else
{
let
tcx
=
self
.tcx
();
if
let
hir
::
ExprAssignOp
(
..
)
=
e
.node
{
if
!
tcx
.sess.features
.borrow
()
.augmented_assignments
&&
!
self
.fcx
.expr_ty
(
e
)
.references_error
()
{
tcx
.sess
.span_err
(
e
.span
,
"overloaded augmented assignments are not stable"
);
fileline_help!
(
tcx
.sess
,
e
.span
,
"add #![feature(augmented_assignments)] to the crate features
\
to enable"
);
}
},
hir
::
ExprAssignOp
(
..
)
=>
{
self
.fcx.inh.tables
.borrow_mut
()
.adjustments
.remove
(
&
lhs
.id
);
},
_
=>
{},
}
}
else
{
let
tcx
=
self
.tcx
();
if
let
hir
::
ExprAssignOp
(
..
)
=
e
.node
{
if
!
tcx
.sess.features
.borrow
()
.augmented_assignments
&&
!
self
.fcx
.expr_ty
(
e
)
.references_error
()
{
tcx
.sess
.span_err
(
e
.span
,
"overloaded augmented assignments are not stable"
);
fileline_help!
(
tcx
.sess
,
e
.span
,
"add #![feature(augmented_assignments)] to the crate features
\
to enable"
);
}
}
}
}
_
=>
{},
}
}
...
...
src/test/compile-fail/augmented-assignments-trait.rs
0 → 100644
浏览文件 @
f5569ecd
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use
std
::
ops
::
AddAssign
;
//~^ error: use of unstable library feature 'op_assign_traits'
struct
Int
(
i32
);
impl
AddAssign
for
Int
{
//~^ error: use of unstable library feature 'op_assign_traits'
fn
add_assign
(
&
mut
self
,
_
:
Int
)
{
//~^ error: use of unstable library feature 'op_assign_traits'
unimplemented!
()
}
}
fn
main
()
{}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录