<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>src/doc/rustc-ux-guidelines.md - fork/rust @ 1.43.0</title>
<meta name="description" content="src/doc/rustc-ux-guidelines.md in fork/rust at 1.43.0.">
<meta property="og:title" content="src/doc/rustc-ux-guidelines.md - fork/rust @ 1.43.0">
<meta property="og:description" content="src/doc/rustc-ux-guidelines.md in fork/rust at 1.43.0.">
<meta property="og:type" content="website">
<meta property="og:site_name" content="git.julia.blue">
<link rel="stylesheet" href="/css/style.css?v=62">
<script type="module" src="/js/main.js?v=62"></script>
</head>
<body>
<main data-ref="1.43.0" data-tip="4fb7144ed159f94491249e86d5bbd033b5d60550">
<noscript>
  <div class="callout">
    <p>
      hey! this is a static git repo viewer. commit diffs, file history,
      older commits and other revisions won't load without javascript.
    </p>
    <p>
      you are still able to view source files on branch/tag tips,
      but the experience is degraded.
    </p>
  </div>
</noscript>
<header class="repo"><div>
<h1>fork/rust</h1>
</div>
<pre class="clone">git clone https://git.julia.blue/fork/rust</pre>
</header>
<p class="snapshot-commit"><span class="commit-author">bors</span><span class="commit-message" data-commit="4fb7144ed159f94491249e86d5bbd033b5d60550">Auto merge of #71365 - Mark-Simulacrum:stable-next, r=Mark-Simulacrum</span><span class="snapshot-sha sha" data-commit="4fb7144ed159f94491249e86d5bbd033b5d60550">4fb7144ed15</span><time datetime="2020-04-20T22:16:25+00:00" title="2020-04-20 22:16:25 +0000">2020-04-20</time></p>
<header class="topbar"><details class="switcher"><summary>1.43.0</summary><div class="menu"><strong>branches</strong><a href="/fork/rust/ref/master/">master</a><strong>tags</strong><a href="/fork/rust/ref/1.39.0/">1.39.0</a><a href="/fork/rust/ref/1.40.0/">1.40.0</a><a href="/fork/rust/ref/1.41.0/">1.41.0</a><a href="/fork/rust/ref/1.41.1/">1.41.1</a><a href="/fork/rust/ref/1.42.0/">1.42.0</a><a class="current" href="/fork/rust/ref/1.43.0/">1.43.0</a><a href="/fork/rust/ref/1.43.1/">1.43.1</a><a href="/fork/rust/ref/1.44.0/">1.44.0</a><a class="all" href="/fork/rust/refs">all refs →</a></div></details><nav class="crumbs"><a href="/fork/rust/">rust</a> / <a href="/fork/rust/ref/1.43.0/tree/src/">src</a> / <a href="/fork/rust/ref/1.43.0/tree/src/doc/">doc</a> / <span class="cur">rustc-ux-guidelines.md</span></nav>
<span class="view-stats"><span>5.1 KiB</span><span>90 lines</span></span><span class="actions"><a class="raw" href="/fork/rust/raw/b626923bcb59cfd8ae48872f0bd7cc758a9b8e8a/src/doc/rustc-ux-guidelines.md">raw</a></span></header>
<div class="markdown-view"><input type="radio" name="markdown-view" id="markdown-rendered" checked><label for="markdown-rendered">rendered</label><input type="radio" name="markdown-view" id="markdown-code"><label for="markdown-code">code</label><section class="readme rendered"><p>% Rustc UX guidelines</p>
<p>Don't forget the user. Whether human or another program, such as an IDE, a
good user experience with the compiler goes a long way toward making developers'
lives better. We do not want users to be baffled by compiler output or
learn arcane patterns to compile their program.</p>
<h2>Error, Warning, Help, Note Messages</h2>
<p>When the compiler detects a problem, it can emit one of the following: an error, a warning,
a note, or a help message.</p>
<p>An <code>error</code> is emitted when the compiler detects a problem that makes it unable
to compile the program, either because the program is invalid or the
programmer has decided to make a specific <code>warning</code> into an error.</p>
<p>A <code>warning</code> is emitted when the compiler detects something odd about a
program. For instance, dead code and unused <code>Result</code> values.</p>
<p>A <code>help</code> message is emitted following an <code>error</code> or <code>warning</code> to give additional
information to the user about how to solve their problem.</p>
<p>A <code>note</code> is emitted to identify additional circumstances and parts of the code
that caused the warning or error. For example, the borrow checker will note any
previous conflicting borrows.</p>
<ul>
<li>Write in plain simple English. If your message, when shown on a – possibly
small – screen (which hasn't been cleaned for a while), cannot be understood
by a normal programmer, who just came out of bed after a night partying, it's
too complex.</li>
<li><code>Errors</code> and <code>Warnings</code> should not suggest how to fix the problem. A <code>Help</code>
message should be emitted instead.</li>
<li><code>Error</code>, <code>Warning</code>, <code>Note</code>, and <code>Help</code> messages start with a lowercase
letter and do not end with punctuation.</li>
<li>Error messages should be succinct. Users will see these error messages many
times, and more verbose descriptions can be viewed with the <code>--explain</code> flag.
That said, don't make it so terse that it's hard to understand.</li>
<li>The word "illegal" is illegal. Prefer "invalid" or a more specific word
instead.</li>
<li>Errors should document the span of code where they occur – the <code>span_..</code>
methods allow to easily do this. Also <code>note</code> other spans that have contributed
to the error if the span isn't too large.</li>
<li>When emitting a message with span, try to reduce the span to the smallest
amount possible that still signifies the issue</li>
<li>Try not to emit multiple error messages for the same error. This may require
detecting duplicates.</li>
<li>When the compiler has too little information for a specific error message,
lobby for annotations for library code that allow adding more. For example see
<code>#[on_unimplemented]</code>. Use these annotations when available!</li>
<li>Keep in mind that Rust's learning curve is rather steep, and that the
compiler messages are an important learning tool.</li>
</ul>
<h2>Error Explanations</h2>
<p>Error explanations are long form descriptions of error messages provided with
the compiler. They are accessible via the <code>--explain</code> flag. Each explanation
comes with an example of how to trigger it and advice on how to fix it.</p>
<p>Please read <a href="https://github.com/rust-lang/rfcs/blob/master/text/1567-long-error-codes-explanation-normalization.md">RFC 1567</a>
for details on how to format and write long error codes.</p>
<ul>
<li>All of them are accessible <a href="http://doc.rust-lang.org/error-index.html">online</a>,
which are auto-generated from rustc source code in different places:
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc/error_codes.rs">librustc</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_ast/error_codes.rs">librustc_ast</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_borrowck/error_codes.rs">librustc_borrowck</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_metadata/error_codes.rs">librustc_metadata</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_mir/error_codes.rs">librustc_mir</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_passes/error_codes.rs">librustc_passes</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_privacy/error_codes.rs">librustc_privacy</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_resolve/error_codes.rs">librustc_resolve</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/error_codes.rs">librustc_codegen_llvm</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_plugin/error_codes.rs">librustc_plugin_impl</a>,
<a href="https://github.com/rust-lang/rust/blob/master/src/librustc_typeck/error_codes.rs">librustc_typeck</a>.</li>
<li>Explanations have full markdown support. Use it, especially to highlight
code with backticks.</li>
<li>When talking about the compiler, call it <code>the compiler</code>, not <code>Rust</code> or
<code>rustc</code>.</li>
</ul>
<h2>Compiler Flags</h2>
<ul>
<li>Flags should be orthogonal to each other. For example, if we'd have a
json-emitting variant of multiple actions <code>foo</code> and <code>bar</code>, an additional
--json flag is better than adding <code>--foo-json</code> and <code>--bar-json</code>.</li>
<li>Always give options a long descriptive name, if only for more
understandable compiler scripts.</li>
<li>The <code>--verbose</code> flag is for adding verbose information to <code>rustc</code> output
when not compiling a program. For example, using it with the <code>--version</code> flag
gives information about the hashes of the code.</li>
<li>Experimental flags and options must be guarded behind the <code>-Z unstable-options</code> flag.</li>
</ul>
</section><div class="code-panel"><pre class="code src" data-hl="src/doc/rustc-ux-guidelines.md"><span class="code-line"><a class="ln" id="L1" href="#L1">1</a><span class="code-text">% Rustc UX guidelines</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L2" href="#L2">2</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L3" href="#L3">3</a><span class="code-text">Don't forget the user. Whether human or another program, such as an IDE, a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L4" href="#L4">4</a><span class="code-text">good user experience with the compiler goes a long way toward making developers'</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L5" href="#L5">5</a><span class="code-text">lives better. We do not want users to be baffled by compiler output or</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L6" href="#L6">6</a><span class="code-text">learn arcane patterns to compile their program.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L7" href="#L7">7</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L8" href="#L8">8</a><span class="code-text">## Error, Warning, Help, Note Messages</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L9" href="#L9">9</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L10" href="#L10">10</a><span class="code-text">When the compiler detects a problem, it can emit one of the following: an error, a warning,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L11" href="#L11">11</a><span class="code-text">a note, or a help message.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L12" href="#L12">12</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L13" href="#L13">13</a><span class="code-text">An `error` is emitted when the compiler detects a problem that makes it unable</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L14" href="#L14">14</a><span class="code-text"> to compile the program, either because the program is invalid or the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L15" href="#L15">15</a><span class="code-text"> programmer has decided to make a specific `warning` into an error.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L16" href="#L16">16</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L17" href="#L17">17</a><span class="code-text">A `warning` is emitted when the compiler detects something odd about a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L18" href="#L18">18</a><span class="code-text">program. For instance, dead code and unused `Result` values.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L19" href="#L19">19</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L20" href="#L20">20</a><span class="code-text">A `help` message is emitted following an `error` or `warning` to give additional</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L21" href="#L21">21</a><span class="code-text">information to the user about how to solve their problem.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L22" href="#L22">22</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L23" href="#L23">23</a><span class="code-text">A `note` is emitted to identify additional circumstances and parts of the code</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L24" href="#L24">24</a><span class="code-text">that caused the warning or error. For example, the borrow checker will note any</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L25" href="#L25">25</a><span class="code-text">previous conflicting borrows.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L26" href="#L26">26</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L27" href="#L27">27</a><span class="code-text">* Write in plain simple English. If your message, when shown on a – possibly</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L28" href="#L28">28</a><span class="code-text">small – screen (which hasn't been cleaned for a while), cannot be understood</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L29" href="#L29">29</a><span class="code-text">by a normal programmer, who just came out of bed after a night partying, it's</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L30" href="#L30">30</a><span class="code-text">too complex.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L31" href="#L31">31</a><span class="code-text">* `Errors` and `Warnings` should not suggest how to fix the problem. A `Help`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L32" href="#L32">32</a><span class="code-text">message should be emitted instead.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L33" href="#L33">33</a><span class="code-text">* `Error`, `Warning`, `Note`, and `Help` messages start with a lowercase</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L34" href="#L34">34</a><span class="code-text">letter and do not end with punctuation.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L35" href="#L35">35</a><span class="code-text">* Error messages should be succinct. Users will see these error messages many</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L36" href="#L36">36</a><span class="code-text">times, and more verbose descriptions can be viewed with the `--explain` flag.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L37" href="#L37">37</a><span class="code-text">That said, don't make it so terse that it's hard to understand.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L38" href="#L38">38</a><span class="code-text">* The word &quot;illegal&quot; is illegal. Prefer &quot;invalid&quot; or a more specific word</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L39" href="#L39">39</a><span class="code-text">instead.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L40" href="#L40">40</a><span class="code-text">* Errors should document the span of code where they occur – the `span_..`</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L41" href="#L41">41</a><span class="code-text">methods allow to easily do this. Also `note` other spans that have contributed</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L42" href="#L42">42</a><span class="code-text">to the error if the span isn't too large.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L43" href="#L43">43</a><span class="code-text">* When emitting a message with span, try to reduce the span to the smallest</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L44" href="#L44">44</a><span class="code-text">amount possible that still signifies the issue</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L45" href="#L45">45</a><span class="code-text">* Try not to emit multiple error messages for the same error. This may require</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L46" href="#L46">46</a><span class="code-text">detecting duplicates.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L47" href="#L47">47</a><span class="code-text">* When the compiler has too little information for a specific error message,</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L48" href="#L48">48</a><span class="code-text">lobby for annotations for library code that allow adding more. For example see</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L49" href="#L49">49</a><span class="code-text">`#[on_unimplemented]`. Use these annotations when available!</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L50" href="#L50">50</a><span class="code-text">* Keep in mind that Rust's learning curve is rather steep, and that the</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L51" href="#L51">51</a><span class="code-text">compiler messages are an important learning tool.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L52" href="#L52">52</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L53" href="#L53">53</a><span class="code-text">## Error Explanations</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L54" href="#L54">54</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L55" href="#L55">55</a><span class="code-text">Error explanations are long form descriptions of error messages provided with</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L56" href="#L56">56</a><span class="code-text">the compiler. They are accessible via the `--explain` flag. Each explanation</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L57" href="#L57">57</a><span class="code-text">comes with an example of how to trigger it and advice on how to fix it.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L58" href="#L58">58</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L59" href="#L59">59</a><span class="code-text">Please read [RFC 1567](https://github.com/rust-lang/rfcs/blob/master/text/1567-long-error-codes-explanation-normalization.md)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L60" href="#L60">60</a><span class="code-text">for details on how to format and write long error codes.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L61" href="#L61">61</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L62" href="#L62">62</a><span class="code-text">* All of them are accessible [online](http://doc.rust-lang.org/error-index.html),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L63" href="#L63">63</a><span class="code-text">  which are auto-generated from rustc source code in different places:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L64" href="#L64">64</a><span class="code-text">  [librustc](https://github.com/rust-lang/rust/blob/master/src/librustc/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L65" href="#L65">65</a><span class="code-text">  [librustc_ast](https://github.com/rust-lang/rust/blob/master/src/librustc_ast/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L66" href="#L66">66</a><span class="code-text">  [librustc_borrowck](https://github.com/rust-lang/rust/blob/master/src/librustc_borrowck/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L67" href="#L67">67</a><span class="code-text">  [librustc_metadata](https://github.com/rust-lang/rust/blob/master/src/librustc_metadata/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L68" href="#L68">68</a><span class="code-text">  [librustc_mir](https://github.com/rust-lang/rust/blob/master/src/librustc_mir/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L69" href="#L69">69</a><span class="code-text">  [librustc_passes](https://github.com/rust-lang/rust/blob/master/src/librustc_passes/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L70" href="#L70">70</a><span class="code-text">  [librustc_privacy](https://github.com/rust-lang/rust/blob/master/src/librustc_privacy/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L71" href="#L71">71</a><span class="code-text">  [librustc_resolve](https://github.com/rust-lang/rust/blob/master/src/librustc_resolve/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L72" href="#L72">72</a><span class="code-text">  [librustc_codegen_llvm](https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L73" href="#L73">73</a><span class="code-text">  [librustc_plugin_impl](https://github.com/rust-lang/rust/blob/master/src/librustc_plugin/error_codes.rs),</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L74" href="#L74">74</a><span class="code-text">  [librustc_typeck](https://github.com/rust-lang/rust/blob/master/src/librustc_typeck/error_codes.rs).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L75" href="#L75">75</a><span class="code-text">* Explanations have full markdown support. Use it, especially to highlight</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L76" href="#L76">76</a><span class="code-text">code with backticks.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L77" href="#L77">77</a><span class="code-text">* When talking about the compiler, call it `the compiler`, not `Rust` or</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L78" href="#L78">78</a><span class="code-text">`rustc`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L79" href="#L79">79</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L80" href="#L80">80</a><span class="code-text">## Compiler Flags</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L81" href="#L81">81</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L82" href="#L82">82</a><span class="code-text">* Flags should be orthogonal to each other. For example, if we'd have a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L83" href="#L83">83</a><span class="code-text">json-emitting variant of multiple actions `foo` and `bar`, an additional</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L84" href="#L84">84</a><span class="code-text">--json flag is better than adding `--foo-json` and `--bar-json`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L85" href="#L85">85</a><span class="code-text">* Always give options a long descriptive name, if only for more</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L86" href="#L86">86</a><span class="code-text">understandable compiler scripts.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L87" href="#L87">87</a><span class="code-text">* The `--verbose` flag is for adding verbose information to `rustc` output</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L88" href="#L88">88</a><span class="code-text">when not compiling a program. For example, using it with the `--version` flag</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L89" href="#L89">89</a><span class="code-text">gives information about the hashes of the code.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L90" href="#L90">90</a><span class="code-text">* Experimental flags and options must be guarded behind the `-Z unstable-options` flag.</span></span></pre></div></div>
</main>
</body>
</html>
