Skip to content

Commit 76463ee

Browse files
committed
Move lint definition generation into xtask/codegen
1 parent 0964374 commit 76463ee

File tree

12 files changed

+90
-86
lines changed

12 files changed

+90
-86
lines changed

Cargo.lock

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ide-db/Cargo.toml

-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,10 @@ line-index.workspace = true
4444

4545
[dev-dependencies]
4646
expect-test = "1.4.0"
47-
oorandom = "11.1.3"
48-
xshell.workspace = true
4947

5048
# local deps
5149
test-utils.workspace = true
5250
test-fixture.workspace = true
53-
sourcegen.workspace = true
5451

5552
[lints]
5653
workspace = true

crates/ide-db/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,3 @@ impl SnippetCap {
412412
}
413413
}
414414
}
415-
416-
#[cfg(test)]
417-
mod tests {
418-
mod line_index;
419-
mod sourcegen_lints;
420-
}

crates/ide-db/src/tests/line_index.rs

-49
This file was deleted.

crates/ide-diagnostics/src/tests.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#![allow(clippy::print_stderr)]
2-
#[cfg(not(feature = "in-rust-tree"))]
3-
mod sourcegen;
42

53
use ide_db::{
64
assists::AssistResolveStrategy, base_db::SourceDatabaseExt, LineIndexDatabase, RootDatabase,

lib/line-index/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ edition = "2021"
1010
text-size = "1.1.1"
1111
nohash-hasher = "0.2.0"
1212

13+
[dev-dependencies]
14+
oorandom = "11.1.3"
15+
1316
[lints]
14-
workspace = true
17+
workspace = true

lib/line-index/src/tests.rs

+53
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,56 @@ fn test_to_wide() {
142142
let wide_line_col = line_index.to_wide(WideEncoding::Utf16, line_col.unwrap());
143143
assert_eq!(wide_line_col, Some(WideLineCol { line: 5, col: 4 }));
144144
}
145+
146+
#[test]
147+
fn test_every_chars() {
148+
let text: String = {
149+
let mut chars: Vec<char> = ((0 as char)..char::MAX).collect(); // Neat!
150+
chars.extend("\n".repeat(chars.len() / 16).chars());
151+
let seed = std::hash::Hasher::finish(&std::hash::BuildHasher::build_hasher(
152+
#[allow(clippy::disallowed_types)]
153+
&std::collections::hash_map::RandomState::new(),
154+
));
155+
let mut rng = oorandom::Rand32::new(seed);
156+
let mut rand_index = |i| rng.rand_range(0..i as u32) as usize;
157+
let mut remaining = chars.len() - 1;
158+
while remaining > 0 {
159+
let index = rand_index(remaining);
160+
chars.swap(remaining, index);
161+
remaining -= 1;
162+
}
163+
chars.into_iter().collect()
164+
};
165+
assert!(text.contains('💩')); // Sanity check.
166+
167+
let line_index = LineIndex::new(&text);
168+
169+
let mut lin_col = LineCol { line: 0, col: 0 };
170+
let mut col_utf16 = 0;
171+
let mut col_utf32 = 0;
172+
for (offset, c) in text.char_indices() {
173+
let got_offset = line_index.offset(lin_col).unwrap();
174+
assert_eq!(usize::from(got_offset), offset);
175+
176+
let got_lin_col = line_index.line_col(got_offset);
177+
assert_eq!(got_lin_col, lin_col);
178+
179+
for (enc, col) in [(WideEncoding::Utf16, col_utf16), (WideEncoding::Utf32, col_utf32)] {
180+
let wide_lin_col = line_index.to_wide(enc, lin_col).unwrap();
181+
let got_lin_col = line_index.to_utf8(enc, wide_lin_col).unwrap();
182+
assert_eq!(got_lin_col, lin_col);
183+
assert_eq!(wide_lin_col.col, col)
184+
}
185+
186+
if c == '\n' {
187+
lin_col.line += 1;
188+
lin_col.col = 0;
189+
col_utf16 = 0;
190+
col_utf32 = 0;
191+
} else {
192+
lin_col.col += c.len_utf8() as u32;
193+
col_utf16 += c.len_utf16() as u32;
194+
col_utf32 += 1;
195+
}
196+
}
197+
}

xtask/src/codegen.rs

+6
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ use crate::{flags, project_root};
99

1010
pub(crate) mod assists_doc_tests;
1111
pub(crate) mod diagnostics_docs;
12+
mod lints;
1213

1314
impl flags::Codegen {
1415
pub(crate) fn run(self, _sh: &Shell) -> anyhow::Result<()> {
1516
match self.codegen_type.unwrap_or_default() {
1617
flags::CodegenType::All => {
18+
diagnostics_docs::generate(self.check);
1719
assists_doc_tests::generate(self.check);
20+
// lints::generate(self.check) Updating clones the rust repo, so don't run it unless
21+
// explicitly asked for
1822
}
1923
flags::CodegenType::AssistsDocTests => assists_doc_tests::generate(self.check),
24+
flags::CodegenType::DiagnosticsDocs => diagnostics_docs::generate(self.check),
25+
flags::CodegenType::LintDefinitions => lints::generate(self.check),
2026
}
2127
Ok(())
2228
}

xtask/src/codegen/diagnostics_docs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
project_root,
88
};
99

10-
fn generate(check: bool) {
10+
pub(crate) fn generate(check: bool) {
1111
let diagnostics = Diagnostic::collect().unwrap();
1212
if !check {
1313
let contents =

crates/ide-db/src/tests/sourcegen_lints.rs renamed to xtask/src/codegen/lints.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
//! and lints from rustc, rustdoc, and clippy.
33
use std::{borrow::Cow, fs, path::Path};
44

5-
use itertools::Itertools;
65
use stdx::format_to;
7-
use test_utils::project_root;
86
use xshell::{cmd, Shell};
97

8+
use crate::{
9+
codegen::{add_preamble, ensure_file_contents, list_files, reformat},
10+
project_root,
11+
};
12+
1013
const DESTINATION: &str = "crates/ide-db/src/generated/lints.rs";
1114

12-
/// This clones rustc repo, and so is not worth to keep up-to-date. We update
13-
/// manually by un-ignoring the test from time to time.
14-
#[test]
15-
#[ignore]
16-
fn sourcegen_lint_completions() {
15+
/// This clones rustc repo, and so is not worth to keep up-to-date on a constant basis.
16+
pub(crate) fn generate(check: bool) {
1717
let sh = &Shell::new().unwrap();
1818

1919
let rust_repo = project_root().join("./target/rust");
@@ -73,10 +73,10 @@ pub struct LintGroup {
7373
.unwrap();
7474
generate_descriptor_clippy(&mut contents, &lints_json);
7575

76-
let contents = sourcegen::add_preamble("sourcegen_lints", sourcegen::reformat(contents));
76+
let contents = add_preamble("sourcegen_lints", reformat(contents));
7777

7878
let destination = project_root().join(DESTINATION);
79-
sourcegen::ensure_file_contents(destination.as_path(), &contents);
79+
ensure_file_contents(destination.as_path(), &contents, check);
8080
}
8181

8282
/// Parses the output of `rustdoc -Whelp` and prints `Lint` and `LintGroup` constants into `buf`.
@@ -130,10 +130,9 @@ fn generate_lint_descriptor(sh: &Shell, buf: &mut String) {
130130
)
131131
});
132132

133-
let lints = lints
134-
.chain(lint_groups)
135-
.sorted_by(|(ident, ..), (ident2, ..)| ident.cmp(ident2))
136-
.collect::<Vec<_>>();
133+
let mut lints = lints.chain(lint_groups).collect::<Vec<_>>();
134+
lints.sort_by(|(ident, ..), (ident2, ..)| ident.cmp(ident2));
135+
137136
for (name, description, ..) in &lints {
138137
push_lint_completion(buf, &name.replace('-', "_"), description);
139138
}
@@ -177,10 +176,8 @@ fn generate_lint_descriptor(sh: &Shell, buf: &mut String) {
177176
)
178177
});
179178

180-
let lints_rustdoc = lints_rustdoc
181-
.chain(lint_groups_rustdoc)
182-
.sorted_by(|(ident, ..), (ident2, ..)| ident.cmp(ident2))
183-
.collect::<Vec<_>>();
179+
let mut lints_rustdoc = lints_rustdoc.chain(lint_groups_rustdoc).collect::<Vec<_>>();
180+
lints_rustdoc.sort_by(|(ident, ..), (ident2, ..)| ident.cmp(ident2));
184181

185182
for (name, description, ..) in &lints_rustdoc {
186183
push_lint_completion(buf, &name.replace('-', "_"), description)
@@ -212,7 +209,7 @@ fn find_and_slice<'a>(i: &'a str, p: &str) -> &'a str {
212209
fn generate_feature_descriptor(buf: &mut String, src_dir: &Path) {
213210
let mut features = ["language-features", "library-features"]
214211
.into_iter()
215-
.flat_map(|it| sourcegen::list_files(&src_dir.join(it)))
212+
.flat_map(|it| list_files(&src_dir.join(it)))
216213
// Get all `.md` files
217214
.filter(|path| path.extension() == Some("md".as_ref()))
218215
.map(|path| {
@@ -302,7 +299,7 @@ fn generate_descriptor_clippy(buf: &mut String, path: &Path) {
302299
let children = children.iter().map(|id| format!("clippy::{id}")).collect::<Vec<_>>();
303300
if !children.is_empty() {
304301
let lint_ident = format!("clippy::{id}");
305-
let description = format!("lint group for: {}", children.iter().join(", "));
302+
let description = format!("lint group for: {}", children.join(", "));
306303
push_lint_group(buf, &lint_ident, &description, &children);
307304
}
308305
}
@@ -331,7 +328,10 @@ fn push_lint_group(buf: &mut String, label: &str, description: &str, children: &
331328

332329
push_lint_completion(buf, label, description);
333330

334-
let children = format!("&[{}]", children.iter().map(|it| format!("\"{it}\"")).join(", "));
331+
let children = format!(
332+
"&[{}]",
333+
children.iter().map(|it| format!("\"{it}\"")).collect::<Vec<_>>().join(", ")
334+
);
335335
format_to!(
336336
buf,
337337
r###"

xtask/src/flags.rs

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ pub enum CodegenType {
9292
#[default]
9393
All,
9494
AssistsDocTests,
95+
DiagnosticsDocs,
96+
LintDefinitions,
9597
}
9698

9799
impl FromStr for CodegenType {
@@ -100,6 +102,8 @@ impl FromStr for CodegenType {
100102
match s {
101103
"all" => Ok(Self::All),
102104
"assists-doc-tests" => Ok(Self::AssistsDocTests),
105+
"diagnostics-docs" => Ok(Self::DiagnosticsDocs),
106+
"lints-definitions" => Ok(Self::LintDefinitions),
103107
_ => Err("Invalid option".to_owned()),
104108
}
105109
}

xtask/src/release.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl flags::Release {
2323
}
2424

2525
// Generates bits of manual.adoc.
26-
cmd!(sh, "cargo test -p ide-diagnostics -p rust-analyzer -- sourcegen_").run()?;
26+
codegen::diagnostics_docs::generate(false);
2727
codegen::assists_doc_tests::generate(false);
2828

2929
let website_root = project_root().join("../rust-analyzer.github.io");

0 commit comments

Comments
 (0)