// LANGUAGE PREVIEW — compiles and runs in this browser playground.
// Runs on the pinned Rust/Wasm compiler; Java/LLVM parity is not claimed.
// The browser compiler supports lexical class loans and primitive copy!/share!,
// NOT general Arc, class sharing, cross-actor references or suspension loans.
//
// Intended rules: two immutable loans can coexist; an exclusive mutable loan
// cannot overlap either read or another write loan. Loans must not escape.
// Copy is allowed for copyable types only. take! consumes the owner.
// share! requires shareability and thread-safety checks, not raw mutable aliases.

define class Parcel as
  pub let int value = 42;
end

pub fnc main(): int {
  let parcel = new Parcel();

  // Two immutable loans: readers coexist without transferring ownership.
  block {
    val first = borrow! parcel;
    val second = borrow! parcel;
    stdio.println(first.value + second.value); // 84
  } // both read loans end at this block boundary

  // An exclusive mutable loan; the owner must not be accessed concurrently.
  block {
    val writer = borrow! mut parcel;
    writer.value = 43;
  }

  // The compiler accepts copyable types only for copy!/share!.
  val source_number = 21;
  val copied = copy! source_number;

  // take! transfers ownership; the original owner is moved.
  val moved = take! parcel;
  stdio.println(moved.value); // 43

  // share! is limited to verified immutable scalar values in this compiler.
  val shared_number = share! 42;
  return copied + shared_number; // 63
}

// INVALID EXAMPLES — deliberately commented out (each must be diagnosed):
// val gone = take! moved;
// stdio.println(moved.value); // use after take
//
// val reader = borrow! moved;
// val writer = borrow! mut moved; // conflicting loans
//
// val uncopyable = copy! new Parcel(); // class lacks an explicit Copy contract
//
// fnc bad_loan(): Parcel {
//   val local = new Parcel();
//   val reader = borrow! local;
//   return reader; // borrow cannot escape its local owner
// }
//
// val unsafe_shared = share! borrow! mut moved;
// // Class and mutable alias sharing across actors/tenants remains unsupported.
//
// Future tests must reject loans crossing await/select and other effects.
// This is not an authorization to publish a new compiler binary.
