ferroforge-contracts 0.2.0

Shared source declaration contracts for FerroForge macros and host tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! Source contracts shared by the procedural macro and host discovery.
//!
//! This crate parses declarations, not task-body semantics. It has no target
//! HAL dependencies. Parsing the standalone syntax is not a checking expansion.

use std::collections::BTreeSet;

use syn::{
    Error, FnArg, Ident, LitStr, Pat, Path, ReturnType, Signature, Token, Type, TypeParamBound,
    bracketed, parenthesized,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    spanned::Spanned,
};

/// Rust raw and ordinary identifiers name the same declaration.
pub fn identifier_key(name: &Ident) -> String {
    name.to_string().trim_start_matches("r#").to_owned()
}

/// An inline resource or configuration entry. A resource needs either a type or
/// a matching bound; `None` is the bare-name form, which `#[task]` rejects for
/// resources because it cannot infer a field type from a name alone.
#[derive(Clone, Debug)]
pub struct Resource {
    pub name: Ident,
    pub ty: Option<Type>,
}

impl Parse for Resource {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let name = input.parse()?;
        let ty = if input.peek(Token![:]) {
            input.parse::<Token![:]>()?;
            Some(input.parse()?)
        } else {
            None
        };
        Ok(Self { name, ty })
    }
}

#[derive(Clone, Debug)]
pub struct ResourceBound {
    pub name: Ident,
    pub traits: Punctuated<TypeParamBound, Token![+]>,
}

impl Parse for ResourceBound {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let name = input.parse()?;
        input.parse::<Token![:]>()?;
        let traits = Punctuated::parse_separated_nonempty(input)?;
        Ok(Self { name, traits })
    }
}

#[derive(Clone, Debug)]
pub struct Parameter {
    pub name: Ident,
    pub ty: Type,
}

impl Parse for Parameter {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let name = input.parse()?;
        input.parse::<Token![:]>()?;
        Ok(Self {
            name,
            ty: input.parse()?,
        })
    }
}

#[derive(Clone, Debug)]
pub struct Spawn {
    pub name: Ident,
    /// `Some([])` is an explicit zero-input signature, `report()`. `None` is the
    /// bare name `report`, which carries no signature to bound the closure with.
    pub inputs: Option<Vec<Parameter>>,
}

impl Parse for Spawn {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let name = input.parse()?;
        let inputs = if input.peek(syn::token::Paren) {
            let body;
            parenthesized!(body in input);
            Some(
                Punctuated::<Parameter, Token![,]>::parse_terminated(&body)?
                    .into_iter()
                    .collect(),
            )
        } else {
            None
        };
        Ok(Self { name, inputs })
    }
}

/// Registry requirements are retained only for the existing prototype adapter.
#[derive(Clone, Debug)]
pub struct TaskDependency {
    pub id: Ident,
    pub features: Vec<LitStr>,
}

impl Parse for TaskDependency {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let id = input.parse()?;
        let mut features = Vec::new();
        if input.peek(syn::token::Paren) {
            let body;
            parenthesized!(body in input);
            let key: Ident = body.parse()?;
            if key != "features" {
                return Err(Error::new(key.span(), "expected `features`"));
            }
            body.parse::<Token![=]>()?;
            features = list(&body)?;
            if !body.is_empty() {
                return Err(body.error("unexpected dependency requirement argument"));
            }
        }
        Ok(Self { id, features })
    }
}

#[derive(Clone, Debug, Default)]
pub struct TaskArguments {
    pub bounds: Vec<ResourceBound>,
    pub local: Vec<Resource>,
    pub shared: Vec<Resource>,
    pub config: Vec<Resource>,
    pub spawn: Vec<Spawn>,
    /// Source reference, not the system's actual tick rate or hardware clock.
    pub monotonic: Option<Path>,
    pub dependencies: Vec<TaskDependency>,
}

fn list<T: Parse>(input: ParseStream<'_>) -> syn::Result<Vec<T>> {
    let body;
    bracketed!(body in input);
    Ok(Punctuated::<T, Token![,]>::parse_terminated(&body)?
        .into_iter()
        .collect())
}

impl Parse for TaskArguments {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let mut args = Self::default();
        let mut keys = BTreeSet::new();
        while !input.is_empty() {
            let key: Ident = input.parse()?;
            if !keys.insert(key.to_string()) {
                return Err(Error::new(
                    key.span(),
                    format!("duplicate task argument `{key}`"),
                ));
            }
            input.parse::<Token![=]>()?;
            match key.to_string().as_str() {
                "bounds" => args.bounds = list(input)?,
                "local" => args.local = list(input)?,
                "shared" => args.shared = list(input)?,
                "config" => args.config = list(input)?,
                "spawn" => args.spawn = list(input)?,
                "monotonic" => args.monotonic = Some(input.parse()?),
                "dependencies" => args.dependencies = list(input)?,
                _ => {
                    return Err(Error::new(
                        key.span(),
                        "unknown task argument; scheduling belongs in composition",
                    ));
                }
            }
            if !input.is_empty() {
                input.parse::<Token![,]>()?;
            }
        }
        Ok(args)
    }
}

fn unique<'a>(names: impl IntoIterator<Item = &'a Ident>, message: &str) -> syn::Result<()> {
    let mut seen = BTreeSet::new();
    for name in names {
        if !seen.insert(identifier_key(name)) {
            return Err(Error::new(name.span(), message));
        }
    }
    Ok(())
}

impl TaskArguments {
    /// Structural validation of the declaration itself, before any expansion
    /// reads it: duplicate names, empty categories, missing types.
    pub fn validate(&self) -> syn::Result<()> {
        unique(
            self.local.iter().map(|r| &r.name),
            "duplicate local resource",
        )?;
        unique(
            self.shared.iter().map(|r| &r.name),
            "duplicate shared resource",
        )?;
        unique(
            self.config.iter().map(|r| &r.name),
            "duplicate task configuration",
        )?;
        unique(
            self.bounds.iter().map(|r| &r.name),
            "duplicate resource bound",
        )?;
        unique(self.spawn.iter().map(|s| &s.name), "duplicate spawn alias")?;
        unique(
            self.dependencies.iter().map(|d| &d.id),
            "duplicate task dependency",
        )?;
        for local in &self.local {
            if self
                .shared
                .iter()
                .any(|r| identifier_key(&r.name) == identifier_key(&local.name))
            {
                return Err(Error::new(
                    local.name.span(),
                    "a task resource cannot be both local and shared",
                ));
            }
        }
        for dependency in &self.dependencies {
            let mut seen = BTreeSet::new();
            for feature in &dependency.features {
                if !seen.insert(feature.value()) {
                    return Err(Error::new(
                        feature.span(),
                        "duplicate task dependency feature",
                    ));
                }
            }
        }
        for spawn in &self.spawn {
            if let Some(inputs) = &spawn.inputs {
                unique(inputs.iter().map(|p| &p.name), "duplicate spawn parameter")?;
            }
        }
        Ok(())
    }

    /// Check the independently authored SW contract. Rust still checks types.
    pub fn validate_standalone(&self) -> syn::Result<()> {
        self.validate()?;
        if let Some(dependency) = self.dependencies.first() {
            return Err(Error::new(
                dependency.id.span(),
                "standalone task dependencies belong in Cargo.toml",
            ));
        }
        for bound in &self.bounds {
            if !self
                .local
                .iter()
                .chain(&self.shared)
                .any(|r| identifier_key(&r.name) == identifier_key(&bound.name))
            {
                return Err(Error::new(
                    bound.name.span(),
                    "bounded resource must be claimed as local or shared",
                ));
            }
            if bound
                .traits
                .iter()
                .any(|b| !matches!(b, TypeParamBound::Trait(_)))
            {
                return Err(Error::new(
                    bound.name.span(),
                    "resource bounds must be trait bounds",
                ));
            }
        }
        for resource in self.local.iter().chain(&self.shared) {
            let bounded = self
                .bounds
                .iter()
                .any(|b| identifier_key(&b.name) == identifier_key(&resource.name));
            if bounded == resource.ty.is_some() {
                return Err(Error::new(
                    resource.name.span(),
                    "resource needs either an inline type or a resource-keyed bound, not both",
                ));
            }
            if let Some(ty) = &resource.ty {
                explicit_type(ty)?;
            }
        }
        for config in &self.config {
            if config.ty.is_none() {
                return Err(Error::new(
                    config.name.span(),
                    "standalone configuration needs an inline type",
                ));
            }
            explicit_type(config.ty.as_ref().expect("configuration type was checked"))?;
        }
        for spawn in &self.spawn {
            if spawn.inputs.is_none() {
                return Err(Error::new(
                    spawn.name.span(),
                    "standalone spawn alias needs a signature, including `()` for no inputs",
                ));
            }
        }
        Ok(())
    }
}

fn explicit_type(ty: &Type) -> syn::Result<()> {
    if matches!(ty, Type::ImplTrait(_) | Type::Infer(_)) {
        return Err(Error::new(
            ty.span(),
            "declare an explicit type; resource trait requirements belong in `bounds`",
        ));
    }
    Ok(())
}

/// Which RTIC task shape a definition was authored as. RTIC itself makes the
/// same distinction by signature, so the authored `fn` versus `async fn` is
/// what decides it here.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TaskKind {
    /// `async fn` - dispatched in software, may take inputs and diverge.
    Software,
    /// `fn` - bound to an interrupt by composition, takes only its context.
    Hardware,
}

#[derive(Clone, Debug)]
pub struct TaskContract {
    pub arguments: TaskArguments,
    pub kind: TaskKind,
    pub context: Ident,
    pub inputs: Vec<Parameter>,
    pub diverges: bool,
}

impl TaskContract {
    pub fn new(arguments: TaskArguments, signature: &Signature) -> syn::Result<Self> {
        arguments.validate_standalone()?;
        if signature.unsafety.is_some()
            || signature.abi.is_some()
            || signature.constness.is_some()
            || !signature.generics.params.is_empty()
            || signature.generics.where_clause.is_some()
            || signature.variadic.is_some()
        {
            return Err(Error::new(
                signature.span(),
                "standalone tasks must be safe functions without generics or an ABI",
            ));
        }
        let kind = if signature.asyncness.is_some() {
            TaskKind::Software
        } else {
            TaskKind::Hardware
        };
        let mut parameters = signature.inputs.iter();
        let context = parameter(
            parameters
                .next()
                .ok_or_else(|| Error::new(signature.span(), "task needs a context parameter"))?,
        )?;
        let Type::Path(context_type) = &context.ty else {
            return Err(Error::new(context.ty.span(), "expected task_name::Context"));
        };
        let segments = &context_type.path.segments;
        if context_type.qself.is_some()
            || context_type.path.leading_colon.is_some()
            || segments.len() != 2
            || identifier_key(&segments[0].ident) != identifier_key(&signature.ident)
            || segments[1].ident != "Context"
            || segments.iter().any(|s| !s.arguments.is_empty())
        {
            return Err(Error::new(context.ty.span(), "expected task_name::Context"));
        }
        let inputs = parameters.map(parameter).collect::<syn::Result<Vec<_>>>()?;
        unique(
            std::iter::once(&context.name).chain(inputs.iter().map(|p| &p.name)),
            "duplicate task parameter",
        )?;
        let diverges = match &signature.output {
            ReturnType::Default => false,
            ReturnType::Type(_, ty) => match ty.as_ref() {
                Type::Never(_) => true,
                Type::Tuple(tuple) if tuple.elems.is_empty() => false,
                _ => {
                    return Err(Error::new(
                        ty.span(),
                        "initial SW tasks must return `()` or `!`",
                    ));
                }
            },
        };
        if kind == TaskKind::Hardware {
            // An interrupt handler is entered by the hardware, so there is no
            // caller to supply inputs and nowhere for a `!` return to go.
            if let Some(extra) = inputs.first() {
                return Err(Error::new(
                    extra.name.span(),
                    "hardware tasks take only their context; an interrupt has no caller to pass inputs",
                ));
            }
            if diverges {
                return Err(Error::new(
                    signature.span(),
                    "hardware tasks must return `()`; an interrupt handler has to return",
                ));
            }
        }
        Ok(Self {
            arguments,
            kind,
            context: context.name,
            inputs,
            diverges,
        })
    }
}

fn parameter(argument: &FnArg) -> syn::Result<Parameter> {
    let FnArg::Typed(argument) = argument else {
        return Err(Error::new(
            argument.span(),
            "task parameters cannot be receivers",
        ));
    };
    let Pat::Ident(pattern) = argument.pat.as_ref() else {
        return Err(Error::new(
            argument.pat.span(),
            "initial task parameters must be named identifiers",
        ));
    };
    if pattern.by_ref.is_some() || pattern.subpat.is_some() {
        return Err(Error::new(
            pattern.span(),
            "initial task parameters cannot use `ref` or subpatterns",
        ));
    }
    Ok(Parameter {
        name: pattern.ident.clone(),
        ty: (*argument.ty).clone(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use quote::ToTokens;

    fn contract(args: &str, function: &str) -> syn::Result<TaskContract> {
        let args = syn::parse_str(args)?;
        let function: syn::ItemFn = syn::parse_str(function)?;
        TaskContract::new(args, &function.sig)
    }
    #[test]
    fn parses_complete_standalone_contract_without_system_types() {
        let task = contract(
            "bounds = [led: StatefulOutputPin + Send], local = [led, count: u32],
             shared = [enabled: bool], config = [period_ms: u32],
             spawn = [report(value: u32), wake()], monotonic = Mono,",
            "pub async fn blink(mut cx: blink::Context, value: (u32, bool)) -> ! { loop {} }",
        )
        .unwrap();
        assert_eq!(task.context, "cx");
        assert_eq!(task.inputs.len(), 1);
        assert_eq!(
            task.inputs[0].ty.to_token_stream().to_string(),
            "(u32 , bool)"
        );
        assert!(task.diverges);
        assert_eq!(task.arguments.bounds[0].traits.len(), 2);
        assert_eq!(task.arguments.local[1].name, "count");
        assert_eq!(task.arguments.shared[0].name, "enabled");
        assert_eq!(task.arguments.config[0].name, "period_ms");
        assert_eq!(task.arguments.spawn[1].inputs.as_ref().unwrap().len(), 0);
        assert!(task.arguments.monotonic.unwrap().is_ident("Mono"));
    }

    #[test]
    fn distinguishes_an_explicit_zero_input_signature_from_a_bare_name() {
        let args: TaskArguments = syn::parse_str("spawn = [wake(), report]").unwrap();
        assert!(args.spawn[0].inputs.as_ref().unwrap().is_empty());
        assert!(args.spawn[1].inputs.is_none());
        assert!(
            args.validate_standalone()
                .unwrap_err()
                .to_string()
                .contains("signature")
        );
    }

    #[test]
    fn rejects_invalid_resource_and_configuration_contracts() {
        for (args, message) in [
            ("bounds = [led: Pin]", "must be claimed"),
            ("local = [led]", "either an inline type"),
            ("bounds = [led: Pin], local = [led: u32]", "not both"),
            (
                "local = [state: bool], shared = [state: bool]",
                "both local and shared",
            ),
            (
                "shared = [state: bool, state: bool]",
                "duplicate shared resource",
            ),
            ("config = [period_ms]", "inline type"),
            ("local = [led: impl Pin]", "explicit type"),
            ("config = [period_ms: _]", "explicit type"),
            ("config = [n: u32, n: u32]", "duplicate task configuration"),
            (
                "spawn = [report(x: u32, x: bool)]",
                "duplicate spawn parameter",
            ),
            ("dependencies = [Fugit]", "Cargo.toml"),
            ("bounds = [led: 'static], local = [led]", "trait bounds"),
        ] {
            let error = contract(args, "async fn run(cx: run::Context) {}").unwrap_err();
            assert!(error.to_string().contains(message), "{args}: {error}");
        }
    }

    #[test]
    fn rejects_duplicate_keys_missing_commas_and_dependency_trailing_tokens() {
        for source in [
            "local = [a], local = [b]",
            "local = [a] shared = [b]",
            "dependencies = [D(features = [], unexpected)]",
            "priority = 1",
        ] {
            assert!(syn::parse_str::<TaskArguments>(source).is_err(), "{source}");
        }
    }

    #[test]
    fn accepts_synchronous_hardware_handlers() {
        let task = contract("", "fn on_tick(cx: on_tick::Context) {}").unwrap();
        assert_eq!(task.kind, TaskKind::Hardware);
        assert!(task.inputs.is_empty());
        assert!(!task.diverges);

        let task = contract("", "async fn run(cx: run::Context) {}").unwrap();
        assert_eq!(task.kind, TaskKind::Software);
    }

    #[test]
    fn rejects_hardware_handlers_that_cannot_be_entered_by_an_interrupt() {
        for source in [
            // an interrupt has no caller to supply inputs
            "fn on_tick(cx: on_tick::Context, value: u32) {}",
            // an interrupt handler has to return
            "fn on_tick(cx: on_tick::Context) -> ! { loop {} }",
        ] {
            assert!(contract("", source).is_err(), "{source}");
        }
    }

    #[test]
    fn rejects_unsupported_task_signatures() {
        for source in [
            "async fn run() {}",
            "async fn run(cx: other::Context) {}",
            "async fn run(cx: run::Context<'static>) {}",
            "async fn run<T>(cx: run::Context) {}",
            "async fn run(cx: run::Context) -> u32 { 0 }",
            "async fn run(cx: run::Context, (a,b): (u32,u32)) {}",
            "async fn run(cx: run::Context, cx: u32) {}",
        ] {
            assert!(contract("", source).is_err(), "{source}");
        }
    }

    #[test]
    fn accepts_returning_tasks_and_raw_parameter_names() {
        let task = contract("", "async fn run(ctx: run::Context, r#type: u32) -> () {}").unwrap();
        assert!(!task.diverges);
        assert_eq!(task.context, "ctx");
        assert_eq!(task.inputs.len(), 1);
    }
}