Skip to content

Commit 3cdadcf

Browse files
committed
Detect double reference when applying binary op
```rust let vr = v.iter().filter(|x| { x % 2 == 0 }); ``` will now yield the following compiler output: ```bash ERROR binary operation `%` cannot be applied to type `&&_` NOTE this is a reference of a reference to a type that `%` can be applied to, you need to dereference this variable once for this operation to work NOTE an implementation of `std::ops::Rem` might be missing for `&&_` ``` The first NOTE is new. Bug rust-lang#33877
1 parent 731d375 commit 3cdadcf

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/librustc_typeck/check/op.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use super::FnCtxt;
1414
use hir::def_id::DefId;
15-
use rustc::ty::{Ty, TypeFoldable, PreferMutLvalue};
15+
use rustc::ty::{Ty, TypeFoldable, PreferMutLvalue, TypeVariants};
1616
use syntax::ast;
1717
use syntax::parse::token;
1818
use rustc::hir;
@@ -186,6 +186,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
186186
"binary operation `{}` cannot be applied to type `{}`",
187187
op.node.as_str(),
188188
lhs_ty);
189+
190+
if let TypeVariants::TyRef(_, ref ty_mut) = lhs_ty.sty {
191+
if self.lookup_op_method(expr, ty_mut.ty, vec![rhs_ty_var],
192+
token::intern(name), trait_def_id,
193+
lhs_expr).is_ok() {
194+
err.span_note(
195+
lhs_expr.span,
196+
&format!(
197+
"this is a reference of type that `{}` can be applied to, \
198+
you need to dereference this variable once for this \
199+
operation to work",
200+
op.node.as_str()));
201+
}
202+
}
203+
189204
let missing_trait = match op.node {
190205
hir::BiAdd => Some("std::ops::Add"),
191206
hir::BiSub => Some("std::ops::Sub"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
13+
let vr = v.iter().filter(|x| {
14+
x % 2 == 0
15+
//~^ ERROR binary operation `%` cannot be applied to type `&&_`
16+
//~| NOTE this is a reference of type that `%` can be applied to
17+
//~| NOTE an implementation of `std::ops::Rem` might be missing for `&&_`
18+
});
19+
println!("{:?}", vr);
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2012-2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let a: &String = &"1".to_owned();
13+
let b: &str = &"2";
14+
let c = a + b;
15+
//~^ ERROR binary operation `+` cannot be applied to type `&std::string::String`
16+
//~| NOTE this is a reference of type that `+` can be applied to
17+
//~| NOTE an implementation of `std::ops::Add` might be missing for `&std::string::String`
18+
println!("{:?}", c);
19+
}

0 commit comments

Comments
 (0)