Skip to content

Commit 7a3f4a6

Browse files
authored
Merge branch 'main' into aibaars/rust-expand-assoc-items
2 parents 2acce96 + b234d77 commit 7a3f4a6

26 files changed

+2057
-1577
lines changed

rust/extractor/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub struct Config {
6262
pub qltest: bool,
6363
pub qltest_cargo_check: bool,
6464
pub qltest_dependencies: Vec<String>,
65+
pub qltest_use_nightly: bool,
6566
pub sysroot: Option<PathBuf>,
6667
pub sysroot_src: Option<PathBuf>,
6768
pub rustc_src: Option<PathBuf>,

rust/extractor/src/qltest.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use std::path::Path;
88
use std::process::Command;
99
use tracing::info;
1010

11+
const EDITION: &str = "2021";
12+
const NIGHTLY: &str = "nightly-2025-06-01";
13+
1114
fn dump_lib() -> anyhow::Result<()> {
1215
let path_iterator = glob("*.rs").context("globbing test sources")?;
1316
let paths = path_iterator
@@ -29,8 +32,11 @@ enum TestCargoManifest<'a> {
2932
uses_proc_macro: bool,
3033
uses_main: bool,
3134
dependencies: &'a [String],
35+
edition: &'a str,
36+
},
37+
Macro {
38+
edition: &'a str,
3239
},
33-
Macro {},
3440
}
3541

3642
impl TestCargoManifest<'_> {
@@ -56,16 +62,26 @@ fn dump_cargo_manifest(dependencies: &[String]) -> anyhow::Result<()> {
5662
uses_proc_macro,
5763
uses_main: fs::exists("main.rs").context("checking existence of main.rs")?,
5864
dependencies,
65+
edition: EDITION,
5966
};
6067
if uses_proc_macro {
6168
TestCargoManifest::Workspace {}.dump("")?;
6269
lib_manifest.dump(".lib")?;
63-
TestCargoManifest::Macro {}.dump(".proc_macro")
70+
TestCargoManifest::Macro { edition: EDITION }.dump(".proc_macro")
6471
} else {
6572
lib_manifest.dump("")
6673
}
6774
}
6875

76+
fn dump_nightly_toolchain() -> anyhow::Result<()> {
77+
fs::write(
78+
"rust-toolchain.toml",
79+
format!("[toolchain]\nchannel = \"{NIGHTLY}\"\n"),
80+
)
81+
.context("writing rust-toolchain.toml")?;
82+
Ok(())
83+
}
84+
6985
fn set_sources(config: &mut Config) -> anyhow::Result<()> {
7086
let path_iterator = glob("**/*.rs").context("globbing test sources")?;
7187
config.inputs = path_iterator
@@ -79,6 +95,9 @@ pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
7995
dump_lib()?;
8096
set_sources(config)?;
8197
dump_cargo_manifest(&config.qltest_dependencies)?;
98+
if config.qltest_use_nightly {
99+
dump_nightly_toolchain()?;
100+
}
82101
if config.qltest_cargo_check {
83102
let status = Command::new("cargo")
84103
.env("RUSTFLAGS", "-Awarnings")

rust/extractor/src/qltest_cargo.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ members = [".lib", ".proc_macro"]
1111
[package]
1212
name = "test"
1313
version = "0.0.1"
14-
edition = "2021"
14+
edition = "{{ edition }}"
1515
[lib]
1616
path = "{{#uses_proc_macro}}../{{/uses_proc_macro}}lib.rs"
1717
{{#uses_main}}
@@ -32,7 +32,7 @@ proc_macro = { path = "../.proc_macro" }
3232
[package]
3333
name = "proc_macro"
3434
version = "0.0.1"
35-
edition = "2021"
35+
edition = "{{ edition }}"
3636
[lib]
3737
path = "../proc_macro.rs"
3838
proc_macro = true

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

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module Impl {
3535
*/
3636
abstract class Call extends ExprImpl::Expr {
3737
/** Holds if the receiver of this call is implicitly borrowed. */
38-
predicate receiverImplicitlyBorrowed() { this.implicitBorrowAt(TSelfArgumentPosition()) }
38+
predicate receiverImplicitlyBorrowed() { this.implicitBorrowAt(TSelfArgumentPosition(), _) }
3939

4040
/** Gets the trait targeted by this call, if any. */
4141
abstract Trait getTrait();
@@ -47,7 +47,7 @@ module Impl {
4747
abstract Expr getArgument(ArgumentPosition pos);
4848

4949
/** Holds if the argument at `pos` might be implicitly borrowed. */
50-
abstract predicate implicitBorrowAt(ArgumentPosition pos);
50+
abstract predicate implicitBorrowAt(ArgumentPosition pos, boolean certain);
5151

5252
/** Gets the number of arguments _excluding_ any `self` argument. */
5353
int getNumberOfArguments() { result = count(this.getArgument(TPositionalArgumentPosition(_))) }
@@ -85,7 +85,7 @@ module Impl {
8585

8686
override Trait getTrait() { none() }
8787

88-
override predicate implicitBorrowAt(ArgumentPosition pos) { none() }
88+
override predicate implicitBorrowAt(ArgumentPosition pos, boolean certain) { none() }
8989

9090
override Expr getArgument(ArgumentPosition pos) {
9191
result = super.getArgList().getArg(pos.asPosition())
@@ -109,7 +109,7 @@ module Impl {
109109
qualifier.toString() != "Self"
110110
}
111111

112-
override predicate implicitBorrowAt(ArgumentPosition pos) { none() }
112+
override predicate implicitBorrowAt(ArgumentPosition pos, boolean certain) { none() }
113113

114114
override Expr getArgument(ArgumentPosition pos) {
115115
pos.isSelf() and result = super.getArgList().getArg(0)
@@ -123,7 +123,9 @@ module Impl {
123123

124124
override Trait getTrait() { none() }
125125

126-
override predicate implicitBorrowAt(ArgumentPosition pos) { pos.isSelf() }
126+
override predicate implicitBorrowAt(ArgumentPosition pos, boolean certain) {
127+
pos.isSelf() and certain = false
128+
}
127129

128130
override Expr getArgument(ArgumentPosition pos) {
129131
pos.isSelf() and result = this.(MethodCallExpr).getReceiver()
@@ -143,10 +145,13 @@ module Impl {
143145

144146
override Trait getTrait() { result = trait }
145147

146-
override predicate implicitBorrowAt(ArgumentPosition pos) {
147-
pos.isSelf() and borrows >= 1
148-
or
149-
pos.asPosition() = 0 and borrows = 2
148+
override predicate implicitBorrowAt(ArgumentPosition pos, boolean certain) {
149+
(
150+
pos.isSelf() and borrows >= 1
151+
or
152+
pos.asPosition() = 0 and borrows = 2
153+
) and
154+
certain = true
150155
}
151156

152157
override Expr getArgument(ArgumentPosition pos) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private predicate isOverloaded(string op, int arity, string path, string method,
2222
op = "!" and path = "core::ops::bit::Not" and method = "not" and borrows = 0
2323
or
2424
// Dereference
25-
op = "*" and path = "core::ops::deref::Deref" and method = "deref" and borrows = 0
25+
op = "*" and path = "core::ops::deref::Deref" and method = "deref" and borrows = 1
2626
)
2727
or
2828
arity = 2 and

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ abstract class ItemNode extends Locatable {
205205
else result = this.getImmediateParentModule().getImmediateParentModule()
206206
or
207207
name = "self" and
208-
if this instanceof Module or this instanceof Enum or this instanceof Struct
208+
if
209+
this instanceof Module or
210+
this instanceof Enum or
211+
this instanceof Struct or
212+
this instanceof Crate
209213
then result = this
210214
else result = this.getImmediateParentModule()
211215
or

0 commit comments

Comments
 (0)