Skip to content

Commit ce1c9fb

Browse files
committed
Rust: Account for arity in operator overloading
For instance the binary `&` is overloadable but the prefix `&` is not. Similarly, `*` has a different target depending on if it's prefix or infix.
1 parent 1858355 commit ce1c9fb

File tree

1 file changed

+76
-69
lines changed

1 file changed

+76
-69
lines changed

rust/ql/lib/codeql/rust/elements/internal/OperationImpl.qll

Lines changed: 76 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,81 @@ private import rust
88
private import codeql.rust.elements.internal.ExprImpl::Impl as ExprImpl
99

1010
/**
11-
* Holds if the operator `op` is overloaded to a trait with the canonical path
12-
* `path` and the method name `method`.
11+
* Holds if the operator `op` with arity `arity` is overloaded to a trait with
12+
* the canonical path `path` and the method name `method`.
1313
*/
14-
private predicate isOverloaded(string op, string path, string method) {
15-
// Negation
16-
op = "-" and path = "core::ops::arith::Neg" and method = "neg"
17-
or
18-
// Not
19-
op = "!" and path = "core::ops::bit::Not" and method = "not"
20-
or
21-
// Dereference
22-
op = "*" and path = "core::ops::Deref" and method = "deref"
23-
or
24-
// Comparison operators
25-
op = "==" and path = "core::cmp::PartialEq" and method = "eq"
26-
or
27-
op = "!=" and path = "core::cmp::PartialEq" and method = "ne"
28-
or
29-
op = "<" and path = "core::cmp::PartialOrd" and method = "lt"
30-
or
31-
op = "<=" and path = "core::cmp::PartialOrd" and method = "le"
32-
or
33-
op = ">" and path = "core::cmp::PartialOrd" and method = "gt"
34-
or
35-
op = ">=" and path = "core::cmp::PartialOrd" and method = "ge"
36-
or
37-
// Arithmetic operators
38-
op = "+" and path = "core::ops::arith::Add" and method = "add"
39-
or
40-
op = "-" and path = "core::ops::arith::Sub" and method = "sub"
41-
or
42-
op = "*" and path = "core::ops::arith::Mul" and method = "mul"
43-
or
44-
op = "/" and path = "core::ops::arith::Div" and method = "div"
45-
or
46-
op = "%" and path = "core::ops::arith::Rem" and method = "rem"
47-
or
48-
// Arithmetic assignment expressions
49-
op = "+=" and path = "core::ops::arith::AddAssign" and method = "add_assign"
50-
or
51-
op = "-=" and path = "core::ops::arith::SubAssign" and method = "sub_assign"
52-
or
53-
op = "*=" and path = "core::ops::arith::MulAssign" and method = "mul_assign"
54-
or
55-
op = "/=" and path = "core::ops::arith::DivAssign" and method = "div_assign"
56-
or
57-
op = "%=" and path = "core::ops::arith::RemAssign" and method = "rem_assign"
58-
or
59-
// Bitwise operators
60-
op = "&" and path = "core::ops::bit::BitAnd" and method = "bitand"
61-
or
62-
op = "|" and path = "core::ops::bit::BitOr" and method = "bitor"
63-
or
64-
op = "^" and path = "core::ops::bit::BitXor" and method = "bitxor"
65-
or
66-
op = "<<" and path = "core::ops::bit::Shl" and method = "shl"
67-
or
68-
op = ">>" and path = "core::ops::bit::Shr" and method = "shr"
69-
or
70-
// Bitwise assignment operators
71-
op = "&=" and path = "core::ops::bit::BitAndAssign" and method = "bitand_assign"
72-
or
73-
op = "|=" and path = "core::ops::bit::BitOrAssign" and method = "bitor_assign"
74-
or
75-
op = "^=" and path = "core::ops::bit::BitXorAssign" and method = "bitxor_assign"
76-
or
77-
op = "<<=" and path = "core::ops::bit::ShlAssign" and method = "shl_assign"
78-
or
79-
op = ">>=" and path = "core::ops::bit::ShrAssign" and method = "shr_assign"
14+
private predicate isOverloaded(string op, int arity, string path, string method) {
15+
arity = 1 and
16+
(
17+
// Negation
18+
op = "-" and path = "core::ops::arith::Neg" and method = "neg"
19+
or
20+
// Not
21+
op = "!" and path = "core::ops::bit::Not" and method = "not"
22+
or
23+
// Dereference
24+
op = "*" and path = "core::ops::Deref" and method = "deref"
25+
)
26+
or
27+
arity = 2 and
28+
(
29+
// Comparison operators
30+
op = "==" and path = "core::cmp::PartialEq" and method = "eq"
31+
or
32+
op = "!=" and path = "core::cmp::PartialEq" and method = "ne"
33+
or
34+
op = "<" and path = "core::cmp::PartialOrd" and method = "lt"
35+
or
36+
op = "<=" and path = "core::cmp::PartialOrd" and method = "le"
37+
or
38+
op = ">" and path = "core::cmp::PartialOrd" and method = "gt"
39+
or
40+
op = ">=" and path = "core::cmp::PartialOrd" and method = "ge"
41+
or
42+
// Arithmetic operators
43+
op = "+" and path = "core::ops::arith::Add" and method = "add"
44+
or
45+
op = "-" and path = "core::ops::arith::Sub" and method = "sub"
46+
or
47+
op = "*" and path = "core::ops::arith::Mul" and method = "mul"
48+
or
49+
op = "/" and path = "core::ops::arith::Div" and method = "div"
50+
or
51+
op = "%" and path = "core::ops::arith::Rem" and method = "rem"
52+
or
53+
// Arithmetic assignment expressions
54+
op = "+=" and path = "core::ops::arith::AddAssign" and method = "add_assign"
55+
or
56+
op = "-=" and path = "core::ops::arith::SubAssign" and method = "sub_assign"
57+
or
58+
op = "*=" and path = "core::ops::arith::MulAssign" and method = "mul_assign"
59+
or
60+
op = "/=" and path = "core::ops::arith::DivAssign" and method = "div_assign"
61+
or
62+
op = "%=" and path = "core::ops::arith::RemAssign" and method = "rem_assign"
63+
or
64+
// Bitwise operators
65+
op = "&" and path = "core::ops::bit::BitAnd" and method = "bitand"
66+
or
67+
op = "|" and path = "core::ops::bit::BitOr" and method = "bitor"
68+
or
69+
op = "^" and path = "core::ops::bit::BitXor" and method = "bitxor"
70+
or
71+
op = "<<" and path = "core::ops::bit::Shl" and method = "shl"
72+
or
73+
op = ">>" and path = "core::ops::bit::Shr" and method = "shr"
74+
or
75+
// Bitwise assignment operators
76+
op = "&=" and path = "core::ops::bit::BitAndAssign" and method = "bitand_assign"
77+
or
78+
op = "|=" and path = "core::ops::bit::BitOrAssign" and method = "bitor_assign"
79+
or
80+
op = "^=" and path = "core::ops::bit::BitXorAssign" and method = "bitxor_assign"
81+
or
82+
op = "<<=" and path = "core::ops::bit::ShlAssign" and method = "shl_assign"
83+
or
84+
op = ">>=" and path = "core::ops::bit::ShrAssign" and method = "shr_assign"
85+
)
8086
}
8187

8288
/**
@@ -109,7 +115,8 @@ module Impl {
109115
* trait `trait`.
110116
*/
111117
predicate isOverloaded(Trait trait, string methodName) {
112-
isOverloaded(this.getOperatorName(), trait.getCanonicalPath(), methodName)
118+
isOverloaded(this.getOperatorName(), this.getNumberOfOperands(), trait.getCanonicalPath(),
119+
methodName)
113120
}
114121
}
115122
}

0 commit comments

Comments
 (0)