Skip to content

Instantly share code, notes, and snippets.

pub fn alloc_mega_group(mblocks: nat) -> ~Block {
let n = MBLOCK_GROUP_BLOCKS(mblocks);
enum SearchResult<'a> {
PerfectMatch(~Block),
BestMatch(&'a mut BlockData, &'a mut BlockMeta),
NoMatch
}
// tail-recursive style makes the borrow-checker *a lot*
// happier, since some of the aliasing effects that occur when
// local mutable variables are reused go away (fresh lexical
#[allow(dead_code)];
use std::util;
enum List<T> {
Cons(T, ~List<T>),
Nil
}
fn pop<T>(prev: &mut ~List<T>) -> Option<~List<T>> {
pub fn freplace<T>(mut src: T, dest: &mut T) -> T {
util::swap(dest, &mut src);
src
}
fn pop<T>(prev: &mut ~List<T>) -> Option<~List<T>> {
Some(freplace(util::replace(match prev {
&~Cons(_, ref mut rs) => rs,
&~Nil => return None
fn pop<T>(prev: &mut ~List<T>) -> Option<~List<T>> {
let xs = util::replace(match prev {
&~Cons(_, ref mut rs) => rs,
&~Nil => return None
}, ~Nil);
Some(util::replace(prev, xs))
}
Less => {
let inserted = find_or_insert(&mut save.left, key, value);
skew(save);
split(save);
inserted
}
enum Bar {
Z(int),
S(~Bar)
}
fn foo<'a>(x: &'a mut Bar) -> &'a int {
match x {
&Z(_) => {
fail!()
},
/home/ezyang/Dev/rust-sandbox/test.rs:13:20: 13:27 error: cannot borrow `*z` as mutable more than once at a time
/home/ezyang/Dev/rust-sandbox/test.rs:13 let x = &mut *z;
^~~~~~~
/home/ezyang/Dev/rust-sandbox/test.rs:12:24: 12:25 note: second borrow of `*z` as mutable occurs here
/home/ezyang/Dev/rust-sandbox/test.rs:12 let r = foo(z);
^
/home/ezyang/Dev/rust-sandbox/test.rs:12:24: 12:25 note: old loan gen'd here
/home/ezyang/Dev/rust-sandbox/test.rs:12 let r = foo(z);
^
/home/ezyang/Dev/rust-sandbox/test.rs:7:4: 16:5 note: old loan kill'd here
@ezyang
ezyang / Productive.hs
Created June 10, 2016 09:51
Productive coprogramming in the Par monad
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE InstanceSigs #-}
module Productive where
-- | Productive coprogramming in the Par monad. Based off of
@ezyang
ezyang / ghc-make.rst
Created August 22, 2016 08:49
Notes on GHC make

How ghc --make works

In abstract, the job of ghc --make is very simple: compute a dependency graph between modules, and rebuild the ones that have changed. Unfortunately, the code in GhcMake is quite a bit more complicated than that, and some of this complexity is essential to the design of GHC:

  • Interaction with GHCi involves mutating process-global state.

In Backpack, we both typecheck packages with holes (producing only interface files) and compile fully instantiated packages (producing interface files and object files). While doing this, we try to minimize the amount of work the compiler does. In particular:

  1. Any package with holes is typechecked exactly once, in its most general form (with no instantiations). Partially instantiated packages are never typechecked; instead, we just rename the most general interfaces according to the instantiation "on the fly."