| @@ -0,0 +1,166 @@ |
| 1 | +//! The static `Send` check on `Subject` message types (ADR 0004). |
| 2 | +//! |
| 3 | +//! Wherever a body's (final) types mention `Subject<m>` with `m` known, |
| 4 | +//! `m` must be sendable: |
| 5 | +//! - `Ref` is not: a mutable reference belongs to its region's actor; |
| 6 | +//! - `Resource<k>` is only if the kind type `k` is marked `@thread_safe` |
| 7 | +//! (its finalizer and operations may then run on any thread); |
| 8 | +//! - an ADT is if its arguments and its constructors' fields are; |
| 9 | +//! tuples likewise; |
| 10 | +//! - type variables and function types are assumed sendable. |
| 11 | +//! |
| 12 | +//! This is deliberately not a trait: generic code needs no `where Send<m>` |
| 13 | +//! and is not checked (decided by the project owner, 2026-10-09). The |
| 14 | +//! runtime refuses what slips through, closures' captures included, when |
| 15 | +//! the message is sent (ADR 0002, Revision 1): the sender crashes. |
| 16 | + |
| 17 | +use std::collections::HashMap; |
| 18 | + |
| 19 | +use tilc_diag::Diagnostic; |
| 20 | +use tilc_hir::{DefId, DefKind, TypeBody}; |
| 21 | +use tilc_span::Span; |
| 22 | + |
| 23 | +use crate::env::Env; |
| 24 | +use crate::ty::Ty; |
| 25 | +use crate::{BodyResults, codes}; |
| 26 | + |
| 27 | +/// Report each body's first use of every unsendable `Subject` type. |
| 28 | +pub(crate) fn check_messages( |
| 29 | + env: &Env, |
| 30 | + bodies: &HashMap<DefId, BodyResults>, |
| 31 | + diags: &mut Vec<Diagnostic>, |
| 32 | +) { |
| 33 | + let hir = env.hir; |
| 34 | + let Some(subject) = hir |
| 35 | + .module_by_path("std/process") |
| 36 | + .and_then(|m| hir.exports[m.index()].types.get("Subject").copied()) |
| 37 | + else { |
| 38 | + return; |
| 39 | + }; |
| 40 | + let mut check = Sendable { |
| 41 | + env, |
| 42 | + ref_: hir |
| 43 | + .module_by_path("std/ref") |
| 44 | + .and_then(|m| hir.exports[m.index()].types.get("Ref").copied()), |
| 45 | + resource: hir |
| 46 | + .module_by_path("std/ffi") |
| 47 | + .and_then(|m| hir.exports[m.index()].types.get("Resource").copied()), |
| 48 | + defs: HashMap::new(), |
| 49 | + }; |
| 50 | + let mut ids: Vec<_> = bodies.keys().copied().collect(); |
| 51 | + ids.sort(); |
| 52 | + for id in ids { |
| 53 | + let results = &bodies[&id]; |
| 54 | + let DefKind::Fn(f) = &hir.def(id).kind else { |
| 55 | + continue; |
| 56 | + }; |
| 57 | + let Some(body) = &f.body else { continue }; |
| 58 | + let file = env.file_of(id); |
| 59 | + let mut reported = Vec::new(); |
| 60 | + for (i, ty) in results.types.exprs.iter().enumerate() { |
| 61 | + let mut messages = Vec::new(); |
| 62 | + ty.visit(&mut |t| { |
| 63 | + if let Ty::Con(d, args) = t |
| 64 | + && *d == subject |
| 65 | + && let [m] = args.as_slice() |
| 66 | + { |
| 67 | + messages.push(m.clone()); |
| 68 | + } |
| 69 | + }); |
| 70 | + for m in messages { |
| 71 | + if reported.contains(&m) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + let Some(why) = check.not_send(&m) else { |
| 75 | + continue; |
| 76 | + }; |
| 77 | + let shown = results.show(hir, &m); |
| 78 | + let span = Span { |
| 79 | + file, |
| 80 | + range: body.expr_spans[i], |
| 81 | + }; |
| 82 | + diags.push( |
| 83 | + Diagnostic::error( |
| 84 | + codes::NOT_SEND, |
| 85 | + format!("messages of type `{shown}` cannot be sent to another actor"), |
| 86 | + span, |
| 87 | + ) |
| 88 | + .with_primary_label(format!("a subject for `{shown}`")) |
| 89 | + .with_note(why), |
| 90 | + ); |
| 91 | + reported.push(m); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +struct Sendable<'a, 'h> { |
| 98 | + env: &'a Env<'h>, |
| 99 | + ref_: Option<DefId>, |
| 100 | + resource: Option<DefId>, |
| 101 | + /// Why values of a type definition are not sendable whatever its |
| 102 | + /// arguments (`None`: they are, or the check is in progress). |
| 103 | + defs: HashMap<DefId, Option<String>>, |
| 104 | +} |
| 105 | + |
| 106 | +impl Sendable<'_, '_> { |
| 107 | + /// Why `ty` is not sendable, if it is not. |
| 108 | + fn not_send(&mut self, ty: &Ty) -> Option<String> { |
| 109 | + match ty { |
| 110 | + Ty::Con(d, _) if Some(*d) == self.ref_ => Some( |
| 111 | + "a `Ref` belongs to its region, in one actor: send the value it holds instead" |
| 112 | + .into(), |
| 113 | + ), |
| 114 | + Ty::Con(d, args) if Some(*d) == self.resource => match args.first() { |
| 115 | + Some(Ty::Con(kind, _)) if !self.env.hir.def(*kind).has_attr("thread_safe") => { |
| 116 | + let name = &self.env.hir.def(*kind).name; |
| 117 | + Some(format!( |
| 118 | + "`Resource<{name}>` may only be used by the actor that made it; if its foreign side is thread-safe, mark `{name}` with `@thread_safe`" |
| 119 | + )) |
| 120 | + } |
| 121 | + _ => None, |
| 122 | + }, |
| 123 | + Ty::Con(d, args) => args |
| 124 | + .iter() |
| 125 | + .find_map(|a| self.not_send(a)) |
| 126 | + .or_else(|| self.def_not_send(*d)), |
| 127 | + Ty::Tuple(items) => items.iter().find_map(|t| self.not_send(t)), |
| 128 | + // Closures are checked when sent; variables are generic. |
| 129 | + _ => None, |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /// Why a type definition's own fields make it unsendable (its type |
| 134 | + /// parameters count as sendable). Recursive types assume they are |
| 135 | + /// while being checked. |
| 136 | + fn def_not_send(&mut self, def: DefId) -> Option<String> { |
| 137 | + if let Some(why) = self.defs.get(&def) { |
| 138 | + return why.clone(); |
| 139 | + } |
| 140 | + self.defs.insert(def, None); |
| 141 | + let hir = self.env.hir; |
| 142 | + let DefKind::Type(t) = &hir.def(def).kind else { |
| 143 | + return None; |
| 144 | + }; |
| 145 | + let TypeBody::Adt(ctors) = &t.body else { |
| 146 | + return None; |
| 147 | + }; |
| 148 | + let mut why = None; |
| 149 | + for ctor in ctors { |
| 150 | + let Some(scheme) = self.env.schemes.get(ctor) else { |
| 151 | + continue; |
| 152 | + }; |
| 153 | + if let Ty::Fn(fields, _, _) = &scheme.ty |
| 154 | + && let Some(field) = fields.iter().find_map(|f| self.not_send(f)) |
| 155 | + { |
| 156 | + why = Some(format!( |
| 157 | + "`{}` has a field that cannot be sent: {field}", |
| 158 | + hir.def(def).name |
| 159 | + )); |
| 160 | + break; |
| 161 | + } |
| 162 | + } |
| 163 | + self.defs.insert(def, why.clone()); |
| 164 | + why |
| 165 | + } |
| 166 | +} |