Skip to content

Instantly share code, notes, and snippets.

@lejenome
Last active April 4, 2017 11:28
Show Gist options
  • Save lejenome/d9791426c64baa710500c99bdb1c9b8a to your computer and use it in GitHub Desktop.
Save lejenome/d9791426c64baa710500c99bdb1c9b8a to your computer and use it in GitHub Desktop.

Rust REPL Proposal Report

Glossary

  • Rust: system programming language developed by Mozilla Research to insures the safety of memory access at compile without the need of a gc using variable borrowing/ownership analyses at compile time. More: Website, Wikipedia, Book.
  • LLVM: a collection of modular compiler technologies (source- and target-independent optimizer and code generator for many CPUs) and toolchain technologies (debugger, linker, runtime, standard library, ...). It's also known for its IR. More: Website, LLVM IR.
  • MIR: Rust mid-level intermediate representation. Used for type-checking before transition to LLVM IR. More: RFC.

Why

REPL is very useful for learning and experimenting with Rust, for Data Mining and Scientific Analyses. An example of a successful REPL for a compiled-language is cling which is C++ REPL and a subproject of CERN' ROOT project. It's based on Clang/LLVM compiler.

Expectations

  • importing external rust file code.
  • Functions, Structs, Trails, Impls, Modules, Macros definition support.
  • extern crate support (download and install missing crates) without closing session.
  • No out-of-memory errors on long running session.
  • explicit memory drop keyword for given var.
  • Bash/Zsh-like history support

Implementation/Design

  • Reuse Rust compiler code instead of starting from the scratch.
  • Add a flag/procedural macro to disable RAII (dropping var at the end of scope) of main block.
  • Save MIR semantic and borrowing/ownership information.
  • Save unexpanded MIR Macros definitions.
  • JIT-ing using LLVM using Symbolic table for previous items address.

Challenges

  • Rust is a compiled and strong-typed language. We need to add JIT support using LLVM implementation which needs fixing by itself.
  • Rust is gc-free language. We need to drop Non-referenced Shadowed vars especially on long running session with Big Data Analyse. One solution is to add new Optimization Pass to automatically drop unused memory. Discussion.
  • Rust uses RAII model to automatically drop vars on the end of scope. REPL should not add drop code to each executed input on the main (outer) block.
  • Sandbox REPL-ed code.
  • Rust developers may add a new experimental backend to the compiler to ensure that Rust is LLVM-independent language. Upstreaming (?) the REPL implementation may require LLVM-independent code too (using just MIR?).

Alternative Rust REPLs

There was many unsuccessful attempts to develop a REPL for Rust. The most known one is Rusti which is broken since build 2016-08-01 when Rust compiler dropped LLVM ExecuteEngine support. Rusti had many design limitations:

  • Recompile all attributes and functions definitions on every input. See repl.rs.
  • Declared vars not available on next input.

An other interesting project is Miri which is an interpreter for MIR (Rust mid-level IR). We could target Miri instead of LLVM JIT. It uses its own limited backend implementation and it's mainly designed for compile-time constant-expressions evaluation (like C++11 constexpr).

Short Term Goals

Extra features to implement on the next 3 months for school project.

  • Auto-Completion support
  • Code Coloring
  • rustdoc integration
  • Jupyter Kernel

Long Term Goals

Extra features that may not be related to REPL but may be useful for a wider use of Rust as REPL/interpreted language. Hopefully to be developed under a summer internship program (GSoC, Mozilla Internship,...)

  • Debugger integration.
  • Numpy uFuncs support (or integration of an other Numerical framework).
  • Ruby-like Open Class support to change Class (Struct) methods on-the-fly.

References

More References

Papers that have had more or less influence on Rust, or which one might want to consult for inspiration or to understand Rust's background.

Type system

Concurrency

Other

Papers about Rust

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment