Skip to content

Commit

Permalink
Fix CI
Browse files Browse the repository at this point in the history
* Run `cargo shear --fix`
* Use transformed source code
* Fix fuzzer to use the new parser API
  • Loading branch information
dhruvmanila committed May 30, 2024
1 parent 400d074 commit 9ef4fb5
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 41 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/ruff_benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ codspeed-criterion-compat = { workspace = true, default-features = false, option
ruff_linter = { workspace = true }
ruff_python_ast = { workspace = true }
ruff_python_formatter = { workspace = true }
ruff_python_index = { workspace = true }
ruff_python_parser = { workspace = true }

[lints]
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ pub fn lint_fix<'a>(
loop {
// Parse once.
let program =
ruff_python_parser::parse_unchecked_source(source_kind.source_code(), source_type);
ruff_python_parser::parse_unchecked_source(transformed.source_code(), source_type);

// Map row and column locations to byte slices (lazily).
let locator = Locator::new(transformed.source_code());
Expand Down
1 change: 0 additions & 1 deletion crates/ruff_python_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ ruff_macros = { workspace = true }
ruff_python_trivia = { workspace = true }
ruff_source_file = { workspace = true }
ruff_python_ast = { workspace = true }
ruff_python_index = { workspace = true }
ruff_python_parser = { workspace = true }
ruff_text_size = { workspace = true }

Expand Down
1 change: 0 additions & 1 deletion crates/ruff_python_trivia_integration_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ license.workspace = true
[dependencies]

[dev-dependencies]
ruff_python_index = { workspace = true }
ruff_python_parser = { workspace = true }
ruff_python_trivia = { workspace = true }
ruff_source_file = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion crates/ruff_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ ruff_python_index = { workspace = true }
ruff_python_parser = { workspace = true }
ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true }
ruff_python_trivia = { workspace = true }
ruff_workspace = { workspace = true }

console_error_panic_hook = { workspace = true, optional = true }
Expand Down
1 change: 1 addition & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ruff_python_index = { path = "../crates/ruff_python_index" }
ruff_python_parser = { path = "../crates/ruff_python_parser" }
ruff_source_file = { path = "../crates/ruff_source_file" }
ruff_python_formatter = { path = "../crates/ruff_python_formatter"}
ruff_text_size = { path = "../crates/ruff_text_size" }

arbitrary = { version = "1.3.0", features = ["derive"] }
libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer", default-features = false }
Expand Down
51 changes: 19 additions & 32 deletions fuzz/fuzz_targets/ruff_parse_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

use libfuzzer_sys::{fuzz_target, Corpus};
use ruff_python_codegen::{Generator, Stylist};
use ruff_python_parser::{lexer, parse_suite, Mode, ParseError};
use ruff_python_parser::{parse_module, ParseError};
use ruff_source_file::Locator;
use ruff_text_size::Ranged;

fn do_fuzz(case: &[u8]) -> Corpus {
let Ok(code) = std::str::from_utf8(case) else {
Expand All @@ -15,8 +16,8 @@ fn do_fuzz(case: &[u8]) -> Corpus {

// just round-trip it once to trigger both parse and unparse
let locator = Locator::new(code);
let python_ast = match parse_suite(code) {
Ok(stmts) => stmts,
let program = match parse_module(code) {
Ok(program) => program,
Err(ParseError { location, .. }) => {
let offset = location.start().to_usize();
assert!(
Expand All @@ -28,38 +29,24 @@ fn do_fuzz(case: &[u8]) -> Corpus {
}
};

let tokens: Vec<_> = lexer::lex(code, Mode::Module).collect();

for maybe_token in tokens.iter() {
match maybe_token.as_ref() {
Ok((_, range)) => {
let start = range.start().to_usize();
let end = range.end().to_usize();
assert!(
code.is_char_boundary(start),
"Invalid start position {} (not at char boundary)",
start
);
assert!(
code.is_char_boundary(end),
"Invalid end position {} (not at char boundary)",
end
);
}
Err(err) => {
let offset = err.location().start().to_usize();
assert!(
code.is_char_boundary(offset),
"Invalid error location {} (not at char boundary)",
offset
);
}
}
for token in program.tokens() {
let start = token.start().to_usize();
let end = token.end().to_usize();
assert!(
code.is_char_boundary(start),
"Invalid start position {} (not at char boundary)",
start
);
assert!(
code.is_char_boundary(end),
"Invalid end position {} (not at char boundary)",
end
);
}

let stylist = Stylist::from_tokens(&tokens, &locator);
let stylist = Stylist::from_tokens(program.tokens(), &locator);
let mut generator: Generator = (&stylist).into();
generator.unparse_suite(&python_ast);
generator.unparse_suite(program.suite());

Corpus::Keep
}
Expand Down

0 comments on commit 9ef4fb5

Please sign in to comment.