| @@ -442,8 +442,37 @@ impl FnInfer<'_, '_> { |
| 442 | 442 | let Some(scheme) = self.env.schemes.get(&op).cloned() else { |
| 443 | 443 | continue; |
| 444 | 444 | }; |
| 445 | | − let (op_ty, _) = self.ctx.instantiate(&scheme); |
| 445 | + // The handler answers for every instantiation of the |
| 446 | + // operation's own type variables, so they are rigid in the |
| 447 | + // clause (issue #169): `pick(_) -> 42` cannot fix `a := Int` |
| 448 | + // for a caller that expects a `String`. The effect's |
| 449 | + // parameters, its instance and the row tail stay flexible: |
| 450 | + // they are fixed by the handled label. An operation's scheme |
| 451 | + // binds the effect's parameters, then its own variables, then |
| 452 | + // the instance (if any) and the tail (`Env::collect_schemes`). |
| 453 | + let effect = effect_of(op); |
| 454 | + let n_params = match effect.map(|e| &hir.def(e).kind) { |
| 455 | + Some(DefKind::Effect(e)) => e.params.len(), |
| 456 | + _ => 0, |
| 457 | + }; |
| 458 | + let implicit = effect.is_some_and(|e| self.env.implicit_instance(e)); |
| 459 | + let own_end = scheme.vars.len().saturating_sub(1 + usize::from(implicit)); |
| 460 | + self.ctx.enter(); |
| 461 | + let args: Vec<Ty> = scheme |
| 462 | + .vars |
| 463 | + .iter() |
| 464 | + .enumerate() |
| 465 | + .map(|(i, name)| { |
| 466 | + if (n_params..own_end).contains(&i) { |
| 467 | + self.ctx.fresh_scoped(name) |
| 468 | + } else { |
| 469 | + self.ctx.fresh() |
| 470 | + } |
| 471 | + }) |
| 472 | + .collect(); |
| 473 | + let op_ty = scheme.ty.subst_gen(&args); |
| 446 | 474 | let Ty::Fn(params, op_ret, op_eff) = op_ty else { |
| 475 | + self.ctx.leave(); |
| 447 | 476 | continue; |
| 448 | 477 | }; |
| 449 | 478 | // Tie the operation's effect arguments to the handled label. |
| @@ -469,7 +498,10 @@ impl FnInfer<'_, '_> { |
| 469 | 498 | } |
| 470 | 499 | let kind = match &hir.def(op).kind { |
| 471 | 500 | DefKind::Op(o) => o.kind, |
| 472 | | − _ => continue, |
| 501 | + _ => { |
| 502 | + self.ctx.leave(); |
| 503 | + continue; |
| 504 | + } |
| 473 | 505 | }; |
| 474 | 506 | match kind { |
| 475 | 507 | louise_hir::OpKind::Fn => self.check_expr(clause.body, &op_ret), |
| @@ -482,6 +514,7 @@ impl FnInfer<'_, '_> { |
| 482 | 514 | } |
| 483 | 515 | louise_hir::OpKind::FinalCtl => self.check_expr(clause.body, &result), |
| 484 | 516 | } |
| 517 | + self.ctx.leave(); |
| 485 | 518 | } |
| 486 | 519 | // The handler exists only while its body runs (ADR 0010). |
| 487 | 520 | if let Some(Ty::Rigid(r)) = instance |