Skip to content

Rust: pick correct edition for the files #19291

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

Merged
merged 4 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 59 additions & 58 deletions rust/extractor/src/rust_analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use itertools::Itertools;
use ra_ap_base_db::{EditionedFileId, RootQueryDb, SourceDatabase};
use ra_ap_base_db::{EditionedFileId, FileText, RootQueryDb, SourceDatabase};
use ra_ap_hir::Semantics;
use ra_ap_ide_db::RootDatabase;
use ra_ap_load_cargo::{LoadCargoConfig, load_workspace_at};
use ra_ap_paths::{AbsPath, Utf8PathBuf};
use ra_ap_project_model::ProjectManifest;
use ra_ap_project_model::{CargoConfig, ManifestPath};
use ra_ap_span::Edition;
use ra_ap_span::EditionedFileId as SpanEditionedFileId;
use ra_ap_span::TextRange;
use ra_ap_span::TextSize;
use ra_ap_syntax::SourceFile;
Expand Down Expand Up @@ -54,7 +53,6 @@ impl<'a> RustAnalyzer<'a> {
) -> Option<(RootDatabase, Vfs)> {
let progress = |t| (trace!("progress: {}", t));
let manifest = project.manifest_path();

match load_workspace_at(manifest.as_ref(), config, load_config, &progress) {
Ok((db, vfs, _macro_server)) => Some((db, vfs)),
Err(err) => {
Expand All @@ -66,67 +64,70 @@ impl<'a> RustAnalyzer<'a> {
pub fn new(vfs: &'a Vfs, semantics: &'a Semantics<'a, RootDatabase>) -> Self {
RustAnalyzer::WithSemantics { vfs, semantics }
}
pub fn parse(&self, path: &Path) -> ParseResult {
let no_semantics_reason;
fn get_file_data(
&self,
path: &Path,
) -> Result<(&Semantics<RootDatabase>, EditionedFileId, FileText), &str> {
match self {
RustAnalyzer::WithoutSemantics { reason } => Err(reason),
RustAnalyzer::WithSemantics { vfs, semantics } => {
if let Some(file_id) = path_to_file_id(path, vfs) {
if let Ok(input) = std::panic::catch_unwind(|| semantics.db.file_text(file_id))
{
let file_id = EditionedFileId::new(
semantics.db,
SpanEditionedFileId::current_edition(file_id),
);
let source_file = semantics.parse(file_id);
let errors = semantics
.db
.parse_errors(file_id)
.into_iter()
.flat_map(|x| x.to_vec())
.collect();

return ParseResult {
ast: source_file,
text: input.text(semantics.db),
errors,
semantics_info: Ok(FileSemanticInformation { file_id, semantics }),
};
}
debug!(
"No text available for file_id '{:?}', falling back to loading file '{}' from disk.",
file_id,
path.to_string_lossy()
);
no_semantics_reason = "no text available for the file in the project";
} else {
no_semantics_reason = "file not found in project";
}
}
RustAnalyzer::WithoutSemantics { reason } => {
no_semantics_reason = reason;
let file_id = path_to_file_id(path, vfs).ok_or("file not found in project")?;
let input = std::panic::catch_unwind(|| semantics.db.file_text(file_id))
.or(Err("no text available for the file in the project"))?;
let editioned_file_id = semantics
.attach_first_edition(file_id)
.ok_or("failed to determine rust edition")?;
Ok((
semantics,
EditionedFileId::new(semantics.db, editioned_file_id),
input,
))
}
}
let mut errors = Vec::new();
let input = match std::fs::read(path) {
Ok(data) => data,
Err(e) => {
errors.push(SyntaxError::new(
format!("Could not read {}: {}", path.to_string_lossy(), e),
TextRange::empty(TextSize::default()),
));
vec![]
}

pub fn parse(&self, path: &Path) -> ParseResult {
match self.get_file_data(path) {
Ok((semantics, file_id, input)) => {
let source_file = semantics.parse(file_id);
let errors = semantics
.db
.parse_errors(file_id)
.into_iter()
.flat_map(|x| x.to_vec())
.collect();

ParseResult {
ast: source_file,
text: input.text(semantics.db),
errors,
semantics_info: Ok(FileSemanticInformation { file_id, semantics }),
}
}
};
let (input, err) = from_utf8_lossy(&input);
Err(reason) => {
let mut errors = Vec::new();
let input = match std::fs::read(path) {
Ok(data) => data,
Err(e) => {
errors.push(SyntaxError::new(
format!("Could not read {}: {}", path.to_string_lossy(), e),
TextRange::empty(TextSize::default()),
));
vec![]
}
};
let (input, err) = from_utf8_lossy(&input);

let parse = ra_ap_syntax::ast::SourceFile::parse(&input, Edition::CURRENT);
errors.extend(parse.errors());
errors.extend(err);
ParseResult {
ast: parse.tree(),
text: input.as_ref().into(),
errors,
semantics_info: Err(no_semantics_reason),
let parse = ra_ap_syntax::ast::SourceFile::parse(&input, Edition::CURRENT);
errors.extend(parse.errors());
errors.extend(err);
ParseResult {
ast: parse.tree(),
text: input.as_ref().into(),
errors,
semantics_info: Err(reason),
}
}
}
}
}
Expand Down
32 changes: 29 additions & 3 deletions rust/ql/integration-tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,55 @@
import json
import commands
import pathlib
import tomllib


@pytest.fixture(params=[2018, 2021, 2024])
def rust_edition(request):
return request.param


@pytest.fixture
def cargo(cwd):
assert (cwd / "Cargo.toml").exists()
def cargo(cwd, rust_edition):
manifest_file = cwd / "Cargo.toml"
assert manifest_file.exists()
(cwd / "rust-project.json").unlink(missing_ok=True)

def update(file):
contents = file.read_text()
m = tomllib.loads(contents)
if 'package' in m:
# tomllib does not support writing, and we don't want to use further dependencies
# so we just do a dumb search and replace
contents = contents.replace(f'edition = "{m["package"]["edition"]}"', f'edition = "{rust_edition}"')
file.write_text(contents)
if 'members' in m.get('workspace', ()):
for member in m['workspace']['members']:
update(file.parent / member / "Cargo.toml")

update(manifest_file)


@pytest.fixture(scope="session")
def rust_sysroot_src() -> str:
rust_sysroot = pathlib.Path(commands.run("rustc --print sysroot", _capture=True))
ret = rust_sysroot.joinpath("lib", "rustlib", "src", "rust", "library")
assert ret.exists()
return str(ret)


@pytest.fixture
def rust_project(cwd, rust_sysroot_src):
def rust_project(cwd, rust_sysroot_src, rust_edition):
project_file = cwd / "rust-project.json"
assert project_file.exists()
project = json.loads(project_file.read_text())
project["sysroot_src"] = rust_sysroot_src
for c in project["crates"]:
c["edition"] = str(rust_edition)
project_file.write_text(json.dumps(project, indent=4))
(cwd / "Cargo.toml").unlink(missing_ok=True)


@pytest.fixture
def rust_check_diagnostics(check_diagnostics):
check_diagnostics.redact += [
Expand Down
2 changes: 1 addition & 1 deletion rust/ql/integration-tests/hello-project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
[package]
name = "hello-cargo"
version = "0.1.0"
edition = "2021"
edition = "2021" # replaced in test

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "exe"
version = "0.1.0"
edition = "2021"
edition = "2021" # replaced in test

[dependencies]
lib = { path = "../lib" }
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lib"
version = "0.1.0"
edition = "2021"
edition = "2021" # replaced in test

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest


@pytest.mark.ql_test("steps.ql", expected=".cargo.expected")
@pytest.mark.ql_test("summary.qlref", expected=".cargo.expected")
def test_cargo(codeql, rust, cargo, check_source_archive, rust_check_diagnostics):
Expand Down