-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Rust: Type inference and path resolution for builtins #19474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /** | ||
| * Provides classes for builtins. | ||
| */ | ||
|
|
||
| private import rust | ||
|
|
||
| /** The folder containing builtins. */ | ||
| class BuiltinsFolder extends Folder { | ||
| BuiltinsFolder() { | ||
| this.getBaseName() = "builtins" and | ||
| this.getParentContainer().getBaseName() = "tools" | ||
| } | ||
| } | ||
|
|
||
| private class BuiltinsTypesFile extends File { | ||
| BuiltinsTypesFile() { | ||
| this.getBaseName() = "types.rs" and | ||
| this.getParentContainer() instanceof BuiltinsFolder | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A builtin type, such as `bool` and `i32`. | ||
| * | ||
| * Builtin types are represented as structs. | ||
| */ | ||
| class BuiltinType extends Struct { | ||
| BuiltinType() { this.getFile() instanceof BuiltinsTypesFile } | ||
|
|
||
| /** Gets the name of this type. */ | ||
| string getName() { result = super.getName().getText() } | ||
| } | ||
|
|
||
| /** The builtin `bool` type. */ | ||
| class Bool extends BuiltinType { | ||
| Bool() { this.getName() = "bool" } | ||
| } | ||
|
|
||
| /** The builtin `char` type. */ | ||
| class Char extends BuiltinType { | ||
| Char() { this.getName() = "char" } | ||
| } | ||
|
|
||
| /** The builtin `str` type. */ | ||
| class Str extends BuiltinType { | ||
| Str() { this.getName() = "str" } | ||
| } | ||
|
|
||
| /** The builtin `i8` type. */ | ||
| class I8 extends BuiltinType { | ||
| I8() { this.getName() = "i8" } | ||
| } | ||
|
|
||
| /** The builtin `i16` type. */ | ||
| class I16 extends BuiltinType { | ||
| I16() { this.getName() = "i16" } | ||
| } | ||
|
|
||
| /** The builtin `i32` type. */ | ||
| class I32 extends BuiltinType { | ||
| I32() { this.getName() = "i32" } | ||
| } | ||
|
|
||
| /** The builtin `i64` type. */ | ||
| class I64 extends BuiltinType { | ||
| I64() { this.getName() = "i64" } | ||
| } | ||
|
|
||
| /** The builtin `i128` type. */ | ||
| class I128 extends BuiltinType { | ||
| I128() { this.getName() = "i128" } | ||
| } | ||
|
|
||
| /** The builtin `u8` type. */ | ||
| class U8 extends BuiltinType { | ||
| U8() { this.getName() = "u8" } | ||
| } | ||
|
|
||
| /** The builtin `u16` type. */ | ||
| class U16 extends BuiltinType { | ||
| U16() { this.getName() = "u16" } | ||
| } | ||
|
|
||
| /** The builtin `u32` type. */ | ||
| class U32 extends BuiltinType { | ||
| U32() { this.getName() = "u32" } | ||
| } | ||
|
|
||
| /** The builtin `u64` type. */ | ||
| class U64 extends BuiltinType { | ||
| U64() { this.getName() = "u64" } | ||
| } | ||
|
|
||
| /** The builtin `u128` type. */ | ||
| class U128 extends BuiltinType { | ||
| U128() { this.getName() = "u128" } | ||
| } | ||
|
|
||
| /** The builtin `usize` type. */ | ||
| class USize extends BuiltinType { | ||
| USize() { this.getName() = "usize" } | ||
| } | ||
|
|
||
| /** The builtin `isize` type. */ | ||
| class ISize extends BuiltinType { | ||
| ISize() { this.getName() = "isize" } | ||
| } | ||
|
|
||
| /** The builtin `f32` type. */ | ||
| class F32 extends BuiltinType { | ||
| F32() { this.getName() = "f32" } | ||
| } | ||
|
|
||
| /** The builtin `f64` type. */ | ||
| class F64 extends BuiltinType { | ||
| F64() { this.getName() = "f64" } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -885,6 +885,41 @@ private Type inferTryExprType(TryExpr te, TypePath path) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private import codeql.rust.frameworks.stdlib.Bultins as Builtins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pragma[nomagic] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StructType getBuiltinType(string name) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = TStruct(any(Builtins::BuiltinType t | name = t.getName())) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pragma[nomagic] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private StructType inferLiteralType(LiteralExpr le) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| le instanceof CharLiteralExpr and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = TStruct(any(Builtins::Char t)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| le instanceof StringLiteralExpr and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = TStruct(any(Builtins::Str t)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| le = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| any(IntegerLiteralExpr n | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| not exists(n.getSuffix()) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = getBuiltinType("i32") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = getBuiltinType(n.getSuffix()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| le = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| any(FloatLiteralExpr n | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| not exists(n.getSuffix()) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = getBuiltinType("f32") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = getBuiltinType(n.getSuffix()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| le instanceof BooleanLiteralExpr and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = TStruct(any(Builtins::Bool t)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pragma[nomagic] | |
| StructType getBuiltinType(string name) { | |
| result = TStruct(any(Builtins::BuiltinType t | name = t.getName())) | |
| } | |
| pragma[nomagic] | |
| private StructType inferLiteralType(LiteralExpr le) { | |
| le instanceof CharLiteralExpr and | |
| result = TStruct(any(Builtins::Char t)) | |
| or | |
| le instanceof StringLiteralExpr and | |
| result = TStruct(any(Builtins::Str t)) | |
| or | |
| le = | |
| any(IntegerLiteralExpr n | | |
| not exists(n.getSuffix()) and | |
| result = getBuiltinType("i32") | |
| or | |
| result = getBuiltinType(n.getSuffix()) | |
| ) | |
| or | |
| le = | |
| any(FloatLiteralExpr n | | |
| not exists(n.getSuffix()) and | |
| result = getBuiltinType("f32") | |
| or | |
| result = getBuiltinType(n.getSuffix()) | |
| ) | |
| or | |
| le instanceof BooleanLiteralExpr and | |
| result = TStruct(any(Builtins::Bool t)) | |
| } | |
| pragma[nomagic] | |
| private StructType inferLiteralType(LiteralExpr le) { | |
| exists(Builtins::BuiltinType t | result = TStruct(t) | | |
| le instanceof CharLiteralExpr and | |
| t instanceof Builtins::Char | |
| or | |
| le instanceof StringLiteralExpr and | |
| t instanceof Builtins::Str | |
| or | |
| le = | |
| any(NumberLiteralExpr ne | | |
| t.getName() = ne.getSuffix() | |
| or | |
| not exists(ne.getSuffix()) and | |
| ( | |
| ne instanceof IntegerLiteralExpr and | |
| t instanceof Builtins::I32 | |
| or | |
| ne instanceof FloatLiteralExpr and | |
| t instanceof Builtins::F64 | |
| ) | |
| ) | |
| or | |
| le instanceof BooleanLiteralExpr and | |
| t instanceof Builtins::Bool | |
| ) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| multiplePathResolutions | ||
| | main.rs:532:10:532:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:532:10:532:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:532:10:532:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:532:10:532:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:532:10:532:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:532:10:532:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:532:10:532:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:538:10:538:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:538:10:538:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:538:10:538:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:538:10:538:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:538:10:538:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:538:10:538:18 | ...::from | file://:0:0:0:0 | fn from | | ||
| | main.rs:538:10:538:18 | ...::from | file://:0:0:0:0 | fn from | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that the Rust type is written as one word in all lower-case (and not something like
iSizeori_size) I'd expect the the QL name to beIsize? Same forusizeabove.