]> git.proxmox.com Git - rustc.git/blob - vendor/codespan-reporting/examples/term.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / vendor / codespan-reporting / examples / term.rs
1 use structopt::StructOpt;
2
3 use codespan::Files;
4 use codespan_reporting::diagnostic::{Diagnostic, Label};
5 use codespan_reporting::term::termcolor::StandardStream;
6 use codespan_reporting::term::{emit, ColorArg};
7
8 #[derive(Debug, StructOpt)]
9 #[structopt(name = "emit")]
10 pub struct Opts {
11 /// Configure coloring of output
12 #[structopt(
13 long = "color",
14 parse(try_from_str),
15 default_value = "auto",
16 raw(possible_values = "ColorArg::VARIANTS", case_insensitive = "true")
17 )]
18 pub color: ColorArg,
19 }
20
21 fn main() {
22 let opts = Opts::from_args();
23 let mut files = Files::new();
24
25 let file_id1 = files.add(
26 "Data/Nat.fun",
27 unindent::unindent(
28 "
29 module Data.Nat where
30
31 data Nat : Type where
32 zero : Nat
33 succ : Nat → Nat
34
35 {-# BUILTIN NATRAL Nat #-}
36
37 infixl 6 _+_ _-_
38
39 _+_ : Nat → Nat → Nat
40 zero + n₂ = n₂
41 succ n₁ + n₂ = succ (n₁ + n₂)
42
43 _-_ : Nat → Nat → Nat
44 n₁ - zero = n₁
45 zero - succ n₂ = zero
46 succ n₁ - succ n₂ = n₁ - n₂
47 ",
48 ),
49 );
50
51 let file_id2 = files.add(
52 "Test.fun",
53 unindent::unindent(
54 r#"
55 module Test where
56
57 _ : Nat
58 _ = 123 + "hello"
59 "#,
60 ),
61 );
62
63 let file_id3 = files.add(
64 "FizzBuzz.fun",
65 unindent::unindent(
66 r#"
67 module FizzBuzz where
68
69 fizz₁ : Nat → String
70 fizz₁ num = case (mod num 5) (mod num 3) of
71 0 0 => "FizzBuzz"
72 0 _ => "Fizz"
73 _ 0 => "Buzz"
74 _ _ => num
75
76 fizz₂ num =
77 case (mod num 5) (mod num 3) of
78 0 0 => "FizzBuzz"
79 0 _ => "Fizz"
80 _ 0 => "Buzz"
81 _ _ => num
82 "#,
83 ),
84 );
85
86 let diagnostics = [
87 // Unknown builtin error
88 Diagnostic::new_error(
89 "unknown builtin: `NATRAL`",
90 Label::new(file_id1, 96..102, "unknown builtin"),
91 )
92 .with_notes(vec![
93 "there is a builtin with a similar name: `NATURAL`".to_owned()
94 ]),
95 // Unused parameter warning
96 Diagnostic::new_warning(
97 "unused parameter pattern: `n₂`",
98 Label::new(file_id1, 285..289, "unused parameter"),
99 )
100 .with_notes(vec!["consider using a wildcard pattern: `_`".to_owned()]),
101 // Unexpected type error
102 Diagnostic::new_error(
103 "unexpected type in application of `_+_`",
104 Label::new(file_id2, 37..44, "expected `Nat`, found `String`"),
105 )
106 .with_code("E0001")
107 .with_secondary_labels(vec![Label::new(
108 file_id1,
109 130..155,
110 "based on the definition of `_+_`",
111 )]),
112 // Incompatible match clause error
113 Diagnostic::new_error(
114 "`case` clauses have incompatible types",
115 Label::new(file_id3, 163..166, "expected `String`, found `Nat`"),
116 )
117 .with_code("E0308")
118 .with_notes(vec![unindent::unindent(
119 "
120 expected type `String`
121 found type `Nat`
122 ",
123 )])
124 .with_secondary_labels(vec![
125 Label::new(file_id3, 62..166, "`case` clauses have incompatible types"),
126 Label::new(file_id3, 41..47, "expected type `String` found here"),
127 ]),
128 // Incompatible match clause error
129 Diagnostic::new_error(
130 "`case` clauses have incompatible types",
131 Label::new(file_id3, 303..306, "expected `String`, found `Nat`"),
132 )
133 .with_code("E0308")
134 .with_notes(vec![unindent::unindent(
135 "
136 expected type `String`
137 found type `Nat`
138 ",
139 )])
140 .with_secondary_labels(vec![
141 Label::new(file_id3, 186..306, "`case` clauses have incompatible types"),
142 Label::new(file_id3, 233..243, "this is found to be of type `String`"),
143 Label::new(file_id3, 259..265, "this is found to be of type `String`"),
144 Label::new(file_id3, 281..287, "this is found to be of type `String`"),
145 ]),
146 ];
147
148 let writer = StandardStream::stderr(opts.color.into());
149 let config = codespan_reporting::term::Config::default();
150 for diagnostic in &diagnostics {
151 emit(&mut writer.lock(), &config, &files, &diagnostic).unwrap();
152 }
153 }