mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-28 20:16:58 +03:00
auto merge of #11449 : rcatolino/rust/assign-binop-handling, r=alexcrichton
So far the following code
```
struct Foo;
fn main() {
let mut t = Foo;
let ref b = Foo;
a += *b;
}
```
errors with
```
test.rs:15:3: 13:11 error: binary operation + cannot be applied to type `Foo`
test.rs:15 *a += *b;
```
Since assignment-operators are no longer expanded to ```left = left OP right``` but are independents operators it should be
```
test.rs:15:3: 13:11 error: binary operation += cannot be applied to type `Foo`
test.rs:15 *a += *b;
```
to make it clear that implementing Add for Foo is not gonna work. (cf issues #11143, #11344)
Besides that, we also need to typecheck the rhs expression even if the operator has no implementation, or we end up with unknown types for the nodes of the rhs and an ICE later on while resolving types. (once again cf #11143 and #11344).
This probably would get fixed with #5992, but in the meantime it's a confusing error to stumble upon.
@pcwalton, you wrote the original code, what do you think?
(closes #11143 and #11344)
This commit is contained in:
@@ -213,11 +213,13 @@ pub fn recurse(&mut self, blk: &ast::Block) -> PurityState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `check_binop` allows overloaded operators to be invoked.
|
||||
/// Whether `check_binop` is part of an assignment or not.
|
||||
/// Used to know wether we allow user overloads and to print
|
||||
/// better messages on error.
|
||||
#[deriving(Eq)]
|
||||
enum AllowOverloadedOperatorsFlag {
|
||||
AllowOverloadedOperators,
|
||||
DontAllowOverloadedOperators,
|
||||
enum IsBinopAssignment{
|
||||
SimpleBinop,
|
||||
BinopAssignment,
|
||||
}
|
||||
|
||||
#[deriving(Clone)]
|
||||
@@ -2086,7 +2088,7 @@ fn check_binop(fcx: @FnCtxt,
|
||||
rhs: @ast::Expr,
|
||||
// Used only in the error case
|
||||
expected_result: Option<ty::t>,
|
||||
allow_overloaded_operators: AllowOverloadedOperatorsFlag
|
||||
is_binop_assignment: IsBinopAssignment
|
||||
) {
|
||||
let tcx = fcx.ccx.tcx;
|
||||
|
||||
@@ -2136,9 +2138,9 @@ fn check_binop(fcx: @FnCtxt,
|
||||
|
||||
}
|
||||
|
||||
// Check for overloaded operators if allowed.
|
||||
// Check for overloaded operators if not an assignment.
|
||||
let result_t;
|
||||
if allow_overloaded_operators == AllowOverloadedOperators {
|
||||
if is_binop_assignment == SimpleBinop {
|
||||
result_t = check_user_binop(fcx,
|
||||
callee_id,
|
||||
expr,
|
||||
@@ -2150,13 +2152,14 @@ fn check_binop(fcx: @FnCtxt,
|
||||
} else {
|
||||
fcx.type_error_message(expr.span,
|
||||
|actual| {
|
||||
format!("binary operation {} cannot be \
|
||||
applied to type `{}`",
|
||||
ast_util::binop_to_str(op),
|
||||
actual)
|
||||
format!("binary assignment operation \
|
||||
{}= cannot be applied to type `{}`",
|
||||
ast_util::binop_to_str(op),
|
||||
actual)
|
||||
},
|
||||
lhs_t,
|
||||
None);
|
||||
check_expr(fcx, rhs);
|
||||
result_t = ty::mk_err();
|
||||
}
|
||||
|
||||
@@ -2760,7 +2763,7 @@ fn check_struct_enum_variant(fcx: @FnCtxt,
|
||||
lhs,
|
||||
rhs,
|
||||
expected,
|
||||
AllowOverloadedOperators);
|
||||
SimpleBinop);
|
||||
|
||||
let lhs_ty = fcx.expr_ty(lhs);
|
||||
let rhs_ty = fcx.expr_ty(rhs);
|
||||
@@ -2781,7 +2784,7 @@ fn check_struct_enum_variant(fcx: @FnCtxt,
|
||||
lhs,
|
||||
rhs,
|
||||
expected,
|
||||
DontAllowOverloadedOperators);
|
||||
BinopAssignment);
|
||||
|
||||
let lhs_t = fcx.expr_ty(lhs);
|
||||
let result_t = fcx.expr_ty(expr);
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2014 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.
|
||||
|
||||
struct Foo;
|
||||
|
||||
fn main() {
|
||||
let mut a = Foo;
|
||||
let ref b = Foo;
|
||||
a += *b; //~ Error: binary assignment operation += cannot be applied to type `Foo`
|
||||
}
|
||||
@@ -11,5 +11,5 @@
|
||||
// Regression test for issue #5239
|
||||
|
||||
fn main() {
|
||||
let x: |int| -> int = |ref x| { x += 1; }; //~ ERROR binary operation + cannot be applied to type `&int`
|
||||
let x: |int| -> int = |ref x| { x += 1; }; //~ ERROR binary assignment operation += cannot be applied to type `&int`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user