Skip to content

Commit

Permalink
Merge pull request #427 from ratmice/clippy_fallout
Browse files Browse the repository at this point in the history
Clippy fallout & overzealous #[deny(unsafe_code)]
  • Loading branch information
ltratt authored Dec 12, 2023
2 parents 4ed68d6 + 092b52a commit 83cb818
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 19 deletions.
15 changes: 5 additions & 10 deletions cfgrammar/src/lib/yacc/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,7 @@ where
// needed). If the first column spills, then we're done. This is basically normal
// arithmetic but with each digit having an arbitrary base.

let mut todo = Vec::new();
todo.resize(prod.len(), 0);
let mut todo = vec![0; prod.len()];
let mut cur = Vec::new();
'b: loop {
for i in 0..todo.len() {
Expand Down Expand Up @@ -893,10 +892,8 @@ where
// means that we can iteratively improve our knowledge of a token's minimum cost:
// eventually we will reach a point where we can determine it definitively.

let mut costs = vec![];
costs.resize(usize::from(grm.rules_len()), 0);
let mut done = vec![];
done.resize(usize::from(grm.rules_len()), false);
let mut costs = vec![0; usize::from(grm.rules_len())];
let mut done = vec![false; usize::from(grm.rules_len())];
loop {
let mut all_done = true;
for i in 0..done.len() {
Expand Down Expand Up @@ -960,10 +957,8 @@ fn rule_max_costs<StorageT: 'static + PrimInt + Unsigned>(
where
usize: AsPrimitive<StorageT>,
{
let mut done = vec![];
done.resize(usize::from(grm.rules_len()), false);
let mut costs = vec![];
costs.resize(usize::from(grm.rules_len()), 0);
let mut done = vec![false; usize::from(grm.rules_len())];
let mut costs = vec![0; usize::from(grm.rules_len())];

// First mark all recursive rules.
for ridx in grm.iter_rules() {
Expand Down
11 changes: 8 additions & 3 deletions lrpar/cttests/src/calc_unsafeaction.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ grammar: |
%actiontype Result<u64, ()>
%avoid_insert 'INT'
%%
Expr: Expr '+' Term { unsafe { Ok(Ok::<u64, ()>($1? + $3?).unwrap_unchecked()) } }
Expr: Expr '+' Term { unsafe { unsafe_ok($1? + $3?) } }
| Term { $1 }
;

Term: Term '*' Factor { unsafe { Ok(Ok::<u64, ()>($1? * $3?).unwrap_unchecked()) } }
Term: Term '*' Factor { unsafe { unsafe_ok($1? * $3?) } }
| Factor { $1 }
;

Factor: '(' Expr ')' { $2 }
| 'INT' {
let l = $1.map_err(|_| ())?;
match $lexer.span_str(l.span()).parse::<u64>() {
Ok(v) => unsafe { Ok(Ok::<u64, ()>(v).unwrap_unchecked()) },
Ok(v) => unsafe { unsafe_ok(v) },
Err(_) => {
let ((_, col), _) = $lexer.line_col(l.span());
eprintln!("Error at column {}: '{}' cannot be represented as a u64",
Expand All @@ -28,6 +28,11 @@ grammar: |
}
}
;
%%
// Just check that unsafe blocks work in actions.
unsafe fn unsafe_ok<T, E>(x:T) -> Result<T, E> {
Ok(x)
}

lexer: |
%%
Expand Down
2 changes: 1 addition & 1 deletion lrpar/cttests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn test_parseparam() {
let lexerdef = parseparam_l::lexerdef();
let lexer = lexerdef.lexer("101");
match parseparam_y::parse(&lexer, &3) {
(Some(i), _) if i == 104 => (),
(Some(104), _) => (),
_ => unreachable!(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion lrpar/cttests_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn generate_codegen_fail_tests(item: TokenStream) -> TokenStream {
let manifest_dir = std::path::Path::new(&manifest_dir)
.strip_prefix(cwd)
.unwrap();
let test_glob_path = manifest_dir.join(&test_glob_str.value());
let test_glob_path = manifest_dir.join(test_glob_str.value());
let test_glob_str = test_glob_path.into_os_string().into_string().unwrap();
let test_files = glob(&test_glob_str).unwrap();
for file in test_files {
Expand Down
16 changes: 14 additions & 2 deletions lrpar/src/lib/ctbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,9 +1100,15 @@ where
fn gen_user_actions(&self, grm: &YaccGrammar<StorageT>) -> String {
let mut outs = String::new();

outs.push_str("\nmod _user_section_ {\n");
outs.push_str(
"\t// Allow an actions and the programs section to embed unsafe blocks within it.\n",
);
outs.push_str("\t#![allow(unsafe_code)]\n\n");
if let Some(s) = grm.programs() {
outs.push_str("\n// User code from the program section\n\n");
outs.push_str("\n// User code from the program section\n");
outs.push_str(s);
outs.push_str("\n// End of user code from the program section\n\n");
}

// Convert actions to functions
Expand Down Expand Up @@ -1145,7 +1151,7 @@ where
" // {rulename}
#[allow(clippy::too_many_arguments)]
#[allow(unsafe_code)] // Allow an action to embed unsafe blocks within it.
fn {prefix}action_{}<'lexer, 'input: 'lexer>({prefix}ridx: ::cfgrammar::RIdx<{storaget}>,
pub(super) fn {prefix}action_{}<'lexer, 'input: 'lexer>({prefix}ridx: ::cfgrammar::RIdx<{storaget}>,
{prefix}lexer: &'lexer dyn ::lrpar::NonStreamingLexer<'input, {lexertypest}>,
{prefix}span: ::cfgrammar::Span,
{parse_paramdef},
Expand Down Expand Up @@ -1207,6 +1213,12 @@ where

outs.push_str("\n }\n\n");
}
// Put the `use` statements at the bottom, allowing
// the program section to set inner attributes at the top.
outs.push_str("\t#[allow(unused_imports)]\n");
outs.push_str("\tuse super::*;\n");
outs.push_str("\n} // End of mod _user_section\n");
outs.push_str("use _user_section_::*;\n");
outs
}

Expand Down
4 changes: 2 additions & 2 deletions lrtable/src/lib/statetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -988,8 +988,8 @@ D : D;
Ok(_) => panic!("Infinitely recursive rule let through"),
Err(StateTableError {
kind: StateTableErrorKind::AcceptReduceConflict(_),
pidx,
}) if pidx == PIdx(1) => (),
pidx: PIdx(1),
}) => (),
Err(e) => panic!("Incorrect error returned {:?}", e),
}
}
Expand Down

0 comments on commit 83cb818

Please sign in to comment.