Skip to content

Commit

Permalink
Safe codegen (#12)
Browse files Browse the repository at this point in the history
* Run the flaky compile test only on stable

* Use a conditional match statement in codegen

This removes `unsafe` from codegen, which is unnecessary and rather obscure.

* Use String::from() consistently
  • Loading branch information
parasyte authored May 4, 2023
1 parent 937a14f commit d50231e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion compile_tests/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn compile_tests() {
t.compile_fail("compile_tests/multiple_non_signed.rs");
t.compile_fail("compile_tests/multiple_one_non_signed.rs");
t.pass("compile_tests/no_display.rs");
if rustversion::cfg!(since(1.68.0)) {
if rustversion::cfg!(all(stable, since(1.68.0))) {
t.compile_fail("compile_tests/no_display_no_impl.rs");
}
}
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub fn derive_error(input: TokenStream) -> TokenStream {
if v.display_fields.contains(&Rc::from(format!("field_{i}"))) {
format!("field_{i},")
} else {
"_,".to_string()
String::from("_,")
}
})
.collect::<String>();
Expand All @@ -175,18 +175,18 @@ pub fn derive_error(input: TokenStream) -> TokenStream {
Ok(msg) => display_matches.push_str(&msg),
}
}
display_matches.push_str(&format!(
"_ => unsafe {{ ::{std_crate}::hint::unreachable_unchecked()}}"
));
let display_matches = if display_matches.is_empty() {
String::from("Ok(())")
} else {
format!("match self {{ {display_matches} }}")
};

format!(
r#"impl ::{std_crate}::fmt::Display for {name} {{
fn fmt(&self, f: &mut ::{std_crate}::fmt::Formatter<'_>) ->
::{std_crate}::result::Result<(), ::{std_crate}::fmt::Error>
{{
match self {{
{display_matches}
}}
{display_matches}
}}
}}"#
)
Expand Down

0 comments on commit d50231e

Please sign in to comment.