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
use super::merge::{merge_labeled_local_types, LabeledLocalType};
use super::*;
use proc_macro2::Ident;
impl<'a> ProjectionContext<'a> {
pub(super) fn merge_parallel_projections(
&mut self,
projections: Vec<LocalType>,
) -> Result<LocalType, ProjectionError> {
// Remove End projections as they don't contribute
let non_end: Vec<_> = projections
.into_iter()
.filter(|p| p != &LocalType::End)
.collect();
match non_end.len() {
0 => Ok(LocalType::End),
1 => Ok(non_end.into_iter().next().unwrap_or(LocalType::End)),
_ => {
// Check for conflicts
self.check_parallel_conflicts(&non_end)?;
// Multiple non-trivial projections - merge them
// Interleaving is normalized to declaration order.
// The merge creates a deterministic sequential composition.
let mut merged = LocalType::End;
for proj in non_end.into_iter().rev() {
merged = self.sequential_merge(proj, merged);
}
Ok(merged)
}
}
}
/// Check for conflicts between parallel projections
///
/// Returns an error if the projections have incompatible operations
/// that cannot be safely interleaved.
fn check_parallel_conflicts(&self, projections: &[LocalType]) -> Result<(), ProjectionError> {
// Check for conflicting sends
let mut send_targets = Vec::new();
let mut recv_sources = Vec::new();
for proj in projections {
match proj {
LocalType::Send { to, .. } => {
if send_targets.contains(to) {
return Err(ProjectionError::InconsistentParallel);
}
send_targets.push(to.clone());
}
LocalType::Receive { from, .. } => {
if recv_sources.contains(from) {
return Err(ProjectionError::InconsistentParallel);
}
recv_sources.push(from.clone());
}
LocalType::Select { to, .. } => {
if send_targets.contains(to) {
return Err(ProjectionError::InconsistentParallel);
}
send_targets.push(to.clone());
}
LocalType::Branch { from, .. } => {
if recv_sources.contains(from) {
return Err(ProjectionError::InconsistentParallel);
}
recv_sources.push(from.clone());
}
_ => {
// Other types are compatible with parallel composition
}
}
}
Ok(())
}
fn sequential_merge(&self, first: LocalType, second: LocalType) -> LocalType {
// Merge two local types sequentially
match (first, second) {
(LocalType::End, other) | (other, LocalType::End) => other,
(first, second) => {
// Chain them together
self.append_continuation(first, second)
}
}
}
fn append_continuation(&self, local_type: LocalType, continuation: LocalType) -> LocalType {
Self::append_continuation_static(local_type, continuation)
}
fn append_continuation_static(local_type: LocalType, continuation: LocalType) -> LocalType {
match local_type {
LocalType::Send {
to,
message,
continuation: cont,
} => LocalType::Send {
to,
message,
continuation: Box::new(Self::append_continuation_static(
*cont,
continuation.clone(),
)),
},
LocalType::Receive {
from,
message,
continuation: cont,
} => LocalType::Receive {
from,
message,
continuation: Box::new(Self::append_continuation_static(
*cont,
continuation.clone(),
)),
},
LocalType::End => continuation,
// For other types, just return as-is with continuation appended at the end
other => other,
}
}
pub(super) fn project_rec(
&mut self,
label: &proc_macro2::Ident,
body: &Protocol,
) -> Result<LocalType, ProjectionError> {
let body_projection = self.project_protocol(body)?;
// Only include Rec if the body actually uses this role
if body_projection == LocalType::End {
Ok(LocalType::End)
} else {
Ok(LocalType::Rec {
label: label.clone(),
body: Box::new(body_projection),
})
}
}
pub(super) fn project_var(
&mut self,
label: &proc_macro2::Ident,
) -> Result<LocalType, ProjectionError> {
Ok(LocalType::Var(label.clone()))
}
pub(super) fn merge_choice_continuations(
&mut self,
branches: &[Branch],
) -> Result<LocalType, ProjectionError> {
// Project each branch and preserve its label
let mut labeled_projections = Vec::new();
for branch in branches {
let projection = self.project_protocol(&branch.protocol)?;
labeled_projections.push((branch.label.clone(), projection));
}
// Check if all projections are identical (common case)
let projections_only: Vec<_> = labeled_projections.iter().map(|(_, p)| p).collect();
if projections_only.windows(2).all(|w| w[0] == w[1]) {
Ok(labeled_projections
.into_iter()
.next()
.map(|(_, projection)| projection)
.unwrap_or(LocalType::End))
} else {
// Different projections per branch - need to merge with label awareness
self.merge_local_types_labeled(labeled_projections)
}
}
/// Merge multiple labeled local types preserving choice labels.
///
/// This is used for merging choice continuations where we need to preserve
/// the original choice labels. When a receiver gets different messages per branch,
/// the resulting Branch node should use the semantic choice labels, not message names.
fn merge_local_types_labeled(
&self,
labeled_projections: Vec<(Ident, LocalType)>,
) -> Result<LocalType, ProjectionError> {
if labeled_projections.is_empty() {
return Ok(LocalType::End);
}
// Filter out End types as they're neutral for merging
let non_end: Vec<_> = labeled_projections
.into_iter()
.filter(|(_, p)| p != &LocalType::End)
.collect();
if non_end.is_empty() {
return Ok(LocalType::End);
}
// Fold the merge operation over all projections, preserving labels
// Safety: non_end is non-empty (checked above)
let mut iter = non_end.into_iter();
let Some((first_label, first_type)) = iter.next() else {
return Ok(LocalType::End);
};
let mut result = LabeledLocalType {
label: first_label,
local_type: first_type,
};
for (label, next) in iter {
result = merge_labeled_local_types(
&result,
&LabeledLocalType {
label,
local_type: next,
},
)?;
}
Ok(result.local_type)
}
}