Skip to content

Commit cfb4203

Browse files
committed
Rust: Add type inference test
1 parent 33e6310 commit cfb4203

File tree

5 files changed

+603
-488
lines changed

5 files changed

+603
-488
lines changed

ruby/ql/lib/codeql/ruby/ast/Call.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
private import codeql.ruby.AST
22
private import internal.AST
33
private import internal.Call
4+
private import internal.Literal
45
private import internal.TreeSitter
56
private import codeql.ruby.dataflow.internal.DataFlowDispatch
67
private import codeql.ruby.dataflow.internal.DataFlowImplCommon
@@ -41,7 +42,7 @@ class Call extends Expr instanceof CallImpl {
4142
final Expr getKeywordArgument(string keyword) {
4243
exists(Pair p |
4344
p = this.getAnArgument() and
44-
p.getKey().getConstantValue().isSymbol(keyword) and
45+
keyword = p.getKey().(SymbolLiteral).(StringlikeLiteralImpl).getStringValue() and
4546
result = p.getValue()
4647
)
4748
}

ruby/ql/lib/codeql/ruby/ast/Expr.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ private import codeql.ruby.AST
22
private import codeql.ruby.CFG
33
private import internal.AST
44
private import internal.Constant
5+
private import internal.Literal
56
private import internal.Expr
67
private import internal.TreeSitter
78

@@ -427,11 +428,11 @@ class StringConcatenation extends Expr, TStringConcatenation {
427428
*/
428429
final string getConcatenatedValueText() {
429430
forall(StringLiteral c | c = this.getString(_) |
430-
exists(c.getConstantValue().getStringlikeValue())
431+
exists(c.(StringlikeLiteralImpl).getStringValue())
431432
) and
432433
result =
433434
concat(string valueText, int i |
434-
valueText = this.getString(i).getConstantValue().getStringlikeValue()
435+
valueText = this.getString(i).(StringlikeLiteralImpl).getStringValue()
435436
|
436437
valueText order by i
437438
)

ruby/ql/lib/codeql/ruby/ast/Pattern.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ private import internal.Pattern
44
private import internal.TreeSitter
55
private import internal.Variable
66
private import internal.Parameter
7+
private import internal.Literal
78

89
private class TPatternNode =
910
TArrayPattern or TFindPattern or THashPattern or TAlternativePattern or TAsPattern or
@@ -205,7 +206,7 @@ class HashPattern extends CasePattern, THashPattern {
205206
/** Gets the value for a given key name. */
206207
CasePattern getValueByKey(string key) {
207208
exists(int i |
208-
this.getKey(i).getConstantValue().isStringlikeValue(key) and result = this.getValue(i)
209+
key = this.getKey(i).(StringlikeLiteralImpl).getStringValue() and result = this.getValue(i)
209210
)
210211
}
211212

rust/ql/test/library-tests/type-inference/main.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,11 @@ mod method_determined_by_argument_type {
20062006

20072007
// MyAdd<bool>::my_add
20082008
fn my_add(self, value: bool) -> Self {
2009-
if value { 1 } else { 0 }
2009+
if value {
2010+
1
2011+
} else {
2012+
0
2013+
}
20102014
}
20112015
}
20122016

@@ -2042,6 +2046,52 @@ mod method_determined_by_argument_type {
20422046
}
20432047
}
20442048

2049+
trait MyFrom<T> {
2050+
// MyFrom::my_from
2051+
fn my_from(value: T) -> Self;
2052+
}
2053+
2054+
impl MyFrom<i64> for i64 {
2055+
// MyFrom<i64>::my_from
2056+
fn my_from(value: i64) -> Self {
2057+
value
2058+
}
2059+
}
2060+
2061+
impl MyFrom<bool> for i64 {
2062+
// MyFrom<bool>::my_from
2063+
fn my_from(value: bool) -> Self {
2064+
if value {
2065+
1
2066+
} else {
2067+
0
2068+
}
2069+
}
2070+
}
2071+
2072+
trait MyFrom2<T> {
2073+
// MyFrom2::my_from2
2074+
fn my_from2(value: T, x: Self) -> ();
2075+
}
2076+
2077+
impl MyFrom2<i64> for i64 {
2078+
// MyFrom2<i64>::my_from2
2079+
fn my_from2(value: i64, _: Self) -> () {
2080+
value;
2081+
}
2082+
}
2083+
2084+
impl MyFrom2<bool> for i64 {
2085+
// MyFrom2<bool>::my_from2
2086+
fn my_from2(value: bool, _: Self) -> () {
2087+
if value {
2088+
1
2089+
} else {
2090+
0
2091+
};
2092+
}
2093+
}
2094+
20452095
pub fn f() {
20462096
let x: i64 = 73;
20472097
x.my_add(5i64); // $ method=MyAdd<i64>::my_add
@@ -2051,6 +2101,13 @@ mod method_determined_by_argument_type {
20512101
S(1i64).my_add(S(2i64)); // $ method=S::my_add1
20522102
S(1i64).my_add(3i64); // $ MISSING: method=S::my_add2
20532103
S(1i64).my_add(&3i64); // $ method=S::my_add3
2104+
2105+
let x = i64::my_from(73i64); // $ method=MyFrom<i64>::my_from $ SPURIOUS: method=MyFrom<bool>::my_from
2106+
let y = i64::my_from(true); // $ method=MyFrom<bool>::my_from $ SPURIOUS: method=MyFrom<i64>::my_from
2107+
let z: i64 = MyFrom::my_from(73i64); // $ MISSING: method=MyFrom<i64>::my_from $ SPURIOUS: method=MyFrom::my_from
2108+
i64::my_from2(73i64, 0i64); // $ method=MyFrom2<i64>::my_from2 $ SPURIOUS: method=MyFrom2<bool>::my_from2
2109+
i64::my_from2(true, 0i64); // $ method=MyFrom2<bool>::my_from2 $ SPURIOUS: method=MyFrom2<i64>::my_from2
2110+
MyFrom2::my_from2(73i64, 0i64); // $ MISSING: method=MyFrom2<i64>::my_from2 $ SPURIOUS: method=MyFrom2::my_from2
20542111
}
20552112
}
20562113

0 commit comments

Comments
 (0)