mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
Merge pull request #19593 from paldepind/rust/operator-overloading
Rust: Type inference for operator overloading
This commit is contained in:
@@ -28,6 +28,10 @@ module Impl {
|
||||
|
||||
override string getOperatorName() { result = Generated::BinaryExpr.super.getOperatorName() }
|
||||
|
||||
override Expr getAnOperand() { result = [this.getLhs(), this.getRhs()] }
|
||||
override Expr getOperand(int n) {
|
||||
n = 0 and result = this.getLhs()
|
||||
or
|
||||
n = 1 and result = this.getRhs()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,78 @@
|
||||
private import rust
|
||||
private import codeql.rust.elements.internal.ExprImpl::Impl as ExprImpl
|
||||
|
||||
/**
|
||||
* Holds if the operator `op` is overloaded to a trait with the canonical path
|
||||
* `path` and the method name `method`.
|
||||
*/
|
||||
private predicate isOverloaded(string op, string path, string method) {
|
||||
// Negation
|
||||
op = "-" and path = "core::ops::arith::Neg" and method = "neg"
|
||||
or
|
||||
// Not
|
||||
op = "!" and path = "core::ops::bit::Not" and method = "not"
|
||||
or
|
||||
// Dereference
|
||||
op = "*" and path = "core::ops::Deref" and method = "deref"
|
||||
or
|
||||
// Comparison operators
|
||||
op = "==" and path = "core::cmp::PartialEq" and method = "eq"
|
||||
or
|
||||
op = "!=" and path = "core::cmp::PartialEq" and method = "ne"
|
||||
or
|
||||
op = "<" and path = "core::cmp::PartialOrd" and method = "lt"
|
||||
or
|
||||
op = "<=" and path = "core::cmp::PartialOrd" and method = "le"
|
||||
or
|
||||
op = ">" and path = "core::cmp::PartialOrd" and method = "gt"
|
||||
or
|
||||
op = ">=" and path = "core::cmp::PartialOrd" and method = "ge"
|
||||
or
|
||||
// Arithmetic operators
|
||||
op = "+" and path = "core::ops::arith::Add" and method = "add"
|
||||
or
|
||||
op = "-" and path = "core::ops::arith::Sub" and method = "sub"
|
||||
or
|
||||
op = "*" and path = "core::ops::arith::Mul" and method = "mul"
|
||||
or
|
||||
op = "/" and path = "core::ops::arith::Div" and method = "div"
|
||||
or
|
||||
op = "%" and path = "core::ops::arith::Rem" and method = "rem"
|
||||
or
|
||||
// Arithmetic assignment expressions
|
||||
op = "+=" and path = "core::ops::arith::AddAssign" and method = "add_assign"
|
||||
or
|
||||
op = "-=" and path = "core::ops::arith::SubAssign" and method = "sub_assign"
|
||||
or
|
||||
op = "*=" and path = "core::ops::arith::MulAssign" and method = "mul_assign"
|
||||
or
|
||||
op = "/=" and path = "core::ops::arith::DivAssign" and method = "div_assign"
|
||||
or
|
||||
op = "%=" and path = "core::ops::arith::RemAssign" and method = "rem_assign"
|
||||
or
|
||||
// Bitwise operators
|
||||
op = "&" and path = "core::ops::bit::BitAnd" and method = "bitand"
|
||||
or
|
||||
op = "|" and path = "core::ops::bit::BitOr" and method = "bitor"
|
||||
or
|
||||
op = "^" and path = "core::ops::bit::BitXor" and method = "bitxor"
|
||||
or
|
||||
op = "<<" and path = "core::ops::bit::Shl" and method = "shl"
|
||||
or
|
||||
op = ">>" and path = "core::ops::bit::Shr" and method = "shr"
|
||||
or
|
||||
// Bitwise assignment operators
|
||||
op = "&=" and path = "core::ops::bit::BitAndAssign" and method = "bitand_assign"
|
||||
or
|
||||
op = "|=" and path = "core::ops::bit::BitOrAssign" and method = "bitor_assign"
|
||||
or
|
||||
op = "^=" and path = "core::ops::bit::BitXorAssign" and method = "bitxor_assign"
|
||||
or
|
||||
op = "<<=" and path = "core::ops::bit::ShlAssign" and method = "shl_assign"
|
||||
or
|
||||
op = ">>=" and path = "core::ops::bit::ShrAssign" and method = "shr_assign"
|
||||
}
|
||||
|
||||
/**
|
||||
* INTERNAL: This module contains the customizable definition of `Operation` and should not
|
||||
* be referenced directly.
|
||||
@@ -16,14 +88,28 @@ module Impl {
|
||||
* An operation, for example `&&`, `+=`, `!` or `*`.
|
||||
*/
|
||||
abstract class Operation extends ExprImpl::Expr {
|
||||
/**
|
||||
* Gets the operator name of this operation, if it exists.
|
||||
*/
|
||||
/** Gets the operator name of this operation, if it exists. */
|
||||
abstract string getOperatorName();
|
||||
|
||||
/** Gets the `n`th operand of this operation, if any. */
|
||||
abstract Expr getOperand(int n);
|
||||
|
||||
/**
|
||||
* Gets an operand of this operation.
|
||||
* Gets the number of operands of this operation.
|
||||
*
|
||||
* This is either 1 for prefix operations, or 2 for binary operations.
|
||||
*/
|
||||
abstract Expr getAnOperand();
|
||||
final int getNumberOfOperands() { result = strictcount(this.getAnOperand()) }
|
||||
|
||||
/** Gets an operand of this operation. */
|
||||
Expr getAnOperand() { result = this.getOperand(_) }
|
||||
|
||||
/**
|
||||
* Holds if this operation is overloaded to the method `methodName` of the
|
||||
* trait `trait`.
|
||||
*/
|
||||
predicate isOverloaded(Trait trait, string methodName) {
|
||||
isOverloaded(this.getOperatorName(), trait.getCanonicalPath(), methodName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,6 +26,6 @@ module Impl {
|
||||
|
||||
override string getOperatorName() { result = Generated::PrefixExpr.super.getOperatorName() }
|
||||
|
||||
override Expr getAnOperand() { result = this.getExpr() }
|
||||
override Expr getOperand(int n) { n = 0 and result = this.getExpr() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ module Impl {
|
||||
|
||||
override string getOperatorName() { result = "&" }
|
||||
|
||||
override Expr getAnOperand() { result = this.getExpr() }
|
||||
override Expr getOperand(int n) { n = 0 and result = this.getExpr() }
|
||||
|
||||
private string getSpecPart(int index) {
|
||||
index = 0 and this.isRaw() and result = "raw"
|
||||
|
||||
@@ -643,12 +643,22 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
|
||||
|
||||
private import codeql.rust.elements.internal.CallExprImpl::Impl as CallExprImpl
|
||||
|
||||
class Access extends CallExprBase {
|
||||
abstract class Access extends Expr {
|
||||
abstract Type getTypeArgument(TypeArgumentPosition apos, TypePath path);
|
||||
|
||||
abstract AstNode getNodeAt(AccessPosition apos);
|
||||
|
||||
abstract Type getInferredType(AccessPosition apos, TypePath path);
|
||||
|
||||
abstract Declaration getTarget();
|
||||
}
|
||||
|
||||
private class CallExprBaseAccess extends Access instanceof CallExprBase {
|
||||
private TypeMention getMethodTypeArg(int i) {
|
||||
result = this.(MethodCallExpr).getGenericArgList().getTypeArg(i)
|
||||
}
|
||||
|
||||
Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
|
||||
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
|
||||
exists(TypeMention arg | result = arg.resolveTypeAt(path) |
|
||||
arg = getExplicitTypeArgMention(CallExprImpl::getFunctionPath(this), apos.asTypeParam())
|
||||
or
|
||||
@@ -656,7 +666,7 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
|
||||
)
|
||||
}
|
||||
|
||||
AstNode getNodeAt(AccessPosition apos) {
|
||||
override AstNode getNodeAt(AccessPosition apos) {
|
||||
exists(int p, boolean isMethodCall |
|
||||
argPos(this, result, p, isMethodCall) and
|
||||
apos = TPositionalAccessPosition(p, isMethodCall)
|
||||
@@ -669,17 +679,42 @@ private module CallExprBaseMatchingInput implements MatchingInputSig {
|
||||
apos = TReturnAccessPosition()
|
||||
}
|
||||
|
||||
Type getInferredType(AccessPosition apos, TypePath path) {
|
||||
override Type getInferredType(AccessPosition apos, TypePath path) {
|
||||
result = inferType(this.getNodeAt(apos), path)
|
||||
}
|
||||
|
||||
Declaration getTarget() {
|
||||
override Declaration getTarget() {
|
||||
result = CallExprImpl::getResolvedFunction(this)
|
||||
or
|
||||
result = inferMethodCallTarget(this) // mutual recursion; resolving method calls requires resolving types and vice versa
|
||||
}
|
||||
}
|
||||
|
||||
private class OperationAccess extends Access instanceof Operation {
|
||||
OperationAccess() { super.isOverloaded(_, _) }
|
||||
|
||||
override Type getTypeArgument(TypeArgumentPosition apos, TypePath path) {
|
||||
// The syntax for operators does not allow type arguments.
|
||||
none()
|
||||
}
|
||||
|
||||
override AstNode getNodeAt(AccessPosition apos) {
|
||||
result = super.getOperand(0) and apos = TSelfAccessPosition()
|
||||
or
|
||||
result = super.getOperand(1) and apos = TPositionalAccessPosition(0, true)
|
||||
or
|
||||
result = this and apos = TReturnAccessPosition()
|
||||
}
|
||||
|
||||
override Type getInferredType(AccessPosition apos, TypePath path) {
|
||||
result = inferType(this.getNodeAt(apos), path)
|
||||
}
|
||||
|
||||
override Declaration getTarget() {
|
||||
result = inferMethodCallTarget(this) // mutual recursion; resolving method calls requires resolving types and vice versa
|
||||
}
|
||||
}
|
||||
|
||||
predicate accessDeclarationPositionMatch(AccessPosition apos, DeclarationPosition dpos) {
|
||||
apos.isSelf() and
|
||||
dpos.isSelf()
|
||||
@@ -1059,6 +1094,26 @@ private module MethodCall {
|
||||
pragma[nomagic]
|
||||
override Type getTypeAt(TypePath path) { result = inferType(receiver, path) }
|
||||
}
|
||||
|
||||
private class OperationMethodCall extends MethodCallImpl instanceof Operation {
|
||||
TraitItemNode trait;
|
||||
string methodName;
|
||||
|
||||
OperationMethodCall() { super.isOverloaded(trait, methodName) }
|
||||
|
||||
override string getMethodName() { result = methodName }
|
||||
|
||||
override int getArity() { result = this.(Operation).getNumberOfOperands() - 1 }
|
||||
|
||||
override Trait getTrait() { result = trait }
|
||||
|
||||
pragma[nomagic]
|
||||
override Type getTypeAt(TypePath path) {
|
||||
result = inferType(this.(BinaryExpr).getLhs(), path)
|
||||
or
|
||||
result = inferType(this.(PrefixExpr).getExpr(), path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
import MethodCall
|
||||
|
||||
@@ -765,11 +765,12 @@ mod method_supertraits {
|
||||
}
|
||||
|
||||
trait MyTrait2<Tr2>: MyTrait1<Tr2> {
|
||||
#[rustfmt::skip]
|
||||
fn m2(self) -> Tr2
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if 1 + 1 > 2 {
|
||||
if 3 > 2 { // $ method=gt
|
||||
self.m1() // $ method=MyTrait1::m1
|
||||
} else {
|
||||
Self::m1(self)
|
||||
@@ -778,11 +779,12 @@ mod method_supertraits {
|
||||
}
|
||||
|
||||
trait MyTrait3<Tr3>: MyTrait2<MyThing<Tr3>> {
|
||||
#[rustfmt::skip]
|
||||
fn m3(self) -> Tr3
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
if 1 + 1 > 2 {
|
||||
if 3 > 2 { // $ method=gt
|
||||
self.m2().a // $ method=m2 $ fieldof=MyThing
|
||||
} else {
|
||||
Self::m2(self).a // $ fieldof=MyThing
|
||||
@@ -1024,21 +1026,24 @@ mod option_methods {
|
||||
let x6 = MyOption::MySome(MyOption::<S>::MyNone());
|
||||
println!("{:?}", MyOption::<MyOption<S>>::flatten(x6));
|
||||
|
||||
let from_if = if 1 + 1 > 2 {
|
||||
#[rustfmt::skip]
|
||||
let from_if = if 3 > 2 { // $ method=gt
|
||||
MyOption::MyNone()
|
||||
} else {
|
||||
MyOption::MySome(S)
|
||||
};
|
||||
println!("{:?}", from_if);
|
||||
|
||||
let from_match = match 1 + 1 > 2 {
|
||||
#[rustfmt::skip]
|
||||
let from_match = match 3 > 2 { // $ method=gt
|
||||
true => MyOption::MyNone(),
|
||||
false => MyOption::MySome(S),
|
||||
};
|
||||
println!("{:?}", from_match);
|
||||
|
||||
#[rustfmt::skip]
|
||||
let from_loop = loop {
|
||||
if 1 + 1 > 2 {
|
||||
if 3 > 2 { // $ method=gt
|
||||
break MyOption::MyNone();
|
||||
}
|
||||
break MyOption::MySome(S);
|
||||
@@ -1240,7 +1245,7 @@ mod builtins {
|
||||
pub fn f() {
|
||||
let x: i32 = 1; // $ type=x:i32
|
||||
let y = 2; // $ type=y:i32
|
||||
let z = x + y; // $ MISSING: type=z:i32
|
||||
let z = x + y; // $ type=z:i32 method=add
|
||||
let z = x.abs(); // $ method=abs $ type=z:i32
|
||||
let c = 'c'; // $ type=c:char
|
||||
let hello = "Hello"; // $ type=hello:str
|
||||
@@ -1250,13 +1255,15 @@ mod builtins {
|
||||
}
|
||||
}
|
||||
|
||||
// Tests for non-overloaded operators.
|
||||
mod operators {
|
||||
pub fn f() {
|
||||
let x = true && false; // $ type=x:bool
|
||||
let y = true || false; // $ type=y:bool
|
||||
|
||||
let mut a;
|
||||
if 34 == 33 {
|
||||
let cond = 34 == 33; // $ method=eq
|
||||
if cond {
|
||||
let z = (a = 1); // $ type=z:() type=a:i32
|
||||
} else {
|
||||
a = 2; // $ type=a:i32
|
||||
@@ -1265,6 +1272,364 @@ mod operators {
|
||||
}
|
||||
}
|
||||
|
||||
// Tests for overloaded operators.
|
||||
mod overloadable_operators {
|
||||
use std::ops::*;
|
||||
// A vector type with overloaded operators.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
struct Vec2 {
|
||||
x: i64,
|
||||
y: i64,
|
||||
}
|
||||
// Implement all overloadable operators for Vec2
|
||||
impl Add for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::add
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
Vec2 {
|
||||
x: self.x + rhs.x, // $ fieldof=Vec2 method=add
|
||||
y: self.y + rhs.y, // $ fieldof=Vec2 method=add
|
||||
}
|
||||
}
|
||||
}
|
||||
impl AddAssign for Vec2 {
|
||||
// Vec2::add_assign
|
||||
#[rustfmt::skip]
|
||||
fn add_assign(&mut self, rhs: Self) {
|
||||
self.x += rhs.x; // $ fieldof=Vec2 method=add_assign
|
||||
self.y += rhs.y; // $ fieldof=Vec2 method=add_assign
|
||||
}
|
||||
}
|
||||
impl Sub for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::sub
|
||||
fn sub(self, rhs: Self) -> Self {
|
||||
Vec2 {
|
||||
x: self.x - rhs.x, // $ fieldof=Vec2 method=sub
|
||||
y: self.y - rhs.y, // $ fieldof=Vec2 method=sub
|
||||
}
|
||||
}
|
||||
}
|
||||
impl SubAssign for Vec2 {
|
||||
// Vec2::sub_assign
|
||||
#[rustfmt::skip]
|
||||
fn sub_assign(&mut self, rhs: Self) {
|
||||
self.x -= rhs.x; // $ fieldof=Vec2 method=sub_assign
|
||||
self.y -= rhs.y; // $ fieldof=Vec2 method=sub_assign
|
||||
}
|
||||
}
|
||||
impl Mul for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::mul
|
||||
fn mul(self, rhs: Self) -> Self {
|
||||
Vec2 {
|
||||
x: self.x * rhs.x, // $ fieldof=Vec2 method=mul
|
||||
y: self.y * rhs.y, // $ fieldof=Vec2 method=mul
|
||||
}
|
||||
}
|
||||
}
|
||||
impl MulAssign for Vec2 {
|
||||
// Vec2::mul_assign
|
||||
fn mul_assign(&mut self, rhs: Self) {
|
||||
self.x *= rhs.x; // $ fieldof=Vec2 method=mul_assign
|
||||
self.y *= rhs.y; // $ fieldof=Vec2 method=mul_assign
|
||||
}
|
||||
}
|
||||
impl Div for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::div
|
||||
fn div(self, rhs: Self) -> Self {
|
||||
Vec2 {
|
||||
x: self.x / rhs.x, // $ fieldof=Vec2 method=div
|
||||
y: self.y / rhs.y, // $ fieldof=Vec2 method=div
|
||||
}
|
||||
}
|
||||
}
|
||||
impl DivAssign for Vec2 {
|
||||
// Vec2::div_assign
|
||||
fn div_assign(&mut self, rhs: Self) {
|
||||
self.x /= rhs.x; // $ fieldof=Vec2 method=div_assign
|
||||
self.y /= rhs.y; // $ fieldof=Vec2 method=div_assign
|
||||
}
|
||||
}
|
||||
impl Rem for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::rem
|
||||
fn rem(self, rhs: Self) -> Self {
|
||||
Vec2 {
|
||||
x: self.x % rhs.x, // $ fieldof=Vec2 method=rem
|
||||
y: self.y % rhs.y, // $ fieldof=Vec2 method=rem
|
||||
}
|
||||
}
|
||||
}
|
||||
impl RemAssign for Vec2 {
|
||||
// Vec2::rem_assign
|
||||
fn rem_assign(&mut self, rhs: Self) {
|
||||
self.x %= rhs.x; // $ fieldof=Vec2 method=rem_assign
|
||||
self.y %= rhs.y; // $ fieldof=Vec2 method=rem_assign
|
||||
}
|
||||
}
|
||||
impl BitAnd for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::bitand
|
||||
fn bitand(self, rhs: Self) -> Self {
|
||||
Vec2 {
|
||||
x: self.x & rhs.x, // $ fieldof=Vec2 method=bitand
|
||||
y: self.y & rhs.y, // $ fieldof=Vec2 method=bitand
|
||||
}
|
||||
}
|
||||
}
|
||||
impl BitAndAssign for Vec2 {
|
||||
// Vec2::bitand_assign
|
||||
fn bitand_assign(&mut self, rhs: Self) {
|
||||
self.x &= rhs.x; // $ fieldof=Vec2 method=bitand_assign
|
||||
self.y &= rhs.y; // $ fieldof=Vec2 method=bitand_assign
|
||||
}
|
||||
}
|
||||
impl BitOr for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::bitor
|
||||
fn bitor(self, rhs: Self) -> Self {
|
||||
Vec2 {
|
||||
x: self.x | rhs.x, // $ fieldof=Vec2 method=bitor
|
||||
y: self.y | rhs.y, // $ fieldof=Vec2 method=bitor
|
||||
}
|
||||
}
|
||||
}
|
||||
impl BitOrAssign for Vec2 {
|
||||
// Vec2::bitor_assign
|
||||
fn bitor_assign(&mut self, rhs: Self) {
|
||||
self.x |= rhs.x; // $ fieldof=Vec2 method=bitor_assign
|
||||
self.y |= rhs.y; // $ fieldof=Vec2 method=bitor_assign
|
||||
}
|
||||
}
|
||||
impl BitXor for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::bitxor
|
||||
fn bitxor(self, rhs: Self) -> Self {
|
||||
Vec2 {
|
||||
x: self.x ^ rhs.x, // $ fieldof=Vec2 method=bitxor
|
||||
y: self.y ^ rhs.y, // $ fieldof=Vec2 method=bitxor
|
||||
}
|
||||
}
|
||||
}
|
||||
impl BitXorAssign for Vec2 {
|
||||
// Vec2::bitxor_assign
|
||||
fn bitxor_assign(&mut self, rhs: Self) {
|
||||
self.x ^= rhs.x; // $ fieldof=Vec2 method=bitxor_assign
|
||||
self.y ^= rhs.y; // $ fieldof=Vec2 method=bitxor_assign
|
||||
}
|
||||
}
|
||||
impl Shl<u32> for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::shl
|
||||
fn shl(self, rhs: u32) -> Self {
|
||||
Vec2 {
|
||||
x: self.x << rhs, // $ fieldof=Vec2 method=shl
|
||||
y: self.y << rhs, // $ fieldof=Vec2 method=shl
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ShlAssign<u32> for Vec2 {
|
||||
// Vec2::shl_assign
|
||||
fn shl_assign(&mut self, rhs: u32) {
|
||||
self.x <<= rhs; // $ fieldof=Vec2 method=shl_assign
|
||||
self.y <<= rhs; // $ fieldof=Vec2 method=shl_assign
|
||||
}
|
||||
}
|
||||
impl Shr<u32> for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::shr
|
||||
fn shr(self, rhs: u32) -> Self {
|
||||
Vec2 {
|
||||
x: self.x >> rhs, // $ fieldof=Vec2 method=shr
|
||||
y: self.y >> rhs, // $ fieldof=Vec2 method=shr
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ShrAssign<u32> for Vec2 {
|
||||
// Vec2::shr_assign
|
||||
fn shr_assign(&mut self, rhs: u32) {
|
||||
self.x >>= rhs; // $ fieldof=Vec2 method=shr_assign
|
||||
self.y >>= rhs; // $ fieldof=Vec2 method=shr_assign
|
||||
}
|
||||
}
|
||||
impl Neg for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::neg
|
||||
fn neg(self) -> Self {
|
||||
Vec2 {
|
||||
x: -self.x, // $ fieldof=Vec2 method=neg
|
||||
y: -self.y, // $ fieldof=Vec2 method=neg
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Not for Vec2 {
|
||||
type Output = Self;
|
||||
// Vec2::not
|
||||
fn not(self) -> Self {
|
||||
Vec2 {
|
||||
x: !self.x, // $ fieldof=Vec2 method=not
|
||||
y: !self.y, // $ fieldof=Vec2 method=not
|
||||
}
|
||||
}
|
||||
}
|
||||
impl PartialEq for Vec2 {
|
||||
// Vec2::eq
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.x == other.x && self.y == other.y // $ fieldof=Vec2 method=eq
|
||||
}
|
||||
// Vec2::ne
|
||||
fn ne(&self, other: &Self) -> bool {
|
||||
self.x != other.x || self.y != other.y // $ fieldof=Vec2 method=ne
|
||||
}
|
||||
}
|
||||
impl PartialOrd for Vec2 {
|
||||
// Vec2::partial_cmp
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
(self.x + self.y).partial_cmp(&(other.x + other.y)) // $ fieldof=Vec2 method=partial_cmp method=add
|
||||
}
|
||||
// Vec2::lt
|
||||
fn lt(&self, other: &Self) -> bool {
|
||||
self.x < other.x && self.y < other.y // $ fieldof=Vec2 method=lt
|
||||
}
|
||||
// Vec2::le
|
||||
fn le(&self, other: &Self) -> bool {
|
||||
self.x <= other.x && self.y <= other.y // $ fieldof=Vec2 method=le
|
||||
}
|
||||
// Vec2::gt
|
||||
fn gt(&self, other: &Self) -> bool {
|
||||
self.x > other.x && self.y > other.y // $ fieldof=Vec2 method=gt
|
||||
}
|
||||
// Vec2::ge
|
||||
fn ge(&self, other: &Self) -> bool {
|
||||
self.x >= other.x && self.y >= other.y // $ fieldof=Vec2 method=ge
|
||||
}
|
||||
}
|
||||
pub fn f() {
|
||||
// Test for all overloadable operators on `i64`
|
||||
|
||||
// Comparison operators
|
||||
let i64_eq = (1i64 == 2i64); // $ type=i64_eq:bool method=eq
|
||||
let i64_ne = (3i64 != 4i64); // $ type=i64_ne:bool method=ne
|
||||
let i64_lt = (5i64 < 6i64); // $ type=i64_lt:bool method=lt
|
||||
let i64_le = (7i64 <= 8i64); // $ type=i64_le:bool method=le
|
||||
let i64_gt = (9i64 > 10i64); // $ type=i64_gt:bool method=gt
|
||||
let i64_ge = (11i64 >= 12i64); // $ type=i64_ge:bool method=ge
|
||||
|
||||
// Arithmetic operators
|
||||
let i64_add = 13i64 + 14i64; // $ type=i64_add:i64 method=add
|
||||
let i64_sub = 15i64 - 16i64; // $ type=i64_sub:i64 method=sub
|
||||
let i64_mul = 17i64 * 18i64; // $ type=i64_mul:i64 method=mul
|
||||
let i64_div = 19i64 / 20i64; // $ type=i64_div:i64 method=div
|
||||
let i64_rem = 21i64 % 22i64; // $ type=i64_rem:i64 method=rem
|
||||
|
||||
// Arithmetic assignment operators
|
||||
let mut i64_add_assign = 23i64;
|
||||
i64_add_assign += 24i64; // $ method=add_assign
|
||||
|
||||
let mut i64_sub_assign = 25i64;
|
||||
i64_sub_assign -= 26i64; // $ method=sub_assign
|
||||
|
||||
let mut i64_mul_assign = 27i64;
|
||||
i64_mul_assign *= 28i64; // $ method=mul_assign
|
||||
|
||||
let mut i64_div_assign = 29i64;
|
||||
i64_div_assign /= 30i64; // $ method=div_assign
|
||||
|
||||
let mut i64_rem_assign = 31i64;
|
||||
i64_rem_assign %= 32i64; // $ method=rem_assign
|
||||
|
||||
// Bitwise operators
|
||||
let i64_bitand = 33i64 & 34i64; // $ type=i64_bitand:i64 method=bitand
|
||||
let i64_bitor = 35i64 | 36i64; // $ type=i64_bitor:i64 method=bitor
|
||||
let i64_bitxor = 37i64 ^ 38i64; // $ type=i64_bitxor:i64 method=bitxor
|
||||
let i64_shl = 39i64 << 40i64; // $ type=i64_shl:i64 method=shl
|
||||
let i64_shr = 41i64 >> 42i64; // $ type=i64_shr:i64 method=shr
|
||||
|
||||
// Bitwise assignment operators
|
||||
let mut i64_bitand_assign = 43i64;
|
||||
i64_bitand_assign &= 44i64; // $ method=bitand_assign
|
||||
|
||||
let mut i64_bitor_assign = 45i64;
|
||||
i64_bitor_assign |= 46i64; // $ method=bitor_assign
|
||||
|
||||
let mut i64_bitxor_assign = 47i64;
|
||||
i64_bitxor_assign ^= 48i64; // $ method=bitxor_assign
|
||||
|
||||
let mut i64_shl_assign = 49i64;
|
||||
i64_shl_assign <<= 50i64; // $ method=shl_assign
|
||||
|
||||
let mut i64_shr_assign = 51i64;
|
||||
i64_shr_assign >>= 52i64; // $ method=shr_assign
|
||||
|
||||
let i64_neg = -53i64; // $ type=i64_neg:i64 method=neg
|
||||
let i64_not = !54i64; // $ type=i64_not:i64 method=not
|
||||
|
||||
// Test for all overloadable operators on Vec2
|
||||
let v1 = Vec2 { x: 1, y: 2 };
|
||||
let v2 = Vec2 { x: 3, y: 4 };
|
||||
|
||||
// Comparison operators
|
||||
let vec2_eq = v1 == v2; // $ type=vec2_eq:bool method=Vec2::eq
|
||||
let vec2_ne = v1 != v2; // $ type=vec2_ne:bool method=Vec2::ne
|
||||
let vec2_lt = v1 < v2; // $ type=vec2_lt:bool method=Vec2::lt
|
||||
let vec2_le = v1 <= v2; // $ type=vec2_le:bool method=Vec2::le
|
||||
let vec2_gt = v1 > v2; // $ type=vec2_gt:bool method=Vec2::gt
|
||||
let vec2_ge = v1 >= v2; // $ type=vec2_ge:bool method=Vec2::ge
|
||||
|
||||
// Arithmetic operators
|
||||
let vec2_add = v1 + v2; // $ type=vec2_add:Vec2 method=Vec2::add
|
||||
let vec2_sub = v1 - v2; // $ type=vec2_sub:Vec2 method=Vec2::sub
|
||||
let vec2_mul = v1 * v2; // $ type=vec2_mul:Vec2 method=Vec2::mul
|
||||
let vec2_div = v1 / v2; // $ type=vec2_div:Vec2 method=Vec2::div
|
||||
let vec2_rem = v1 % v2; // $ type=vec2_rem:Vec2 method=Vec2::rem
|
||||
|
||||
// Arithmetic assignment operators
|
||||
let mut vec2_add_assign = v1;
|
||||
vec2_add_assign += v2; // $ method=Vec2::add_assign
|
||||
|
||||
let mut vec2_sub_assign = v1;
|
||||
vec2_sub_assign -= v2; // $ method=Vec2::sub_assign
|
||||
|
||||
let mut vec2_mul_assign = v1;
|
||||
vec2_mul_assign *= v2; // $ method=Vec2::mul_assign
|
||||
|
||||
let mut vec2_div_assign = v1;
|
||||
vec2_div_assign /= v2; // $ method=Vec2::div_assign
|
||||
|
||||
let mut vec2_rem_assign = v1;
|
||||
vec2_rem_assign %= v2; // $ method=Vec2::rem_assign
|
||||
|
||||
// Bitwise operators
|
||||
let vec2_bitand = v1 & v2; // $ type=vec2_bitand:Vec2 method=Vec2::bitand
|
||||
let vec2_bitor = v1 | v2; // $ type=vec2_bitor:Vec2 method=Vec2::bitor
|
||||
let vec2_bitxor = v1 ^ v2; // $ type=vec2_bitxor:Vec2 method=Vec2::bitxor
|
||||
let vec2_shl = v1 << 1u32; // $ type=vec2_shl:Vec2 method=Vec2::shl
|
||||
let vec2_shr = v1 >> 1u32; // $ type=vec2_shr:Vec2 method=Vec2::shr
|
||||
|
||||
// Bitwise assignment operators
|
||||
let mut vec2_bitand_assign = v1;
|
||||
vec2_bitand_assign &= v2; // $ method=Vec2::bitand_assign
|
||||
|
||||
let mut vec2_bitor_assign = v1;
|
||||
vec2_bitor_assign |= v2; // $ method=Vec2::bitor_assign
|
||||
|
||||
let mut vec2_bitxor_assign = v1;
|
||||
vec2_bitxor_assign ^= v2; // $ method=Vec2::bitxor_assign
|
||||
|
||||
let mut vec2_shl_assign = v1;
|
||||
vec2_shl_assign <<= 1u32; // $ method=Vec2::shl_assign
|
||||
|
||||
let mut vec2_shr_assign = v1;
|
||||
vec2_shr_assign >>= 1u32; // $ method=Vec2::shr_assign
|
||||
|
||||
// Prefix operators
|
||||
let vec2_neg = -v1; // $ type=vec2_neg:Vec2 method=Vec2::neg
|
||||
let vec2_not = !v1; // $ type=vec2_not:Vec2 method=Vec2::not
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
field_access::f();
|
||||
method_impl::f();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user