Skip to content

Commit

Permalink
Merge pull request #18906 from Veykril/push-upuxsyovskmt
Browse files Browse the repository at this point in the history
feat: Re-implement rust string highlighting via tool attribute
  • Loading branch information
Veykril authored Jan 10, 2025
2 parents 897f7e5 + bf669da commit e8ad0c0
Show file tree
Hide file tree
Showing 72 changed files with 386 additions and 173 deletions.
4 changes: 2 additions & 2 deletions crates/hir-def/src/body/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ mod tests {
}
}

fn do_check(ra_fixture: &str, expected: &[&str]) {
fn do_check(#[rust_analyzer::rust_fixture] ra_fixture: &str, expected: &[&str]) {
let (offset, code) = extract_offset(ra_fixture);
let code = {
let mut buf = String::new();
Expand Down Expand Up @@ -509,7 +509,7 @@ fn foo() {
);
}

fn do_check_local_name(ra_fixture: &str, expected_offset: u32) {
fn do_check_local_name(#[rust_analyzer::rust_fixture] ra_fixture: &str, expected_offset: u32) {
let (db, position) = TestDB::with_position(ra_fixture);
let file_id = position.file_id;
let offset = position.offset;
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-def/src/body/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{test_db::TestDB, ModuleDefId};

use super::*;

fn lower(ra_fixture: &str) -> (TestDB, Arc<Body>, DefWithBodyId) {
fn lower(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> (TestDB, Arc<Body>, DefWithBodyId) {
let db = TestDB::with_files(ra_fixture);

let krate = db.fetch_test_crate();
Expand All @@ -27,22 +27,22 @@ fn lower(ra_fixture: &str) -> (TestDB, Arc<Body>, DefWithBodyId) {
(db, body, fn_def)
}

fn def_map_at(ra_fixture: &str) -> String {
fn def_map_at(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> String {
let (db, position) = TestDB::with_position(ra_fixture);

let module = db.module_at_position(position);
module.def_map(&db).dump(&db)
}

fn check_block_scopes_at(ra_fixture: &str, expect: Expect) {
fn check_block_scopes_at(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let (db, position) = TestDB::with_position(ra_fixture);

let module = db.module_at_position(position);
let actual = module.def_map(&db).dump_block_scopes(&db);
expect.assert_eq(&actual);
}

fn check_at(ra_fixture: &str, expect: Expect) {
fn check_at(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let actual = def_map_at(ra_fixture);
expect.assert_eq(&actual);
}
Expand Down
26 changes: 21 additions & 5 deletions crates/hir-def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ mod tests {
/// module the cursor is in.
#[track_caller]
fn check_found_path_(
ra_fixture: &str,
#[rust_analyzer::rust_fixture] ra_fixture: &str,
path: &str,
prefer_prelude: bool,
prefer_absolute: bool,
Expand Down Expand Up @@ -727,19 +727,35 @@ mod tests {
expect.assert_eq(&res);
}

fn check_found_path(ra_fixture: &str, path: &str, expect: Expect) {
fn check_found_path(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
path: &str,
expect: Expect,
) {
check_found_path_(ra_fixture, path, false, false, false, expect);
}

fn check_found_path_prelude(ra_fixture: &str, path: &str, expect: Expect) {
fn check_found_path_prelude(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
path: &str,
expect: Expect,
) {
check_found_path_(ra_fixture, path, true, false, false, expect);
}

fn check_found_path_absolute(ra_fixture: &str, path: &str, expect: Expect) {
fn check_found_path_absolute(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
path: &str,
expect: Expect,
) {
check_found_path_(ra_fixture, path, false, true, false, expect);
}

fn check_found_path_prefer_no_std(ra_fixture: &str, path: &str, expect: Expect) {
fn check_found_path_prefer_no_std(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
path: &str,
expect: Expect,
) {
check_found_path_(ra_fixture, path, false, false, true, expect);
}

Expand Down
9 changes: 7 additions & 2 deletions crates/hir-def/src/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,12 @@ mod tests {
}
}

fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect) {
fn check_search(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
crate_name: &str,
query: Query,
expect: Expect,
) {
let db = TestDB::with_files(ra_fixture);
let crate_graph = db.crate_graph();
let krate = crate_graph
Expand Down Expand Up @@ -587,7 +592,7 @@ mod tests {
))
}

fn check(ra_fixture: &str, expect: Expect) {
fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let db = TestDB::with_files(ra_fixture);
let crate_graph = db.crate_graph();

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/item_tree/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use test_fixture::WithFixture;

use crate::{db::DefDatabase, test_db::TestDB};

fn check(ra_fixture: &str, expect: Expect) {
fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let (db, file_id) = TestDB::with_single_file(ra_fixture);
let item_tree = db.file_item_tree(file_id.into());
let pretty = item_tree.pretty_print(&db, Edition::CURRENT);
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use crate::{
};

#[track_caller]
fn check_errors(ra_fixture: &str, expect: Expect) {
fn check_errors(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let db = TestDB::with_files(ra_fixture);
let krate = db.fetch_test_crate();
let def_map = db.crate_def_map(krate);
Expand Down Expand Up @@ -77,7 +77,7 @@ fn check_errors(ra_fixture: &str, expect: Expect) {
}

#[track_caller]
fn check(ra_fixture: &str, mut expect: Expect) {
fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str, mut expect: Expect) {
let extra_proc_macros = vec![(
r#"
#[proc_macro_attribute]
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/nameres/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ use triomphe::Arc;

use crate::{db::DefDatabase, nameres::DefMap, test_db::TestDB};

fn compute_crate_def_map(ra_fixture: &str) -> Arc<DefMap> {
fn compute_crate_def_map(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> Arc<DefMap> {
let db = TestDB::with_files(ra_fixture);
let krate = db.fetch_test_crate();
db.crate_def_map(krate)
}

fn render_crate_def_map(ra_fixture: &str) -> String {
fn render_crate_def_map(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> String {
let db = TestDB::with_files(ra_fixture);
let krate = db.fetch_test_crate();
db.crate_def_map(krate).dump(&db)
}

fn check(ra_fixture: &str, expect: Expect) {
fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let actual = render_crate_def_map(ra_fixture);
expect.assert_eq(&actual);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-expand/src/fixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ mod tests {
}

#[track_caller]
fn check(ra_fixture: &str, mut expect: Expect) {
fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str, mut expect: Expect) {
let parsed = syntax::SourceFile::parse(ra_fixture, span::Edition::CURRENT);
let span_map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(EditionedFileId::new(
FileId::from_raw(0),
Expand Down
14 changes: 10 additions & 4 deletions crates/hir-ty/src/consteval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ fn simplify(e: ConstEvalError) -> ConstEvalError {
}

#[track_caller]
fn check_fail(ra_fixture: &str, error: impl FnOnce(ConstEvalError) -> bool) {
fn check_fail(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
error: impl FnOnce(ConstEvalError) -> bool,
) {
let (db, file_id) = TestDB::with_single_file(ra_fixture);
match eval_goal(&db, file_id) {
Ok(_) => panic!("Expected fail, but it succeeded"),
Expand All @@ -42,7 +45,7 @@ fn check_fail(ra_fixture: &str, error: impl FnOnce(ConstEvalError) -> bool) {
}

#[track_caller]
fn check_number(ra_fixture: &str, answer: i128) {
fn check_number(#[rust_analyzer::rust_fixture] ra_fixture: &str, answer: i128) {
check_answer(ra_fixture, |b, _| {
assert_eq!(
b,
Expand All @@ -54,7 +57,7 @@ fn check_number(ra_fixture: &str, answer: i128) {
}

#[track_caller]
fn check_str(ra_fixture: &str, answer: &str) {
fn check_str(#[rust_analyzer::rust_fixture] ra_fixture: &str, answer: &str) {
check_answer(ra_fixture, |b, mm| {
let addr = usize::from_le_bytes(b[0..b.len() / 2].try_into().unwrap());
let size = usize::from_le_bytes(b[b.len() / 2..].try_into().unwrap());
Expand All @@ -71,7 +74,10 @@ fn check_str(ra_fixture: &str, answer: &str) {
}

#[track_caller]
fn check_answer(ra_fixture: &str, check: impl FnOnce(&[u8], &MemoryMap)) {
fn check_answer(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
check: impl FnOnce(&[u8], &MemoryMap),
) {
let (db, file_ids) = TestDB::with_many_files(ra_fixture);
let file_id = *file_ids.last().unwrap();
let r = match eval_goal(&db, file_id) {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/dyn_compatibility/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum DynCompatibilityViolationKind {
}

fn check_dyn_compatibility<'a>(
ra_fixture: &str,
#[rust_analyzer::rust_fixture] ra_fixture: &str,
expected: impl IntoIterator<Item = (&'a str, Vec<DynCompatibilityViolationKind>)>,
) {
let mut expected: FxHashMap<_, _> =
Expand Down
26 changes: 21 additions & 5 deletions crates/hir-ty/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ fn current_machine_data_layout() -> String {
.unwrap()
}

fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutError> {
fn eval_goal(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
minicore: &str,
) -> Result<Arc<Layout>, LayoutError> {
let target_data_layout = current_machine_data_layout();
let ra_fixture = format!(
"//- target_data_layout: {target_data_layout}\n{minicore}//- /main.rs crate:test\n{ra_fixture}",
Expand Down Expand Up @@ -81,7 +84,10 @@ fn eval_goal(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutErro
}

/// A version of `eval_goal` for types that can not be expressed in ADTs, like closures and `impl Trait`
fn eval_expr(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutError> {
fn eval_expr(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
minicore: &str,
) -> Result<Arc<Layout>, LayoutError> {
let target_data_layout = current_machine_data_layout();
let ra_fixture = format!(
"//- target_data_layout: {target_data_layout}\n{minicore}//- /main.rs crate:test\nfn main(){{let goal = {{{ra_fixture}}};}}",
Expand Down Expand Up @@ -114,21 +120,31 @@ fn eval_expr(ra_fixture: &str, minicore: &str) -> Result<Arc<Layout>, LayoutErro
}

#[track_caller]
fn check_size_and_align(ra_fixture: &str, minicore: &str, size: u64, align: u64) {
fn check_size_and_align(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
minicore: &str,
size: u64,
align: u64,
) {
let l = eval_goal(ra_fixture, minicore).unwrap();
assert_eq!(l.size.bytes(), size, "size mismatch");
assert_eq!(l.align.abi.bytes(), align, "align mismatch");
}

#[track_caller]
fn check_size_and_align_expr(ra_fixture: &str, minicore: &str, size: u64, align: u64) {
fn check_size_and_align_expr(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
minicore: &str,
size: u64,
align: u64,
) {
let l = eval_expr(ra_fixture, minicore).unwrap();
assert_eq!(l.size.bytes(), size, "size mismatch");
assert_eq!(l.align.abi.bytes(), align, "align mismatch");
}

#[track_caller]
fn check_fail(ra_fixture: &str, e: LayoutError) {
fn check_fail(#[rust_analyzer::rust_fixture] ra_fixture: &str, e: LayoutError) {
let r = eval_goal(ra_fixture, "");
assert_eq!(r, Err(e));
}
Expand Down
10 changes: 7 additions & 3 deletions crates/hir-ty/src/mir/eval/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ fn eval_main(db: &TestDB, file_id: EditionedFileId) -> Result<(String, String),
Ok((output.stdout().into_owned(), output.stderr().into_owned()))
}

fn check_pass(ra_fixture: &str) {
fn check_pass(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
check_pass_and_stdio(ra_fixture, "", "");
}

fn check_pass_and_stdio(ra_fixture: &str, expected_stdout: &str, expected_stderr: &str) {
fn check_pass_and_stdio(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
expected_stdout: &str,
expected_stderr: &str,
) {
let (db, file_ids) = TestDB::with_many_files(ra_fixture);
let file_id = *file_ids.last().unwrap();
let x = eval_main(&db, file_id);
Expand Down Expand Up @@ -73,7 +77,7 @@ fn check_pass_and_stdio(ra_fixture: &str, expected_stdout: &str, expected_stderr
}
}

fn check_panic(ra_fixture: &str, expected_panic: &str) {
fn check_panic(#[rust_analyzer::rust_fixture] ra_fixture: &str, expected_panic: &str) {
let (db, file_ids) = TestDB::with_many_files(ra_fixture);
let file_id = *file_ids.last().unwrap();
let e = eval_main(&db, file_id).unwrap_err();
Expand Down
21 changes: 13 additions & 8 deletions crates/hir-ty/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,32 @@ fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
}

#[track_caller]
fn check_types(ra_fixture: &str) {
fn check_types(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
check_impl(ra_fixture, false, true, false)
}

#[track_caller]
fn check_types_source_code(ra_fixture: &str) {
fn check_types_source_code(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
check_impl(ra_fixture, false, true, true)
}

#[track_caller]
fn check_no_mismatches(ra_fixture: &str) {
fn check_no_mismatches(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
check_impl(ra_fixture, true, false, false)
}

#[track_caller]
fn check(ra_fixture: &str) {
fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str) {
check_impl(ra_fixture, false, false, false)
}

#[track_caller]
fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_source: bool) {
fn check_impl(
#[rust_analyzer::rust_fixture] ra_fixture: &str,
allow_none: bool,
only_types: bool,
display_source: bool,
) {
let _tracing = setup_tracing();
let (db, files) = TestDB::with_many_files(ra_fixture);

Expand Down Expand Up @@ -282,7 +287,7 @@ fn pat_node(
})
}

fn infer(ra_fixture: &str) -> String {
fn infer(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> String {
infer_with_mismatches(ra_fixture, false)
}

Expand Down Expand Up @@ -520,13 +525,13 @@ fn ellipsize(mut text: String, max_len: usize) -> String {
text
}

fn check_infer(ra_fixture: &str, expect: Expect) {
fn check_infer(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let mut actual = infer(ra_fixture);
actual.push('\n');
expect.assert_eq(&actual);
}

fn check_infer_with_mismatches(ra_fixture: &str, expect: Expect) {
fn check_infer_with_mismatches(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let mut actual = infer_with_mismatches(ra_fixture, true);
actual.push('\n');
expect.assert_eq(&actual);
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/tests/closure_captures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::test_db::TestDB;

use super::visit_module;

fn check_closure_captures(ra_fixture: &str, expect: Expect) {
fn check_closure_captures(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
let (db, file_id) = TestDB::with_single_file(ra_fixture);
let module = db.module_for_file(file_id);
let def_map = module.def_map(&db);
Expand Down
Loading

0 comments on commit e8ad0c0

Please sign in to comment.