Skip to content

Commit c6cbd1d

Browse files
committed
Evolve: Quiet warnings
Signed-off-by: Mark Van de Vyver <[email protected]>
1 parent 873c080 commit c6cbd1d

File tree

6 files changed

+110
-59
lines changed

6 files changed

+110
-59
lines changed

minitrace-macro/src/darling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate syn;
33

44
use {
55
darling::*,
6-
std::path::*,
6+
// std::path::*,
77
};
88

99
#[derive(FromMeta)]

minitrace-macro/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,27 @@ use syn::{punctuated::Punctuated, visit_mut::VisitMut, Ident, *};
3434
use crate::trace::validate::TraceAttr;
3535

3636
mod trace;
37-
mod darling;
37+
//mod darling;
3838

3939
#[proc_macro_attribute]
4040
#[proc_macro_error]
41-
pub fn trace2(
41+
pub fn trace(
4242
args: proc_macro::TokenStream,
4343
items: proc_macro::TokenStream,
4444
) -> proc_macro::TokenStream {
4545
// Parse TokenStream in context of `#[proc_macro_attribute]`.
4646
// This context is required by `parse_macro_input!(...)`.
4747
// NOTE: See comment above Trace
48+
println!("{:#?}", args);
49+
println!("{:#?}", items);
4850
let argsc = args.clone();
4951
let attr_args = parse_macro_input!(argsc as TraceAttr);
5052
let args2: proc_macro2::TokenStream = args.clone().into();
5153

5254
// Validate - Analyze - Lower - Generate - Rust (VALGR)
53-
55+
println!("{:#?}", args2);
5456
trace::validate(args2, items.clone().into());
57+
println!("{:#?}", attr_args);
5558
let models = trace::analyze(attr_args, items.into());
5659
let ir = trace::lower(models);
5760
let rust = trace::generate(ir);
@@ -101,7 +104,7 @@ impl Args {
101104

102105
#[proc_macro_attribute]
103106
#[proc_macro_error]
104-
pub fn trace(
107+
pub fn trace2(
105108
args: proc_macro::TokenStream,
106109
item: proc_macro::TokenStream,
107110
) -> proc_macro::TokenStream {
@@ -604,7 +607,7 @@ fn get_async_trait_info(block: &Block, block_is_async: bool) -> Option<AsyncTrai
604607
}
605608

606609
// Return a path as a String
607-
fn path_to_string(path: &Path) -> String {
610+
fn path_to_string(path: &syn::Path) -> String {
608611
use std::fmt::Write;
609612
// some heuristic to prevent too many allocations
610613
let mut res = String::with_capacity(path.segments.len() * 5);
@@ -662,7 +665,7 @@ struct AsyncTraitBlockReplacer<'a> {
662665

663666
impl<'a> syn::visit_mut::VisitMut for AsyncTraitBlockReplacer<'a> {
664667
fn visit_block_mut(&mut self, i: &mut Block) {
665-
if i == self.block {
668+
if i == self.block {
666669
*i = self.patched_block.clone();
667670
}
668671
}

minitrace-macro/src/trace/analyze.rs

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ use proc_macro_error::{abort, abort_call_site};
2929
use syn::{
3030
parenthesized,
3131
parse::{Parse, ParseStream},
32-
spanned::Spanned,
33-
Expr, ItemFn,
32+
Expr,
3433
};
3534

36-
use crate::trace::validate::Ast;
35+
//use crate::trace::validate::Ast;
3736

3837
// - `<Macro>.name: syn::LitStr,` // String`
3938
// - `<Macro>.enter_on_poll: syn::LitBool,` // boolean
@@ -112,8 +111,8 @@ enum Scope {
112111
)]
113112
pub struct Trace {
114113
// Anything that implements `syn::parse::Parse` is supported.
115-
name: syn::LitStr,
116-
scope: Scope, // Scope::Local, Scope::Thread, etc.
114+
name: Option<syn::LitStr>,
115+
scope: Option<Scope>, // Scope::Local, Scope::Thread, etc.
117116

118117
// Fields wrapped in `Option` are and default to `None` if
119118
// not specified in the attribute.
@@ -154,17 +153,27 @@ impl Parse for Scope {
154153
//
155154
// The `Models` container is built based on the attribute parameters
156155
// held in the `Trace` type.
156+
//
157+
// The inputs are:
158+
// - `meta`: A `syn::Attribute` encapsulated in `TraceAttr`.
159+
// - `items`: A `proc_macr2::TokenStream`.
157160
use syn::visit::Visit;
158161
pub fn analyze(
159162
meta: crate::trace::validate::TraceAttr,
160163
items: proc_macro2::TokenStream,
161164
) -> Models<Model> {
162165
let mut models = Models::<Model>::new();
166+
//println!("Meta {:#?}", meta);
167+
// `from_attributes(..)` takes a slice reference.
163168
// `meta.attrs` is a `Vec<>`, which implements `AsRef`, so is coerced to `&[]`
164169
let attrs: &[syn::Attribute] = &meta.attrs;
170+
// let attribute = Trace::from_attributes(attrs).unwrap();
171+
//println!("Attrs {:#?}", attrs);
165172
let attribute = match Trace::from_attributes(attrs) {
166-
Ok(attrs) => attrs,
167-
Err(err) => return err.into_compile_error().into(),
173+
Ok(trace) => trace,
174+
Err(err) => {
175+
return err.into_compile_error().into();
176+
}
168177
};
169178

170179
models.push(Model::Attribute(attribute));
@@ -180,21 +189,10 @@ pub fn analyze(
180189
models.push(Model::Item(syn::Item::Fn((*f).clone())));
181190
}
182191

183-
// Move this check for duplicate `#[trace]` attributes to validate
184-
//
185-
// for index in (0..attrs.len()).rev() {
186-
// if let Some(ident) = attrs[index].path.get_ident() {
187-
// if ident.to_string().as_str() == "precondition" {
188-
// let attr = attrs.remove(index);
189-
// let _span = attr.tokens.span();
190-
// }
191-
// }
192-
// }
193-
194192
models
195193
}
196194

197-
// `Models` a Vec-newtype
195+
// `Models` are a Vec-newtype
198196
//
199197
// A wrapper that allows us to implement the [`From`] trait.
200198
// The [`From`] trait provides these conveniences (`match` branch):
@@ -216,6 +214,7 @@ impl<T: std::fmt::Debug> Models<T> {
216214
Models(Vec::<T>::new())
217215
}
218216

217+
#[allow(dead_code)]
219218
pub fn with_capacity(capacity: usize) -> Models<T> {
220219
Models(Vec::<T>::with_capacity(capacity))
221220
}
@@ -248,7 +247,7 @@ impl<T: std::fmt::Debug> std::ops::DerefMut for Models<T> {
248247

249248
#[derive(Debug)]
250249
struct Item {
251-
item: syn::Item,
250+
//item: syn::Item,
252251
}
253252

254253
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
@@ -290,7 +289,7 @@ impl<'ast> syn::visit::Visit<'ast> for FnVisitor<'ast> {
290289
// Err(err) => return err.into_compile_error().into(),
291290
//
292291
impl std::convert::From<proc_macro2::TokenStream> for Model {
293-
fn from(inner: proc_macro2::TokenStream) -> Model {
292+
fn from(_inner: proc_macro2::TokenStream) -> Model {
294293
let attribute = Default::default();
295294
Model::Attribute(attribute)
296295
}
@@ -302,7 +301,7 @@ impl std::convert::From<proc_macro2::TokenStream> for Model {
302301
//
303302
//
304303
impl std::convert::From<proc_macro2::TokenStream> for Models<Model> {
305-
fn from(inner: proc_macro2::TokenStream) -> Models<Model> {
304+
fn from(_inner: proc_macro2::TokenStream) -> Models<Model> {
306305
let attribute = Default::default();
307306
let mut models = Models::<Model>::new();
308307
models.push(Model::Attribute(attribute));
@@ -314,15 +313,15 @@ impl Default for Trace {
314313
fn default() -> Self {
315314
// let scope = proc_macro2::Ident::new("Local", proc_macro2::Span::call_site());
316315
// Some(syn::LitBool::new(false, proc_macro2::Span::call_site()));
317-
let name = syn::LitStr::new("root", proc_macro2::Span::call_site());
318-
let scope = Scope::Local;
316+
let name = Some(syn::LitStr::new("root", proc_macro2::Span::call_site()));
317+
let scope = Some(Scope::Local);
319318
let enter_on_poll = Some(syn::LitBool::new(true, proc_macro2::Span::call_site()));
320319
let recorder = Some(proc_macro2::Ident::new(
321320
"span",
322321
proc_macro2::Span::call_site(),
323322
));
324323
let recurse = None;
325-
let root= Some(syn::LitBool::new(false, proc_macro2::Span::call_site()));
324+
let root = Some(syn::LitBool::new(false, proc_macro2::Span::call_site()));
326325
let variables = None;
327326
let parent = Some(syn::LitStr::new("root", proc_macro2::Span::call_site()));
328327
let async_trait = None;
@@ -412,8 +411,9 @@ mod tests {
412411
#[test]
413412
fn with_traces() {
414413
let models = analyze(
415-
parse_quote!(#[trace]),
414+
parse_quote!(),
416415
parse_quote!(
416+
#[trace]
417417
fn f(x: bool) {}
418418
),
419419
);
@@ -454,30 +454,37 @@ mod tests {
454454
}
455455

456456
#[test]
457-
#[should_panic]
458-
fn error_without_trace() {
457+
fn with_no_trace() {
459458
analyze(
460459
parse_quote!(),
461460
parse_quote!(
462-
#[without_trace]
463-
fn f(x: bool) {}
461+
fn f(x: bool) -> bool {
462+
x
463+
}
464464
),
465465
);
466466
}
467467

468+
// There is no filtering/validation in the `analyze` function.
469+
// All such checks are done in `validate` function.
468470
#[test]
469-
fn preserve_non_dsl_attributes() {
471+
fn others_with_traces() {
470472
let models = analyze(
471-
parse_quote!(#[trace]),
473+
parse_quote!(#[trace()]),
472474
parse_quote!(
473475
#[a]
474476
#[trace]
475477
#[b]
476-
fn f(x: bool) {}
478+
fn f(x: bool) -> bool {
479+
x
480+
}
477481
),
478482
);
479-
480-
let expected: &[Attribute] = &[parse_quote!(#[a]), parse_quote!(#[b])];
483+
let expected: &[Attribute] = &[
484+
parse_quote!(#[a]),
485+
parse_quote!(#[trace]),
486+
parse_quote!(#[b]),
487+
];
481488
let model = models.get(1).unwrap();
482489
let item = if let Model::Item(item) = model {
483490
item
@@ -491,4 +498,17 @@ mod tests {
491498
};
492499
assert_eq!(expected, actual.attrs);
493500
}
501+
502+
#[test]
503+
fn others_with_no_trace() {
504+
let models = analyze(
505+
parse_quote!(),
506+
parse_quote!(
507+
#[a]
508+
#[b]
509+
fn f(x: bool) {}
510+
),
511+
);
512+
assert_eq!(models.len(),1);
513+
}
494514
}

minitrace-macro/src/trace/generate.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use syn::{parse_quote, Stmt};
44

55
use crate::trace::lower::Assertion;
66

7-
pub type Rust = proc_macro2::TokenStream;
7+
//pub type Rust = proc_macro2::TokenStream;
88

99
use crate::trace::lower::Quotable;
1010
use crate::trace::lower::Quotables;
@@ -17,6 +17,7 @@ pub fn generate(quotes: Quotables<Quotable>) -> proc_macro2::TokenStream {
1717
// Have a logic check earlier to error if there is not **at least one**
1818
// `syn::Item`
1919
#[allow(clippy::collapsible_match)]
20+
#[allow(unreachable_patterns)]
2021
let it = match quotes.get(0).expect("An item") {
2122
Quotable::Item(item) => match item {
2223
syn::Item::Fn(itemf) => Some(itemf),
@@ -59,11 +60,11 @@ mod tests {
5960
fn f(x: bool) {}
6061
),
6162
);
62-
println!("{:?}",models.clone());
63+
println!("{:?}", models.clone());
6364
let ir = crate::trace::lower(models);
64-
println!("{:?}",ir.clone());
65+
println!("{:?}", ir.clone());
6566
let rust = crate::trace::generate(ir);
66-
println!("{:?}",rust.clone());
67+
println!("{:?}", rust.clone());
6768

6869
assert!(syn::parse2::<syn::ItemFn>(rust).is_ok());
6970
}

minitrace-macro/src/trace/lower.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#[allow(unused_imports)]
22
use quote::quote;
3-
use syn::{Expr, ItemFn};
3+
use syn::{Expr};
44

55
use crate::trace::analyze::Model;
66
use crate::trace::analyze::Models;
@@ -15,7 +15,7 @@ pub fn lower(models: Models<Model>) -> Quotables<Quotable>
1515
//where Model: std::fmt::Display + std::fmt::Debug + From<Model>,
1616
{
1717
let quotes = Quotables::new();
18-
if let Some(Model::Attribute(attribute)) = models.get(0) {};
18+
if let Some(Model::Attribute(_attribute)) = models.get(0) {};
1919

2020
// let assertions = preconditions
2121
// .into_iter()
@@ -50,6 +50,7 @@ impl<T: std::fmt::Debug> Quotables<T> {
5050
Quotables(Vec::<T>::new())
5151
}
5252

53+
#[allow(dead_code)]
5354
pub fn with_capacity(capacity: usize) -> Quotables<T> {
5455
Quotables(Vec::<T>::with_capacity(capacity))
5556
}
@@ -84,7 +85,7 @@ impl<T: std::fmt::Debug> std::ops::DerefMut for Quotables<T> {
8485
// struct Quote2 {
8586
// item: syn::Item,
8687
// }
87-
88+
#[allow(dead_code)]
8889
#[derive(Clone, Debug, thiserror::Error)]
8990
#[error("Validation logic error")]
9091
pub enum Quotable {

0 commit comments

Comments
 (0)