Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ jobs:
GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go vet ./hybridge/
# row 18: the workload triplet's shapes are emitted by shape.py —
# drift between the generator and the committed programs fails CI
python3 benchmarks/workloads/shape.py --check
python3 "$GITHUB_WORKSPACE/benchmarks/workloads/shape.py" --check
echo "go runtime hygiene OK"
- name: MVP-0 examples end-to-end (parse → check → emit → go build → run)
run: |
Expand Down
30 changes: 30 additions & 0 deletions compiler/crates/hy-check/src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ fn check_one_closure(
env: st.env.clone(),
..FnState::default()
};
for (k, b) in cst.env.iter_mut() {
if b.non_owned_origin.is_none() {
b.non_owned_origin = Some(format!(
"captured outer binding `{}` inside a closure (the enclosing scope retains `{}` and the closure may run multiple times)",
k, k
));
}
}
for p in params {
let shape = ck.shape_of_type(&p.ty);
cst.env.insert(
Expand All @@ -60,6 +68,10 @@ fn check_one_closure(
immutable: false,
managed_taint: false,
moved: None,
non_owned_origin: Some(format!(
"closure parameter `{}` (the caller retains the argument across calls)",
p.name
)),
},
);
}
Expand Down Expand Up @@ -207,9 +219,27 @@ fn check_stmt_boundaries(ck: &mut Checker<'_>, st: &mut FnState, stmt: &Stmt, ma
st.scope_push(); // the loop vars' scope
match kind {
Iterate { vars, expr } => {
let is_chan = matches!(ck.shape_of_expr(st, expr), TypeShape::Chan(_));
let shapes = ck.iterate_var_shapes(st, expr, vars.len());
for (name, shape) in vars.iter().zip(shapes) {
let is_ref = shape.is_reference_alias();
ck.define_binding(st, name, shape, None, false, managed);
if !is_chan {
if is_ref {
if let Some(root) = expr.root_ident() {
if ck.lookup_binding(st, root).is_some() {
st.aliases.insert(name.clone(), root.to_string());
}
}
}
if let Some(b) = st.env_lookup_mut(name) {
b.non_owned_origin = Some(format!(
"range variable `{}` iterating `{}` (the collection still holds the element)",
name,
expr.render()
));
}
}
}
}
Range { var, .. } => {
Expand Down
51 changes: 51 additions & 0 deletions compiler/crates/hy-check/src/freeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,56 @@ impl<'a> Checker<'a> {
Diagnostic::error("HYB-E3004", self.path.clone(), span, msg).with_notes(notes),
);
}

/// O5.1 / O5.2 / O5.4: `move x` at a concurrency boundary requires that `x`
/// be an exclusively owned local binding so freezing `x` (and its local
/// aliases) freezes every live reference in the sending task. A function/
/// closure parameter or receiver, an outer binding captured by a closure,
/// or a range variable over a non-channel collection remains aliased in
/// the caller / enclosing scope / collection, so moving it is rejected
/// with HYB-E3004 (`tier0_rule: "A1b"`).
pub(crate) fn check_non_owned_move(
&mut self,
st: &FnState,
name: &str,
span: hy_diag::Span,
) {
let Some(b) = self.lookup_binding(st, name) else {
return;
};
if b.shape.is_sendable_by_type() || b.immutable || b.managed_taint {
return;
}
let root = resolve_alias(st, name);
let origin = b.non_owned_origin.clone().or_else(|| {
self.lookup_binding(st, &root)
.and_then(|rb| rb.non_owned_origin.clone())
});
let Some(origin) = origin else {
return;
};
let ty_str = b
.ty
.as_ref()
.map(|t| t.render())
.unwrap_or_else(|| b.shape.render());
let msg = format!(
"cannot move `{}` ({}) — `move` requires an exclusively owned local binding, not {} (O5.1 / O5.4)",
name, ty_str, origin
);
let notes = vec![
format!(
"why: {} still aliases the underlying storage outside this move site, so freezing `{}` cannot prevent concurrent access (O5.2 / O5.4)",
origin, name
),
format!(
"fix: copy `{}` into a fresh local before moving (`fresh := ...; move fresh`), move the caller's owned binding at the spawn/send site directly, or wrap a genuinely synchronized share in `managed(\"reason\", \"category\") {{ ... }}`",
name
),
];
self.diags.push(
Diagnostic::error("HYB-E3004", self.path.clone(), span, msg).with_notes(notes),
);
}
}

98 changes: 98 additions & 0 deletions compiler/crates/hy-check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ fn check_fn(ck: &mut Checker<'_>, f: &FnDecl) {
immutable: false,
managed_taint: false,
moved: None,
non_owned_origin: Some(format!(
"method receiver `{}` (the caller retains the receiver across intra-task calls)",
r.name
)),
},
);
}
Expand All @@ -205,6 +209,10 @@ fn check_fn(ck: &mut Checker<'_>, f: &FnDecl) {
immutable: false,
managed_taint: false,
moved: None,
non_owned_origin: Some(format!(
"parameter `{}` (the caller retains the argument across intra-task calls)",
p.name
)),
},
);
}
Expand Down Expand Up @@ -392,9 +400,27 @@ fn check_fn(ck: &mut Checker<'_>, f: &FnDecl) {
// Loop variables: fresh bindings per iteration shape.
Stmt::For { kind, .. } => match kind {
ForKind::Iterate { vars, expr } => {
let is_chan = matches!(ck.shape_of_expr(&st, expr), TypeShape::Chan(_));
let shapes = ck.iterate_var_shapes(&st, expr, vars.len());
for (name, shape) in vars.iter().zip(shapes) {
let is_ref = shape.is_reference_alias();
ck.define_binding(&mut st, name, shape, None, false, managed);
if !is_chan {
if is_ref {
if let Some(root) = expr.root_ident() {
if ck.lookup_binding(&st, root).is_some() {
st.aliases.insert(name.clone(), root.to_string());
}
}
}
if let Some(b) = st.env_lookup_mut(name) {
b.non_owned_origin = Some(format!(
"range variable `{}` iterating `{}` (the collection still holds the element)",
name,
expr.render()
));
}
}
}
}
ForKind::Range { var, .. } => {
Expand Down Expand Up @@ -1466,5 +1492,77 @@ mod tests {
assert_eq!(r.verdict, Verdict::Accept, "{:?}", r.diagnostics);
assert!(r.diagnostics.is_empty(), "{:?}", r.diagnostics);
}

#[test]
fn non_owned_param_capture_and_range_moves_reject_e3004() {
// 1. Function parameter moved across a task boundary: caller retains `m`.
let r_param = check(
"fn worker(m map[string]int, done chan<- bool) {\n\
m[\"k\"] = 1\n\
done <- true\n\
}\n\
fn spawn_it(m map[string]int, done chan<- bool) {\n\
task worker(move m, done)\n\
}\n\
fn main() {\n\
m := map[string]int{\"k\": 0}\n\
done := make(chan bool, 1)\n\
spawn_it(m, done)\n\
m[\"k\"] = 2\n\
<-done\n\
}",
);
assert_eq!(r_param.verdict, Verdict::Reject, "{:?}", r_param.diagnostics);
assert_eq!(first_error_rule(&r_param).as_deref(), Some("A1b"));
assert!(r_param.diagnostics.iter().any(|d| d.code == "HYB-E3004"));

// 2.Captured outer binding moved inside a closure: enclosing scope retains `m`.
let r_cap = check(
"fn worker(m map[string]int, done chan<- bool) {\n\
m[\"k\"] = 1\n\
done <- true\n\
}\n\
fn main() {\n\
m := map[string]int{\"k\": 0}\n\
done := make(chan bool, 1)\n\
f := fn() {\n\
task worker(move m, done)\n\
}\n\
f()\n\
m[\"k\"] = 2\n\
<-done\n\
}",
);
assert_eq!(r_cap.verdict, Verdict::Reject, "{:?}", r_cap.diagnostics);
assert_eq!(first_error_rule(&r_cap).as_deref(), Some("A1b"));
assert!(r_cap.diagnostics.iter().any(|d| d.code == "HYB-E3004"));

// 3. Non-channel `for k, v in mm` range variable moved: `mm` still holds the element.
let r_range = check(
"fn worker(m map[string]int) { m[\"k\"] = 1 }\n\
fn main() {\n\
mm := map[string]map[string]int{\"a\": map[string]int{\"k\": 0}}\n\
for k, v in mm {\n\
task worker(move v)\n\
}\n\
mm[\"a\"][\"k\"] = 2\n\
}",
);
assert_eq!(r_range.verdict, Verdict::Reject, "{:?}", r_range.diagnostics);
assert_eq!(first_error_rule(&r_range).as_deref(), Some("A1b"));
assert!(r_range.diagnostics.iter().any(|d| d.code == "HYB-E3004"));
assert!(r_range.diagnostics.iter().any(|d| d.code == "HYB-E3003"));

// 4. Positive control: channel iteration (`for b in ch`) transfers ownership per element.
let r_chan = check(
"struct batch { rows []int }\n\
fn relay(in_ch <-chan batch, out_ch chan<- batch) {\n\
for b in in_ch {\n\
out_ch <- move b\n\
}\n\
}",
);
assert_eq!(r_chan.verdict, Verdict::Accept, "{:?}", r_chan.diagnostics);
}
}

25 changes: 25 additions & 0 deletions compiler/crates/hy-check/src/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,21 @@ impl<'a> Checker<'a> {
};
// `x := move y` — y is moved; x takes ownership (no alias);
// non-ident moves (`x := move s.data`) trigger HYB-E3004 (O5.4).
let moved_non_owned = move_operand_ident(value)
.and_then(|src| self.lookup_binding(st, &src))
.and_then(|b| b.non_owned_origin.clone());
self.mark_move_in_value(
st,
value,
format!("the move-define `{}`", value.render()),
);
if move_operand_ident(value).is_some() {
self.define_binding(st, name, shape, ty, immutable, managed);
if let Some(origin) = moved_non_owned {
if let Some(b) = st.env_lookup_mut(name) {
b.non_owned_origin = Some(origin);
}
}
continue;
}
let is_view = matches!(shape, TypeShape::BorrowHandle(_));
Expand All @@ -365,9 +373,15 @@ impl<'a> Checker<'a> {
// alias recording: `m2 := agg` (reference-typed value)
if let Expr::Ident { name: src, .. } = value {
if let Some(b) = self.lookup_binding(st, src) {
let src_origin = b.non_owned_origin.clone();
if b.shape.is_reference_alias() {
st.aliases.insert(name.clone(), src.clone());
}
if let Some(origin) = src_origin {
if let Some(nb) = st.env_lookup_mut(name) {
nb.non_owned_origin = Some(origin);
}
}
}
}
}
Expand Down Expand Up @@ -421,6 +435,7 @@ impl<'a> Checker<'a> {
immutable,
managed_taint: managed, // O6.2: born inside `managed {}`
moved: None,
non_owned_origin: None,
},
);
}
Expand Down Expand Up @@ -500,12 +515,21 @@ impl<'a> Checker<'a> {
if op == AssignOp::Assign && values.len() == 1 {
let shape = self.shape_of_expr(st, &values[0]);
let ty = self.ty_of_expr(st, &values[0]);
let rhs_non_owned = match &values[0] {
Expr::Ident { name: src, .. } => self
.lookup_binding(st, src)
.and_then(|b| b.non_owned_origin.clone()),
other => move_operand_ident(other)
.and_then(|src| self.lookup_binding(st, &src))
.and_then(|b| b.non_owned_origin.clone()),
};
for t in targets {
if let PlaceKind::Ident(name) = &t.kind {
if let Some(b) = st.env_lookup_mut(name) {
b.shape = shape.clone();
b.ty = ty.clone();
b.moved = None; // a fresh value re-owns the binding
b.non_owned_origin = rhs_non_owned.clone();
}
// rebind may create a fresh alias
st.aliases.remove(name);
Expand Down Expand Up @@ -688,6 +712,7 @@ impl<'a> Checker<'a> {
immutable: true, // class 4: const values are immutable-by-type
managed_taint: false,
moved: None,
non_owned_origin: None,
}
}
}
5 changes: 5 additions & 0 deletions compiler/crates/hy-check/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ pub(crate) struct Binding {
pub managed_taint: bool,
/// Moved-from / consumed-from state (O5.1 / O13).
pub moved: Option<MoveInfo>,
/// When `Some(reason)`, this binding is not exclusively owned by the
/// current function/closure invocation (parameter, receiver, closure
/// capture, or non-channel `for ... in` element), so `move x` at a
/// concurrency boundary cannot freeze all live aliases (O5.1/O5.2/O5.4).
pub non_owned_origin: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
6 changes: 6 additions & 0 deletions compiler/crates/hy-check/src/sendability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ impl<'a> Checker<'a> {
let op = crate::model::unwrap_parens(v);
if let Expr::Ident { name, span: ispan } = op {
if self.lookup_binding(st, name).is_some() {
if !managed {
self.check_non_owned_move(st, name, *ispan);
}
moves.push((name.clone(), *ispan));
} else {
unprovens.push(UnprovenInfo {
Expand All @@ -600,6 +603,9 @@ impl<'a> Checker<'a> {
for (op, mspan) in v_moves {
if let Expr::Ident { name, .. } = op {
if self.lookup_binding(st, name).is_some() {
if !managed {
self.check_non_owned_move(st, name, mspan);
}
moves.push((name.clone(), mspan));
} else {
unprovens.push(UnprovenInfo {
Expand Down
Loading