| @@ -0,0 +1,152 @@ |
| 1 | +//! Functions passed to C: the callback parameters of `@extern` functions |
| 2 | +//! (ADR 0015). |
| 3 | +//! |
| 4 | +//! A callback runs in the actor that called into C, while that call is in |
| 5 | +//! progress, and that actor's fiber must not suspend meanwhile. So a |
| 6 | +//! callback may perform `<ffi>` at most (no `proc` operation, no socket), |
| 7 | +//! and a `blocking` extern, which runs on another thread, takes none. |
| 8 | + |
| 9 | +use tilc_diag::Diagnostic; |
| 10 | +use tilc_span::Span; |
| 11 | + |
| 12 | +use crate::Hir; |
| 13 | +use crate::codes; |
| 14 | +use crate::def::*; |
| 15 | + |
| 16 | +/// `ty` as a function type, through type aliases. |
| 17 | +pub fn as_fn_type<'a>(hir: &'a Hir, ty: &'a TypeRef) -> Option<&'a TypeRef> { |
| 18 | + let mut ty = ty; |
| 19 | + // Bounded: alias cycles are reported elsewhere. |
| 20 | + for _ in 0..16 { |
| 21 | + match ty { |
| 22 | + TypeRef::Fn { .. } => return Some(ty), |
| 23 | + TypeRef::Def { def, .. } => match &hir.def(*def).kind { |
| 24 | + DefKind::Type(TypeDef { |
| 25 | + body: TypeBody::Alias(target), |
| 26 | + .. |
| 27 | + }) => ty = target, |
| 28 | + _ => return None, |
| 29 | + }, |
| 30 | + _ => return None, |
| 31 | + } |
| 32 | + } |
| 33 | + None |
| 34 | +} |
| 35 | + |
| 36 | +/// Whether the extern `def` takes a function, which C may call back. |
| 37 | +pub fn takes_callbacks(hir: &Hir, def: DefId) -> bool { |
| 38 | + let DefKind::Fn(f) = &hir.def(def).kind else { |
| 39 | + return false; |
| 40 | + }; |
| 41 | + f.sig |
| 42 | + .params |
| 43 | + .iter() |
| 44 | + .filter_map(|p| p.ty.as_ref()) |
| 45 | + .any(|t| as_fn_type(hir, t).is_some()) |
| 46 | +} |
| 47 | + |
| 48 | +fn is_ffi(hir: &Hir, label: &TypeRef) -> bool { |
| 49 | + match label { |
| 50 | + TypeRef::Def { def, .. } => { |
| 51 | + let d = hir.def(*def); |
| 52 | + d.name == "ffi" && hir.modules[d.module].path == "std/prelude" |
| 53 | + } |
| 54 | + _ => false, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Report extern callback parameters that break the rules above |
| 59 | +/// (`E0113`). |
| 60 | +pub(crate) fn check(hir: &Hir, diags: &mut Vec<Diagnostic>) { |
| 61 | + for (_, def) in hir.defs.iter() { |
| 62 | + let Some(attr) = def.attrs.iter().find(|a| a.name == "extern") else { |
| 63 | + continue; |
| 64 | + }; |
| 65 | + let DefKind::Fn(f) = &def.kind else { |
| 66 | + continue; |
| 67 | + }; |
| 68 | + let file = hir.modules[def.module].file; |
| 69 | + let mut error = |message: String, range, note: &str| { |
| 70 | + let mut d = Diagnostic::error(codes::INVALID_CALLBACK, message, Span { file, range }); |
| 71 | + if !note.is_empty() { |
| 72 | + d = d.with_note(note); |
| 73 | + } |
| 74 | + diags.push(d); |
| 75 | + }; |
| 76 | + let blocking = attr.args.iter().skip(2).any(|a| a == "blocking"); |
| 77 | + let own_ffi = f |
| 78 | + .sig |
| 79 | + .effects |
| 80 | + .as_ref() |
| 81 | + .is_some_and(|row| row.labels.iter().any(|l| is_ffi(hir, l))); |
| 82 | + for p in &f.sig.params { |
| 83 | + let Some(ty) = &p.ty else { continue }; |
| 84 | + let Some(TypeRef::Fn { effects, span, .. }) = as_fn_type(hir, ty) else { |
| 85 | + if nested_fn(hir, ty) { |
| 86 | + error( |
| 87 | + format!( |
| 88 | + "parameter `{}` of extern `{}` holds a function: only a parameter itself can be one", |
| 89 | + p.name, def.name |
| 90 | + ), |
| 91 | + ty.span(), |
| 92 | + "C receives functions as callbacks, which only parameters can be (ADR 0015)", |
| 93 | + ); |
| 94 | + } |
| 95 | + continue; |
| 96 | + }; |
| 97 | + let span = if matches!(ty, TypeRef::Fn { .. }) { |
| 98 | + *span |
| 99 | + } else { |
| 100 | + ty.span() |
| 101 | + }; |
| 102 | + if blocking { |
| 103 | + error( |
| 104 | + format!( |
| 105 | + "the `blocking` extern `{}` cannot take a function", |
| 106 | + def.name |
| 107 | + ), |
| 108 | + span, |
| 109 | + "a blocking call runs on another thread, where tilcayo code cannot run; remove `blocking` or the callback", |
| 110 | + ); |
| 111 | + } |
| 112 | + let row_ok = effects |
| 113 | + .as_ref() |
| 114 | + .is_none_or(|row| row.tail.is_none() && row.labels.iter().all(|l| is_ffi(hir, l))); |
| 115 | + if !row_ok { |
| 116 | + error( |
| 117 | + format!( |
| 118 | + "a function passed to C (parameter `{}` of `{}`) may perform `<ffi>` at most", |
| 119 | + p.name, def.name |
| 120 | + ), |
| 121 | + span, |
| 122 | + "it runs while its actor is inside the C call, which cannot wait or be preempted meanwhile", |
| 123 | + ); |
| 124 | + } else if !own_ffi |
| 125 | + && effects |
| 126 | + .as_ref() |
| 127 | + .is_some_and(|row| row.labels.iter().any(|l| is_ffi(hir, l))) |
| 128 | + { |
| 129 | + error( |
| 130 | + format!( |
| 131 | + "extern `{}` calls a function that performs `ffi`, so it performs `ffi` too", |
| 132 | + def.name |
| 133 | + ), |
| 134 | + span, |
| 135 | + "add `ffi` to its effect row", |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/// Whether `ty` is or contains a function type. |
| 143 | +fn nested_fn(hir: &Hir, ty: &TypeRef) -> bool { |
| 144 | + if as_fn_type(hir, ty).is_some() { |
| 145 | + return true; |
| 146 | + } |
| 147 | + match ty { |
| 148 | + TypeRef::Def { args, .. } => args.iter().any(|a| nested_fn(hir, a)), |
| 149 | + TypeRef::Tuple { elems, .. } => elems.iter().any(|e| nested_fn(hir, e)), |
| 150 | + _ => false, |
| 151 | + } |
| 152 | +} |